Skip to content

ReadyToRun: Unsafe cross-version-bubble compile-time folding in comparer and RuntimeHelpers intrinsics #137054

Closed

Description

@davidwrighton

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):

  • ComparerIntrinsics inspects T's compile-time RuntimeInterfaces (IEquatable<T> / IComparable<T>).
  • In src/coreclr/jit/gentree.cpp (gtGetClassHandle, lines 21480–21489), the JIT marks the return value of get_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>):

  1. When compiling an R2R assembly against v1, getDefaultEqualityComparerClass(typeof(ExtClass)) returns ObjectEqualityComparer<ExtClass>.
  2. Because the JIT treats the return type of EqualityComparer<ExtClass>.Default as exact (ObjectEqualityComparer<ExtClass>) and embedGenericHandle allows embedding typeof(ObjectEqualityComparer<ExtClass>) without a version-bubble check:
    • EqualityComparer<ExtClass>.Default.GetType() == typeof(ObjectEqualityComparer<ExtClass>) is constant-folded to mov eax, 1 (true).
    • EqualityComparer<ExtClass>.Default is ObjectEqualityComparer<ExtClass> is folded to a non-null check (true).
    • When ExtClass is updated to v2 (implementing IEquatable<ExtClass>), EqualityComparer<ExtClass>.Default at runtime is actually GenericEqualityComparer<ExtClass>, so the R2R code returns true while JIT returns false.

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.EmitIL in src/coreclr/tools/Common/TypeSystem/IL/Stubs/RuntimeHelpersIntrinsics.cs:54)
  • EqualityComparer<T>.Create() and Comparer<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.

Activity

  1. added
    area-crossgen2-coreclr
    on Nov 6, 2026
  2. added
    untriaged
    on Nov 6, 2026
  3. changed the title [-]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[/+] on Nov 6, 2026
  4. added and removed
    area-crossgen2-coreclr
    on Nov 9, 2026
  5. dotnet-policy-service commented on Nov 9, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/crossgen-contrib
    See info in area-owners.md if you want to be subscribed.

  6. added a commit that references this issue on Nov 9, 2026
    e43d4b9
  7. removed
    untriaged
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions