Skip to content

fix(rust): bound a program's compile by its length, for deep nesting and for regex literals - #177

Merged
kommander merged 1 commit into
devfrom
compile-bound
Dec 2, 2026
Merged

kommander merged 1 commit into
devfrom
compile-bound

Conversation

@simonklee

@simonklee simonklee commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What

  • Add run::MAX_COMPILE_LEVELS (65,536). run::compile already reads a bound on a program's nesting from its bytes (nesting::bound) to size the thread it parses on. A program bounded past MAX_COMPILE_LEVELS is now refused there, before it is parsed, with a ParseError: "SyntaxError: Program is nested too deeply to compile."
  • A regular expression literal is checked when its program compiles, and no longer compiled. builtins::regexp::validate_literal checks the flags and the syntax (regexp_syntax::check, as before) and stops there. It used to compile the pattern with regress too, and drop the result. The program compiles the literal where it evaluates it, as it always did (Op::RegExp), and there a run's limits apply. A program compiles outside any run, so nothing could end that compile.
  • The syntax check takes time by the pattern's length (regexp_syntax.rs). Two things in it did not:
    • Named groups. Each group and each | copied the set of names seen so far, to know which names an alternative may use again. Names now keeps each alternative as a set that a closing group joins to the one it is in (a union-find), and each name with the set of its last use. A use is one lookup, and nothing is copied. The rule is the same.
    • Property escapes. The check asked regress about each \p{...} by compiling it, at every place a pattern names it. It now asks once per name. These lines are identical in fix(rust): the run's limits stop a regular expression that backtracks without end #80, so the two PRs merge in either order.
  • One difference in behaviour. A literal whose syntax is valid and which regress refuses used to fail the program's compile. Now the program compiles, and the literal throws its SyntaxError when it is evaluated. The cases found: more than 65,535 loops (a* 80,000 times), groups nested 60,000 deep, classes nested 120,000 deep under v. test262 is unchanged: language/literals/regexp 214 of 214 and built-ins/RegExp 935 of 935 pass.
  • Document both in rust/README.md under "Design".

Why

The parser (oxc) has no guard and recurses once per level of syntax, so run::compile gives it a thread with the stack the bound asks for: 1 KiB a level in a release build, 16 KiB in a debug build. A hostile source then costs stack, memory and time by its depth, only to be refused once parsed: the compiler takes 2,000 levels. One shape was not refused at all. Nested parentheses leave no depth in the tree, so 524,288 of them compiled.

Release build, time and peak memory of one Runtime::compile. The 1 MiB columns need #176, with max_source_bytes raised:

Source 256 KiB before 256 KiB after 1 MiB before 1 MiB after
{ repeated 132 ms, 261 MiB 5 ms, 20 MiB 576 ms, 961 MiB 14 ms, 47 MiB
[ repeated 101 ms, 196 MiB 7 ms, 20 MiB 361 ms, 705 MiB 14 ms, 47 MiB
a template's ${ repeated 84 ms, 162 MiB 2 ms, 12 MiB 281 ms, 621 MiB 5 ms, 23 MiB
f( repeated 79 ms, 143 MiB 3 ms, 14 MiB 262 ms, 513 MiB 11 ms, 27 MiB
blocks, opened and closed 83 ms, 135 MiB 3 ms, 14 MiB 271 ms, 486 MiB 7 ms, 27 MiB
parentheses around 1 46 ms, 107 MiB, compiled 3 ms, 14 MiB 185 ms, 376 MiB, compiled 8 ms, 27 MiB
labels, arrows, !, a?, +1, .b, if(1), new 7 to 48 ms, 15 to 105 MiB 1 to 2 ms, 6 MiB 20 to 170 ms, 40 to 393 MiB 3 to 7 ms, 7 MiB

Every one of them is now refused in the time of the scan. What is left of the memory is the scan's own (a frame for each open group). Wide is not deep: an array of 524,288 elements, as many statements, and an object of 262,144 keys compile as before (58 to 107 ms and 73 to 139 MiB at 1 MiB).

The bound is far above what compiles. It counts a level for each token of the longest item at each depth, so it is larger than a program's depth, and the compiler refuses a tree past 2,000 levels. Of 35 minified bundles (44 KB to 1.1 MB: three, katex, lodash, axe, chevrotain, js-yaml and others), the deepest was bounded at 591, a hundredth of the cap.

Regular expression literals

The OpenCode Rust client compiles plugins on one worker thread, so a program that is slow to compile holds up the load of every other plugin. Release build, one Runtime::compile of a program that is one literal, within the default source limit of 256 KiB:

The literal (size of the source) dev This PR
3,000 named groups, then 15,000 () (62 KB) 7.8 s 2.6 ms
3,000 named groups, then a group of 15,000 alternatives (62 KB) 8.8 s 2.2 ms
10,000 named groups, then 30,000 (?:) (229 KB) 74 s 7.7 ms
10,000 named groups, then a group of 65,000 alternatives (239 KB) over 120 s 8.1 ms
6,000 named groups inside 30,000 groups of two alternatives (245 KB) 30.9 s and 2.8 GiB, then refused 7.2 ms
\p{RGI_Emoji} 19,000 times, v (247 KB) killed at 6 GiB 5.6 ms, 9 MiB
\p{Lu} 41,000 times, iu (246 KB) 10.0 s 4.9 ms
ab| 83,000 times (249 KB) stack overflow: the process aborts 3.6 ms
a name in each of 20,000 alternatives; one name in each of 28,000 stack overflow: the process aborts 10.6 ms; 6.7 ms
(?<=a) 40,000 times (240 KB) 1.1 s 4.2 ms
[\S\W] 40,000 times, iv (240 KB) 0.81 s 6.6 ms
\p{L} 50,000 times, u (250 KB) 0.65 s, 537 MiB 4.6 ms, 8 MiB
(?=a); \S under iu; \k<a>; a{1,2}; [\u{0}-\u{10FFFF}] under iu (250 KB each) 0.27 to 0.66 s 2.5 to 5.1 ms
an ordinary program: 4,000 literals of 50 bytes (238 KB) 1.9 s 36 ms

With this PR, each of the 26 hostile literals tried is done in 1.4 to 11 ms, in 11 MiB at most.

With a source limit of 1 MiB. "Before" is #176 alone. "After" is this PR with #176, #80, native-fuel (not opened) and gc-bytes (not opened):

The literal (1 MiB of source) Before After
[\S\W] 170,000 times, iv 3.1 s 17 ms
\p{RGI_Emoji} 80,000 times, v killed at 6 GiB 17 ms, 12 MiB
ab| 340,000 times stack overflow: the process aborts 14 ms
30,000 named groups, then 170,000 (?:) over 90 s 32 ms
30,000 named groups, then a group of 340,000 alternatives; 20,000 inside 120,000 groups not run 31 ms; 26 ms
an ordinary program: 16,000 literals of 50 bytes 6.6 s 154 ms

The last row of each table is not hostile: a program with many small literals compiled each of them twice, once to check it and once to use it.

Not bounded

  • A literal's compile now belongs to the run, with whatever bounds the run has. With this PR alone that is none for a regular expression: evaluating one of the literals above costs what compiling its program did, the abort for the alternatives included, as new RegExp of the same pattern does on dev. fix(rust): the run's limits stop a regular expression that backtracks without end #80 bounds it: the run's time limits end a compile, a pattern over 64 KiB is refused under a time limit, and alternatives no longer overflow the stack.
  • Just under the cap a program still parses on a thread with 64 MiB of stack. By the 1 MiB rows above, that is about 23 ms and 47 MiB for 65,535 nested parentheses (not measured at that size).
  • A debug build gives the parser 16 KiB of stack a level, so the cap is 1 GiB of stack there, reserved and used only as deep as the program nests. It checks the literals above in 14 to 105 ms.
  • A wide program costs by its length (107 ms and 139 MiB for 1 MiB of statements), which the source size limit bounds.

Upstream

Rebased onto origin/dev (dec90d1; tip 8db6d66). Resolved one conflict in rust/crates/psychoscript/src/run.rs keeping #153's #[cfg(target_arch = "wasm32")] guard below the MAX_COMPILE_LEVELS check (so programs past MAX_COMPILE_LEVELS get the same ParseError on native and wasm32, while programs between RUN_STACK_BUDGET and MAX_COMPILE_LEVELS still hit #153's wasm32 guard). Nothing was superseded by merged upstream work.

Checked against #158 (960bb0e, compile returns Script with its syntax tree): #158 is TypeScript-only and does not touch rust/crates/psychoscript/; this branch is Rust-only and does not change Runtime::compile's return type (Compiled).

Compared with open PRs:

Tests

  • Added rust/crates/psychoscript/tests/compile_bound.rs, one test: a_program_nested_past_the_bound_is_refused_unparsed. Five shapes of 80,000 levels (brackets left open, parentheses, prefix operators, blocks, template substitutions) are refused as nested too deeply. Three wide programs compile (is_ok(), no wall-clock threshold): two of 80,000 items, and a regular expression literal of 222 KB. Without the change each of the five fails: two get the parser's "Unexpected token" after it recursed 80,000 deep, two get the compiler's "nested more than 2000 levels", and the parentheses compile.
  • The literal's row is 3,000 named groups, 15,000 () and a* 80,000 times. It fails to compile whenever validate_literal still runs regress (regress refuses 80,000 loops). (The linear-time Names rewrite is exercised by the same row, though the test no longer asserts a wall-clock cap.)
  • The names' rule is the same. A scratch test, not committed, made 150,000 random patterns of named groups (one to four names), groups and alternatives, nested up to 30 deep; 122,045 of them use a name twice where they may not. The new check and the old one agree with the rule as written on every pattern.
  • cargo fmt --all --check, cargo clippy --locked -p psychoscript -p psychoscript-bench --all-targets -- -D warnings, cargo test --locked -p psychoscript --lib --test compile_bound --test host --test host_recording --test globals --test boundary --test program_lines --test number_locale --test snapshot (109 passed, 0 failed), and cargo test --locked -p psychoscript-bench --test conformance pass.

@simonklee
simonklee requested a review from kommander December 2, 2026 12:38
@kommander
kommander merged commit 3cfaff8 into dev Dec 2, 2026
2 checks passed
@kommander
kommander deleted the compile-bound branch December 2, 2026 15:49
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.

2 participants