Skip to content

Ensure constraints are loaded when performing constraints checks - #137669

Merged
BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-r2r-constraints
Nov 17, 2026
Merged

BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-r2r-constraints

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

When JIT is used we fully load the type together with the constraints from all generic methods. When compiling with R2R, we set READYTORUN_FLAG_SKIP_TYPE_VALIDATION on the module, which defers loading of the constraints, following PR #120459.

In the added testcase, when running it in R2R mode, Main gets loaded from R2R (with the constraints from Run method not fully loaded) and then attempts to compile the Run method. As part of validation of the method constraints, when compiling the recursive call to Run, we will do constraint validation, checking whether TDerived implements IComparable<TDerived>, via TypeHandle::IsBoxedAndCanCastTo. This was explicitly loading the constraints for TDerived (informing that TDerived derives from TBase), but, for the successful contravariant interface check, we first need to validate that TDerived is a reference type (ConstrainedAsObjRef), which needs to know that TBase derives from Base. Since the constraints for TBase weren't loaded, we would crash in TypeVarTypeDesc::GetCachedConstraints due to missing cached constraints.

As a fix, we are using TypeVarTypeDesc::GetConstraints instead, which has the same behavior as the cached version, except it loads the constraints if they weren't available. As part of this change we need to change the contract of some constrained check helper methods to THROWS and GC_TRIGGERS, but this is not a problem since all their callers already had this contract. Also validated that this doesn't introduce any GC holes in these callers, which wouldn't have expected a GC to get triggered.

Add runtime test

Fixes #136504

When JIT is used we fully load the type together with the constraints from all generic methods. When compiling with R2R, we set READYTORUN_FLAG_SKIP_TYPE_VALIDATION on the module, which defers loading of the constraints, following PR dotnet#120459.

In the added testcase, when running it in R2R mode, `Main` gets loaded from R2R (with the constraints from Run method not fully loaded) and then attempts to compile the `Run` method. As part of validation of the method constraints, when compiling the recursive call to `Run`, we will do constraint validation, checking whether `TDerived` implements `IComparable<TDerived>`, via `TypeHandle::IsBoxedAndCanCastTo`. This was explicitly loading the constraints for `TDerived` (informing that `TDerived` derives from `TBase`), but, for the successful contravariant interface check, we first need to validate that `TDerived` is a reference type (`ConstrainedAsObjRef`), which needs to know that `TBase` derives from `Base`. Since the constraints for `TBase` weren't loaded, we would crash in `TypeVarTypeDesc::GetCachedConstraints` due to missing cached constraints.

As a fix, we are using `TypeVarTypeDesc::GetConstraints` instead, which has the same behavior as the cached version, except it loads the constraints if they weren't available. As part of this change we need to change the contract of some constrained check helper methods to THROWS and GC_TRIGGERS, but this is not a problem since all their callers already had this contract. Also validated that this doesn't introduce any GC holes in these callers, which wouldn't have expected a GC to get triggered.

Add runtime test
@azure-pipelines

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.

@BrzVlad

BrzVlad commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

/ba-g infra failures

@BrzVlad
BrzVlad merged commit 2ce18f7 into dotnet:main Nov 17, 2026
98 of 103 checks passed
@BrzVlad

BrzVlad commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

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

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
jkotas pushed a commit that referenced this pull request Nov 20, 2026
…nts checks (#137719)

Backport of #137669 to release/11.0

Fixes #136504

/cc @BrzVlad

## Customer Impact

- [x] Customer reported
- [ ] Found internally

On iOS, when we have mixing between R2R compiled code and interpreter
compiled code (particularly in debug builds where we don't R2R
everything), when compiling methods with constraints on the generic
arguments we can reach a situation where we were expecting these
constraints to have been loaded, even though they were not. This leads
to crash when trying to compile this code.

## Regression

- [x] Yes
- [ ] No

Regression in .NET11 following switch from Mono

## Testing

Reproduced and validated issue locally. Added runtime test case.

## Risk

Low. The fix triggers loading of the constraints in a few places, where
they could have been missing.

Co-authored-by: Vlad Brezae <[email protected]>
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.

CoreCLR iOS interpreter crashes on generic type with mutually constrained type parameters

2 participants