Skip to content

JIT: (bug) R2R/NativeAOT skip covariant array store check for different instantiations of a sealed generic class #134889

Closed

Description

@EgorBo

In R2R/NativeAOT shared generic code, the JIT drops the covariant array store check when storing G<U> into a G<T>[]. This lets a G<object> be stored into a G<string>[], which breaks type safety.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public sealed class G<T> { }

public class Program
{
    public static void Main()
    {
        G<string>[] arr = new G<string>[1];
        try
        {
            Test<string, object>(arr, new G<object>());
            Console.WriteLine("No exception: " + arr[0].GetType());
        }
        catch (ArrayTypeMismatchException)
        {
            Console.WriteLine("ArrayTypeMismatchException");
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Test<T, U>(G<T>[] arr, G<U> val)
    {
        object[] o = arr;
        o[0] = val;
    }
}

Compile with ReadyToRun or NativeAOT.

Expected

ArrayTypeMismatchException

Actual

No exception: G`1[System.Object]

Codegen for Test[__Canon,__Canon] calls CORINFO_HELP_ASSIGN_REF instead of CORINFO_HELP_ARRADDR_ST. Plain JIT (DOTNET_ReadyToRun=0) is correct.

Regression?

Yes, regression in .NET 10. .NET 8.0 and 9.0 are correct. .NET 10.0, 11.0-rc2 and main are wrong (both R2R and NativeAOT).

Notes

gtCanSkipCovariantStoreCheck treats isExactType(G<__Canon>) && valueHandle == arrayElementHandle as proof of type safety. In shared code both handles are G<__Canon>, but the runtime types differ. crossgen2/ILC report sealed canonical types as exact.

Activity

  1. added this to the 12.0.0 milestone on Sep 29, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 29, 2026
  3. dotnet-policy-service commented on Sep 29, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added a commit that references this issue on Oct 1, 2026
    55ed719
  5. locked and limited conversation to collaborators on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions