You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit def2794
Browse filesBrowse the repository at this point in the historyBrowse files
[cDAC][wasm] Keep caller SP for non-R2R callers and restore SoftwareExceptionFrame FP (#135494)
Aligns the wasm cDAC R2R unwinder with native `WasmUnwindStackFrameCore`
and `UpdateRegDisplay_Impl`:
- **Non-R2R caller:** keep the caller SP with a null IP and FP instead
of clearing SP. Otherwise an R2R method whose active `InlinedCallFrame`
lives in its own frame is reported repeatedly, because the walker never
gets past that frame.
- **`SoftwareExceptionFrame`:** restore FP along with IP and SP. FP
(`InterpreterFP`) is wasm's only callee-saved register, but the
`CalleeSavedRegisters` descriptor is empty on wasm, so FP was left over
from the previous frame.
- **Inactive `InlinedCallFrame`:** return before writing the
interpreter-frame stash, as native does.
`StackWalk.md` is updated. The earlier reverse P/Invoke check was
dropped after #135440 identified those roots from the tagged frame
identity.
<details><summary>Not included</summary>
- `IStackWalk.GetRuntimeFramePointer` on wasm: native
`GetRegdisplayStackMark` has no `TARGET_WASM` implementation to mirror.
- Leaf FP when a walk starts inside a funclet (#135147), which depends
on #134608.
</details>
> [!NOTE]
> This PR description was generated with GitHub Copilot.
---------
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: cf17d923-d419-4c13-bcb4-094d592f8243
Copy file name to clipboardExpand all lines: docs/design/datacontracts/StackWalk.md
+24-2Lines changed: 24 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -450,7 +450,7 @@ Most of the handlers are implemented in `BaseFrameHandler`. Platform specific co
450
450
451
451
#### InlinedCallFrame
452
452
453
-
InlinedCallFrames store and update only the IP, SP, and FP of a given context. If the stored IP (CallerReturnAddress) is 0 then the InlinedCallFrame does not have an active call and should not update the context.
453
+
InlinedCallFrames store and update only the IP, SP, and FP of a given context. If the stored IP (CallerReturnAddress) is 0 then the InlinedCallFrame does not have an active call and should not update the context. On WASM this includes recording the following `InterpreterFrame` for the interpreter unwind.
454
454
455
455
* On ARM, the InlinedCallFrame stores the value of the SP after the prolog (`SPAfterProlog`) to allow unwinding for functions with stackalloc. When a function uses stackalloc, the CallSiteSP can already have been adjusted. This value should be placed in R9.
456
456
* On WASM, a `CallerReturnAddress` of `INLINED_PINVOKE_FROM_R2R` (`1`) marks an active inlined P/Invoke from ReadyToRun code rather than an address. SP is taken from `CallSiteSP`, IP is the R2R virtual IP of the shadow frame at `CallSiteSP`, and FP is the WASM logical frame pointer at `CallSiteSP` (see below). If no virtual IP can be recovered, IP is set to null.
@@ -463,6 +463,8 @@ An active InlinedCallFrame stays the current Frame after its context update so t
463
463
464
464
SoftwareExceptionFrames store a copy of the context struct. The IP, SP, and all ABI specified (platform specific) callee-saved registers are copied from the stored context to the working context.
465
465
466
+
On WASM the only callee-saved register is the frame pointer (`InterpreterFP`), and the `CalleeSavedRegisters` data descriptor is empty, so the IP, SP and FP are copied explicitly from the stored context (native `SoftwareExceptionFrame::UpdateRegDisplay_Impl`).
467
+
466
468
**Return Address**: Read from the `ReturnAddress` field on the frame.
467
469
468
470
#### TransitionFrame
@@ -488,7 +490,27 @@ A funclet instead stores its establishing-frame pointer plus two, giving the ide
488
490
489
491
**WASM R2R unwinding.** The virtual IP is the identity-derived method base plus twice the physical frame's stored function-local value. `WasmContext.Unwind` passes its current virtual IP as the control PC to `IExecutionManager.TryGetUnwindInfo`, reads the fixed frame size as a uint32 ULEB128 value from `unwindInfoBaseAddress + RuntimeFunction.UnwindData`, and adds that size to the physical frame base. A zero frame size terminates the R2R walk because it would make no progress; malformed uint32 ULEB128 data is invalid.
490
492
491
-
For a managed caller, the resulting stack pointer identifies the caller's R2R frame record; its virtual IP and logical frame pointer are resolved as above. When the current frame has a positive odd reverse-P/Invoke identity, unwinding still preserves the advanced caller SP for the explicit `Frame` chain, but leaves IP and FP null because the caller is native. At a terminator or another non-R2R caller, the R2R context is cleared and walking continues through the explicit `Frame` or interpreter frame chain.
493
+
For a managed caller, the resulting stack pointer identifies the caller's R2R frame record; its virtual IP and logical frame pointer are resolved as above. When the current frame has a positive odd reverse-P/Invoke identity, unwinding still preserves the advanced caller SP for the explicit `Frame` chain, but leaves IP and FP null because the caller is native. When the resulting stack pointer has no R2R frame record (a terminator, an interpreter transition or the stack top), the caller is not R2R code: the advanced SP is kept, IP and FP are null, and walking continues through the explicit `Frame` or interpreter frame chain. The R2R walk is cleared (SP, IP and FP all null) only when the current frame cannot be unwound at all: no R2R frame at SP, no unwind data, or a zero frame size.
494
+
495
+
```csharp
496
+
// WasmContext.Unwind: one R2R frame, mirroring native RtlVirtualUnwind / WasmUnwindStackFrameCore.
497
+
voidUnwind()
498
+
{
499
+
TargetPointerframeBase=/* frame base of the R2R frame at SP */;
IP=callerIsNative?null:/* R2R virtual IP at SP, or null when SP has no R2R frame record */;
511
+
FP=IP!=null?/* WASM logical frame pointer at SP */:null;
512
+
}
513
+
```
492
514
493
515
**Return Address**: Read from `TransitionBlock.ReturnAddress`. On WASM, it is derived from `TransitionBlock.StackPointer` when it is 0, as above. For an x86 `StubDispatchFrame` with neither a method nor a GCRefMap, subtract five bytes to return the adjusted call address, matching the context update.
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/Context/Wasm/WasmUnwinder.cs
+7-10Lines changed: 7 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -155,9 +155,11 @@ public TargetCodePointer GetVirtualIP(TargetPointer sp)
155
155
156
156
/// <summary>
157
157
/// Advances <paramref name="sp"/> by one R2R frame and produces the caller's virtual IP,
158
-
/// mirroring <c>WasmUnwindStackFrameCore</c>. Returns false when the R2R walk terminates
159
-
/// (no R2R frame at <paramref name="sp"/>), in which case <paramref name="sp"/> is set to
160
-
/// <see cref="TargetPointer.Null"/>.
158
+
/// mirroring <c>WasmUnwindStackFrameCore</c>. Returns false when the R2R walk cannot advance
159
+
/// (no R2R frame at <paramref name="sp"/>, missing unwind data, or a zero frame size), in
160
+
/// which case <paramref name="sp"/> is set to <see cref="TargetPointer.Null"/>. Otherwise
161
+
/// <paramref name="sp"/> is the caller's stack pointer, and <paramref name="ip"/> is null when
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/Context/WasmContext.cs
+4-3Lines changed: 4 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -85,9 +85,10 @@ public void Unwind(Target target)
85
85
86
86
internalvoidUnwind(Wasm.WasmUnwinderunwinder)
87
87
{
88
-
// Advance one ReadyToRun frame over the managed linear stack. When the R2R walk
89
-
// terminates (an interpreter transition or the stack top), StackPointer becomes null and
90
-
// the caller falls back to the explicit Frame chain / interpreter frame chain.
88
+
// Advance one ReadyToRun frame over the managed linear stack. When the caller is not R2R
89
+
// code (an interpreter transition, native code above a reverse P/Invoke, or the stack top),
90
+
// StackPointer is the caller's SP and the IP and FP are null; the walker continues through
91
+
// the explicit Frame chain / interpreter frame chain.
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/FrameHandling/WasmFrameHandler.cs
0 commit comments