From 7299ddcf0e381eb67420f1baddb8527ae1324578 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 6 Dec 2026 16:36:18 +0100 Subject: [PATCH 1/2] fix(rust-lsp): keep the macro fragment specifier in the matcher's frame macro_pattern_match copied a metavariable's fragment specifier (ident, expr, ...) into a function-static buffer and passed a pointer to it on to macro_consume_fragment. Parallel extraction runs the matcher on several workers at once, so one worker could overwrite another's specifier before it was read. The buffer now lives beside the pointer that uses it. The race is latent today: invocation arguments are not captured on the extraction path yet, so the matcher never reaches the buffer and neither the ASan nor the TSan lane can observe it. The new parallel test extracts typed- fragment macros on four threads and checks every expansion resolves; it will exercise the matcher concurrently once argument capture works. Signed-off-by: Martin Vogel --- internal/cbm/lsp/rust_lsp.c | 7 +++- tests/test_parallel.c | 78 +++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/internal/cbm/lsp/rust_lsp.c b/internal/cbm/lsp/rust_lsp.c index be040f7f8f..673a11059b 100644 --- a/internal/cbm/lsp/rust_lsp.c +++ b/internal/cbm/lsp/rust_lsp.c @@ -3428,13 +3428,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..f04b54389b 100644 --- a/tests/test_parallel.c +++ b/tests/test_parallel.c @@ -18,6 +18,7 @@ #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 "cbm.h" @@ -1289,6 +1290,82 @@ 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); + } + 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 +6303,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 */ From 7912265c2c5724d3795ed6f3a054030053a520a8 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 6 Dec 2026 18:50:32 +0100 Subject: [PATCH 2/2] test(parallel): release the Rust macro workers' thread state on exit The threads of parallel_rust_macro_fragments_do_not_share_state call cbm_extract_file directly and ended without releasing the extraction thread-locals, so LeakSanitizer on the Linux GCC leg reported 124,768 bytes in 132 allocations (field-id cache, tree cursor, slab, arena). They now exit the way a pool worker does in pass_parallel.c. Signed-off-by: Martin Vogel --- tests/test_parallel.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_parallel.c b/tests/test_parallel.c index f04b54389b..8e78a8f9c0 100644 --- a/tests/test_parallel.c +++ b/tests/test_parallel.c @@ -21,7 +21,9 @@ #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 @@ -1330,6 +1332,13 @@ static void *rust_macro_extract_worker(void *arg) { } 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; }