Repository navigation
VM: skip preCommitStackMemory before stackPointerAtVMEntry is set on Windows #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jarred-Sumner
merged 1 commit into
main
from
farm/0b435ebf/skip-precommit-before-vmentry
Jul 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-JSLockHolderupdateStackLimits()computes the same soft limit as the pre-entry call, solastSoftStackLimit == newSoftStackLimitandpreCommitStackMemoryis 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 whenm_stackPointerAtVMEntrytransitions from null → non-null even if the computed limit is unchanged.Extended reasoning...
What the bug is
The PR gates
preCommitStackMemoryonm_stackPointerAtVMEntrybeing non-null, relying on the fact thatJSLockHolder(a few lines later inVM::VM()) will callJSLock::didAcquireLock()→setStackPointerAtVMEntry()→updateStackLimits()again with the entry pointer set. That second call is expected to enter theif (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 — sopreCommitStackMemoryis 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):m_bound + reservedZoneSize.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 samem_currentSoftReservedZoneSizeas 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 tom_bound + 128 KB. After this PR, the first call enters the block but the newif (m_stackPointerAtVMEntry)guard skips the commit; the second call has the entry pointer set but computes the same limit, so the outerifis false. Subsequent lock release/re-acquire cycles on the same thread produce the same clamped value again. There is no other path that callspreCommitStackMemory.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.updateSoftReservedZoneSizeinVM::VM(),m_stackPointerAtVMEntry == nullptr):newSoftStackLimit = m_bound + 128 KB = 0x1020000.lastSoftStackLimitwasnullptr, so the outerifis true;setStackSoftLimit(0x1020000).m_stackPointerAtVMEntryis null → skippreCommitStackMemory.JSLockHolder lock(this)→didAcquireLock()→setStackPointerAtVMEntry(sp ≈ 0x10F0000)→updateStackLimits():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.updateStackLimits()on this thread will change the limit.preCommitStackMemoryis never invoked.Impact
This regresses the guard-page-skip protection that
preCommitStackMemoryexists 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 tom_bound + 128 KBon 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 everyCreateThreadwithdwStackSize = 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_stackPointerAtVMEntrytransitions from null → non-null, regardless of whether the computed limit changed. For example, hoist the Windows block out of thelastSoftStackLimit != newSoftStackLimitcheck and gate it onm_stackPointerAtVMEntry && (lastSoftStackLimit != newSoftStackLimit || !stackWasCommitted), or simply track a one-shotm_didPreCommitStackflag and commit on the first call wherem_stackPointerAtVMEntryis set.