diff options
author | Koichi Sasada <ko1@atdot.net> | 2023-02-10 16:02:20 +0900 |
---|---|---|
committer | Koichi Sasada <ko1@atdot.net> | 2023-02-10 17:55:25 +0900 |
commit | be94808282e50d3ecaa1392ffc38c9ec89e3438b (patch) | |
tree | f1e98d44d7743ce8785ac257282ba8082f831bd5 /vm.c | |
parent | 38ecf08ba16b7e8946ac414f4f8c7ee155b34083 (diff) | |
download | ruby-be94808282e50d3ecaa1392ffc38c9ec89e3438b.tar.gz |
use correct svar even if env is escaped
This patch is follo-up of 0a82bfe.
Without this patch, if env is escaped (Proc'ed), strange svar
can be touched.
This patch tracks escaped env and use it.
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 56 |
1 files changed, 39 insertions, 17 deletions
@@ -1291,17 +1291,41 @@ MJIT_FUNC_EXPORTED VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda) { VALUE procval; + enum imemo_type code_type = imemo_type(captured->code.val); if (!VM_ENV_ESCAPED_P(captured->ep)) { rb_control_frame_t *cfp = VM_CAPTURED_BLOCK_TO_CFP(captured); vm_make_env_object(ec, cfp); } + VM_ASSERT(VM_EP_IN_HEAP_P(ec, captured->ep)); - VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq) || - imemo_type_p(captured->code.val, imemo_ifunc)); + VM_ASSERT(code_type == imemo_iseq || code_type == imemo_ifunc); procval = vm_proc_create_from_captured(klass, captured, - imemo_type(captured->code.val) == imemo_iseq ? block_type_iseq : block_type_ifunc, FALSE, is_lambda); + code_type == imemo_iseq ? block_type_iseq : block_type_ifunc, + FALSE, is_lambda); + + if (code_type == imemo_ifunc) { + struct vm_ifunc *ifunc = (struct vm_ifunc *)captured->code.val; + if (ifunc->svar_lep) { + VALUE ep0 = ifunc->svar_lep[0]; + if (RB_TYPE_P(ep0, T_IMEMO) && imemo_type_p(ep0, imemo_env)) { + // `ep0 == imemo_env` means this ep is escaped to heap (in env object). + const rb_env_t *env = (const rb_env_t *)ep0; + ifunc->svar_lep = (VALUE *)env->ep; + } + else { + VM_ASSERT(FIXNUM_P(ep0)); + if (ep0 & VM_ENV_FLAG_ESCAPED) { + // ok. do nothing + } + else { + ifunc->svar_lep = NULL; + } + } + } + } + return procval; } @@ -1620,19 +1644,13 @@ rb_vm_invoke_proc_with_self(rb_execution_context_t *ec, rb_proc_t *proc, VALUE s /* special variable */ -static rb_control_frame_t * -vm_svar_frame(const rb_execution_context_t *ec, rb_control_frame_t *cfp) +VALUE * +rb_vm_svar_lep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp) { while (cfp->pc == 0) { if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_IFUNC) { struct vm_ifunc *ifunc = (struct vm_ifunc *)cfp->iseq; - rb_control_frame_t *owner_cfp = ifunc->owner_cfp; - if (cfp < owner_cfp) { - cfp = owner_cfp; - } - else { - return NULL; - } + return ifunc->svar_lep; } else { cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); @@ -1642,21 +1660,25 @@ vm_svar_frame(const rb_execution_context_t *ec, rb_control_frame_t *cfp) return NULL; } } - return cfp; + + if (cfp) { + return (VALUE *)VM_CF_LEP(cfp); + } + else { + return NULL; + } } static VALUE vm_cfp_svar_get(const rb_execution_context_t *ec, rb_control_frame_t *cfp, VALUE key) { - cfp = vm_svar_frame(ec, cfp); - return lep_svar_get(ec, cfp ? VM_CF_LEP(cfp) : 0, key); + return lep_svar_get(ec, rb_vm_svar_lep(ec, cfp), key); } static void vm_cfp_svar_set(const rb_execution_context_t *ec, rb_control_frame_t *cfp, VALUE key, const VALUE val) { - cfp = vm_svar_frame(ec, cfp); - lep_svar_set(ec, cfp ? VM_CF_LEP(cfp) : 0, key, val); + lep_svar_set(ec, rb_vm_svar_lep(ec, cfp), key, val); } static VALUE |