Skip to content

JIT: (bug) NullReferenceException dropped - non-null assertion ignores the peeled offset #133678

Closed

Description

@EgorBo

The JIT knows p != null and concludes that p + (-1) is also non-null, marks the load through it
non-faulting, and deletes it. The NullReferenceException disappears.

Repro

using System;
using System.Runtime.CompilerServices;

public static unsafe class Program
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private static byte Read(ref byte r) => r;

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static void Test(ref byte p)
    {
        if (Unsafe.IsNullRef(ref p))
        {
            return;
        }

        ref byte q = ref Unsafe.Add(ref p, -1);
        Read(ref q); // must fault: q is the null reference
        Read(ref q);
    }

    public static int Main()
    {
        for (int i = 0; i < 3; i++)
        {
            try
            {
                Test(ref Unsafe.AsRef<byte>((void*)1)); // p == 1, so q == p - 1 == null
                Console.WriteLine("NO EXCEPTION  <-- WRONG");
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("NullReferenceException (correct)");
            }
        }

        return 100;
    }
}

Expected

NullReferenceException (correct)
NullReferenceException (correct)
NullReferenceException (correct)

Actual

NO EXCEPTION  <-- WRONG
NO EXCEPTION  <-- WRONG
NO EXCEPTION  <-- WRONG

Notes

  • Windows x64, net11.0, Release. Reproduces with default settings and with
    DOTNET_TieredCompilation=0. Correct under DOTNET_JITMinOpts=1 and with Debug IL.
  • Verified on a Checked build of 97ea9ba17a825a62c1b52a259999566eed748b79; no assert fires.
  • The two Read(ref q) calls matter: they make Roslyn keep q in a local, so the address reaches
    assertion prop as an SSA local whose VN is ADD(pVN, -1). With the ADD inline in the address tree
    the JIT correctly refuses to peel it.
  • Compiler::optAssertionVNIsNonNull (src/coreclr/jit/assertionprop.cpp) computes the peeled
    offset via PeelOffsets but never examines it, unlike ValueNumStore::IsKnownNonNull
    (src/coreclr/jit/valuenum.cpp), which rejects negative and big offsets.

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 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. changed the title [-]JIT: NullReferenceException dropped - non-null assertion ignores the peeled offset[/-] [+]JIT: (bug) NullReferenceException dropped - non-null assertion ignores the peeled offset[/+] on Sep 11, 2026
  5. self-assigned this
    on Sep 11, 2026
  6. jakobbotsch commented on Sep 11, 2026

    @jakobbotsch
    Member

    Not clear to me that this is even a valid program – almost null byrefs are generally not valid within the JIT except when produced temporarily. I remember discussing this with @SingleAccretion previously.

  7. EgorBo commented on Sep 11, 2026

    @EgorBo
    MemberAuthor

    Not clear to me that this is even a valid program – almost null byrefs are generally not valid within the JIT except when produced temporarily. I remember discussing this with @SingleAccretion previously.

    Well, the fix is. We do the same check everywhere else. I can drop the test, thought.

  8. added a commit that references this issue on Sep 14, 2026
    f9b470a
  9. added a commit that references this issue on Sep 18, 2026
    0508ea8
  10. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  11. locked and limited conversation to collaborators on Oct 19, 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