Skip to content

Commit

Permalink
http://redmine.ruby-lang.org/issues/2294
Browse files Browse the repository at this point in the history
* 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
sunaku committed Nov 8, 2011
1 parent 7bb2da2 commit 1370927
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gc.c
Expand Up @@ -22,6 +22,7 @@
#include "gc.h"
#include "constant.h"
#include <stdio.h>
#include <assert.h>
#include <setjmp.h>
#include <sys/types.h>

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions include/ruby/ruby.h
Expand Up @@ -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 *);
Expand Down

0 comments on commit 1370927

Please sign in to comment.