Repository navigation
[JSC] Do not apply maximumRegExpJITCodeSize to an inlined RegExp#test - #886
Conversation
hasExceededCodeSizeLimit() compares the size of the assembler buffer with
maximumRegExpJITCodeSize. A stand-alone compile starts from an empty
buffer, but an inlined RegExp#test is emitted into the buffer of the DFG
or FTL function, which already holds everything that precedes the RegExp.
In a function with more than 16 MiB of code ahead of it, generate() and
backtrack() each stopped after one op. compileInline() does not look at
m_failureReason after them, so the truncated matcher was used as it was:
/^a/.test("x") returned true.
Inlined code cannot fall back once it has been emitted into the caller's
buffer, so the limit is not checked there. Its size is already bounded
before inlining starts: a RegExp is inlined only when its stand-alone
compile passed this limit and came out no larger than
maximumRegExpTestInlineCodesize.
Measuring from where the RegExp's code starts is not enough. The inlined
code can be larger than the stand-alone code, so a limit between the two
sizes still fails with no way to recover (/^a/ with a limit of 96 on
ARM64).
compileInline() now asserts that code generation did not fail.
…lify the test The comment gave only one of the two reasons the limit is skipped. Without the other, that codeSize() counts the caller's code, measuring from where the RegExp starts looks like the obvious fix, and it is not enough. In the test, opaque() is never inlined, so the calls stay whether or not their results are used. The sum, the extra arguments and passing opaque in are not needed.
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
InlineTest code-size handling Source/JavaScriptCore/yarr/YarrJIT.cpp |
hasExceededCodeSizeLimit skips the code-size check for InlineTest; other execution modes retain the check. compileInline asserts that m_failureReason is unset after backtracking generation. |
Stress test for preceding code JSTests/stress/regexp-test-inline-code-size-limit-ignores-preceding-code.js |
The test configures a 1,024-byte limit, places 128 opaque() calls before an inlined regexp test, and checks results for "x" and "a" across repeated iterations. |
Suggested reviewers: constellation
Priority:
Merge Risk: 
Minimal · up to 27b80
-
No actionable merge-blocking issue is established by the reviewed implementation or test changes.
Pre-merge checks: 
4
-
Full details: Description check
-
Explanation
The description is detailed and relevant. It explains the bug, cause, fix, test coverage, and validation results. It does not include the template's explicit Bugzilla URL, Reviewed by NOBODY line, or changed-file/function list, but the core information is complete.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. Since it changes Yarr JIT code generation (paths listed under jsc-reviewers in .github/CODEOWNERS) and adds a RELEASE_ASSERT that also fires in release builds, a human look would still be worthwhile.
What was reviewed:
- The new
RELEASE_ASSERT(!m_failureReason)incompileInline()(YarrJIT.cpp:9768): everym_failureReasonassignment reachable duringgenerate()/backtrack()is a backreference path, whichcompileInlinealready excludes at line 9708; the rest are inopCompile*and gated at line 9729, so with the size check skipped the assert cannot fire. - The early
return falseforExecutionMode::InlineTest(YarrJIT.cpp:6658): checked the bound the comment relies on —convertTestToTestInlinein DFGStrengthReductionPhase.cpp:1179-1191 requires a stand-alone JIT code block (which passedmaximumRegExpJITCodeSize) andcodeSize <= maximumRegExpTestInlineCodesize. - The new stress test:
//@ requireOptionswith a Yarr option follows the existing pattern (regexp-test-inline-frame-in-outgoing-argument-area.js), usestestLoopCount, and fails by throwing.
Extended reasoning...
The change touches Source/JavaScriptCore/yarr/YarrJIT.cpp (two hunks: skip the code-size limit for ExecutionMode::InlineTest in hasExceededCodeSizeLimit(), and assert no failure reason after generate()/backtrack() in compileInline()) plus one new JSTests stress test. It touches no auth, injection, or data-exposure surface, but it is JIT code generation where the prior behavior produced a silently wrong matcher. The fix is small and I verified the assert is unreachable and the DFG-side bound holds, but both changed paths are covered by .github/CODEOWNERS (jsc-reviewers), which is the deciding factor against auto-approval.
|
Preview build of 27b8006: |
…yarr-inline-code-size-limit # Conflicts: # Source/JavaScriptCore/yarr/YarrJIT.cpp
main got #886 (48a0ce9) and #926 (23d7f1d) after the merge of 0e10366. There is no conflict. Both sides changed yarr/YarrJIT.cpp, and git merged it. Upstream 1984003 changes how the frame slots of a ParenContext are copied. #886 changes hasExceededCodeSizeLimit() and adds an assertion behind the backtracking code. The two changes are in different functions. #926 changes yarr/YarrInterpreter.cpp, which upstream did not change in this range. The other two files are the tests that #886 and #926 add. Checked with a Release build of jsc for Linux x64 without the ahead-of-time compiler: run-javascriptcore-tests with --filter="regex|regexp|yarr", 7278 runs from all collections, no failure. The two new tests are among them.
…d362b3 That head has a repair for Bun.JSONL: with the object loop that upstream rewrote, a streaming parse could give an object a Structure that a collection had freed (bcd362b31e over there, with a stress test). It also has the fork's main merged in again (oven-sh/WebKit#886 and #926). Its CI is green.
… RegExp throws when its second match fails WEBKIT_VERSION is the preview build of oven-sh/WebKit#929 (autobuild-preview-pr-929-c5087553). From fcc1ea0973a4 the range is oven-sh/WebKit#886, oven-sh/WebKit#926 and upstream WebKit/WebKit@191803bb2304, cherry-picked with no change. With the upstream commit, a read of RegExp.lastMatch or of another legacy static property throws a RangeError when its second match fails. It used to end the process: "Segmentation fault at address 0x4". The engine's stress test for it is a fixture of test/js/bun/jsc-stress/jsc-stress.test.ts.
Main's WebKit pin contains oven-sh/WebKit#886 and oven-sh/WebKit#926, and nothing in this repository tests either. - An inlined RegExp#test is not held to maximumRegExpJITCodeSize, which counted the code of the function around it. - The interpreter and the JIT agree on a greedy backreference whose group is rewritten. The test has one case for each, and runs the second with and without the RegExp JIT.
The bug
/^a/.test("x")returnstrueonce the function around it is compiled by the DFG or FTL, if that function has more than 16 MiB of machine code ahead of the RegExp:It prints
test() said true. With--useJIT=0, or with--maximumRegExpJITCodeSize=33554432, it printstest() always said false.The size of the function only matters relative to the option. With
--maximumRegExpJITCodeSize=1024an ordinary function shows the same thing in milliseconds, which is what the test uses.Cause
hasExceededCodeSizeLimit()(316157@main, https://bugs.webkit.org/show_bug.cgi?id=314589) comparesm_jit.m_assembler.buffer().codeSize()withmaximumRegExpJITCodeSize. That is the size of the RegExp's code only when the buffer started empty, as it does injitCompile().jitCompileInlinedTest()is handed theCCallHelpersof the DFG or FTL function, so the buffer already holds everything emitted before the RegExp. Once that alone is over the limit:generate()emits one op, sees the limit exceeded, setsm_failureReasonand stops.backtrack()does the same.compileInline()only looks atm_failureReasonbefore generating code, not after, so it links what there is and returns.The truncated matcher is then run as if it were complete. In the test's function the
RegExpTestInlinenode is 192 bytes of DFG code without the fix and 244 with it./^a/then reports a match for"x","zzz"and"ba", and/^\d{3}$/for"1234"and"foo".It can also fail to return. Calling one such function with
"","x","zzz","ba","a","abc","123"and"foo"in turn does not finish within 5 seconds for either pattern, though each of the strings I tried on its own does. I have not worked out why. With the fix both patterns give the right result for all eight.Fix
The limit is not checked for
ExecutionMode::InlineTest. By the time it could trip, code is already in the caller's buffer and there is nothing to fall back to. The size of inlined code is bounded before inlining starts: strength reduction convertsRegExpTesttoRegExpTestInlineonly when the stand-alone compile succeeded, which means it passedmaximumRegExpJITCodeSize, and its code is no larger thanmaximumRegExpTestInlineCodesize(500 bytes by default).compileInline()asserts that code generation did not fail, next to the asserts it already has form_abortExecutionandm_hitMatchLimit. A failure there can only produce a wrong matcher.I first measured from where the RegExp's code starts instead. That fixes the case above but is not enough: the inlined code can be larger than the stand-alone code, so a limit between the two sizes passes the stand-alone compile and fails the inlined one. On ARM64
/^a/with--maximumRegExpJITCodeSize=96does that, and hit the new assert. So did a run at 192 in which the only other inlined pattern was/^\d{3}-\d{4}$/. 96 and 192 are the smallest limits at which those two patterns are inlined at all.Why this belongs in WebKit
The check, the option and both callers are upstream JSC code, and the wrong result reproduces in the
jscshell with nothing from an embedder.Test
JSTests/stress/regexp-test-inline-code-size-limit-ignores-preceding-code.jslowers the limit to 1024 and puts 128 calls to anoInlinefunction ahead of/^a/.test(s), then checks a string that matches and one that does not. It takes about 13 ms.All of this is on ARM64 macOS with release builds. I have not run x86_64, a build with assertions enabled, or the whole of JSTests.
Two
jscbinaries from the same build directory, differing only by the change toYarrJIT.cpp:test() said truetest() always said false--useConcurrentJIT=false --thresholdForJITAfterWarmUp=100EAGER_OPTIONSthresholds,--useFTLJIT=false--useLLInt=false--useJIT=falseThe default run stops at the DFG. The two runs without the concurrent JIT also compile the function with the FTL, and
--dumpFTLDisassembly=1showsRegExpTestInlinein it. Without the fix the DFG fails the test first, so to see the FTL on its own I ran the first version of the test's function 300000 times in Bun 1.4.3 without stopping at a wrong answer:--reportCompileTimes=1shows an FTL compile and the last 1000 calls are all wrong.With the fix:
^(classes, alternation, greedy, lazy and counted quantifiers,i,u,\b,$, lookahead), each in a function of its own called with 27 strings in turn and compared withexec, under every--maximumRegExpJITCodeSizefrom 0 to 600 in steps of 4: right results and no assert at all 151 limits. The dumps show all 23 inlined by both the DFG and the FTL at 600, and the smallest limit at which each is inlined is between 80 and 440, so the range covers the limit next to each pattern's size. Options: no concurrent JIT,--maximumRegExpTestInlineCodesize=10000, DFG after 300 and FTL after 2000. With the DFG after 20 none of these small functions gets the RegExp inlined.regexp-test-inline-*.js,regexp-testinline-stacklimits.jsandregexp-many-non-greedy-paren-groups.js(the test of the limit itself) pass with the options they declare.test() always said falsefor the script above, where Bun 1.4.3 printstest() said true.