10#include "internal/sanitizers.h"
11#include "internal/string.h"
12#include "internal/hash.h"
13#include "internal/variable.h"
14#include "internal/compile.h"
15#include "internal/class.h"
16#include "internal/fixnum.h"
17#include "internal/numeric.h"
18#include "internal/gc.h"
20#include "vm_callinfo.h"
23#include "insns_info.inc"
26#include "vm_insnhelper.h"
28#include "probes_helper.h"
31#include "internal/cont.h"
45 ROBJECT_OFFSET_AS_ARY = offsetof(
struct RObject, as.ary),
50 RUBY_OFFSET_RSTRING_LEN = offsetof(
struct RString,
len)
55STATIC_ASSERT(64b_size_t, SIZE_MAX == UINT64_MAX);
58STATIC_ASSERT(size_t_no_padding_bits,
sizeof(
size_t) ==
sizeof(uint64_t));
62STATIC_ASSERT(pointer_tagging_scheme,
USE_FLONUM);
80rb_yjit_mark_writable(
void *mem_block, uint32_t mem_size)
82 return mprotect(mem_block, mem_size, PROT_READ | PROT_WRITE) == 0;
86rb_yjit_mark_executable(
void *mem_block, uint32_t mem_size)
93 if (mprotect(mem_block, mem_size, PROT_READ | PROT_EXEC)) {
94 rb_bug(
"Couldn't make JIT page (%p, %lu bytes) executable, errno: %s",
95 mem_block, (
unsigned long)mem_size, strerror(
errno));
101rb_yjit_mark_unused(
void *mem_block, uint32_t mem_size)
106 madvise(mem_block, mem_size, MADV_DONTNEED);
110 return mprotect(mem_block, mem_size, PROT_NONE) == 0;
114rb_yjit_array_len(
VALUE a)
121rb_yjit_icache_invalidate(
void *start,
void *end)
127 __builtin___clear_cache(start, end);
128#elif defined(__aarch64__)
129#error No instruction cache clear available with this compiler on Aarch64!
133# define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
140 VALUE frame_id = PTR2NUM(frame);
142 if (
RTEST(rb_hash_aref(hash, frame_id))) {
146 VALUE frame_info = rb_hash_new();
159 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"name")), name);
160 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"file")), file);
161 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"samples")),
INT2NUM(0));
162 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"total_samples")),
INT2NUM(0));
163 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"edges")), rb_hash_new());
164 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"lines")), rb_hash_new());
167 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"line")), line);
170 rb_hash_aset(hash, frame_id, frame_info);
180rb_yjit_exit_locations_dict(
VALUE *yjit_raw_samples,
int *yjit_line_samples,
int samples_len)
182 VALUE result = rb_hash_new();
183 VALUE raw_samples = rb_ary_new_capa(samples_len);
184 VALUE line_samples = rb_ary_new_capa(samples_len);
185 VALUE frames = rb_hash_new();
190 while (idx < samples_len) {
191 int num = (int)yjit_raw_samples[idx];
192 int line_num = (int)yjit_line_samples[idx];
196 rb_ary_push(raw_samples,
SIZET2NUM(num + 1));
197 rb_ary_push(line_samples,
INT2NUM(line_num + 1));
202 for (
int o = 0; o < num; o++) {
203 rb_yjit_add_frame(frames, yjit_raw_samples[idx]);
204 rb_ary_push(raw_samples,
SIZET2NUM(yjit_raw_samples[idx]));
205 rb_ary_push(line_samples,
INT2NUM(yjit_line_samples[idx]));
209 rb_ary_push(raw_samples,
SIZET2NUM(yjit_raw_samples[idx]));
210 rb_ary_push(line_samples,
INT2NUM(yjit_line_samples[idx]));
213 rb_ary_push(raw_samples,
SIZET2NUM(yjit_raw_samples[idx]));
214 rb_ary_push(line_samples,
INT2NUM(yjit_line_samples[idx]));
220 rb_hash_aset(result,
ID2SYM(rb_intern(
"raw")), raw_samples);
221 rb_hash_aset(result,
ID2SYM(rb_intern(
"lines")), line_samples);
222 rb_hash_aset(result,
ID2SYM(rb_intern(
"frames")), frames);
228rb_yjit_get_page_size(
void)
230#if defined(_SC_PAGESIZE)
231 long page_size = sysconf(_SC_PAGESIZE);
232 if (page_size <= 0) rb_bug(
"yjit: failed to get page size");
237 if (page_size > 0x40000000l) rb_bug(
"yjit page size too large");
239 return (uint32_t)page_size;
241#error "YJIT supports POSIX only for now"
245#if defined(MAP_FIXED_NOREPLACE) && defined(_SC_PAGESIZE)
248align_ptr(uint8_t *ptr, uint32_t multiple)
251 uint32_t rem = ((uint32_t)(uintptr_t)ptr) % multiple;
258 uint32_t pad = multiple - rem;
267rb_yjit_reserve_addr_space(uint32_t mem_size)
273 #if defined(MAP_FIXED_NOREPLACE) && defined(_SC_PAGESIZE)
274 uint32_t
const page_size = (uint32_t)sysconf(_SC_PAGESIZE);
275 uint8_t *
const cfunc_sample_addr = (
void *)(uintptr_t)&rb_yjit_reserve_addr_space;
276 uint8_t *
const probe_region_end = cfunc_sample_addr + INT32_MAX;
278 uint8_t *req_addr = align_ptr(cfunc_sample_addr, page_size);
287 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
293 if (mem_block != MAP_FAILED) {
294 ruby_annotate_mmap(mem_block, mem_size,
"Ruby:rb_yjit_reserve_addr_space");
302 req_addr -= 4 * 1024 * 1024;
303 }
while (req_addr < probe_region_end);
309 (
void *)rb_yjit_reserve_addr_space,
312 MAP_PRIVATE | MAP_ANONYMOUS,
319 if (mem_block == MAP_FAILED) {
325 MAP_PRIVATE | MAP_ANONYMOUS,
330 if (mem_block != MAP_FAILED) {
331 ruby_annotate_mmap(mem_block, mem_size,
"Ruby:rb_yjit_reserve_addr_space:fallback");
336 if (mem_block == MAP_FAILED) {
337 perror(
"ruby: yjit: mmap:");
338 if(
errno == ENOMEM) {
342 rb_bug(
"mmap failed");
357 if (rb_multi_ractor_p()) {
358 tracing_events = ruby_vm_event_enabled_global_flags;
364 tracing_events = rb_ec_ractor_hooks(ec)->events;
388 EXEC_EVENT_HOOK(ec,
RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, return_value);
392 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
396 ec->cfp->sp[0] = return_value;
401rb_iseq_encoded_size(
const rb_iseq_t *iseq)
403 return iseq->body->iseq_size;
408rb_iseq_get_yjit_payload(
const rb_iseq_t *iseq)
412 return iseq->body->yjit_payload;
421rb_iseq_set_yjit_payload(
const rb_iseq_t *iseq,
void *payload)
426 iseq->body->yjit_payload = payload;
430rb_iseq_reset_jit_func(
const rb_iseq_t *iseq)
433 iseq->body->jit_entry = NULL;
434 iseq->body->jit_exception = NULL;
437 iseq->body->jit_entry_calls = 0;
438 iseq->body->jit_exception_calls = 0;
443rb_iseq_pc_at_idx(
const rb_iseq_t *iseq, uint32_t insn_idx)
447 VALUE *encoded = iseq->body->iseq_encoded;
448 VALUE *pc = &encoded[insn_idx];
459 const VALUE at_pc = *pc;
460 return rb_vm_insn_addr2opcode((
const void *)at_pc);
464rb_RSTRING_LEN(
VALUE str)
466 return RSTRING_LEN(str);
470rb_RSTRING_PTR(
VALUE str)
472 return RSTRING_PTR(str);
476rb_yjit_get_proc_ptr(
VALUE procv)
479 GetProcPtr(procv, proc);
486typedef struct rb_iseq_param_keyword rb_seq_param_keyword_struct;
489rb_insn_name(
VALUE insn)
491 return insn_name(insn);
497 return vm_ci_argc(ci);
503 return vm_ci_mid(ci);
509 return vm_ci_flag(ci);
515 return vm_ci_kwarg(ci);
521 return cikw->keyword_len;
527 return cikw->keywords[idx];
530rb_method_visibility_t
533 return METHOD_ENTRY_VISI(me);
539 if (UNDEFINED_METHOD_ENTRY_P(cme)) {
540 return VM_METHOD_TYPE_UNDEF;
543 return cme->def->type;
550 return cme->def->body.attr.id;
553ID rb_get_symbol_id(
VALUE namep);
555enum method_optimized_type
558 return cme->def->body.optimized.type;
564 return cme->def->body.optimized.index;
570 return UNALIGNED_MEMBER_PTR(cme->def, body.cfunc);
576 return def->method_serial;
582 return def->original_id;
594 return (
void*)(uintptr_t)mct->func;
600 return def_iseq_ptr(def);
607 return def->body.bmethod.proc;
611rb_get_iseq_body_local_iseq(
const rb_iseq_t *iseq)
613 return iseq->body->local_iseq;
617rb_get_iseq_body_parent_iseq(
const rb_iseq_t *iseq)
619 return iseq->body->parent_iseq;
623rb_get_iseq_body_local_table_size(
const rb_iseq_t *iseq)
625 return iseq->body->local_table_size;
629rb_get_iseq_body_iseq_encoded(
const rb_iseq_t *iseq)
631 return iseq->body->iseq_encoded;
635rb_get_iseq_body_stack_max(
const rb_iseq_t *iseq)
637 return iseq->body->stack_max;
641rb_get_iseq_body_type(
const rb_iseq_t *iseq)
643 return iseq->body->type;
647rb_get_iseq_flags_has_lead(
const rb_iseq_t *iseq)
649 return iseq->body->
param.flags.has_lead;
653rb_get_iseq_flags_has_opt(
const rb_iseq_t *iseq)
655 return iseq->body->
param.flags.has_opt;
659rb_get_iseq_flags_has_kw(
const rb_iseq_t *iseq)
661 return iseq->body->
param.flags.has_kw;
665rb_get_iseq_flags_has_post(
const rb_iseq_t *iseq)
667 return iseq->body->
param.flags.has_post;
671rb_get_iseq_flags_has_kwrest(
const rb_iseq_t *iseq)
673 return iseq->body->
param.flags.has_kwrest;
677rb_get_iseq_flags_anon_kwrest(
const rb_iseq_t *iseq)
679 return iseq->body->
param.flags.anon_kwrest;
683rb_get_iseq_flags_has_rest(
const rb_iseq_t *iseq)
685 return iseq->body->
param.flags.has_rest;
689rb_get_iseq_flags_ruby2_keywords(
const rb_iseq_t *iseq)
691 return iseq->body->
param.flags.ruby2_keywords;
695rb_get_iseq_flags_has_block(
const rb_iseq_t *iseq)
697 return iseq->body->
param.flags.has_block;
701rb_get_iseq_flags_ambiguous_param0(
const rb_iseq_t *iseq)
703 return iseq->body->
param.flags.ambiguous_param0;
707rb_get_iseq_flags_accepts_no_kwarg(
const rb_iseq_t *iseq)
709 return iseq->body->
param.flags.accepts_no_kwarg;
713rb_get_iseq_flags_forwardable(
const rb_iseq_t *iseq)
715 return iseq->body->
param.flags.forwardable;
718const rb_seq_param_keyword_struct *
719rb_get_iseq_body_param_keyword(
const rb_iseq_t *iseq)
721 return iseq->body->
param.keyword;
725rb_get_iseq_body_param_size(
const rb_iseq_t *iseq)
727 return iseq->body->
param.size;
731rb_get_iseq_body_param_lead_num(
const rb_iseq_t *iseq)
733 return iseq->body->
param.lead_num;
737rb_get_iseq_body_param_opt_num(
const rb_iseq_t *iseq)
739 return iseq->body->
param.opt_num;
743rb_get_iseq_body_param_opt_table(
const rb_iseq_t *iseq)
745 return iseq->body->
param.opt_table;
752 GetProcPtr(recv, proc);
753 return rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, block_handler);
757rb_yjit_iseq_builtin_attrs(
const rb_iseq_t *iseq)
759 return iseq->body->builtin_attrs;
764invokebuiltin_delegate_leave_p(
const rb_iseq_t *iseq)
766 int insn1 = rb_vm_insn_addr2opcode((
void *)iseq->body->iseq_encoded[0]);
767 if ((
int)iseq->body->iseq_size != insn_len(insn1) + insn_len(BIN(leave))) {
770 int insn2 = rb_vm_insn_addr2opcode((
void *)iseq->body->iseq_encoded[insn_len(insn1)]);
771 return (insn1 == BIN(opt_invokebuiltin_delegate) || insn1 == BIN(opt_invokebuiltin_delegate_leave)) &&
777rb_yjit_builtin_function(
const rb_iseq_t *iseq)
779 if (invokebuiltin_delegate_leave_p(iseq)) {
788rb_yjit_str_simple_append(
VALUE str1,
VALUE str2)
790 return rb_str_cat(str1, RSTRING_PTR(str2), RSTRING_LEN(str2));
808 return (
VALUE*)cfp->pc;
838 return (
VALUE*)cfp->ep;
846 for (i = 0; i < lv; i++) {
847 ep = VM_ENV_PREV_EP(ep);
855rb_yarv_class_of(
VALUE obj)
862rb_yarv_str_eql_internal(
VALUE str1,
VALUE str2)
865 return rb_str_eql_internal(str1, str2);
876rb_yarv_ary_entry_internal(
VALUE ary,
long offset)
878 return rb_ary_entry_internal(ary, offset);
884rb_yjit_rb_ary_subseq_length(
VALUE ary,
long beg)
887 return rb_ary_subseq(ary, beg,
len);
893 return rb_fix_div_fix(recv, obj);
899 return rb_fix_mod_fix(recv, obj);
905rb_yjit_ruby2_keywords_splat_p(
VALUE obj)
909 if (
len == 0)
return 0;
923 if (len < 0 || len > VM_ARGC_STACK_MAX)
return Qfalse;
943rb_yjit_splat_varg_cfunc(
VALUE *stack_splat_array)
945 VALUE splat_array = *stack_splat_array;
960rb_yjit_dump_iseq_loc(
const rb_iseq_t *iseq, uint32_t insn_idx)
964 VALUE path = rb_iseq_path(iseq);
966 fprintf(stderr,
"%s %.*s:%u\n", __func__, (
int)
len, ptr, rb_iseq_line_no(iseq, insn_idx));
971num_digits(
int integer)
974 while (integer /= 10) {
982rb_yjit_iseq_inspect(
const rb_iseq_t *iseq)
984 const char *label = RSTRING_PTR(iseq->body->location.label);
985 const char *path = RSTRING_PTR(rb_iseq_path(iseq));
986 int lineno = iseq->body->location.code_location.beg_pos.lineno;
988 char *buf =
ZALLOC_N(
char, strlen(label) + strlen(path) + num_digits(lineno) + 3);
989 sprintf(buf,
"%s@%s:%d", label, path, lineno);
1015rb_RSTRUCT_LEN(
VALUE st)
1017 return RSTRUCT_LEN(st);
1026 RSTRUCT_SET(st, k, v);
1036rb_BASIC_OP_UNREDEFINED_P(
enum ruby_basic_operators bop, uint32_t klass)
1038 return BASIC_OP_UNREDEFINED_P(bop, klass);
1042rb_RCLASS_ORIGIN(
VALUE c)
1044 return RCLASS_ORIGIN(c);
1049rb_ENCODING_GET(
VALUE obj)
1055rb_yjit_multi_ractor_p(
void)
1057 return rb_multi_ractor_p();
1062rb_assert_iseq_handle(
VALUE handle)
1068rb_IMEMO_TYPE_P(
VALUE imemo,
enum imemo_type imemo_type)
1070 return IMEMO_TYPE_P(imemo, imemo_type);
1076 return (ice->flags & IMEMO_CONST_CACHE_SHAREABLE) != 0;
1080rb_assert_cme_handle(
VALUE handle)
1088 rb_iseq_callback callback;
1094for_each_iseq_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1098 for (; v != (
VALUE)vend; v += stride) {
1099 void *ptr = rb_asan_poisoned_object_p(v);
1100 rb_asan_unpoison_object(v,
false);
1102 if (rb_obj_is_iseq(v)) {
1104 callback_data->callback(iseq, callback_data->data);
1107 asan_poison_object_if(ptr, v);
1115rb_yjit_for_each_iseq(rb_iseq_callback callback,
void *data)
1118 rb_objspace_each_objects(for_each_iseq_i, (
void *)&callback_data);
1124rb_yjit_obj_written(
VALUE old,
VALUE young,
const char *file,
int line)
1126 rb_obj_written(old,
Qundef, young, file, line);
1134rb_yjit_vm_lock_then_barrier(
unsigned int *recursive_lock_level,
const char *file,
int line)
1136 rb_vm_lock_enter(recursive_lock_level, file, line);
1143rb_yjit_vm_unlock(
unsigned int *recursive_lock_level,
const char *file,
int line)
1145 rb_vm_lock_leave(recursive_lock_level, file, line);
1156 uintptr_t code_ptr = (uintptr_t)rb_yjit_iseq_gen_entry_point(iseq, ec, jit_exception);
1158 if (jit_exception) {
1159 iseq->body->jit_exception = (rb_jit_func_t)code_ptr;
1162 iseq->body->jit_entry = (rb_jit_func_t)code_ptr;
1175rb_yjit_invalidate_all_method_lookup_assumptions(
void)
1183rb_object_shape_count(
void)
1186 return ULONG2NUM((
unsigned long)GET_SHAPE_TREE()->next_shape_id);
1192rb_yjit_assert_holding_vm_lock(
void)
1194 ASSERT_vm_locking();
1199rb_yjit_sendish_sp_pops(
const struct rb_callinfo *ci)
1201 return 1 - sp_inc_of_sendish(ci);
1206rb_yjit_invokeblock_sp_pops(
const struct rb_callinfo *ci)
1208 return 1 - sp_inc_of_invokeblock(ci);
1214rb_yjit_set_exception_return(
rb_control_frame_t *cfp,
void *leave_exit,
void *leave_exception)
1216 if (VM_FRAME_FINISHED_P(cfp)) {
1218 cfp->jit_return = leave_exit;
1220 else if (cfp->jit_return) {
1221 while (!VM_FRAME_FINISHED_P(cfp)) {
1222 if (cfp->jit_return == leave_exit) {
1227 cfp->jit_return = leave_exception;
1230 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1236 cfp->jit_return = leave_exception;
1258#ifdef YJIT_C_BUILTIN
1261#define yjit_c_builtin_p rb_yjit_c_builtin_p
1265#include "yjit.rbinc"
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_profile_frame_full_label(VALUE frame)
Identical to rb_profile_frame_label(), except it returns a qualified result.
VALUE rb_profile_frame_absolute_path(VALUE frame)
Identical to rb_profile_frame_path(), except it tries to expand the returning path.
VALUE rb_profile_frame_path(VALUE frame)
Queries the path of the passed backtrace.
VALUE rb_profile_frame_first_lineno(VALUE frame)
Queries the first line of the method of the passed frame pointer.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
uint32_t rb_event_flag_t
Represents event(s).
static VALUE RB_FL_TEST(VALUE obj, VALUE flags)
Tests if the given flag(s) are set or not.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
static int RB_ENCODING_GET(VALUE obj)
Just another name of rb_enc_get_index.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RARRAY_LEN
Just another name of rb_array_len.
static long rb_array_len(VALUE a)
Queries the length of the array.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define errno
Ractor-aware version of errno.
#define RTEST
This is an old name of RB_TEST.
VALUE * ivptr
Pointer to a C array that holds instance variables.
struct rb_id_table * iv_index_tbl
This is a table that holds instance variable name to index mapping.
struct RObject::@50::@51 heap
Object that use separated memory region for instance variables use this pattern.
struct rb_iseq_constant_body::@154 param
parameter information
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.