Repository navigation
Description
Description
Claude Opus found this while cranking away on some interop-related code. I've read through this and it makes sense to the best of my knowledge, and I pushed it to find as small of a min-repro as it could. Most of the rest of this bug report are written by Claude. cc @EgorBo looks like he's already on the fix 😂
On .NET 11 (preview 7 and RC1), x64, the JIT can reload a raw pointer as a byref, keep it in a callee-saved register across an unmanaged function-pointer call, and report it as live there. If a GC that another thread started scans this thread while it's inside that call, the stack walk reaches the frame through its InlinedCallFrame, which has no saved location for the register. The GC then faults in PromoteCarefully and the process dies with Fatal error. Internal CLR error. (0x80131506).
The byref comes from the usual COM-binding call pattern, the one TerraFX.Interop.Windows generates: a struct with a vtable pointer and an AggressiveInlining instance method that calls ((delegate* unmanaged<T*, ...>)lpVtbl[n])((T*)Unsafe.AsPointer(ref this), ...). The caller holds a T* and calls p->Method() several times.
We hit this as an intermittent crash in Paint.NET's test harness, which calls COM through raw pointers this way. The repro below crashes every time, within about a second.
Reproduction Steps
Here's a zip with the csproj and Program.cs:
JitGcHoleMinimal.zip
Unzip it and run:
dotnet run -c Release
It employs a COM-style struct whose method passes this as Unsafe.AsPointer(ref this) (mind you, this is on the stack, so this is safe), and a method that calls it several times through a raw pointer:
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
// A COM-style interface in the shape TerraFX generates: `this` is passed as Unsafe.AsPointer(ref this).
unsafe struct IFoo
{
public void** lpVtbl;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Get()
{
return ((delegate* unmanaged<IFoo*, int>)this.lpVtbl[0])((IFoo*)Unsafe.AsPointer(ref this));
}
}
static unsafe class Program
{
static void** vtbl;
static int sink;
[UnmanagedCallersOnly]
static int GetImpl(IFoo* self)
{
return 0;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static IFoo* Create()
{
IFoo* p = (IFoo*)NativeMemory.Alloc((nuint)sizeof(IFoo));
p->lpVtbl = vtbl;
return p;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void Check(string name, bool condition)
{
sink += condition ? name.Length : 0;
}
// The interpolated strings' hoisted pieces use up the callee-saved registers, so `p` lives on the stack. The
// JIT reloads it as a byref for `ref this`, keeps that byref in a callee-saved register across the second call
// (the third call reuses it), and reports it there. A GC from another thread during that call walks this frame
// through its InlinedCallFrame, which has no location for the register, and crashes.
[MethodImpl(MethodImplOptions.NoInlining)]
static void Test(string[] names)
{
foreach (string name in names)
{
IFoo* p = Create();
Check($"[{name}] a", p->Get() == 0);
Check($"[{name}] b", p->Get() == 0);
Check($"[{name}] c", p->Get() == 0);
}
}
static void Main()
{
vtbl = (void**)NativeMemory.Alloc((nuint)sizeof(void*));
vtbl[0] = (delegate* unmanaged<IFoo*, int>)&GetImpl;
// GC pressure from another thread.
Thread thread = new Thread(() =>
{
while (true)
{
GC.KeepAlive(new byte[600_000]);
}
});
thread.IsBackground = true;
thread.Start();
string[] names = new[] { "one", "two" };
for (int i = 0; i < 1_000_000; ++i)
{
Test(names);
}
Console.WriteLine("completed without crashing");
}
}lpVtbl[0] points at an [UnmanagedCallersOnly] method that returns 0. Main calls Test in a loop while a background thread allocates 600 KB arrays to drive GCs.
Expected behavior
The program runs to completion and prints completed without crashing.
Actual behavior
The process crashes within about a second, on every run:
Fatal error.
Internal CLR error. (0x80131506)
at Program+<>c.<Main>b__6_0()
at System.Threading.Thread.StartCallback(System.Threading.Thread*)
The managed stack shown is the allocating thread, which triggered the GC. A crash dump shows the fault is in the GC's stack scan of the main thread:
coreclr!PromoteCarefully+0x3 <- access violation, reading through a null slot pointer
coreclr!GcEnumObject+0x7b
coreclr!TGcInfoDecoder<AMD64GcInfoEncoding>::ReportSlotToGC+0x94
coreclr!TGcInfoDecoder<AMD64GcInfoEncoding>::EnumerateLiveSlots+0x9fd
coreclr!EECodeManager::EnumGcRefs+0x243
coreclr!GcStackCrawlCallBack+0x15d
coreclr!Thread::StackWalkFramesEx+0xc9
coreclr!Thread::StackWalkFrames+0xb6
coreclr!ScanStackRoots+0x8f
coreclr!GCToEEInterface::GcScanRoots+0xa0
coreclr!WKS::gc_heap::mark_phase+0x1d7
coreclr!WKS::gc_heap::gc1+0x58
coreclr!WKS::gc_heap::garbage_collect+0x1a9
At that moment the main thread is inside the second p->Get() call in Test. It's waiting for the GC to finish before it can enter the UnmanagedCallersOnly callee:
coreclr!WKS::GCHeap::WaitUntilGCComplete
coreclr!Thread::RareDisablePreemptiveGC
coreclr!JIT_ReversePInvokeEnterRare2
Program.GetImpl(IFoo*)
Program.Test(System.String[]) <- return address = offset 0x188, right after the second unmanaged call
Program.Main()
The JIT listing for Test (DOTNET_JitDisasm=Test, RC1) shows p stored as a native int, then reloaded as a byref into the callee-saved register r14. r14 is then live across the second unmanaged call and reused for the third:
call [rcx]Program:Create():ptr
mov qword ptr [rbp-0x40], rax ; p spilled as native int
...
mov rdx, bword ptr [rbp-0x40] ; 1st call: reloaded as byref (volatile register)
...
mov r14, bword ptr [rbp-0x40] ; 2nd call: reloaded as byref into callee-saved r14
mov rcx, qword ptr [r14]
mov rax, qword ptr [rcx]
mov rcx, r14
...
mov byte ptr [rsi+0x04], 0 ; InlinedCallFrame: switch to preemptive
G_M000_IG08: ;; offset=0x0186
call rax ; 2nd unmanaged call, r14 live across it
G_M000_IG09: ;; offset=0x0188
mov byte ptr [rsi+0x04], 1
...
mov rcx, qword ptr [r14] ; 3rd call reuses r14
mov rax, qword ptr [rcx]
mov rcx, r14The method header says FullOpts, partially interruptible, x64 + VEX + EVEX.
Regression?
Yes. The same repro, built for each target framework and run five times each:
| Runtime | Byref held in a callee-saved register across the call | Crashes |
|---|---|---|
| 8.0.31 | no | 0/5 |
| 9.0.20 | no | 0/5 |
| 10.0.12 | no | 0/5 |
| 11.0.0-preview.7.26381.103 | yes | 5/5 |
| 11.0.0-rc.1.26425.128 | yes | 5/5 |
The net10.0 build's IL also crashes 5/5 when run on the 11.0 RC1 runtime (dotnet exec --fx-version 11.0.0-rc.1.26425.128). That points at the .NET 11 JIT rather than at differences in the libraries. For comparison, the .NET 10 JIT reloads p as a plain native int before each call (mov rcx, qword ptr [rbp-0x20]). I didn't test .NET 11 previews earlier than preview 7.
Known Workarounds
Calling through the vtable with the raw pointer, instead of the ref this instance method, avoids the byref. Changing each call in Test to
Check($"[{name}] a", ((delegate* unmanaged<IFoo*, int>)p->lpVtbl[0])(p) == 0);ran to completion 5/5 times.
That isn't practical for code that uses generated bindings like TerraFX.Interop.Windows, where every COM call goes through the ref this pattern.
Configuration
- .NET 11.0.0-rc.1.26425.128 (SDK 11.0.100-rc.1.26425.128); also reproduces on 11.0.0-preview.7.26381.103
- Windows 11 Pro 10.0.26200, x64
- Default GC settings (workstation, concurrent); also reproduces with
DOTNET_gcConcurrent=0 - Not tested on ARM64 or on Linux
Other information
What I found while reducing the repro, where removing each ingredient made the crash go away:
- The byref comes from
Unsafe.AsPointer(ref this). The same calls through an explicit function pointer, with noref this, never crash. phas to live on the stack, not in a register. Here the interpolated strings' hoisted pieces take up the callee-saved registers in the loop. Without the loop or the interpolations, the JIT keepspin a register as a native int and the bug doesn't occur.- The byref has to be needed after an unmanaged call. Three calls reproduce, because the reload made for the second call is reused for the third. Two calls with no later use of
pdon't reproduce. Two calls followed byNativeMemory.Free(p)do. - The GC has to come from another thread. In a larger version of this repro, a
GC.Collect()inside theUnmanagedCallersOnlycallee did not crash, presumably because that stack walk unwinds through the callee's own frame and finds where the register was saved.DOTNET_GCStress(0x1, 0x2, 0x3, 0x4, 0x8, 0xC, 0xF on the release runtime) without the allocating thread does not reproduce. - Tiering matters only for code shape. With default tiered compilation,
Testreaches Tier1 with PGO, and that code happens not to keep the byref live across a call. MarkingTestwith[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]reproduces 5/5 with default tiering.
My reading, which I haven't confirmed against the JIT or GC info: when unwinding from an InlinedCallFrame the runtime has no saved locations for callee-saved registers, so the JIT must not keep GC references or byrefs live in callee-saved registers across an unmanaged call that uses that frame. This byref, reloaded from a native-int local and possibly CSE'd, seems to get past that rule.
Activity
dotnet-policy-service commentedon Oct 7, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
Add regression test for dotnet#135374
JIT: Use local's GC type when unspilling a register candidate (#135381)
[release/11.0] JIT: Use local's GC type when unspilling a register ca…