From 9301ef5d0eac8fda440b021445a09c17397db5d1 Mon Sep 17 00:00:00 2001 From: ko1 Date: Thu, 1 Jun 2017 15:12:14 +0000 Subject: [PATCH] make Binding object WB protected. * proc.c (ruby_binding_data_type): set RUBY_TYPED_WB_PROTECTED. Insert write barriers (WBs) to all of writes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58988 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- proc.c | 25 ++++++++++---------- vm.c | 68 +++++++++++++++++++++++++++++++------------------------ vm_core.h | 8 ++++--- vm_eval.c | 2 +- 4 files changed, 58 insertions(+), 45 deletions(-) diff --git a/proc.c b/proc.c index 8ad703cbd3..c166fc9c0e 100644 --- a/proc.c +++ b/proc.c @@ -266,9 +266,7 @@ static void binding_free(void *ptr) { RUBY_FREE_ENTER("binding"); - ruby_xfree(ptr); - RUBY_FREE_LEAVE("binding"); } @@ -276,6 +274,7 @@ static void binding_mark(void *ptr) { rb_binding_t *bind = ptr; + RUBY_MARK_ENTER("binding"); block_mark(&bind->block); rb_gc_mark(bind->pathobj); @@ -295,7 +294,7 @@ const rb_data_type_t ruby_binding_data_type = { binding_free, binding_memsize, }, - 0, 0, RUBY_TYPED_FREE_IMMEDIATELY + 0, 0, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY }; VALUE @@ -307,6 +306,7 @@ rb_binding_alloc(VALUE klass) return obj; } + /* :nodoc: */ static VALUE binding_dup(VALUE self) @@ -315,8 +315,8 @@ binding_dup(VALUE self) rb_binding_t *src, *dst; GetBindingPtr(self, src); GetBindingPtr(bindval, dst); - dst->block = src->block; - dst->pathobj = src->pathobj; + rb_vm_block_copy(bindval, &dst->block, &src->block); + RB_OBJ_WRITE(bindval, &dst->pathobj, src->pathobj); dst->first_lineno = src->first_lineno; return bindval; } @@ -542,7 +542,7 @@ bind_local_variable_set(VALUE bindval, VALUE sym, VALUE val) env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block)); if ((ptr = get_local_variable_ptr(env, lid)) == NULL) { /* not found. create new env */ - ptr = rb_binding_add_dynavars(bind, 1, &lid); + ptr = rb_binding_add_dynavars(bindval, bind, 1, &lid); env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block)); } @@ -2766,18 +2766,19 @@ proc_binding(VALUE self) bindval = rb_binding_alloc(rb_cBinding); GetBindingPtr(bindval, bind); - - bind->block.as.captured.self = binding_self; - bind->block.as.captured.code.iseq = env->iseq; - bind->block.as.captured.ep = env->ep; + RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, binding_self); + RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq); + rb_vm_block_ep_update(bindval, &bind->block, env->ep); + RB_OBJ_WRITTEN(bindval, Qundef, VM_ENV_ENVVAL(env->ep)); if (iseq) { rb_iseq_check(iseq); - bind->pathobj = iseq->body->location.pathobj; + RB_OBJ_WRITE(bindval, &bind->pathobj, iseq->body->location.pathobj); bind->first_lineno = FIX2INT(rb_iseq_first_lineno(iseq)); } else { - bind->pathobj = rb_iseq_pathobj_new(rb_fstring_cstr("(binding)"), Qnil); + RB_OBJ_WRITE(bindval, &bind->pathobj, + rb_iseq_pathobj_new(rb_fstring_cstr("(binding)"), Qnil)); bind->first_lineno = 1; } diff --git a/vm.c b/vm.c index 3b9fd5ec95..6eda8d1450 100644 --- a/vm.c +++ b/vm.c @@ -262,12 +262,19 @@ vm_cref_dump(const char *mesg, const rb_cref_t *cref) } } +void +rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep) +{ + *((const VALUE **)&dst->as.captured.ep) = ep; + RB_OBJ_WRITTEN(obj, Qundef, VM_ENV_ENVVAL(ep)); +} + static void -vm_bind_update_env(rb_binding_t *bind, VALUE envval) +vm_bind_update_env(VALUE bindval, rb_binding_t *bind, VALUE envval) { const rb_env_t *env = (rb_env_t *)envval; - bind->block.as.captured.code.iseq = env->iseq; - bind->block.as.captured.ep = env->ep; + RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq); + rb_vm_block_ep_update(bindval, &bind->block, env->ep); } #if VM_COLLECT_USAGE_DETAILS @@ -467,7 +474,7 @@ vm_set_main_stack(rb_thread_t *th, const rb_iseq_t *iseq) /* save binding */ if (iseq->body->local_table_size > 0) { - vm_bind_update_env(bind, vm_make_env_object(th, th->ec.cfp)); + vm_bind_update_env(toplevel_binding, bind, vm_make_env_object(th, th->ec.cfp)); } } @@ -806,8 +813,7 @@ rb_proc_create_from_captured(VALUE klass, /* copy block */ RB_OBJ_WRITE(procval, &proc->block.as.captured.self, captured->self); RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, captured->code.val); - *((const VALUE **)&proc->block.as.captured.ep) = captured->ep; - RB_OBJ_WRITTEN(procval, Qundef, VM_ENV_ENVVAL(captured->ep)); + rb_vm_block_ep_update(procval, &proc->block, captured->ep); vm_block_type_set(&proc->block, block_type); proc->safe_level = safe_level; @@ -817,6 +823,26 @@ rb_proc_create_from_captured(VALUE klass, return procval; } +void +rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src) +{ + /* copy block */ + switch (vm_block_type(src)) { + case block_type_iseq: + case block_type_ifunc: + RB_OBJ_WRITE(obj, &dst->as.captured.self, src->as.captured.self); + RB_OBJ_WRITE(obj, &dst->as.captured.code.val, src->as.captured.code.val); + rb_vm_block_ep_update(obj, dst, src->as.captured.ep); + break; + case block_type_symbol: + RB_OBJ_WRITE(obj, &dst->as.symbol, src->as.symbol); + break; + case block_type_proc: + RB_OBJ_WRITE(obj, &dst->as.proc, src->as.proc); + break; + } +} + VALUE rb_proc_create(VALUE klass, const struct rb_block *block, int8_t safe_level, int8_t is_from_method, int8_t is_lambda) @@ -825,23 +851,7 @@ rb_proc_create(VALUE klass, const struct rb_block *block, rb_proc_t *proc = RTYPEDDATA_DATA(procval); VM_ASSERT(VM_EP_IN_HEAP_P(GET_THREAD(), vm_block_ep(block))); - - /* copy block */ - switch (vm_block_type(block)) { - case block_type_iseq: - case block_type_ifunc: - RB_OBJ_WRITE(procval, &proc->block.as.captured.self, block->as.captured.self); - RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, block->as.captured.code.val); - *((const VALUE **)&proc->block.as.captured.ep) = block->as.captured.ep; - RB_OBJ_WRITTEN(procval, Qundef, VM_ENV_ENVVAL(block->as.captured.ep)); - break; - case block_type_symbol: - RB_OBJ_WRITE(procval, &proc->block.as.symbol, block->as.symbol); - break; - case block_type_proc: - RB_OBJ_WRITE(procval, &proc->block.as.proc, block->as.proc); - break; - } + rb_vm_block_copy(procval, &proc->block, block); vm_block_type_set(&proc->block, block->type); proc->safe_level = safe_level; proc->is_from_method = is_from_method; @@ -899,17 +909,17 @@ rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp) bindval = rb_binding_alloc(rb_cBinding); GetBindingPtr(bindval, bind); - vm_bind_update_env(bind, envval); - bind->block.as.captured.self = cfp->self; - bind->block.as.captured.code.iseq = cfp->iseq; - bind->pathobj = ruby_level_cfp->iseq->body->location.pathobj; + vm_bind_update_env(bindval, bind, envval); + RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, cfp->self); + RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, cfp->iseq); + RB_OBJ_WRITE(bindval, &bind->pathobj, ruby_level_cfp->iseq->body->location.pathobj); bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp); return bindval; } const VALUE * -rb_binding_add_dynavars(rb_binding_t *bind, int dyncount, const ID *dynvars) +rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars) { VALUE envval, pathobj = bind->pathobj; VALUE path = pathobj_path(pathobj); @@ -944,7 +954,7 @@ rb_binding_add_dynavars(rb_binding_t *bind, int dyncount, const ID *dynvars) ALLOCV_END(idtmp); vm_set_eval_stack(th, iseq, 0, base_block); - vm_bind_update_env(bind, envval = vm_make_env_object(th, th->ec.cfp)); + vm_bind_update_env(bindval, bind, envval = vm_make_env_object(th, th->ec.cfp)); rb_vm_pop_frame(th); env = (const rb_env_t *)envval; diff --git a/vm_core.h b/vm_core.h index 6a5f10d0bd..bb9d58e903 100644 --- a/vm_core.h +++ b/vm_core.h @@ -914,8 +914,8 @@ extern const rb_data_type_t ruby_binding_data_type; GetCoreDataFromValue((obj), rb_binding_t, (ptr)) typedef struct { - struct rb_block block; - VALUE pathobj; + const struct rb_block block; + const VALUE pathobj; unsigned short first_lineno; } rb_binding_t; @@ -1195,6 +1195,8 @@ VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v) const VALUE *rb_vm_ep_local_ep(const VALUE *ep); const VALUE *rb_vm_proc_local_ep(VALUE proc); +void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep); +void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src); VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp); @@ -1458,7 +1460,7 @@ VALUE rb_vm_make_proc(rb_thread_t *th, const struct rb_captured_block *captured, VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp); VALUE rb_vm_env_local_variables(const rb_env_t *env); const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env); -const VALUE *rb_binding_add_dynavars(rb_binding_t *bind, int dyncount, const ID *dynvars); +const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars); void rb_vm_inc_const_missing_count(void); void rb_vm_gvl_destroy(rb_vm_t *vm); VALUE rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc, diff --git a/vm_eval.c b/vm_eval.c index 9ef9f17549..bf7d47c4a8 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1360,7 +1360,7 @@ eval_string_with_cref(VALUE self, VALUE src, VALUE scope, rb_cref_t *const cref_ /* save new env */ if (bind && iseq->body->local_table_size > 0) { - vm_bind_update_env(bind, vm_make_env_object(th, th->ec.cfp)); + vm_bind_update_env(scope, bind, vm_make_env_object(th, th->ec.cfp)); } }