Skip to content

Commit 37dd42c

Browse files
Fix Mono Windows x64 hardware exception continuations (#133774)
## Summary Fix Windows x64 Mono MiniJIT hardware-exception handling when Windows validates exception-continuation addresses. The vectored exception handler currently resumes directly at a dynamically generated managed handler; affected processes terminate with `FAST_FAIL_SET_CONTEXT_DENIED` instead of entering the catch. Save the resolved managed handler context in existing thread-local storage and resume first at a native runtime entry point, which restores that context through Mono's existing restore trampoline. Supply the Win64 entry-stack alignment and argument home area, and keep the stack-overflow restoration context separate from the native continuation. This does not change process mitigation settings or add general CET support. Add a three-case theory covering repeated divide-by-zero, overflow, and null-reference exceptions with nested hardware exceptions in filters and finally blocks. Restore the Windows x64 MiniJIT runtime-test lane disabled by #133747 so this PR exercises the full lane. Fixes #133702. ## Validation - Built Release and Debug Mono on Windows x64. - `div2_d`, `div2_ro`, `HardwareEh`, `unhandledTester`, and `ExceptionInterop` pass with both native builds in MiniJIT mode. - The same final `HardwareEh` binary exits with `0xC0000409` on the saved unfixed runtime and passes on the fixed Release and Debug runtimes. Verified that its generated entry point invokes all three new theory cases. It also passes with the Release interpreter. - Repeated local explicit-throw measurements overlap within measurement uncertainty: baseline 2.070 us versus candidate 2.008 us. The unfixed hardware-throw benchmark terminates, so there is no valid before/after throughput ratio for that path. - Verified that `eng/pipelines/runtime.yml` exactly matches its pre-quarantine version. Full MiniJIT CI and cross-platform suites have not been run locally. > [!NOTE] > This PR was generated with GitHub Copilot. Co-authored-by: Copilot App <[email protected]>
1 parent ea973e7 commit 37dd42c

3 files changed

Lines changed: 126 additions & 4 deletions

File tree

‎eng/pipelines/runtime.yml‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,8 +1777,44 @@ extends:
17771777
extraVariablesTemplates:
17781778
- template: /eng/pipelines/common/templates/runtimes/test-variables.yml
17791779

1780-
# ActiveIssue https://github.com/dotnet/runtime/issues/133702
1781-
# Re-enable Windows x64 Mono MiniJIT runtime tests once FAST_FAIL_SET_CONTEXT_DENIED is resolved.
1780+
#
1781+
# Build the whole product using Mono and run runtime tests
1782+
#
1783+
- template: /eng/pipelines/common/platform-matrix.yml
1784+
parameters:
1785+
jobTemplate: /eng/pipelines/common/global-build-job.yml
1786+
helixQueuesTemplate: /eng/pipelines/coreclr/templates/helix-queues-setup.yml
1787+
buildConfig: Release
1788+
runtimeFlavor: mono
1789+
platforms:
1790+
- windows_x64
1791+
variables:
1792+
- name: timeoutPerTestInMinutes
1793+
value: 60
1794+
- name: timeoutPerTestCollectionInMinutes
1795+
value: 180
1796+
jobParameters:
1797+
testGroup: innerloop
1798+
nameSuffix: AllSubsets_Mono_Minijit_RuntimeTests
1799+
runtimeVariant: minijit
1800+
buildArgs: -s mono+libs+clr.hosts+clr.iltools -c Release -lc ${{ variables.debugOnPrReleaseOnRolling }}
1801+
timeoutInMinutes: 180
1802+
condition: >-
1803+
or(
1804+
eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_mono_excluding_wasm.containsChange'], true),
1805+
eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_runtimetests.containsChange'], true),
1806+
eq(variables['isRollingBuild'], true))
1807+
1808+
postBuildSteps:
1809+
- template: /eng/pipelines/common/templates/runtimes/build-runtime-tests-and-send-to-helix.yml
1810+
parameters:
1811+
creator: dotnet-bot
1812+
testRunNamePrefixSuffix: Mono_Release
1813+
useHelixMonitor: ${{ variables.enableHelixJobMonitor }}
1814+
extraVariablesTemplates:
1815+
- template: /eng/pipelines/common/templates/runtimes/test-variables.yml
1816+
parameters:
1817+
liveLibrariesBuildConfig: ${{ variables.debugOnPrReleaseOnRolling }}
17821818

17831819
#
17841820
# Mono CoreCLR runtime Test executions using live libraries in interpreter mode

‎src/mono/mono/mini/exceptions-amd64.c‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ void *mono_win_vectored_exception_handle;
5656
#define W32_SEH_HANDLE_EX(_ex) \
5757
if (_ex##_handler) _ex##_handler(er->ExceptionCode, &info, ctx)
5858

59+
static void
60+
seh_restore_context (void)
61+
{
62+
MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
63+
mono_restore_context (&jit_tls->ex_ctx);
64+
}
65+
5966
static LONG CALLBACK seh_unhandled_exception_filter(EXCEPTION_POINTERS* ep)
6067
{
6168
#ifndef MONO_CROSS_COMPILE
@@ -156,8 +163,10 @@ static LONG CALLBACK seh_vectored_exception_handler(EXCEPTION_POINTERS* ep)
156163
/* need to restore stack protection once stack is unwound
157164
* restore_stack will restore stack protection and then
158165
* resume control to the saved stack_restore_ctx */
159-
mono_sigctx_to_monoctx (ctx, &jit_tls->stack_restore_ctx);
160-
ctx->Rip = (guint64)restore_stack;
166+
jit_tls->stack_restore_ctx = jit_tls->ex_ctx;
167+
MONO_CONTEXT_SET_IP (&jit_tls->ex_ctx, restore_stack);
168+
MONO_CONTEXT_SET_SP (&jit_tls->ex_ctx,
169+
ALIGN_DOWN_TO ((guint64)MONO_CONTEXT_GET_SP (&jit_tls->ex_ctx), 16) - 8);
161170
}
162171
} else {
163172
info.handled = FALSE;
@@ -826,6 +835,19 @@ mono_arch_handle_exception (void *sigctx, gpointer obj)
826835

827836
mono_handle_exception (&mctx, obj);
828837

838+
#ifdef TARGET_WIN32
839+
/*
840+
* Windows validates exception continuation IPs when hardware stack protection is
841+
* enabled. Resume in native runtime code rather than an unregistered JIT handler.
842+
* Keep the handler context in TLS and provide the native entry point with an
843+
* aligned stack, a return-address slot, and the Win64 argument home area.
844+
*/
845+
MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
846+
jit_tls->ex_ctx = mctx;
847+
MONO_CONTEXT_SET_IP (&mctx, seh_restore_context);
848+
MONO_CONTEXT_SET_SP (&mctx, ALIGN_DOWN_TO ((guint64)MONO_CONTEXT_GET_SP (&mctx), 16) - 40);
849+
#endif
850+
829851
mono_monoctx_to_sigctx (&mctx, sigctx);
830852

831853
return TRUE;

‎src/tests/baseservices/exceptions/simple/HardwareEh.cs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,70 @@ public class HardwareEh
2323
public const long c_VALUE = 34252;
2424
public delegate bool TestDelegate();
2525

26+
[Theory]
27+
[InlineData(0, typeof(DivideByZeroException))]
28+
[InlineData(1, typeof(OverflowException))]
29+
[InlineData(2, typeof(NullReferenceException))]
30+
public static void NestedHardwareExceptions(int operation, Type exceptionType)
31+
{
32+
int finallyCount = 0;
33+
34+
for (int i = 0; i < 16; i++)
35+
{
36+
bool caught = false;
37+
38+
try
39+
{
40+
try
41+
{
42+
TriggerHardwareException(operation);
43+
}
44+
finally
45+
{
46+
Assert.Throws<NullReferenceException>(() => TriggerHardwareException(2));
47+
finallyCount++;
48+
}
49+
}
50+
catch (Exception ex) when (FilterHardwareException(ex, exceptionType))
51+
{
52+
caught = true;
53+
}
54+
55+
Assert.True(caught);
56+
Assert.Equal(i + 1, finallyCount);
57+
}
58+
}
59+
60+
private static bool FilterHardwareException(Exception exception, Type exceptionType)
61+
{
62+
Assert.Throws<DivideByZeroException>(() => TriggerHardwareException(0));
63+
return exception.GetType() == exceptionType;
64+
}
65+
66+
[MethodImpl(MethodImplOptions.NoInlining)]
67+
private static int Divide(int dividend, int divisor)
68+
{
69+
return dividend / divisor;
70+
}
71+
72+
[MethodImpl(MethodImplOptions.NoInlining)]
73+
private static void TriggerHardwareException(int operation)
74+
{
75+
switch (operation)
76+
{
77+
case 0:
78+
Divide(1, 0);
79+
break;
80+
case 1:
81+
Divide(int.MinValue, -1);
82+
break;
83+
case 2:
84+
int[] values = null;
85+
GC.KeepAlive(values[0]);
86+
break;
87+
}
88+
}
89+
2690
[Fact]
2791
public static int TestEntryPoint()
2892
{

0 commit comments

Comments
 (0)