-
Notifications
You must be signed in to change notification settings - Fork 5k
console: guard AggregateError .errors recursion (cycle, depth, tampered property) #35825
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
+246
−28
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1e26674
console: guard AggregateError .errors recursion with visited set, sta…
robobun ad9a91e
[autofix.ci] apply automated fixes
autofix-ci[bot] 14c5111
trim advisory comments in the AggregateError guard block
robobun ebdaed7
test: drop depth-marker assertion for Bun.inspect (stack-address depe…
robobun b7b1a39
test: run depth block concurrently; file now ~9s under debug+ASAN
robobun c3df429
ci: retrigger
robobun c4ec505
gate: retrigger (release build infra timeout)
robobun 0eaf953
test: update jsx-template-string-crash snapshots for AggregateError h…
robobun 747b909
console: let the header-print RangeError propagate instead of enterin…
robobun de1b576
console: gate .errors iteration on pending exception only, not format…
robobun 055856f
console: only preserve the for_each exception when the stack guard ac…
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // Error-graph cycle / deep-chain segfaults in the native error printer. | ||
| // The AggregateError `errors` recursion had no stack check and no visited | ||
| // set, so self/mutual cycles and very deep nesting hit the stack guard page | ||
| // (silent SIGSEGV) via `print_errorlike_object` -> `for_each` -> `agg_iter`. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| type Shape = { name: string; build: string }; | ||
| type Sink = { name: string; wrap: (b: string) => string; allowFail: boolean }; | ||
|
|
||
| const shapes: Shape[] = [ | ||
| { | ||
| name: "self-cycle", | ||
| build: `const ae = new AggregateError([], "self"); ae.errors = [ae]; const e = ae;`, | ||
| }, | ||
| { | ||
| name: "mutual-cycle", | ||
| build: `const a = new AggregateError([], "A"); const b = new AggregateError([], "B"); a.errors = [b]; b.errors = [a]; const e = a;`, | ||
| }, | ||
| { | ||
| name: "deleted-errors", | ||
| build: `const ae = new AggregateError([new Error("x")], "del"); delete ae.errors; const e = ae;`, | ||
| }, | ||
| { | ||
| name: "accessor-errors", | ||
| build: `const ae = new AggregateError([], "acc"); Object.defineProperty(ae, "errors", { get() { throw new Error("boom"); } }); const e = ae;`, | ||
| }, | ||
| { | ||
| name: "non-iterable-errors", | ||
| build: `const ae = new AggregateError([], "ni"); ae.errors = 42; const e = ae;`, | ||
| }, | ||
| { | ||
| name: "mixed-agg-cause", | ||
| build: `const a = new AggregateError([], "A"); const c = new Error("C"); a.errors = [c]; c.cause = a; const e = a;`, | ||
| }, | ||
| ]; | ||
|
|
||
| const sinks: Sink[] = [ | ||
| { name: "console.log", wrap: b => `${b} console.log(e);`, allowFail: false }, | ||
| { name: "console.error", wrap: b => `${b} console.error(e);`, allowFail: false }, | ||
| { name: "Bun.inspect", wrap: b => `${b} Bun.inspect(e);`, allowFail: false }, | ||
| { name: "uncaught-throw", wrap: b => `${b} throw e;`, allowFail: true }, | ||
| { | ||
| name: "unhandled-reject", | ||
| wrap: b => `${b} Promise.reject(e); await 1;`, | ||
| allowFail: true, | ||
| }, | ||
| { | ||
| name: "uncaughtException-handler", | ||
| wrap: b => `process.on("uncaughtException", err => { console.error(err); process.exit(0); }); ${b} throw e;`, | ||
| allowFail: false, | ||
| }, | ||
| ]; | ||
|
|
||
| describe.concurrent("error-graph cycles do not crash the printer", () => { | ||
| for (const shape of shapes) { | ||
| for (const sink of sinks) { | ||
| const cell = `${shape.name} x ${sink.name}`; | ||
| test(cell, async () => { | ||
| const code = sink.wrap(shape.build); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "--no-install", "-e", code], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| if (proc.signalCode) { | ||
| throw new Error( | ||
| `crashed with ${proc.signalCode}\nstdout: ${stdout.slice(0, 300)}\nstderr: ${stderr.slice(0, 300)}`, | ||
| ); | ||
| } | ||
| if (sink.allowFail) { | ||
| expect(exitCode).toBeLessThan(128); | ||
| } else { | ||
| if (exitCode !== 0) { | ||
| throw new Error(`exit ${exitCode}\nstdout: ${stdout.slice(0, 300)}\nstderr: ${stderr.slice(0, 300)}`); | ||
| } | ||
| expect(exitCode).toBe(0); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Release bun segfaults at ~2000 levels. | ||
| describe.concurrent("error-graph depth does not crash the printer", () => { | ||
| const deepAgg = `let x = new AggregateError([], "leaf"); for (let i = 0; i < 3000; i++) x = new AggregateError([x], "n" + i); const e = x;`; | ||
| const deepCause = `let x = new Error("leaf"); for (let i = 0; i < 3000; i++) x = new Error("n" + i, { cause: x }); const e = x;`; | ||
|
|
||
| for (const sink of sinks) { | ||
| test(`deep-aggregate x ${sink.name}`, async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "--no-install", "-e", sink.wrap(deepAgg)], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(proc.signalCode).toBeFalsy(); | ||
| // On can_throw_stack_overflow sinks (Bun.inspect / console.*) the | ||
| // RangeError propagates like it does for a deep cause chain. | ||
| expect(exitCode).toBeLessThan(128); | ||
| }); | ||
| } | ||
|
|
||
| // The cause-chain depth guard exists but was inert on the uncaught / | ||
| // rejection path because the formatter's stack_check was never seated. | ||
| for (const sink of sinks.filter(s => s.allowFail)) { | ||
| test(`deep-cause x ${sink.name}`, async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "--no-install", "-e", sink.wrap(deepCause)], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(proc.signalCode).toBeFalsy(); | ||
| expect(exitCode).toBeLessThan(128); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe.concurrent("AggregateError printer output", () => { | ||
| test("self-cycle renders [Circular] and includes the header", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| bunExe(), | ||
| "-e", | ||
| `const ae = new AggregateError([], "outer"); ae.errors = [ae]; process.stdout.write(Bun.inspect(ae));`, | ||
| ], | ||
| env: { ...bunEnv, NO_COLOR: "1" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toContain("[Circular]"); | ||
| expect(stdout).toContain("outer"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test("uncaught AggregateError prints its own message", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", `throw new AggregateError([new Error("inner")], "outer message");`], | ||
| env: { ...bunEnv, NO_COLOR: "1" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toContain("outer message"); | ||
| expect(stderr).toContain("inner"); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test("deleted errors property prints header", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| bunExe(), | ||
| "-e", | ||
| `const ae = new AggregateError([new Error("x")], "msg"); delete ae.errors; process.stdout.write(Bun.inspect(ae));`, | ||
| ], | ||
| env: { ...bunEnv, NO_COLOR: "1" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(stdout).toContain("msg"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.