Skip to content
Browse files

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.
  • Loading branch information...
1 parent 7bb2da2 commit 137092768af325827f3d0764325713ec51387218 Suraj N. Kurapati committed Oct 24, 2009
Showing with 32 additions and 0 deletions.
  1. +21 −0 gc.c
  2. +11 −0 include/ruby/ruby.h
View
21 gc.c
@@ -22,6 +22,7 @@
#include "gc.h"
#include "constant.h"
#include <stdio.h>
+#include <assert.h>
#include <setjmp.h>
#include <sys/types.h>
@@ -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);
View
11 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 *);

0 comments on commit 1370927

Please sign in to comment.
Something went wrong with that request. Please try again.