From 62008b1d0508d66c5dc04c55ddd2010d08912d08 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 16:15:04 +0000 Subject: [PATCH 1/7] PinnedArrayBuffer: copy a resizable ArrayBuffer for a pool job that only reads it --- src/jsc/array_buffer.rs | 28 ++++++++ src/runtime/node/types.rs | 6 +- src/runtime/webcore/CompressionStreamCoder.rs | 17 ++--- test/js/bun/util/zstd.test.ts | 64 +++++++++++++++++++ test/js/node/fs/fs.test.ts | 46 +++++++++++++ 5 files changed, 147 insertions(+), 14 deletions(-) diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index 216aef586a1c..6534546d5ef8 100644 --- a/src/jsc/array_buffer.rs +++ b/src/jsc/array_buffer.rs @@ -648,9 +648,15 @@ impl ArrayBuffer { /// GC-roots the cell. `Drop` releases what was taken. Constructed on the JS /// thread; a `root()`ed value is dropped there too, while a `pin()`-only value /// held by a `Blob` store drops wherever the store's last ref goes. +/// +/// A pin does not stop `ArrayBuffer.prototype.resize()`: a shrink unmaps the +/// trimmed pages under the borrow. [`root_read_only`](Self::root_read_only) +/// is the constructor for a job that only reads a buffer it cannot watch. pub struct PinnedArrayBuffer { buffer: ArrayBuffer, rooted: bool, + /// The bytes `buffer.ptr` points at when [`root_read_only`](Self::root_read_only) took a copy. + copy: Option>, } impl PinnedArrayBuffer { @@ -669,6 +675,7 @@ impl PinnedArrayBuffer { Some(Self { buffer, rooted: false, + copy: None, }) } @@ -680,8 +687,28 @@ impl PinnedArrayBuffer { Some(this) } + /// [`root`](Self::root) for a job that only reads the bytes. A resizable + /// non-shared buffer can shrink while the job runs, so its current bytes are + /// copied and the view reads the copy. A fixed-length buffer cannot shrink + /// and a growable `SharedArrayBuffer` only grows in place, so both stay + /// borrowed. `None` also when the copy cannot be allocated. + pub fn root_read_only(global: &JSGlobalObject, value: JSValue) -> Option { + let mut this = Self::root(global, value)?; + if this.buffer.resizable && !this.buffer.shared && this.buffer.byte_len > 0 { + let bytes = this.buffer.byte_slice(); + let mut copy = Vec::new(); + copy.try_reserve_exact(bytes.len()).ok()?; + copy.extend_from_slice(bytes); + global.vm().report_extra_memory(copy.len()); + this.buffer.ptr = copy.as_mut_ptr(); + this.copy = Some(copy); + } + Some(this) + } + #[inline] pub fn slice_mut(&mut self) -> &mut [u8] { + debug_assert!(self.copy.is_none(), "a read-only root is not writable"); self.buffer.byte_slice_mut() } @@ -690,6 +717,7 @@ impl PinnedArrayBuffer { pub fn defuse(&mut self) { self.buffer = ArrayBuffer::default(); self.rooted = false; + self.copy = None; } } diff --git a/src/runtime/node/types.rs b/src/runtime/node/types.rs index 9b4763e38814..3cc1fdafe3f0 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -342,7 +342,7 @@ impl StringOrBuffer<'static> { } /// `value` is ArrayBuffer-like (the caller checked): borrowed for `Sync`, - /// pinned and GC-rooted for `Async`. + /// pinned and GC-rooted for `Async` (a resizable buffer is copied, the job only reads it). pub(crate) fn buffer_from_js( global: &JSGlobalObject, value: JSValue, @@ -351,7 +351,7 @@ impl StringOrBuffer<'static> { if flavor == Flavor::Sync { return Ok(Self::Buffer(Buffer::from_array_buffer(global, value))); } - match PinnedArrayBuffer::root(global, value) { + match PinnedArrayBuffer::root_read_only(global, value) { Some(buffer) => Ok(Self::PinnedBuffer(buffer)), None => Err(global.throw_out_of_memory()), } @@ -1100,7 +1100,7 @@ impl PathLikeExt for PathLike<'_> { let path = match arg.js_type() { JSType::Uint8Array | JSType::DataView | JSType::ArrayBuffer => { let buffer = if arguments.will_be_async { - PinnedArrayBuffer::root(ctx, arg) + PinnedArrayBuffer::root_read_only(ctx, arg) } else { PinnedArrayBuffer::pin(ctx, arg) } diff --git a/src/runtime/webcore/CompressionStreamCoder.rs b/src/runtime/webcore/CompressionStreamCoder.rs index 1bfc90b888f4..3614f8bdf6ae 100644 --- a/src/runtime/webcore/CompressionStreamCoder.rs +++ b/src/runtime/webcore/CompressionStreamCoder.rs @@ -696,15 +696,15 @@ impl CompressionStreamCoder { } } -/// A chunk's bytes for the pool thread: a pinned ArrayBuffer's backing -/// store (its pin + GC root is the paired [`PinnedArrayBuffer`] on the JS side) -/// or an owned copy. +/// A chunk's bytes for the pool thread: the bytes a paired [`PinnedArrayBuffer`] +/// on the JS side keeps valid (a pinned + GC-rooted backing store, or its copy +/// of a resizable one) or an owned copy. pub(crate) enum AsyncInput { Pinned { ptr: *const u8, len: usize }, Owned(Vec), } -// SAFETY: `Pinned.ptr` is a backing store pinned + rooted by the paired -// `PinnedArrayBuffer` for as long as the job lives; read only under the pool borrow. +// SAFETY: `Pinned.ptr` points at bytes the paired `PinnedArrayBuffer` keeps +// valid for as long as the job lives; read only under the pool borrow. unsafe impl Send for AsyncInput {} impl AsyncInput { @@ -718,12 +718,7 @@ impl AsyncInput { if !chunk.is_cell() { return (Self::Owned(fallback.to_vec()), None); } - if let Some(buf) = PinnedArrayBuffer::root(global, chunk) { - // A resizable non-shared backing can `mprotect()` pages out on - // `resize()`; pinning does not block that, so spill to a copy. - if buf.resizable && !buf.shared { - return (Self::Owned(fallback.to_vec()), None); - } + if let Some(buf) = PinnedArrayBuffer::root_read_only(global, chunk) { return ( Self::Pinned { ptr: buf.ptr, diff --git a/test/js/bun/util/zstd.test.ts b/test/js/bun/util/zstd.test.ts index 243003fe563e..64edf7f6d359 100644 --- a/test/js/bun/util/zstd.test.ts +++ b/test/js/bun/util/zstd.test.ts @@ -682,6 +682,70 @@ describe("sync compression argument handling", () => { }, 60_000); }); +// The async functions read the input on a pool thread. The unfixed build segfaults there, so each +// case runs in a child process: it compares the result against a fixed-length input's result. +describe.concurrent("async compression of a resizable ArrayBuffer that shrinks after the call", () => { + async function runInChild(script: string): Promise { + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", script], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stderr).toBe(""); + expect(exitCode).toBe(0); + return stdout.trim(); + } + + it("zstdCompress reads the bytes the caller passed", async () => { + const stdout = await runInChild(/* js */ ` + const fixed = Buffer.alloc(256 * 1024, 0x41); + const expected = Buffer.from(Bun.zstdCompressSync(fixed)).toString("hex"); + let wrong = 0; + for (let i = 0; i < 20; i++) { + const ab = new ArrayBuffer(fixed.byteLength, { maxByteLength: 1 << 21 }); + new Uint8Array(ab).fill(0x41); + const promise = Bun.zstdCompress(new Uint8Array(ab)); + ab.resize(0); + if (Buffer.from(await promise).toString("hex") !== expected) wrong++; + } + console.log("wrong:", wrong); + `); + expect(stdout).toBe("wrong: 0"); + }); + + it("zstdDecompress reads the bytes the caller passed", async () => { + const stdout = await runInChild(/* js */ ` + const fixed = Buffer.from(Bun.zstdCompressSync(Buffer.alloc(256 * 1024, 0x41))); + const expected = Buffer.from(Bun.zstdDecompressSync(fixed)).toString("hex"); + let wrong = 0; + for (let i = 0; i < 20; i++) { + const ab = new ArrayBuffer(fixed.byteLength, { maxByteLength: 1 << 21 }); + new Uint8Array(ab).set(fixed); + const promise = Bun.zstdDecompress(new Uint8Array(ab)); + ab.resize(0); + if (Buffer.from(await promise).toString("hex") !== expected) wrong++; + } + console.log("wrong:", wrong); + `); + expect(stdout).toBe("wrong: 0"); + }); + + it("a growable SharedArrayBuffer stays a borrow and compresses the same bytes", async () => { + const stdout = await runInChild(/* js */ ` + const fixed = Buffer.alloc(256 * 1024, 0x41); + const expected = Buffer.from(Bun.zstdCompressSync(fixed)).toString("hex"); + const sab = new SharedArrayBuffer(fixed.byteLength, { maxByteLength: 1 << 21 }); + new Uint8Array(sab).fill(0x41); + const promise = Bun.zstdCompress(new Uint8Array(sab, 0, fixed.byteLength)); + sab.grow(1 << 21); + console.log(Buffer.from(await promise).toString("hex") === expected ? "same" : "different"); + `); + expect(stdout).toBe("same"); + }); +}); + describe.concurrent("Zstandard HTTP compression", () => { // Sample data for HTTP tests const testData = { diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 4a95daebc05b..e341bb4f0330 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -6715,6 +6715,52 @@ it("fs.promises.writeFile keeps the source buffer attached while the write is in expect(readFileSync(file, "latin1")).toBe("EEEEEEEE"); }); +// A pin stops a detach but not `ArrayBuffer.prototype.resize()`. The pool thread reads the bytes +// captured at call time, not the shrunk buffer. +it("fs.write writes the bytes captured at call time when a resizable source shrinks in flight", async () => { + using dir = tempDir("fs-write-resizable", {}); + const file = join(String(dir), "out.bin"); + const fd = openSync(file, "w"); + const buf = new Uint8Array(new ArrayBuffer(1 << 16, { maxByteLength: 1 << 16 })); + for (let i = 0; i < buf.length; i++) buf[i] = i & 0xff; + const { promise, resolve, reject } = Promise.withResolvers(); + try { + fs.write(fd, buf, 5, 10, 0, (err, written) => (err ? reject(err) : resolve(written))); + buf.buffer.resize(0); + expect(await promise).toBe(10); + } finally { + closeSync(fd); + } + expect(readFileSync(file)).toEqual(Buffer.from([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])); +}); + +it("fs.promises.stat reads a Buffer path captured at call time when its resizable ArrayBuffer shrinks in flight", async () => { + // The unfixed build segfaults on the pool thread, so this runs in a child process. + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + ` + const fs = require("node:fs"); + const encoded = new TextEncoder().encode(process.execPath); + const ab = new ArrayBuffer(encoded.length, { maxByteLength: 1 << 16 }); + const path = new Uint8Array(ab); + path.set(encoded); + const pending = fs.promises.stat(path); + ab.resize(0); + pending.then(stat => console.log(stat.size > 0 ? "ok" : "empty")); + `, + ], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toBe("ok\n"); + expect(stderr).toBe(""); + expect(exitCode).toBe(0); +}); + it.if(isPosix)("realpathSync reports ENAMETOOLONG when cwd plus the path exceeds the system path limit", async () => { using dir = tempDir("fs-realpath-too-long", {}); From 47c7fb785fe152cf95b391c374644a1cfe875a83 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 16:21:41 +0000 Subject: [PATCH 2/7] PinnedArrayBuffer: one-line docs for root_read_only and AsyncInput --- src/jsc/array_buffer.rs | 10 +--------- src/runtime/webcore/CompressionStreamCoder.rs | 4 +--- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index 6534546d5ef8..d63529aa63f4 100644 --- a/src/jsc/array_buffer.rs +++ b/src/jsc/array_buffer.rs @@ -648,10 +648,6 @@ impl ArrayBuffer { /// GC-roots the cell. `Drop` releases what was taken. Constructed on the JS /// thread; a `root()`ed value is dropped there too, while a `pin()`-only value /// held by a `Blob` store drops wherever the store's last ref goes. -/// -/// A pin does not stop `ArrayBuffer.prototype.resize()`: a shrink unmaps the -/// trimmed pages under the borrow. [`root_read_only`](Self::root_read_only) -/// is the constructor for a job that only reads a buffer it cannot watch. pub struct PinnedArrayBuffer { buffer: ArrayBuffer, rooted: bool, @@ -687,11 +683,7 @@ impl PinnedArrayBuffer { Some(this) } - /// [`root`](Self::root) for a job that only reads the bytes. A resizable - /// non-shared buffer can shrink while the job runs, so its current bytes are - /// copied and the view reads the copy. A fixed-length buffer cannot shrink - /// and a growable `SharedArrayBuffer` only grows in place, so both stay - /// borrowed. `None` also when the copy cannot be allocated. + /// [`root`](Self::root) for a job that only reads: a resizable non-shared buffer is copied, as a pin stops a detach but not a shrink. pub fn root_read_only(global: &JSGlobalObject, value: JSValue) -> Option { let mut this = Self::root(global, value)?; if this.buffer.resizable && !this.buffer.shared && this.buffer.byte_len > 0 { diff --git a/src/runtime/webcore/CompressionStreamCoder.rs b/src/runtime/webcore/CompressionStreamCoder.rs index 3614f8bdf6ae..48b34b2f4b6b 100644 --- a/src/runtime/webcore/CompressionStreamCoder.rs +++ b/src/runtime/webcore/CompressionStreamCoder.rs @@ -696,9 +696,7 @@ impl CompressionStreamCoder { } } -/// A chunk's bytes for the pool thread: the bytes a paired [`PinnedArrayBuffer`] -/// on the JS side keeps valid (a pinned + GC-rooted backing store, or its copy -/// of a resizable one) or an owned copy. +/// A chunk's bytes for the pool thread: what the paired [`PinnedArrayBuffer`] on the JS side keeps valid, or an owned copy. pub(crate) enum AsyncInput { Pinned { ptr: *const u8, len: usize }, Owned(Vec), From 732252153b0b8f766618333285474ef8f593dcb2 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 16:24:41 +0000 Subject: [PATCH 3/7] test: assert the child's stdout before its exit code --- test/js/bun/util/zstd.test.ts | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/test/js/bun/util/zstd.test.ts b/test/js/bun/util/zstd.test.ts index 64edf7f6d359..d50a5f79809f 100644 --- a/test/js/bun/util/zstd.test.ts +++ b/test/js/bun/util/zstd.test.ts @@ -685,7 +685,7 @@ describe("sync compression argument handling", () => { // The async functions read the input on a pool thread. The unfixed build segfaults there, so each // case runs in a child process: it compares the result against a fixed-length input's result. describe.concurrent("async compression of a resizable ArrayBuffer that shrinks after the call", () => { - async function runInChild(script: string): Promise { + async function runInChild(script: string, expectedStdout: string) { await using proc = Bun.spawn({ cmd: [bunExe(), "-e", script], env: bunEnv, @@ -693,13 +693,14 @@ describe.concurrent("async compression of a resizable ArrayBuffer that shrinks a stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toBe(expectedStdout); expect(stderr).toBe(""); expect(exitCode).toBe(0); - return stdout.trim(); } it("zstdCompress reads the bytes the caller passed", async () => { - const stdout = await runInChild(/* js */ ` + await runInChild( + /* js */ ` const fixed = Buffer.alloc(256 * 1024, 0x41); const expected = Buffer.from(Bun.zstdCompressSync(fixed)).toString("hex"); let wrong = 0; @@ -711,12 +712,14 @@ describe.concurrent("async compression of a resizable ArrayBuffer that shrinks a if (Buffer.from(await promise).toString("hex") !== expected) wrong++; } console.log("wrong:", wrong); - `); - expect(stdout).toBe("wrong: 0"); + `, + "wrong: 0\n", + ); }); it("zstdDecompress reads the bytes the caller passed", async () => { - const stdout = await runInChild(/* js */ ` + await runInChild( + /* js */ ` const fixed = Buffer.from(Bun.zstdCompressSync(Buffer.alloc(256 * 1024, 0x41))); const expected = Buffer.from(Bun.zstdDecompressSync(fixed)).toString("hex"); let wrong = 0; @@ -728,12 +731,14 @@ describe.concurrent("async compression of a resizable ArrayBuffer that shrinks a if (Buffer.from(await promise).toString("hex") !== expected) wrong++; } console.log("wrong:", wrong); - `); - expect(stdout).toBe("wrong: 0"); + `, + "wrong: 0\n", + ); }); it("a growable SharedArrayBuffer stays a borrow and compresses the same bytes", async () => { - const stdout = await runInChild(/* js */ ` + await runInChild( + /* js */ ` const fixed = Buffer.alloc(256 * 1024, 0x41); const expected = Buffer.from(Bun.zstdCompressSync(fixed)).toString("hex"); const sab = new SharedArrayBuffer(fixed.byteLength, { maxByteLength: 1 << 21 }); @@ -741,8 +746,9 @@ describe.concurrent("async compression of a resizable ArrayBuffer that shrinks a const promise = Bun.zstdCompress(new Uint8Array(sab, 0, fixed.byteLength)); sab.grow(1 << 21); console.log(Buffer.from(await promise).toString("hex") === expected ? "same" : "different"); - `); - expect(stdout).toBe("same"); + `, + "same\n", + ); }); }); From 515211145a7287c1daed6ead638642eb4853cc7a Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 16:50:09 +0000 Subject: [PATCH 4/7] PinnedArrayBuffer: copy a resizable buffer only for a job that reads it in user space fs.write and fs.writeFile hand the borrow to write(2), which returns EFAULT for a shrunk source instead of faulting, so they keep the zero-copy borrow: a full-view copy before offset and length are parsed would make a small write from a large resizable buffer copy the whole buffer. --- src/jsc/array_buffer.rs | 32 +++++++++++++++++++++---------- src/runtime/node/types.rs | 14 ++++++++++---- test/js/node/fs/fs.test.ts | 39 +++++++++++++------------------------- 3 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index d63529aa63f4..4e967ff2adb4 100644 --- a/src/jsc/array_buffer.rs +++ b/src/jsc/array_buffer.rs @@ -683,19 +683,31 @@ impl PinnedArrayBuffer { Some(this) } - /// [`root`](Self::root) for a job that only reads: a resizable non-shared buffer is copied, as a pin stops a detach but not a shrink. + /// [`root`](Self::root) for a job that reads the bytes itself: see [`copy_if_resizable`](Self::copy_if_resizable). pub fn root_read_only(global: &JSGlobalObject, value: JSValue) -> Option { let mut this = Self::root(global, value)?; - if this.buffer.resizable && !this.buffer.shared && this.buffer.byte_len > 0 { - let bytes = this.buffer.byte_slice(); - let mut copy = Vec::new(); - copy.try_reserve_exact(bytes.len()).ok()?; - copy.extend_from_slice(bytes); - global.vm().report_extra_memory(copy.len()); - this.buffer.ptr = copy.as_mut_ptr(); - this.copy = Some(copy); + this.copy_if_resizable(global).then_some(this) + } + + /// A pin stops a detach but not a shrink, which unmaps pages: a resizable non-shared buffer is copied so a job that reads the bytes itself cannot fault (a syscall reader gets `EFAULT` and needs no copy). `false` if the copy cannot be allocated. + pub fn copy_if_resizable(&mut self, global: &JSGlobalObject) -> bool { + if !self.buffer.resizable + || self.buffer.shared + || self.buffer.byte_len == 0 + || self.copy.is_some() + { + return true; } - Some(this) + let bytes = self.buffer.byte_slice(); + let mut copy = Vec::new(); + if copy.try_reserve_exact(bytes.len()).is_err() { + return false; + } + copy.extend_from_slice(bytes); + global.vm().report_extra_memory(copy.len()); + self.buffer.ptr = copy.as_mut_ptr(); + self.copy = Some(copy); + true } #[inline] diff --git a/src/runtime/node/types.rs b/src/runtime/node/types.rs index 3cc1fdafe3f0..f4f0ed9c7d32 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -342,7 +342,7 @@ impl StringOrBuffer<'static> { } /// `value` is ArrayBuffer-like (the caller checked): borrowed for `Sync`, - /// pinned and GC-rooted for `Async` (a resizable buffer is copied, the job only reads it). + /// pinned and GC-rooted for `Async`. pub(crate) fn buffer_from_js( global: &JSGlobalObject, value: JSValue, @@ -351,7 +351,7 @@ impl StringOrBuffer<'static> { if flavor == Flavor::Sync { return Ok(Self::Buffer(Buffer::from_array_buffer(global, value))); } - match PinnedArrayBuffer::root_read_only(global, value) { + match PinnedArrayBuffer::root(global, value) { Some(buffer) => Ok(Self::PinnedBuffer(buffer)), None => Err(global.throw_out_of_memory()), } @@ -454,13 +454,19 @@ impl StringOrBuffer<'static> { Self::from_js_maybe_async(global, value, Flavor::Sync, StringObjects::Allow) } - /// [`from_js`](Self::from_js) for a work-pool job: strings thread-isolated, buffers pinned and GC-rooted. + /// [`from_js`](Self::from_js) for a work-pool job that reads the bytes itself: strings thread-isolated, buffers pinned and GC-rooted, a resizable buffer copied ([`PinnedArrayBuffer::copy_if_resizable`]). #[inline] pub(crate) fn from_js_async( global: &JSGlobalObject, value: JSValue, ) -> JsResult>> { - let parsed = Self::from_js_maybe_async(global, value, Flavor::Async, StringObjects::Allow)?; + let mut parsed = + Self::from_js_maybe_async(global, value, Flavor::Async, StringObjects::Allow)?; + if let Some(Self::PinnedBuffer(buffer)) = &mut parsed + && !buffer.copy_if_resizable(global) + { + return Err(global.throw_out_of_memory()); + } // SAFETY: parsed with `Flavor::Async`. Ok(parsed.map(|v| unsafe { ThreadIsolated::new(v) })) } diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index e341bb4f0330..75326b8a63a1 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -6715,25 +6715,8 @@ it("fs.promises.writeFile keeps the source buffer attached while the write is in expect(readFileSync(file, "latin1")).toBe("EEEEEEEE"); }); -// A pin stops a detach but not `ArrayBuffer.prototype.resize()`. The pool thread reads the bytes -// captured at call time, not the shrunk buffer. -it("fs.write writes the bytes captured at call time when a resizable source shrinks in flight", async () => { - using dir = tempDir("fs-write-resizable", {}); - const file = join(String(dir), "out.bin"); - const fd = openSync(file, "w"); - const buf = new Uint8Array(new ArrayBuffer(1 << 16, { maxByteLength: 1 << 16 })); - for (let i = 0; i < buf.length; i++) buf[i] = i & 0xff; - const { promise, resolve, reject } = Promise.withResolvers(); - try { - fs.write(fd, buf, 5, 10, 0, (err, written) => (err ? reject(err) : resolve(written))); - buf.buffer.resize(0); - expect(await promise).toBe(10); - } finally { - closeSync(fd); - } - expect(readFileSync(file)).toEqual(Buffer.from([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])); -}); - +// A pin stops a detach but not `ArrayBuffer.prototype.resize()`. The pool thread copies the path into +// its own buffer, so it must read the bytes captured at call time, not the shrunk buffer. it("fs.promises.stat reads a Buffer path captured at call time when its resizable ArrayBuffer shrinks in flight", async () => { // The unfixed build segfaults on the pool thread, so this runs in a child process. await using proc = Bun.spawn({ @@ -6743,12 +6726,16 @@ it("fs.promises.stat reads a Buffer path captured at call time when its resizabl ` const fs = require("node:fs"); const encoded = new TextEncoder().encode(process.execPath); - const ab = new ArrayBuffer(encoded.length, { maxByteLength: 1 << 16 }); - const path = new Uint8Array(ab); - path.set(encoded); - const pending = fs.promises.stat(path); - ab.resize(0); - pending.then(stat => console.log(stat.size > 0 ? "ok" : "empty")); + let wrong = 0; + for (let i = 0; i < 20; i++) { + const ab = new ArrayBuffer(encoded.length, { maxByteLength: 1 << 16 }); + const path = new Uint8Array(ab); + path.set(encoded); + const pending = fs.promises.stat(path); + ab.resize(0); + if (!((await pending).size > 0)) wrong++; + } + console.log("wrong:", wrong); `, ], env: bunEnv, @@ -6756,7 +6743,7 @@ it("fs.promises.stat reads a Buffer path captured at call time when its resizabl stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); - expect(stdout).toBe("ok\n"); + expect(stdout).toBe("wrong: 0\n"); expect(stderr).toBe(""); expect(exitCode).toBe(0); }); From da103d46f1146484603a5c083342d97d5f92de9f Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 18:58:13 +0000 Subject: [PATCH 5/7] PathLike: copy a resizable ArrayBuffer path for sync calls and Blob stores too The PathLike funnel copied a resizable non-shared buffer only for the async arm. A sync call reads the path after the option getters ran, and a getter can call `resize(0)` on the same buffer. `Bun.file(view)` keeps the pinned buffer in its store and reads it on every `.text()`. Both read unmapped pages after a shrink and segfault on the main thread. `copy_if_resizable` now runs for both arms of the funnel, so every PathLike consumer reads the bytes captured at call time. --- src/jsc/array_buffer.rs | 4 +-- src/runtime/node/types.rs | 9 +++-- test/js/node/fs/fs.test.ts | 73 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index 4e967ff2adb4..bc10d428e5b2 100644 --- a/src/jsc/array_buffer.rs +++ b/src/jsc/array_buffer.rs @@ -651,7 +651,7 @@ impl ArrayBuffer { pub struct PinnedArrayBuffer { buffer: ArrayBuffer, rooted: bool, - /// The bytes `buffer.ptr` points at when [`root_read_only`](Self::root_read_only) took a copy. + /// The bytes `buffer.ptr` points at when [`copy_if_resizable`](Self::copy_if_resizable) took a copy. copy: Option>, } @@ -689,7 +689,7 @@ impl PinnedArrayBuffer { this.copy_if_resizable(global).then_some(this) } - /// A pin stops a detach but not a shrink, which unmaps pages: a resizable non-shared buffer is copied so a job that reads the bytes itself cannot fault (a syscall reader gets `EFAULT` and needs no copy). `false` if the copy cannot be allocated. + /// A pin stops a detach but not a shrink, which unmaps pages: a resizable non-shared buffer is copied so a later read of the bytes in user space cannot fault (a syscall reader gets `EFAULT` and needs no copy). `false` if the copy cannot be allocated. pub fn copy_if_resizable(&mut self, global: &JSGlobalObject) -> bool { if !self.buffer.resizable || self.buffer.shared diff --git a/src/runtime/node/types.rs b/src/runtime/node/types.rs index f4f0ed9c7d32..332e47010fbb 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -1105,12 +1105,17 @@ impl PathLikeExt for PathLike<'_> { use jsc::JSType; let path = match arg.js_type() { JSType::Uint8Array | JSType::DataView | JSType::ArrayBuffer => { - let buffer = if arguments.will_be_async { - PinnedArrayBuffer::root_read_only(ctx, arg) + let mut buffer = if arguments.will_be_async { + PinnedArrayBuffer::root(ctx, arg) } else { PinnedArrayBuffer::pin(ctx, arg) } .ok_or_else(|| ctx.throw_out_of_memory())?; + // Read later than this call: on the pool thread, after another argument's getter + // ran (sync), or from a `Blob` store. A `resize(0)` in between unmaps the pages. + if !buffer.copy_if_resizable(ctx) { + return Err(ctx.throw_out_of_memory()); + } Valid::path_buffer(buffer.slice(), ctx)?; Valid::path_null_bytes(buffer.slice(), ctx)?; arguments.eat(); diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index 75326b8a63a1..b133c8287e58 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -6748,6 +6748,79 @@ it("fs.promises.stat reads a Buffer path captured at call time when its resizabl expect(exitCode).toBe(0); }); +// A sync call reads the path after the option getters ran, and a `Bun.file()` store reads it on every +// `.text()`. Both read the bytes captured at call time when a getter or the caller shrinks the buffer. +it("sync fs calls and Bun.file read a Buffer path captured at call time when its resizable ArrayBuffer shrinks", async () => { + using dir = tempDir("fs-resizable-path", {}); + // The unfixed build segfaults on the main thread, so this runs in a child process. + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + ` + const fs = require("node:fs"); + const path = require("node:path"); + const dir = process.cwd(); + function resizablePath(p) { + const bytes = new TextEncoder().encode(p); + const ab = new ArrayBuffer(bytes.length, { maxByteLength: 1 << 16 }); + new Uint8Array(ab).set(bytes); + return ab; + } + + const written = path.join(dir, "written.txt"); + { + const ab = resizablePath(written); + fs.writeFileSync(new Uint8Array(ab), "sync-write", { + get flag() { + ab.resize(0); + return "w"; + }, + }); + console.log("writeFileSync:", fs.readFileSync(written, "utf8")); + } + { + const ab = resizablePath(written); + const text = fs.readFileSync(new DataView(ab), { + get encoding() { + ab.resize(0); + return "utf8"; + }, + }); + console.log("readFileSync:", text); + } + { + const made = path.join(dir, "made"); + const ab = resizablePath(made); + fs.mkdirSync(ab, { + get recursive() { + ab.resize(0); + return true; + }, + }); + console.log("mkdirSync:", fs.statSync(made).isDirectory()); + } + { + const ab = resizablePath(written); + const file = Bun.file(new Uint8Array(ab)); + ab.resize(0); + console.log("Bun.file:", await file.text(), await file.text()); + } + `, + ], + env: bunEnv, + cwd: String(dir), + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toBe( + "writeFileSync: sync-write\nreadFileSync: sync-write\nmkdirSync: true\nBun.file: sync-write sync-write\n", + ); + expect(stderr).toBe(""); + expect(exitCode).toBe(0); +}); + it.if(isPosix)("realpathSync reports ENAMETOOLONG when cwd plus the path exceeds the system path limit", async () => { using dir = tempDir("fs-realpath-too-long", {}); From d6f6d2ebf7ed22b25573d588dbf17ed13076c089 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 19:06:33 +0000 Subject: [PATCH 6/7] review: one-line comment on the PathLike copy, static imports in the child scripts --- src/runtime/node/types.rs | 3 +-- test/js/node/fs/fs.test.ts | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/runtime/node/types.rs b/src/runtime/node/types.rs index 332e47010fbb..aedf9cf262d7 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -1111,8 +1111,7 @@ impl PathLikeExt for PathLike<'_> { PinnedArrayBuffer::pin(ctx, arg) } .ok_or_else(|| ctx.throw_out_of_memory())?; - // Read later than this call: on the pool thread, after another argument's getter - // ran (sync), or from a `Blob` store. A `resize(0)` in between unmaps the pages. + // Read after this call (pool thread, a later argument's getter, a `Blob` store): a shrink in between unmaps the pages. if !buffer.copy_if_resizable(ctx) { return Err(ctx.throw_out_of_memory()); } diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index b133c8287e58..eec894e568c8 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -6724,7 +6724,7 @@ it("fs.promises.stat reads a Buffer path captured at call time when its resizabl bunExe(), "-e", ` - const fs = require("node:fs"); + import fs from "node:fs"; const encoded = new TextEncoder().encode(process.execPath); let wrong = 0; for (let i = 0; i < 20; i++) { @@ -6758,8 +6758,8 @@ it("sync fs calls and Bun.file read a Buffer path captured at call time when its bunExe(), "-e", ` - const fs = require("node:fs"); - const path = require("node:path"); + import fs from "node:fs"; + import path from "node:path"; const dir = process.cwd(); function resizablePath(p) { const bytes = new TextEncoder().encode(p); From d160666771ec13faaa3e5a5cb8fc49f42ae6fd5b Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 27 Aug 2026 19:30:31 +0000 Subject: [PATCH 7/7] test: drop the Bun.file case from the resizable path test Under BUN_DESTRUCT_VM_ON_EXIT=1 (the ASAN lane), the Blob store that Bun.file(buffer) keeps drops its PinnedArrayBuffer during the shutdown sweep and unpins through a JSCell downcast there, which trips JSC's validateIsNotSweeping assertion. That happens for any Buffer path, with or without this change, and is separate from the resizable copy. --- test/js/node/fs/fs.test.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/test/js/node/fs/fs.test.ts b/test/js/node/fs/fs.test.ts index eec894e568c8..2b827c0e26da 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -6748,9 +6748,9 @@ it("fs.promises.stat reads a Buffer path captured at call time when its resizabl expect(exitCode).toBe(0); }); -// A sync call reads the path after the option getters ran, and a `Bun.file()` store reads it on every -// `.text()`. Both read the bytes captured at call time when a getter or the caller shrinks the buffer. -it("sync fs calls and Bun.file read a Buffer path captured at call time when its resizable ArrayBuffer shrinks", async () => { +// A sync call reads the path after the option getters ran. It reads the bytes captured at call time +// when a getter shrinks the buffer. +it("sync fs calls read a Buffer path captured at call time when an option getter shrinks its resizable ArrayBuffer", async () => { using dir = tempDir("fs-resizable-path", {}); // The unfixed build segfaults on the main thread, so this runs in a child process. await using proc = Bun.spawn({ @@ -6800,12 +6800,6 @@ it("sync fs calls and Bun.file read a Buffer path captured at call time when its }); console.log("mkdirSync:", fs.statSync(made).isDirectory()); } - { - const ab = resizablePath(written); - const file = Bun.file(new Uint8Array(ab)); - ab.resize(0); - console.log("Bun.file:", await file.text(), await file.text()); - } `, ], env: bunEnv, @@ -6814,9 +6808,7 @@ it("sync fs calls and Bun.file read a Buffer path captured at call time when its stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); - expect(stdout).toBe( - "writeFileSync: sync-write\nreadFileSync: sync-write\nmkdirSync: true\nBun.file: sync-write sync-write\n", - ); + expect(stdout).toBe("writeFileSync: sync-write\nreadFileSync: sync-write\nmkdirSync: true\n"); expect(stderr).toBe(""); expect(exitCode).toBe(0); });