diff --git a/internal/cbm/lsp/rust_lsp.c b/internal/cbm/lsp/rust_lsp.c index 4d08172202..ff520f1ecf 100644 --- a/internal/cbm/lsp/rust_lsp.c +++ b/internal/cbm/lsp/rust_lsp.c @@ -3456,13 +3456,16 @@ static bool macro_pattern_match(const char *pat, int pat_len, const char *in, in int name_end = pp; if (name_end == name_start) return false; + /* The fragment specifier is copied into this frame, not a static + * buffer: parallel extraction runs this matcher on several workers + * at once, and a shared buffer let one worker's specifier replace + * another's before macro_consume_fragment read it. */ + char frag_buf[16]; const char *frag = "tt"; if (pp < pat_len && pat[pp] == ':') { pp++; int frag_start = pp; pp = macro_consume_ident(pat, pat_len, pp); - /* Copy frag into a small buffer. */ - static char frag_buf[16]; int fl = pp - frag_start; if (fl > 0 && fl < (int)sizeof(frag_buf)) { memcpy(frag_buf, pat + frag_start, fl); diff --git a/tests/test_parallel.c b/tests/test_parallel.c index 7906b92c6b..8e78a8f9c0 100644 --- a/tests/test_parallel.c +++ b/tests/test_parallel.c @@ -18,9 +18,12 @@ #include "discover/discover.h" #include "foundation/platform.h" #include "foundation/compat_fs.h" +#include "foundation/compat_thread.h" #include "foundation/log.h" #include "foundation/mem.h" +#include "foundation/slab_alloc.h" #include "cbm.h" +#include "helpers.h" /* cbm_kind_in_set_free_cache */ #include "result_spill.h" #include @@ -1289,6 +1292,89 @@ TEST(parallel_full_index_keeps_edge_ids_unique) { PASS(); } +/* Rust macro_rules! expansion runs inside per-file extraction, which the + * parallel route executes on several workers at once. Each thread extracts its + * own file repeatedly, with typed fragments ($n:ident, $e:expr) in a different + * order per thread, and must resolve its expansion to its own target every + * time. + * + * What this does NOT prove today: the pattern matcher's fragment specifier used + * to live in a process-wide static buffer, but invocation arguments are not + * captured on this path yet (fragment substitution does not apply), so the + * matcher never reaches that buffer and the TSan lane cannot observe the old + * race. The expansion still resolves through the first-rule fallback. Once + * argument capture works, this test runs the matcher concurrently and the TSan + * lane (CBM_WORKERS=4, halt_on_error) is the one that would catch a shared + * buffer. */ +enum { RUST_MACRO_THREADS = 4, RUST_MACRO_ROUNDS = 25 }; + +typedef struct { + char source[256]; + char target[32]; + int resolved; +} rust_macro_job_t; + +static void *rust_macro_extract_worker(void *arg) { + rust_macro_job_t *job = arg; + for (int round = 0; round < RUST_MACRO_ROUNDS; round++) { + CBMFileResult *r = cbm_extract_file(job->source, (int)strlen(job->source), CBM_LANG_RUST, + "test", "src/main.rs", 0, NULL, NULL); + if (!r) { + continue; + } + for (int i = 0; i < r->resolved_calls.count; i++) { + const CBMResolvedCall *rc = &r->resolved_calls.items[i]; + if (rc->caller_qn && strstr(rc->caller_qn, "run") && rc->callee_qn && + strstr(rc->callee_qn, job->target)) { + job->resolved++; + break; + } + } + cbm_free_result(r); + } + /* Exit the way a pool worker does (pass_parallel.c): the parser, cursors, + * field-id cache, work arena, slab and node-type cache are thread-locals, + * and a thread that ends without releasing them leaks them (LSan). */ + cbm_destroy_thread_parser(); + cbm_work_arena_release(); + cbm_slab_destroy_thread(); + cbm_kind_in_set_free_cache(); + return NULL; +} + +TEST(parallel_rust_macro_fragments_do_not_share_state) { + rust_macro_job_t jobs[RUST_MACRO_THREADS]; + for (int t = 0; t < RUST_MACRO_THREADS; t++) { + memset(&jobs[t], 0, sizeof(jobs[t])); + snprintf(jobs[t].target, sizeof(jobs[t].target), "target_%d", t); + /* Alternate the fragment order so concurrent matches parse + * different specifiers at the same moment. */ + snprintf(jobs[t].source, sizeof(jobs[t].source), + (t % 2) ? "macro_rules! m { ($e:expr, $n:ident) => { target_%d($e) } }\n" + "fn target_%d(x: i32) -> i32 { x }\n" + "fn run() -> i32 { m!(1, unused) }\n" + : "macro_rules! m { ($n:ident, $e:expr) => { target_%d($e) } }\n" + "fn target_%d(x: i32) -> i32 { x }\n" + "fn run() -> i32 { m!(unused, 1) }\n", + t, t); + } + cbm_thread_t threads[RUST_MACRO_THREADS]; + bool started[RUST_MACRO_THREADS]; + for (int t = 0; t < RUST_MACRO_THREADS; t++) { + started[t] = cbm_thread_create(&threads[t], 0, rust_macro_extract_worker, &jobs[t]) == 0; + } + for (int t = 0; t < RUST_MACRO_THREADS; t++) { + if (started[t]) { + cbm_thread_join(&threads[t]); + } + } + for (int t = 0; t < RUST_MACRO_THREADS; t++) { + ASSERT_TRUE(started[t]); + ASSERT_EQ(jobs[t].resolved, RUST_MACRO_ROUNDS); + } + PASS(); +} + /* The closure-repair route always runs the parallel phases; both of its * syncs go through the same helper. */ TEST(parallel_closure_repair_syncs_ids_through_the_helper) { @@ -6226,6 +6312,7 @@ SUITE(parallel) { RUN_TEST(gbuf_next_id_accessors); RUN_TEST(gbuf_raise_next_id_never_lowers); RUN_TEST(parallel_full_index_keeps_edge_ids_unique); + RUN_TEST(parallel_rust_macro_fragments_do_not_share_state); RUN_TEST(parallel_closure_repair_syncs_ids_through_the_helper); /* Parallel pipeline parity tests */