Repository navigation
Description
Description
On wasm with R2R, calling a (virtual or non-virtual) instance method that returns a
struct via an intermediate instance method appears to corrupt this (or the field
load) and ends up dispatching on a null/garbage MethodTable.
The original failing test is JIT/opt/OSR/synchronized, which manifests as an
object.cpp:548 pMT->Validate() assert during VirtualDispatchHelpers::ResolveVirtualFunctionPointer:
ASSERT FAILED
Expression: !CREATE_CHECK_STRING(pMT && pMT->Validate())
Location: src/coreclr/vm/object.cpp:548
0) System.Runtime.CompilerServices.VirtualDispatchHelpers::ResolveVirtualFunctionPointer
1) System.Runtime.CompilerServices.VirtualDispatchHelpers::VirtualFunctionPointerSlow
2) System.Runtime.CompilerServices.VirtualDispatchHelpers::VirtualFunctionPointer
3) System.Environment::CallEntryPoint
A minimized variant produces a clean managed exception that points at the same root
cause (helper sees a null object → MT validation fails):
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Runtime.CompilerServices.RuntimeHelpers.GetMethodTable(Object obj)
at X.G()
at X.TestEntryPoint()
at __GeneratedMainWrapper.Main()
Minimal Repro
using System.Runtime.CompilerServices;
using Xunit;
struct S { public long y; public int x; }
class Z { public virtual S F() => new S { x = 100, y = -1 }; }
public class X
{
Z z;
[MethodImpl(MethodImplOptions.NoInlining)]
internal S G() => z.F();
[Fact]
public static int TestEntryPoint()
{
var x = new X();
x.z = new Z();
return x.G().x; // expected 100
}
}Bisection of contributing factors
Variants of JIT/opt/OSR/synchronized.cs were run as wasm R2R; the failing
ingredient is the struct-returning instance method called through an intermediate
non-inlined method on a class field, independent of Synchronized and independent
of looping:
| Variant | Method G body |
Z.F return | Repros? |
|---|---|---|---|
| A (baseline) | Synchronized + for { z.F() } loop |
struct S |
yes — pMT->Validate assert |
| B | Drop Synchronized, keep loop |
struct S |
yes — same assert |
| C | Sealed Z, direct (non-virtual) call |
struct S |
no |
| D | G() => z.F() (no loop) |
int |
no |
| E/H | for { z.F() } loop |
struct S |
yes — NRE on GetMethodTable(null) |
| F/G | for { sum += z.F() } loop |
int |
no |
| I | G() => z.F(); (no loop) |
struct S |
yes — same NRE |
| J | G() => z.F(); (sealed Z, non-virtual) |
struct S |
yes — same NRE |
| K | Drop G, inline at call site |
struct S |
inconclusive — different cctor assert (gchelpers.cpp:573 HasEmptySyncBlockInfo) masks |
| L | Variant I, but z rooted in a static |
struct S |
yes — same NRE (so not a GC reclaim of z) |
Common ingredient in all repros: an instance method G() on a class with a field of
reference type Z, where G returns a struct produced by calling z.F(). Variant L
confirms the issue is not GC reclaiming z — it persists even when z is rooted by a
static field.
Workaround
DOTNET_ReadyToRunExcludeList=synchronized (or the minimal-repro assembly name) makes
the test pass. Disabling R2R entirely (DOTNET_ReadyToRun=0) also works.
Notes / Suspected scope
- Probably an R2R codegen issue around struct returns + a hidden retbuf argument
interacting with thethisarg, exposed whenz.F()is invoked through an
intermediate method. The MethodTable that reaches the dispatch helper is null or
garbage. - The assert location matches [wasm][coreclr] !CREATE_CHECK_STRING(pMT && pMT->Validate()) assert failures #121107 but the failing call path is different (that
issue was about an unhandledPlatformNotSupportedExceptionrethrow path). - Possibly an interaction with [Wasm R2R] Bypass dynamic LDVIRTFTN helper #129075 which routed wasm R2R
LDVIRTFTNcalls through
CORINFO_HELP_VIRTUAL_FUNC_PTR, but variant J (non-virtualZ) repros without that
helper being involved — so the issue is broader than the virtual-dispatch helper.
cc @AndyAyersMS @davidwrighton @janvorli
Note
This issue was drafted with assistance from GitHub Copilot.
Activity
dotnet-policy-service commentedon Jun 12, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Jun 12, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
AndyAyersMS commentedon Jun 12, 2026
Note
AI-generated by GitHub Copilot.
Triage of a wider wasm R2R
.cmdsweep surfaced 4 additional repros for what appears to be the same root cause (wasm struct return via GDV / hidden retbuf / type-context arg in wrong slot — the pattern explicitly described in the test comment onstructreturningstruct.cslinking to #52975):JIT/opt/Devirtualization/structreturningstructResult=100but returns12345(also reproduces withDOTNET_ReadyToRun=0— wasm JIT, not just R2R)JIT/Regression/JitBlue/Runtime_53549DecimalpMT->Validate()assert atmethodtable.cpp:8626inVirtualDispatchHelpers::ResolveVirtualFunctionPointerJIT/Regression/JitBlue/Runtime_53549_1Decimalvia derived classProcess terminated. hiProd + 1 < Buf24.LengthinDecimal.DecCalc.DecAddSub(with duplicated stack frames)JIT/Regression/JitBlue/Runtime_65694_2StructWithObjby value acrossGC.CollectpMT->Validate()assert duringSystem.GC._CollectP/InvokeAll four bypass with
DOTNET_ReadyToRunExcludeList=<testname>(andstructreturningstructalso bypasses withDOTNET_ReadyToRun=0). None of the tests usetry/catch/finally, so this is not EH-related.cc @AndyAyersMS
AndyAyersMS commentedon Jun 12, 2026
Note
AI-generated by GitHub Copilot.
One more wasm R2R repro from the same sweep that appears to belong to this family (struct-by-value / GC pointer integrity across helper transitions):
JIT/opt/OSR/Runtime_69032ConditionalWeakTable.Addin a loop withGC.Collect()every 1000 iterations100(no exception thrown, wrapper'sAssert.Equaldoes not fire), yet the R2R'd__GeneratedMainWrapper.Mainexits 0 instead of 100. Bypasses withDOTNET_ReadyToRunExcludeList=Runtime_69032. Notry/catch/finallyin the test.Same shape as
Runtime_65694_2(GC.Collect P/Invoke transition with a struct-by-value flowing across).AndyAyersMS commentedon Jun 12, 2026
Possibly some disagreement somewhere about the order of hidden parameters.
AndyAyersMS commentedon Jun 12, 2026
Root cause:
WasmInterpreterToR2RThunkNodearg-order swapWhen an interpreter-only method (e.g. a runtime-synthesized
Synchronizedwrapper, or any IL-stub) calls a R2R-compiled instance method that returns a
struct, the resulting
call_indirectswapsthisand the hidden retbufpointer. The R2R callee then reads the retbuf bytes as
this, dispatchesGetMethodTableon a stack address, and assertspMT->Validate().Three sources, one disagreement
0=sp, 1=this, 2=retbuf, …, last=pepimporter.cpp:888,morph.cpp:1797WasmR2RToInterpreterThunkNode(R2R→Interp local layout)0=sp, 1=this, 2=retbuf, …WasmR2RToInterpreterThunkNode.cs:162-178, line 288retBufLocalIndex = 1 + (hasThis ? 1 : 0)WasmInterpreterToR2RThunkNode(Interp→R2R push order)0=sp, 1=retbuf, 2=this, …❌WasmInterpreterToR2RThunkNode.cs:142-172(swapped)The thunk's own comment at the time read
Target R2R wasm params: (, [retbuf], [this], …), which is the oppositeof what the JIT and the sister thunk node use.
Why the original
synchronizedtest triggers itX.G()has both[MethodImpl(Synchronized)]and a struct return. The runtimesynthesizes a
Synchronizedwrapper (interp-only) that calls the underlyingR2R-compiled
G. That call is an Interp→R2R call with bothhasThis=trueand
hasRetBuffArg=true— exactly the conditions where the swap occurs.Fix
Swap the two
ifblocks inEmitCodesothisis pushed beforeretbuf(matching the JIT/R2RToInterp convention). Committed as
5ac582f6fc7 Wasm R2R: push this before retbuf in InterpToR2R thunk args.Verification
synchronizedhits thepMT->Validate()assert reliably.SUCCESS(result == 100reachedinside
TestEntryPoint), confirming the struct value now flows back from theR2R-compiled
Gthrough the wrapper to the caller unchanged.bulk sweep — also improve with this single fix:
JIT/opt/Devirtualization/structreturningstructnow exits 100(previously returned 12345 with
DOTNET_ReadyToRun=1).JIT/Regression/JitBlue/Runtime_53549no longer asserts inResolveVirtualFunctionPointer(was the samepMT->Validatesymptom).Note
This investigation and fix were produced with assistance from GitHub Copilot.
Wasm R2R: push this before retbuf in InterpToR2R thunk args (#129356)
Wasm R2R: push this before retbuf in InterpToR2R thunk args (#129356)
Wasm R2R: push this before retbuf in InterpToR2R thunk args (dotnet#1…