Skip to content

Fix GC on Windows with multiple CPU groups - #134127

Merged
janvorli merged 2 commits into
dotnet:mainfrom
janvorli:fix-windows-gc-with-multiple-cpu-groups
Sep 18, 2026
Merged

janvorli merged 2 commits into
dotnet:mainfrom
janvorli:fix-windows-gc-with-multiple-cpu-groups

Conversation

@janvorli

Copy link
Copy Markdown
Member

When there are multiple CPU groups on Windows, the OS can assign the current process into any of these groups. When CPU groups support is not enabled for GC (using DOTNET_GCCpuGroup=1 or other means), the CPU index that is computed from the group index and cpu number exceeds some of the preallocated arrays for mapping processor to heap or numa node to heap and it can result in writing past those arrays, corrupting other heap allocations.

This change fixes it by getting the max processor count based on the max CPU group count. This max count includes even possibly hot-plugged CPU groups.

When there are multiple CPU groups on Windows, the OS can assign the
current process into any of these groups. When CPU groups support is not
enabled for GC (using DOTNET_GCCpuGroup=1 or other means), the CPU index
that is computed from the group index and cpu number exceeds some of the
preallocated arrays for mapping processor to heap or numa node to heap
and it can result in writing past those arrays, corrupting other heap
allocations.

This change fixes it by getting the max processor count based on the max
CPU group count.
@janvorli janvorli added this to the 10.0.x milestone Sep 17, 2026
@janvorli
janvorli requested a review from jkotas September 17, 2026 12:02
@janvorli janvorli self-assigned this Sep 17, 2026
Copilot AI lite review requested due to automatic review settings September 17, 2026 12:02
@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.

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.

🟢 Approval recommended

The focused change correctly sizes mappings for all possible Windows processor groups without altering normal processor-count behavior.

Pull request overview

Fixes Windows GC array sizing when processes run across multiple CPU groups without GC CPU-group support.

Changes:

  • Computes maximum processor capacity from the OS maximum processor-group count.
  • Prevents processor and NUMA mapping arrays from being undersized.
File summaries
File Description
src/coreclr/gc/windows/gcenv.windows.cpp Updates Windows maximum processor-count calculation.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

@jkotas

jkotas commented Sep 18, 2026

Copy link
Copy Markdown
Member

The affinity set is initialized using GetTotalProcessorCount in some places and using GetMaxProcessorCount in other places. Is that a problem?

if (!g_processAffinitySet.Initialize(GCToOSInterface::GetTotalProcessorCount()))

if (!config_affinity_set.Initialize(GCToOSInterface::GetMaxProcessorCount()))

Can we capture the difference between GetTotalProcessorCount and GetMaxProcessorCount in a comment? It is not clear why we have two APIs.

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.

🟡 Changes recommended

The new API call can return zero on failure, leading to zero-length arrays and out-of-bounds access.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

@janvorli

janvorli commented Sep 18, 2026 •

Copy link
Copy Markdown
Member Author

The affinity set is initialized using GetTotalProcessorCount in some places and using GetMaxProcessorCount in other places. Is that a problem?

Thank you for spotting it. It is wrong, it needs to use the max count in all cases. The max processor count represents the maximum possible count of processors in the process and includes possible hot-plugged CPUs. The total processor count is the current total CPU count running the process. When CPU groups support is not enabled, the process is limited to single CPU group, so this is limited to 64 CPUs.

The comment on the methods attempt to describe it:

// Gets the total number of processors on the machine, not taking
// into account current process affinity.
// Return:
// Number of processors on the machine
static uint32_t GetTotalProcessorCount();
// Gets the maximum number of processors that could potentially exist on
// the machine (including offlined ones). Processor indices returned by
// GetCurrentProcessorNumber are guaranteed to be less than this value.
// Return:
// Maximum number of processors
static uint32_t GetMaxProcessorCount();

@janvorli

Copy link
Copy Markdown
Member Author

Actually, no, it was correct. The g_processAffinitySet cannot be initialized by the GetMaxProcessorCount, because the GetMaxProcessorCount is implemented as g_processAffinitySet.MaxCpuCount(), so it would cause an infinite loop.
The g_processAffinitySet needs to represent the current affinity set of the process at launch. So the GetTotalProcessorCount was right there.
I need to remember why I am initializing the config_affinity_set with the GetMaxProcessorCount.

Copilot AI review requested due to automatic review settings September 18, 2026 13:46

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.

🟢 Approval recommended

The bounded processor count safely covers group-based indices and handles query failure correctly.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@janvorli
janvorli merged commit a540e47 into dotnet:main Sep 18, 2026
107 of 110 checks passed
@janvorli

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@janvorli

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@janvorli backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git cherry-pick a540e47694dfa5a10444c8f0958366120bddf1cb

Auto-merging src/coreclr/gc/windows/gcenv.windows.cpp
CONFLICT (content): Merge conflict in src/coreclr/gc/windows/gcenv.windows.cpp
error: could not apply a540e47694d... Fix GC on Windows with multiple CPU groups (#134127)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"


$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix GC on Windows with multiple CPU groups
Applying: Reflect PR feedback
Using index info to reconstruct a base tree...
M	src/coreclr/gc/windows/gcenv.windows.cpp
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/gc/windows/gcenv.windows.cpp
CONFLICT (content): Merge conflict in src/coreclr/gc/windows/gcenv.windows.cpp
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Reflect PR feedback
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

steveisok pushed a commit that referenced this pull request Sep 21, 2026
Backport of #134127 to release/11.0.

## Customer Impact

- [x] Customer reported
- [ ] Found internally

On Windows systems with multiple CPU groups, a process running in a
nonzero processor group can use a packed processor index beyond the
dynamically allocated GC processor-to-heap mapping. This can write past
the allocation and cause heap corruption when GC CPU-group support is
disabled.

The fix sizes the relevant mappings and affinity set for the maximum
possible processor-group index space. It also validates and caches the
Windows processor-group count during GC OS initialization.

This issue was reported by two 1st party customers independently.

## Regression

- [x] Yes
- [ ] No

Regression introduced by #131740.

## Testing

CoreCLR tests

## Risk

Low. This is a direct backport of the fix merged into main in #134127.
The change only increases Windows GC processor-indexed allocation
capacity and fails initialization if the topology API unexpectedly
returns zero.

> [!NOTE]
> This pull request description was generated with GitHub Copilot.

Co-authored-by: Jan Vorlicek <[email protected]>
steveisok pushed a commit that referenced this pull request Sep 21, 2026
Backport of #134127 to release/10.0.

## Customer Impact

- [x] Customer reported
- [ ] Found internally

On Windows systems with multiple CPU groups, a process running in a
nonzero processor group can use a packed processor index beyond the
dynamically allocated GC processor-to-heap mapping. This can write past
the allocation and cause heap corruption when GC CPU-group support is
disabled.

The fix sizes the relevant mappings and affinity set for the maximum
possible processor-group index space. It also validates and caches the
Windows processor-group count during GC OS initialization.

This issue was reported by two 1st party customers independently.

## Regression

- [x] Yes
- [ ] No

Regression introduced by #131740.

## Testing

Built the checked x64 CoreCLR runtime with:

`src\coreclr\build-runtime.cmd -x64 -checked -component runtime`

## Risk

Low. This is a direct backport of the fix merged into main in #134127.
The change only increases Windows GC processor-indexed allocation
capacity and fails initialization if the topology API unexpectedly
returns zero.

> [!NOTE]
> This pull request description was generated with GitHub Copilot.

Copilot-Session: da1e6e4c-bb17-4b8b-9962-b0b5f8b38bcb
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 19, 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.

3 participants