ruby-changes:46873
From: ko1 <ko1@a...>
Date: Fri, 2 Jun 2017 00:12:19 +0900 (JST)
Subject: [ruby-changes:46873] ko1:r58988 (trunk): make Binding object WB protected.
ko1 2017-06-02 00:12:14 +0900 (Fri, 02 Jun 2017) New Revision: 58988 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58988 Log: make Binding object WB protected. * proc.c (ruby_binding_data_type): set RUBY_TYPED_WB_PROTECTED. Insert write barriers (WBs) to all of writes. Modified files: trunk/proc.c trunk/vm.c trunk/vm_core.h trunk/vm_eval.c Index: vm_core.h =================================================================== --- vm_core.h (revision 58987) +++ vm_core.h (revision 58988) @@ -914,8 +914,8 @@ extern const rb_data_type_t ruby_binding https://github.com/ruby/ruby/blob/trunk/vm_core.h#L914 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 https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1195 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, c https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1460 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, Index: vm_eval.c =================================================================== --- vm_eval.c (revision 58987) +++ vm_eval.c (revision 58988) @@ -1360,7 +1360,7 @@ eval_string_with_cref(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1360 /* 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)); } } Index: proc.c =================================================================== --- proc.c (revision 58987) +++ proc.c (revision 58988) @@ -266,9 +266,7 @@ static void https://github.com/ruby/ruby/blob/trunk/proc.c#L266 binding_free(void *ptr) { RUBY_FREE_ENTER("binding"); - ruby_xfree(ptr); - RUBY_FREE_LEAVE("binding"); } @@ -276,6 +274,7 @@ static void https://github.com/ruby/ruby/blob/trunk/proc.c#L274 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_t https://github.com/ruby/ruby/blob/trunk/proc.c#L294 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) https://github.com/ruby/ruby/blob/trunk/proc.c#L306 return obj; } + /* :nodoc: */ static VALUE binding_dup(VALUE self) @@ -315,8 +315,8 @@ binding_dup(VALUE self) https://github.com/ruby/ruby/blob/trunk/proc.c#L315 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, V https://github.com/ruby/ruby/blob/trunk/proc.c#L542 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) https://github.com/ruby/ruby/blob/trunk/proc.c#L2766 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; } Index: vm.c =================================================================== --- vm.c (revision 58987) +++ vm.c (revision 58988) @@ -262,12 +262,19 @@ vm_cref_dump(const char *mesg, const rb_ https://github.com/ruby/ruby/blob/trunk/vm.c#L262 } } +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 https://github.com/ruby/ruby/blob/trunk/vm.c#L474 /* 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 https://github.com/ruby/ruby/blob/trunk/vm.c#L813 /* 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,31 +823,35 @@ rb_proc_create_from_captured(VALUE klass https://github.com/ruby/ruby/blob/trunk/vm.c#L823 return procval; } -VALUE -rb_proc_create(VALUE klass, const struct rb_block *block, - int8_t safe_level, int8_t is_from_method, int8_t is_lambda) +void +rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src) { - VALUE procval = rb_proc_alloc(klass); - 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)) { + switch (vm_block_type(src)) { 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)); + 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(procval, &proc->block.as.symbol, block->as.symbol); + RB_OBJ_WRITE(obj, &dst->as.symbol, src->as.symbol); break; case block_type_proc: - RB_OBJ_WRITE(procval, &proc->block.as.proc, block->as.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) +{ + VALUE procval = rb_proc_alloc(klass); + rb_proc_t *proc = RTYPEDDATA_DATA(procval); + + VM_ASSERT(VM_EP_IN_HEAP_P(GET_THREAD(), vm_block_ep(block))); + 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, cons https://github.com/ruby/ruby/blob/trunk/vm.c#L909 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 *bi https://github.com/ruby/ruby/blob/trunk/vm.c#L954 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; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/