From 4e57d901e47e6aaddace23b728c4f0e6d467b860 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 12 Oct 2026 06:26:10 +0000 Subject: [PATCH 1/4] Transpiler::set_log: re-point options.log set_log re-pointed three of the four raw pointers to the Log and left options.log unchanged. The Bun.Transpiler constructor builds its Log on the stack, moves it into a Box and calls set_log. configure_defines then logged through options.log into the moved-from stack slot. When the Log already held a message, the stale copy shared the Vec buffer with the live Log and freed it on growth: new Bun.Transpiler({ external: ["a*b*c"], define: { "0x": "1", "1x": "1", "2x": "1", "3x": "1" } }) is a use-after-free in Log::clear_and_free when the transpiler is collected. A define value that is not JSON lost its parser message for the same reason. --- src/bundler/transpiler.rs | 8 ++-- test/bundler/transpiler/transpiler.test.js | 46 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/bundler/transpiler.rs b/src/bundler/transpiler.rs index 92bb829b163b..a9ab51899406 100644 --- a/src/bundler/transpiler.rs +++ b/src/bundler/transpiler.rs @@ -63,11 +63,13 @@ pub struct Transpiler<'a> { } impl<'a> Transpiler<'a> { - /// Takes `*mut Log` (not `&'a mut`) because the same - /// `*Log` is aliased into `linker.log` / `resolver.log`; the struct - /// field is a raw pointer for that reason. + /// Takes `*mut Log` (not `&'a mut`) because the same `*Log` is aliased + /// into `options.log` / `linker.log` / `resolver.log`; the struct field + /// is a raw pointer for that reason. All four are re-pointed here: a + /// caller that moved its `Log` must not leave any of them on the old slot. pub fn set_log(&mut self, log: *mut bun_ast::Log) { self.log = log; + self.options.log = log; self.linker.log = log; // SAFETY: caller (`ThreadPool::Worker::create`) passes the per-worker // arena-allocated `Log`, which outlives this `Transpiler<'a>`. diff --git a/test/bundler/transpiler/transpiler.test.js b/test/bundler/transpiler/transpiler.test.js index 2ec82493d4ff..69534c3205dc 100644 --- a/test/bundler/transpiler/transpiler.test.js +++ b/test/bundler/transpiler/transpiler.test.js @@ -2558,6 +2558,52 @@ export default <>hi expect(exitCode).toBe(0); }); + it("define errors after an earlier config error do not corrupt the log", async () => { + // An `external` with two `*` logs one error and construction continues. + // The four define-key errors that follow make the buffer of the log grow. + // In a child process: on a build without the fix this is a crash. + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + ` + const define = { "0x": "1", "1x": "1", "2x": "1", "3x": "1" }; + for (let i = 0; i < 20; i++) { + const t = new Bun.Transpiler({ external: ["a*b*c"], define }); + if (i % 10 === 0) t.transformSync("export const a = 1;"); + if (i % 10 === 9) Bun.gc(true); + } + Bun.gc(true); + console.log("constructed"); + `, + ], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect({ stdout, stderr, exitCode, signalCode: proc.signalCode }).toEqual({ + stdout: "constructed\n", + stderr: "", + exitCode: 0, + signalCode: null, + }); + }); + + it("a define value that is not JSON throws the parser's error", () => { + let thrown; + try { + new Bun.Transpiler({ define: { a: "{" } }); + } catch (e) { + thrown = e; + } + expect([thrown?.name, thrown?.level, thrown?.message]).toEqual([ + "BuildMessage", + "error", + 'Expected "}" but found end of file', + ]); + }); + it("JSX keys", () => { var bun = new Bun.Transpiler({ loader: "jsx", From 212aeb21cdbddba6d06d4e7db05aa4eab13362b1 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:06:09 +0000 Subject: [PATCH 2/4] Bun.Transpiler: re-point options.log in the constructor, not in set_log set_log also runs when WebWorker::create restores a saved transpiler.log. The module loader swaps transpiler.log, linker.log and resolver.log to a per-call log with direct field writes and does not touch options.log. With the write in set_log, a worker created while a per-call log is swapped in would leave options.log of the VM transpiler on that log after it is freed. The constructor is the one caller that moves its Log after Transpiler::init, so it sets options.log next to its set_log call. set_log is as it was. --- src/bundler/transpiler.rs | 8 +++----- src/runtime/api/JSTranspiler.rs | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bundler/transpiler.rs b/src/bundler/transpiler.rs index a9ab51899406..92bb829b163b 100644 --- a/src/bundler/transpiler.rs +++ b/src/bundler/transpiler.rs @@ -63,13 +63,11 @@ pub struct Transpiler<'a> { } impl<'a> Transpiler<'a> { - /// Takes `*mut Log` (not `&'a mut`) because the same `*Log` is aliased - /// into `options.log` / `linker.log` / `resolver.log`; the struct field - /// is a raw pointer for that reason. All four are re-pointed here: a - /// caller that moved its `Log` must not leave any of them on the old slot. + /// Takes `*mut Log` (not `&'a mut`) because the same + /// `*Log` is aliased into `linker.log` / `resolver.log`; the struct + /// field is a raw pointer for that reason. pub fn set_log(&mut self, log: *mut bun_ast::Log) { self.log = log; - self.options.log = log; self.linker.log = log; // SAFETY: caller (`ThreadPool::Worker::create`) passes the per-worker // arena-allocated `Log`, which outlives this `Transpiler<'a>`. diff --git a/src/runtime/api/JSTranspiler.rs b/src/runtime/api/JSTranspiler.rs index df5d4bc5f529..0172d7f2f686 100644 --- a/src/runtime/api/JSTranspiler.rs +++ b/src/runtime/api/JSTranspiler.rs @@ -1011,6 +1011,9 @@ impl JSTranspiler { // is uniquely owned at init time, so `&mut` projection is alias-free. let transpiler = unsafe { this.transpiler.get_mut() }; transpiler.set_log(&raw mut config.log); + // `set_log` does not re-point `options.log`, which `configure_defines` + // below logs through. It must not stay on the moved-from stack slot. + transpiler.options.log = &raw mut config.log; transpiler.options.no_macros = config.no_macros; transpiler.configure_linker_with_auto_jsx(false); From 183ea3d972bd4ec5141c471b02fb60f92a79364f Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:49:27 +0000 Subject: [PATCH 3/4] Bun.Transpiler: one-line comment on the options.log write --- src/runtime/api/JSTranspiler.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/runtime/api/JSTranspiler.rs b/src/runtime/api/JSTranspiler.rs index 0172d7f2f686..32baba256e92 100644 --- a/src/runtime/api/JSTranspiler.rs +++ b/src/runtime/api/JSTranspiler.rs @@ -1011,8 +1011,7 @@ impl JSTranspiler { // is uniquely owned at init time, so `&mut` projection is alias-free. let transpiler = unsafe { this.transpiler.get_mut() }; transpiler.set_log(&raw mut config.log); - // `set_log` does not re-point `options.log`, which `configure_defines` - // below logs through. It must not stay on the moved-from stack slot. + // `set_log` does not cover `options.log`, which `configure_defines` logs through. transpiler.options.log = &raw mut config.log; transpiler.options.no_macros = config.no_macros; From cffcf148853131ae8a6d1c996fa4bf7cfa5fcdda Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:06:39 +0000 Subject: [PATCH 4/4] ci: retrigger