Skip to content
Merged
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
10 changes: 9 additions & 1 deletion Source/JavaScriptCore/runtime/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,15 @@
// In contrast, we do not need to worry about VM::m_stackLimit because that limit is
// used exclusively by C++ code, and the C++ compiler will automatically commit the
// needed stack pages.
preCommitStackMemory(newSoftStackLimit);
//
// LLInt/JIT code cannot run before the VM has been entered, at which point
// setStackPointerAtVMEntry() recomputes the limit here against maxPerThreadStackUsage.
// Skipping the pre-entry commit avoids eagerly committing the entire stack reserve
// (StackBounds on Windows reports the full reserve via GetCurrentThreadStackLimits),
// which raises EXCEPTION_STACK_OVERFLOW on commit-constrained hosts when the reserve
// is large.
if (m_stackPointerAtVMEntry)
preCommitStackMemory(newSoftStackLimit);

Check warning on line 1228 in Source/JavaScriptCore/runtime/VM.cpp

View check run for this annotation

Claude / Claude Code Review

preCommitStackMemory never called on threads with stack reserve <= maxPerThreadStackUsage

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 enterin
Comment on lines +1227 to +1228

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.

#endif
}
}
Expand Down
Loading