Skip to content

JIT: Illegal reordering of null check with argument evaluation for GVMs #121711

Closed

Description

@jakobbotsch

The following program should print "Printed!", then throw NRE. However it does not print anything.

using System;
using System.Runtime.CompilerServices;

public class Program
{
    public static void Main()
    {
        Test(null);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void Test(Base b)
    {
        b.Foo<string>(Bar());
    }

    private static int Bar()
    {
        Console.WriteLine("Printed!");
        return 42;
    }
}

public abstract class Base
{
    public abstract void Foo<T>(int x);
}

public class Derived : Base
{
    public override void Foo<T>(int x)
    {
    }
}

The cause is the following code that spills the function pointer after having popped the arguments into the call:

// Now make an indirect call through the function pointer
unsigned lclNum = lvaGrabTemp(true DEBUGARG("VirtualCall through function pointer"));
impStoreToTemp(lclNum, fptr, CHECK_SPILL_ALL);
fptr = gtNewLclvNode(lclNum, TYP_I_IMPL);

This reorders the evaluation of the function pointer with the arguments. Evaluating the function pointer for GVMs results in a null check.

Activity

  1. added
    untriaged
    on Nov 17, 2025
  2. added
    needs-area-label
    on Nov 17, 2025
  3. added
    area-CodeGen-coreclr
    and removed
    needs-area-label
    on Nov 17, 2025
  4. dotnet-policy-service commented on Nov 17, 2025

    @dotnet-policy-service
    Contributor

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

  5. self-assigned this
    on Nov 26, 2025
  6. removed
    untriaged
    on Nov 26, 2025
  7. added this to the 11.0.0 milestone on Nov 26, 2025
  8. added a commit that references this issue on Nov 27, 2025
  9. locked and limited conversation to collaborators on Dec 28, 2025
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