Skip to content

Commit 7bd7aee

Browse files
KJ Tsanaktsidisbyroot
authored andcommitted
Fix interpreter crash caused by RUBY_INTERNAL_EVENT_NEWOBJ + Ractors
When a Ractor is created whilst a tracepoint for RUBY_INTERNAL_EVENT_NEWOBJ is active, the interpreter crashes. This is because during the early setup of the Ractor, the stdio objects are created, which allocates Ruby objects, which fires the tracepoint. However, the tracepoint machinery tries to dereference the control frame (ec->cfp->pc), which isn't set up yet and so crashes with a null pointer dereference. Fix this by not firing GC tracepoints if cfp isn't yet set up.
1 parent 1a0d3ec commit 7bd7aee

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

gc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,7 @@ rb_objspace_set_event_hook(const rb_event_flag_t event)
24842484
static void
24852485
gc_event_hook_body(rb_execution_context_t *ec, rb_objspace_t *objspace, const rb_event_flag_t event, VALUE data)
24862486
{
2487+
if (UNLIKELY(!ec->cfp)) return;
24872488
const VALUE *pc = ec->cfp->pc;
24882489
if (pc && VM_FRAME_RUBYFRAME_P(ec->cfp)) {
24892490
int prev_opcode = rb_vm_insn_addr2opcode((void *)*ec->cfp->iseq->body->iseq_encoded);

test/objspace/test_ractor.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "test/unit"
2+
3+
class TestObjSpaceRactor < Test::Unit::TestCase
4+
def test_tracing_does_not_crash
5+
assert_ractor(<<~RUBY, require: 'objspace')
6+
ObjectSpace.trace_object_allocations do
7+
r = Ractor.new do
8+
obj = 'a' * 1024
9+
Ractor.yield obj
10+
end
11+
12+
r.take
13+
r.take
14+
end
15+
RUBY
16+
end
17+
end

0 commit comments

Comments
 (0)