Skip to content

Commit aa9e74e

Browse files
lewingCopilot
andauthored
[wasm] Recognize R2R virtual IPs in IsManagedCode (#133266)
Fixes #133264. `ExecutionManager::IsManagedCodeWorker` looks up wasm R2R virtual instruction pointers in the native code-range map, incorrectly classifying managed funclet callers as native. GC stack walking inside a normally called nested finally can then assert `fFrameWasUnwound` in `stackwalk.cpp`. Route wasm virtual IPs through the existing `FindCodeRange(..., ScanNoReaderLock)` lookup. Native addresses retain `GetRangeSection` and its existing lock-state handling. Registered virtual ranges still undergo the existing R2R method check, including its deadlock-safe early return when no `MethodDesc` is requested. Add a two-case nested-finally regression covering normal and exceptional entry, explicit GC, expected exception handling, and the post-GC array value. ### Validation Reused completed local validation from the source worktree after confirming that the isolated branch has the identical `main` base tree (`de29e26a4f69da5d446900a6ba9d68ba3ddc5ba9`) and both changed files match byte-for-byte; no new build was run in the publication worktree. - Browser-wasm Checked runtime build and targeted `corerun` rebuild succeeded; `Methodical_do` and `Methodical_d1` were rebuilt with the new cases. - The unmodified native host asserts `fFrameWasUnwound` on the first new case in both runners. - The fixed native host passes both new cases and the existing `Test_throwinfinallynestedintry_30_cs` in each runner: six focused passes. - Independence control: `Methodical_do`, recompiled with the saved unmodified cross-JIT, passes both new cases and the existing `_30` test with the fixed native runtime. Execution used the browser-wasm R2R runner under Node.js on macOS ARM64 with a retained managed Core_Root. Graphical-browser and native-ARM64 execution were not performed. This PR contains no JIT changes and is independent of the separate conditional-EH-fallthrough fix. It is also distinct from #133219's post-catch cooperative-mode restoration; no GC-mode patch was applied for this validation. > [!NOTE] > This PR description was generated by GitHub Copilot from the reviewed patch and local validation evidence. Co-authored-by: Copilot App <[email protected]>
1 parent de29e26 commit aa9e74e

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎src/coreclr/vm/codeman.cpp‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5688,7 +5688,17 @@ BOOL ExecutionManager::IsManagedCodeWorker(PCODE currentPC, RangeSectionLockStat
56885688
// taken over the call to JitCodeToMethodInfo too so that nobody pulls out
56895689
// the range section from underneath us.
56905690

5691-
RangeSection * pRS = GetRangeSection(currentPC, pLockState);
5691+
RangeSection* pRS;
5692+
#ifdef TARGET_WASM
5693+
if (IsVirtualIP(currentPC))
5694+
{
5695+
pRS = FindCodeRange(currentPC, ScanNoReaderLock);
5696+
}
5697+
else
5698+
#endif // TARGET_WASM
5699+
{
5700+
pRS = GetRangeSection(currentPC, pLockState);
5701+
}
56925702
if (pRS == NULL)
56935703
return FALSE;
56945704

‎src/tests/JIT/Methodical/eh/basics/tryfinallytryfinally.cs‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Runtime.CompilerServices;
56
using Xunit;
67

78
namespace hello_tryfinallytryfinally_basics_cs
@@ -55,4 +56,57 @@ static public int TestEntryPoint()
5556
return testLog.VerifyOutput();
5657
}
5758
}
59+
60+
public class NestedFinallyGc
61+
{
62+
[Theory]
63+
[InlineData(false)]
64+
[InlineData(true)]
65+
public static void Test(bool throwException)
66+
{
67+
int[] values = new int[1];
68+
bool caught = false;
69+
try
70+
{
71+
Run(values, throwException);
72+
}
73+
catch (InvalidOperationException)
74+
{
75+
caught = true;
76+
}
77+
78+
Assert.Equal(throwException, caught);
79+
Assert.Equal(111, values[0]);
80+
}
81+
82+
[MethodImpl(MethodImplOptions.NoInlining)]
83+
private static void Run(int[] values, bool throwException)
84+
{
85+
try
86+
{
87+
values[0] = 1;
88+
if (throwException)
89+
throw new InvalidOperationException();
90+
}
91+
finally
92+
{
93+
try
94+
{
95+
Update(values);
96+
}
97+
finally
98+
{
99+
// This finally is called normally, including during exceptional entry to the outer finally.
100+
GC.Collect();
101+
values[0] += 100;
102+
}
103+
}
104+
}
105+
106+
[MethodImpl(MethodImplOptions.NoInlining)]
107+
private static void Update(int[] values)
108+
{
109+
values[0] += 10;
110+
}
111+
}
58112
}

0 commit comments

Comments
 (0)