forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 55
JSON.stringify: give the DynamicBuffer fast path a depth limit so cyclic values fail fast #540
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
Open
robobun
wants to merge
1
commit into
main
Choose a base branch
from
robobun/93d6e106/fast-stringifier-depth-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−4
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // The DynamicBuffer FastStringifier bails to the general Stringifier past a | ||
| // depth limit, so a cyclic value is rejected after bounded work instead of | ||
| // growing the buffer toward the 2GB string length limit. This test checks the | ||
| // behavior around that limit: cycles still throw TypeError, and values nested | ||
| // deeper than the limit still stringify correctly through the general path. | ||
|
|
||
| function shouldThrowTypeError(fn) { | ||
| let threw = null; | ||
| try { | ||
| fn(); | ||
| } catch (e) { | ||
| threw = e; | ||
| } | ||
| if (!(threw instanceof TypeError)) | ||
| throw new Error("expected TypeError, got " + threw); | ||
| } | ||
|
|
||
| function makeCyclicObject(keys) { | ||
| const o = {}; | ||
| for (let i = 0; i < keys; i++) | ||
| o["k" + i] = "y".repeat(50); | ||
| o.self = o; | ||
| return o; | ||
| } | ||
|
|
||
| // A payload large enough that the StaticBuffer attempt fails with BufferFull | ||
| // and the DynamicBuffer attempt runs. | ||
| const bigPayload = "y".repeat(10 * 1024); | ||
|
|
||
| // Cyclic values throw, flat and with a gap, through objects and arrays, for | ||
| // direct and indirect cycles. | ||
| for (const space of [undefined, 2, "\t"]) { | ||
| for (const keys of [0, 8]) | ||
| shouldThrowTypeError(() => JSON.stringify(makeCyclicObject(keys), null, space)); | ||
|
|
||
| shouldThrowTypeError(() => JSON.stringify([makeCyclicObject(1)], null, space)); | ||
|
|
||
| const selfArray = [1]; | ||
| selfArray.push(selfArray); | ||
| shouldThrowTypeError(() => JSON.stringify(selfArray, null, space)); | ||
| } | ||
|
|
||
| // Cycles that carry a large payload per revolution. | ||
| for (const space of [undefined, 2]) { | ||
| const a = { payload: bigPayload }; | ||
| const b = { a }; | ||
| a.b = b; | ||
| shouldThrowTypeError(() => JSON.stringify(a, null, space)); | ||
|
|
||
| const direct = { payload: bigPayload }; | ||
| direct.self = direct; | ||
| shouldThrowTypeError(() => JSON.stringify(direct, null, space)); | ||
| } | ||
|
|
||
| // Acyclic values nested deeper than any reasonable fast path depth limit | ||
| // round-trip correctly, flat and with a gap, for objects and arrays, with | ||
| // 8-bit and 16-bit content. | ||
| for (const space of [undefined, 1]) { | ||
| for (const leafValue of ["eight-bit", "sixte\u00e9n-bit \u2603"]) { | ||
| const depth = 1000; | ||
|
|
||
| let root = {}; | ||
| let node = root; | ||
| for (let i = 0; i < depth; i++) | ||
| node = node.x = {}; | ||
| node.leaf = leafValue; | ||
| let parsed = JSON.parse(JSON.stringify(root, null, space)); | ||
| let p = parsed; | ||
| for (let i = 0; i < depth; i++) | ||
| p = p.x; | ||
| if (p.leaf !== leafValue) | ||
| throw new Error("deep object round-trip broken for space=" + space); | ||
|
|
||
| let arrayRoot = []; | ||
| let arrayNode = arrayRoot; | ||
| for (let i = 0; i < depth; i++) { | ||
| const next = []; | ||
| arrayNode.push(next); | ||
| arrayNode = next; | ||
| } | ||
| arrayNode.push(leafValue); | ||
| parsed = JSON.parse(JSON.stringify(arrayRoot, null, space)); | ||
| p = parsed; | ||
| for (let i = 0; i < depth; i++) | ||
| p = p[p.length - 1]; | ||
| if (p[0] !== leafValue) | ||
| throw new Error("deep array round-trip broken for space=" + space); | ||
| } | ||
| } |
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.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
Check the limit only before entering another container.
m_depthincrements after{or[is written. A scalar in the 512th container callsappend()withm_depth == 512, so this branch fails the fast path even though serialization does not enter depth 513. Empty values at the same nesting remain fast, but equivalent non-empty acyclic values fall back toStringifier.Move this check into the object and array cases before their depth increment.
🤖 Prompt for AI Agents