From 137092768af325827f3d0764325713ec51387218 Mon Sep 17 00:00:00 2001 From: "Suraj N. Kurapati" Date: Sat, 24 Oct 2009 20:55:11 -0700 Subject: [PATCH] http://redmine.ruby-lang.org/issues/2294 * 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. --- gc.c | 21 +++++++++++++++++++++ include/ruby/ruby.h | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/gc.c b/gc.c index 490dd30cdfa6ff..11e8b0a946c707 100644 --- a/gc.c +++ b/gc.c @@ -22,6 +22,7 @@ #include "gc.h" #include "constant.h" #include +#include #include #include @@ -2105,6 +2106,24 @@ ready_to_gc(rb_objspace_t *objspace) return TRUE; } +static VALUE *ruby_stack_lower_bound = 0, *ruby_stack_upper_bound = 0; +static char ruby_stack_is_bound = 0; + +void +ruby_bind_stack(void *lower_bound, void *upper_bound) +{ + assert(upper_bound > lower_bound && lower_bound > 0); + ruby_stack_lower_bound = lower_bound; + ruby_stack_upper_bound = upper_bound; + ruby_stack_is_bound = 1; +} + +#define FIX_STACK_BOUNDS(start, end, th) \ + if (ruby_stack_is_bound && th == th->vm->main_thread) { \ + if (start < ruby_stack_lower_bound) { start = ruby_stack_lower_bound; } \ + if (end > ruby_stack_upper_bound) { end = ruby_stack_upper_bound; } \ + } + static void before_gc_sweep(rb_objspace_t *objspace) { @@ -2431,6 +2450,7 @@ mark_current_machine_context(rb_objspace_t *objspace, rb_thread_t *th) SET_STACK_END; GET_STACK_BOUNDS(stack_start, stack_end, 1); + FIX_STACK_BOUNDS(stack_start, stack_end, th); mark_locations_array(objspace, save_regs_gc_mark.v, numberof(save_regs_gc_mark.v)); @@ -2560,6 +2580,7 @@ rb_gc_mark_machine_stack(rb_thread_t *th) VALUE *stack_start, *stack_end; GET_STACK_BOUNDS(stack_start, stack_end, 0); + FIX_STACK_BOUNDS(stack_start, stack_end, th); rb_gc_mark_locations(stack_start, stack_end); #ifdef __ia64 rb_gc_mark_locations(th->machine_register_stack_start, th->machine_register_stack_end); diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 03b1a1d1c427ed..e973e15ec3f6b8 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1221,6 +1221,17 @@ void ruby_init_stack(volatile VALUE*); #define RUBY_INIT_STACK \ VALUE variable_in_this_stack_frame; \ ruby_init_stack(&variable_in_this_stack_frame); +/* + * Binds the stack of Ruby's main thread to the region of memory that spans + * inclusively from the given lower boundary to the given upper boundary: + * + * (lower) <= (stack pointer of Ruby's main thread) <= (upper) + * + * These boundaries do not protect Ruby's main thread against stack + * overflow and they do not apply to non-main Ruby threads (whose stacks + * are dynamically allocated and managed by the native Operating System). + */ +void ruby_bind_stack(void *lower_bound, void *upper_bound); void ruby_init(void); void *ruby_options(int, char**); int ruby_run_node(void *);