-
Notifications
You must be signed in to change notification settings - Fork 5k
Normalize scope-object this in mock function calls to prevent leak/crash
#31605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+17
−1
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟣 Pre-existing: the same raw
callframe->thisValue()leak fixed here is still present ~600 lines down inJSMock__jsSetSystemTime(returnsJSValue::encode(callframe->thisValue())on both paths, lines ~1462/1467) and inJSMock__jsUseRealTimers(~1435).setSystemTimeis exposed as a top-levelbun:testexport (jest.zig:224-225), soconst { setSystemTime } = require("bun:test"); const c = () => setSystemTime; typeof setSystemTime(0) === "function"reproduces the same JSLexicalEnvironment leak / LLInt crash. The 7return callframe.this()sites inFakeTimers.zig(useFakeTimers, useRealTimers, advanceTimersByTime, etc.) have the identical hazard. Since this is the same root cause and same fuzzer fingerprint being closed, it may be worth applying the same.toThis(globalObject, ECMAMode::strict())(or returningjsUndefined()) to those while you're here.Extended reasoning...
What the bug is
This PR correctly fixes
jsMockFunctionCallby normalizingcallframe->thisValue()viatoThis(globalObject, ECMAMode::strict())so that an engine-internalJSScope(e.g.JSLexicalEnvironment) cannot escape to JavaScript throughmockReturnThis()ormock.contexts. However, the identical pattern remains unfixed in the same file:and likewise
JSMock__jsUseRealTimersat line ~1435. Additionally,src/runtime/test_runner/timers/FakeTimers.zighas 7 host functions (useFakeTimers,useRealTimers,advanceTimersToNextTimer,advanceTimersByTime,runOnlyPendingTimers,runAllTimers,clearAllTimers) thatreturn callframe.this()raw with the same hazard.Code path that triggers it
JSMock__jsSetSystemTimeis registered directly on thebun:testmodule object insrc/runtime/test_runner/jest.zig:224-225:So
const { setSystemTime } = require("bun:test")(orimport { setSystemTime } from "bun:test") gives JS a bare host-function binding. When that binding is captured by a closure and called as a bare identifier, JSC'sFunctionCallResolveNode::emitBytecodepasses the resolvedJSScopeas the unconvertedthisargument. Native callees never runop_to_this, socallframe->thisValue()is the rawJSLexicalEnvironment, which is then returned straight to JS.Why existing code doesn't prevent it
The PR description itself notes that ordinary
JSFunction-wrapped host natives (citingCallSite.cpp/JSBuffer.cppas precedent) need explicittoThis()normalization — the wrapper type doesn't shield them.JSMock__jsSetSystemTimeis wrapped viajsc.JSFunction.create, which is exactly that case. There is nodynamicDowncastguard onthisValuein this function (unlike e.g.jsMockFunctionMockClear, which requiresthisto be aJSMockFunctionand so can't leak a scope object).Step-by-step proof
const { setSystemTime } = require("bun:test");—setSystemTimeis now a binding in the local lexical environment.const capture = () => setSystemTime;— closure-capture forcessetSystemTimeto live in a heap-allocatedJSLexicalEnvironmentrather than a register.const leaked = setSystemTime(0);— bytecode emitsresolve_scope→ finds theJSLexicalEnvironment→ emitscallwith that scope object in thethisslot. The native callee reads it unmodified and returnsJSValue::encode(callframe->thisValue()), handing theJSLexicalEnvironmentback to JS.typeof leaked === "function"—llint_op_typeof_is_functionon aJSLexicalEnvironmentcell dereferences a null cell (or, in release builds, the scope object stringifies as[native code: JSLexicalEnvironment]) — the exact fuzzer fingerprint (signal:SIGNED_RIGHT_SHIFT, SIGSEGV) this PR is closing formockReturnThis.Impact
Same impact class as the bug being fixed: an engine-internal scope object becomes JS-observable, and reading a TDZ binding from it surfaces the empty
JSValue, crashing the LLInt on a subsequenttypeof. It's reachable from any test file that destructuressetSystemTime(or the FakeTimers helpers) at module level into a closure-captured binding.How to fix
Apply the same one-liner: replace
callframe->thisValue()withcallframe->thisValue().toThis(globalObject, JSC::ECMAMode::strict())inJSMock__jsSetSystemTime(both return paths) andJSMock__jsUseRealTimers. For the Zig sites inFakeTimers.zig, either normalize via the equivalenttoThisbinding or simplyreturn .js_undefinedwhencallframe.this()isn't an object — the chaining return value isn't load-bearing for a bare call.Severity
This is pre-existing — the PR does not touch these functions, add callers, or change their reachability. It's flagged because it's the same root cause, in the same file, producing the same fuzzer fingerprint the PR is meant to close, so the fuzzer will likely rediscover it. Not blocking, but a reasonable "while you're here".