Repository navigation
threading: don't touch WaitGroup after publishing count==0 in finish() #34458
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b165e5
threading: don't touch WaitGroup after publishing count==0 in finish()
robobun a9841a5
[autofix.ci] apply automated fixes
autofix-ci[bot] 97f2eba
test: validate the expected linker error in the catch block
robobun 4d06bed
Replace the bundler stress test with a WaitGroup unit test
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,79 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isASAN, tempDir } from "harness"; | ||
|
|
||
| // Regression guard for a heap-use-after-free observed under ASAN during | ||
| // back-to-back Bun.build() calls that enable sourcemaps and fail with a | ||
| // link-time "no matching export" error. `compute_data_for_source_map` | ||
| // schedules source-map tasks onto the work pool, then `link()` returns Err, | ||
| // and the caller waits on the source-map WaitGroups before dropping the | ||
| // heap-allocated BundleV2. The last worker's `WaitGroup::finish()` must not | ||
| // touch `self` after publishing count==0 because that lets `wait()` return | ||
| // and the owner free the WaitGroup; the slow-path `Mutex::lock()` that | ||
| // followed would then write to freed memory. The window needs the worker to | ||
| // be futex-parked in `Mutex::lock_slow` past the drop, so this is a stress | ||
| // loop rather than a deterministic probe. ASAN-only: release builds would | ||
| // just silently write to freed-but-unreused memory. | ||
| test.skipIf(!isASAN)( | ||
| "Bun.build sourcemap + link-time error drops BundleV2 only after the last source-map WaitGroup::finish() is done", | ||
| async () => { | ||
| const N_FILES = 8; | ||
| const N_ITER = 300; | ||
|
|
||
| const files: Record<string, string> = { | ||
| "package.json": `{"type":"module"}`, | ||
| }; | ||
| let entry = ""; | ||
| for (let i = 0; i < N_FILES; i++) { | ||
| entry += `import { v${i} } from "./leaf${i}";\n`; | ||
| } | ||
| entry += `export const out = [${Array.from({ length: N_FILES }, (_, i) => `v${i}`).join(",")}];\n`; | ||
| files["entry.ts"] = entry; | ||
| // leaf0 imports a binding that entry.ts does not export: a link-time | ||
| // error detected after source-map tasks are already scheduled. | ||
| files["leaf0.ts"] = `import { DOES_NOT_EXIST } from "./entry";\nexport const v0 = DOES_NOT_EXIST;\n`; | ||
| for (let i = 1; i < N_FILES; i++) { | ||
| files[`leaf${i}.ts`] = `export const v${i} = ${i};\n`; | ||
| } | ||
|
|
||
| // Inline runner so a single child process performs every iteration. | ||
| files["run.ts"] = ` | ||
| const { join } = require("path"); | ||
| const dir = process.argv[2]; | ||
| const iters = parseInt(process.argv[3], 10); | ||
| let failed = 0; | ||
| for (let i = 0; i < iters; i++) { | ||
| try { | ||
| await Bun.build({ | ||
| entrypoints: [join(dir, "entry.ts")], | ||
| sourcemap: "external", | ||
| outdir: join(dir, "out"), | ||
| }); | ||
| console.error("unexpected success at iter", i); | ||
| process.exit(1); | ||
| } catch { | ||
| failed++; | ||
| } | ||
| } | ||
| console.log("failed=" + failed + " of " + iters); | ||
| `; | ||
|
|
||
| using dir = tempDir("bun-build-sourcemap-link-error-uaf", files); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "run.ts", String(dir), String(N_ITER)], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| // ASAN aborts the child with a heap-use-after-free report on stderr when | ||
| // the race fires; otherwise every iteration rejects with a build error. | ||
| expect(stderr).not.toContain("AddressSanitizer"); | ||
| expect(stderr).not.toContain("heap-use-after-free"); | ||
| expect(stdout.trim()).toBe(`failed=${N_ITER} of ${N_ITER}`); | ||
| expect(exitCode).toBe(0); | ||
| }, | ||
| 60_000, | ||
|
Check warning on line 78 in test/bundler/bun-build-sourcemap-link-error-uaf.test.ts
|
||
|
robobun marked this conversation as resolved.
Resolved
|
||
| ); | ||
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.