Skip to content

JIT: (bug) constrained GetType() expansion drops the receiver's bounds/null check #133715

Closed

Description

@EgorBo
using System;
using System.Runtime.CompilerServices;

class P
{
    // Roslyn emits: ldarg.0; ldarg.1; readonly. ldelema !!T; constrained. !!T; callvirt object::GetType()
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static Type Get<T>(T[] a, int i) => a[i].GetType();

    static void Main()
    {
        try { Console.WriteLine("oob:  " + Get(new int[1], 5)); }
        catch (IndexOutOfRangeException) { Console.WriteLine("oob:  IndexOutOfRangeException"); }

        try { Console.WriteLine("null: " + Get<int>(null, 0)); }
        catch (NullReferenceException) { Console.WriteLine("null: NullReferenceException"); }
    }
}

Expected: oob: IndexOutOfRangeException / null: NullReferenceException.
Actual: oob: System.Int32 / null: System.Int32 — the array access never happens.

Notes:

  • csc -optimize+, then corerun repro.dll (fails with tiering on and with DOTNET_TieredCompilation=0). Building with -optimize- throws correctly, and the IL for Get<T> is identical in both.
  • src/coreclr/jit/importercalls.cpp:5288-5307: the CORINFO_BOX_THIS Object.GetType expansion calls impPopStack() and never re-attaches the receiver tree or its side effects, unlike the neighbouring IsBoxedValue branch which uses gtTryRemoveBoxUpstreamEffects.

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 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 Sep 14, 2026
    251ea39
  5. added a commit that references this issue on Sep 18, 2026
    9440cf4
  6. 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
  7. 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