-
Notifications
You must be signed in to change notification settings - Fork 5k
jsc: throw RangeError from JSON.parse when a string value cannot be allocated #35194
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
6
commits into
main
Choose a base branch
from
farm/50a7f08f/json-parse-oom-throw
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.
+150
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f2722e
jsc: throw RangeError from JSON.parse when a string value cannot be a…
robobun 4d147eb
test: cover array/object string values and tighten exit-code assertions
robobun 20661c1
[autofix.ci] apply automated fixes
autofix-ci[bot] d770417
test: run fixture cases concurrently, add reviver shape, drop per-tes…
robobun 4c0fd83
test: require every shape in the matrix to reach JSON.parse
robobun 4b69972
build: bump WebKit to the tryMakeJSString rename (preview-pr-317-23fb…
robobun 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,88 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isASAN, isDebug, isLinux } from "harness"; | ||
| import { join } from "node:path"; | ||
|
|
||
| // JSON.parse copies every string value longer than 16 chars into a fresh | ||
| // StringImpl. That copy used the crash-or-succeed StringImplMalloc::malloc | ||
| // path, so a near-OOM process died with SIGILL inside fastCompactMalloc | ||
| // instead of throwing. The fix routes through StringImpl::tryCreateUninitialized | ||
| // and surfaces a RangeError, same as "x".repeat(tooLarge). | ||
| // | ||
| // RLIMIT_AS is the only portable way to make tryMalloc actually fail, and it | ||
| // cannot be combined with AddressSanitizer's 16 TB shadow reservation. Debug | ||
| // builds take several seconds per fixture just to zero-fill and toString() the | ||
| // ~200 MB inputs, so this runs only on release Linux lanes. | ||
| test.skipIf(!isLinux || isASAN || isDebug)( | ||
| "JSON.parse throws RangeError instead of crashing when a string value cannot be allocated", | ||
| async () => { | ||
| const fixture = join(import.meta.dir, "json-parse-oom-fixture.js"); | ||
| const limitKiB = 5 * 1024 * 1024; | ||
| // The filler loop in the fixture exhausts address space down to N/4, so any | ||
| // size should hit the OOM path, but mimalloc's arena layout varies. Sweep a | ||
| // couple of sizes and every parsePrimitiveValue caller (top-level literal, | ||
| // reviver-mode literal, array element, object property value); every run | ||
| // that reaches INPUT-OK must either succeed or throw RangeError, never | ||
| // crash. The fixture's filler uses allocUnsafe, which reserves address | ||
| // space without committing pages, so the children's combined RSS stays | ||
| // well under the host's memory and they can run concurrently. | ||
| const cases: Array<[shape: string, mb: number]> = [ | ||
| ["root", 200], | ||
| ["root", 400], | ||
| ["array", 300], | ||
| ["object", 300], | ||
| ["reviver", 300], | ||
| ]; | ||
|
|
||
| const results = await Promise.all( | ||
| cases.map(async ([shape, mb]) => { | ||
| const size = mb * 1024 * 1024; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| "/bin/sh", | ||
| "-c", | ||
| `ulimit -v ${limitKiB} && ulimit -c 0 && exec "$0" "$1" "$2" "$3"`, | ||
| bunExe(), | ||
| fixture, | ||
| String(size), | ||
| shape, | ||
| ], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { shape, size, stdout, stderr, exitCode, signal: proc.signalCode }; | ||
| }), | ||
| ); | ||
|
|
||
| let sawCaught = false; | ||
| const reachedShapes = new Set<string>(); | ||
| for (const { shape, size, stdout, stderr, exitCode, signal } of results) { | ||
| if (!stdout.includes("INPUT-OK")) continue; | ||
| reachedShapes.add(shape); | ||
|
|
||
| // Once the input is built, JSON.parse must not kill the process. | ||
| expect({ shape, size, stdout: stdout.trim(), stderr: stderr.trim(), exitCode, signal }).toMatchObject({ | ||
| shape, | ||
| size, | ||
| signal: null, | ||
| }); | ||
|
|
||
| if (stdout.includes("CAUGHT:")) { | ||
| expect(stdout).toContain("CAUGHT:RangeError:Out of memory"); | ||
| expect(exitCode).toBe(0); | ||
| sawCaught = true; | ||
| } else { | ||
| expect(stdout).toContain("PARSED"); | ||
| expect(exitCode).toBe(1); | ||
| } | ||
| } | ||
|
|
||
| // Every shape must reach JSON.parse; a SETUP-FAIL means the address-space | ||
| // cap was too tight for that case and nothing was exercised there. | ||
| expect([...reachedShapes].sort()).toEqual([...new Set(cases.map(([s]) => s))].sort()); | ||
| // And at least one of those runs must have taken the out-of-memory branch, | ||
| // otherwise the sweep never exercised the path this test is for. | ||
| expect(sawCaught).toBe(true); | ||
| }, | ||
| ); | ||
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.