Skip to content

Commit a7fda7e

Browse files
authored
[wasm] Fix caller identification across R2R prestub transitions (dotnet#133707)
Fixes dotnet#133617. ## Problem The Wasm portable-entrypoint slow path retains a `PrestubMethodFrame` while invoking a resolved R2R body. Function-only stack walks can report both the real managed activation and that transition frame. `MethodBase.GetCurrentMethod` can therefore accept its own prestub frame as the caller. A Checked-runtime capture confirmed this: the stack marker was `0x4fe71c`; the real `GetCurrentMethod` body at SP `0x4fe710` did not pass the caller-marker comparison, but its explicit `PrestubMethodFrame` at SP `0x4fe720` did. The result was `GetCurrentMethod` instead of the calling method. This also reproduces without trimming. dotnet#133610 addressed the duplicate in `GetStackFramesCallback`, but caller-sensitive reflection uses a different callback and did not benefit from that filter. ## Change - Mark completed non-FCall Wasm prestub transitions as not representing an additional managed activation. - Filter those frames centrally for `FUNCTIONSONLY` walks, covering both ordinary and skipped explicit frames. - Retain the frame, MethodDesc, argument-root reporting, and physical unwind/exception lifetime. Popping early or clearing the MethodDesc would lose GC protection and the unwind bridge across interpreter-to-R2R transitions. - Replace dotnet#133610's StackTrace-specific adjacency filter. Filtering by lifecycle rather than matching method identities preserves genuine recursion. - Extend the existing `github60486` regression with direct/inlineable/generic caller identity, assembly lookup, recursive frame counts, and compacting-GC object/byref/struct argument coverage. Its original DIM frame-sequence and exception-stack checks remain intact. Native FCalls retain their existing representation. Non-Wasm filtering behavior is unchanged. ## Validation ### CoreCLR runtime regression Built browser-Wasm and macOS arm64 Checked runtimes with Release libraries. The expanded `Loader/classloader/DefaultInterfaceMethods/regressions/github60486` passes with fixed Wasm R2R, the interpreter, and native arm64. The identical final regression assembly fails against the saved original Wasm runtime: expected `ValidateCurrentMethod`, actual `GetCurrentMethod`. These fixed runs include the original regression checks from dotnet#133610, with its `debugdebugger.cpp` filter removed. ### Original browser library lane Validated the exact patch in an isolated checkout of dotnet#133656 at `282d12dfe220a1f34d0bb81a85239cab60c779ce`, which supplies the browser R2R publishing/test prerequisites. Built matched original/fixed CoreCLR Release products locally and ran Chromium 153. Temporarily enabled only the nine dotnet#133617-quarantined methods, preserving every original assertion and unrelated exclusion; all annotations were restored afterward. This PR contains no library-test quarantine changes. | Coverage | Original | Fixed | |---|---:|---:| | Cold trimmed direct GetCurrentMethod | 1 failed | 1 passed | | Cold trimmed inlineable GetCurrentMethod | 1 failed | 1 passed | | Trimmed targeted batch | 7 passed, 1 failed | 8 passed | | Untrimmed R2R targeted batch | 8 passed, 1 failed | 9 passed | | Full trimmed System.Runtime | 77,396 passed, 1 failed, 175 skipped | 77,397 passed, 0 failed, 175 skipped | The affected-class fixed run also passed: 23 passed, 3 existing skips. `CrossAssembly2` remains excluded when trimmed by dotnet/linker#2078 and passes untrimmed; it is not counted as a trimmed pass. Main test IL and main R2R images are byte-identical across each original/fixed pair; skip sets are identical. Served assets were verified against their respective rebuilt packs. This is a matched-product A/B, not a native-module-only swap: CoreLib R2R image bytes also differ. ### Existing reflection compatibility controls `System.Reflection.Tests` caller-assembly theory, real cross-assembly delegate invocation, static-constructor caller lookup, executing-assembly lookup, and reflected first/second invocation all pass: six cases on each original/fixed, trimmed/untrimmed browser R2R configuration. Fresh-process delegate and reflective-invocation probes also pass in all four configurations. All existing annotations remained intact. These are compatibility controls, not failing-before reproductions. Full browser reflection suites have identical original/fixed results: - Untrimmed: 1,750 passed, 1 failed, 23 skipped. - Trimmed: 1,747 passed, 2 failed, 23 skipped. Existing failures are `GetEntryAssembly` (browser host expectation versus R2R expectation) and, trimmed only, `AssemblyGetForwardedTypesLoadFailure` (underlying trimming cause not isolated). Failure signatures and skip sets match before/after; neither was suppressed. Native arm64 Checked reflection control: six focused cases passed; full suite 1,776 passed, 2 platform skips, zero failures. ## Remaining scope Draft for review of the frame-lifecycle distinction and FCall exception. Dedicated FCall cold/warm stack-identity coverage, a broader CoreCLR GC-stress campaign, and other browsers have not been run. No performance claim is made. > [!NOTE] > This change and pull request description were developed with GitHub Copilot assistance. Co-authored-by: Copilot App <[email protected]>
1 parent 778255f commit a7fda7e

5 files changed

Lines changed: 99 additions & 13 deletions

File tree

‎src/coreclr/vm/debugdebugger.cpp‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,6 @@ static StackWalkAction GetStackFramesCallback(CrawlFrame* pCf, VOID* data)
196196
MethodDesc* pFunc = pCf->GetFunction();
197197
DebugStackTrace::GetStackFramesData* pData = (DebugStackTrace::GetStackFramesData*)data;
198198

199-
#ifdef TARGET_WASM
200-
// The portable-entrypoint slow path keeps its prestub frame active while it invokes a newly
201-
// discovered R2R body. The body is already reported as a frameless method.
202-
if (!pCf->IsFrameless() &&
203-
pCf->GetFrame()->GetFrameIdentifier() == FrameIdentifier::PrestubMethodFrame &&
204-
pData->cElements > 0 &&
205-
pData->pElements[pData->cElements - 1].pFunc == pFunc &&
206-
pData->pElements[pData->cElements - 1].ip != (PCODE)NULL)
207-
{
208-
return SWA_CONTINUE;
209-
}
210-
#endif // TARGET_WASM
211-
212199
if (pFunc != nullptr && pFunc == g_pEnvironmentCallEntryPointMethodDesc)
213200
{
214201
return SWA_CONTINUE;

‎src/coreclr/vm/frames.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ class Frame
300300
FRAME_ATTR_EXCEPTION = 1, // This frame caused an exception
301301
FRAME_ATTR_FAULTED = 4, // Exception caused by Win32 fault
302302
FRAME_ATTR_RESUMABLE = 8, // We may resume from this frame
303+
FRAME_ATTR_NO_MANAGED_ACTIVATION = 16, // Retained for rooting and unwinding, not an additional managed call
303304
};
304305
unsigned GetFrameAttribs_Impl()
305306
{
@@ -1397,9 +1398,34 @@ typedef DPTR(class PrestubMethodFrame) PTR_PrestubMethodFrame;
13971398

13981399
class PrestubMethodFrame : public FramedMethodFrame
13991400
{
1401+
#ifdef TARGET_WASM
1402+
bool m_isPrestubComplete = false;
1403+
#endif // TARGET_WASM
1404+
14001405
public:
14011406
PrestubMethodFrame(TransitionBlock * pTransitionBlock, MethodDesc * pMD);
14021407

1408+
#ifdef TARGET_WASM
1409+
void MarkPrestubComplete()
1410+
{
1411+
CONTRACTL
1412+
{
1413+
NOTHROW;
1414+
GC_NOTRIGGER;
1415+
MODE_COOPERATIVE;
1416+
}
1417+
CONTRACTL_END;
1418+
1419+
m_isPrestubComplete = true;
1420+
}
1421+
1422+
unsigned GetFrameAttribs_Impl()
1423+
{
1424+
LIMITED_METHOD_DAC_CONTRACT;
1425+
return m_isPrestubComplete ? FRAME_ATTR_NO_MANAGED_ACTIVATION : FRAME_ATTR_NONE;
1426+
}
1427+
#endif // TARGET_WASM
1428+
14031429
void GcScanRoots_Impl(promote_func *fn, ScanContext* sc)
14041430
{
14051431
WRAPPER_NO_CONTRACT;

‎src/coreclr/vm/prestub.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,15 @@ void ExecuteInterpretedMethodWithArgs_PortableEntryPoint_Complex(PCODE portableE
22072207
if (targetIp == NULL)
22082208
{
22092209
_ASSERTE(!PortableEntryPoint::PrefersInterpreterEntryPoint(portableEntrypoint));
2210+
#ifdef TARGET_WASM
2211+
// Keep the transition and argument roots while invoking R2R code, but report
2212+
// the managed activation through its R2R body rather than this prestub.
2213+
// An FCall can still need the prestub to represent its native implementation.
2214+
if (!pMethod->IsFCall())
2215+
{
2216+
pPFrame->MarkPrestubComplete();
2217+
}
2218+
#endif // TARGET_WASM
22102219
Object* continuationRet = nullptr;
22112220
Object** pContinuationRet = nullptr;
22122221
#ifdef TARGET_WASM

‎src/coreclr/vm/stackwalk.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,13 @@ StackWalkAction StackFrameIterator::Filter(void)
20392039
case SFITER_SKIPPED_FRAME_FUNCTION:
20402040
if (!fSkippingFunclet)
20412041
{
2042+
#ifdef TARGET_WASM
2043+
if ((m_flags & FUNCTIONSONLY) &&
2044+
(m_crawl.pFrame->GetFrameAttribs() & Frame::FRAME_ATTR_NO_MANAGED_ACTIVATION))
2045+
{
2046+
break;
2047+
}
2048+
#endif // TARGET_WASM
20422049
if (m_flags & GC_FUNCLET_REFERENCE_REPORTING)
20432050
{
20442051
// If we are enumerating frames for GC reporting and we determined that

‎src/tests/Loader/classloader/DefaultInterfaceMethods/regressions/github60486.cs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using System.Runtime.CompilerServices;
34
using Xunit;
45
using TestLibrary;
@@ -117,10 +118,66 @@ public class Program : ProgramBase<InputData>, TestItf2<InputData>
117118
[Fact]
118119
public static void TestEntryPoint()
119120
{
121+
ValidateCurrentMethod();
120122
new Program().Start();
121123
ValidateExceptionStackTrace();
122124
}
123125

126+
private static void ValidateCurrentMethod()
127+
{
128+
for (int i = 0; i < 2; i++)
129+
{
130+
Assert.Equal(nameof(ValidateCurrentMethod), MethodBase.GetCurrentMethod().Name);
131+
Assert.Equal(nameof(GetCurrentMethodInlineable), GetCurrentMethodInlineable().Name);
132+
MethodBase genericMethod = GetCurrentMethodGeneric<string>();
133+
Assert.Equal(nameof(GetCurrentMethodGeneric), genericMethod.Name);
134+
Assert.True(genericMethod.IsGenericMethodDefinition);
135+
Assert.Equal(typeof(Program).Assembly, Assembly.GetExecutingAssembly());
136+
Assert.Equal(typeof(Program).Assembly, Assembly.GetCallingAssembly());
137+
ValidateRecursiveCurrentMethod(3);
138+
InputData byref = new InputData { i = 2 };
139+
ValidateTransitionRoots(new InputData { i = 1 }, ref byref,
140+
(new InputData { i = 3 }, new InputData { i = 4 }));
141+
}
142+
}
143+
144+
private static MethodBase GetCurrentMethodInlineable() => MethodBase.GetCurrentMethod();
145+
146+
private static MethodBase GetCurrentMethodGeneric<T>() => MethodBase.GetCurrentMethod();
147+
148+
[MethodImpl(MethodImplOptions.NoInlining)]
149+
private static void ValidateTransitionRoots(InputData value, ref InputData byref, (InputData, InputData) pair)
150+
{
151+
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, blocking: true, compacting: true);
152+
Assert.Equal(1, value.i);
153+
Assert.Equal(2, byref.i);
154+
Assert.Equal(3, pair.Item1.i);
155+
Assert.Equal(4, pair.Item2.i);
156+
}
157+
158+
[MethodImpl(MethodImplOptions.NoInlining)]
159+
private static void ValidateRecursiveCurrentMethod(int depth)
160+
{
161+
Assert.Equal(nameof(ValidateRecursiveCurrentMethod), MethodBase.GetCurrentMethod().Name);
162+
if (depth > 0)
163+
{
164+
ValidateRecursiveCurrentMethod(depth - 1);
165+
}
166+
else
167+
{
168+
int frameCount = 0;
169+
foreach (System.Diagnostics.StackFrame frame in new System.Diagnostics.StackTrace().GetFrames())
170+
{
171+
if (frame.GetMethod().Name == nameof(ValidateRecursiveCurrentMethod))
172+
{
173+
frameCount++;
174+
}
175+
}
176+
177+
Assert.Equal(4, frameCount);
178+
}
179+
}
180+
124181
[MethodImpl(MethodImplOptions.NoInlining)]
125182
private static void ThrowForStackTrace() => throw new Exception();
126183

0 commit comments

Comments
 (0)