Skip to content

Fix GC hole in InlineArray struct layout - #137932

Open
jkotas wants to merge 5 commits into
mainfrom
copilot/fix-gc-hole-inlinearray
Open

jkotas wants to merge 5 commits into
mainfrom
copilot/fix-gc-hole-inlinearray

Conversation

@jkotas

@jkotas jkotas commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

In GatherClassGCLayout, MarkGcField returns 0 when a slot is already marked with the same GC type. Because o marks slot 0 first, walking Arr gives result == 0, and the replication loop for elements 1..N-1 is skipped. Replication should depend on the element's GC layout (e.g. ContainsGCPointers), not on the number of newly marked slots.

Fixes #137842

@azure-pipelines

azure-pipelines Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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: @agocke
See info in area-owners.md if you want to be subscribed.

@jkotas

jkotas commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

@copilot Try again. This is not a security fix

Copilot AI commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@copilot Try again. This is not a security fix

Resumed and validated the correctness fix in ed564e6d with the test follow-up in 46622e67. The CoreCLR/libraries/host baseline builds successfully. Six inline-array cases fail against the original implementation; all 48 GC-info regression cases pass with the fix restored. No further code changes were needed.

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.

🔵 Needs a closer look

The shared unsafe GC-layout change needs maintainer confirmation because reported Crossgen2 GCStress testing does not establish NativeAOT runtime coverage for recursive nested/reference/byref replication.

No open issues 🎉

What changed in this PR

Fixes incomplete GC layouts for inline arrays overlapped by reference fields, addressing #137842 in the shared JIT interface used by crossgen2 and NativeAOT.

Changes:

  • Processes each inline-array element independently of whether earlier slots were already marked.
  • Replaces layout copying with per-element GC marking to account for overlapping fields.
  • Adds a regression test checking that overlapping inline-array references survive a forced compacting GC.
File Description
src/​tests/​Loader/​classloader/​InlineArray/​InlineArrayValid.cs Adds GC-survival coverage for an inline array overlapped by an object field.
src/​coreclr/​tools/​Common/​JitInterface/​CorInfoImpl.cs Ensures GC layout generation marks all inline-array elements despite existing overlapping slots.

Review effort: Balanced

@MichalStrehovsky

Copy link
Copy Markdown
Member

Doesn't the same bug exist in jitinterface.cpp?

if (isInlineArray)
{
if (result > 0)
{
_ASSERTE(pFD->GetOffset() == 0);
DWORD totalLayoutSize = pMT->GetNumInstanceFieldBytes() / TARGET_POINTER_SIZE;
DWORD elementLayoutSize = pFD->GetSize() / TARGET_POINTER_SIZE;
DWORD gcPointersInElement = result;
for (DWORD offset = elementLayoutSize; offset < totalLayoutSize; offset += elementLayoutSize)
{
memcpy(gcPtrs + offset, gcPtrs, elementLayoutSize);
result += gcPointersInElement;
}
}
// inline array has only one element field
break;
}

I would expect us to see the test failing because of that, but maybe the test can just luck out even if we don't report the elements?

@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr gcstress-extra

@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Let's see whether the test is any good

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@copilot Double check that the test is failing without the fix and passes with the fix (with GCStress on). It does not appear to be the case

Copilot AI commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@copilot Double check that the test is failing without the fix and passes with the fix (with GCStress on). It does not appear to be the case

Verified on Linux x64 Debug with DOTNET_GCStress=0xC, using the standalone runtime test harness and freshly crossgen2-compiled test images: the unfixed compiler fails at element 1 (Expected: aa), while the fixed compiler passes all 12 tests. R2RDump shows 1 untracked stack slot without the fix versus 42 with it; the test method was not JIT-compiled in these runs.

A separate xUnit-console run passed with both images because its second assembly load disabled ReadyToRun; that run does not validate the fix. No source changes were needed.

@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@copilot The same issue exists with JIT - see #137932 (comment) . Can you modify the test so that it crashes with JIT? (Do not fix the bug in the JIT just yet.)

@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@copilot Try again - this is reliability bug, not a sensitive security issue

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crossgen: (bug) GC hole when an [InlineArray] is overlapped by a ref field at offset 0 (elements 1..N-1 lose their GC slots)

4 participants