-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix stack overflow when inspecting circular JSX elements #30657
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
Closed
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
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.
🟣 While you're on this line:
ConsoleObject.zig'scanHaveCircularReferences()also includes.Event, but this version still doesn't. The.Eventbranch inprintAs()here recurses viathis.format()into a MessageEvent'sdata/ ErrorEvent'serror, so an event whosedatapoints back to itself can still stack-overflow the Jest formatter — same root cause as the JSX fix. Pre-existing and contrived, but it's a one-token addition for parity.Extended reasoning...
What
This PR adds
.JSXtocanHaveCircularReferences()in bothConsoleObject.zigandpretty_format.zigso that circular JSX elements get a visited-map guard. However, the two functions are still not in parity:ConsoleObject.zig:1139lists.Function, .Array, .Object, .Map, .Set, .Error, .Class, .Event, .JSX, whilepretty_format.zig:329after this PR lists only.Array, .Object, .Map, .Set, .JSX. The missing one that matters is.Event— the.Eventbranch inpretty_format.zig'sprintAs()does recurse into user-controlled values, so it has the exact same stack-overflow exposure that this PR is fixing for.JSX. (.Function/.Class/.Errorinpretty_format.zigdo not recurse, so they don't need to be added —.Eventis the only real gap.)Code path
MessageEvent,ErrorEvent,CloseEventetc. are constructed withJSType(JSEventType)(0b11101111) persrc/jsc/bindings/JSDOMWrapper.h, which maps toJSType.Eventin Zig. InTag.get(), this is distinct from.DOMWrapper(which returns.Private), so it falls through to the final switch and returns.tag = .Event.format()dispatches toprintAs(.Event, ...). Because.Eventis not incanHaveCircularReferences(), theif (comptime Format.canHaveCircularReferences())block is skipped — no entry is inserted intothis.mapfor the event..Event => { ... }arm, for aMessageEventthe formatter doesconst data = (try value.fastGet(this.globalThis, .data)) orelse .js_undefined;and thentry this.format(tag, ..., data, ...). For anErrorEventit does the same with.error.fastGetis a normal property lookup, so an own-property override is honored.data(orerror) is the event itself,Tag.get(data)returns.Eventagain, and step 2 re-entersprintAs(.Event, value)with no visited-map check anywhere on the path.Why nothing else catches it
The visited-map guard in
printAsis gated entirely oncomptime Format.canHaveCircularReferences(). Since.Eventreturnsfalsethere, thegetOrPut/found_existingcheck never runs for the event value. The only other recursion limiter in this file is the per-type guard, and there is none for.Event. Note that indirect cycles through a plain object (e.g.ev.data = obj; obj.x = ev) are caught, because.Objectis guarded and insertsobjinto the map — only the direct Event→Event self-reference loops forever.Step-by-step proof
The
Object.definePropertyoverride is the same class of "contrived but fuzzer-reachable" input as the JSX repro this PR fixes (andinspect.test.jsalready exercisesObject.definePropertyonMessageEventfor the "deleted data" test, so it's a supported shape).Impact and fix
Impact: a snapshot/diff of such an event in
bun testcrashes the process with a native stack overflow instead of printing[Circular]. It's pre-existing and requires deliberately overriding a native getter, so it shouldn't block this PR — but since the fix is literally addingor tag == .Eventto the line already being edited, andConsoleObject.zigalready has it, it's worth doing here for parity.