Repository navigation
[JSC] DFG RegExpTest asserts when the RegExp is also the argument (cherry-pick WebKit/WebKit#74030) - #654
[JSC] DFG RegExpTest asserts when the RegExp is also the argument (cherry-pick WebKit/WebKit#74030)#654robobun wants to merge 1 commit into
RegExpTest asserts when the RegExp is also the argument (cherry-pick WebKit/WebKit#74030)#654Conversation
There was a problem hiding this comment.
LGTM — debug-only assertion relaxation matching the established pattern in the sibling function.
What was reviewed:
- Verified
emitRegExpMinimumLengthFilterGuardsonly readsbaseGPR/argumentGPR(loads at lines 10402, 10410, 10417, 10423) and writes only the scratch registers, so base/argument aliasing is safe. - Confirmed the split-assert pattern is copied exactly from
emitRegExpStickyFirstCharacterFilterGuards(lines 10373-10374); no codegen change, release builds unaffected. - Checked the stress test against JSTests/README.md rules — uses
testLoopCount, throws on failure, no logging; walked through eachshouldBeexpectation (including the sticky/\/ab/ylastIndex bookkeeping) and they are correct.
Extended reasoning...
Overview
The PR changes one debug ASSERT in Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp::emitRegExpMinimumLengthFilterGuards, splitting noOverlap(baseGPR, argumentGPR, scratch1GPR, scratch2GPR) into two assertions that permit baseGPR == argumentGPR while still guaranteeing neither operand aliases a scratch register. It also adds JSTests/stress/regexp-test-regexp-is-also-the-argument.js, which drives regExp.test(regExp) through DFG/FTL for non-constant, constant, anchored, sticky, and global RegExps and checks results plus lastIndex.
Security risks
None. The modification is to a debug-build ASSERT only; ASSERT compiles away in release, so shipped codegen is byte-identical. Reading the full function body confirms baseGPR and argumentGPR are used solely as base addresses for loads and are never written, so allowing them to share a register cannot corrupt state. The scratch-vs-operand and scratch-vs-scratch invariants are still enforced (noOverlap on three args still checks pairwise distinctness among scratch1/scratch2). The new test is pure JS with no I/O.
Level of scrutiny
Low. This is a one-line-to-two-line change that copies the exact idiom already used 24 lines above in emitRegExpStickyFirstCharacterFilterGuards. It follows an established pattern with no ambiguity, is self-contained, and the intent (fix a false-positive debug assertion when the DFG allocates one register for two edges to the same node) is clear and well-documented in the PR description. The bug-hunt ran to dry_streak with no findings.
Other factors
CODEOWNERS lists /Source/JavaScriptCore and /JSTests under WebKit/jsc-reviewers, but the file's own header states "Contributors do not 'own' WebKit components. This file is used to automatically add reviewers to pull requests" — it is upstream WebKit's reviewer-suggestion list carried into the Bun fork, not an ownership gate. The stress test complies with JSTests/README.md requirements (throws on failure, testLoopCount-driven, no printing). I hand-checked every expected value in the test, including the /\/ab/y case where the string form is the 7-char "/\\/ab/y" and /ab matches at index 2 giving lastIndex 5. Given zero release-build impact and trivially verifiable correctness, this is safe to approve.
|
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 (1)
Included review availability: Your plan provides up to 5 included reviews per hour; 1 remains after this review. WalkthroughThe change relaxes DFG register assertions for RegExp minimum-length guards and adds stress coverage for ChangesRegExp self-argument handling
Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to The change addresses a reachable RegExp self-argument assertion failure, and no merge-blocking risk is established. 🚥 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 |
|
Preview build of cb4a0b2: |
8d308b7 to
ede7410
Compare
There was a problem hiding this comment.
LGTM — debug-only assertion split that mirrors the sibling function.
What was reviewed:
emitRegExpMinimumLengthFilterGuardsonly readsbaseGPR/argumentGPRand writes only the scratch registers, so aliasing the two inputs is safe; the split matchesemitRegExpStickyFirstCharacterFilterGuardsright above it.- Confirmed the caller diverts non-string arguments to the slow path via
branchIfNotStringbefore the guards run, so generated code is unchanged. - Checked FTL (
FTLLowerDFGToB3.cpp) and the 32-bit SpeculativeJIT for an equivalent assertion — FTL uses B3 LValues (nonoOverlap), and there is no 32-bit implementation of this function. - New stress test follows JSTests rules (uses
testLoopCount, throws on failure, no logging); walked eachshouldBeagainst the RegExp's owntoString()and the values check out.
Extended reasoning...
Overview
The PR relaxes a single debug ASSERT in SpeculativeJIT::emitRegExpMinimumLengthFilterGuards (Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp) from a four-way noOverlap(baseGPR, argumentGPR, scratch1GPR, scratch2GPR) into two three-way checks that permit baseGPR == argumentGPR. It adds JSTests/stress/regexp-test-regexp-is-also-the-argument.js, which drives regExp.test(regExp) through DFG with plain, anchored, longer-than-source, global, constant, constant-anchored and constant-sticky RegExps. That is the entire diff.
Security risks
None. ASSERT compiles away in release builds, so this change alters no shipped code path. The relaxed invariant is sound: reading the function body, baseGPR and argumentGPR are used only as load addresses; all stores go to scratch1GPR/scratch2GPR, which are still asserted distinct from both inputs and from each other. At run time the aliased case cannot even reach the guard body — emitRegExpTestWithFilter sends a non-string argument (which a RegExp is) to the slow-path operationRegExpTest via branchIfNotString before any guard executes.
Level of scrutiny
Low. This is a two-line, debug-only assertion adjustment that copies the exact pattern already used 25 lines above in emitRegExpStickyFirstCharacterFilterGuards. I checked whether the same over-strict assertion exists elsewhere: the FTL lowering (compileRegExpTestMinimumLengthFilter / emitMinimumLengthGuardChain) works on B3 LValues and has no register-overlap assertion, and there is no 32-bit implementation of this helper. Nothing else needs the same fix.
Other factors
The new stress test conforms to JSTests/README.md: it uses testLoopCount, fails by throwing from shouldBe, and prints nothing on success. It is a new file (not a modified upstream test), so no // Bun: marker or BUN-TEST-DIFFERENCES.md entry is required. I hand-checked each expected value against the RegExp's toString() output (including the global lastIndex progression to 4 then reset to 0, and the sticky match at index 2 advancing lastIndex to 5). A commit was pushed after the earlier bot review, so a fresh verdict is appropriate; there are no outstanding CHANGES_REQUESTED reviews.
…the DFG register assertion The DFG gives the RegExp and the argument of RegExpTest one register when both are the same value. The minimum-length filter of RegExp#test asserted that the two registers differ, so a debug or ASAN build aborted in the DFG compiler. oven-sh/WebKit#654 asserts each input against the scratch registers only. Pin WEBKIT_VERSION to the preview build of that PR and add its stress test as a jsc-stress fixture.
ede7410 to
17b2731
Compare
…the DFG register assertion The DFG gives the RegExp and the argument of RegExpTest one register when both are the same value. The minimum-length filter of RegExp#test asserted that the two registers differ, so a debug or ASAN build aborted in the DFG compiler. oven-sh/WebKit#654 asserts each input against the scratch registers only. Pin WEBKIT_VERSION to the preview build of that PR and add its stress test as a jsc-stress fixture.
|
Upstream landed the same change after I opened this PR. The description says that upstream The upstream commit is WebKit/WebKit@d22fa24 (https://commits.webkit.org/321146@main, https://bugs.webkit.org/show_bug.cgi?id=324211). Yusuke Suzuki reviewed it, and it landed on 2026-09-15. Its change to The upstream merge in #651 does not include that commit. #651 targets The fuzzer reported this assertion a third time, at the current pin function f() {
const re = /abcdef/;
return re.test(re);
}
for (let i = 0; i < 100000; i++) f();The release build of the same Bun revision returns |
https://bugs.webkit.org/show_bug.cgi?id=324211 Reviewed by Yusuke Suzuki. In `regExp.test(regExp)` both operands of RegExpTest are the same node, so baseGPR and argumentGPR are the same register. Debug builds then hit ASSERT(noOverlap(baseGPR, argumentGPR, scratch1GPR, scratch2GPR)) in emitRegExpMinimumLengthFilterGuards(), added in 320487@main. The function only reads baseGPR and argumentGPR and only writes the scratch registers, so assert each input against the scratch registers separately, as emitRegExpStickyFirstCharacterFilterGuards() already does. The generated code does not change: a RegExp argument is not a string, so it branches to the slow path call before the guards run. Test: JSTests/stress/regexp-test-regexp-is-also-the-argument.js * JSTests/stress/regexp-test-regexp-is-also-the-argument.js: Added. (shouldBe): (testConstant): (testConstantAnchored): (testConstantSticky): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp: (JSC::DFG::SpeculativeJIT::emitRegExpMinimumLengthFilterGuards): Canonical link: https://commits.webkit.org/321146@main (cherry picked from commit d22fa24)
17b2731 to
cb4a0b2
Compare
regExp.test(regExp) fails a register assertion in the minimum-length filterRegExpTest asserts when the RegExp is also the argument (cherry-pick WebKit/WebKit#74030)
|
The branch now holds the upstream commit itself: a cherry-pick of WebKit/WebKit@d22fa24 on the current The source change is the same as before. The test file is now byte-identical to the upstream one (my version had three more comment lines). Upstream uses the same path, so a later upstream merge does not conflict on it. I ran the upstream test file on two debug |
|
#725 (upstream merge to So the fix can reach
|
|
Closing: Bun has the fix since oven-sh/bun#43882, which moved the WebKit pin to that merge. oven-sh/bun#42694 now only adds the test as a |
Cherry-pick of WebKit#74030 (upstream commit
d22fa241f19e4a2ab3565132df3bc7db82d0b684, https://commits.webkit.org/321146@main, https://bugs.webkit.org/show_bug.cgi?id=324211). The commit applies cleanly onmain. The author and the commit message are unchanged.This PR had the same source change before upstream landed it on 2026-09-15. The branch now holds the upstream commit itself. Upstream adds its test at the same path,
JSTests/stress/regexp-test-regexp-is-also-the-argument.js, so with identical content a later upstream merge does not conflict here.Problem
regExp.test(regExp)aborts a build with assertions when the function reaches the DFG:ASSERTION FAILED: noOverlap(baseGPR, argumentGPR, scratch1GPR, scratch2GPR)atdfg/DFGSpeculativeJIT64.cpp:10401, inemitRegExpMinimumLengthFilterGuards.RegExpTestare the same node there, so both operands get the same register. The assertion does not permit that.ASSERTcompiles to nothing there, and the generated code is correct.Fix
emitRegExpStickyFirstCharacterFilterGuards, the function above this one, already does that.baseGPRandargumentGPR. It writesscratch1GPRandscratch2GPRonly.emitRegExpTestWithFiltersends it to the slow path call (branchIfNotString) before a guard runs.JSTests/stress/regexp-test-regexp-is-also-the-argument.js. The debugjscfrom theautobuild-d3720d515e14release (bun-webkit-linux-amd64-debug-asan), which does not have the change, fails it with the assertion above. The samejscpasses it with--useDFGJIT=0and with--useJIT=0, so the expected values in the test are right. The debugjscof the preview build of this head (autobuild-preview-pr-654-cb4a0b23) passes it with default options, with--useConcurrentJIT=0, with--useFTLJIT=0, and with low tier-up thresholds.run-javascriptcore-testson this head. The only failure was one run ofstress/sampling-profiler-bound-function-name.jsin thelinux-amd64-ltotest lane, a release build where this change compiles to nothing. The re-run of that lane passed.Background
RegExp#testshould fast-fail on an input shorter than the pattern's minimum size" (https://bugs.webkit.org/show_bug.cgi?id=322689). Upstream fixed the assertion in 321146@main, the commit that this PR picks.x.test(x)givesRegExpTesttwo such edges.noOverlap(a, b, c)is true when no two of its arguments are the same register.Notes
Smallest repro for a debug
jsc:The test covers four shapes. All have the RegExp as the argument:
RegExp::m_minimumSizeat run time. This shape and the next one fail the assertion.^. This one usesemitRegExpAnchoredFirstCharacterFilterGuards, which has no RegExp register.emitRegExpStickyFirstCharacterFilterGuards, which already permits one register for both.How it was found: a fuzzer run of Bun's debug build, with this script.
sliceconvertsv0to a number, so it callsvalueOf, which isf1.testconvertsv0to a string.toStringgives back an object, so the conversion callsvalueOfagain. The recursion makesf1hot before the stack overflows.v0is a constant inf1.RegExpTestgets aCheckStructureagainst the original RegExp structure before it (addRegExpPrimordialStructureCheck). In a fresh process the DFG can watch the structure ofv0(RegExp plusvalueOfplustoString). It then proves that the check fails and emits no code for the rest of the block.v0.valueOf = f1. That store is then a transition away from a watched structure, soStructure::didTransitionFromThisStructureWithoutFiringWatchpointsetstransitionWatchpointIsLikelyToBeFired. The new structures ofv0inherit the flag, soGraph::tryWatchrefuses them.v0.testload with an absence condition onv0and adds noCheckStructure(ByteCodeParser::load). The DFG now knows nothing about the structure ofv0, so it keeps the check as a run-time check and emits code forRegExpTest.Later fuzzer reports have a script that fails on every run in a fresh process, with no earlier script:
F0calls itself throughnew this.constructor()until the stack overflows. Each level then callsf6, sof6gets hot.v5is a new closure variable at each level, so it is not a constant inf6. The DFG merges the two loads ofv5into one node, and the structure check passes at run time.The branch is based on
mainof this repository (b54a4733d8f7). Earlier heads of this branch followed the WebKit pin of Bunmainso that oven-sh/bun#42694 could pin their preview builds. That PR no longer follows each pin move.Bun PR that adds the test as a
jsc-stressfixture: oven-sh/bun#42694. With the preview build of an earlier head of this branch, the fuzzer scripts pass in a fresh process and after the earlier script above (3 of 3 runs each). That PR waits for this change to reach the WebKit pin of Bunmain.