Skip to content

Commit 660b995

Browse files
viralpraxispeterzhu2118
authored andcommitted
[Bug #20915] Fix SEGV with TracePoint#parameters and aliased C method
The following snippet results with a SEGV: ```ruby C = Class.new do alias_method :new_to_s, :to_s end TracePoint.new(:c_call, &:parameters).enable { C.new.new_to_s } ``` at MRI 3.3.6 and ruby 3.4.0dev The root cause of the issue lies in the `rb_tracearg_parameters` function within the `RUBY_EVENT_C_RETURN` branch. Specifically, when the invoked method is an alias for a C function, `rb_method_entry_without_refinements(..., trace_arg->called_id, ...)` may return NULL. In that case we can fallback to `trace_arg->id`.
1 parent 88764dd commit 660b995

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

test/ruby/test_settracefunc.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ def test_c_call_removed_method
9494
assert_equal([[:req]], parameters)
9595
end
9696

97+
def test_c_call_aliased_method
98+
# [Bug #20915]
99+
klass = Class.new do
100+
alias_method :new_method, :method
101+
end
102+
103+
instance = klass.new
104+
parameters = nil
105+
106+
TracePoint.new(:c_call) do |tp|
107+
parameters = tp.parameters
108+
end.enable { instance.new_method(:to_s) }
109+
110+
assert_equal([[:req]], parameters)
111+
end
112+
97113
def test_call
98114
events = []
99115
name = "#{self.class}\##{__method__}"

vm_trace.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,9 @@ rb_tracearg_parameters(rb_trace_arg_t *trace_arg)
937937
const rb_method_entry_t *me;
938938
VALUE iclass = Qnil;
939939
me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->called_id, &iclass);
940+
if (!me) {
941+
me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->id, &iclass);
942+
}
940943
return rb_unnamed_parameters(rb_method_entry_arity(me));
941944
}
942945
break;

0 commit comments

Comments
 (0)