Skip to content

VM: skip preCommitStackMemory before stackPointerAtVMEntry is set on Windows - #310

Merged
Jarred-Sumner merged 1 commit into
mainfrom
farm/0b435ebf/skip-precommit-before-vmentry
Jul 19, 2026
Merged

Jarred-Sumner merged 1 commit into
mainfrom
farm/0b435ebf/skip-precommit-before-vmentry

Conversation

@robobun

@robobun robobun commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Problem

Sentry BUN-2V26: 33k+ events, 100% Windows, exception code 0xC00000FD EXCEPTION_STACK_OVERFLOW, always at:

JSC::preCommitStackMemory
JSC::VM::updateStackLimits
JSC::VM::updateSoftReservedZoneSize
JSC::VM::VM
JSC::VM::tryCreate
Zig__GlobalObject__create

i.e. cold process start, before any JS runs. 66% of events are bun build --compile standalone 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 uses GetCurrentThreadStackLimits, which returns the full stack reserve. During VM::VM(), m_stackPointerAtVMEntry is still null, so updateStackLimits() takes the no-entry branch:

newSoftStackLimit = stack.recursionLimit(m_currentSoftReservedZoneSize); // = m_bound + 128KB

and preCommitStackMemory(newSoftStackLimit) then walks from the current SP down to lowLimit + 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 raises EXCEPTION_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 preCommitStackMemory on m_stackPointerAtVMEntry. LLInt/JIT code cannot run before the JSLock is acquired, and JSLock::didAcquireLock() calls setStackPointerAtVMEntry(currentStackPointer()), which re-runs updateStackLimits() with the maxPerThreadStackUsage cap applied. VM::VM() itself takes a JSLockHolder a few lines after the initial updateSoftReservedZoneSize, 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.

…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.
@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 8 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 08ea39d3-5fbd-403b-8660-eac74794d9f5

📥 Commits

Reviewing files that changed from the base of the PR and between a8d15c1 and c70864c.

📒 Files selected for processing (1)
  • Source/JavaScriptCore/runtime/VM.cpp

Comment @coderabbitai help to get the list of available commands.

@Jarred-Sumner
Jarred-Sumner merged commit 639550a into main Jul 19, 2026
47 checks passed
Comment on lines +1227 to +1228
if (m_stackPointerAtVMEntry)
preCommitStackMemory(newSoftStackLimit);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 + reservedZoneSize and availableUserStack = startOfUserStack - endOfStackWithReservedZone. When maxUserStackWithReservedZone > availableUserStack (line 112), it clamps and returns startOfUserStack - availableUserStack — which is exactly endOfStackWithReservedZone = 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.

  1. Call 1 (from updateSoftReservedZoneSize in VM::VM(), m_stackPointerAtVMEntry == nullptr):
    • Takes the else branch → newSoftStackLimit = m_bound + 128 KB = 0x1020000.
    • lastSoftStackLimit was nullptr, so the outer if is true; setStackSoftLimit(0x1020000).
    • New guard: m_stackPointerAtVMEntry is null → skip preCommitStackMemory.
  2. 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 → return 0x10F0000 − 0xD0000 = 0x1020000.
    • lastSoftStackLimit (0x1020000) == newSoftStackLimit (0x1020000) → outer if is false, entire block skipped.
  3. No further updateStackLimits() on this thread will change the limit. preCommitStackMemory is 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.

@github-actions

Copy link
Copy Markdown

Preview Builds

Commit Release Date
c70864c5 autobuild-preview-pr-310-c70864c5 2026-07-19 00:51:40 UTC

robobun added a commit that referenced this pull request Jul 19, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants