From 08e6389d6e3e027fd8821ce63bda52821d68bec6 Mon Sep 17 00:00:00 2001 From: Suraj N. Kurapati Date: Sat, 24 Oct 2009 20:55:11 -0700 Subject: [PATCH 2/2] * 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 | 22 ++++++++++++++++++++++ include/ruby/ruby.h | 6 ++++++ 3 files changed, 35 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 01fae7a..980c729 100644 --- a/ChangeLog +++ b/ChangeLog @@ -313,6 +313,13 @@ Sun Oct 25 13:33:58 2009 Nobuyoshi Nakada 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. + +Mon Oct 25 13:28:00 2009 Suraj N. Kurapati + * gc.c: refactor common code from mark_current_machine_context() and rb_gc_mark_machine_stack() into get_machine_stack_bounds(). diff --git a/gc.c b/gc.c index 0b588dc..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,17 @@ 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) { @@ -2108,6 +2120,16 @@ get_machine_stack_bounds(rb_thread_t *th, VALUE **stack_start, VALUE **stack_end *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 diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 55836a8..957b953 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1143,6 +1143,12 @@ 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 Ruby's stack to the region of memory that spans from the given + * "lower" boundary up to (but not including) the given "upper" boundary. + * Note that these boundaries _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