Repository navigation
Description
JIT Tier1 incorrectly merges distinct shared-generic interface dispatch cells under Synthesized PGO
Description
When the Tier1 JIT compiles a method containing multiple callvirt sites to different instantiations of the same generic interface (e.g., IFoo<byte[]>.Bar() and IFoo<string>.Bar()), and both call sites go through shared-generic dispatch (IFoo<System.__Canon>), the optimizer can merge them into a single dispatch cell if they become structurally identical after inlining. The shared cell then resolves monomorphically to the first implementation rather than dispatching polymorphically per call site.
This causes silent data corruption at runtime: one type's implementation is called with another type's arguments.
Reproduction
Prerequisites: Redis server running on localhost:6379
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CacheManager.Core" Version="2.0.0" />
<PackageReference Include="CacheManager.Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
<PackageReference Include="CacheManager.Serialization.Json" Version="2.0.0" />
<PackageReference Include="CacheManager.StackExchange.Redis" Version="2.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.8.58" />
</ItemGroup>
</Project>
using CacheManager.Core; using StackExchange.Redis;var mux = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true");
var cache = CacheFactory.Build<object>(s => s
.WithGzJsonSerializer()
.WithMicrosoftMemoryCacheHandle("l1")
.And
.WithRedisConfiguration("repro", mux)
.WithRedisCacheHandle("repro"));const string region = "JitBugRepro";
cache.ClearRegion(region);// 1. Store objects and one string
for (int i = 0; i < 50; i++)
cache.Put($"obj-{i}", new { Id = i, Name = $"Object {i}" }, region);
cache.Put("target-string", "Hello, World!", region);// 2. Read objects heavily from L2 (forces Tier1 compilation via the JSON fallback path)
var handles = cache.CacheHandles.ToArray();
for (int round = 0; round < 100; round++)
{
handles[0].ClearRegion(region); // evict L1 → force L2 reads
for (int i = 0; i < 50; i++)
cache.Get($"obj-{i}", region);
}// 3. Read the string from L2
handles[0].ClearRegion(region); // evict L1 → force L2 read
var result = cache.Get("target-string", region);// Expected: result is string "Hello, World!"
// Actual: result is byte[] containing UTF-8 bytes of "Hello, World!"
Console.WriteLine($"Type: {result?.GetType().FullName}");
Console.WriteLine($"Is string: {result is string}");
Console.WriteLine($"Is byte[]: {result is byte[]}");if (result is byte[] bytes)
{
Console.WriteLine($"BUG! Got byte[{bytes.Length}] instead of string");
Console.WriteLine($"Content: {System.Text.Encoding.UTF8.GetString(bytes)}");
Environment.ExitCode = 1;
}
else
{
Console.WriteLine($"OK: "{result}"");
}
cache.ClearRegion(region);
cache.Dispose();
mux.Dispose();
Expected behavior
Type: System.String
Is string: True
Is byte[]: False
OK: "Hello, World!"
Actual behavior (.NET 10)
Type: System.Byte[]
Is string: False
Is byte[]: True
BUG! Got byte[13] instead of string
Content: Hello, World!
Environment
- .NET SDK 10.0.101 (commit fad253f51b)
- Runtime: Microsoft.NETCore.App 10.0.1
- OS: Windows 11 (10.0.26200), x64
- 100% reproduction rate on all machines tested
.NET 8 (same code, same packages): 0% reproduction — always returns string correctly.
Root Cause Analysis
The affected method
CacheManager.Redis.RedisValueConverter implements explicit interface IRedisValueConverter<object>.FromRedisValue(). This 357-byte IL method:
- Resolves a type name string to a
Typevia a type cache - Compares against 13 known types using
Type.op_Equality - Dispatches to the appropriate typed interface:
((IRedisValueConverter<byte[]>)this).FromRedisValue(...)or((IRedisValueConverter<string>)this).FromRedisValue(...), etc.
Since the cache is instantiated as ICacheManager<object>, the call goes through the shared-generic instantiation. The callvirt instructions in IL target IRedisValueConverter<byte[]> and IRedisValueConverter<string> respectively, but at the Canon level both become `IRedisValueConverter<System.Canon>.FromRedisValue`.
.NET 8 Tier1 — correct (Dynamic PGO, 2 inlinees)
Each reference-type branch gets its own dispatch cell:
; byte[] branch → G_M000_IG08, uses cell at 0x7FF89B9B2050 G_M000_IG08: mov rcx, rdi mov r8, rsi mov rdx, rbx mov r11, 0x7FF89B9B2050 ; ← Cell A cmp dword ptr [rcx], ecx ... tail.jmp [r11]CacheManager.Redis.IRedisValueConverter`1[System.__Canon]:FromRedisValue(...)
; string branch → G_M000_IG11, uses cell at 0x7FF89B9B2040
G_M000_IG11:
mov rcx, rdi
mov r8, rsi
mov rdx, rbx
mov r11, 0x7FF89B9B2040 ; ← Cell B (different!)
cmp dword ptr [rcx], ecx
...
tail.jmp [r11]CacheManager.Redis.IRedisValueConverter`1[System.__Canon]:FromRedisValue(...)
Each cell resolves independently at runtime to the correct implementation. ✓
.NET 10 Tier1 — BUG (Synthesized PGO, 29 + 80 inlinees)
Both reference-type branches jump to the same merged target with one shared cell:
G_M000_IG02: ... mov rcx, 0x210CA0427C8 ; _byteArrayType pointer cmp rax, rcx je G_M000_IG70 ; ← byte[] jumps to IG70 mov rcx, 0x210CA042ED0 cmp rax, rcx je G_M000_IG71 mov rcx, 0x210CA040020 ; _stringType pointer cmp rax, rcx je G_M000_IG70 ; ← string ALSO jumps to IG70! ...
; MERGED target — ONE dispatch cell for both types
G_M000_IG70:
mov rcx, rdi
mov r8, rsi
mov rdx, rbx
mov r11, 0x7FF91A222278 ; ← ONE cell for both!
call [r11]CacheManager.Redis.IRedisValueConverter`1[System.__Canon]:FromRedisValue(...)
mov r14, rax
jmp SHORT G_M000_IG67
The single cell resolves monomorphically to the byte[] implementation (first in metadata order). When the string path takes this branch, it dispatches to the byte[] converter, which interprets the RedisValue as raw bytes instead of a string. ✗
Why the merge happens
Under Synthesized PGO with aggressive inlining (29+ methods inlined, including Type.op_Equality):
Type.op_Equalitybecomes a direct pointer comparison (cmp rax, <constant>)- Both the byte[] and string branches reduce to the same shape:
compare → dispatch via __Canon - The JIT identifies these as structurally identical blocks and merges them
- The merged block has a single dispatch cell address
The key error: these call sites are semantically distinct even though they are structurally identical. Each was originally a separate callvirt targeting a different generic instantiation, and each requires its own dispatch cell to resolve to the correct implementation at runtime.
Environment variable isolation
| Variable | Result | Notes |
|---|---|---|
| DOTNET_JitMinOpts=1 | ✅ Fixed | No Tier1 optimization → no merge |
| DOTNET_JitNoInline=1 | ✅ Fixed | Without inlining, branches differ structurally |
| [NoOptimization] on method (IL patch) | ✅ Fixed | Per-method Tier1 opt suppression → no merge |
| DOTNET_ReadyToRun=0 | ❌ Bug persists | Same Synthesized PGO + merge without R2R |
| DOTNET_TieredPGO=0 | ❌ Bug persists | |
| DOTNET_TieredCompilation=0 | ❌ Bug persists | |
| DOTNET_JitDisableTailMerge=1 | ❌ Bug persists | Merge occurs via another pass |
| DOTNET_JitDisableGDV=1 | ❌ Bug persists | |
| DOTNET_JitDisableCSE=1 | ❌ Bug persists |
This confirms the bug is isolated to Tier1 optimization of this specific method. [MethodImpl(MethodImplOptions.NoOptimization)] on the source would be a targeted fix, but requires source access to the CacheManager library.
General workarounds (global, high performance impact)
DOTNET_JitMinOpts=1— disables Tier1 optimization (severe performance impact, not production-viable)DOTNET_JitNoInline=1— disables inlining (severe performance impact, not production-viable)
Category
- Area: JIT, Shared Generics, Tier1 Optimization
- Keywords: Synthesized PGO, block merge, dispatch cell, __Canon, interface dispatch, shared generic
- Regression from: .NET 8 → .NET 10
Activity
dotnet-policy-service commentedon May 27, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
AndyAyersMS commentedon May 27, 2026
This is a bug in tail merging, specifically in
GenTree::Compare: it does not compare the stub addresses for calls.Tail merging can be disabled via
DOTNET_JitEnableHeadTailMerge=0and this fixes the issue.JIT: Compare stub addresses in GenTreeCall::Equals
JIT: Fix bug in GenTree::Equals (#128654)
[Release/10.0] JIT: Fix bug in GenTree::Equals (#128654) (#128724)
JIT: Fix bug in GenTree::Equals (#128654)