diff options
Diffstat (limited to 'iseq.c')
| -rw-r--r-- | iseq.c | 512 |
1 files changed, 335 insertions, 177 deletions
@@ -19,6 +19,7 @@ #endif #include "eval_intern.h" +#include "id.h" #include "id_table.h" #include "internal.h" #include "internal/bits.h" @@ -38,6 +39,7 @@ #include "iseq.h" #include "ruby/util.h" #include "vm_core.h" +#include "ractor_core.h" #include "vm_callinfo.h" #include "yjit.h" #include "ruby/ractor.h" @@ -86,7 +88,7 @@ free_arena(struct iseq_compile_data_storage *cur) while (cur) { next = cur->next; - ruby_xfree(cur); + ruby_xfree_sized(cur, offsetof(struct iseq_compile_data_storage, buff) + cur->size * sizeof(char)); cur = next; } } @@ -100,7 +102,7 @@ compile_data_free(struct iseq_compile_data *compile_data) if (compile_data->ivar_cache_table) { rb_id_table_free(compile_data->ivar_cache_table); } - ruby_xfree(compile_data); + SIZED_FREE(compile_data); } } @@ -111,14 +113,14 @@ remove_from_constant_cache(ID id, IC ic) VALUE lookup_result; st_data_t ic_data = (st_data_t)ic; - if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) { + if (rb_id_table_lookup(&vm->constant_cache, id, &lookup_result)) { set_table *ics = (set_table *)lookup_result; set_table_delete(ics, &ic_data); if (ics->num_entries == 0 && // See comment in vm_track_constant_cache on why we need this check id != vm->inserting_constant_cache_id) { - rb_id_table_delete(vm->constant_cache, id); + rb_id_table_delete(&vm->constant_cache, id); set_free_table(ics); } } @@ -150,14 +152,33 @@ iseq_clear_ic_references(const rb_iseq_t *iseq) if (segments == NULL) continue; - for (int i = 0; segments[i]; i++) { + int i; + for (i = 0; segments[i]; i++) { ID id = segments[i]; if (id == idNULL) continue; remove_from_constant_cache(id, ic); } - ruby_xfree((void *)segments); + SIZED_FREE_N(segments, i + 1); + } +} + + +rb_hook_list_t * +rb_iseq_local_hooks(const rb_iseq_t *iseq, rb_ractor_t *r, bool create) +{ + rb_hook_list_t *hook_list = NULL; + st_data_t val; + if (st_lookup(rb_ractor_targeted_hooks(r), (st_data_t)iseq, &val)) { + hook_list = (rb_hook_list_t*)val; + RUBY_ASSERT(hook_list->type == hook_list_type_targeted_iseq); + } + else if (create) { + hook_list = RB_ZALLOC(rb_hook_list_t); + hook_list->type = hook_list_type_targeted_iseq; + st_insert(rb_ractor_targeted_hooks(r), (st_data_t)iseq, (st_data_t)hook_list); } + return hook_list; } void @@ -175,39 +196,44 @@ rb_iseq_free(const rb_iseq_t *iseq) rb_yjit_live_iseq_count--; } #endif - ruby_xfree((void *)body->iseq_encoded); - ruby_xfree((void *)body->insns_info.body); - ruby_xfree((void *)body->insns_info.positions); +#if USE_ZJIT + rb_zjit_iseq_free(iseq); +#endif + SIZED_FREE_N(body->iseq_encoded, body->iseq_size); + SIZED_FREE_N(body->insns_info.body, body->insns_info.size); + SIZED_FREE_N(body->insns_info.positions, body->insns_info.size); #if VM_INSN_INFO_TABLE_IMPL == 2 ruby_xfree(body->insns_info.succ_index_table); #endif - ruby_xfree((void *)body->is_entries); - ruby_xfree(body->call_data); - ruby_xfree((void *)body->catch_table); - ruby_xfree((void *)body->param.opt_table); + SIZED_FREE_N(body->is_entries, ISEQ_IS_SIZE(body)); + SIZED_FREE_N(body->call_data, body->ci_size); + if (body->catch_table) { + ruby_xfree_sized(body->catch_table, iseq_catch_table_bytes(body->catch_table->size)); + } + SIZED_FREE_N(body->param.opt_table, body->param.opt_num + 1); if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) { - ruby_xfree((void *)body->mark_bits.list); + SIZED_FREE_N(body->mark_bits.list, ISEQ_MBITS_BUFLEN(body->iseq_size)); } - ruby_xfree(body->variable.original_iseq); + ISEQ_ORIGINAL_ISEQ_CLEAR(iseq); - if (body->param.keyword != NULL) { - if (body->param.keyword->table != &body->local_table[body->param.keyword->bits_start - body->param.keyword->num]) - ruby_xfree((void *)body->param.keyword->table); - if (body->param.keyword->default_values) { - ruby_xfree((void *)body->param.keyword->default_values); + struct rb_iseq_param_keyword *pkw = (struct rb_iseq_param_keyword *)body->param.keyword; + if (pkw != NULL) { + if (pkw->table != &body->local_table[pkw->bits_start - pkw->num]) + SIZED_FREE_N(pkw->table, pkw->required_num); + if (pkw->default_values) { + SIZED_FREE_N(pkw->default_values, pkw->num - pkw->required_num); } - ruby_xfree((void *)body->param.keyword); + SIZED_FREE(pkw); + } + if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl)) { + SIZED_FREE_N(body->local_table, body->local_table_size); } - if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl)) - ruby_xfree((void *)body->local_table); + SIZED_FREE_N(body->lvar_states, body->local_table_size); + compile_data_free(ISEQ_COMPILE_DATA(iseq)); if (body->outer_variables) rb_id_table_free(body->outer_variables); - ruby_xfree(body); - } - - if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) { - rb_hook_list_free(iseq->aux.exec.local_hooks); + SIZED_FREE(body); } RUBY_FREE_LEAVE("iseq"); @@ -276,9 +302,7 @@ rb_iseq_mark_and_move_each_body_value(const rb_iseq_t *iseq, VALUE *original_ise for (unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) { ICVARC icvarc = (ICVARC)is_entries; if (icvarc->entry) { - RUBY_ASSERT(!RB_TYPE_P(icvarc->entry->class_value, T_NONE)); - - rb_gc_mark_and_move(&icvarc->entry->class_value); + rb_gc_mark_and_move((VALUE *)&icvarc->entry); } } @@ -350,8 +374,6 @@ rb_iseq_mark_and_move(rb_iseq_t *iseq, bool reference_updating) rb_iseq_mark_and_move_each_body_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL); - rb_gc_mark_and_move(&body->variable.coverage); - rb_gc_mark_and_move(&body->variable.pc2branchindex); rb_gc_mark_and_move(&body->variable.script_lines); rb_gc_mark_and_move(&body->location.label); rb_gc_mark_and_move(&body->location.base_label); @@ -406,34 +428,43 @@ rb_iseq_mark_and_move(rb_iseq_t *iseq, bool reference_updating) #endif } else { + // TODO: check jit payload + if (!rb_gc_checking_shareable()) { #if USE_YJIT - rb_yjit_iseq_mark(body->yjit_payload); + rb_yjit_iseq_mark(body->yjit_payload); #endif #if USE_ZJIT - rb_zjit_iseq_mark(body->zjit_payload); + rb_zjit_iseq_mark(body->zjit_payload); #endif + } + } + + // TODO: ractor aware coverage + if (!rb_gc_checking_shareable()) { + rb_gc_mark_and_move(&body->variable.coverage); + rb_gc_mark_and_move(&body->variable.pc2branchindex); } } if (FL_TEST_RAW((VALUE)iseq, ISEQ_NOT_LOADED_YET)) { - rb_gc_mark_and_move(&iseq->aux.loader.obj); + if (!rb_gc_checking_shareable()) { + rb_gc_mark_and_move(&iseq->aux.loader.obj); + } } else if (FL_TEST_RAW((VALUE)iseq, ISEQ_USE_COMPILE_DATA)) { - const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq); + if (!rb_gc_checking_shareable()) { + const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq); - rb_iseq_mark_and_move_insn_storage(compile_data->insn.storage_head); - rb_iseq_mark_and_move_each_compile_data_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL); + rb_iseq_mark_and_move_insn_storage(compile_data->insn.storage_head); + rb_iseq_mark_and_move_each_compile_data_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL); - rb_gc_mark_and_move((VALUE *)&compile_data->err_info); - rb_gc_mark_and_move((VALUE *)&compile_data->catch_table_ary); + rb_gc_mark_and_move((VALUE *)&compile_data->err_info); + rb_gc_mark_and_move((VALUE *)&compile_data->catch_table_ary); + } } else { /* executable */ VM_ASSERT(ISEQ_EXECUTABLE_P(iseq)); - - if (iseq->aux.exec.local_hooks) { - rb_hook_list_mark_and_update(iseq->aux.exec.local_hooks); - } } RUBY_MARK_LEAVE("iseq"); @@ -538,9 +569,14 @@ rb_iseq_pathobj_new(VALUE path, VALUE realpath) pathobj = rb_fstring(path); } else { - if (!NIL_P(realpath)) realpath = rb_fstring(realpath); - pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath); + if (!NIL_P(realpath)) { + realpath = rb_fstring(realpath); + } + VALUE fpath = rb_fstring(path); + + pathobj = rb_ary_new_from_args(2, fpath, realpath); rb_ary_freeze(pathobj); + RB_OBJ_SET_SHAREABLE(pathobj); } return pathobj; } @@ -559,6 +595,11 @@ rb_iseq_alloc_with_dummy_path(VALUE fname) rb_iseq_t *dummy_iseq = iseq_alloc(); ISEQ_BODY(dummy_iseq)->type = ISEQ_TYPE_TOP; + + if (!RB_OBJ_SHAREABLE_P(fname)) { + RB_OBJ_SET_FROZEN_SHAREABLE(fname); + } + RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.pathobj, fname); RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.label, fname); @@ -634,6 +675,18 @@ new_arena(void) return new_arena; } +static int +prepare_node_id(const NODE *node) +{ + if (!node) return -1; + + if (nd_type(node) == NODE_SCOPE && RNODE_SCOPE(node)->nd_parent) { + return nd_node_id(RNODE_SCOPE(node)->nd_parent); + } + + return nd_node_id(node); +} + static VALUE prepare_iseq_build(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_code_location_t *code_location, const int node_id, @@ -707,7 +760,7 @@ rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq) if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table); body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size); #if VM_CHECK_MODE == 0 - ruby_xfree(body->insns_info.positions); + SIZED_FREE_N(body->insns_info.positions, body->insns_info.size); body->insns_info.positions = NULL; #endif #endif @@ -811,10 +864,6 @@ set_compile_option_from_hash(rb_compile_option_t *option, VALUE opt) static rb_compile_option_t * set_compile_option_from_ast(rb_compile_option_t *option, const rb_ast_body_t *ast) { -#define SET_COMPILE_OPTION(o, a, mem) \ - ((a)->mem < 0 ? 0 : ((o)->mem = (a)->mem > 0)) - SET_COMPILE_OPTION(option, ast, coverage_enabled); -#undef SET_COMPILE_OPTION if (ast->frozen_string_literal >= 0) { option->frozen_string_literal = ast->frozen_string_literal; } @@ -927,7 +976,7 @@ rb_iseq_new_top(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, c rb_iseq_t * pm_iseq_new_top(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, int *error_state) { - iseq_new_setup_coverage(path, (int) (node->parser->newline_list.size - 1)); + iseq_new_setup_coverage(path, (int) (pm_parser_line_offsets(node->parser)->size - 1)); return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0, ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT, error_state); @@ -951,7 +1000,7 @@ rb_iseq_new_main(const VALUE ast_value, VALUE path, VALUE realpath, const rb_ise rb_iseq_t * pm_iseq_new_main(pm_scope_node_t *node, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt, int *error_state) { - iseq_new_setup_coverage(path, (int) (node->parser->newline_list.size - 1)); + iseq_new_setup_coverage(path, (int) (pm_parser_line_offsets(node->parser)->size - 1)); return pm_iseq_new_with_opt(node, rb_fstring_lit("<main>"), path, realpath, 0, @@ -968,8 +1017,13 @@ rb_iseq_new_eval(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, } } + rb_compile_option_t option = COMPILE_OPTION_DEFAULT; + rb_ast_t *ast = rb_ruby_ast_data_get(ast_value); + if (ast->body.coverage_enabled >= 0) { + option.coverage_enabled = ast->body.coverage_enabled; + } return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno, - parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT, + parent, isolated_depth, ISEQ_TYPE_EVAL, &option, Qnil); } @@ -980,7 +1034,7 @@ pm_iseq_new_eval(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) { VALUE coverages = rb_get_coverages(); if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) { - iseq_setup_coverage(coverages, path, ((int) (node->parser->newline_list.size - 1)) + first_lineno - 1); + iseq_setup_coverage(coverages, path, ((int) (pm_parser_line_offsets(node->parser)->size - 1)) + first_lineno - 1); } } @@ -1031,7 +1085,7 @@ rb_iseq_new_with_opt(VALUE ast_value, VALUE name, VALUE path, VALUE realpath, script_lines = ISEQ_BODY(parent)->variable.script_lines; } - prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1, + prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, prepare_node_id(node), parent, isolated_depth, type, script_lines, option); rb_iseq_compile_node(iseq, node); @@ -1041,41 +1095,15 @@ rb_iseq_new_with_opt(VALUE ast_value, VALUE name, VALUE path, VALUE realpath, return iseq_translate(iseq); } -struct pm_iseq_new_with_opt_data { - rb_iseq_t *iseq; - pm_scope_node_t *node; -}; - -VALUE -pm_iseq_new_with_opt_try(VALUE d) -{ - struct pm_iseq_new_with_opt_data *data = (struct pm_iseq_new_with_opt_data *)d; - - // This can compile child iseqs, which can raise syntax errors - pm_iseq_compile_node(data->iseq, data->node); - - // This raises an exception if there is a syntax error - finish_iseq_build(data->iseq); - - return Qundef; -} - /** - * This is a step in the prism compiler that is called once all of the various - * options have been established. It is called from one of the pm_iseq_new_* - * functions or from the RubyVM::InstructionSequence APIs. It is responsible for - * allocating the instruction sequence, calling into the compiler, and returning - * the built instruction sequence. - * - * Importantly, this is also the function where the compiler is re-entered to - * compile child instruction sequences. A child instruction sequence is always - * compiled using a scope node, which is why we cast it explicitly to that here - * in the parameters (as opposed to accepting a generic pm_node_t *). + * Core implementation for building a prism iseq. This does not use rb_protect, + * so any exceptions (e.g. from finish_iseq_build) propagate normally up the + * call stack — matching the parse.y compiler's behavior. */ rb_iseq_t * -pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, - int first_lineno, const rb_iseq_t *parent, int isolated_depth, - enum rb_iseq_type type, const rb_compile_option_t *option, int *error_state) +pm_iseq_build(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, + int first_lineno, const rb_iseq_t *parent, int isolated_depth, + enum rb_iseq_type type, const rb_compile_option_t *option) { rb_iseq_t *iseq = iseq_alloc(); ISEQ_BODY(iseq)->prism = true; @@ -1088,10 +1116,11 @@ pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpa option = &next_option; pm_location_t *location = &node->base.location; - int32_t start_line = node->parser->start_line; + int32_t start_line = pm_parser_start_line(node->parser); + const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(node->parser); - pm_line_column_t start = pm_newline_list_line_column(&node->parser->newline_list, location->start, start_line); - pm_line_column_t end = pm_newline_list_line_column(&node->parser->newline_list, location->end, start_line); + pm_line_column_t start = pm_line_offset_list_line_column(line_offsets, location->start, start_line); + pm_line_column_t end = pm_line_offset_list_line_column(line_offsets, location->start + location->length, start_line); rb_code_location_t code_location = (rb_code_location_t) { .beg_pos = { .lineno = (int) start.line, .column = (int) start.column }, @@ -1101,15 +1130,59 @@ pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpa prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, node->ast_node->node_id, parent, isolated_depth, type, node->script_lines == NULL ? Qnil : *node->script_lines, option); + pm_iseq_compile_node(iseq, node); + finish_iseq_build(iseq); + + return iseq_translate(iseq); +} + +struct pm_iseq_new_with_opt_data { + rb_iseq_t *iseq; + pm_scope_node_t *node; + VALUE name, path, realpath; + int first_lineno, isolated_depth; + const rb_iseq_t *parent; + enum rb_iseq_type type; + const rb_compile_option_t *option; +}; + +static VALUE +pm_iseq_new_with_opt_try(VALUE d) +{ + struct pm_iseq_new_with_opt_data *data = (struct pm_iseq_new_with_opt_data *)d; + data->iseq = pm_iseq_build(data->node, data->name, data->path, data->realpath, + data->first_lineno, data->parent, data->isolated_depth, + data->type, data->option); + return Qundef; +} + +/** + * This is a step in the prism compiler that is called once all of the various + * options have been established. It is called from one of the pm_iseq_new_* + * functions or from the RubyVM::InstructionSequence APIs. + * + * This function uses rb_protect to catch exceptions, storing the error state + * in the provided out parameter. This is only needed at top-level entry points + * where the caller wants to handle errors gracefully. Child iseqs compiled + * during the compilation process do NOT go through this function — they use + * pm_iseq_build directly, letting exceptions propagate naturally (matching + * the parse.y compiler's behavior). + */ +rb_iseq_t * +pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, + int first_lineno, const rb_iseq_t *parent, int isolated_depth, + enum rb_iseq_type type, const rb_compile_option_t *option, int *error_state) +{ struct pm_iseq_new_with_opt_data data = { - .iseq = iseq, - .node = node + .node = node, .name = name, .path = path, .realpath = realpath, + .first_lineno = first_lineno, .parent = parent, + .isolated_depth = isolated_depth, .type = type, .option = option }; rb_protect(pm_iseq_new_with_opt_try, (VALUE)&data, error_state); if (*error_state) return NULL; - return iseq_translate(iseq); + return data.iseq; } rb_iseq_t * @@ -1137,7 +1210,22 @@ rb_iseq_load_iseq(VALUE fname) VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("load_iseq"), 1, &fname); if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) { - return iseqw_check(iseqv); + return iseqw_check(iseqv); + } + + return NULL; +} + +const rb_iseq_t * +rb_iseq_compile_iseq(VALUE str, VALUE fname) +{ + VALUE args[] = { + str, fname + }; + VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("compile"), 2, args); + + if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) { + return iseqw_check(iseqv); } return NULL; @@ -1310,6 +1398,7 @@ rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, V rb_exc_raise(GET_EC()->errinfo); } else { + iseq_new_setup_coverage(file, ast_line_count(ast_value)); iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option, Qnil); @@ -1341,19 +1430,20 @@ pm_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, V src = StringValue(src); } - pm_parse_result_t result = { 0 }; - pm_options_line_set(&result.options, NUM2INT(line)); - pm_options_scopes_init(&result.options, 1); + pm_parse_result_t result; + pm_parse_result_init(&result); + pm_options_line_set(result.options, NUM2INT(line)); + pm_options_scopes_init(result.options, 1); result.node.coverage_enabled = 1; switch (option.frozen_string_literal) { case ISEQ_FROZEN_STRING_LITERAL_UNSET: break; case ISEQ_FROZEN_STRING_LITERAL_DISABLED: - pm_options_frozen_string_literal_set(&result.options, false); + pm_options_frozen_string_literal_set(result.options, false); break; case ISEQ_FROZEN_STRING_LITERAL_ENABLED: - pm_options_frozen_string_literal_set(&result.options, true); + pm_options_frozen_string_literal_set(result.options, true); break; default: rb_bug("pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal); @@ -1374,6 +1464,7 @@ pm_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, V if (error == Qnil) { int error_state; + iseq_new_setup_coverage(file, (int) (pm_parser_line_offsets(result.node.parser)->size - 1)); iseq = pm_iseq_new_with_opt(&result.node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state); pm_parse_result_free(&result); @@ -1535,6 +1626,7 @@ iseqw_new(const rb_iseq_t *iseq) RB_OBJ_WRITE(obj, ptr, iseq); /* cache a wrapper object */ + RB_OBJ_SET_FROZEN_SHAREABLE((VALUE)obj); RB_OBJ_WRITE((VALUE)iseq, &iseq->wrapper, obj); return obj; @@ -1597,7 +1689,7 @@ iseqw_s_compile_parser(int argc, VALUE *argv, VALUE self, bool prism) * real path and first line number of the ruby code in +source+ which are * metadata attached to the returned +iseq+. * - * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for + * +file+ is used for +__FILE__+ and exception backtrace. +path+ is used for * +require_relative+ base. It is recommended these should be the same full * path. * @@ -1639,7 +1731,7 @@ iseqw_s_compile(int argc, VALUE *argv, VALUE self) * real path and first line number of the ruby code in +source+ which are * metadata attached to the returned +iseq+. * - * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for + * +file+ is used for +__FILE__+ and exception backtrace. +path+ is used for * +require_relative+ base. It is recommended these should be the same full * path. * @@ -1681,7 +1773,7 @@ iseqw_s_compile_parsey(int argc, VALUE *argv, VALUE self) * real path and first line number of the ruby code in +source+ which are * metadata attached to the returned +iseq+. * - * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for + * +file+ is used for +__FILE__+ and exception backtrace. +path+ is used for * +require_relative+ base. It is recommended these should be the same full * path. * @@ -1712,6 +1804,8 @@ iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self) return iseqw_s_compile_parser(argc, argv, self, true); } +static VALUE iseqw_s_compile_file_prism(int argc, VALUE *argv, VALUE self); + /* * call-seq: * InstructionSequence.compile_file(file[, options]) -> iseq @@ -1735,6 +1829,10 @@ iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self) static VALUE iseqw_s_compile_file(int argc, VALUE *argv, VALUE self) { + if (rb_ruby_prism_p()) { + return iseqw_s_compile_file_prism(argc, argv, self); + } + VALUE file, opt = Qnil; VALUE parser, f, exc = Qnil, ret; rb_ast_t *ast; @@ -1758,6 +1856,7 @@ iseqw_s_compile_file(int argc, VALUE *argv, VALUE self) parser = rb_parser_new(); rb_parser_set_context(parser, NULL, FALSE); ast_value = rb_parser_load_file(parser, file); + iseq_new_setup_coverage(file, ast_line_count(ast_value)); ast = rb_ruby_ast_data_get(ast_value); if (!ast->body.root) exc = GET_EC()->errinfo; @@ -1821,17 +1920,32 @@ iseqw_s_compile_file_prism(int argc, VALUE *argv, VALUE self) rb_execution_context_t *ec = GET_EC(); VALUE v = rb_vm_push_frame_fname(ec, file); - pm_parse_result_t result = { 0 }; - result.options.line = 1; + make_compile_option(&option, opt); + + pm_parse_result_t result; + pm_parse_result_init(&result); result.node.coverage_enabled = 1; + switch (option.frozen_string_literal) { + case ISEQ_FROZEN_STRING_LITERAL_UNSET: + break; + case ISEQ_FROZEN_STRING_LITERAL_DISABLED: + pm_options_frozen_string_literal_set(result.options, false); + break; + case ISEQ_FROZEN_STRING_LITERAL_ENABLED: + pm_options_frozen_string_literal_set(result.options, true); + break; + default: + rb_bug("iseqw_s_compile_file_prism: invalid frozen_string_literal=%d", option.frozen_string_literal); + break; + } + VALUE script_lines; VALUE error = pm_load_parse_file(&result, file, ruby_vm_keep_script_lines ? &script_lines : NULL); if (error == Qnil) { - make_compile_option(&option, opt); - int error_state; + iseq_new_setup_coverage(file, (int) (pm_parser_line_offsets(result.node.parser)->size - 1)); rb_iseq_t *iseq = pm_iseq_new_with_opt(&result.node, rb_fstring_lit("<main>"), file, rb_realpath_internal(Qnil, file, 1), @@ -1948,7 +2062,7 @@ iseqw_eval(VALUE self) if (0 == ISEQ_BODY(iseq)->iseq_size) { rb_raise(rb_eTypeError, "attempt to evaluate dummy InstructionSequence"); } - return rb_iseq_eval(iseq); + return rb_iseq_eval(iseq, rb_current_box()); } /* @@ -2382,17 +2496,22 @@ rb_iseq_event_flags(const rb_iseq_t *iseq, size_t pos) } } +static void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos); + // Clear tracing event flags and turn off tracing for a given instruction as needed. // This is currently used after updating a one-shot line coverage for the current instruction. void rb_iseq_clear_event_flags(const rb_iseq_t *iseq, size_t pos, rb_event_flag_t reset) { - struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos); - if (entry) { - entry->events &= ~reset; - if (!(entry->events & iseq->aux.exec.global_trace_events)) { - void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos); - rb_iseq_trace_flag_cleared(iseq, pos); + RB_VM_LOCKING() { + rb_vm_barrier(); + + struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos); + if (entry) { + entry->events &= ~reset; + if (!(entry->events & iseq->aux.exec.global_trace_events)) { + rb_iseq_trace_flag_cleared(iseq, pos); + } } } } @@ -2911,24 +3030,31 @@ rb_iseq_disasm(const rb_iseq_t *iseq) attr_index_t rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq) { - struct rb_id_table * iv_names = rb_id_table_create(0); + set_table iv_names = { 0 }; + set_init_embedded_numtable_with_size(&iv_names, 0); for (unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) { IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i]; if (cache->iv_set_name) { - rb_id_table_insert(iv_names, cache->iv_set_name, Qtrue); + set_insert(&iv_names, cache->iv_set_name); } } - attr_index_t count = (attr_index_t)rb_id_table_size(iv_names); + size_t count = iv_names.num_entries; VALUE superclass = rb_class_superclass(klass); - count += RCLASS_MAX_IV_COUNT(superclass); + if (!NIL_P(superclass)) { // BasicObject doesn't have a superclass + count += RCLASS_MAX_IV_COUNT(superclass); + } - rb_id_table_free(iv_names); + set_free_embedded_table(&iv_names); + + if (count > (attr_index_t)-1) { + return (attr_index_t)-1; + } - return count; + return (attr_index_t)count; } /* @@ -3149,7 +3275,7 @@ iseqw_s_of(VALUE klass, VALUE body) * 0000 trace 8 ( 1) * 0002 trace 1 ( 2) * 0004 putself - * 0005 putstring "hello, world" + * 0005 dupstring "hello, world" * 0007 send :puts, 1, nil, 8, <ic:0> * 0013 trace 16 ( 3) * 0015 leave ( 2) @@ -3214,7 +3340,7 @@ static int cdhash_each(VALUE key, VALUE value, VALUE ary) { rb_ary_push(ary, obj_resurrect(key)); - rb_ary_push(ary, value); + rb_ary_push(ary, INT2FIX(value)); return ST_CONTINUE; } @@ -3273,6 +3399,7 @@ iseq_type_id(enum rb_iseq_type type) static VALUE iseq_data_to_ary(const rb_iseq_t *iseq) { + VALUE iseq_value = (VALUE)iseq; unsigned int i; long l; const struct rb_iseq_constant_body *const iseq_body = ISEQ_BODY(iseq); @@ -3308,7 +3435,7 @@ iseq_data_to_ary(const rb_iseq_t *iseq) for (i=0; i<iseq_body->local_table_size; i++) { ID lid = iseq_body->local_table[i]; if (lid) { - if (rb_id2str(lid)) { + if (lid != idItImplicit && rb_id2str(lid)) { rb_ary_push(locals, ID2SYM(lid)); } else { /* hidden variable from id_internal() */ @@ -3454,11 +3581,11 @@ iseq_data_to_ary(const rb_iseq_t *iseq) break; case TS_CDHASH: { - VALUE hash = *seq; + VALUE cdhash = *seq; VALUE val = rb_ary_new(); int i; - rb_hash_foreach(hash, cdhash_each, val); + st_foreach(rb_imemo_cdhash_tbl(cdhash), cdhash_each, val); for (i=0; i<RARRAY_LEN(val); i+=2) { VALUE pos = FIX2INT(rb_ary_entry(val, i+1)); @@ -3605,6 +3732,9 @@ iseq_data_to_ary(const rb_iseq_t *iseq) rb_ary_push(val, params); rb_ary_push(val, exception); rb_ary_push(val, body); + + RB_GC_GUARD(iseq_value); + return val; } @@ -3618,10 +3748,10 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) ID req, opt, rest, block, key, keyrest; #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type)) #define PARAM_ID(i) body->local_table[(i)] -#define PARAM(i, type) ( \ - PARAM_TYPE(type), \ - rb_id2str(PARAM_ID(i)) ? \ - rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \ +#define PARAM(i, type) ( \ + PARAM_TYPE(type), \ + PARAM_ID(i) != idItImplicit && rb_id2str(PARAM_ID(i)) ? \ + rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \ a) CONST_ID(req, "req"); @@ -3639,11 +3769,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) if (is_proc) { for (i = 0; i < body->param.lead_num; i++) { - PARAM_TYPE(opt); - if (rb_id2str(PARAM_ID(i))) { - rb_ary_push(a, ID2SYM(PARAM_ID(i))); - } - rb_ary_push(args, a); + rb_ary_push(args, PARAM(i, opt)); } } else { @@ -3653,11 +3779,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) } r = body->param.lead_num + body->param.opt_num; for (; i < r; i++) { - PARAM_TYPE(opt); - if (rb_id2str(PARAM_ID(i))) { - rb_ary_push(a, ID2SYM(PARAM_ID(i))); - } - rb_ary_push(args, a); + rb_ary_push(args, PARAM(i, opt)); } if (body->param.flags.has_rest) { CONST_ID(rest, "rest"); @@ -3666,11 +3788,7 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) r = body->param.post_start + body->param.post_num; if (is_proc) { for (i = body->param.post_start; i < r; i++) { - PARAM_TYPE(opt); - if (rb_id2str(PARAM_ID(i))) { - rb_ary_push(a, ID2SYM(PARAM_ID(i))); - } - rb_ary_push(args, a); + rb_ary_push(args, PARAM(i, opt)); } } else { @@ -3719,7 +3837,13 @@ rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc) } rb_ary_push(args, a); } - if (body->param.flags.has_block) { + if (body->param.flags.accepts_no_block) { + ID noblock; + CONST_ID(noblock, "noblock"); + PARAM_TYPE(noblock); + rb_ary_push(args, a); + } + else if (body->param.flags.has_block) { CONST_ID(block, "block"); rb_ary_push(args, PARAM(body->param.block_start, block)); } @@ -3874,14 +3998,14 @@ rb_vm_insn_decode(const VALUE encoded) // Turn on or off tracing for a given instruction address static inline int -encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_current_trace) +encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_traced) { st_data_t key = (st_data_t)*iseq_encoded_insn; st_data_t val; if (st_lookup(encoded_insn_data, key, &val)) { insn_data_t *e = (insn_data_t *)val; - if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) { + if (remain_traced && key == (st_data_t)e->trace_encoded_insn) { turnon = 1; } *iseq_encoded_insn = (VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn); @@ -3892,7 +4016,7 @@ encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, } // Turn off tracing for an instruction at pos after tracing event flags are cleared -void +static void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos) { const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq); @@ -3918,14 +4042,16 @@ add_bmethod_events(rb_event_flag_t events) // Note, to support call/return events for bmethods, turnon_event can have more events than tpval. static int -iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line) +iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line, rb_ractor_t *r) { unsigned int pc; int n = 0; const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq); VALUE *iseq_encoded = (VALUE *)body->iseq_encoded; + rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq; VM_ASSERT(ISEQ_EXECUTABLE_P(iseq)); + ASSERT_vm_locking_with_barrier(); for (pc=0; pc<body->iseq_size;) { const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pc); @@ -3947,11 +4073,9 @@ iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, } if (n > 0) { - if (iseq->aux.exec.local_hooks == NULL) { - ((rb_iseq_t *)iseq)->aux.exec.local_hooks = RB_ZALLOC(rb_hook_list_t); - iseq->aux.exec.local_hooks->is_local = true; - } - rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line); + rb_hook_list_t *hook_list = rb_iseq_local_hooks(iseq, r, true); + rb_hook_list_connect_local_tracepoint(hook_list, tpval, target_line); + iseq_mut->aux.exec.local_hooks_cnt++; } return n; @@ -3962,19 +4086,21 @@ struct trace_set_local_events_struct { VALUE tpval; unsigned int target_line; int n; + rb_ractor_t *r; }; static void iseq_add_local_tracepoint_i(const rb_iseq_t *iseq, void *p) { struct trace_set_local_events_struct *data = (struct trace_set_local_events_struct *)p; - data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line); + data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line, data->r); iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p); } int rb_iseq_add_local_tracepoint_recursively(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line, bool target_bmethod) { + ASSERT_vm_locking_with_barrier(); struct trace_set_local_events_struct data; if (target_bmethod) { turnon_events = add_bmethod_events(turnon_events); @@ -3983,35 +4109,52 @@ rb_iseq_add_local_tracepoint_recursively(const rb_iseq_t *iseq, rb_event_flag_t data.tpval = tpval; data.target_line = target_line; data.n = 0; + data.r = GET_RACTOR(); iseq_add_local_tracepoint_i(iseq, (void *)&data); - if (0) rb_funcall(Qnil, rb_intern("puts"), 1, rb_iseq_disasm(iseq)); /* for debug */ + if (0) fprintf(stderr, "Iseq disasm:\n:%s", RSTRING_PTR(rb_iseq_disasm(iseq))); /* for debug */ return data.n; } static int -iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval) +iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval, rb_ractor_t *r) { int n = 0; + unsigned int num_hooks_left; + unsigned int pc; + const struct rb_iseq_constant_body *body; + rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq; + rb_hook_list_t *hook_list; + VALUE *iseq_encoded; + ASSERT_vm_locking_with_barrier(); - if (iseq->aux.exec.local_hooks) { - unsigned int pc; - const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq); - VALUE *iseq_encoded = (VALUE *)body->iseq_encoded; + hook_list = rb_iseq_local_hooks(iseq, r, false); + + if (hook_list) { rb_event_flag_t local_events = 0; - rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval); - local_events = iseq->aux.exec.local_hooks->events; + rb_event_flag_t prev_events = hook_list->events; + if (rb_hook_list_remove_local_tracepoint(hook_list, tpval)) { + RUBY_ASSERT(iseq->aux.exec.local_hooks_cnt > 0); + iseq_mut->aux.exec.local_hooks_cnt--; + local_events = hook_list->events; // remaining events for this ractor + num_hooks_left = rb_hook_list_count(hook_list); + if (local_events == 0 && prev_events != 0) { + st_delete(rb_ractor_targeted_hooks(r), (st_data_t*)&iseq, NULL); + rb_hook_list_free(hook_list); + } - if (local_events == 0) { - rb_hook_list_free(iseq->aux.exec.local_hooks); - ((rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL; - } + if (iseq->aux.exec.local_hooks_cnt == num_hooks_left) { + body = ISEQ_BODY(iseq); + iseq_encoded = (VALUE *)body->iseq_encoded; + local_events = add_bmethod_events(local_events); + for (pc = 0; pc<body->iseq_size;) { + rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc); + pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events), false); + } + } - local_events = add_bmethod_events(local_events); - for (pc = 0; pc<body->iseq_size;) { - rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc); - pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events), false); + n++; } } return n; @@ -4020,22 +4163,25 @@ iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval) struct trace_clear_local_events_struct { VALUE tpval; int n; + rb_ractor_t *r; }; static void iseq_remove_local_tracepoint_i(const rb_iseq_t *iseq, void *p) { struct trace_clear_local_events_struct *data = (struct trace_clear_local_events_struct *)p; - data->n += iseq_remove_local_tracepoint(iseq, data->tpval); + data->n += iseq_remove_local_tracepoint(iseq, data->tpval, data->r); iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p); } int -rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval) +rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval, rb_ractor_t *r) { struct trace_clear_local_events_struct data; + ASSERT_vm_locking_with_barrier(); data.tpval = tpval; data.n = 0; + data.r = r; iseq_remove_local_tracepoint_i(iseq, (void *)&data); return data.n; @@ -4053,11 +4199,14 @@ rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events) return; } else { + // NOTE: this does not need VM barrier if it's a new ISEQ unsigned int pc; const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq); + VALUE *iseq_encoded = (VALUE *)body->iseq_encoded; rb_event_flag_t enabled_events; - rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0; + rb_hook_list_t *local_hooks = rb_iseq_local_hooks(iseq, GET_RACTOR(), false); + rb_event_flag_t local_events = local_hooks ? local_hooks->events : 0; ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events; enabled_events = add_bmethod_events(turnon_events | local_events); @@ -4073,6 +4222,7 @@ void rb_vm_cc_general(const struct rb_callcache *cc); static bool clear_attr_cc(VALUE v) { + ASSERT_vm_locking_with_barrier(); if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((const struct rb_callcache *)v)) { rb_vm_cc_general((struct rb_callcache *)v); return true; @@ -4085,6 +4235,7 @@ clear_attr_cc(VALUE v) static bool clear_bf_cc(VALUE v) { + ASSERT_vm_locking_with_barrier(); if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((const struct rb_callcache *)v)) { rb_vm_cc_general((struct rb_callcache *)v); return true; @@ -4110,7 +4261,10 @@ clear_attr_ccs_i(void *vstart, void *vend, size_t stride, void *data) void rb_clear_attr_ccs(void) { - rb_objspace_each_objects(clear_attr_ccs_i, NULL); + RB_VM_LOCKING() { + rb_vm_barrier(); + rb_objspace_each_objects(clear_attr_ccs_i, NULL); + } } static int @@ -4129,6 +4283,7 @@ clear_bf_ccs_i(void *vstart, void *vend, size_t stride, void *data) void rb_clear_bf_ccs(void) { + ASSERT_vm_locking_with_barrier(); rb_objspace_each_objects(clear_bf_ccs_i, NULL); } @@ -4158,7 +4313,10 @@ trace_set_i(void *vstart, void *vend, size_t stride, void *data) void rb_iseq_trace_set_all(rb_event_flag_t turnon_events) { - rb_objspace_each_objects(trace_set_i, &turnon_events); + RB_VM_LOCKING() { + rb_vm_barrier(); + rb_objspace_each_objects(trace_set_i, &turnon_events); + } } VALUE |
