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 716a871
Browse filesBrowse the repository at this point in the historyBrowse files
[cDAC][wasm] Continue stack walks from a TransitionFrame into its R2R caller (#135140)
On WASM, a cDAC `IStackWalk` walk drops every R2R frame between an
`InterpreterFrame` and the next explicit Frame when R2R code entered the
interpreter through a transition helper, such as the
portable-entry-point thunk `WasmR2RToInterpreterThunk` →
`ExecuteInterpretedMethodWithArgs_PortableEntryPoint`. The walk covers
the interpreted chain, reports `NativeMarker ip=0 sp=<end of
TransitionBlock>`, and jumps to the next Frame. The walk still ends and
every reported frame is named, so frames go missing without any error.
## Cause
On WASM such a helper records the R2R caller's linear-stack pointer in
`TransitionBlock.m_StackPointer`, and may leave `m_ReturnAddress` 0.
Native handles that in `src/coreclr/vm/frames.h` and
`src/coreclr/vm/wasm/helpers.cpp`:
- `FramedMethodFrame::GetTransitionBlock_Impl` fills a 0 return address
lazily with `GetWasmVirtualIPFromStackPointer(m_StackPointer)`.
- `TransitionFrame::GetSP` returns `m_StackPointer` when both fields are
set, and `UpdateRegDisplay_Impl` uses those values plus that frame's
frame pointer.
The cDAC didn't describe `m_StackPointer`, and
`BaseFrameHandler.HandleTransitionFrame` used the raw return address (0)
and the end of the TransitionBlock.
## Changes
- `datadescriptor.inc`: describe `TransitionBlock.StackPointer` under
`TARGET_WASM`; it's an optional field
(`Data.TransitionBlock.StackPointer`).
- `FrameHelpers.GetTransitionBlockReturnAddress`: if the return address
is 0 and `StackPointer` is set, derive the R2R virtual IP from it
(`WasmUnwinder.GetVirtualIP`). `GetReturnAddress` uses this for all
TransitionFrame types.
- `WasmFrameHandler.HandleTransitionFrame`: when `StackPointer` is set
and a return address is known, the caller's SP is `StackPointer` and its
FP is the WASM logical frame pointer there (see below). Otherwise the SP
is the end of the TransitionBlock and the FP is null. The same path runs
when the interpreted chain under an `InterpreterFrame` is exhausted
(`InterpreterVirtualUnwind` → `ApplyInterpreterFrameTransition`).
- `WasmContext.Unwind`: recompute the frame pointer from each caller's
stack pointer, as native `WasmUnwindStackFrame` does. Before, every R2R
caller after the first kept the previous frame's frame pointer.
- Funclet-aware logical frame pointer, mirroring native
`GetWasmFramePointerFromStackPointer`, adapted from #133890.
`IWasmR2RInfo.TryIsFunclet` reads the `RUNTIME_FUNCTION` funclet bit.
`WasmUnwinder.TryGetLogicalFramePointer` returns a method's own frame
base. For a funclet, it unwinds out to the establishing frame: either
the parent method or funclet, or the frame pointer stored beside the
`TERMINATE_R2R_STACK_WALK` marker when the VM invoked the funclet
through `CallFuncletWith[out]Throwable`. Each step must move toward the
caller. `WasmContext.Unwind` and both R2R seeding paths (the
`InlinedCallFrame` marker and `HandleTransitionFrame`) use it.
- `StackWalk.md` TransitionFrame section updated, and the generated
tables regenerated.
#133890 carries an overlapping version of this fix and the logical frame
pointer, and will drop its copy when it restacks.
## Validation
- `./dotnet.sh test
src/native/managed/cdac/tests/UnitTests/Microsoft.Diagnostics.DataContractReader.Tests.csproj`:
**passed**, 3266/3266.
- `pwsh src/native/managed/cdac/tools/CdacUsageGraph/generate-docs.ps1
-Check`: **passed**, docs up to date.
- `./build.sh -os browser -c Debug -subset clr.runtime`: **passed**. The
generated descriptor lays out `TransitionBlock` as `ReturnAddress`@0,
`StackPointer`@4, size 8.
- New tests:
-
`UpdateContextFromFrame_WasmTransitionFrame_MirrorsNativeTransitionBlock`:
four cases, return address and stack pointer each set or 0.
-
`CreateStackWalk_WasmInterpreterFrameEnteredFromR2R_ContinuesIntoR2RCaller`:
the walk continues from an exhausted interpreted chain into two R2R
callers, checking each one's IP, SP and frame pointer.
- `WasmUnwinderTests.Unwind_ProducerLayout_*` (six cases): funclet
linear-stack layouts taken from the JIT and runtime producers. They
cover a finally called by its parent (with and without localloc), a
funclet invoked by the VM (catch, and filter with the throwing frames
still live), and nested finallys. Each uses real R2R function-table data
and checks SP, IP and FP for every frame through `WasmContext.Unwind`.
-
`UpdateContextFromFrame_WasmFuncletCaller_ReportsEstablishingFramePointer`:
both R2R seeding paths report the establishing frame pointer for a
VM-invoked funclet.
Each new test fails with its fix removed:
- TransitionFrame change reverted: the walk test and both stack-pointer
cases fail; the other two cases pass, as expected.
- Only the `WasmContext.Unwind` FP change reverted: the walk test fails
on the outer caller's frame pointer.
- Frame base instead of the logical frame pointer: both seeding cases
fail (checked on this branch), and all six producer-layout cases fail
(checked when they were written, on `main` @ `25c3eaec650`).
- Live browser-wasm validation through Blazor-Playground/nesm, with
nesm's workarounds and frame guard off: Release runtime packs built from
`9977dd27dc2` (descriptor `TransitionBlock.StackPointer`@4), with the
reader at `dd4ec1e0b6e` and again at `30047665a62` (funclet commits
included; no reader-attributable differences; only interpreter bytecode
addresses and async-pause depth vary between runs).
- R2R paused at a breakpoint: **pass**. Seeded from the frame chain (13
frames) and from a leaf `$sp` (21 frames). The 8 R2R CoreLib frames the
issue reported missing (`IntrinsicInvokeHelper.InvokeEmitted` …
`<BindManagedFunction>b__0`) now appear between the interpreted wrapper
and the next `InterpreterFrame`, in wasm-stack order. Their names match
a separate static ReadyToRun lookup. The `NativeMarker ip=0
sp=<TransitionBlock end>` is gone.
- R2R paused in a `[JSImport]` call (23 frames), and interpreter-only
paused in a tight loop (18) and in a `[JSImport]` call (22): **pass**.
Every walk completes with no errors.
- Funclets, reader `30047665a62`: an R2R app breaking inside each
funclet layout: finally called by its parent (with and without
localloc), catch and finally invoked by the VM, a finally nested in a
VM-invoked catch, and a filter. Both seeds complete in every scenario.
Each unwound funclet frame reports its establishing frame's frame
pointer, and the localloc parent reports FP ≠ SP from its indirect slot.
Finallys called by their parent were marked `NoOptimization`, because
otherwise the JIT clones them and no funclet runs. This checks
consistency with the parent frame's frame pointer, not a direct
comparison with native.
Not covered: a leaf funclet that makes no calls has no frame record.
Neither native nor the cDAC can walk it, so it's only visible at a
debugger pause inside it. Open #134608 may change that layout. Starting
a walk inside a funclet still takes the leaf frame pointer from the
frame base, tracked in #135147.
Resolves#135137
> [!NOTE]
> This PR description was generated with assistance from GitHub Copilot.
---------
Co-authored-by: Copilot App <[email protected]>
Copy file name to clipboardExpand all lines: docs/design/datacontracts/StackWalk.md
+7-2Lines changed: 7 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,7 @@ Unwinding call frames on the stack usually requires an OS specific implementatio
196
196
|`ResumableFrame`|`TargetContextPtr`|`pointer`| Pointer to the Frame's Target Context |
197
197
|`RuntimeFunction`|*(type size)*|`uint32`| Size of a runtime function entry in bytes |
198
198
|`RuntimeFunction`|`BeginAddress`|`uint32`| Begin address of the function. On ARM32, bit 0 is the Thumb bit; on WebAssembly, bit 31 marks a funclet and is excluded from address arithmetic. |
199
+
|`RuntimeFunction`|`UnwindData`|`uint32`| Pointer to the unwind info for the function |
199
200
|`SoftwareExceptionFrame`|`ReturnAddress`|`CodePointer`| Return address saved in Frame |
200
201
|`SoftwareExceptionFrame`|`TargetContext`|`pointer`| Context object saved in Frame |
201
202
|`String`|`m_StringLength`|`uint32`| Length of the string in UTF-16 characters |
@@ -212,6 +213,7 @@ Unwinding call frames on the stack usually requires an OS specific implementatio
212
213
|`TransitionBlock`|`CalleeSavedRegisters`|`pointer`| Platform specific CalleeSavedRegisters struct associated with the TransitionBlock |
|`TransitionBlock`|`ReturnAddress`|`CodePointer`| Return address associated with the TransitionBlock |
216
+
|`TransitionBlock`|`StackPointer`|`pointer`| WASM only: R2R linear-stack pointer of the caller, saved by the transition helper |
215
217
|`VASigCookie`|`SizeOfArgs`|`uint32`| Total size in bytes of the varargs argument area; used on x86 to locate the argument base |
216
218
217
219
### Global variables used
@@ -451,7 +453,7 @@ Most of the handlers are implemented in `BaseFrameHandler`. Platform specific co
451
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.
452
454
453
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.
454
-
* 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 that shadow frame's base. If no virtual IP can be recovered, IP is set to null.
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.
455
457
456
458
An active InlinedCallFrame stays the current Frame after its context update so the skipped-Frame check can step past it once the walk reaches the managed caller. If the updated IP is not managed code (for example, no WASM R2R virtual IP could be recovered), the walk fails, matching native `StackFrameIterator::NextRaw`; otherwise it would never advance past the Frame.
457
459
@@ -472,8 +474,11 @@ When updating the context from a TransitionFrame, the IP, SP, and all ABI specif
472
474
* On ARM, the additional register values stored in `ArgumentRegisters` are copied over. The `TransitionBlock` holds a pointer to the `ArgumentRegister` struct containing these values.
473
475
* On x86, the caller SP also skips callee-popped stack arguments. For `ExternalMethodFrame` and `StubDispatchFrame`, the argument size comes from the frame's GCRefMap, including when its MethodDesc is null. `ReadStackPop()` gives the number of pointer-sized slots to add to the SP. If no GCRefMap is available, MethodDesc-backed frames use the argument map computed from their signature. `PInvokeCalliFrame` instead uses `VASigCookie.SizeOfArgs`.
474
476
* For an x86 `StubDispatchFrame` without a GCRefMap, resolve its method from the stored MethodDesc or its representative MethodTable and slot. If no method is available, the SP stays at the end of the transition block and the IP is adjusted to the call instruction by subtracting five bytes from the saved return address.
477
+
* On WASM, a transition helper called from R2R code records the caller's R2R linear-stack pointer in `TransitionBlock.StackPointer`, and may leave `ReturnAddress` 0. When `ReturnAddress` is 0 and `StackPointer` is set, the return address is the R2R virtual IP of the frame at `StackPointer` (native `FramedMethodFrame::GetTransitionBlock_Impl`). When `StackPointer` is set and a return address is known, the caller's SP is `StackPointer` and its FP is the WASM logical frame pointer at `StackPointer` (native `TransitionFrame::GetSP`); otherwise the SP is the end of the TransitionBlock and the FP is null. This also applies when the interpreted chain under an `InterpreterFrame` is exhausted and the walker applies the `InterpreterFrame`'s transition.
475
478
476
-
**Return Address**: Read from `TransitionBlock.ReturnAddress`. For an x86 `StubDispatchFrame` with neither a method nor a GCRefMap, subtract five bytes to return the adjusted call address, matching the context update.
479
+
**WASM logical frame pointer.** R2R frames on WASM keep a record on the linear stack: the function-table index, then the function-local virtual IP / 2. Unwinding one frame (`WasmContext.Unwind`) adds the function's frame size from its unwind data to the frame base, and sets the caller's FP as native `GetWasmFramePointerFromStackPointer` does. For a method, the FP is its own frame base. For a funclet (its `RUNTIME_FUNCTION.BeginAddress` has bit 31 set), the FP is the establishing method's frame. The walker unwinds out of the funclet: if the caller slot holds the `TERMINATE_R2R_STACK_WALK` marker, the funclet was invoked by the VM through `CallFuncletWith[out]Throwable`, and the establishing frame pointer is stored one pointer after the marker. Otherwise the funclet was called by its parent method or funclet, and the walker repeats from there. Each step must move toward the caller, or the frame pointer is unknown (null).
480
+
481
+
**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.
477
482
478
483
The following Frame types also use this mechanism:
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ExecutionManager/Helpers/WasmFunctionTableIndexLookup.cs
+13Lines changed: 13 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -93,6 +93,19 @@ public bool TryGetVirtualIPBase(uint functionTableIndex, out ulong baseVirtualIP
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/Context/Wasm/WasmR2RInfo.cs
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -23,4 +23,7 @@ public bool TryGetVirtualIPBase(uint functionTableIndex, out ulong baseVirtualIP
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/Context/Wasm/WasmUnwinder.cs
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/Context/WasmContext.cs
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -88,11 +88,15 @@ public void Unwind(Target target)
88
88
{
89
89
StackPointer=sp;
90
90
InstructionPointer=ip;
91
+
// Native WasmUnwindStackFrame recomputes the caller's frame pointer from its stack pointer;
92
+
// funclets report the establishing method's frame base (GetWasmFramePointerFromStackPointer).
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/FrameHandling/FrameHelpers.cs
+20-1Lines changed: 20 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -286,6 +286,25 @@ public void UpdateContextFromFrame(Data.Frame frame, IPlatformAgnosticContext co
286
286
}
287
287
}
288
288
289
+
/// <summary>
290
+
/// Returns the return address recorded in <paramref name="transitionBlock"/>. On WASM, a
291
+
/// transition helper called from R2R code may store only the caller's linear-stack pointer;
292
+
/// the return address is then that frame's R2R virtual IP, matching the lazy computation in
Copy file name to clipboardExpand all lines: src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/StackWalk/FrameHandling/WasmFrameHandler.cs
+28-2Lines changed: 28 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -41,8 +41,8 @@ public override void HandleInlinedCallFrame(InlinedCallFrame inlinedCallFrame)
0 commit comments