From 331592b132c0e4d7124568d7d2aca7b71e892492 Mon Sep 17 00:00:00 2001 From: Suraj N. Kurapati Date: Sat, 24 Oct 2009 20:55:11 -0700 Subject: [PATCH] * include/ruby/ruby.h: declare ruby_bind_stack(). [ruby-core:26361] * gc.c: implement ruby_bind_stack(). restrict GC marking region to ruby_bind_stack() boundaries for main thread. refactor common code from mark_current_machine_context() and rb_gc_mark_machine_stack() into get_machine_stack_bounds(). --- ChangeLog | 9 ++++++ gc.c | 77 ++++++++++++++++++++++++++++++++------------------ include/ruby/ruby.h | 8 +++++ 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3072677..e9c2b88 100644 --- a/ChangeLog +++ b/ChangeLog @@ -165,6 +165,15 @@ Sun Oct 25 13:33:58 2009 Nobuyoshi Nakada * io.c (io_cntl): F_DUPFD is platform dependent. +Mon Oct 25 13:28:00 2009 Suraj N. Kurapati + + * include/ruby/ruby.h: declare ruby_bind_stack(). [ruby-core:26361] + + * gc.c: implement ruby_bind_stack(). restrict GC marking + region to ruby_bind_stack() boundaries for main thread. + refactor common code from mark_current_machine_context() + and rb_gc_mark_machine_stack() into get_machine_stack_bounds(). + Sun Oct 25 10:19:09 2009 Nobuyoshi Nakada * ext/dl/handle.c (rb_dlhandle_close): fixed an invalid local diff --git a/gc.c b/gc.c index 2568d42..4f47267 100644 --- a/gc.c +++ b/gc.c @@ -19,6 +19,7 @@ #include "eval_intern.h" #include "vm_core.h" #include "gc.h" +#include #include #include #include @@ -2089,6 +2090,48 @@ obj_free(rb_objspace_t *objspace, VALUE obj) void rb_vm_mark(void *ptr); +static VALUE *ruby_stack_lower_bound = 0, + *ruby_stack_upper_bound = 0; + +void +ruby_bind_stack(void *lower, void *upper) +{ + assert(upper > lower); + ruby_stack_lower_bound = lower; + ruby_stack_upper_bound = upper; +} + +static void +get_machine_stack_bounds(rb_thread_t *th, VALUE **stack_start, VALUE **stack_end, unsigned stack_end_increment) +{ +#if STACK_GROW_DIRECTION < 0 + *stack_start = th->machine_stack_end; + *stack_end = th->machine_stack_start; +#elif STACK_GROW_DIRECTION > 0 + *stack_start = th->machine_stack_start; + *stack_end = th->machine_stack_end + stack_end_increment; +#else + if (th->machine_stack_end < th->machine_stack_start) { + *stack_start = th->machine_stack_end; + *stack_end = th->machine_stack_start; + } + else { + *stack_start = th->machine_stack_start; + *stack_end = th->machine_stack_end + stack_end_increment; + } +#endif + + if (th == th->vm->main_thread) { + if (ruby_stack_lower_bound && *stack_start < ruby_stack_lower_bound) { + *stack_start = ruby_stack_lower_bound; + } + + if (ruby_stack_upper_bound && *stack_end > ruby_stack_upper_bound) { + *stack_end = ruby_stack_upper_bound; + } + } +} + static void mark_current_machine_context(rb_objspace_t *objspace, rb_thread_t *th) { @@ -2100,22 +2143,7 @@ mark_current_machine_context(rb_objspace_t *objspace, rb_thread_t *th) rb_setjmp(save_regs_gc_mark); SET_STACK_END; -#if STACK_GROW_DIRECTION < 0 - stack_start = th->machine_stack_end; - stack_end = th->machine_stack_start; -#elif STACK_GROW_DIRECTION > 0 - stack_start = th->machine_stack_start; - stack_end = th->machine_stack_end + 1; -#else - if (th->machine_stack_end < th->machine_stack_start) { - stack_start = th->machine_stack_end; - stack_end = th->machine_stack_start; - } - else { - stack_start = th->machine_stack_start; - stack_end = th->machine_stack_end + 1; - } -#endif + get_machine_stack_bounds(th, &stack_start, &stack_end, 1); mark_locations_array(objspace, (VALUE*)save_regs_gc_mark, @@ -2219,19 +2247,12 @@ rb_garbage_collect(void) void rb_gc_mark_machine_stack(rb_thread_t *th) { + VALUE *stack_start, *stack_end; + get_machine_stack_bounds(th, &stack_start, &stack_end, 0); + rb_objspace_t *objspace = &rb_objspace; -#if STACK_GROW_DIRECTION < 0 - rb_gc_mark_locations(th->machine_stack_end, th->machine_stack_start); -#elif STACK_GROW_DIRECTION > 0 - rb_gc_mark_locations(th->machine_stack_start, th->machine_stack_end); -#else - if (th->machine_stack_start < th->machine_stack_end) { - rb_gc_mark_locations(th->machine_stack_start, th->machine_stack_end); - } - else { - rb_gc_mark_locations(th->machine_stack_end, th->machine_stack_start); - } -#endif + rb_gc_mark_locations(stack_start, stack_end); + #ifdef __ia64 rb_gc_mark_locations(th->machine_register_stack_start, th->machine_register_stack_end); #endif diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 19f146e..2b6d225 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1138,6 +1138,14 @@ void ruby_init_stack(volatile VALUE*); #define RUBY_INIT_STACK \ VALUE variable_in_this_stack_frame; \ ruby_init_stack(&variable_in_this_stack_frame); +/* + * Bind Ruby's stack to the region of memory that + * spans inclusively from the given "lower" pointer + * up to the given "upper" pointer. These pointers + * _do not_ define the top or bottom of Ruby's stack, + * and they _do not_ protect against stack overflow. + */ +void ruby_bind_stack(void *lower, void *upper); void ruby_init(void); void *ruby_options(int, char**); int ruby_run_node(void *); -- 1.6.5.1