Skip to content

tex-expansion: memoize the trailing-space lexer probe (fuzz-found hang) - #818

Merged
d-q222 merged 1 commit into
mainfrom
agent/daniel-muse-lead/fuzz-crash-hang-sweep
Sep 17, 2026
Merged

d-q222 merged 1 commit into
mainfrom
agent/daniel-muse-lead/fuzz-crash-hang-sweep

Conversation

@d-q222

@d-q222 d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

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_blank rescanned to the end of the physical line on every call, and next_token calls it once per space character. The compiler blanks lstlisting bodies 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 with lldb while stuck. Fixed by memoizing the probe (blank_probe on Lexer): 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 unterminated test ... ... line concatenates with the worker's own first START\t<N> line on one stdout line, which the supervisor's line matcher misreads as Outcome::Crash("?"). Verified independently: every flagged index replays clean OK in milliseconds through the exact worker path, including case 0's two-document shape byte-identical to the saved finding. A real crash reports crash stack overflow/crash <status>, never crash ?. The fix belongs in fuzz_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. Full cargo test in both crates/tex-expansion and crates/compiler clean (independently re-run by the supervisor twice, including post-rebase). The artifact claim was independently reproduced by the supervisor directly (captured the literal concatenated START line).

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

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]>
@d-q222

d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Commander (daniel-parent): independent Opus review of cee66115, APPROVE. The quadratic cost is confirmed: 152 s on the old code vs 0.08 s now. Merging in batch T.

PR #818 review (head cee6611)

tex-expansion: memoize the trailing-space lexer probe (fuzz-found hang)

Material findings

None.

Analysis

  • Complexity claim is real. next_token calls rest_of_line_blank for every
    literal space byte (lexer.rs:276), whatever the state. When the run of spaces is
    followed by a non-space on the same line, the probe returns false and the lexer
    goes on one space at a time, so the old code rescanned the whole remaining run
    each time: O(N^2). Measured: the new test on the base lexer took 152 s in debug and
    failed the 30 s assertion. On the PR it takes 0.08 s.
  • The memo is correct. The probe only compares raw bytes (' ', \n, \r) and never
    looks at the catcode table or \endlinechar, which matches the old take_while.
    A \catcode/\makeatletter change mid-line therefore cannot make a cached result
    stale: the old result did not depend on catcodes either.
  • Invariants hold: src is only set in new/resume, and both reset
    blank_probe: None. pos is private and only moves forward (+=,
    finish() sets it to len). For any pos in probed_from..=floor, the bytes
    pos..floor are all spaces, so the answer equals the cached one. That includes
    pos == floor: a non-space byte gives false, and a newline or EOF gives true.
    If pos < probed_from, it rescans. Clone (the incremental checkpoint) copies
    src, pos and memo together, so they stay consistent.
  • Rescans after floor cover disjoint ranges, so a line costs O(len) in total.

Non-blocking notes

  1. robustness.rs:578: on a regression the test does not hang, but it takes
    about 2.5 min in debug before it fails. It is bounded because the work is O(N^2)
    and finite. You could cut the run to 100_000 spaces (about 4x faster to fail)
    and keep the 30 s bound.
  2. The second (lstlisting, 100KB of x) half is unverified as a regression
    guard: on the old lexer the first assertion fails before it runs. It also passes
    no assertion on messages. That is harmless, but the "fuzz shape itself" comment
    overstates what it proves.
  3. The doc comment says "a probe from past floor rescans, so the scanned ranges
    ... are disjoint". That is accurate. Ranges from a rescan that starts before
    probed_from could overlap, but that cannot happen.

Tests run (crate dirs, nice, CARGO_BUILD_JOBS=3, perl-alarm timeout)

  • compiler --test robustness a_very_long_blank_run: PR ok (0.08 s). With the base
    lexer.rs swapped in: FAILED, "took 152.05s" (confirms the regression guard).
  • tex-expansion --test expand_tests: 86 passed (includes
    trailing_spaces_are_stripped_before_endlinechar).
  • tex-expansion --test incremental_tests: 4 passed (resume/checkpoint path).
  • tex-expansion has no lexer unit tests in src/lexer.rs.

VERDICT: APPROVE

@d-q222
d-q222 merged commit 19f6620 into main Sep 17, 2026
20 of 22 checks passed
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