Repository navigation
Commit 7c1cb8a
[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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
574 | 574 | | |
575 | 575 | | |
576 | 576 | | |
577 | | - | |
578 | | - | |
579 | | - | |
| 577 | + | |
| 578 | + | |
580 | 579 | | |
581 | 580 | | |
582 | 581 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
786 | 786 | | |
787 | 787 | | |
788 | 788 | | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
789 | 795 | | |
790 | 796 | | |
791 | 797 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3571 | 3571 | | |
3572 | 3572 | | |
3573 | 3573 | | |
3574 | | - | |
3575 | | - | |
3576 | | - | |
3577 | | - | |
3578 | | - | |
| 3574 | + | |
| 3575 | + | |
3579 | 3576 | | |
3580 | 3577 | | |
3581 | 3578 | | |
| |||
0 commit comments