Skip to content

Commit a654a77

Browse files
authored
[mini] Enter GC Unsafe mode in handle_signal_exception (#88436)
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 #88405
1 parent 9a67179 commit a654a77

File tree

7 files changed

+31
-0
lines changed

7 files changed

+31
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,8 +764,12 @@ handle_signal_exception (gpointer obj)
764764

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

767+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
768+
767769
mono_handle_exception (&ctx, (MonoObject *)obj);
768770

771+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
772+
769773
mono_restore_context (&ctx);
770774
}
771775

src/mono/mono/mini/exceptions-arm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,12 @@ handle_signal_exception (gpointer obj)
574574

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

577+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
578+
577579
mono_handle_exception (&ctx, (MonoObject*)obj);
578580

581+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
582+
579583
mono_restore_context (&ctx);
580584
}
581585

src/mono/mono/mini/exceptions-arm64.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,12 @@ handle_signal_exception (gpointer obj)
522522

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

525+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
526+
525527
mono_handle_exception (&ctx, (MonoObject*)obj);
526528

529+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
530+
527531
mono_restore_context (&ctx);
528532
}
529533

src/mono/mono/mini/exceptions-ppc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,12 @@ handle_signal_exception (gpointer obj)
734734

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

737+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
738+
737739
mono_handle_exception (&ctx, obj);
738740

741+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
742+
739743
mono_restore_context (&ctx);
740744
}
741745

src/mono/mono/mini/exceptions-riscv.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,12 @@ handle_signal_exception (gpointer obj)
390390
MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
391391
MonoContext ctx = jit_tls->ex_ctx;
392392

393+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
394+
393395
mono_handle_exception (&ctx, obj);
396+
397+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
398+
394399
mono_restore_context (&ctx);
395400
}
396401

src/mono/mono/mini/exceptions-s390x.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,13 @@ handle_signal_exception (gpointer obj)
673673
MonoContext ctx;
674674

675675
memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
676+
677+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
678+
676679
mono_handle_exception (&ctx, obj);
680+
681+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
682+
677683
mono_restore_context (&ctx);
678684
}
679685

src/mono/mono/mini/exceptions-x86.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,12 @@ handle_signal_exception (gpointer obj)
935935

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

938+
MONO_ENTER_GC_UNSAFE_UNBALANCED;
939+
938940
mono_handle_exception (&ctx, (MonoObject*)obj);
939941

942+
MONO_EXIT_GC_UNSAFE_UNBALANCED;
943+
940944
mono_restore_context (&ctx);
941945
}
942946

0 commit comments

Comments
 (0)