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();
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];
202 for (
int o = 0; o < num; o++) {
203 rb_yjit_add_frame(frames, yjit_raw_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;
402rb_iseq_get_yjit_payload(
const rb_iseq_t *iseq)
406 return iseq->body->yjit_payload;
415rb_iseq_set_yjit_payload(
const rb_iseq_t *iseq,
void *payload)
420 iseq->body->yjit_payload = payload;
424rb_iseq_reset_jit_func(
const rb_iseq_t *iseq)
427 iseq->body->jit_entry = NULL;
428 iseq->body->jit_exception = NULL;
431 iseq->body->jit_entry_calls = 0;
432 iseq->body->jit_exception_calls = 0;
436rb_yjit_get_proc_ptr(
VALUE procv)
439 GetProcPtr(procv, proc);
446typedef struct rb_iseq_param_keyword rb_seq_param_keyword_struct;
448ID rb_get_symbol_id(
VALUE namep);
454 return def->body.bmethod.proc;
461 GetProcPtr(recv, proc);
462 return rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, block_handler);
466rb_yjit_iseq_builtin_attrs(
const rb_iseq_t *iseq)
468 return iseq->body->builtin_attrs;
473invokebuiltin_delegate_leave_p(
const rb_iseq_t *iseq)
475 int insn1 = rb_vm_insn_addr2opcode((
void *)iseq->body->iseq_encoded[0]);
476 if ((
int)iseq->body->iseq_size != insn_len(insn1) + insn_len(BIN(leave))) {
479 int insn2 = rb_vm_insn_addr2opcode((
void *)iseq->body->iseq_encoded[insn_len(insn1)]);
480 return (insn1 == BIN(opt_invokebuiltin_delegate) || insn1 == BIN(opt_invokebuiltin_delegate_leave)) &&
486rb_yjit_builtin_function(
const rb_iseq_t *iseq)
488 if (invokebuiltin_delegate_leave_p(iseq)) {
497rb_yjit_str_simple_append(
VALUE str1,
VALUE str2)
499 return rb_str_cat(str1, RSTRING_PTR(str2), RSTRING_LEN(str2));
518rb_yarv_str_eql_internal(
VALUE str1,
VALUE str2)
521 return rb_str_eql_internal(str1, str2);
533rb_yjit_rb_ary_subseq_length(
VALUE ary,
long beg)
542 return rb_fix_div_fix(recv, obj);
548 return rb_fix_mod_fix(recv, obj);
554rb_yjit_ruby2_keywords_splat_p(
VALUE obj)
558 if (
len == 0)
return 0;
572 if (len < 0 || len > VM_ARGC_STACK_MAX)
return Qfalse;
592rb_yjit_splat_varg_cfunc(
VALUE *stack_splat_array)
594 VALUE splat_array = *stack_splat_array;
609rb_yjit_dump_iseq_loc(
const rb_iseq_t *iseq, uint32_t insn_idx)
613 VALUE path = rb_iseq_path(iseq);
615 fprintf(stderr,
"%s %.*s:%u\n", __func__, (
int)
len, ptr, rb_iseq_line_no(iseq, insn_idx));
620num_digits(
int integer)
623 while (integer /= 10) {
631rb_yjit_iseq_inspect(
const rb_iseq_t *iseq)
633 const char *label = RSTRING_PTR(iseq->body->location.label);
634 const char *path = RSTRING_PTR(rb_iseq_path(iseq));
635 int lineno = iseq->body->location.code_location.beg_pos.lineno;
637 char *buf =
ZALLOC_N(
char, strlen(label) + strlen(path) + num_digits(lineno) + 3);
638 sprintf(buf,
"%s@%s:%d", label, path, lineno);
648 RSTRUCT_SET(st, k, v);
653rb_ENCODING_GET(
VALUE obj)
659rb_yjit_multi_ractor_p(
void)
661 return rb_multi_ractor_p();
667 return (ice->flags & IMEMO_CONST_CACHE_SHAREABLE) != 0;
672 rb_iseq_callback callback;
678for_each_iseq_i(
void *vstart,
void *vend,
size_t stride,
void *data)
682 for (; v != (
VALUE)vend; v += stride) {
683 void *ptr = rb_asan_poisoned_object_p(v);
684 rb_asan_unpoison_object(v,
false);
686 if (rb_obj_is_iseq(v)) {
688 callback_data->callback(iseq, callback_data->data);
691 asan_poison_object_if(ptr, v);
699rb_yjit_for_each_iseq(rb_iseq_callback callback,
void *data)
702 rb_objspace_each_objects(for_each_iseq_i, (
void *)&callback_data);
708rb_yjit_obj_written(
VALUE old,
VALUE young,
const char *file,
int line)
710 rb_obj_written(old,
Qundef, young, file, line);
718rb_yjit_vm_lock_then_barrier(
unsigned int *recursive_lock_level,
const char *file,
int line)
720 rb_vm_lock_enter(recursive_lock_level, file, line);
727rb_yjit_vm_unlock(
unsigned int *recursive_lock_level,
const char *file,
int line)
729 rb_vm_lock_leave(recursive_lock_level, file, line);
735 RB_VM_LOCKING() { rb_vm_barrier();
739 uintptr_t code_ptr = (uintptr_t)rb_yjit_iseq_gen_entry_point(iseq, ec, jit_exception);
742 iseq->body->jit_exception = (rb_jit_func_t)code_ptr;
745 iseq->body->jit_entry = (rb_jit_func_t)code_ptr;
757rb_yjit_invalidate_all_method_lookup_assumptions(
void)
765rb_object_shape_count(
void)
768 return ULONG2NUM((
unsigned long)rb_shape_tree.next_shape_id);
772rb_yjit_shape_too_complex_p(shape_id_t shape_id)
774 return rb_shape_too_complex_p(shape_id);
778rb_yjit_shape_obj_too_complex_p(
VALUE obj)
780 return rb_shape_obj_too_complex_p(obj);
784rb_yjit_shape_capacity(shape_id_t shape_id)
786 return RSHAPE_CAPACITY(shape_id);
790rb_yjit_shape_index(shape_id_t shape_id)
792 return RSHAPE_INDEX(shape_id);
798rb_yjit_assert_holding_vm_lock(
void)
805rb_yjit_sendish_sp_pops(
const struct rb_callinfo *ci)
807 return 1 - sp_inc_of_sendish(ci);
812rb_yjit_invokeblock_sp_pops(
const struct rb_callinfo *ci)
814 return 1 - sp_inc_of_invokeblock(ci);
820rb_yjit_set_exception_return(
rb_control_frame_t *cfp,
void *leave_exit,
void *leave_exception)
822 if (VM_FRAME_FINISHED_P(cfp)) {
824 cfp->jit_return = leave_exit;
826 else if (cfp->jit_return) {
827 while (!VM_FRAME_FINISHED_P(cfp)) {
828 if (cfp->jit_return == leave_exit) {
833 cfp->jit_return = leave_exception;
836 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
842 cfp->jit_return = leave_exception;
867#define yjit_c_builtin_p rb_yjit_c_builtin_p
#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).
#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 int RB_ENCODING_GET(VALUE obj)
Just another name of rb_enc_get_index.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_subseq(VALUE ary, long beg, long len)
Obtains a part of the passed array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
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 * fields
Pointer to a C array that holds instance variables.
struct RObject::@49::@50 heap
Object that use separated memory region for instance variables use this pattern.
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.