Skip to content

JIT: (bug) MD-array Set elides the array covariance check when the element type is itself an array #133975

Closed

Description

@EgorBo

impArrayAccessIntrinsic treats a sealed reference element type as proof that an MD-array Set needs no covariance check. Array element types are sealed but covariant, so an inline Set can store an incompatible array reference into a covariant MD array.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Test(object[,][] a, object[] v) => a[0, 0] = v;

    static void Main()
    {
        string[,][] s = new string[1, 1][];
        try
        {
            Test(s, new object[] { 42 });
            string bad = s[0, 0][0];
            Console.WriteLine("no exception; bad.Length = " + bad.Length);
        }
        catch (ArrayTypeMismatchException)
        {
            Console.WriteLine("ArrayTypeMismatchException");
        }
    }
}

Requires DOTNET_TieredCompilation=0.

Expected

ArrayTypeMismatchException

Actual

no exception; bad.Length = 42

The string reference actually points at a boxed int, so reading bad.Length reads the boxed payload as a string length.

Notes

impArrayAccessIntrinsic in src\coreclr\jit\importercalls.cpp uses CORINFO_FLG_FINAL on actualElemClsHnd to prove no runtime covariance check is needed.
Array types satisfy CORINFO_FLG_FINAL but are still covariant, so the inline expansion emits only a range check and write barrier instead of the covariance-checking Set/ARRADDR_ST path.
With DOTNET_JITMinOpts=1, the intrinsic is rejected and the real Set stub correctly throws ArrayTypeMismatchException.
Reproduces on released .NET 10.0.12 as well as main Checked.

Activity

  1. added this to the 12.0.0 milestone on Sep 15, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 15, 2026
  3. dotnet-policy-service commented on Sep 15, 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. changed the title [-]JIT (bug): MD-array `Set` elides the array covariance check when the element type is itself an array (type-safety hole)[/-] [+]JIT (bug): MD-array `Set` elides the array covariance check when the element type is itself an array[/+] on Sep 15, 2026
  5. self-assigned this
    on Sep 16, 2026
  6. changed the title [-]JIT (bug): MD-array `Set` elides the array covariance check when the element type is itself an array[/-] [+]JIT: (bug) MD-array `Set` elides the array covariance check when the element type is itself an array[/+] on Sep 16, 2026
  7. added a commit that references this issue on Sep 18, 2026
    498a04c
  8. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  9. locked and limited conversation to collaborators on Oct 19, 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