[前][次][番号順一覧][スレッド一覧]

ruby-changes:37517

From: naruse <ko1@a...>
Date: Sat, 14 Feb 2015 13:46:34 +0900 (JST)
Subject: [ruby-changes:37517] naruse:r49598 (ruby_2_2): merge revision(s) 49499, 49500, 49501, 49502, 49504, 49505, 49506, 49507: [Backport #10828]

naruse	2015-02-14 13:46:22 +0900 (Sat, 14 Feb 2015)

  New Revision: 49598

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49598

  Log:
    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]

  Modified directories:
    branches/ruby_2_2/
  Modified files:
    branches/ruby_2_2/ChangeLog
    branches/ruby_2_2/test/-ext-/symbol/test_inadvertent_creation.rb
    branches/ruby_2_2/version.h
    branches/ruby_2_2/vm_eval.c
    branches/ruby_2_2/vm_insnhelper.c
Index: ruby_2_2/ChangeLog
===================================================================
--- ruby_2_2/ChangeLog	(revision 49597)
+++ ruby_2_2/ChangeLog	(revision 49598)
@@ -1,3 +1,19 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ChangeLog#L1
+Sat Feb 14 13:27:41 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@m...>
+
+	* vm_insnhelper.c: Fix symbol leak with +send+ [Bug #10828]
+
 Sat Feb 14 08:53:50 2015  Shugo Maeda  <shugo@r...>
 
 	* class.c (method_entry_i, class_instance_method_list,
Index: ruby_2_2/vm_eval.c
===================================================================
--- ruby_2_2/vm_eval.c	(revision 49597)
+++ ruby_2_2/vm_eval.c	(revision 49598)
@@ -865,12 +865,22 @@ rb_funcall_with_block(VALUE recv, ID mid https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_eval.c#L865
     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 *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_eval.c#L894
 	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;
 }
 
 /*
Index: ruby_2_2/version.h
===================================================================
--- ruby_2_2/version.h	(revision 49597)
+++ ruby_2_2/version.h	(revision 49598)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1
 #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
Index: ruby_2_2/vm_insnhelper.c
===================================================================
--- ruby_2_2/vm_insnhelper.c	(revision 49597)
+++ ruby_2_2/vm_insnhelper.c	(revision 49598)
@@ -1499,6 +1499,19 @@ vm_call_bmethod(rb_thread_t *th, rb_cont https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_insnhelper.c#L1499
     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_con https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_insnhelper.c#L1541
 
     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_contr https://github.com/ruby/ruby/blob/trunk/ruby_2_2/vm_insnhelper.c#L1803
     }
     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);
Index: ruby_2_2/test/-ext-/symbol/test_inadvertent_creation.rb
===================================================================
--- ruby_2_2/test/-ext-/symbol/test_inadvertent_creation.rb	(revision 49597)
+++ ruby_2_2/test/-ext-/symbol/test_inadvertent_creation.rb	(revision 49598)
@@ -358,5 +358,73 @@ module Test_Symbol https://github.com/ruby/ruby/blob/trunk/ruby_2_2/test/-ext-/symbol/test_inadvertent_creation.rb#L358
       }
       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

Property changes on: ruby_2_2
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r49499-49502,49504-49507


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]