Skip to content

Fix WKS BGC access violation from stale ma_committed on unlinked expansion segment (#123490) - #131310

Merged
janvorli merged 3 commits into
dotnet:mainfrom
kkokosa:fix/123490-wks-bgc-stale-ma-committed
Jul 30, 2026
Merged

janvorli merged 3 commits into
dotnet:mainfrom
kkokosa:fix/123490-wks-bgc-stale-ma-committed

Conversation

@kkokosa

@kkokosa kkokosa commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Fix #123490: WKS BGC access violation in background_mark_simple during pinning-handle scan

Fixes #123490.

The same assertion — an identical WKS::gc_heap::background_mark_simple pinning-scan access
violation on x86-Checked in the GC/Features/HeapExpansion/plug test — was reported earlier as
intermittent 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_simple while the BGC thread scans pinning
handles (background_promote ← PinObject ← async pinning-handle scan). The faulting object is a
perfectly 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 S is 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_seg
commits S's slice on the current mark array (0x5153C400) and sets heap_segment_flags_ma_committed.
S is put in the segment-mapping table, but — per expand_heap — it is deliberately not threaded
onto any generation's segment list
while relocation/compaction runs:

//Note: the ephemeral segment shouldn't be threaded onto the segment chain
//because the relocation and compact phases shouldn't see it

At this instant the flag is honest: S really is committed on the array that is live right now.

2 — A grow fires before S is linked, on the same thread. The same compacting GC keeps
promoting 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 via commit_new_mark_array — which walks
the generation segment lists
. S is on none of them, so the walk never reaches it. The live
mark_array pointer is switched to the new array, and S's slice on it was never committed. The
ma_committed flag is now stale: it describes the retired array, not the live one.

3 — The foreground GC finishes. Relocation/compaction completes, S is eventually linked, and
the EE resumes — with S still carrying its stale ma_committed = TRUE.

4 — A later BGC trusts the stale flag and crashes. The next BGC's commit_mark_array_bgc_init
commits mark-array pages for each segment unless it already claims ma_committed; trusting S's
stale 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 to mark_array[obj >> 8] hits an
uncommitted 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 a
GC-thread join where one thread stashes each heap's segment for the others. commit_new_mark_array
re-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_segment machinery 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_segment lifetime is byte-for-byte identical to Server's, which has shipped
this pattern for years.

Validation

A deterministic repro was built (a temporary env-gated hook that forces the exact
"grow while S is unlinked" interleaving), turning an otherwise ~1/2000-over-8h race into an
on-demand crash. Native x86 Checked coreclr, Workstation background GC (segments):

Build Result
Without fix (repro hook on) 6/6 crash — AV on the BGC thread (WKS::gc_heap, pinning-scan / background-sweep mark-array read).
With fix (repro hook on) 8/8 pass (exit 100) — the identical forced grow fires 3× per run, no crash.
With fix (plain plug.dll, hook removed) 4/4 pass (exit 100).
  • x86 Checked clr.runtime builds clean (0 warnings, 0 errors).
  • x64 Checked clr.runtime builds 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

  • Affects Workstation + segments only (!USE_REGIONS, i.e. 32-bit builds). Regions builds
    (default on 64-bit) are excluded by the guards and unchanged.
  • Server GC behavior is unchanged (it already had this member and re-commit).
  • No new allocation, no new locking; the field mirrors an existing, long-shipped Server field.

Note

This pull request description was drafted with the assistance of GitHub Copilot.

…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
Copilot AI review requested due to automatic review settings July 24, 2026 10:40
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @anicka-net, @dotnet/gc
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_segment tracking to single-heap (WKS) !USE_REGIONS builds and reset it at the start of planning.
  • Switch WKS expansion planning to store the expansion segment in the durable new_heap_segment field (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.

Comment thread src/coreclr/gc/gcpriv.h Resolved
Comment thread src/coreclr/gc/gcpriv.h Resolved
Comment thread src/coreclr/gc/plan_phase.cpp Resolved
Co-authored-by: Copilot <[email protected]>
Copilot-Session: ad1ce451-e247-42ec-accc-c4a688fcd555
Copilot AI review requested due to automatic review settings July 28, 2026 09:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 #endif comment doesn't match the corresponding directive (#ifndef USE_REGIONS). This file generally uses //!USE_REGIONS for #ifndef blocks, and the native-style guidance prefers matching #endif comments 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
Copilot AI review requested due to automatic review settings July 28, 2026 09:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@kkokosa
kkokosa marked this pull request as ready for review July 28, 2026 09:54
@azure-pipelines

Copy link
Copy Markdown
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.

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you!

@janvorli
janvorli merged commit 727a4ac into dotnet:main Jul 30, 2026
108 of 110 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 31, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GC - segments] Consistency check failed: AV in clr at this callstack

3 participants