From 3af9033cdad3a9ed6fff662121d297290c9b2410 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. --- ChangeLog | 7 +++++++ gc.c | 21 +++++++++++++++++++++ include/ruby/ruby.h | 11 +++++++++++ 3 files changed, 39 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index efc2edf..9190d3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -402,6 +402,13 @@ 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. + 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 737b124..bd44418 100644 --- a/gc.c +++ b/gc.c @@ -20,6 +20,7 @@ #include "vm_core.h" #include "gc.h" #include +#include #include #include @@ -2099,6 +2100,24 @@ void rb_vm_mark(void *ptr); (start = STACK_END, end = STACK_START) : (start = STACK_START, end = STACK_END+appendix)) #endif +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 mark_current_machine_context(rb_objspace_t *objspace, rb_thread_t *th) { @@ -2111,6 +2130,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, (VALUE*)save_regs_gc_mark, @@ -2218,6 +2238,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 55836a8..58328aa 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1143,6 +1143,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 *); -- 1.6.5.2