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

ruby-changes:26550

From: ko1 <ko1@a...>
Date: Tue, 25 Dec 2012 22:24:29 +0900 (JST)
Subject: [ruby-changes:26550] ko1:r38601 (trunk): * vm_trace.c (rb_threadptr_exec_event_hooks_and_pop_frame):

ko1	2012-12-25 22:24:17 +0900 (Tue, 25 Dec 2012)

  New Revision: 38601

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

  Log:
    * vm_trace.c (rb_threadptr_exec_event_hooks_and_pop_frame):
      pop a frame before JUMP_TAG() if exception occurred.
      This change fix bug of Ruby 1.9.
      [ruby-core:51128] [ruby-trunk - Bug #7624]
    * vm_core.h (EXEC_EVENT_HOOK_AND_POP_FRAME): add to use
      `rb_threadptr_exec_event_hooks_and_pop_frame()'.
    * vm.c (vm_exec): use EXEC_EVENT_HOOK_AND_POP_FRAME() while
      exception handling. While exception hadnling, if an exception
      is raised in hooks, need to pop current frame and raise this
      raised exception by hook.
    * test/ruby/test_settracefunc.rb: add a test.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_settracefunc.rb
    trunk/vm.c
    trunk/vm_core.h
    trunk/vm_trace.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38600)
+++ ChangeLog	(revision 38601)
@@ -1,3 +1,20 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Dec 25 22:06:33 2012  Koichi Sasada  <ko1@a...>
+
+	* vm_trace.c (rb_threadptr_exec_event_hooks_and_pop_frame):
+	  pop a frame before JUMP_TAG() if exception occurred.
+	  This change fix bug of Ruby 1.9.
+	  [ruby-core:51128] [ruby-trunk - Bug #7624]
+
+	* vm_core.h (EXEC_EVENT_HOOK_AND_POP_FRAME): add to use
+	  `rb_threadptr_exec_event_hooks_and_pop_frame()'.
+
+	* vm.c (vm_exec): use EXEC_EVENT_HOOK_AND_POP_FRAME() while
+	  exception handling. While exception hadnling, if an exception
+	  is raised in hooks, need to pop current frame and raise this
+	  raised exception by hook.
+
+	* test/ruby/test_settracefunc.rb: add a test.
+
 Tue Dec 25 21:08:53 2012  Keiju Ishitsuka  <keiju@i...>
 
 	* lib/irb/init.rb, lib/irb/lc/ja/error.rb, lib/irb/lc/error.rb:
Index: vm_core.h
===================================================================
--- vm_core.h	(revision 38600)
+++ vm_core.h	(revision 38601)
@@ -962,8 +962,9 @@ struct rb_trace_arg_struct { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L962
 };
 
 void rb_threadptr_exec_event_hooks(struct rb_trace_arg_struct *trace_arg);
+void rb_threadptr_exec_event_hooks_and_pop_frame(struct rb_trace_arg_struct *trace_arg);
 
-#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, klass_, data_) do { \
+#define EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, pop_p_) do { \
     if (UNLIKELY(ruby_vm_event_flags & (flag_))) { \
 	if (((th)->event_hooks.events | (th)->vm->event_hooks.events) & (flag_)) { \
 	    struct rb_trace_arg_struct trace_arg; \
@@ -976,11 +977,18 @@ void rb_threadptr_exec_event_hooks(struc https://github.com/ruby/ruby/blob/trunk/vm_core.h#L977
 	    trace_arg.data = (data_); \
 	    trace_arg.path = Qundef; \
 	    trace_arg.klass_solved = 0; \
-	    rb_threadptr_exec_event_hooks(&trace_arg); \
+	    if (pop_p_) rb_threadptr_exec_event_hooks_and_pop_frame(&trace_arg); \
+	    else rb_threadptr_exec_event_hooks(&trace_arg); \
 	} \
     } \
 } while (0)
 
+#define EXEC_EVENT_HOOK(th_, flag_, self_, id_, klass_, data_) \
+  EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, 0)
+
+#define EXEC_EVENT_HOOK_AND_POP_FRAME(th_, flag_, self_, id_, klass_, data_) \
+  EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, 1)
+
 #if defined __GNUC__ && __GNUC__ >= 4
 #pragma GCC visibility push(default)
 #endif
Index: vm_trace.c
===================================================================
--- vm_trace.c	(revision 38600)
+++ vm_trace.c	(revision 38601)
@@ -279,8 +279,8 @@ exec_hooks(rb_thread_t *th, rb_hook_list https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L279
     return state;
 }
 
