Skip to content
32 changes: 32 additions & 0 deletions src/jsc/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ 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.
copy: Option<Vec<u8>>,
}

impl PinnedArrayBuffer {
Expand All @@ -669,6 +671,7 @@ impl PinnedArrayBuffer {
Some(Self {
buffer,
rooted: false,
copy: None,
})
}

Expand All @@ -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<Self> {
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 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;
}
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
}
Comment thread
robobun marked this conversation as resolved.
Resolved

#[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()
}

Expand All @@ -690,6 +721,7 @@ impl PinnedArrayBuffer {
pub fn defuse(&mut self) {
self.buffer = ArrayBuffer::default();
self.rooted = false;
self.copy = None;
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/runtime/node/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<ThreadIsolated<Self>>> {
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) }))
}
Expand Down Expand Up @@ -1100,7 +1106,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)
}
Expand Down
15 changes: 4 additions & 11 deletions src/runtime/webcore/CompressionStreamCoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>),
}
// 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 {
Expand All @@ -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,
Expand Down
70 changes: 70 additions & 0 deletions test/js/bun/util/zstd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
33 changes: 33 additions & 0 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6715,6 +6715,39 @@ 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",
`
const fs = require("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);
});

it.if(isPosix)("realpathSync reports ENAMETOOLONG when cwd plus the path exceeds the system path limit", async () => {
using dir = tempDir("fs-realpath-too-long", {});

Expand Down
Loading