Skip to content

Commit 3db5b35

Browse files
[debugger] Switch to GC Unsafe in signal handler callbacks (#20495)
If the runtime gets a single step or breakpoint signal while it is already running native code for a P/Invoke, it will be in GC Safe mode. Switch back to GC Unsafe to run the debugger engine steps. Addresses #20490 Co-authored-by: Aleksey Kliger <alklig@microsoft.com>
1 parent af315f4 commit 3db5b35

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

mono/mini/debugger-agent.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,8 @@ process_suspend (DebuggerTlsData *tls, MonoContext *ctx)
28292829
static gboolean
28302830
try_process_suspend (void *the_tls, MonoContext *ctx, gboolean from_breakpoint)
28312831
{
2832+
MONO_REQ_GC_UNSAFE_MODE;
2833+
28322834
DebuggerTlsData *tls = (DebuggerTlsData*)the_tls;
28332835
/* if there is a suspend pending that is not executed yes */
28342836
if (suspend_count > 0) {
@@ -4946,7 +4948,13 @@ debugger_agent_single_step_from_context (MonoContext *ctx)
49464948
mono_thread_state_init_from_monoctx (&tls->restore_state, ctx);
49474949
memcpy (&tls->handler_ctx, ctx, sizeof (MonoContext));
49484950

4951+
/* We might be called while the thread is already running some native
4952+
* code after an native-to-managed transition, so the thread might be
4953+
* in GC Safe mode.
4954+
*/
4955+
MONO_ENTER_GC_UNSAFE;
49494956
mono_de_process_single_step (tls, FALSE);
4957+
MONO_EXIT_GC_UNSAFE;
49504958

49514959
memcpy (ctx, &tls->restore_state.ctx, sizeof (MonoContext));
49524960
memcpy (&tls->restore_state, &orig_restore_state, sizeof (MonoThreadUnwindState));
@@ -4976,7 +4984,13 @@ debugger_agent_breakpoint_from_context (MonoContext *ctx)
49764984
mono_thread_state_init_from_monoctx (&tls->restore_state, ctx);
49774985
memcpy (&tls->handler_ctx, ctx, sizeof (MonoContext));
49784986

4987+
/* We might be called while the thread is already running some native
4988+
* code after an native-to-managed transition, so the thread might be
4989+
* in GC Safe mode.
4990+
*/
4991+
MONO_ENTER_GC_UNSAFE;
49794992
mono_de_process_breakpoint (tls, FALSE);
4993+
MONO_EXIT_GC_UNSAFE;
49804994

49814995
memcpy (ctx, &tls->restore_state.ctx, sizeof (MonoContext));
49824996
memcpy (&tls->restore_state, &orig_restore_state, sizeof (MonoThreadUnwindState));

0 commit comments

Comments
 (0)