From 2944959ef9255e0b754aae7ccc55e7265541baa2 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 14 Jul 2026 03:55:11 +0000 Subject: [PATCH 1/3] Bun.mmap: throw RangeError for files larger than 4 GiB instead of aborting JSC ArrayBuffers are hard-capped at MAX_ARRAY_BUFFER_SIZE (1<<32 bytes, JavaScriptCore/runtime/PageCount.h), enforced by RELEASE_ASSERT in the ArrayBufferContents constructor. mmap_file passed the fstat()'d size straight into make_typed_array_with_bytes_no_copy with no bound check, so mapping any file one byte past 4 GiB aborted the whole process with no catchable error. Check map.len() against the limit after the mmap succeeds; on overflow, munmap and throw a RangeError that points at the {size} option. --- src/runtime/api/BunObject.rs | 13 ++++++++++ test/js/bun/util/mmap.test.js | 45 ++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/runtime/api/BunObject.rs b/src/runtime/api/BunObject.rs index 2547980b2d02..0ed281f5e9d9 100644 --- a/src/runtime/api/BunObject.rs +++ b/src/runtime/api/BunObject.rs @@ -1838,6 +1838,19 @@ pub(crate) fn mmap_file(global_this: &JSGlobalObject, callframe: &CallFrame) -> } }; + // JSC's `MAX_ARRAY_BUFFER_SIZE` (JavaScriptCore/runtime/PageCount.h). + // ArrayBufferContents RELEASE_ASSERTs this, so anything larger aborts. + const MAX_ARRAY_BUFFER_SIZE: usize = 1 << 32; + if map.len() > MAX_ARRAY_BUFFER_SIZE { + let len = map.len(); + let _ = sys::munmap(map.as_ptr().cast_mut(), len); + let err = global_this.create_range_error_instance(format_args!( + "File is too large to mmap: {} bytes exceeds the maximum typed array size ({} bytes). Pass {{ size }} to map a smaller range.", + len, MAX_ARRAY_BUFFER_SIZE, + )); + return Err(global_this.throw_value(err)); + } + extern "C" fn munmap_dealloc(ptr: *mut c_void, size: *mut c_void) { // SAFETY: ptr is the original mmap base, size is its length stuffed into a pointer. let _ = sys::munmap(ptr.cast::(), size as usize); diff --git a/test/js/bun/util/mmap.test.js b/test/js/bun/util/mmap.test.js index 6d507d3b968e..5f105f3f326e 100644 --- a/test/js/bun/util/mmap.test.js +++ b/test/js/bun/util/mmap.test.js @@ -1,5 +1,6 @@ import { describe, expect, it } from "bun:test"; -import { gcTick, isWindows, tmpdirSync } from "harness"; +import { bunEnv, bunExe, gcTick, isWindows, tmpdirSync } from "harness"; +import { truncateSync, unlinkSync, writeFileSync } from "node:fs"; import { join } from "path"; // TODO: We do not support mmap() on Windows. Maybe we can add it later. @@ -85,6 +86,48 @@ describe.skipIf(isWindows)("Bun.mmap", async () => { expect(() => Bun.mmap(path, null)).not.toThrow(); }); + it("mmap file > 4 GiB throws RangeError instead of aborting", async () => { + // Sparse file: truncate() to 4 GiB + 1 uses no disk space. + const dir = tmpdirSync(); + const big = join(dir, "big.bin"); + writeFileSync(big, ""); + truncateSync(big, 2 ** 32 + 1); + try { + // Spawned so a regression (SIGABRT) fails the test rather than the runner. + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + `const f = ${JSON.stringify(big)}; + try { Bun.mmap(f); console.log("no throw"); } + catch (e) { console.log("threw", e.name, e.message); } + // exactly 4 GiB must still succeed + console.log("at-limit", Bun.mmap(f, { size: 2 ** 32 }).length); + // and a capped size on the same file works + console.log("capped", Bun.mmap(f, { size: 4096 }).length);`, + ], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + const lines = stdout.trim().split("\n"); + expect({ lines, signalCode: proc.signalCode, exitCode }).toEqual({ + lines: [ + expect.stringMatching(/^threw RangeError .*4294967297/), + "at-limit 4294967296", + "capped 4096", + ], + signalCode: null, + exitCode: 0, + }); + expect(stderr).not.toContain("ASSERTION FAILED"); + } finally { + unlinkSync(big); + } + }); + it("mmap handles non-number offset/size without crashing", () => { // These should not crash - non-number values coerce to 0 per JavaScript semantics // Previously these caused assertion failures (issue ENG-22413) From 3d4ce5f84149c07c5c8f21ac83ecbc86c074aa4a Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 03:57:25 +0000 Subject: [PATCH 2/3] [autofix.ci] apply automated fixes --- test/js/bun/util/mmap.test.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/js/bun/util/mmap.test.js b/test/js/bun/util/mmap.test.js index 5f105f3f326e..7c92088c89a3 100644 --- a/test/js/bun/util/mmap.test.js +++ b/test/js/bun/util/mmap.test.js @@ -114,11 +114,7 @@ describe.skipIf(isWindows)("Bun.mmap", async () => { const lines = stdout.trim().split("\n"); expect({ lines, signalCode: proc.signalCode, exitCode }).toEqual({ - lines: [ - expect.stringMatching(/^threw RangeError .*4294967297/), - "at-limit 4294967296", - "capped 4096", - ], + lines: [expect.stringMatching(/^threw RangeError .*4294967297/), "at-limit 4294967296", "capped 4096"], signalCode: null, exitCode: 0, }); From b3282fca0a2bfeffb492211051e8e88abdc6f8c3 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 14 Jul 2026 04:02:57 +0000 Subject: [PATCH 3/3] test: drop redundant stderr marker check signalCode: null and exitCode: 0 in the toEqual already detect any abort; the stderr check is redundant and can false-positive on benign debug output. --- test/js/bun/util/mmap.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/js/bun/util/mmap.test.js b/test/js/bun/util/mmap.test.js index 7c92088c89a3..dc7916962803 100644 --- a/test/js/bun/util/mmap.test.js +++ b/test/js/bun/util/mmap.test.js @@ -110,7 +110,7 @@ describe.skipIf(isWindows)("Bun.mmap", async () => { stdout: "pipe", stderr: "pipe", }); - const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + const [stdout, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); const lines = stdout.trim().split("\n"); expect({ lines, signalCode: proc.signalCode, exitCode }).toEqual({ @@ -118,7 +118,6 @@ describe.skipIf(isWindows)("Bun.mmap", async () => { signalCode: null, exitCode: 0, }); - expect(stderr).not.toContain("ASSERTION FAILED"); } finally { unlinkSync(big); }