Repository navigation
Ensure constraints are loaded when performing constraints checks - #137669
Merged
Merged
Conversation
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: 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. |
davidwrighton
approved these changes
Nov 16, 2026
This was referenced Nov 16, 2026
Open
Member
Author
|
/ba-g infra failures |
Member
Author
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
2 of 4 tasks
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,
Maingets loaded from R2R (with the constraints from Run method not fully loaded) and then attempts to compile theRunmethod. As part of validation of the method constraints, when compiling the recursive call toRun, we will do constraint validation, checking whetherTDerivedimplementsIComparable<TDerived>, viaTypeHandle::IsBoxedAndCanCastTo. This was explicitly loading the constraints forTDerived(informing thatTDerivedderives fromTBase), but, for the successful contravariant interface check, we first need to validate thatTDerivedis a reference type (ConstrainedAsObjRef), which needs to know thatTBasederives fromBase. Since the constraints forTBaseweren't loaded, we would crash inTypeVarTypeDesc::GetCachedConstraintsdue to missing cached constraints.As a fix, we are using
TypeVarTypeDesc::GetConstraintsinstead, 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