Skip to content

fix(rust-lsp): keep the macro fragment specifier in the matcher's frame - #3214

Open
DeusData wants to merge 3 commits into
mainfrom
fix/rust-macro-frag-buf-race
Open

DeusData wants to merge 3 commits into
mainfrom
fix/rust-macro-frag-buf-race

Conversation

@DeusData

@DeusData DeusData commented Dec 6, 2026

Copy link
Copy Markdown
Owner

What

macro_pattern_match (internal/cbm/lsp/rust_lsp.c) copied a macro_rules! metavariable's fragment specifier (ident, expr, ...) into a function-static char frag_buf[16] and passed a pointer to it on to macro_consume_fragment. The buffer now lives in the matcher's own frame, beside the frag pointer that uses it.

Why

Parallel extraction runs this matcher on several workers at once. With a static buffer, one worker can overwrite another's specifier between the copy and the read, which changes how a fragment is consumed and makes macro resolution nondeterministic.

How sure are we, honestly

The race is latent today. Invocation arguments are not captured on the extraction path yet (fragment substitution does not apply: a probe macro ($f:ident) => { $f() } invoked as m!(target) resolves nothing), so the matcher never reaches the buffer. We confirmed this the hard way. With the static restored and the TSan runner rebuilt from scratch, the new test still passes, so neither the ASan nor the TSan lane can observe the old race. The fix is still the right one, because the code is wrong as written and the next change to argument capture would make it live.

Test

parallel_rust_macro_fragments_do_not_share_state (suite parallel, which the TSan lane runs with CBM_WORKERS=4 and halt_on_error). Four threads each extract a Rust file with typed fragments ($n:ident, $e:expr, in a different order per thread) 25 times and must resolve the expansion to their own target every time. Its comment says plainly what it does not prove yet. Once argument capture works, it runs the matcher concurrently and the TSan lane is the one that would catch a shared buffer.

Local: parallel + rust_lsp suites, 615 passed (ASan+UBSan); TSan runner clean with the fix.

Side finding (not fixed here)

macro_rules! fragment substitution never takes effect on the extraction path, apparently because the invocation's argument token tree is looked up by a field name the Rust grammar does not use for macro_invocation (unverified; to be investigated separately). Calls written through substituted fragments therefore don't resolve.

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 <martin.vogel.tech@gmail.com>
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
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 <martin.vogel.tech@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant