Skip to content

Fix Linux crash loading recursive Nullable static-abstract value types - #130088

Merged
davidwrighton merged 9 commits into
mainfrom
copilot/fix-sigsegv-linux-nullable
Jul 24, 2026
Merged

davidwrighton merged 9 commits into
mainfrom
copilot/fix-sigsegv-linux-nullable

Conversation

Copilot AI commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

This change addresses a Linux CoreCLR crash during prestub/JIT-triggered type loading of Nullable<T> when T is a recursively constrained value type using static abstract interface members. The failure came from System V struct classification assuming nested value-type metadata was always resolvable during approximate load.

Copilot AI self-assigned this Jul 1, 2026
Copilot AI review requested due to automatic review settings July 1, 2026 16:10
Copilot AI removed the request for review from Copilot July 1, 2026 16:10
Copilot AI changed the title [WIP] Fix SIGSEGV on Linux when loading Nullable<T> with readonly record struct Fix Linux crash loading recursive Nullable static-abstract value types Jul 1, 2026
Copilot AI requested a review from davidwrighton July 1, 2026 16:56
@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.

Copilot AI requested review from Copilot and removed request for Copilot July 1, 2026 17:50
@davidwrighton

Copy link
Copy Markdown
Member

@copilot, returning false from HasImpliedRepeatedFields or ClassifyEightBytesWithManagedLayout is not a conservative approach, it is incorrect. We need to find a suitable TypeHandle for running the logic, not giving up.

…g false

When ClassifyEightBytesWithManagedLayout is called recursively with
pByValueClassCache=NULL, ByValueClassCacheLookup returns NULL for
value-type fields. The subsequent GetSize(NULL) call resolves the field
type via LookupApproxFieldTypeHandle (DontLoadTypes), which can fail to
find a generic instantiation that was canonicalized during recursive
layout loading (e.g. EntityIdValue<UserId> → EntityIdValue<__Canon>),
causing a null TypeHandle assert in debug or SIGSEGV in release.

Fix: when pByValueClassCache is NULL and the field is ELEMENT_TYPE_VALUETYPE,
call GetApproxFieldTypeHandleThrowing() (LoadTypes + dropGenericArgumentLevel=TRUE)
which always resolves to the canonical __Canon form and never returns null.
Pre-resolve pFirstFieldValueTypeMT before the HasImpliedRepeatedFields call
and thread it through all subsequent GetSize() invocations.

Co-authored-by: davidwrighton <[email protected]>
Copilot AI requested review from Copilot and removed request for Copilot July 1, 2026 22:32

Copilot AI commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot, returning false from HasImpliedRepeatedFields or ClassifyEightBytesWithManagedLayout is not a conservative approach, it is incorrect. We need to find a suitable TypeHandle for running th...

Fixed in 2280e79. Instead of returning false, both HasImpliedRepeatedFields and ClassifyEightBytesWithManagedLayout now call GetApproxFieldTypeHandleThrowing() (which uses LoadTypes + dropGenericArgumentLevel=TRUE) when pByValueClassCache is NULL and the field is ELEMENT_TYPE_VALUETYPE. This always resolves to the canonical __Canon form and never returns null. pFirstFieldValueTypeMT is pre-resolved once before the HasImpliedRepeatedFields call and threaded through all subsequent GetSize() invocations.

@davidwrighton davidwrighton 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.

@copilot, the fix to methodtable.cpp is correct. Do not change it. However, the test placement is wrong. Please fix.

Copilot AI review requested due to automatic review settings July 2, 2026 16:20
@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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

src/coreclr/vm/methodtable.cpp:2218

  • The comment says GetApproxFieldTypeHandleThrowing "always succeeds", but it can throw (e.g., type load failure / OOM). To avoid misleading future readers, please reword this to indicate that it avoids returning a null TypeHandle by loading (or throwing) rather than guaranteeing success.
        // field MT.  Otherwise look up the cache entry; when pByValueClassCache is NULL
        // (recursive call) the exact generic instantiation may not be in the type loader hash
        // yet, so fall back to GetApproxFieldTypeHandleThrowing which loads the canonical
        // __Canon form and always succeeds.

Copilot AI review requested due to automatic review settings July 22, 2026 23:11

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.

Copilot was unable to review this pull request because there is no eligible user to bill. To allow Copilot reviews on bot-authored pull requests, enable direct organization billing in your organization's Copilot settings.

@AaronRobinsonMSFT AaronRobinsonMSFT 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.

Is anything needed for native AOT or mono here?

Comment thread src/coreclr/vm/methodtable.cpp Resolved
Comment thread src/coreclr/vm/methodtable.cpp Resolved
Comment thread src/coreclr/vm/methodtable.cpp Resolved
Comment thread src/coreclr/vm/methodtable.cpp Resolved
Comment thread src/coreclr/vm/methodtable.cpp Resolved
Comment thread src/coreclr/vm/methodtable.cpp Resolved
Comment thread src/coreclr/vm/methodtable.cpp Resolved
…a helper

- Extract ResolveValueTypeFieldMT to collapse three near-duplicate
  resolve-then-fallback blocks in System V struct classification.
- Move the explanatory comment (including how GetApproxFieldTypeHandleThrowing
  implies dropGenericArgumentLevel=TRUE) onto the helper.
- Use NULL and test the pointer before the field type, matching file style.
- Remove the now-dead fieldIndexForCacheLookup variable and its reset.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: dbf1f745-5a2e-46aa-86c1-a1f50eb42c63
@davidwrighton

Copy link
Copy Markdown
Member

Is anything needed for native AOT or mono here?

No. This fix is specific to CoreCLR's runtime System V (Unix x64) struct classification in MethodTable::ClassifyEightBytesWithManagedLayout, which resolves nested value-type field metadata during prestub/JIT-triggered approximate type load and could hit a partially-loaded (canonicalized) instantiation.

  • NativeAOT: struct classification happens in the compiler's type system ahead of time against fully-loaded types; it doesn't go through this runtime approximate-load path, so it doesn't crash on the recursive shape.
  • Mono: uses its own ABI/marshalling layout code, not this CoreCLR path, and doesn't exhibit the failure.

I confirmed the repro from #127696 doesn't reproduce under either, so no changes are needed there.

Note

AI-generated (GitHub Copilot) on behalf of @davidwrighton.

Copilot AI review requested due to automatic review settings July 23, 2026 18:56

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 3 out of 3 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/tests/Loader/classloader/regressions/LoaderClassloaderRegressions.csproj:9

  • The PR description says the regression is wired into Regression_o_2, but the changes actually add the new test to the Loader/classloader merged regressions project. Please either update the PR description, or also wire the test into the intended JIT regression project if that was the plan.
  <ItemGroup>
    <Compile Include="GitHub_127696/GitHub_127696.cs" />
    <MergedWrapperProjectReference Include="*/**/*.??proj" />
  </ItemGroup>

@davidwrighton

Copy link
Copy Markdown
Member

/ba-g infra issues

@davidwrighton
davidwrighton merged commit ba5a0fd into main Jul 24, 2026
109 of 117 checks passed
@davidwrighton
davidwrighton deleted the copilot/fix-sigsegv-linux-nullable branch July 24, 2026 03:18
hez2010 pushed a commit to hez2010/runtime that referenced this pull request Jul 26, 2026
dotnet#130088)

This change addresses a Linux CoreCLR crash during prestub/JIT-triggered
type loading of `Nullable<T>` when `T` is a recursively constrained
value type using static abstract interface members. The failure came
from System V struct classification assuming nested value-type metadata
was always resolvable during approximate load.

- **VM change**
- Teach managed-layout System V struct classification to treat
temporarily unresolved nested value-type field metadata as a
conservative fallback instead of asserting.
  - Reuse small helpers to:
    - resolve value-type field handles when available
    - compute field sizes without forcing the existing assert path
- Result: recursive/partially loaded cases fall back to non-register
passing rather than crashing.

- **Regression coverage**
  - Add a JIT regression covering the reported shape:
    - `Nullable<UserId>`
    - `UserId` as a `readonly record struct`
    - recursive generic interface constraint
    - `static abstract` interface members
  - Wire the test into `Regression_o_2`.

- **Scenario covered**
  ```csharp
  class MyNullableUser
  {
      public UserId? UserId { get; set; }
  }

  public interface IEntityId<TSelf> : IEntityId
      where TSelf : IEntityId<TSelf>
  {
      EntityIdValue<TSelf> Value { get; }
      static abstract int MaxValue { get; }
  }

public readonly record struct UserId(EntityIdValue<UserId> Value) :
IEntityId<UserId>;
  ```
  
  Fixes dotnet#127696 and dotnet#130661

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: davidwrighton <[email protected]>
Co-authored-by: David Wrighton <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: dbf1f745-5a2e-46aa-86c1-a1f50eb42c63
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 26, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 26, 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.

[JIT] SIGSEGV on Linux when loading Nullable<T> where T is a readonly record struct with static abstract interface members

5 participants