Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
72 changes: 72 additions & 0 deletions src/crash_handler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"),
}
}
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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!(
"\n<r><red>error<r>: 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!(
"<d>Committed by this process: {} MB<r>\n",
process.PagefileUsage as u64 / MIB,
);
}
if let Some(machine) = windows::commit_charge() {
pretty_error!(
"<d>Committed on this machine: {} MB of {} MB (physical memory plus page file)<r>\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!(
"<d>Job object limit: {} MB per process<r>\n",
limit as u64 / MIB
);
}
if let Some(limit) = job.whole_job {
pretty_error!(
"<d>Job object limit: {} MB for the whole job<r>\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)]
Expand Down
1 change: 1 addition & 0 deletions src/jsc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
126 changes: 125 additions & 1 deletion src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}

Expand All @@ -1522,6 +1525,127 @@ pub fn GetProcessMemoryInfo(process: HANDLE) -> Result<PROCESS_MEMORY_COUNTERS,
}
Ok(out)
}

/// Commits `bytes` of fresh memory and releases it again. `false` means this
/// process cannot commit that much anymore: the commit limit of the machine
/// (physical memory plus page file) or the memory limit of its job object is
/// used up.
pub fn can_commit(bytes: usize) -> 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<CommitCharge> {
/// `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::<MEMORYSTATUSEX>() 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<usize>,
/// `JOB_OBJECT_LIMIT_JOB_MEMORY`: the limit for all processes in the job together.
pub whole_job: Option<usize>,
}

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::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() 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;

Expand Down
2 changes: 2 additions & 0 deletions src/windows_sys/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading