Skip to content

JIT: (bug) big-offset field load in a call argument is hoisted above the null check of an earlier argument (AV instead of NRE) #134473

Closed

Description

@EgorBo

A big-offset field load in a later call argument gets moved before the implicit null check done by an earlier argument. The process crashes with a fatal AccessViolationException when it should throw a NullReferenceException.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

[InlineArray(100000)] struct Buf { byte _b; }
struct Big { public Buf Pad; public int Val; }
class C { public int Small; public Big B; }

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Foo(int a, int b) => a + b;

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(C o, int x) => Foo(o.Small, o.B.Val * 3 + x);

    static void Main()
    {
        Console.WriteLine(Test(new C(), 1));
        try
        {
            Test(null, 1);
            Console.WriteLine("no exception");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("NRE");
        }
    }
}

Expected

1
NRE

Actual

1
Fatal error.
       mov      eax, dword ptr [rcx+0x000186AC]   ; o.B.Val, no null check
       lea      eax, [rax+2*rax]
       add      edx, eax
       mov      ecx, dword ptr [rcx+0x08]         ; o.Small (the only remaining null check)
       call     [Program:Foo(int,int):int]

Regression?

Not a regression. Reproduces on .NET 8, 9, 10, 11 RC2 and main (win-x64).

Notes

While morphing the args, local assertion prop uses the o != null assertion from arg0 (o.Small) to remove the explicit NULLCHECK of the big-offset access in arg1. arg1 then has no GTF_EXCEPT, so ArgsComplete doesn't force temps, and SortArgs moves arg1 before arg0. The assertion only holds after arg0 has been evaluated.
This is related to #133524, but it's a separate bug: once the NULLCHECK is gone, arg1 no longer carries GTF_ORDER_SIDEEFF.

Activity

  1. added this to the 12.0.0 milestone on Sep 22, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 22, 2026
  3. dotnet-policy-service commented on Sep 22, 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 Nov 11, 2026
    af49c2c
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