Repository navigation
Conversation
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked the two save-point paths that do not go through a retry of the same text: parseYieldExpression (Source/JavaScriptCore/parser/Parser.cpp:4519) restores after an overflowed argument and returns a bare yield, but the enclosing production then sees the argument's first token and fails, and any continuation reaches a guarded function within a few tokens, so the flag still ends the parse; and the flag itself is set only by failWithStackOverflow(), initialized false per Parser instance (Parser.cpp:146), never cleared, and already what Parser::parse() reports as ParserError::StackOverflow (Parser.h:2260). The new test's cost is one lexer pass over ~300 KB per case, ending at the first overflow, so it stays well inside the 200 ms budget in the tested lanes.
Extended reasoning...
The one inline finding records the depth narrowing the author already documented in the PR's "What no longer parses" table; this note only lists what else was examined. The yield save point was traced by hand: after restoreSavePoint clears m_errorMessage, next() consumes yield and control returns up parseAssignmentExpression with the argument's first token ([, {, () still current, which no caller accepts without entering another failIfStackOverflow()-guarded function, so the parse terminates with m_hasStackOverflow set and Parser::parse() reports StackOverflow. The flag is per-instance, initialized false, and never reset, so there is no stale-flag risk across parses. The test builds strings of ~300 KB and every case fails at the first overflow with one lexer pass, so runtime is linear in depth; asan and debug lanes are not tested in CI per the root CLAUDE.md.
|
Preview build of 8ddd387: |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 5 included reviews per hour; 1 remains after this review. WalkthroughThe parser now stops repeated reparsing after a stack overflow. A stress test covers deeply nested literals, assignment patterns, and arrow-function parameters through ChangesParser stack-overflow handling
Priority: ➖ Normal 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Warning Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use Comment |
The parser parses some text a second time as another production when the
first parse fails: an object or array literal as a destructuring pattern
(parseAssignmentExpression), a pattern element as a member expression
(parseAssignmentElement), a parenthesized expression as arrow function
parameters (parseArrowFunctionCandidate). Restoring the save point clears
m_errorMessage, so "Stack exhausted" counted as such a failure. Each nesting
level on the way out then parsed everything below it again, and each of
those parses did the same. `var x = {v:{v: ... 1 ... }}` nested one level
past the stack limit never finished parsing, and the process grew by about
250 MB per second.
failIfStackOverflow() now also fails when m_hasStackOverflow is set. Every
recursive path of the parser goes through it, so a second parse fails at
once and the error reaches the caller in time linear in the depth.
Parser::parse() already reports ParserError::StackOverflow when the flag is
set, so these programs throw "RangeError: Maximum call stack size exceeded."
The depth at which a literal overflows does not change (3042 levels of
`{v:` and 3664 of `[` in a release build on Linux x64). Two kinds of program
recovered from an overflow before, because their second parse uses smaller
frames than the first, and no longer do:
- A destructuring assignment pattern in expression position parsed about 20
levels deeper than the literal. It now overflows at the same depth.
- An arrow function in code that is parsed with ASTBuilder, with an
expression nested more than 3664 deep in its parameter list, was accepted
up to depth 8713, because the second parse of the parameters uses
SyntaxChecker. Every call of such a function threw a RangeError, because
the parse at the first call builds the AST. Now the program that contains
it throws the RangeError. The deepest such function that could be called
was 3666 levels deep and is 3663 now.
ecbaab3 to
8ddd387
Compare
Problem
(0, eval)("var x = " + "{v:".repeat(3043) + "1" + "}".repeat(3043))spins one core and grows by about 250 MB per second. Node throws aRangeErrorin 44 ms.{or[fails,parseAssignmentExpression(Source/JavaScriptCore/parser/Parser.cpp:4417) parses it again as a destructuring pattern. The save point restore clearsm_errorMessage, so "Stack exhausted" counts as such a failure. Each nesting level on the way out parses everything below it again.Fix
failIfStackOverflow()also fails whenm_hasStackOverflowis set. All eight recursion guards use it, so a second parse fails at once.Parser::parse()already reportsParserError::StackOverflowfor that flag, so these programs throwRangeError: Maximum call stack size exceeded.JSTests/stress/parser-stack-overflow-is-final.js, 16 cases that all hang without the change. A filteredrun-javascriptcore-testsrun (586 tests, 5678 runs) has 0 failures.Background
failIfStackOverflow(), which compares the stack pointer with the VM's soft limit.{a: b}can be a literal or a pattern,(a = 1)can be an expression or arrow function parameters.failWithStackOverflow()setsm_hasStackOverflow. It belongs to oneParserinstance, each instance parses once, and nothing clears the flag.Notes
This replaces #297. That PR has the same one-line change. It also carries CI changes (
Dockerfile,build-reusable.yml) that now conflict withmain, and it has no test. This branch is one commit onebd5a6145b, the commit Bun pins today, so the preview build is that pin plus this change.All numbers below come from two
jscshells built from this tree in one configuration (Release, Linux x64), one without theParser.cppline.Programs nested 100000 deep
var x = {v:{v: ... 1 ... }}RangeError, 7 msvar x = [[ ... 1 ... ]]RangeError, 4 ms[[ ... a ... ]] = []RangeError, 5 ms({v:{v: ... a ... }} = {})RangeError, 5 msvar x = (a = (a = ... 1 ... ))RangeError, 7 msvar x = (a = (a = ... 1 ... ) => 1) => 1RangeError, 6 msasyncRangeError, 6 msThe same holds for
new Function(program), which checks the body withSyntaxChecker, the parser's other instantiation. Shapes with no second parse already threw before: nested parentheses, calls, template literals, function bodies, blocks, classes.What no longer parses
The old and the new parser do the same thing until the first overflow. After it, the new parser always fails. So the only programs that change are those where the old parser recovered from an overflow and then succeeded. That needs a second parse with smaller frames than the first. There are two such places. Deepest nesting that parses, found by bisection:
var x = {v: ... }var x = [ ... ][ ... a ... ] = []({v: ... a ... } = {})var f = (a = [0,[0, ... ]]) => 1, definition onlyASTBuilder, the parameters of an arrow function are first parsed as an expression withASTBuilderand then as parameters withSyntaxChecker, which has much smaller frames. So the old parser accepted the definition up to depth 8713. Every call of such a function threw aRangeError, because the parse at the first call builds the AST. Now the program that contains it throws theRangeError.What I did to look for other effects
Parser.cpp. OnlyparseArrowFunctionCandidate,parseDestructuringAssignment,parseAssignmentElement, theforstatement head andparseYieldExpressionparse recursively and then restore after a failure. The"use strict"restart and the async function body parse again only after a success. The others look ahead one or two tokens. After the flag is set, each of them enters a guarded function within a constant number of tokens.Functionbody and as a module). Each one that overflowed ended within 75 ms, and the time does not grow with the depth. No crash.--filter "stack-overflow|deep-recursion|destructuring|arrow|parser|syntax|nested|yield|parse|literal|pattern|recursion|stack-limit|StackOverflow". The new test ran in 17 modes there, among thembytecode-cache,mini-mode,lockdownand the eager JIT tiers. It also passes with--maxPerThreadStackUsage=262144.typescript.js), 12 interleaved rounds of 31 parses for each shell on a shared machine. Best parse: 102.1 ms without the change, 101.2 ms with it. Median of the rounds' best parses: 106.0 ms without, 105.5 ms with. The check costs nothing that this can measure.Not part of this PR
(a = (a = 9) => a) => a, parse in exponential time with no stack overflow at all. [JSC] Parser: arrow functions nested in parameter defaults no longer parse in exponential time #699 is about that. It is why the arrow check at the end of the new test stays at depth 8.[[[a] = []] = []] = [](3 s at depth 6000) and a wide array assignment pattern with an invalid target deep inside.evalof a program that is only a literal,({v:{v: ... }}), goes toLiteralParserand never reaches this parser. The test usesvar x = ...for that reason.