-void
-rb_threadptr_exec_event_hooks(rb_trace_arg_t *trace_arg)
+static void
+rb_threadptr_exec_event_hooks_orig(rb_trace_arg_t *trace_arg, int pop_p)
 {
     rb_thread_t *th = trace_arg->th;
     if (th->trace_arg == 0 &&
@@ -316,12 +316,25 @@ rb_threadptr_exec_event_hooks(rb_trace_a https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L316
 	th->vm->trace_running--;
 
 	if (state) {
+	    if (pop_p) th->cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp);
 	    TH_JUMP_TAG(th, state);
 	}
 	th->state = outer_state;
     }
 }
 
+void
+rb_threadptr_exec_event_hooks_and_pop_frame(rb_trace_arg_t *trace_arg)
+{
+    rb_threadptr_exec_event_hooks_orig(trace_arg, 1);
+}
+
+void
+rb_threadptr_exec_event_hooks(rb_trace_arg_t *trace_arg)
+{
+    rb_threadptr_exec_event_hooks_orig(trace_arg, 0);
+}
+
 VALUE
 rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg)
 {
Index: vm.c
===================================================================
--- vm.c	(revision 38600)
+++ vm.c	(revision 38601)
@@ -1160,7 +1160,6 @@ vm_exec(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/vm.c#L1160
     _tag.retval = Qnil;
     if ((state = EXEC_TAG()) == 0) {
       vm_loop_start:
-	_th->tag = &_tag;
 	result = vm_exec_core(th, initial);
 	if ((state = th->state) != 0) {
 	    err = result;
@@ -1179,7 +1178,6 @@ vm_exec(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/vm.c#L1178
 	err = th->errinfo;
 
       exception_handler:
-	TH_POP_TAG2();
 	cont_pc = cont_sp = catch_iseqval = 0;
 
 	while (th->cfp->pc == 0 || th->cfp->iseq == 0) {
@@ -1360,19 +1358,20 @@ vm_exec(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/vm.c#L1358
 	    switch (VM_FRAME_TYPE(th->cfp)) {
 	      case VM_FRAME_MAGIC_METHOD:
 		RUBY_DTRACE_METHOD_RETURN_HOOK(th, 0, 0);
-		EXEC_EVENT_HOOK(th, RUBY_EVENT_RETURN, th->cfp->self, 0, 0, Qnil);
+		EXEC_EVENT_HOOK_AND_POP_FRAME(th, RUBY_EVENT_RETURN, th->cfp->self, 0, 0, Qnil);
 		break;
 	      case VM_FRAME_MAGIC_BLOCK:
-		EXEC_EVENT_HOOK(th, RUBY_EVENT_B_RETURN, th->cfp->self, 0, 0, Qnil);
+		EXEC_EVENT_HOOK_AND_POP_FRAME(th, RUBY_EVENT_B_RETURN, th->cfp->self, 0, 0, Qnil);
 		break;
 	      case VM_FRAME_MAGIC_CLASS:
-		EXEC_EVENT_HOOK(th, RUBY_EVENT_END, th->cfp->self, 0, 0, Qnil);
+		EXEC_EVENT_HOOK_AND_POP_FRAME(th, RUBY_EVENT_END, th->cfp->self, 0, 0, Qnil);
 		break;
 	    }
 
 	    if (VM_FRAME_TYPE_FINISH_P(th->cfp)) {
 		vm_pop_frame(th);
 		th->errinfo = err;
+		TH_POP_TAG2();
 		JUMP_TAG(state);
 	    }
 	    else {
Index: test/ruby/test_settracefunc.rb
===================================================================
--- test/ruby/test_settracefunc.rb	(revision 38600)
+++ test/ruby/test_settracefunc.rb	(revision 38601)
@@ -848,4 +848,27 @@ class TestSetTraceFunc < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/ruby/test_settracefunc.rb#L848
       end
     end
   end
+
+  class FOO_ERROR < RuntimeError; end
+  class BAR_ERROR < RuntimeError; end
+  def m1_test_trace_point_at_return_when_exception
+    m2_test_trace_point_at_return_when_exception
+  end
+  def m2_test_trace_point_at_return_when_exception
+    raise BAR_ERROR
+  end
+
+  def test_trace_point_at_return_when_exception
+    bug_7624 = '[ruby-core:51128] [ruby-trunk - Bug #7624]'
+    TracePoint.new{|tp|
+      if tp.event == :return &&
+        tp.method_id == :m2_test_trace_point_at_return_when_exception
+        raise FOO_ERROR
+      end
+    }.enable do
+      assert_raise(FOO_ERROR, bug_7624) do
+        m1_test_trace_point_at_return_when_exception
+      end
+    end
+  end
 end

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

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