Skip to content

JIT: Inlining runs an interface static constructor before callvirt's null check #138604

Backlog

Description

@NinoFloris

Description

Inlining an instance method on an interface can run the interface’s static constructor when the receiver is null.

Reproduction Steps

Changing only AggressiveInlining to NoInlining makes the initializer’s side effect disappear.

using System;
using System.Runtime.CompilerServices;

class Program
{
    static bool s_initialized;

    interface I
    {
        static I() => s_initialized = true;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        sealed int Get() => 42;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Invoke(I receiver) => receiver.Get();

    static void Main()
    {
        try { Invoke(null!); }
        catch (NullReferenceException)
        {
            Console.WriteLine($"Interface initializer ran: {s_initialized}");
        }
    }
}

Expected behavior

NRE without init

Actual behavior

NRE with init

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    area-CodeGen-coreclr
    on Dec 1, 2026
  2. dotnet-policy-service commented on Dec 1, 2026

    @dotnet-policy-service
    Contributor

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

  3. added
    untriaged
    on Dec 1, 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

    Labels

    area-CodeGen-coreclruntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions