Repository navigation
tex-expansion: memoize the trailing-space lexer probe (fuzz-found hang) - #818
Merged
Merged
Conversation
The mutation fuzzer (crates/compiler/tests/fuzz_compile.rs) found a real
hang: fuzz case seeded from fixtures/real-world/listings-manual/main.tex
with mutations ["splice-lines", "long-control-sequence"] never finished.
Root cause: `Lexer::rest_of_line_blank` (lexer.rs) rescanned forward to
the end of the physical line on every call, and `next_token` calls it
once per space character. The compiler's `prepare()` blanks `lstlisting`
bodies (including their newlines) to spaces for the engine, producing one
long space-only "line" for any sizeable listing. Lexing N spaces this way
costs O(N^2): a 100KB blanked lstlisting body is ~1e10 byte comparisons.
Measured scaling pre-fix (debug build): N=2000 -> 17ms, 4000 -> 55ms,
8000 -> 220ms, 16000 -> 870ms (~4x per doubling); 100000 extrapolates to
~35s, past the fuzzer's 20s timeout. Confirmed with lldb while stuck:
rest_of_line_blank (lexer.rs) <- next_token <- Engine::next_raw_unchecked
<- drive (incremental.rs).
Fix: memoize the probe as `blank_probe: Option<(probed_from, floor,
blank)>` on `Lexer`. A probe from inside a previously-scanned
`probed_from..=floor` range reuses the cached answer; a probe from past
`floor` rescans. Scanned ranges across rescans are disjoint, so a whole
line costs O(line length) however many times it's asked, and token output
is byte-identical (same predicate, just cached).
New regression test `a_very_long_blank_run_lexes_in_linear_time`
(crates/compiler/tests/robustness.rs): a 200k-space run and the minimized
100KB lstlisting shape both complete well under the old hang threshold.
Separately investigated the fuzzer's "crash ?" findings (5 occurrences
pre-fix, at worker-chunk starts 0/500/1000/1500 plus 1004 = restart after
the hang): these are NOT compiler bugs. They are a fuzz-harness
stdout-parsing artifact — the supervisor spawns each worker without `-q`,
so libtest's unterminated "test fuzz_compile_mutations ... " line
concatenates with the worker's own first "START\t<N>" line on one
stdout line, which the supervisor's line matcher then misreads as
Outcome::Crash("?"). Verified independently: every flagged index replays
`OK` in milliseconds through the exact worker path
(FLASHTEX_FUZZ_WORKER=<idx>..<idx+1>), including case 0's two-document
shape byte-identical to the saved finding files. A real process crash
would report as `crash stack overflow` / `crash <status>`, never
`crash ?`. Fixing the artifact needs a change in fuzz_compile.rs/
fuzz_support/mod.rs (e.g. spawning workers with -q), which is
deliberately out of scope here to keep this PR to the compiler-side fix;
noted for whoever owns the fuzzer next. Post-fix, two full sweeps
(FLASHTEX_FUZZ_CASES=3000 default) report zero hangs and zero panics, the
same 4 chunk-start artifacts, and zero real crashes.
Tests: `cargo test` in crates/tex-expansion (independently re-run):
clean, 0 failed. `cargo test` in crates/compiler (independently re-run):
clean, 0 failed, including the new regression test at 0.07s.
Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane fuzz-crash-hang-sweep, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet) — independently re-verified the harness-artifact claim (reproduced the concatenated START line directly) and both full test suites
Co-authored-by: d-q222 <[email protected]>
Contributor
Author
|
Commander (daniel-parent): independent Opus review of PR #818 review (head cee6611)tex-expansion: memoize the trailing-space lexer probe (fuzz-found hang) Material findingsNone. Analysis
Non-blocking notes
Tests run (crate dirs, nice, CARGO_BUILD_JOBS=3, perl-alarm timeout)
VERDICT: APPROVE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Assigned crash/hang fuzz sweep. Ran the existing mutation fuzzer (
crates/compiler/tests/fuzz_compile.rs) and found one real hang and (initially, misleadingly) some "crash" findings.The hang (real bug, fixed):
Lexer::rest_of_line_blankrescanned to the end of the physical line on every call, andnext_tokencalls it once per space character. The compiler blankslstlistingbodies to spaces for the engine, so a sizeable listing becomes one long space-only line — O(N²) lexing, extrapolating to ~35s for a 100KB body, past the fuzzer's timeout. Confirmed withlldbwhile stuck. Fixed by memoizing the probe (blank_probeonLexer): a query inside a previously-scanned range reuses the cached answer, so a whole line costs O(line length) regardless of query count. Token output is byte-identical by construction.The "crash ?" findings (investigated, NOT a compiler bug): these are a fuzz-harness stdout-parsing artifact, not real crashes. The supervisor spawns workers without
-q, so libtest's unterminatedtest ... ...line concatenates with the worker's own firstSTART\t<N>line on one stdout line, which the supervisor's line matcher misreads asOutcome::Crash("?"). Verified independently: every flagged index replays cleanOKin milliseconds through the exact worker path, including case 0's two-document shape byte-identical to the saved finding. A real crash reportscrash stack overflow/crash <status>, nevercrash ?. The fix belongs infuzz_compile.rs/fuzz_support/mod.rs(both off-limits to this PR's scope) — noted for whoever owns the fuzzer next; post-fix sweeps show zero hangs, zero panics, zero real crashes, just the same 4 harness artifacts.Tests
New regression test
a_very_long_blank_run_lexes_in_linear_time. Fullcargo testin bothcrates/tex-expansionandcrates/compilerclean (independently re-run by the supervisor twice, including post-rebase). The artifact claim was independently reproduced by the supervisor directly (captured the literal concatenatedSTARTline).Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane fuzz-crash-hang-sweep, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
🤖 Generated with Claude Code