You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
usingSystem;usingSystem.Runtime.CompilerServices;publicstaticunsafeclassProgram{[MethodImpl(MethodImplOptions.AggressiveInlining)]privatestaticbyteRead(refbyter)=>r;[MethodImpl(MethodImplOptions.NoInlining|MethodImplOptions.AggressiveOptimization)]publicstaticvoidTest(refbytep){if(Unsafe.IsNullRef(refp)){return;}refbyteq=refUnsafe.Add(refp,-1);Read(refq);// must fault: q is the null referenceRead(refq);}publicstaticintMain(){for(inti=0;i<3;i++){try{Test(refUnsafe.AsRef<byte>((void*)1));// p == 1, so q == p - 1 == nullConsole.WriteLine("NO EXCEPTION <-- WRONG");}catch(NullReferenceException){Console.WriteLine("NullReferenceException (correct)");}}return100;}}
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.
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.
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.
Activity
dotnet-policy-service commentedon Sep 11, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
[-]JIT: NullReferenceException dropped - non-null assertion ignores the peeled offset[/-][+]JIT: (bug) NullReferenceException dropped - non-null assertion ignores the peeled offset[/+]jakobbotsch commentedon Sep 11, 2026
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.
EgorBo commentedon Sep 11, 2026
Well, the fix is. We do the same check everywhere else. I can drop the test, thought.
JIT: Respect offsets in non-null assertion propagation (#133685)
JIT: Respect offsets in non-null assertion propagation (dotnet#133685)
EgorBo commentedon Sep 18, 2026
Affected SDK versions: