diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-02-14 04:46:22 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-02-14 04:46:22 +0000 |
commit | 43defa70006d957574578503365fef8d4d18c646 (patch) | |
tree | a72409ef2141abc2b0ed16b5da6533608e067499 | |
parent | 68c6739e965cc5e7b0dd57d787a69126be45b865 (diff) |
merge revision(s) 49499,49500,49501,49502,49504,49505,49506,49507: [Backport #10828]
* vm_insnhelper.c: Fix one type of symbol leak with +send+
* vm_insnhelper.c: Fix symbol leak with +send+ [Bug #10828]
* vm_insnhelper.c (ci_missing_reason): return the reason of method
missing in call info.
* vm_insnhelper.c (vm_call_opt_send): re-apply r49500 with the
proper missing reason. [Bug #10828]
* vm_eval.c (send_internal), vm_insnhelper.c (vm_call_opt_send):
convert String method name into a Symbol, as method_missing
method expects its first argument to be a Symbol. [Bug #10828]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@49598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 16 | ||||
-rw-r--r-- | test/-ext-/symbol/test_inadvertent_creation.rb | 68 | ||||
-rw-r--r-- | version.h | 2 | ||||
-rw-r--r-- | vm_eval.c | 39 | ||||
-rw-r--r-- | vm_insnhelper.c | 43 |
5 files changed, 145 insertions, 23 deletions
@@ -1,3 +1,19 @@ +Sat Feb 14 13:27:41 2015 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * vm_eval.c (send_internal), vm_insnhelper.c (vm_call_opt_send): + convert String method name into a Symbol, as method_missing + method expects its first argument to be a Symbol. [Bug #10828] + + * vm_insnhelper.c (ci_missing_reason): return the reason of method + missing in call info. + + * vm_insnhelper.c (vm_call_opt_send): re-apply r49500 with the + proper missing reason. [Bug #10828] + +Sat Feb 14 13:27:41 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca> + + * vm_insnhelper.c: Fix symbol leak with +send+ [Bug #10828] + Sat Feb 14 08:53:50 2015 Shugo Maeda <shugo@ruby-lang.org> * class.c (method_entry_i, class_instance_method_list, diff --git a/test/-ext-/symbol/test_inadvertent_creation.rb b/test/-ext-/symbol/test_inadvertent_creation.rb index b180d08b04..9262fc3396 100644 --- a/test/-ext-/symbol/test_inadvertent_creation.rb +++ b/test/-ext-/symbol/test_inadvertent_creation.rb @@ -358,5 +358,73 @@ module Test_Symbol } assert_not_pinneddown(name) end + + def assert_no_immortal_symbol_created(name) + name = noninterned_name(name) + yield(name) + assert_not_pinneddown(name) + end + + def assert_no_immortal_symbol_in_method_missing(name) + assert_no_immortal_symbol_created("send should not leak - #{name}") do |name| + assert_raise(NoMethodError) {yield(name)} + end + end + + def test_send_leak_string + assert_no_immortal_symbol_in_method_missing("str") do |name| + 42.send(name) + end + end + + def test_send_leak_symbol + assert_no_immortal_symbol_in_method_missing("sym") do |name| + 42.send(name.to_sym) + end + end + + def test_send_leak_string_custom_method_missing + x = Object.new + def x.method_missing(*); super; end + assert_no_immortal_symbol_in_method_missing("str mm") do |name| + x.send(name) + end + end + + def test_send_leak_symbol_custom_method_missing + x = Object.new + def x.method_missing(*); super; end + assert_no_immortal_symbol_in_method_missing("sym mm") do |name| + x.send(name.to_sym) + end + end + + def test_send_leak_string_no_optimization + assert_no_immortal_symbol_in_method_missing("str slow") do |name| + 42.method(:send).call(name) + end + end + + def test_send_leak_symbol_no_optimization + assert_no_immortal_symbol_in_method_missing("sym slow") do |name| + 42.method(:send).call(name.to_sym) + end + end + + def test_send_leak_string_custom_method_missing_no_optimization + x = Object.new + def x.method_missing(*); super; end + assert_no_immortal_symbol_in_method_missing("str mm slow") do |name| + x.method(:send).call(name) + end + end + + def test_send_leak_symbol_custom_method_missing_no_optimization + x = Object.new + def x.method_missing(*); super; end + assert_no_immortal_symbol_in_method_missing("sym mm slow") do |name| + x.method(:send).call(name.to_sym) + end + end end end @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.2.0" #define RUBY_RELEASE_DATE "2015-02-14" -#define RUBY_PATCHLEVEL 47 +#define RUBY_PATCHLEVEL 48 #define RUBY_RELEASE_YEAR 2015 #define RUBY_RELEASE_MONTH 2 @@ -865,12 +865,22 @@ rb_funcall_with_block(VALUE recv, ID mid, int argc, const VALUE *argv, VALUE pas return rb_call(recv, mid, argc, argv, CALL_PUBLIC); } +static VALUE * +current_vm_stack_arg(rb_thread_t *th, const VALUE *argv) +{ + rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp); + if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, prev_cfp)) return NULL; + if (prev_cfp->sp + 1 != argv) return NULL; + return prev_cfp->sp + 1; +} + static VALUE send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope) { ID id; VALUE vid; VALUE self; + VALUE ret, vargv = 0; rb_thread_t *th = GET_THREAD(); if (scope == CALL_PUBLIC) { @@ -884,19 +894,40 @@ send_internal(int argc, const VALUE *argv, VALUE recv, call_type scope) rb_raise(rb_eArgError, "no method name given"); } - vid = *argv++; argc--; + vid = *argv; id = rb_check_id(&vid); if (!id) { if (rb_method_basic_definition_p(CLASS_OF(recv), idMethodMissing)) { VALUE exc = make_no_method_exception(rb_eNoMethodError, NULL, - recv, ++argc, --argv); + recv, argc, argv); rb_exc_raise(exc); } - id = rb_to_id(vid); + if (!SYMBOL_P(*argv)) { + VALUE *tmp_argv = current_vm_stack_arg(th, argv); + vid = rb_str_intern(vid); + if (tmp_argv) { + tmp_argv[0] = vid; + } + else if (argc > 1) { + tmp_argv = ALLOCV_N(VALUE, vargv, argc); + tmp_argv[0] = vid; + MEMCPY(tmp_argv+1, argv+1, VALUE, argc-1); + argv = tmp_argv; + } + else { + argv = &vid; + } + } + id = idMethodMissing; + } + else { + argv++; argc--; } PASS_PASSED_BLOCK_TH(th); - return rb_call0(recv, id, argc, argv, scope, self); + ret = rb_call0(recv, id, argc, argv, scope, self); + ALLOCV_END(vargv); + return ret; } /* diff --git a/vm_insnhelper.c b/vm_insnhelper.c index f8939b6bf1..3fd3100d7c 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1499,6 +1499,19 @@ vm_call_bmethod(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) return vm_call_bmethod_body(th, ci, argv); } +static int +ci_missing_reason(const rb_call_info_t *ci) +{ + int stat = 0; + if (ci->flag & VM_CALL_VCALL) { + stat |= NOEX_VCALL; + } + if (ci->flag & VM_CALL_SUPER) { + stat |= NOEX_SUPER; + } + return stat; +} + static #ifdef _MSC_VER __forceinline @@ -1528,24 +1541,24 @@ vm_call_opt_send(rb_thread_t *th, rb_control_frame_t *reg_cfp, rb_call_info_t *c sym = TOPN(i); - if (SYMBOL_P(sym)) { - ci->mid = SYM2ID(sym); - } - else if (!(ci->mid = rb_check_id(&sym))) { + if (!(ci->mid = rb_check_id(&sym))) { if (rb_method_basic_definition_p(CLASS_OF(ci->recv), idMethodMissing)) { VALUE exc = make_no_method_exception(rb_eNoMethodError, NULL, ci->recv, rb_long2int(ci->argc), &TOPN(i)); rb_exc_raise(exc); } - ci->mid = rb_to_id(sym); + TOPN(i) = rb_str_intern(sym); + ci->mid = idMethodMissing; + th->method_missing_reason = ci->aux.missing_reason = ci_missing_reason(ci); } - - /* shift arguments */ - if (i > 0) { - MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i); + else { + /* shift arguments */ + if (i > 0) { + MEMMOVE(&TOPN(i), &TOPN(i-1), VALUE, i); + } + ci->argc -= 1; + DEC_SP(1); } ci->me = rb_method_entry_without_refinements(CLASS_OF(ci->recv), ci->mid, &ci->defined_class); - ci->argc -= 1; - DEC_SP(1); ci->flag = VM_CALL_FCALL | VM_CALL_OPT_SEND; @@ -1790,13 +1803,7 @@ vm_call_method(rb_thread_t *th, rb_control_frame_t *cfp, rb_call_info_t *ci) } else { /* method missing */ - int stat = 0; - if (ci->flag & VM_CALL_VCALL) { - stat |= NOEX_VCALL; - } - if (ci->flag & VM_CALL_SUPER) { - stat |= NOEX_SUPER; - } + const int stat = ci_missing_reason(ci); if (ci->mid == idMethodMissing) { rb_control_frame_t *reg_cfp = cfp; VALUE *argv = STACK_ADDR_FROM_TOP(ci->argc); |