From a6f490d490e6434f155bc325ca4f2fe4d242e89d Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 21 Aug 2026 16:42:34 +0000 Subject: [PATCH] crash_handler(windows): report a JSC initialization that ran out of memory as an error, not a crash JSC::initialize() commits 10.5 MiB of mimalloc arena metadata for the Structure heap. When the commit limit of the machine or the memory limit of a job object is used up, that commit fails and JSC aborts. With the abort() now reported, that would be a crash report for the machine's condition. bun_jsc::initialize marks the call with Action::InitializeJsc. A crash under that action in a process that cannot commit 16 MiB anymore prints an error that names the commit charge and the job object limits, and exits with code 1. --- src/crash_handler/lib.rs | 72 +++++++++++++ src/jsc/lib.rs | 1 + src/sys/windows/mod.rs | 126 ++++++++++++++++++++++- src/windows_sys/externs.rs | 2 + test/cli/run/run-crash-handler.test.ts | 134 +++++++++++++++++++++++++ 5 files changed, 334 insertions(+), 1 deletion(-) diff --git a/src/crash_handler/lib.rs b/src/crash_handler/lib.rs index bf2cf6c2f5d9..45229f1b1700 100644 --- a/src/crash_handler/lib.rs +++ b/src/crash_handler/lib.rs @@ -740,6 +740,8 @@ mod draft { Print(&'static [u8]), Resolver, Dlopen(&'static [u8]), + /// Inside `JSC::initialize()`. See `exit_if_jsc_initialization_ran_out_of_memory`. + InitializeJsc, } impl fmt::Display for Action { @@ -752,6 +754,7 @@ mod draft { Action::Dlopen(path) => { write!(writer, "loading native module: {}", bstr::BStr::new(path)) } + Action::InitializeJsc => writer.write_str("initializing JavaScriptCore"), } } } @@ -826,6 +829,10 @@ mod draft { match PANIC_STAGE.with(|s| s.get()) { 0 => { + if matches!(current_action(), Some(Action::InitializeJsc)) { + exit_if_jsc_initialization_ran_out_of_memory(); + } + bun_core::maybe_handle_panic_during_process_reload(); PANIC_STAGE.with(|s| s.set(1)); @@ -1274,6 +1281,71 @@ mod draft { crash(reason); } + #[cfg(windows)] + const MIB: u64 = 1024 * 1024; + + /// The largest single commit in `JSC::initialize()` is the metadata of the + /// Structure heap's mimalloc arena: 10.5 MiB for the 4 GiB heap + /// (`StructureAlignedMemoryAllocator.cpp`, `mi_manage_os_memory_ex`). + /// JSC cannot start in a process that has less than this left to commit. + #[cfg(windows)] + const JSC_MINIMUM_COMMIT_MIB: u64 = 16; + + /// A crash inside `JSC::initialize()` while the process cannot commit + /// [`JSC_MINIMUM_COMMIT_MIB`] anymore is the machine's doing (its commit + /// limit or a job object's memory limit is used up), not a bug: print that + /// and exit instead of reporting it. JSC reacts to the failed commit with a + /// `RELEASE_ASSERT`, which is `abort()` on Windows, so this is reached from + /// `handle_abort_windows`. + fn exit_if_jsc_initialization_ran_out_of_memory() { + #[cfg(windows)] + { + use bun_core::pretty_error; + use bun_sys::windows; + + if windows::can_commit((JSC_MINIMUM_COMMIT_MIB * MIB) as usize) { + return; + } + + pretty_error!( + "\nerror: Bun ran out of memory while initializing JavaScriptCore.\n\nJavaScriptCore commits memory for its heaps at startup. This process could not commit another {} MB.\n\n", + JSC_MINIMUM_COMMIT_MIB, + ); + if let Ok(process) = + windows::GetProcessMemoryInfo(windows::kernel32::GetCurrentProcess()) + { + pretty_error!( + "Committed by this process: {} MB\n", + process.PagefileUsage as u64 / MIB, + ); + } + if let Some(machine) = windows::commit_charge() { + pretty_error!( + "Committed on this machine: {} MB of {} MB (physical memory plus page file)\n", + machine.limit.saturating_sub(machine.available) / MIB, + machine.limit / MIB, + ); + } + let job = windows::job_memory_limits(); + if let Some(limit) = job.per_process { + pretty_error!( + "Job object limit: {} MB per process\n", + limit as u64 / MIB + ); + } + if let Some(limit) = job.whole_job { + pretty_error!( + "Job object limit: {} MB for the whole job\n", + limit as u64 / MIB + ); + } + pretty_error!( + "\nTo fix this, close other programs or enlarge the page file. If Bun runs in a container or a CI job, raise its memory limit.\n", + ); + Global::exit(1); + } + } + /// This is called when `main` returns an error. /// We don't want to treat it as a crash under certain error codes. #[allow(clippy::needless_pass_by_value)] diff --git a/src/jsc/lib.rs b/src/jsc/lib.rs index 605f0c9e9e16..906c9dffe56d 100644 --- a/src/jsc/lib.rs +++ b/src/jsc/lib.rs @@ -501,6 +501,7 @@ pub fn initialize_with(eval_mode: bool, short_lived_globals: bool) { // any long-running event loop; tell JSC to skip the worker threads it // otherwise spawns eagerly at VM creation (see `JSCInitialize`). let one_shot = is_one_shot_eval_invocation(); + let _action = bun_crash_handler::scoped_action(bun_crash_handler::Action::InitializeJsc); // SAFETY: `env` borrows the libc `environ` global for the duration of the // call; `on_jsc_invalid_env_var` is `extern "C"` and only reads the (ptr,len) // it is handed. JSCInitialize is called exactly once at startup. diff --git a/src/sys/windows/mod.rs b/src/sys/windows/mod.rs index b71b082db9c0..6fc8c4a0f33c 100644 --- a/src/sys/windows/mod.rs +++ b/src/sys/windows/mod.rs @@ -1316,6 +1316,8 @@ pub use bun_windows_sys::externs::ResizePseudoConsole; pub use bun_windows_sys::externs::ClosePseudoConsole; pub const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE: DWORD = 0x2000; +pub(crate) const JOB_OBJECT_LIMIT_PROCESS_MEMORY: DWORD = 0x100; +pub(crate) const JOB_OBJECT_LIMIT_JOB_MEMORY: DWORD = 0x200; pub(crate) const JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION: DWORD = 0x400; pub(crate) const JOB_OBJECT_LIMIT_BREAKAWAY_OK: DWORD = 0x800; pub(crate) const JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK: DWORD = 0x00001000; @@ -1496,7 +1498,8 @@ pub struct PROCESS_MEMORY_COUNTERS { pub(crate) QuotaPagedPoolUsage: usize, pub(crate) QuotaPeakNonPagedPoolUsage: usize, pub(crate) QuotaNonPagedPoolUsage: usize, - pub(crate) PagefileUsage: usize, + /// The commit charge of the process ("Commit size" in Task Manager). + pub PagefileUsage: usize, pub(crate) PeakPagefileUsage: usize, } @@ -1522,6 +1525,127 @@ pub fn GetProcessMemoryInfo(process: HANDLE) -> Result bool { + use kernel32::{MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE}; + unsafe extern "system" { + fn VirtualAlloc( + lpAddress: *mut c_void, + dwSize: usize, + flAllocationType: DWORD, + flProtect: DWORD, + ) -> *mut c_void; + fn VirtualFree(lpAddress: *mut c_void, dwSize: usize, dwFreeType: DWORD) -> BOOL; + } + // SAFETY: a null address asks for a fresh region, so no memory in use is + // affected. The region is released right away (`MEM_RELEASE` takes a size + // of 0) and nothing reads or writes it in between. + unsafe { + let region = VirtualAlloc( + ptr::null_mut(), + bytes, + MEM_RESERVE | MEM_COMMIT, + PAGE_READWRITE, + ); + if region.is_null() { + return false; + } + VirtualFree(region, 0, MEM_RELEASE); + } + true +} + +/// The commit charge of the whole machine (`GlobalMemoryStatusEx`), in bytes. +#[derive(Clone, Copy)] +pub struct CommitCharge { + /// Physical memory plus the current size of the page file. + pub limit: u64, + /// How much of `limit` is not committed yet. + pub available: u64, +} + +pub fn commit_charge() -> Option { + /// `MEMORYSTATUSEX` (`sysinfoapi.h`). + #[repr(C)] + #[derive(Default)] + struct MEMORYSTATUSEX { + dwLength: DWORD, + dwMemoryLoad: DWORD, + ullTotalPhys: u64, + ullAvailPhys: u64, + ullTotalPageFile: u64, + ullAvailPageFile: u64, + ullTotalVirtual: u64, + ullAvailVirtual: u64, + ullAvailExtendedVirtual: u64, + } + unsafe extern "system" { + // safe: the out-param is a `&mut` to a struct whose `dwLength` says + // how much of it the kernel may write. + safe fn GlobalMemoryStatusEx(lpBuffer: &mut MEMORYSTATUSEX) -> BOOL; + } + let mut status = MEMORYSTATUSEX { + dwLength: size_of::() as DWORD, + ..Default::default() + }; + if GlobalMemoryStatusEx(&mut status) == 0 { + return None; + } + Some(CommitCharge { + limit: status.ullTotalPageFile, + available: status.ullAvailPageFile, + }) +} + +/// The memory limits of the job object this process runs in, in bytes. Both +/// are `None` when the process is not in a job or the job does not limit +/// memory. Windows charges committed memory against these limits, so a +/// process that hits one fails to commit memory like a machine whose commit +/// limit is used up. Containers and CI runners set them. +#[derive(Clone, Copy, Default)] +pub struct JobMemoryLimits { + /// `JOB_OBJECT_LIMIT_PROCESS_MEMORY`: the limit for each process in the job. + pub per_process: Option, + /// `JOB_OBJECT_LIMIT_JOB_MEMORY`: the limit for all processes in the job together. + pub whole_job: Option, +} + +pub fn job_memory_limits() -> JobMemoryLimits { + unsafe extern "system" { + // safe: a null `hJob` is documented to mean the job of the calling + // process (a process outside of every job gets `FALSE`); the out-param + // is a `&mut` to the struct that `cbJobObjectInformationLength` sizes. + safe fn QueryInformationJobObject( + hJob: HANDLE, + JobObjectInformationClass: DWORD, + lpJobObjectInformation: &mut JOBOBJECT_EXTENDED_LIMIT_INFORMATION, + cbJobObjectInformationLength: DWORD, + lpReturnLength: Option<&mut DWORD>, + ) -> BOOL; + } + let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = bun_core::ffi::zeroed(); + if QueryInformationJobObject( + ptr::null_mut(), + JobObjectExtendedLimitInformation, + &mut info, + size_of::() as DWORD, + None, + ) == 0 + { + return JobMemoryLimits::default(); + } + let flags = info.BasicLimitInformation.LimitFlags; + JobMemoryLimits { + per_process: (flags & JOB_OBJECT_LIMIT_PROCESS_MEMORY != 0) + .then_some(info.ProcessMemoryLimit), + whole_job: (flags & JOB_OBJECT_LIMIT_JOB_MEMORY != 0).then_some(info.JobMemoryLimit), + } +} + pub use bun_windows_sys::externs::GetConsoleMode; pub use bun_windows_sys::externs::SetConsoleMode; diff --git a/src/windows_sys/externs.rs b/src/windows_sys/externs.rs index f3996922cf55..dc5e36a0a7d9 100644 --- a/src/windows_sys/externs.rs +++ b/src/windows_sys/externs.rs @@ -834,6 +834,8 @@ pub mod kernel32 { pub Type: u32, } pub const MEM_COMMIT: u32 = 0x1000; + pub const MEM_RESERVE: u32 = 0x2000; + pub const MEM_RELEASE: u32 = 0x8000; pub const MEM_FREE: u32 = 0x10000; pub const PAGE_NOACCESS: u32 = 0x01; diff --git a/test/cli/run/run-crash-handler.test.ts b/test/cli/run/run-crash-handler.test.ts index b8229602dcd1..124ccc781d3e 100644 --- a/test/cli/run/run-crash-handler.test.ts +++ b/test/cli/run/run-crash-handler.test.ts @@ -1,3 +1,4 @@ +import { dlopen, ptr } from "bun:ffi"; import { crash_handler } from "bun:internal-for-testing"; import { describe, expect, test } from "bun:test"; import { bunEnv, bunExe, isASAN, isDebug, isLinux, isPosix, isWindows, mergeWindowEnvs, tempDir } from "harness"; @@ -608,6 +609,139 @@ describe.if(isWindows)("Windows: aborts and traps are reported, exit status refl }); }); +// JSC::initialize() fails with a RELEASE_ASSERT, an abort() (above), when it +// cannot get the memory it needs. Out of memory is the machine's problem, so +// that case ends with an error message; anything else that goes wrong in there +// is still a crash. +describe.if(isWindows)("Windows: crash while initializing JavaScriptCore", () => { + const outOfMemory = "Bun ran out of memory while initializing JavaScriptCore"; + + // structureHeapSizeInKB must be a power of two: 3072 trips a RELEASE_ASSERT + // in the constructor that reserves the Structure heap. Memory is fine, so + // the report says where it happened instead of blaming memory. + test.concurrent("a crash that memory does not explain is reported", async () => { + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", "1", "--debug-crash-handler-use-trace-string"], + env: { ...noReportEnv, BUN_JSC_structureHeapSizeInKB: "3072" }, + stdio: ["ignore", "pipe", "pipe"], + }); + const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]); + + expect(stderr).toContain("abort() called"); + expect(stderr).toContain("Crashed while initializing JavaScriptCore"); + expect(stderr).not.toContain(outOfMemory); + expect(exitCode).not.toBe(0); + }); + + // Runs `bun -e 1` in a job object with a per-process memory limit, which is + // what containers and CI runners use. Windows charges committed memory + // against it. The limits under which JSC's initialization is what runs + // into it span the 10.5 MB that JSC commits for the Structure heap, and + // they start at whatever bun has committed by then, which depends on the + // build: 5 MB more than `bun --version` peaks at on a release build, 8 MB + // more on a debug build. So start 8 MB above that peak and walk up in steps + // smaller than the span. The run that lands in the span must end with the + // error message, not with a crash report and not silently. A run below the + // span dies of an ordinary allocation failure, a __fastfail, which Windows + // Error Reporting holds for over a second, so every step matters. + test.concurrent("out of memory during initialization exits with an error", async () => { + const kernel32 = openJobObjectApi(); + const calibration = await runInJob(kernel32, undefined, [bunExe(), "--version"]); + expect(calibration.exitCode).toBe(0); + const startMiB = Math.floor(calibration.peakCommitMiB) + 8; + + const outcomes: string[] = []; + for (let limitMiB = startMiB; limitMiB < startMiB + 256; limitMiB += 4) { + const { stderr, exitCode } = await runInJob(kernel32, limitMiB, [bunExe(), "-e", "1"]); + if (stderr.includes(outOfMemory)) { + expect(stderr).toContain(`Job object limit: ${limitMiB} MB per process`); + expect(stderr).not.toContain("panic"); + expect(stderr).not.toContain("Bun has crashed"); + expect(exitCode).toBe(1); + return; + } + const summary = stderr.split("\n").find(line => line.includes("panic") || line.includes("error")) ?? ""; + outcomes.push(`${limitMiB} MiB: exit ${exitCode} ${summary}`.trimEnd()); + // It ran, so every larger limit works too. + if (exitCode === 0) break; + } + throw new Error( + `bun --version peaked at ${calibration.peakCommitMiB.toFixed(1)} MiB, and no limit from ${startMiB} MiB up ended with the out of memory error:\n${outcomes.join("\n")}`, + ); + }); +}); + +function openJobObjectApi() { + return dlopen("kernel32.dll", { + CreateJobObjectW: { args: ["ptr", "ptr"], returns: "ptr" }, + SetInformationJobObject: { args: ["ptr", "i32", "ptr", "u32"], returns: "i32" }, + QueryInformationJobObject: { args: ["ptr", "i32", "ptr", "u32", "ptr"], returns: "i32" }, + OpenProcess: { args: ["u32", "i32", "u32"], returns: "ptr" }, + AssignProcessToJobObject: { args: ["ptr", "ptr"], returns: "i32" }, + CloseHandle: { args: ["ptr"], returns: "i32" }, + GetLastError: { args: [], returns: "u32" }, + }).symbols; +} + +// Starts `cmd.exe`, moves it into a new job object (with a per-process memory +// limit of `limitMiB`, if given) while it waits for its stdin, then has it run +// `argv`, which inherits the job. Returns what `argv` wrote to stderr, its exit +// code, and the peak commit charge of a process in the job, which is `argv`'s +// (cmd.exe stays far below it). +async function runInJob(kernel32: ReturnType, limitMiB: number | undefined, argv: string[]) { + const JobObjectExtendedLimitInformation = 9; + const JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x100; + const PROCESS_SET_QUOTA = 0x0100; + const PROCESS_TERMINATE = 0x0001; + // JOBOBJECT_EXTENDED_LIMIT_INFORMATION is 144 bytes. Field offsets: + const LimitFlags = 16; + const ProcessMemoryLimit = 112; + const PeakProcessMemoryUsed = 128; + const info = new DataView(new ArrayBuffer(144)); + + const job = kernel32.CreateJobObjectW(null, null); + if (!job) throw new Error(`CreateJobObjectW failed: ${kernel32.GetLastError()}`); + try { + if (limitMiB !== undefined) { + info.setUint32(LimitFlags, JOB_OBJECT_LIMIT_PROCESS_MEMORY, true); + info.setBigUint64(ProcessMemoryLimit, BigInt(limitMiB) * 1024n * 1024n, true); + if (!kernel32.SetInformationJobObject(job, JobObjectExtendedLimitInformation, ptr(info.buffer), 144)) { + throw new Error(`SetInformationJobObject failed: ${kernel32.GetLastError()}`); + } + } + + // /Q: do not echo the commands. /D: skip the AutoRun registry entries. + await using shell = Bun.spawn({ + cmd: ["cmd.exe", "/Q", "/D"], + env: noReportEnv, + stdin: "pipe", + stdout: "pipe", + stderr: "pipe", + }); + const shellProcess = kernel32.OpenProcess(PROCESS_SET_QUOTA | PROCESS_TERMINATE, 0, shell.pid); + if (!shellProcess) throw new Error(`OpenProcess failed: ${kernel32.GetLastError()}`); + try { + if (!kernel32.AssignProcessToJobObject(job, shellProcess)) { + throw new Error(`AssignProcessToJobObject failed: ${kernel32.GetLastError()}`); + } + } finally { + kernel32.CloseHandle(shellProcess); + } + + shell.stdin.write(`${argv.map(arg => `"${arg}"`).join(" ")}\r\nexit %ERRORLEVEL%\r\n`); + await shell.stdin.end(); + const [stderr, exitCode] = await Promise.all([shell.stderr.text(), shell.exited, shell.stdout.text()]); + + if (!kernel32.QueryInformationJobObject(job, JobObjectExtendedLimitInformation, ptr(info.buffer), 144, null)) { + throw new Error(`QueryInformationJobObject failed: ${kernel32.GetLastError()}`); + } + const peakCommitMiB = Number(info.getBigUint64(PeakProcessMemoryUsed, true)) / (1024 * 1024); + return { stderr, exitCode, peakCommitMiB }; + } finally { + kernel32.CloseHandle(job); + } +} + describe("automatic crash reporter", () => { for (const approach of ["panic", "segfault", "outOfMemory", "abort"]) { test(`${approach} should report`, async () => {