Repository navigation
VM: skip preCommitStackMemory before stackPointerAtVMEntry is set on Windows - #310
Conversation
…Windows On Windows, StackBounds reports the full stack reserve via GetCurrentThreadStackLimits. During VM construction m_stackPointerAtVMEntry is null, so updateStackLimits() computes the soft limit as stack.end() + softReservedZoneSize and preCommitStackMemory() then touches every page down to it, committing nearly the entire reserve. For embedders with a large main-thread reserve (Bun links /STACK:0x1200000 = 18MB) this forces ~18MB of commit charge at process start; when the host is commit-constrained the guard-page grow fails and the process dies with EXCEPTION_STACK_OVERFLOW (0xC00000FD) inside preCommitStackMemory. The pre-entry commit is unnecessary: LLInt/JIT code cannot run before the JSLock is acquired, and JSLock::didAcquireLock() calls setStackPointerAtVMEntry(), which re-runs updateStackLimits() with the maxPerThreadStackUsage cap applied. Gate preCommitStackMemory on m_stackPointerAtVMEntry so the commit is bounded by maxPerThreadStackUsage and only happens once the VM is actually entered.
|
Warning Review limit reached
Next review available in: 8 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
Comment |
| if (m_stackPointerAtVMEntry) | ||
| preCommitStackMemory(newSoftStackLimit); |
There was a problem hiding this comment.
🟡 On Windows threads whose stack reserve is ≤ Options::maxPerThreadStackUsage() (default 5 MB — e.g. the 1 MB Windows default), the post-JSLockHolder updateStackLimits() computes the same soft limit as the pre-entry call, so lastSoftStackLimit == newSoftStackLimit and preCommitStackMemory is never called for the VM's lifetime. Bun's own 18 MB-reserve threads are unaffected so this shouldn't block, but it's a latent regression for any smaller-stack embedder thread; consider also entering the pre-commit path when m_stackPointerAtVMEntry transitions from null → non-null even if the computed limit is unchanged.
Extended reasoning...
What the bug is
The PR gates preCommitStackMemory on m_stackPointerAtVMEntry being non-null, relying on the fact that JSLockHolder (a few lines later in VM::VM()) will call JSLock::didAcquireLock() → setStackPointerAtVMEntry() → updateStackLimits() again with the entry pointer set. That second call is expected to enter the if (lastSoftStackLimit != newSoftStackLimit) block and perform the (now bounded-to-5 MB) commit.
However, that block is only entered when the soft limit actually changes between the two calls. On threads whose total stack reserve is ≤ maxPerThreadStackUsage, it doesn't change — so preCommitStackMemory is never called at all, for the entire lifetime of the VM.
The code path that triggers it
Looking at StackBounds::recursionLimit (StackBounds.h:95-115):
- 1-arg overload (line 98): returns
m_bound + reservedZoneSize. - 3-arg overload (lines 101-115): computes
endOfStackWithReservedZone = m_bound + reservedZoneSizeandavailableUserStack = startOfUserStack - endOfStackWithReservedZone. WhenmaxUserStackWithReservedZone > availableUserStack(line 112), it clamps and returnsstartOfUserStack - availableUserStack— which is exactlyendOfStackWithReservedZone = m_bound + reservedZoneSize.
Both calls in updateStackLimits() pass the same m_currentSoftReservedZoneSize as the reserved-zone argument, so on a small-stack thread the two overloads return byte-identical values.
Why existing code doesn't prevent it
The pre-commit is nested inside if (lastSoftStackLimit != newSoftStackLimit). Before this PR, the first call (pre-entry, lastSoftStackLimit == nullptr) always entered that block and committed down to m_bound + 128 KB. After this PR, the first call enters the block but the new if (m_stackPointerAtVMEntry) guard skips the commit; the second call has the entry pointer set but computes the same limit, so the outer if is false. Subsequent lock release/re-acquire cycles on the same thread produce the same clamped value again. There is no other path that calls preCommitStackMemory.
Step-by-step proof
Take a Windows thread with the default 1 MB stack reserve, m_bound = 0x1000000, m_origin = 0x1100000, softReservedZoneSize = 128 KB, maxPerThreadStackUsage = 5 MB.
- Call 1 (from
updateSoftReservedZoneSizeinVM::VM(),m_stackPointerAtVMEntry == nullptr):- Takes the else branch →
newSoftStackLimit = m_bound + 128 KB = 0x1020000. lastSoftStackLimitwasnullptr, so the outerifis true;setStackSoftLimit(0x1020000).- New guard:
m_stackPointerAtVMEntryis null → skippreCommitStackMemory.
- Takes the else branch →
JSLockHolder lock(this)→didAcquireLock()→setStackPointerAtVMEntry(sp ≈ 0x10F0000)→updateStackLimits():- Takes the if branch → 3-arg overload with
startOfUserStack = 0x10F0000. endOfStackWithReservedZone = 0x1020000;availableUserStack = 0xD0000(832 KB);maxUserStackWithReservedZone = 5 MB − 128 KB = 4.875 MB.4.875 MB > 832 KB→ clamp → return0x10F0000 − 0xD0000 = 0x1020000.lastSoftStackLimit (0x1020000) == newSoftStackLimit (0x1020000)→ outerifis false, entire block skipped.
- Takes the if branch → 3-arg overload with
- No further
updateStackLimits()on this thread will change the limit.preCommitStackMemoryis never invoked.
Impact
This regresses the guard-page-skip protection that preCommitStackMemory exists to provide: LLInt/JIT frames that allocate more than one page of stack in a single step can land past the guard page onto uncommitted memory and access-violate. Before this PR, call 1 always committed down to m_bound + 128 KB on such threads.
In practice Bun links with /STACK:0x1200000, which sets 18 MB as the process-wide default reserve for the main thread and for every CreateThread with dwStackSize = 0, so Bun's main and worker threads take the > 5 MB path where the second call does produce a different (smaller) limit and the commit runs. The gap only bites a VM created on a thread with an explicitly small stack (jsc.exe shell, N-API addon threads, embedders using the Windows default), which is not a known Bun production configuration — hence nit, not blocking for a fix addressing 33k+ Sentry crashes.
How to fix
Also enter the pre-commit path when m_stackPointerAtVMEntry transitions from null → non-null, regardless of whether the computed limit changed. For example, hoist the Windows block out of the lastSoftStackLimit != newSoftStackLimit check and gate it on m_stackPointerAtVMEntry && (lastSoftStackLimit != newSoftStackLimit || !stackWasCommitted), or simply track a one-shot m_didPreCommitStack flag and commit on the first call where m_stackPointerAtVMEntry is set.
Preview Builds
|
Resolves the Lookup.cpp conflict by taking main's #306 version of setUpStaticFunctionSlot: both branches independently dropped the ThrowScope from the reifyStaticProperty call site; main's version checks vm.exceptionForInspection() directly and keeps the original RELEASE_ASSERT_NOT_REACHED for the no-exception-but-no-offset case. Also brings in #304 (Windows WallTime/MonotonicTime) and #310 (preCommitStackMemory guard).
Problem
Sentry BUN-2V26: 33k+ events, 100% Windows, exception code
0xC00000FD EXCEPTION_STACK_OVERFLOW, always at:i.e. cold process start, before any JS runs. 66% of events are
bun build --compilestandalone executables (deployed binaries), 64% are the baseline dist (older/VM hardware), which points at commit-charge exhaustion rather than actual stack depth.Cause
StackBounds::currentThreadStackBoundsInternal()on Windows usesGetCurrentThreadStackLimits, which returns the full stack reserve. DuringVM::VM(),m_stackPointerAtVMEntryis still null, soupdateStackLimits()takes the no-entry branch:newSoftStackLimit = stack.recursionLimit(m_currentSoftReservedZoneSize); // = m_bound + 128KBand
preCommitStackMemory(newSoftStackLimit)then walks from the current SP down tolowLimit + 128KB, touching every page. Bun links with/STACK:0x1200000,0x200000(18MB reserve, 2MB initial commit), so this forces ~17.8MB of fresh stack commit on every process start. When the host is low on commit charge the guard-page grow fails and Windows raisesEXCEPTION_STACK_OVERFLOW.This commit is also wasted:
Options::maxPerThreadStackUsage()is 5MB, so the JIT is never allowed to use more than ~5MB of stack regardless. Worker threads hit the same path with an 18MB reserve each.Fix
Gate
preCommitStackMemoryonm_stackPointerAtVMEntry. LLInt/JIT code cannot run before the JSLock is acquired, andJSLock::didAcquireLock()callssetStackPointerAtVMEntry(currentStackPointer()), which re-runsupdateStackLimits()with themaxPerThreadStackUsagecap applied.VM::VM()itself takes aJSLockHoldera few lines after the initialupdateSoftReservedZoneSize, so the commit still happens during construction, just bounded to ~5MB instead of ~18MB.No behavior change on non-Windows (the whole block is
#if OS(WINDOWS)).Refs oven-sh/bun#27987.