Repository navigation
fix(rust): bound a program's compile by its length, for deep nesting and for regex literals - #177
Merged
Merged
Conversation
…and for regex literals
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
run::MAX_COMPILE_LEVELS(65,536).run::compilealready reads a bound on a program's nesting from its bytes (nesting::bound) to size the thread it parses on. A program bounded pastMAX_COMPILE_LEVELSis now refused there, before it is parsed, with aParseError: "SyntaxError: Program is nested too deeply to compile."builtins::regexp::validate_literalchecks the flags and the syntax (regexp_syntax::check, as before) and stops there. It used to compile the pattern withregresstoo, 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.regexp_syntax.rs). Two things in it did not:|copied the set of names seen so far, to know which names an alternative may use again.Namesnow 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.regressabout 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.regressrefuses used to fail the program's compile. Now the program compiles, and the literal throws itsSyntaxErrorwhen 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 underv. test262 is unchanged:language/literals/regexp214 of 214 andbuilt-ins/RegExp935 of 935 pass.rust/README.mdunder "Design".Why
The parser (
oxc) has no guard and recurses once per level of syntax, sorun::compilegives 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, withmax_source_bytesraised:{repeated[repeated${repeatedf(repeated1!,a?,+1,.b,if(1),newEvery 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-yamland 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::compileof a program that is one literal, within the default source limit of 256 KiB:dev()(62 KB)(?:)(229 KB)\p{RGI_Emoji}19,000 times,v(247 KB)\p{Lu}41,000 times,iu(246 KB)ab|83,000 times (249 KB)(?<=a)40,000 times (240 KB)[\S\W]40,000 times,iv(240 KB)\p{L}50,000 times,u(250 KB)(?=a);\Sunderiu;\k<a>;a{1,2};[\u{0}-\u{10FFFF}]underiu(250 KB each)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):
[\S\W]170,000 times,iv\p{RGI_Emoji}80,000 times,vab|340,000 times(?:)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
new RegExpof the same pattern does ondev. 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.Upstream
Rebased onto
origin/dev(dec90d1; tip8db6d66). Resolved one conflict inrust/crates/psychoscript/src/run.rskeeping #153's#[cfg(target_arch = "wasm32")]guard below theMAX_COMPILE_LEVELScheck (so programs pastMAX_COMPILE_LEVELSget the sameParseErroron native andwasm32, while programs betweenRUN_STACK_BUDGETandMAX_COMPILE_LEVELSstill hit #153'swasm32guard). Nothing was superseded by merged upstream work.Checked against #158 (
960bb0e,compilereturnsScriptwith its syntax tree): #158 is TypeScript-only and does not touchrust/crates/psychoscript/; this branch is Rust-only and does not changeRuntime::compile's return type (Compiled).Compared with open PRs:
rust/regex-budget,1b391f6) bounds regular expression search at run time; this PR bounds compilation (nesting depth and literal syntax checking). They merge with 0 conflicts.d926fa6) also modifiesrust/crates/psychoscript/src/run.rs(compile_with/compile_module_with), so whichever lands later has a textual conflict aroundrun::compile.Tests
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.()anda*80,000 times. It fails to compile whenevervalidate_literalstill runsregress(regressrefuses 80,000 loops). (The linear-timeNamesrewrite is exercised by the same row, though the test no longer asserts a wall-clock cap.)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), andcargo test --locked -p psychoscript-bench --test conformancepass.