Repository navigation
Description
Description
Suspected x64 JIT GC-reporting hole after Interlocked.Exchange(ref reference, null) in fully interruptible code: the returned object is live in a register but absent from GCInfo until a subsequent instruction/group boundary. A pure-managed reproducer crashes during IDisposable.Dispose dispatch on .NET 8.0.30 and 8.0.31. The reporting gap is also present on 10.0.12, but I have not reproduced a crash on 10.0.12.
This investigation started with two crashes in a Windows machine-vision application during atomic owner removal/disposal. Both application dumps show the same kind of reporting gap. The reproducer below has no application dependencies, camera SDK, unsafe code, or custom native interop; the application dump analysis is supporting context, not a reconstruction of the original GC suspension instant.
Reproduction Steps
Save these two files in an otherwise empty directory. Source is the tested baseline, including its diagnostic strings. The no-call loop in Release makes the method fully interruptible; retain it and the method attributes.
ExchangeGcProbe.csproj and complete Program.cs
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RuntimeFrameworkVersion>8.0.30</RuntimeFrameworkVersion>
<RollForward>Disable</RollForward>
<Nullable>enable</Nullable>
<Optimize>true</Optimize>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
</Project>#nullable enable
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
internal static class Program
{
private static int _sink;
private static int _disposed;
private static bool _stop;
private sealed class Token : IDisposable
{
private readonly long _marker = 0x123456789abcdef;
[MethodImpl(MethodImplOptions.NoInlining)]
public void Dispose()
{
if (_marker != 0x123456789abcdef)
throw new InvalidOperationException("对象标记损坏");
Interlocked.Increment(ref _disposed);
}
}
private sealed class Frame
{
public byte[]? Data;
public IDisposable? Owner;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static Frame Create() => new Frame { Data = new byte[32], Owner = new Token() };
// 无调用循环使方法具备完全可中断区间;不使用原生指针或 SDK。
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static void Release(Frame frame, int spin)
{
frame.Data = null;
IDisposable? owner = Interlocked.Exchange(ref frame.Owner, null);
owner?.Dispose();
int sum = 0;
for (int i = 0; i < spin; i++)
sum += i;
Volatile.Write(ref _sink, sum);
}
private static int Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine($"运行时={Environment.Version}; GCStress={Environment.GetEnvironmentVariable("DOTNET_GCStress")}");
if (args.Length > 0 && args[0] == "inspect")
{
Release(Create(), 100);
Debugger.Break();
return 0;
}
bool race = args.Length > 0 && args[0] == "race";
int iterations = race ? 2_000_000 : 2000;
Thread? collector = null;
if (race)
{
collector = new Thread(() =>
{
while (!Volatile.Read(ref _stop))
{
GC.Collect(0, GCCollectionMode.Forced, blocking: true, compacting: true);
Thread.Yield();
}
}) { IsBackground = true };
collector.Start();
}
try
{
for (int i = 0; i < iterations; i++)
Release(Create(), 100);
}
finally
{
Volatile.Write(ref _stop, true);
collector?.Join();
}
Console.WriteLine($"完成={_disposed}/{iterations}; 校验={_sink}; GC={GC.CollectionCount(0)}/{GC.CollectionCount(1)}/{GC.CollectionCount(2)}");
return _disposed == iterations ? 0 : 1;
}
}Build once, then run the same DLL against each installed x64 runtime. PowerShell, in a fresh session:
dotnet build ./ExchangeGcProbe.csproj -c Release -o ./out
$env:DOTNET_TieredCompilation = '0'
$env:DOTNET_ReadyToRun = '0'
$env:DOTNET_GCStress = '0'
$runtimeVersion = '8.0.30' # repeat with 8.0.31 or 10.0.12
for ($round = 1; $round -le 10; $round++) {
Write-Host "Round $round"
dotnet exec --fx-version $runtimeVersion ./out/ExchangeGcProbe.dll race
if ($LASTEXITCODE -ne 0) { break }
}Ensure the requested runtime is installed (or invoke its extracted dotnet.exe). --fx-version overrides the pinned runtimeconfig; the program prints the actual runtime. This is timing-sensitive and may pass all ten runs. Each run creates/disposes 2 million objects while a second thread repeatedly performs blocking compacting Gen0 collections. Our harness used a 60-second per-process timeout; timeouts are not counted as crashes.
For deterministic code/GCInfo inspection, run the same command with inspect instead of race under WinDbg/CDB. Continue past the initial debugger breakpoint to the program's Debugger.Break(), then:
!sos.name2ee ExchangeGcProbe.dll Program.Release
!sos.u -gcinfo <JITTED-Code-Address-from-name2ee>
!sos.gcinfo <MethodDesc-from-name2ee>
lmvm coreclr
lmvm clrjit
An inspect breakpoint dump is not a crash dump. For crash capture, run race with first-chance AV breaking enabled (sxe av).
Expected behavior
The old Owner remains a valid GC root throughout the interruptible interval after the exchange, including relocation updates, until its last use. Successful runs print 完成=2000000/2000000; 校验=4950 (completed count/checksum) and exit 0.
Actual behavior
| Runtime | Code/GCInfo inspection | Dynamic result |
|---|---|---|
| 8.0.30 | Returned reference missing after xchg |
Native AV without debugger; separately captured first-chance AV under CDB after approximately 20 seconds |
| 8.0.31 | Same gap as 8.0.30 | A 10-run batch passed rounds 1–8, then crashed in round 9; no first-chance dump for that run |
| 10.0.12 | Gap still present, with different subsequent instruction ordering | 10/10 runs passed (20 million disposals); no crash claimed |
The .NET 8 failures exit with -1073741819 / 0xC0000005:
Fatal error. Internal CLR error. (0x80131506)
at System.IDisposable.Dispose()
at Program.Release(Frame, Int32)
at Program.Main(System.String[])
In the 8.0.30 first-chance dump, the native fault is VirtualCallStubManager::ResolveWorker+0x6b, inlined ModuleBase::GetLoaderAllocator, reading address 0x98. The interface receiver recovered from its saved slot is 0x82800028; SOS dumpobj reports Free Object, size 0x1e0. This supports reference invalidation before interface dispatch; it does not record the earlier GC suspension itself.
Relevant decoded offsets (relative to Program.Release, size 0x46 in each version):
8.0.30 / 8.0.31 10.0.12
+0x12 xchg r11,[rcx] +0x12 xchg r11,[rcx]
+0x15 mov rcx,r11 +0x15 test r11,r11
+0x18 test rcx,rcx +0x18 je +0x2a
+0x1a mov rcx,r11
GCInfo (8.0.30 / 8.0.31):
+0x07 interruptible; +rcx
+0x0a +r11
+0x12 -r11 -rcx +rcx(interior)
+0x18 -rcx(interior) +rcx
+0x2a -rcx
+0x40 not interruptible
GCInfo (10.0.12):
+0x07 interruptible; +rcx
+0x0a +r11
+0x12 -r11 -rcx +rcx(interior)
+0x1a -rcx(interior) +r11
+0x1d +rcx
+0x27 -r11
+0x2a -rcx
+0x44 not interruptible
At +0x15, Owner has already been cleared and the old reference exists only in r11, which is not reported. The reported interior rcx roots the Frame, not the removed Token. In 10.0.12 the omission also covers the +0x18 instruction boundary.
Configuration
- Reproducer host: Windows x64, OS version
10.0.26100, 20 logical processors; optimized Release build. Current SDK10.0.400, MSBuild18.9.6. - All comparisons used one
net8.0-windowsDLL (SHA25668FB4CC30F5981F17DB6A399539D35A67AA27AAACD3769775FEC1862E469EE7C), not a net10.0 rebuild. Program output, host trace and debugger module paths verified the selected runtimes. - Runtime/coreclr versions and product commits:
| Runtime | File version | Commit |
|---|---|---|
| 8.0.30 | 8.0.3026.36720 | a83db3e0eb2defb6220e15dae2f1a0462fdbf99f |
| 8.0.31 | 8.0.3126.42015 | 1219a42122cf5190c7f512850557e38c422430dc |
| 10.0.12 | 10.0.1226.42308 | 95017c711e6afc1085133d440e42b4bd78155701 |
8.0.31 used an isolated official runtime ZIP verified against its published SHA512; 8.0.30 and 10.0.12 used existing installations. No other OS/architecture or .NET 11 build was tested. The controlled comparison disables tiering/ReadyToRun as above; it is not a complete default-tiering/PGO matrix. GCStress=C normal-mode tests also passed on 8.0.30 and 10.0.12 (2,000 disposals each), so GCStress passing does not exclude this gap.
Regression?
Unknown first affected version; no known-good baseline established.
Known Workarounds
In the standalone repro, replace the exchange expression with TakeOwner(ref frame.Owner):
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private static IDisposable? TakeOwner(ref IDisposable? owner)
=> Interlocked.Exchange(ref owner, null);On 8.0.30 and 8.0.31, inspection shows continuous return-reference reporting at the caller and a helper-based exchange path. Each version passed 10 independent runs of 2 million disposals (40 million combined), plus 2,000 with GCStress=C. This preserves the atomic exchange; it is a tested repro workaround, not a proof of correctness or an application deployment result. Not validated as a workaround on .NET 10.
Other information
Suspected implementation path, from the exact 10.0.12 product commit:
genLockedInstructionsemits the memory exchange throughemitIns_AR_R, then callsgenProduceReg.emitIns_ARX_RselectsIF_ARW_RRWforxchg; the GCREF output branch accepts that format but does not update the result register's GC liveness at the instruction end. This appears consistent with the decoded gap; maintainer confirmation would be appreciated.
This appears distinct from #131708, which concerns a hijacked return into CallDescrWorkerInternal, rather than an interruptible memory xchg. Raw application dumps are intentionally not included; the standalone source and relevant diagnostic excerpts are above.
Activity
dotnet-policy-service commentedon Sep 14, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: Fix GC reporting for xarch interlocked reference results (dotnet…
JIT: Fix GC reporting for xarch interlocked reference results (dotnet…