Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/jsc/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,13 @@
}
}

/// The length is not range-checked here: `to_js*` hands it to
/// `Bun__make*WithBytesNoCopy`, which throws a RangeError for anything
/// above JSC's `MAX_ARRAY_BUFFER_SIZE` (2^32 bytes on 64-bit targets).
pub fn from_bytes(bytes: &mut [u8], typed_array_type: JSType) -> ArrayBuffer {
ArrayBuffer {
len: u32::try_from(bytes.len()).expect("int cast") as usize,
byte_len: u32::try_from(bytes.len()).expect("int cast") as usize,
len: bytes.len(),
byte_len: bytes.len(),
typed_array_type,
ptr: bytes.as_mut_ptr(),
..Default::default()
Expand All @@ -411,8 +414,8 @@
// this is an FFI hand-off, not a leak.
let ptr = bun_core::heap::into_raw(bytes).cast::<u8>();
ArrayBuffer {
len: u32::try_from(len).expect("int cast") as usize,
byte_len: u32::try_from(len).expect("int cast") as usize,
len,
byte_len: len,
typed_array_type,
ptr,
..Default::default()
Expand Down Expand Up @@ -946,15 +949,21 @@
/// calls `deallocator(ptr, deallocator_context)` on the JS thread when it is
/// collected (never, if `deallocator` is `None`).
///
/// Fails with a RangeError when `len` is above JSC's `MAX_ARRAY_BUFFER_SIZE`
/// (2^32 bytes on 64-bit targets). The bytes are still consumed: the
/// deallocator runs before the error is returned, so the caller must not
/// release them again on `Err`.
///
Comment thread
robobun marked this conversation as resolved.
Resolved
/// # Safety
///
/// - `ptr` must be valid for reads and writes of `len` bytes (JS code can do
/// both through the returned object) for the returned object's entire
/// lifetime: until the deallocator runs, or indefinitely when `deallocator`
/// is `None`. `ptr` may be null only when `len == 0`.
/// - `deallocator`, if `Some`, must be sound to call exactly once with
/// `(ptr, deallocator_context)` on the JS thread at GC time, and
/// `deallocator_context` must remain valid until then.
/// `(ptr, deallocator_context)` on the JS thread, either at GC time or
/// when this call fails, and `deallocator_context` must remain valid
/// until then.

Check warning on line 966 in src/jsc/array_buffer.rs

View check run for this annotation

Claude / Claude Code Review

to_js_with_context # Safety doc not updated for run-deallocator-on-Err

The `# Safety` contract on `to_js_with_context` (src/jsc/array_buffer.rs:533) still says the callback runs "at GC time" only — after this change it also runs synchronously before `Err` when `self.byte_len > MAX_ARRAY_BUFFER_SIZE`, so a caller reading only this doc could free `ptr` again on `Err` and double-free. The wording you added to `make_array_buffer_with_bytes_no_copy` ("either at GC time or when this call fails", "must not release them again on `Err`") should be mirrored here and in the f
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) unsafe fn make_array_buffer_with_bytes_no_copy(
global: &JSGlobalObject,
ptr: *mut c_void,
Expand Down
28 changes: 28 additions & 0 deletions src/jsc/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,12 @@
// Windows doesn't have mmap
// This code should pretty much only be called on Linux.
#if !OS(WINDOWS)
// An empty result makes the caller fall back to a copying allocation, which
// throws for this length. createFromBytes below would RELEASE_ASSERT instead.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (byteLength > MAX_ARRAY_BUFFER_SIZE) [[unlikely]] {
return JSC::JSValue::encode(JSC::JSValue {});
}

auto ptr = mmap(nullptr, totalLength, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);

if (ptr == MAP_FAILED) {
Expand Down Expand Up @@ -1521,11 +1527,30 @@
RELEASE_AND_RETURN(scope, JSValue::encode(array));
}

// ArrayBuffer::createFromBytes RELEASE_ASSERTs above MAX_ARRAY_BUFFER_SIZE, so an
// oversized length has to be turned into the RangeError `new ArrayBuffer(len)`
// throws before the bytes reach it. The caller already gave the bytes up, so
// they are released through the deallocator here, the same as when the
// returned object is collected.
static bool rejectBytesNoCopyAboveArrayBufferLimit(JSC::JSGlobalObject* globalObject, JSC::ThrowScope& scope, const void* ptr, size_t len, JSTypedArrayBytesDeallocator deallocator, void* deallocatorContext)
{
if (len <= MAX_ARRAY_BUFFER_SIZE) [[likely]]
return false;

if (deallocator)
deallocator(const_cast<void*>(ptr), deallocatorContext);
throwOutOfMemoryError(globalObject, scope);
return true;
}

Check warning on line 1544 in src/jsc/bindings/ZigGlobalObject.cpp

View check run for this annotation

Claude / Claude Code Review

Sibling ArrayBuffer::createFromBytes sites in JSBuffer.cpp still unguarded

Two more zero-copy `ArrayBuffer::createFromBytes` sites remain unguarded and still RELEASE_ASSERT above `MAX_ARRAY_BUFFER_SIZE`: `JSBuffer__fromMmap` (JSBuffer.cpp:2595, reached from `to_js_buffer_from_memfd` in this file) and `JSBuffer__bufferFromPointerAndLengthAndDeinit` (JSBuffer.cpp:383, reached from `MarkedArrayBuffer::to_node_buffer`). Both fit the new `rejectBytesNoCopyAboveArrayBufferLimit` helper (munmap / call the deallocator, then `throwOutOfMemoryError`). The new doc comment on `fro
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

extern "C" JSC::EncodedJSValue Bun__makeArrayBufferWithBytesNoCopy(JSC::JSGlobalObject* globalObject, const void* ptr, size_t len, JSTypedArrayBytesDeallocator deallocator, void* deallocatorContext)
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);

if (rejectBytesNoCopyAboveArrayBufferLimit(globalObject, scope, ptr, len, deallocator, deallocatorContext)) [[unlikely]]
return {};

auto buffer = ArrayBuffer::createFromBytes({ static_cast<const uint8_t*>(ptr), len }, createSharedTask<void(void*)>([=](void* p) {
if (deallocator) deallocator(p, deallocatorContext);
}));
Expand All @@ -1540,6 +1565,9 @@
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);

if (rejectBytesNoCopyAboveArrayBufferLimit(globalObject, scope, ptr, len, deallocator, deallocatorContext)) [[unlikely]]
return {};

auto buffer_ = ArrayBuffer::createFromBytes({ static_cast<const uint8_t*>(ptr), len }, createSharedTask<void(void*)>([=](void* p) {
if (deallocator) deallocator(p, deallocatorContext);
}));
Expand Down
47 changes: 46 additions & 1 deletion test/js/bun/util/mmap.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterAll, describe, expect, it } from "bun:test";
import { bunEnv, bunExe, gcTick, isWindows, tempDir, tmpdirSync } from "harness";
import { writeFileSync } from "node:fs";
import { truncateSync, writeFileSync } from "node:fs";
import { join } from "path";

// TODO: We do not support mmap() on Windows. Maybe we can add it later.
Expand Down Expand Up @@ -153,6 +153,51 @@ try {
expect(exitCode).toBe(0);
});

it("mmap of more than 2^32 bytes throws a RangeError instead of aborting", async () => {
using dir = tempDir("mmap-4gib", {});
const file = join(String(dir), "big.bin");
// Sparse: the mappings below cost address space, not memory or disk.
writeFileSync(file, "");
truncateSync(file, 2 ** 32 + 4096);

await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`const file = ${JSON.stringify(file)};
const out = {};
try {
out.wholeFile = Bun.mmap(file).length;
} catch (e) {
out.wholeFile = e.name;
}
try {
out.sizeAboveLimit = Bun.mmap(file, { size: 2 ** 32 + 1 }).length;
} catch (e) {
out.sizeAboveLimit = e.name;
}
out.sizeAtLimit = Bun.mmap(file, { size: 2 ** 32 }).length;
out.smallWindow = Bun.mmap(file, { size: 4096 }).length;
console.log(JSON.stringify(out));`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ out: JSON.parse(stdout.trim() || "null"), stderr, exitCode, signalCode: proc.signalCode }).toEqual({
out: {
wholeFile: "RangeError",
sizeAboveLimit: "RangeError",
sizeAtLimit: 2 ** 32,
smallWindow: 4096,
},
stderr: "",
exitCode: 0,
signalCode: null,
});
});

it("mmap rejects negative offset", () => {
expect(() => Bun.mmap(path, { offset: -1 })).toThrow("offset must be a non-negative integer");
});
Expand Down
51 changes: 51 additions & 0 deletions test/js/web/fetch/blob-oom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,54 @@ describe.skipIf(os.totalmem() < 10 * 1024 ** 3)("byte sources at the 2 GiB strin
expect(exitCode).toBe(0);
});
});

// An ArrayBuffer can hold at most 2^32 bytes (JSC's MAX_ARRAY_BUFFER_SIZE). The
// file path hands the bytes it read to JSC without a copy; converting the
// length through u32 on the way panicked at exactly 2^32, and JSC aborts when
// it is asked to adopt more than that. The file is sparse, but each case still
// reads a real 4 GiB into the child (about 6 seconds in a debug build, all of
// it page faults), so it runs in a subprocess, gets a long timeout, and skips
// on small machines like the 2 GiB cases above.
describe.skipIf(os.totalmem() < 10 * 1024 ** 3)("Bun.file().arrayBuffer() at the 4 GiB ArrayBuffer limit", () => {
async function readSparseFileAsArrayBuffer(size: number) {
using dir = tempDir("blob-4gib", {});
const file = path.join(String(dir), "big.bin");
// 'x' followed by a hole, which reads back as NUL bytes. Nothing is written
// after the truncate: on NTFS that would zero-fill the whole file on disk.
writeFileSync(file, "x");
truncateSync(file, size);
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`
const report = buf => {
const view = new DataView(buf);
return { byteLength: buf.byteLength, first: view.getUint8(0), last: view.getUint8(buf.byteLength - 1) };
};
const result = await Bun.file(${JSON.stringify(file)}).arrayBuffer().then(report, e => ({ error: { name: e.name, message: e.message } }));
console.log(JSON.stringify(result));
`,
],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
return JSON.parse(stdout.trim() || JSON.stringify({ stdout, stderr, exitCode, signalCode: proc.signalCode }));
}

test("a file of exactly 2^32 bytes is returned whole", async () => {
expect(await readSparseFileAsArrayBuffer(2 ** 32)).toEqual({
byteLength: 2 ** 32,
first: "x".charCodeAt(0),
last: 0,
});
}, 120_000);

test("a file of 2^32 + 1 bytes rejects with the RangeError that new ArrayBuffer() throws for that size", async () => {
expect(await readSparseFileAsArrayBuffer(2 ** 32 + 1)).toEqual({
error: { name: "RangeError", message: "Out of memory" },
});
}, 120_000);
});
Loading