From ffa5f16273f46c97bfca56e4549b0b38b9322d63 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 20 Dec 2023 13:54:32 -0800 Subject: [PATCH] Make rb_profile_frames return 0 for NULL ec When using M:N threads, EC is set to NULL in the shared native thread when nothing is scheduled. This previously caused a segfault when we try to examine the EC. Returning 0 instead means we may miss profiling information, but a profiler relying on this isn't thread aware anyways, and observing that "nothing" is running is probably correct. Fixes [Bug #20017] Co-authored-by: Dustin Brown --- vm_backtrace.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/vm_backtrace.c b/vm_backtrace.c index 0d55eae0420718..6e9436f76a33f7 100644 --- a/vm_backtrace.c +++ b/vm_backtrace.c @@ -1641,7 +1641,14 @@ thread_profile_frames(rb_execution_context_t *ec, int start, int limit, VALUE *b int rb_profile_frames(int start, int limit, VALUE *buff, int *lines) { - rb_execution_context_t *ec = GET_EC(); + rb_execution_context_t *ec = rb_current_execution_context(false); + + // If there is no EC, we may be attempting to profile a non-Ruby thread or a + // M:N shared native thread which has no active Ruby thread. + if (!ec) { + return 0; + } + return thread_profile_frames(ec, start, limit, buff, lines); }