diff --git a/ChangeLog b/ChangeLog index 9f307aa015..ae07f5b4fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Tue Dec 25 22:06:33 2012 Koichi Sasada + + * 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 * lib/irb/init.rb, lib/irb/lc/ja/error.rb, lib/irb/lc/error.rb: diff --git a/test/ruby/test_settracefunc.rb b/test/ruby/test_settracefunc.rb index a9054378c8..ad99f1621d 100644 --- a/test/ruby/test_settracefunc.rb +++ b/test/ruby/test_settracefunc.rb @@ -848,4 +848,27 @@ class TestSetTraceFunc < Test::Unit::TestCase 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 diff --git a/vm.c b/vm.c index 9ed5d08fa2..a05f3690f5 100644 --- a/vm.c +++ b/vm.c @@ -1160,7 +1160,6 @@ vm_exec(rb_thread_t *th) _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) 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) 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 { diff --git a/vm_core.h b/vm_core.h index 48f887c03a..8a4dd239a1 100644 --- a/vm_core.h +++ b/vm_core.h @@ -962,8 +962,9 @@ struct rb_trace_arg_struct { }; 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(struct rb_trace_arg_struct *trace_arg); 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 diff --git a/vm_trace.c b/vm_trace.c index 743524b0b8..ad0fce0713 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -279,8 +279,8 @@ exec_hooks(rb_thread_t *th, rb_hook_list_t *list, const rb_trace_arg_t *trace_ar 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_arg_t *trace_arg) 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) {