diff --git a/src/jsc/array_buffer.rs b/src/jsc/array_buffer.rs index 216aef586a1c..bc10d428e5b2 100644 --- a/src/jsc/array_buffer.rs +++ b/src/jsc/array_buffer.rs @@ -651,6 +651,8 @@ impl ArrayBuffer { pub struct PinnedArrayBuffer { buffer: ArrayBuffer, rooted: bool, + /// The bytes `buffer.ptr` points at when [`copy_if_resizable`](Self::copy_if_resizable) took a copy. + copy: Option>, } impl PinnedArrayBuffer { @@ -669,6 +671,7 @@ impl PinnedArrayBuffer { Some(Self { buffer, rooted: false, + copy: None, }) } @@ -680,8 +683,36 @@ impl PinnedArrayBuffer { Some(this) } + /// [`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)?; + 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 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 + || self.buffer.byte_len == 0 + || self.copy.is_some() + { + return true; + } + 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] 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 +721,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..aedf9cf262d7 100644 --- a/src/runtime/node/types.rs +++ b/src/runtime/node/types.rs @@ -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) })) } @@ -1099,12 +1105,16 @@ 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 { + 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 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()); + } Valid::path_buffer(buffer.slice(), ctx)?; Valid::path_null_bytes(buffer.slice(), ctx)?; arguments.eat(); diff --git a/src/runtime/webcore/CompressionStreamCoder.rs b/src/runtime/webcore/CompressionStreamCoder.rs index 1bfc90b888f4..48b34b2f4b6b 100644 --- a/src/runtime/webcore/CompressionStreamCoder.rs +++ b/src/runtime/webcore/CompressionStreamCoder.rs @@ -696,15 +696,13 @@ 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: 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), } -// 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 +716,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..d50a5f79809f 100644 --- a/test/js/bun/util/zstd.test.ts +++ b/test/js/bun/util/zstd.test.ts @@ -682,6 +682,76 @@ 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, expectedStdout: string) { + 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(stdout).toBe(expectedStdout); + expect(stderr).toBe(""); + expect(exitCode).toBe(0); + } + + it("zstdCompress reads the bytes the caller passed", async () => { + 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); + `, + "wrong: 0\n", + ); + }); + + it("zstdDecompress reads the bytes the caller passed", async () => { + 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); + `, + "wrong: 0\n", + ); + }); + + it("a growable SharedArrayBuffer stays a borrow and compresses the same bytes", async () => { + 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"); + `, + "same\n", + ); + }); +}); + 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..2b827c0e26da 100644 --- a/test/js/node/fs/fs.test.ts +++ b/test/js/node/fs/fs.test.ts @@ -6715,6 +6715,104 @@ 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 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({ + cmd: [ + bunExe(), + "-e", + ` + import fs from "node:fs"; + const encoded = new TextEncoder().encode(process.execPath); + 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, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toBe("wrong: 0\n"); + expect(stderr).toBe(""); + expect(exitCode).toBe(0); +}); + +// 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({ + cmd: [ + bunExe(), + "-e", + ` + import fs from "node:fs"; + import path from "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()); + } + `, + ], + 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\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", {});