Repository navigation
Fix WKS BGC access violation from stale ma_committed on unlinked expansion segment (#123490) - #131310
Merged
janvorli merged 3 commits intoJul 30, 2026
Merged
Conversation
…nsion segment (dotnet#123490) On x86 Windows with Workstation background GC (segments), a card-table grow that runs during a foreground compacting GC re-commits the new mark array by walking the generation segment lists. The just-created expansion segment S is deliberately not yet threaded onto any generation list, so the walk skips it: S keeps a stale heap_segment_flags_ma_committed while its pages on the new mark array are uncommitted. The next BGC's commit_mark_array_bgc_init trusts that stale flag and skips S, so the concurrent pinning-handle scan later writes to an uncommitted mark array page for a pinned object in S -> AV in background_mark_simple. Server GC already avoids this by keeping the in-flight expansion segment in the durable per-heap member new_heap_segment (needed for its cross-thread hand-off under the GC-thread join) and re-committing it in commit_new_mark_array. This ports that same member and grow-time re-commit to Workstation-segments by widening the existing Server-only preprocessor guards to !USE_REGIONS. Regions builds and Server GC are unaffected. Fixes dotnet#123490. Co-authored-by: Copilot <[email protected]> Copilot-Session: ad1ce451-e247-42ec-accc-c4a688fcd555
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @anicka-net, @dotnet/gc |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a Workstation (WKS) background GC crash on segment-based heaps by tracking the in-flight expansion segment created during compacting GCs and ensuring its mark-array slice is re-committed when the mark array grows (aligning WKS behavior with the existing Server GC safety net for the same scenario).
Changes:
- Extend
new_heap_segmenttracking to single-heap (WKS)!USE_REGIONSbuilds and reset it at the start of planning. - Switch WKS expansion planning to store the expansion segment in the durable
new_heap_segmentfield (instead of a local). - Re-commit
new_heap_segment’s mark-array pages when switching to a newly grown mark array.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/coreclr/gc/plan_phase.cpp | Resets and populates new_heap_segment for WKS compacting+expanding GCs. |
| src/coreclr/gc/gcpriv.h | Adds new_heap_segment to the WKS (single-heap) !USE_REGIONS field set. |
| src/coreclr/gc/gc.cpp | Defines/initializes the WKS static new_heap_segment. |
| src/coreclr/gc/background.cpp | Ensures commit_new_mark_array also re-commits the unlinked expansion segment’s mark-array slice. |
janvorli
reviewed
Jul 24, 2026
Co-authored-by: Copilot <[email protected]> Copilot-Session: ad1ce451-e247-42ec-accc-c4a688fcd555
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/coreclr/gc/gcpriv.h:3551
- The
#endifcomment doesn't match the corresponding directive (#ifndef USE_REGIONS). This file generally uses//!USE_REGIONSfor#ifndefblocks, and the native-style guidance prefers matching#endifcomments to the opening conditional to avoid confusion during future edits.
#ifndef USE_REGIONS
PER_HEAP_FIELD_SINGLE_GC heap_segment* new_heap_segment;
#endif //USE_REGIONS
Co-authored-by: Copilot <[email protected]> Copilot-Session: ad1ce451-e247-42ec-accc-c4a688fcd555
kkokosa
marked this pull request as ready for review
July 28, 2026 09:54
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fix #123490: WKS BGC access violation in
background_mark_simpleduring pinning-handle scanFixes #123490.
The same assertion — an identical
WKS::gc_heap::background_mark_simplepinning-scan accessviolation on x86-Checked in the
GC/Features/HeapExpansion/plugtest — was reported earlier asintermittent CI failures in #88977, #93162, and #105211. Those were closed as transient / no-repro
(or as a presumed duplicate of an unrelated heap-corruption fix) without a root cause ever being
established; this is the same symptom, now root-caused and fixed.
Summary
On x86 Windows with Workstation background GC (segments layout), a background GC can crash with
an access violation inside
WKS::gc_heap::background_mark_simplewhile the BGC thread scans pinninghandles (
background_promote←PinObject← async pinning-handle scan). The faulting object is aperfectly valid, live, pinned object — the crash is not object corruption. The BGC reads an
uncommitted page of the mark array for the segment that object lives on.
This ports Server GC's existing safety net to Workstation GC so the mark array for an in-flight
expansion segment is re-committed whenever the card table / mark array grows.
Root cause
The whole corruption window is a single foreground (ephemeral) GC that runs mid-BGC with the EE
suspended — one thread, no concurrent race. The BGC only sets the stage (it forces every new
segment to have a committed mark array) and delivers the delayed crash in a later epoch. Here is
the sequence, with addresses from the reproduced dump (
core.4988):1 — A segment
Sis born mid-BGC, off-list. A compacting foreground GC needs room to promote,so it creates SOH segment
S = 0x4CB50000. Because a BGC is in progress,commit_mark_array_new_segcommits
S's slice on the current mark array (0x5153C400) and setsheap_segment_flags_ma_committed.Sis put in the segment-mapping table, but — perexpand_heap— it is deliberately not threadedonto any generation's segment list while relocation/compaction runs:
At this instant the flag is honest:
Sreally is committed on the array that is live right now.2 — A grow fires before
Sis linked, on the same thread. The same compacting GC keepspromoting survivors into gen2, and as gen2 fills it acquires further SOH segments; one of them is
reserved above the heap's current high boundary, which extends the address range the card/brick/mark
tables must cover. Still inside that one foreground GC, that next segment therefore triggers
grow_brick_card_tables. Because a BGC is active, the grow allocates a new, larger mark array(
0x53AAE400) and re-commits existing segments onto it viacommit_new_mark_array— which walksthe generation segment lists.
Sis on none of them, so the walk never reaches it. The livemark_arraypointer is switched to the new array, andS's slice on it was never committed. Thema_committedflag is now stale: it describes the retired array, not the live one.3 — The foreground GC finishes. Relocation/compaction completes,
Sis eventually linked, andthe EE resumes — with
Sstill carrying its stalema_committed = TRUE.4 — A later BGC trusts the stale flag and crashes. The next BGC's
commit_mark_array_bgc_initcommits mark-array pages for each segment unless it already claims
ma_committed; trustingS'sstale flag, it skips
S. When that BGC's concurrent pinning-handle scan later marks a live,pinned object living in
S(ScanConsecutiveHandlesWithoutUserData→PinObject→background_promote→background_mark_simple), the write tomark_array[obj >> 8]hits anuncommitted page → access violation. The object is valid; the mark-array memory being written to
is unmapped.
Why Workstation only
Server GC already keeps the in-flight expansion segment in a durable per-heap member,
new_heap_segment— a cross-thread hand-off it needs because the expand decision happens under aGC-thread join where one thread stashes each heap's segment for the others.
commit_new_mark_arrayre-commits that member in addition to the generation-list walk, so on Server the off-list segment
is caught during a grow. Workstation, with a single GC thread and no join, kept the segment in a
local variable, so its grow had nothing to re-commit — the crash is Workstation-only. The entire
new_heap_segmentmachinery was previously behind#ifdef MULTIPLE_HEAPS.The fix
Make Workstation-segments use the same durable member and grow-time re-commit that Server already
has. Every change simply widens an existing Server-only preprocessor guard to also cover
Workstation-segments (
!USE_REGIONS);USE_REGIONS(regions) builds are untouched. After the fix,Workstation's
new_heap_segmentlifetime is byte-for-byte identical to Server's, which has shippedthis pattern for years.
Validation
A deterministic repro was built (a temporary env-gated hook that forces the exact
"grow while
Sis unlinked" interleaving), turning an otherwise ~1/2000-over-8h race into anon-demand crash. Native x86 Checked coreclr, Workstation background GC (segments):
WKS::gc_heap, pinning-scan / background-sweep mark-array read).plug.dll, hook removed)clr.runtimebuilds clean (0 warnings, 0 errors).clr.runtimebuilds clean (regions default + Server namespace unaffected).The repro hook and all diagnostic instrumentation were reverted before finalizing; the committed
diff is the fix only.
Scope / risk
!USE_REGIONS, i.e. 32-bit builds). Regions builds(default on 64-bit) are excluded by the guards and unchanged.
Note
This pull request description was drafted with the assistance of GitHub Copilot.