Skip to content

Commit

Permalink
[mini] Enter GC Unsafe mode in handle_signal_exception (dotnet#88436)
Browse files Browse the repository at this point in the history
When the runtime needs to turn some kinds of signals into managed
exceptions (for example: SIGINT turns into
`new ExecutionEngineException ("Interrupted (SIGINT)")`, and some
SIGFPE turn into `DivideByZeroException`, and some SIGSEGV turn into a
`NullReferenceException`) instead of unwinding the stack from inside a
signal handler it instead adjusts the normal stack so that when the
signal handler returns, execution will resume in
`handle_signal_exception`.

That means that if the runtime was in GC Safe mode when the signal
was raised, even if the signal handler code transitions to GC Unsafe
mode, by the time the `handle_signal_exception` runs, we will have
undone the GC Unsafe transition and will be back in GC Safe.

That means if the code in `handle_signal_exception` (notably
`mono_handle_exception`) calls anything that tries to do a transition
to GC Safe, we may get an assertion.

Fixes dotnet#88405
  • Loading branch information
lambdageek authored Jul 18, 2023
1 parent 9a67179 commit a654a77
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/mono/mono/mini/exceptions-amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,12 @@ handle_signal_exception (gpointer obj)

memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, (MonoObject *)obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down
4 changes: 4 additions & 0 deletions src/mono/mono/mini/exceptions-arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,12 @@ handle_signal_exception (gpointer obj)

memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, (MonoObject*)obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down
4 changes: 4 additions & 0 deletions src/mono/mono/mini/exceptions-arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,12 @@ handle_signal_exception (gpointer obj)

memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, (MonoObject*)obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down
4 changes: 4 additions & 0 deletions src/mono/mono/mini/exceptions-ppc.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,12 @@ handle_signal_exception (gpointer obj)

memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down
5 changes: 5 additions & 0 deletions src/mono/mono/mini/exceptions-riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,12 @@ handle_signal_exception (gpointer obj)
MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
MonoContext ctx = jit_tls->ex_ctx;

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down
6 changes: 6 additions & 0 deletions src/mono/mono/mini/exceptions-s390x.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,13 @@ handle_signal_exception (gpointer obj)
MonoContext ctx;

memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down
4 changes: 4 additions & 0 deletions src/mono/mono/mini/exceptions-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,12 @@ handle_signal_exception (gpointer obj)

memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));

MONO_ENTER_GC_UNSAFE_UNBALANCED;

mono_handle_exception (&ctx, (MonoObject*)obj);

MONO_EXIT_GC_UNSAFE_UNBALANCED;

mono_restore_context (&ctx);
}

Expand Down

0 comments on commit a654a77

Please sign in to comment.