Skip to content

Commit 7c1cb8a

Browse files
lewingjkotasCopilot
authored
[wasm] Make the finalizer slot callable from R2R before returning it (#130841)
## Summary On `FEATURE_PORTABLE_ENTRYPOINTS` (wasm) targets, running finalizers under ReadyToRun traps with `RuntimeError: null function or function signature mismatch` in `System.GC.RunFinalizers`. `RunFinalizers` invokes each object's `Finalize` via a typed `call_indirect` on the finalizer method's PortableEntryPoint (PEP). `GCInterface_GetNextFinalizableObject` returned the raw `GetRestoredSlot` result, which for a not-yet-prepared method is a **cold** PEP (`_pActualCode == 0`, prefers the interpreter). The `call_indirect` therefore targets wasm table slot 0 (the reserved null slot) and traps. This is the finalizer manifestation of the cold-first-call R2R trap family in #130634. Fixes #130839. ## Root cause Per the documented invariant (`method.cpp:2947-2954`, `docs/design/coreclr/botr/clr-abi.md:865-871`), any slot fetched directly and then invoked via an R2R `call_indirect` must first be made callable — its PEP must resolve to real code (native R2R code or a correctly-typed interpreter thunk) rather than the temporary precode. Every other such call site already does this: - `GetMultiCallableAddrOfCode` - cctor slot (`methodtable.cpp:3579`) - managed main entry (`assembly.cpp:1164`) - default ctor (`callhelpers.cpp:580`) - P/Invoke (`dllimport.cpp:5919`) - `ExternalMethodFixupWorker` (`prestub.cpp:3074`) - EnC (`method.cpp:3462`) The finalizer QCall is the one missing site. ## Fix One line, mirroring the class-constructor slot handling in `MethodTable::CallClassConstructor` (`methodtable.cpp:3579`): ```cpp funcPtr = pMT->GetRestoredSlot(g_pObjectFinalizerMD->GetSlot()); #ifdef FEATURE_PORTABLE_ENTRYPOINTS MethodDesc::EnsurePortableEntryPointIsCallableFromR2R(funcPtr); #endif ``` `EnsurePortableEntryPointIsCallableFromR2R` wires the R2R→interpreter thunk for the finalizer signature into `_pActualCode` (or leaves already-published native code in place), so the `call_indirect` resolves to real code instead of table slot 0. This is `WRAPPER_NO_CONTRACT` / load-safe (no GC transition), so no extra GC bookkeeping is needed — same as the cctor call site. ## Validation The bug only *reproduces* with the (not-yet-merged) R2R-on-wasm bring-up stack, so there is no wasm-R2R CI leg to exercise it on `main` yet. Validated on the R2R-on-wasm prototype, controlling for corerun identity (md5) and harness confounds, across **both R2R modes**: | test | R2R-corelib (interpreted finalizer) | R2R-full (crossgen'd finalizer) | interpreter | |------|:---:|:---:|:---:| | `b12795` | crash → **pass** | crash → **pass** | pass | | `b38269` | crash → **pass** | crash → **pass** | pass | A same-build R2R-corelib sweep of the JIT/Regression suite (fixed vs. no-fix corerun, with only this change varying) corroborates: the finalizer/GC tests flip crash→pass and every other test produces an identical result with and without the change — i.e. no regressions. The change is a correct addition to the documented call-site-makes-callable pattern and applies to `main` as-is. ## Notes for reviewers - Draft because it cannot be CI-validated on `main` until the R2R-on-wasm consumption path lands. - Related: #130634 (canonical cold-first-call family); #130839 (finalizer issue). > [!NOTE] > This pull request was authored with the assistance of GitHub Copilot. --------- Co-authored-by: Jan Kotas <[email protected]> Co-authored-by: Copilot App <[email protected]>
1 parent 15c8879 commit 7c1cb8a

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

‎src/coreclr/vm/callhelpers.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,8 @@ void CallDefaultConstructor(OBJECTREF ref)
574574

575575
PCODE ctorCode = pMD->GetSingleCallableAddrOfCode();
576576
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
577-
// CallDefaultConstructor invokes the ctor via a typed call_indirect, so its portable
578-
// entry point must resolve to real code (native R2R or a correctly-typed interpreter
579-
// thunk) rather than a temporary precode.
577+
// CallDefaultConstructor invokes the ctor via the function pointer, so its portable entrypoint
578+
// must resolve to real code if possible.
580579
MethodDesc::EnsurePortableEntryPointIsCallableFromR2R(ctorCode);
581580
#endif // FEATURE_PORTABLE_ENTRYPOINTS
582581

‎src/coreclr/vm/comutilnative.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,12 @@ extern "C" void* QCALLTYPE GCInterface_GetNextFinalizableObject(QCall::ObjectHan
786786
MethodTable* pMT = target->GetMethodTable();
787787

788788
funcPtr = pMT->GetRestoredSlot(g_pObjectFinalizerMD->GetSlot());
789+
790+
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
791+
// RunFinalizers invokes the finalizer via the function pointer, so its portable entrypoint must
792+
// resolve to real code if possible.
793+
MethodDesc::EnsurePortableEntryPointIsCallableFromR2R(funcPtr);
794+
#endif // FEATURE_PORTABLE_ENTRYPOINTS
789795
}
790796

791797
END_QCALL;

‎src/coreclr/vm/methodtable.cpp‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,11 +3571,8 @@ BOOL MethodTable::RunClassInitEx(OBJECTREF *pThrowable)
35713571
MethodTable* instantiatingArg = pCanonMT->IsSharedByGenericInstantiations() ? this : nullptr;
35723572

35733573
#ifdef FEATURE_PORTABLE_ENTRYPOINTS
3574-
// On portable-entrypoint (wasm) targets, CallClassConstructor invokes the cctor via a
3575-
// typed call_indirect, so the slot's portable entry point must resolve to real code
3576-
// (native R2R code or a correctly-typed interpreter thunk) rather than the temporary
3577-
// precode. The normal R2R method-entry fixups perform this for direct calls, but the
3578-
// cctor slot is fetched directly here and so must be made callable explicitly.
3574+
// CallClassConstructor invokes the cctor via the function pointer, so its portable entrypoint
3575+
// must resolve to real code if possible.
35793576
MethodDesc::EnsurePortableEntryPointIsCallableFromR2R(pCctorCode);
35803577
#endif // FEATURE_PORTABLE_ENTRYPOINTS
35813578

0 commit comments

Comments
 (0)