merge revision(s) 660b995365: [Backport #20915]

[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`.
This commit is contained in:
Takashi Kokubun 2025-01-14 17:51:27 -08:00
parent 1e48631e0f
commit 745fe4cf7e
3 changed files with 20 additions and 1 deletions

View file

@ -93,6 +93,22 @@ class TestSetTraceFunc < Test::Unit::TestCase
assert_equal([[:req]], parameters)
end
def test_c_call_aliased_method
# [Bug #20915]
klass = Class.new do
alias_method :new_method, :method
end
instance = klass.new
parameters = nil
TracePoint.new(:c_call) do |tp|
parameters = tp.parameters
end.enable { instance.new_method(:to_s) }
assert_equal([[:req]], parameters)
end
def test_call
events = []
name = "#{self.class}\##{__method__}"

View file

@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 114
#define RUBY_PATCHLEVEL 115
#include "ruby/version.h"
#include "ruby/internal/abi.h"

View file

@ -937,6 +937,9 @@ rb_tracearg_parameters(rb_trace_arg_t *trace_arg)
const rb_method_entry_t *me;
VALUE iclass = Qnil;
me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->called_id, &iclass);
if (!me) {
me = rb_method_entry_without_refinements(trace_arg->klass, trace_arg->id, &iclass);
}
return rb_unnamed_parameters(rb_method_entry_arity(me));
}
break;