-
Notifications
You must be signed in to change notification settings - Fork 5k
js_parser: guard stack depth when parsing nested JSX elements #31537
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
| import path from "node:path"; | ||
|
|
||
| // Regression test for a stack overflow in the TSX parser, found by fuzzing. | ||
| // | ||
| // `parse_jsx_element` recurses directly for every nested child element | ||
| // (`<a><b><c>...`), but unlike the other recursive parse entry points it never | ||
| // consulted the parser's stack guard. A source like `() => <div>` repeated | ||
| // thousands of times nests that many `<div>` children (the `() =>` between each | ||
| // pair is parsed as JSX text), so the unbounded recursion ran off the end of | ||
| // the stack and the process died on the guard page with a bare SIGSEGV — no | ||
| // crash handler, no error message. | ||
| // | ||
| // With the guard in place the parser stops and reports "Maximum call stack size | ||
| // exceeded" instead of crashing. The transpile runs in a child process so a | ||
| // regression fails these assertions rather than taking down the test runner. | ||
| test("deeply nested arrow/JSX does not overflow the stack", async () => { | ||
| // Each `() => <div>` adds one arrow frame and one JSX-element frame. This is | ||
| // far deeper than the fuzzer's ~23k repetitions so the guard fires well | ||
| // before the real stack end on both release and the larger debug frames. | ||
| const source = "() => <div>".repeat(50_000); | ||
|
Check warning on line 22 in test/bundler/transpiler/jsx-deep-nesting-stack-overflow.test.ts
|
||
|
|
||
| using dir = tempDir("jsx-deep-nesting-stack-overflow", { | ||
| "input.tsx": source, | ||
| "run.ts": ` | ||
| const src = require("node:fs").readFileSync(${JSON.stringify(path.join("input.tsx"))}, "latin1"); | ||
|
Check warning on line 27 in test/bundler/transpiler/jsx-deep-nesting-stack-overflow.test.ts
|
||
|
robobun marked this conversation as resolved.
Outdated
|
||
| try { | ||
| new Bun.Transpiler({ | ||
| loader: "tsx", | ||
| target: "bun", | ||
| minifyWhitespace: true, | ||
| deadCodeElimination: true, | ||
| }).transformSync(src); | ||
| console.log("NO ERROR"); | ||
| } catch (e) { | ||
| console.error(String((e as Error).message)); | ||
| } | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "run.ts"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| // Must terminate on its own, not be killed by the stack-guard-page SIGSEGV. | ||
| expect(proc.signalCode).toBeNull(); | ||
| // The parser bounds the recursion and throws a catchable SyntaxError. | ||
| expect(stderr).toContain("Maximum call stack size exceeded"); | ||
| expect(stdout).not.toContain("NO ERROR"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.