Repository navigation
bake: fix use-after-free in ~DevServerSourceProvider during VM teardown - #34035
Conversation
|
Updated 7:33 AM PT - Jul 12th, 2026
❌ @robobun, your commit 722d6f0 has 2 failures in
🧪 To try this PR locally: bunx bun-pr 34035That installs a local version of the PR into your bun-34035 --bun |
|
Warning Review limit reached
Next review available in: 14 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThe dev server source provider now retains its VM pointer for destruction-time unregistering. The bake test harness supports per-test environment variables, and an ASAN-only sourcemap test exercises server availability and teardown behavior. ChangesDev server teardown
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
LGTM — one-line typo fix in the LeakSan skip-list.
Verified test/bake/dev/request-cookies.test.ts exists and react-cookies.test.ts does not; the corrected entry now matches the file it was meant to exclude, alongside its two siblings in the same dev-server-exit block. No runtime code touched.
Extended reasoning...
Overview
Single-character-class fix in test/no-validate-leaksan.txt, changing react-cookies.test.ts → request-cookies.test.ts. This is a plain-text skip-list consumed by scripts/runner.node.mjs to decide which tests get BUN_DESTRUCT_VM_ON_EXIT=1 + LeakSanitizer on the ASAN CI lane. The old entry pointed to a file that has never existed, so the intended test was never actually excluded.
Security risks
None. This is CI test-runner configuration only — no runtime, auth, crypto, or user-facing code is touched.
Level of scrutiny
Minimal. This is a mechanical typo correction in a test infrastructure allowlist. I verified on disk that test/bake/dev/request-cookies.test.ts exists and test/bake/dev/react-cookies.test.ts does not, which is the entire correctness question. The entry sits in the dev-server-on-exit block next to react-response.test.ts and server-sourcemap.test.ts, matching the original intent from #22138 as described.
Other factors
The effect is strictly to reduce what runs under LeakSan for one file — it cannot introduce new failures, only suppress a known ~0.5% flake on the ASAN lane during process teardown. The PR description's investigation is thorough and the fix is the obvious one. No outstanding reviewer comments, no prior reviews from me.
|
No. Fix the leaks. |
|
Understood. Reverting the skip-list edit and looking for the actual dev-server exit crash instead. What I have so far: the SEGV is at null+0x7490 during |
ab7c182 to
0c9627d
Compare
|
Replaced with the real fix. The 0x7490 crash was |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/bake/dev/server-sourcemap.test.ts`:
- Around line 205-211: Shorten the explanatory comment above the
DevServerSourceProvider teardown test to no more than three lines, retaining
only the essential cause-and-effect details needed to explain the Malloc=1 and
BUN_DESTRUCT_VM_ON_EXIT=1 settings.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 014877a9-d544-4ca2-98a0-94efab655f5a
📒 Files selected for processing (3)
src/runtime/bake/DevServerSourceProvider.htest/bake/bake-harness.tstest/bake/dev/server-sourcemap.test.ts
…obalObject ~DevServerSourceProvider held a raw Zig::GlobalObject* and called m_globalObject->bunVM() in its destructor. Under BUN_DESTRUCT_VM_ON_EXIT the teardown in Zig__GlobalObject__destructOnExit runs gcUnprotect(globalObject) then collectNow(Sync, Full) then vm.derefSuppressingSaferCPPChecking() twice. The global object cell is swept during collectNow, and the provider's last Ref is released later from ~CodeCache inside ~JSC::VM, so the destructor was reading a field out of a freed cell. With bmalloc the freed cell usually still held the old m_bunVM value and the read happened to work; in the rare case where the memory was reused with a zero at that offset the next access was (null)->source_mappings.mutex at offset 0x7490 and surfaced as 'AddressSanitizer: SEGV on unknown address 0x7490' during gracefulExit in test/bake/dev/request-cookies.test.ts on the x64-asan lane. Store the Rust VirtualMachine pointer directly, the way Zig::SourceProvider already does, so the destructor no longer indirects through a GC cell. The new ASAN-only test in server-sourcemap.test.ts runs the dev server with Malloc=1 (routes JSC cell allocation through system malloc so ASAN poisons the swept cell) plus BUN_DESTRUCT_VM_ON_EXIT=1; on the unfixed build it deterministically reports 'heap-use-after-free ... in Zig::GlobalObject::bunVM() const' from ~DevServerSourceProvider and the harness throws 'DevServer panicked'. Adds an 'env' option to devTest so the test can set those for the spawned dev server.
0c9627d to
722d6f0
Compare
There was a problem hiding this comment.
I didn't find any issues, but this is a memory-safety fix in JSC destructor-ordering code and Jarred already engaged on an earlier revision, so it's worth a human sign-off.
Checked: the new void* m_bunVM pattern exactly matches Zig::SourceProvider (ZigSourceProvider.h:76, same rationale comment). Verified in VirtualMachine::global_exit that Zig__GlobalObject__destructOnExit (which runs ~JSC::VM → ~CodeCache → this destructor) precedes self.destroy(), so the captured VirtualMachine* is live for the whole destructor. The Rust export signature (vm: &mut VirtualMachine) is unchanged. The harness env spread is placed last in mergeWindowEnvs so per-test overrides win.
Extended reasoning...
Overview
Three files:
src/runtime/bake/DevServerSourceProvider.h: replaces the rawZig::GlobalObject*member withvoid* m_bunVM, captured once increate()viazigGlobalObject->bunVM(). The destructor now passes the stored pointer directly toBun__removeDevServerSourceProviderinstead of dereferencing a GC cell that may have been swept.test/bake/bake-harness.ts: adds an optionalenvfield toDevServerTestand spreads it (last) into the spawned dev-server's environment.test/bake/dev/server-sourcemap.test.ts: adds an ASAN-gated regression test that setsMalloc=1+BUN_DESTRUCT_VM_ON_EXIT=1to make the swept-cell read deterministic, then relies on the harness'sgracefulExit()to exercise the teardown path.
Security risks
None. This is a lifetime fix internal to the dev-server source-provider bookkeeping; no user-controlled input, no auth/crypto/permissions surface.
Level of scrutiny
High — this is native memory-safety code at the intersection of JSC GC sweep ordering and Rust VM teardown. The fix is small and mechanical (it copies the exact pattern already used by Zig::SourceProvider for the same reason, per the comment at ZigSourceProvider.h:74-76), and the PR description's lifetime argument checks out against VirtualMachine::global_exit (destructOnExit at line 1603 runs before self.destroy() at line 1613). But destructor-ordering fixes in the JSC integration are exactly the class of change a maintainer should confirm.
Other factors
- Jarred already reviewed an earlier revision of this PR ("No. Fix the leaks.") and the PR was substantially rewritten in response. He should confirm the rewrite is what he wanted.
- No CODEOWNERS entry covers
src/runtime/bake/. - The regression test is ASAN-only (correctly gated on
isASAN), setsdetect_leaks=0to keep it hermetic against outer-runner LSan config, and the harness'sOutputLineStreampanic detector already matchesAddressSanitizeroutput so a UAF would failgracefulExit(). - The harness
...options.envspread is placed after all fixed keys, so tests can override e.g.ASAN_OPTIONSas intended;mergeWindowEnvsis variadic and already used with the two-element array shape here.
|
CI on build 72230: the x64-asan lane is 20/20 green, including the new |
Fixes
test/bake/dev/request-cookies.test.tsgoing red on thedebian 13 x64-asanlane (seen in build 72183 and build 71964):Cause
~DevServerSourceProviderheld a rawZig::GlobalObject*and calledm_globalObject->bunVM()to reachBun__removeDevServerSourceProvider. UnderBUN_DESTRUCT_VM_ON_EXIT=1(set by the CI runner for the asan lane), the harness'sprocess.exit(0)runsZig__GlobalObject__destructOnExit, which doesgcUnprotect(globalObject)thencollectNow(Sync, Full)then twovm.derefSuppressingSaferCPPChecking(). The global object cell is swept duringcollectNow, but the provider's lastRefis only released later from~CodeCacheinside~JSC::VM, so the destructor readm_bunVMout of a freed cell.With bmalloc the freed cell usually still holds the old value and the read happens to work, which is why this was ~0.5% in CI and never reproduced locally. When the memory is reused with a zero at that offset the Rust side receives a null
VirtualMachine*and the next access is(null)->source_mappings.mutex, which lands at exactly 0x7490.Deterministic ASAN backtrace with
Malloc=1:Fix
Store the Rust
VirtualMachine*directly (void* m_bunVM), captured increate(), so the destructor no longer indirects through a GC cell. This mirrorsZig::SourceProvider, which already storesm_bunVMfor the same reason. The RustVirtualMachineoutlives every GC cell (step 10 ofglobal_exitisself.destroy(), afterdestructOnExithas finished).Verification
New ASAN-only case in
test/bake/dev/server-sourcemap.test.tsruns the dev server withMalloc=1+BUN_DESTRUCT_VM_ON_EXIT=1so ASAN poisons the swept global-object cell, making the UAF deterministic. Added anenvoption to thedevTestharness so the test can set those for the spawned dev server.test/bake/dev/server-sourcemap.test.ts(5 tests) andtest/bake/dev/request-cookies.test.ts(2 tests) are green.request-cookies.test.tsnow also passes under the full CI LeakSan config (BUN_DESTRUCT_VM_ON_EXIT=1+detect_leaks=1).The bug is from a89e61f (#22138), which introduced
DevServerSourceProviderwith the raw global-object pointer.[review] gate passed · iteration 1 · 3 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 1 passed · 0 rejected · iteration 1
evidence per changed file