Skip to content

[JSC] Do not apply maximumRegExpJITCodeSize to an inlined RegExp#test - #886

Merged
sosukesuzuki merged 4 commits into
mainfrom
claude/yarr-inline-code-size-limit
Oct 28, 2026
Merged

sosukesuzuki merged 4 commits into
mainfrom
claude/yarr-inline-code-size-limit

Conversation

@dylan-conway

@dylan-conway dylan-conway commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

The bug

/^a/.test("x") returns true once 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:

// 1600000 closure variables: DFG code writes each one's initial value with an instruction of its own, over 16 MiB of code ahead of the RegExp
const f = new Function("s", "n", "var " + Array.from({ length: 1600000 }, (_, i) => "q" + i) + ";" +
  "function unused() { return eval('1'); }" + // never called; its presence makes every variable above a closure variable
  "let hits = 0; for (let i = 0; i < n; i++) if (/^a/.test(s)) hits++; return hits;");
f("x", 1); f("x", 1); // two activations, so that the DFG allocates the activation inline
let hits = 0;
for (let c = 0; c < 60 && !hits; c++) hits = f("x", 5000);
print(hits ? "test() said true" : "test() always said false"); // "x" does not match /^a/

It prints test() said true. With --useJIT=0, or with --maximumRegExpJITCodeSize=33554432, it prints test() always said false.

The size of the function only matters relative to the option. With --maximumRegExpJITCodeSize=1024 an 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) compares m_jit.m_assembler.buffer().codeSize() with maximumRegExpJITCodeSize. That is the size of the RegExp's code only when the buffer started empty, as it does in jitCompile().

jitCompileInlinedTest() is handed the CCallHelpers of the DFG or FTL function, so the buffer already holds everything emitted before the RegExp. Once that alone is over the limit:

  1. generate() emits one op, sees the limit exceeded, sets m_failureReason and stops.
  2. backtrack() does the same.
  3. compileInline() only looks at m_failureReason before 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 RegExpTestInline node 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 converts RegExpTest to RegExpTestInline only when the stand-alone compile succeeded, which means it passed maximumRegExpJITCodeSize, and its code is no larger than maximumRegExpTestInlineCodesize (500 bytes by default).

compileInline() asserts that code generation did not fail, next to the asserts it already has for m_abortExecution and m_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=96 does 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 jsc shell with nothing from an embedder.

Test

JSTests/stress/regexp-test-inline-code-size-limit-ignores-preceding-code.js lowers the limit to 1024 and puts 128 calls to a noInline function 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 jsc binaries from the same build directory, differing only by the change to YarrJIT.cpp:

without with
the script above test() said true test() always said false
the new test, default options fails passes
... --useConcurrentJIT=false --thresholdForJITAfterWarmUp=100 fails passes
... the harness's EAGER_OPTIONS thresholds, --useFTLJIT=false fails passes
... the eager thresholds fails passes
... the eager thresholds, no concurrent JIT fails passes
... --useLLInt=false fails passes
... --useJIT=false passes passes

The default run stops at the DFG. The two runs without the concurrent JIT also compile the function with the FTL, and --dumpFTLDisassembly=1 shows RegExpTestInline in 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=1 shows an FTL compile and the last 1000 calls are all wrong.

With the fix:

  • 23 patterns anchored with ^ (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 with exec, under every --maximumRegExpJITCodeSize from 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.js and regexp-many-non-greedy-paren-groups.js (the test of the limit itself) pass with the options they declare.
  • Bun built against this branch prints test() always said false for the script above, where Bun 1.4.3 prints test() said true.

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.
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 9f4edded-fd08-42fe-9a41-568500357815
Commits

Reviewing files that changed from the base of the PR and between 8d72957 and 27b8006.

Files selected for processing: 2
  • JSTests/stress/regexp-test-inline-code-size-limit-ignores-preceding-code.js
  • Source/JavaScriptCore/yarr/YarrJIT.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

The RegExp JIT now skips the generated-code-size check for inline tests. A stress test places an inlined regexp after 128 opaque() calls and checks its results with a 1,024-byte code-size limit.

Changes

Inline RegExp code-size handling

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: Normal

Merge Risk: Minimal · up to 27b80

No actionable merge-blocking issue is established by the reviewed implementation or test changes.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main fix: excluding inlined RegExp#test code from the maximumRegExpJITCodeSize check.
Description check Passed 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 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) in compileInline() (YarrJIT.cpp:9768): every m_failureReason assignment reachable during generate()/backtrack() is a backreference path, which compileInline already excludes at line 9708; the rest are in opCompile* and gated at line 9729, so with the size check skipped the assert cannot fire.
  • The early return false for ExecutionMode::InlineTest (YarrJIT.cpp:6658): checked the bound the comment relies on — convertTestToTestInline in DFGStrengthReductionPhase.cpp:1179-1191 requires a stand-alone JIT code block (which passed maximumRegExpJITCodeSize) and codeSize <= maximumRegExpTestInlineCodesize.
  • The new stress test: //@ requireOptions with a Yarr option follows the existing pattern (regexp-test-inline-frame-in-outgoing-argument-area.js), uses testLoopCount, 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.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Preview build of 27b8006: autobuild-preview-pr-886-27b80060

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@sosukesuzuki
sosukesuzuki merged commit 48a0ce9 into main Oct 28, 2026
87 checks passed
robobun added a commit that referenced this pull request Oct 28, 2026
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.
robobun added a commit to oven-sh/bun that referenced this pull request Oct 28, 2026
…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.
robobun added a commit to oven-sh/bun that referenced this pull request Oct 28, 2026
… 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.
robobun added a commit to oven-sh/bun that referenced this pull request Oct 29, 2026
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.
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