Skip to content

Bun.build / Bun.Transpiler: remove unsafe from JSBundler, the completion task and JSTranspiler - #40405

Open
Jarred-Sumner wants to merge 3 commits into
claude/modload-zero-unsafefrom
claude/jsbundler-zero-unsafe
Open

Jarred-Sumner wants to merge 3 commits into
claude/modload-zero-unsafefrom
claude/jsbundler-zero-unsafe

Conversation

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

What

Same programme as #40055 … #40399, applied to the JS-facing bundler API. Stacked on #40383 (base branch claude/modload-zero-unsafe, for bun_jsc::Job / JobTranspiler); retarget to main once that lands. Bundler internals (bundle_v2/ParseTask/LinkerContext) are out of scope here — only the completion-task/plugin boundary is typed.

src/runtime/api/JSBundler.rs 27 → 1 (all-safe fn extern block), js_bundle_completion_task.rs 26 → 0, JSTranspiler.rs 22 → 0.

  • JSBundleCompletionTask is split into a JS-side JSBundleCompletionTask (RefCounted, typed build_ref slot, completion delivered via a TaskHop on its ThisPtr, registered as ActiveHandle::Bundle for VM teardown), a bundle-side BundleJob (Send by construction, owned and dropped by the bundle thread), and BuildShared (Arc: cancel/stage atomics, Guarded<Option<Ticket>>, Guarded<Option<Box<BuildOutcome>>>). The old Releasing spin state is gone since the halves are separate allocations. bun_bundler::BundleThread takes Box<C> and drops it on the bundle thread; bundle_v2::dispatch::CompletionDispatch trait + Arc<dyn CompletionDispatch> replace the vtable struct and opaque marker; BundleV2.file_map: Option<Arc<FileMap>>, BundleOptions.optimize_imports: Option<Arc<StringSet>>.
  • Plugin bridge: JSBundlerPlugin__onResolveAsync/onLoadAsync/onDefer/addErrorResolve/addErrorLoad/answerCancelledResolve/answerCancelledLoad are HOST_EXPORTs taking &mut Resolve/&mut Load/&JSGlobalObject; C++ JSBundlerPlugin callbacks take the global instead of the removed config pointer (JSBundlerPlugin__setConfig removed); Resolve/Load::{answer, plugin(), notify_deferred} live on the bundler side, null-checked.
  • Bun.Transpiler holds JsCell<OwnedTranspiler>; per-call re-aiming through with_call/TranspilerCall (restricted handle, restores the previous arena/log so nested calls match); TransformTask is Send by construction (tsconfig bits snapshotted at schedule; JobTranspiler copy via OwnedTranspiler::new_job, with a job-copy-alive assert on with_mut).
  • Primitives: bun_bundler::transpiler::{OwnedTranspiler, TranspilerCall} (OwnedTranspiler is !Send/!Sync; inner Send/Sync with SAFETY), BundleThread::singleton::{enqueue(Box<C>), wake}, bun_threading::intrusive_link!, bun_event_loop::TaskHop + dispatch hop!/release_hop! (same hunks as fetch: remove unsafe from FetchTasklet and fetch.rs #40202).

Pre-existing issues noticed (unchanged): Transpiler::for_worker deep-clones BundleOptions per pool worker per Bun.build (~4 % of bundle-thread instructions); html_build_task's RefPtr still leaks on the cancelled-at-teardown path.

Perf (release, taskset -c 56-63, perf stat -e instructions:u, interleaved): transpiler.transformSync ×1e4 +0.01 %. Bun.build of a 50-file project ×30 +0.32 %, ×100 +0.29–0.30 %; startup-only −0.15 %. Symbol-level sampling puts the delta in Transpiler::for_worker and each_impl::<scan_imports_and_exports> on the bundle thread — neither changed here (consistent with LTO layout perturbation from the BundleOptions field-type change); the JS thread is < 1 % of the profile. This is at the 0.3 % bar, stated rather than rounded away.

Testing

Debug+ASAN: bun-build-api 56/56, bundler_plugin 56, bundler_defer 12, native-plugin 19, plugins.test 47, bundler_compile 74/75 (HelloWorldWithProcessVersionsBun fails identically on a main debug build — debug version string), bundler_compile_autoload 23, bundler_splitting 13, bun-build-compile 15, -sourcemap 9, -wasm 1, transpiler.test.js 190, repl-transform 36, tsconfig-uaf 3, error-gc-uaf 1, utf16-loader 5, parse-error-column 7, unsupported-loader 33, macro-test 22, decorators 24, decorator-metadata 5, jsx-tsconfig 2, bundler_html 22, bake/dev/plugins 3, esbuild/default 151/0, source lints. Hand-driven: Bun.build ×200 sequential / ×20 concurrent (temporary counter: both halves back to 0), onLoad throw / reject / never-resolves-then-exit, onResolve external, .defer(), Bun.build({compile}) (binary runs), files + optimizeImports, a Worker terminated mid-build ×5 + completed ×1 (all released), transform() ×1e4 concurrent, transformSync/scan/scanImports/macros — exit 0, no ASAN output. clippy clean on bundler/runtime/threading/event_loop; rust-check-all windows-msvc + aarch64-darwin pass.

@robobun

robobun commented Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator
Updated 12:59 PM PT - Sep 8th, 2026

✅ @Jarred-Sumner, your commit 756dc44dda31c14abe77e74f54d087daa180ff32 passed in Build #113136! 🎉


🧪   To try this PR locally:

bunx bun-pr 40405

That installs a local version of the PR into your bun-40405 executable, so you can run:

bun-40405 --bun

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR and the bug hunt found no issues. Given the scope — splitting the completion task into three cross-thread halves, new OwnedTranspiler/TaskHop abstractions, reworked teardown/cancellation flow, C++↔Rust FFI signature changes, and a stated ~0.3 % Bun.build regression — a human pass on the ownership model and perf trade-off is still worthwhile.

What was reviewed: the BundleJob/BuildShared split and Box<C> handoff to the bundle thread (drop-unrun path, ticket release ordering in complete_on_bundle_thread); stop_for_vm_teardown for both Queued→Released and Started stages against the removed Releasing spin; OwnedTranspiler::with_call restore-on-drop and the with_mut job-copy-alive assert; the C++ plugin callback signature change (global replaces config) across all call sites and the split addError/answerCancelled HOST_EXPORTs.

Extended reasoning...

Overview

This PR continues the zero-unsafe programme across the JS-facing bundler surface: JSBundler.rs (27→1), js_bundle_completion_task.rs (26→0), JSTranspiler.rs (22→0). The completion task is split into a JS-thread JSBundleCompletionTask (RefCounted, build_ref self-keepalive, TaskHop delivery), a bundle-thread BundleJob (Box<C> owned and dropped there), and an Arc<BuildShared> (cancel/stage atomics, Guarded<Option<Ticket>>, outcome slot). Bun.Transpiler is rebuilt around a new OwnedTranspiler that owns its arena and lends a lifetime-narrowed TranspilerCall per host-fn call. The plugin bridge callbacks become typed HOST_EXPORTs taking &mut Resolve/&mut Load + &JSGlobalObject, and the C++ BundlerPlugin.config pointer is removed. New primitives: bun_event_loop::TaskHop, bun_threading::intrusive_link!, singleton::wake() (process-wide bundle-loop wake replacing per-task bundle_loop).

Security risks

None identified. No user-input parsing, auth, or crypto is touched. The changes are internal ownership/lifetime restructuring; the FFI surface is narrowed (typed references replace *mut c_void).

Level of scrutiny

High. This is a substantial architectural refactor of cross-thread memory ownership in a hot, memory-safety-critical path (Bun.build completion, plugin dispatch, VM teardown). It introduces several new abstractions (OwnedTranspiler with hand-justified unsafe impl Send/Sync on its inner, TaskHop, the three-way completion split), removes a synchronization state (Releasing spin) by changing the allocation model, and reshapes the teardown protocol. The author flags a ~0.3 % Bun.build instruction-count regression attributed to LTO layout perturbation — that is at the bar where a maintainer should weigh in. The PR is also stacked on #40383.

Other factors

The PR description is thorough and the test matrix is broad (bun-build-api, bundler_plugin/defer, transpiler, macros, worker-terminated-mid-build, ASAN clean). The bug-hunt pass examined the ticket-release ordering in complete_on_bundle_thread vs stop_for_vm_teardown, the OwnedTranspiler restore guard (correctly restores previous arena/log so nested with_call is safe), the new_job re-aiming to home arena/log (so a job created inside a with_call doesn't alias the call's temporaries), and the C++ side's addError/answerCancelled split — all checked out. No prior reviews on the timeline. Deferring because the design surface (new abstractions, perf trade-off, teardown protocol change) warrants human sign-off even absent concrete findings.

Comment thread src/bundler/bundle_v2.rs Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sweeping the vestigial { } blocks — confirmed gone at all three FileMap sites. I re-ran over the full diff and didn't find any bugs; given the scope (thread-hop ownership rework, unsafe impl Send on JobTranspiler, C++ plugin-bridge signature changes), a human pass is still worthwhile.

What was reviewed:

  • OwnedTranspiler's Arc<UnsafeCell<..>> aliasing contract and the with_mut job-copy-alive assert vs. new_job retainer — no window where a live JobTranspiler observes reconfiguration.
  • BundleThread::singleton::enqueue(Box<C>) drop-on-bundle-thread vs. the removed free_released_unstarted — every enqueue path transfers ownership before the fallible wake.
  • TaskHop/hop!/release_hop! wiring for JSBundleCompletionTask against stop_for_vm_teardown(ThisPtr) — cancel path settles the promise slot and drops BuildShared on the JS thread.
  • JSBundlerPlugin.{cpp,h} host-export signatures vs. the Rust extern block — arity/types line up after config pointer removal.
Extended reasoning...

Overview

This PR reworks ownership and thread-hop plumbing for the JS-facing bundler/transpiler API: JSBundleCompletionTask is split into a JS-side RefCounted half, a Send bundle-side BundleJob, and an Arc<BuildShared>; BundleThread now takes Box<C> and drops on the bundle thread; a new OwnedTranspiler wraps Transpiler<'static> in Arc<UnsafeCell<..>> with with_mut/with_call/new_job accessors and an unsafe impl Send on JobTranspiler; the C++ JSBundlerPlugin bridge drops the opaque config pointer in favor of HOST_EXPORTs taking &mut Resolve/&mut Load/&JSGlobalObject; and TaskHop/task_hop!/intrusive_link!/hop!/release_hop! primitives are added to type the concurrent-task dispatch. Net ~1500 lines churned across 20 files, with JSBundler.rs/JSTranspiler.rs/js_bundle_completion_task.rs heavily rewritten and 10 entries dropped from the vm-thread-door lint inventory.

Security risks

No user-facing input parsing or auth/crypto surface changes. The risk profile is entirely memory-safety and thread-affinity: Arc<UnsafeCell<Transpiler<'static>>> with hand-written Send/Sync bounds, Box<C> ownership transfer across threads with an AtomicPtr LOOP + wake(), and FFI signature changes on both sides of the Rust↔C++ plugin bridge. A mismatch in any of these is UAF/data-race territory rather than a security bypass, but it is exactly the class REVIEW.md flags as most-blocked.

Level of scrutiny

High. This is a large refactor of cross-thread ownership with new unsafe impl Send, an UnsafeCell aliasing contract enforced by convention + a debug assert, and coordinated changes to a C++ FFI boundary. The PR description's test matrix is thorough (ASAN+debug across the bundler/transpiler suites, hand-driven cancellation/teardown/concurrent runs, clippy, cross-target check), and the change is part of a series following an established pattern (#40055…#40399), but the invariants (JobTranspiler retainer keeps the aliased config alive; with_mut only when no job copies out; completion halves dropped on their owning threads) are the kind a maintainer should read the SAFETY comments for directly.

Other factors

Stacked on #40383 (base claude/modload-zero-unsafe), so a human needs to confirm the base landed before retarget anyway. The one prior inline nit from this bot (vestigial { } around the collapsed FileMap if let chain) was addressed in 610714e — verified at the remaining self.file_map.as_deref().and_then(...) sites. The earlier [COMMENTED] review under this app's identity at 04:24 is not verified as this bot's output and carries no state to block on. No outstanding third-party CHANGES_REQUESTED in the timeline.

@Jarred-Sumner
Jarred-Sumner force-pushed the claude/modload-zero-unsafe branch from 0b406e5 to 2168fd0 Compare August 29, 2026 09:10
@Jarred-Sumner
Jarred-Sumner force-pushed the claude/jsbundler-zero-unsafe branch 2 times, most recently from aa66992 to 78e9475 Compare August 29, 2026 09:56

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

@Jarred-Sumner
Jarred-Sumner force-pushed the claude/modload-zero-unsafe branch from bfa92b4 to 4a95c43 Compare September 8, 2026 14:27
… exports, OwnedTranspiler

JSBundleCompletionTask is split into the JS-thread side (ref-counted, a
typed `build_ref` slot, completion delivered as a `TaskHop` on its
`ThisPtr`, registered as an ActiveHandle), the bundle-thread `BundleJob`
(Send by construction, owned and dropped by the bundle thread) and a
shared `BuildShared` (cancel/stage atomics, the ticket, the outcome
hand-back). The bundler sees them through `CompletionStruct` (now
Box-owned) and a `CompletionDispatch` trait object; `file_map` and
`optimize_imports` become owned (`Arc`) instead of lifetime-extended
borrows, and the bundle thread exposes `singleton::wake()`.

The C++ plugin bridge calls typed host exports (`&mut Resolve` /
`&mut Load`, per-kind addError/answerCancelled, the global passed
explicitly); `Resolve`/`Load` gain `answer`, `plugin` and
`notify_deferred` next to `dispatch`.

Bun.Transpiler holds an `OwnedTranspiler` (a Transpiler that owns its
home arena; calls go through `with_call`, off-thread transforms take a
`JobTranspiler` copy that keeps the shared storage alive), so
JSTranspiler.rs, JSBundler.rs and js_bundle_completion_task.rs have no
unsafe left.
@Jarred-Sumner
Jarred-Sumner force-pushed the claude/jsbundler-zero-unsafe branch from 78e9475 to 756dc44 Compare September 8, 2026 17:45

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants