-
Notifications
You must be signed in to change notification settings - Fork 3
fix(runtime): root the instanceof prototype-chain walk across proxy traps #1211
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
Merged
frostney
merged 1 commit into
claude/r4-boundary-throw-sweep
from
claude/r4-adjacent-hazard-rooting
Aug 24, 2026
Merged
Changes from all commits
Commits
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
87 changes: 87 additions & 0 deletions
87
tests/language/expressions/instanceof/prototype-chain-gc-roots.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /*--- | ||
| description: instanceof keeps the walked prototype reachable across a proxy getPrototypeOf trap | ||
| features: [Goccia.gc, Proxy, Reflect] | ||
| ---*/ | ||
|
|
||
| const hasGoccia = typeof Goccia !== "undefined"; | ||
|
|
||
| const range = (n) => Array.from({ length: n }, (_, i) => i); | ||
|
|
||
| // A bare gc() usually leaves a freed slot readable; allocating a run of objects | ||
| // of the same GC class as the collected temporary is what overwrites the slot | ||
| // and makes a dangling read observable. The walked prototype is a Proxy, so the | ||
| // churn allocates proxies. | ||
| const churn = () => { | ||
| Goccia.gc(); | ||
| Goccia.gc(); | ||
| let sink = null; | ||
| for (const i of range(200)) { | ||
| sink = new Proxy({ a: i, b: i + 1 }, { get(t, k) { return t[k]; } }); | ||
| } | ||
| return sink; | ||
| }; | ||
|
|
||
| describe.runIf(hasGoccia)("instanceof prototype-chain GC roots", () => { | ||
| // OrdinaryHasInstance walks the instance's prototype chain with | ||
| // GetPrototypeOfObject, which invokes a proxy getPrototypeOf trap — guest code | ||
| // that can force a collection. An intermediate prototype reachable only through | ||
| // the walk (here, a fresh proxy returned by an outer trap) is held solely in | ||
| // the loop variable; DispatchProxyGetPrototype dereferences the proxy's | ||
| // internal target for its post-trap invariant check, so without a root the | ||
| // proxy is swept mid-walk and the invariant check reads freed memory. | ||
| test("an engine-only intermediate proxy survives its getPrototypeOf trap", () => { | ||
| let outerRuns = 0; | ||
| let innerRuns = 0; | ||
| class C {} | ||
| const obj = {}; | ||
|
|
||
| Reflect.setPrototypeOf( | ||
| obj, | ||
| new Proxy( | ||
| {}, | ||
| { | ||
| getPrototypeOf() { | ||
| outerRuns += 1; | ||
| const innerTarget = Object.preventExtensions(Object.create(null)); | ||
| return new Proxy(innerTarget, { | ||
| getPrototypeOf(t) { | ||
| innerRuns += 1; | ||
| churn(); | ||
| return Reflect.getPrototypeOf(t); | ||
| }, | ||
| }); | ||
| }, | ||
| } | ||
| ) | ||
| ); | ||
|
|
||
| const result = obj instanceof C; | ||
|
|
||
| expect(outerRuns).toBe(1); | ||
| expect(innerRuns).toBe(1); | ||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| // The direct case: the walked proxy is orphaned by the trap that runs on it and | ||
| // must survive to the post-trap extensibility invariant check. | ||
| test("a directly walked proxy survives a collecting getPrototypeOf trap", () => { | ||
| let trapRuns = 0; | ||
| class C {} | ||
| const obj = {}; | ||
| const target = Object.preventExtensions(Object.create(null)); | ||
|
|
||
| Reflect.setPrototypeOf( | ||
| obj, | ||
| new Proxy(target, { | ||
| getPrototypeOf(t) { | ||
| trapRuns += 1; | ||
| churn(); | ||
| return Reflect.getPrototypeOf(t); | ||
| }, | ||
| }) | ||
| ); | ||
|
|
||
| expect(obj instanceof C).toBe(false); | ||
| expect(trapRuns).toBe(1); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.