Skip to content

JIT: (bug) [Tier0] ArgumentNullException.ThrowIfNull(r.Ref) on a null byref no longer throws NullReferenceException #135822

Closed

Description

@EgorBo

In Tier0 a struct box is a CORINFO_HELP_BOX(cls, addr) call; the "Case 1b" path in impThrowIfNull keeps only the evaluation of addr and drops the read through it that the helper would do, so a null byref is never dereferenced.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public struct S { public int A; public int B; }

public ref struct R { public ref S Ref; }

public static class Program
{
    public static int Main()
    {
        try
        {
            Test(default);
            Console.WriteLine("no exception");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("NullReferenceException");
        }
        return 100;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Test(R r) => ArgumentNullException.ThrowIfNull(r.Ref, "r");
}

(CA2264 warning is expected.)

Expected

NullReferenceException (reading the null byref r.Ref must fault).

Actual

no exception

Tier0 only spills the byref to a "boxedAddr spilled" temp and never reads through it. With DOTNET_TieredCompilation=0 (FullOpts) it correctly prints NullReferenceException (cmp byte ptr [rcx], cl is kept).

Regression?

Default tiering, x64:

  • .NET 8.0.x / 9.0.x / 10.0.13: NullReferenceException (not affected)
  • main (Checked): no exception - regression on main ("Case 1b" is newer than the .NET 10 runtime tested).

Notes

  • Compiler::impThrowIfNull, "Case 1b" (value->IsHelperCall(CORINFO_HELP_BOX)): spills boxHelperAddrArg and returns gtNewNothingNode(), assuming the helper has no effect beyond computing its address argument.
  • The helper also reads addr to copy the struct, which can fault; Case 1 (GT_BOX) preserves this via gtTryRemoveBoxUpstreamEffects.
  • Fix: keep a null check/read of addr (e.g. GT_NULLCHECK) in Case 1b.

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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 Oct 20, 2026
    1a7c0b4
  5. locked and limited conversation to collaborators on Nov 20, 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