Repository navigation
Description
While auditing ReadyToRun (R2R) version-resilience around embedClassHandle and embedGenericHandle (see #116808), we found two active places where Crossgen2 and the JIT combine an unguarded handle or type reference outside the version bubble with a compile-time metadata query that is not version-resilient across servicing updates.
1. EqualityComparer<T>.Default and Comparer<T>.Default exact return-type inference ignores the version bubble
In src/coreclr/jit/importercalls.cpp (impGetSpecialIntrinsicExactReturnType), the JIT recognizes EqualityComparer<T>.get_Default() and Comparer<T>.get_Default() as special intrinsics and queries ICorJitInfo::getDefaultEqualityComparerClass / getDefaultComparerClass.
In src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs (getDefaultComparerClass / getDefaultEqualityComparerClass, lines 1776–1790), the compiler delegates directly to ComparerIntrinsics.GetComparerForType / GetEqualityComparerForType without checking whether comparand (T) is in the version bubble (VersionsWithType):
ComparerIntrinsicsinspectsT's compile-timeRuntimeInterfaces(IEquatable<T>/IComparable<T>).- In
src/coreclr/jit/gentree.cpp(gtGetClassHandle, lines 21480–21489), the JIT marks the return value ofget_Default()as having exact, non-null type (*pIsExact = true,*pIsNonNull = true).
What goes wrong when T is outside the version bubble:
Suppose External.dll (outside the version bubble) defines public class ExtClass { public int X; } in v1, and in v2 adds IEquatable<ExtClass> (public class ExtClass : IEquatable<ExtClass>):
- When compiling an R2R assembly against v1,
getDefaultEqualityComparerClass(typeof(ExtClass))returnsObjectEqualityComparer<ExtClass>. - Because the JIT treats the return type of
EqualityComparer<ExtClass>.Defaultas exact (ObjectEqualityComparer<ExtClass>) andembedGenericHandleallows embeddingtypeof(ObjectEqualityComparer<ExtClass>)without a version-bubble check:EqualityComparer<ExtClass>.Default.GetType() == typeof(ObjectEqualityComparer<ExtClass>)is constant-folded tomov eax, 1(true).EqualityComparer<ExtClass>.Default is ObjectEqualityComparer<ExtClass>is folded to a non-null check (true).- When
ExtClassis updated to v2 (implementingIEquatable<ExtClass>),EqualityComparer<ExtClass>.Defaultat runtime is actuallyGenericEqualityComparer<ExtClass>, so the R2R code returnstruewhile JIT returnsfalse.
Fix direction: Override or gate getDefaultEqualityComparerClass and getDefaultComparerClass in CorInfoImpl.ReadyToRun.cs (or ComparerIntrinsics) so that they return null when !_compilationModuleGroup.VersionsWithType(comparand) (or when any type argument needed to determine the comparer does not version with the bubble).
2. CrossModuleInlineableUncached permits non-generic external types (!t.HasInstantiation) in synthesized CoreLib intrinsics (RuntimeHelpers.IsBitwiseEquatable<T>, EqualityComparer<T>.Create)
In src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCompilationModuleGroupBase.cs:595-607, CrossModuleInlineableUncached checks generic instantiation arguments using:
foreach (TypeDesc t in method.Instantiation)
{
if (!VersionsWithType(t) && t.HasInstantiation)
return false;
}Because of && t.HasInstantiation, when --opt-cross-module System.Private.CoreLib is enabled, any CoreLib generic method instantiated over an external non-generic type (ExtStruct or ExtClass outside the version bubble) is considered cross-module inlineable!
Combined with ReadyToRunILProvider.GetMethodIL (src/coreclr/tools/aot/ILCompiler.ReadyToRun/IL/ReadyToRunILProvider.cs:82-136), which synthesizes per-instantiation IL bodies for:
RuntimeHelpers.IsBitwiseEquatable<T>()(RuntimeHelpersIntrinsics.EmitILinsrc/coreclr/tools/Common/TypeSystem/IL/Stubs/RuntimeHelpersIntrinsics.cs:54)EqualityComparer<T>.Create()andComparer<T>.Create()(ComparerIntrinsics.EmitEqualityComparerCreate/EmitComparerCreate)
this causes Crossgen2 to inline ldc.i4.1 / ldc.i4.0 for RuntimeHelpers.IsBitwiseEquatable<ExtStruct>() (or new ObjectEqualityComparer<ExtStruct>() for EqualityComparer<ExtStruct>.Create()) directly into the caller without checking VersionsWithType(ExtStruct) or emitting a CheckTypeLayout fixup. If ExtStruct changes fields, overrides Equals, or implements IEquatable<ExtStruct> in a servicing update, the inlined caller uses stale compile-time assumptions.
Fix direction: Either ReadyToRunILProvider.GetMethodIL must refuse to synthesize per-instantiation IL stubs when !_compilationModuleGroup.VersionsWithType holds for the type argument, or CrossModuleInlineableUncached should require VersionsWithType(t) for instantiation arguments of per-instantiation synthesized intrinsics.
Note
This issue was created with assistance from GitHub Copilot.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
[-]ReadyToRun: Unsafe cross-version-bubble compile-time type folding via embedGenericHandle, isEnum, and comparer intrinsics[/-][+]ReadyToRun: Unsafe cross-version-bubble compile-time folding in comparer and RuntimeHelpers intrinsics[/+]dotnet-policy-service commentedon Nov 9, 2026
Tagging subscribers to this area: @dotnet/crossgen-contrib
See info in area-owners.md if you want to be subscribed.
Check version bubble for R2R comparer and per-instantiation intrinsics (
Remove VersionsWithType check in embedClassHandle and check full impl…