Skip to content

JIT: (bug) comparing GetType() of two references drops the null check on a null reference #133559

Description

@EgorBo

Repro

using System;
using System.Runtime.CompilerServices;

internal static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    private static int Test(string a, string b)
    {
        if (a.GetType() != b.GetType())
            return 2;

        return 3;
    }

    private static void Main()
    {
        try
        {
            Console.WriteLine($"returned {Test(null, "text")}");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("NullReferenceException");
        }
    }
}

Expected

NullReferenceException

Actual

returned 3

a is null, so a.GetType() must throw, but the whole method body is optimized away to
return 3.

Platform

Windows x64, local Checked build of dotnet/runtime main (ef97eb5).

Activity

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-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions