Ruby 3.5.0dev (2025-07-01 revision c3bdf7043cca0131e7ca66c1bc76ae6e24dc8965)
yjit.c (c3bdf7043cca0131e7ca66c1bc76ae6e24dc8965)
1// This part of YJIT helps interfacing with the rest of CRuby and with the OS.
2// Sometimes our FFI binding generation tool gives undesirable outputs when it
3// sees C features that Rust doesn't support well. We mitigate that by binding
4// functions which have simple parameter types. The boilerplate C functions for
5// that purpose are in this file.
6// Similarly, we wrap OS facilities we need in simple functions to help with
7// FFI and to avoid the need to use external crates.io Rust libraries.
8
9#include "internal.h"
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"
19#include "vm_core.h"
20#include "vm_callinfo.h"
21#include "builtin.h"
22#include "insns.inc"
23#include "insns_info.inc"
24#include "vm_sync.h"
25#include "yjit.h"
26#include "vm_insnhelper.h"
27#include "probes.h"
28#include "probes_helper.h"
29#include "iseq.h"
30#include "ruby/debug.h"
31#include "internal/cont.h"
32#include "zjit.h"
33
34// For mmapp(), sysconf()
35#ifndef _WIN32
36#include <unistd.h>
37#include <sys/mman.h>
38#endif
39
40#include <errno.h>
41
42// Field offsets for the RObject struct
43enum robject_offsets {
44 ROBJECT_OFFSET_AS_HEAP_FIELDS = offsetof(struct RObject, as.heap.fields),
45 ROBJECT_OFFSET_AS_ARY = offsetof(struct RObject, as.ary),
46};
47
48// Field offsets for the RString struct
49enum rstring_offsets {
50 RUBY_OFFSET_RSTRING_LEN = offsetof(struct RString, len)
51};
52
53// We need size_t to have a known size to simplify code generation and FFI.
54// TODO(alan): check this in configure.ac to fail fast on 32 bit platforms.
55STATIC_ASSERT(64b_size_t, SIZE_MAX == UINT64_MAX);
56// I don't know any C implementation that has uint64_t and puts padding bits
57// into size_t but the standard seems to allow it.
58STATIC_ASSERT(size_t_no_padding_bits, sizeof(size_t) == sizeof(uint64_t));
59
60// This build config impacts the pointer tagging scheme and we only want to
61// support one scheme for simplicity.
62STATIC_ASSERT(pointer_tagging_scheme, USE_FLONUM);
63
64// NOTE: We can trust that uint8_t has no "padding bits" since the C spec
65// guarantees it. Wording about padding bits is more explicit in C11 compared
66// to C99. See C11 7.20.1.1p2. All this is to say we have _some_ standards backing to
67// use a Rust `*mut u8` to represent a C `uint8_t *`.
68//
69// If we don't want to trust that we can interpreter the C standard correctly, we
70// could outsource that work to the Rust standard library by sticking to fundamental
71// types in C such as int, long, etc. and use `std::os::raw::c_long` and friends on
72// the Rust side.
73//
74// What's up with the long prefix? Even though we build with `-fvisibility=hidden`
75// we are sometimes a static library where the option doesn't prevent name collision.
76// The "_yjit_" part is for trying to be informative. We might want different
77// suffixes for symbols meant for Rust and symbols meant for broader CRuby.
78
79bool
80rb_yjit_mark_writable(void *mem_block, uint32_t mem_size)
81{
82 return mprotect(mem_block, mem_size, PROT_READ | PROT_WRITE) == 0;
83}
84
85void
86rb_yjit_mark_executable(void *mem_block, uint32_t mem_size)
87{
88 // Do not call mprotect when mem_size is zero. Some platforms may return
89 // an error for it. https://github.com/Shopify/ruby/issues/450
90 if (mem_size == 0) {
91 return;
92 }
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));
96 }
97}
98
99// Free the specified memory block.
100bool
101rb_yjit_mark_unused(void *mem_block, uint32_t mem_size)
102{
103 // On Linux, you need to use madvise MADV_DONTNEED to free memory.
104 // We might not need to call this on macOS, but it's not really documented.
105 // We generally prefer to do the same thing on both to ease testing too.
106 madvise(mem_block, mem_size, MADV_DONTNEED);
107
108 // On macOS, mprotect PROT_NONE seems to reduce RSS.
109 // We also call this on Linux to avoid executing unused pages.
110 return mprotect(mem_block, mem_size, PROT_NONE) == 0;
111}
112
113long
114rb_yjit_array_len(VALUE a)
115{
116 return rb_array_len(a);
117}
118
119// `start` is inclusive and `end` is exclusive.
120void
121rb_yjit_icache_invalidate(void *start, void *end)
122{
123 // Clear/invalidate the instruction cache. Compiles to nothing on x86_64
124 // but required on ARM before running freshly written code.
125 // On Darwin it's the same as calling sys_icache_invalidate().
126#ifdef __GNUC__
127 __builtin___clear_cache(start, end);
128#elif defined(__aarch64__)
129#error No instruction cache clear available with this compiler on Aarch64!
130#endif
131}
132
133# define PTR2NUM(x) (rb_int2inum((intptr_t)(void *)(x)))
134
135// For a given raw_sample (frame), set the hash with the caller's
136// name, file, and line number. Return the hash with collected frame_info.
137static void
138rb_yjit_add_frame(VALUE hash, VALUE frame)
139{
140 VALUE frame_id = PTR2NUM(frame);
141
142 if (RTEST(rb_hash_aref(hash, frame_id))) {
143 return;
144 }
145 else {
146 VALUE frame_info = rb_hash_new();
147 // Full label for the frame
149 // Absolute path of the frame from rb_iseq_realpath
151 // Line number of the frame
153
154 // If absolute path isn't available use the rb_iseq_path
155 if (NIL_P(file)) {
156 file = rb_profile_frame_path(frame);
157 }
158
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());
165
166 if (line != INT2FIX(0)) {
167 rb_hash_aset(frame_info, ID2SYM(rb_intern("line")), line);
168 }
169
170 rb_hash_aset(hash, frame_id, frame_info);
171 }
172}
173
174// Parses the YjitExitLocations raw_samples and line_samples collected by
175// rb_yjit_record_exit_stack and turns them into 3 hashes (raw, lines, and frames) to
176// be used by RubyVM::YJIT.exit_locations. yjit_raw_samples represents the raw frames information
177// (without name, file, and line), and yjit_line_samples represents the line information
178// of the iseq caller.
179VALUE
180rb_yjit_exit_locations_dict(VALUE *yjit_raw_samples, int *yjit_line_samples, int samples_len)
181{
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();
186 int idx = 0;
187
188 // While the index is less than samples_len, parse yjit_raw_samples and
189 // yjit_line_samples, then add casted values to raw_samples and line_samples array.
190 while (idx < samples_len) {
191 int num = (int)yjit_raw_samples[idx];
192 int line_num = (int)yjit_line_samples[idx];
193 idx++;
194
195 // + 1 as we append an additional sample for the insn
196 rb_ary_push(raw_samples, SIZET2NUM(num + 1));
197 rb_ary_push(line_samples, INT2NUM(line_num + 1));
198
199 // Loop through the length of samples_len and add data to the
200 // frames hash. Also push the current value onto the raw_samples
201 // and line_samples array respectively.
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]));
206 idx++;
207 }
208
209 rb_ary_push(raw_samples, SIZET2NUM(yjit_raw_samples[idx]));
210 rb_ary_push(line_samples, INT2NUM(yjit_line_samples[idx]));
211 idx++;
212
213 rb_ary_push(raw_samples, SIZET2NUM(yjit_raw_samples[idx]));
214 rb_ary_push(line_samples, INT2NUM(yjit_line_samples[idx]));
215 idx++;
216 }
217
218 // Set add the raw_samples, line_samples, and frames to the results
219 // hash.
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);
223
224 return result;
225}
226
227uint32_t
228rb_yjit_get_page_size(void)
229{
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");
233
234 // 1 GiB limit. x86 CPUs with PDPE1GB can do this and anything larger is unexpected.
235 // Though our design sort of assume we have fine grained control over memory protection
236 // which require small page sizes.
237 if (page_size > 0x40000000l) rb_bug("yjit page size too large");
238
239 return (uint32_t)page_size;
240#else
241#error "YJIT supports POSIX only for now"
242#endif
243}
244
245#if defined(MAP_FIXED_NOREPLACE) && defined(_SC_PAGESIZE)
246// Align the current write position to a multiple of bytes
247static uint8_t *
248align_ptr(uint8_t *ptr, uint32_t multiple)
249{
250 // Compute the pointer modulo the given alignment boundary
251 uint32_t rem = ((uint32_t)(uintptr_t)ptr) % multiple;
252
253 // If the pointer is already aligned, stop
254 if (rem == 0)
255 return ptr;
256
257 // Pad the pointer by the necessary amount to align it
258 uint32_t pad = multiple - rem;
259
260 return ptr + pad;
261}
262#endif
263
264// Address space reservation. Memory pages are mapped on an as needed basis.
265// See the Rust mm module for details.
266uint8_t *
267rb_yjit_reserve_addr_space(uint32_t mem_size)
268{
269#ifndef _WIN32
270 uint8_t *mem_block;
271
272 // On Linux
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;
277 // Align the requested address to page size
278 uint8_t *req_addr = align_ptr(cfunc_sample_addr, page_size);
279
280 // Probe for addresses close to this function using MAP_FIXED_NOREPLACE
281 // to improve odds of being in range for 32-bit relative call instructions.
282 do {
283 mem_block = mmap(
284 req_addr,
285 mem_size,
286 PROT_NONE,
287 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE,
288 -1,
289 0
290 );
291
292 // If we succeeded, stop
293 if (mem_block != MAP_FAILED) {
294 ruby_annotate_mmap(mem_block, mem_size, "Ruby:rb_yjit_reserve_addr_space");
295 break;
296 }
297
298 // -4MiB. Downwards to probe away from the heap. (On x86/A64 Linux
299 // main_code_addr < heap_addr, and in case we are in a shared
300 // library mapped higher than the heap, downwards is still better
301 // since it's towards the end of the heap rather than the stack.)
302 req_addr -= 4 * 1024 * 1024;
303 } while (req_addr < probe_region_end);
304
305 // On MacOS and other platforms
306 #else
307 // Try to map a chunk of memory as executable
308 mem_block = mmap(
309 (void *)rb_yjit_reserve_addr_space,
310 mem_size,
311 PROT_NONE,
312 MAP_PRIVATE | MAP_ANONYMOUS,
313 -1,
314 0
315 );
316 #endif
317
318 // Fallback
319 if (mem_block == MAP_FAILED) {
320 // Try again without the address hint (e.g., valgrind)
321 mem_block = mmap(
322 NULL,
323 mem_size,
324 PROT_NONE,
325 MAP_PRIVATE | MAP_ANONYMOUS,
326 -1,
327 0
328 );
329
330 if (mem_block != MAP_FAILED) {
331 ruby_annotate_mmap(mem_block, mem_size, "Ruby:rb_yjit_reserve_addr_space:fallback");
332 }
333 }
334
335 // Check that the memory mapping was successful
336 if (mem_block == MAP_FAILED) {
337 perror("ruby: yjit: mmap:");
338 if(errno == ENOMEM) {
339 // No crash report if it's only insufficient memory
340 exit(EXIT_FAILURE);
341 }
342 rb_bug("mmap failed");
343 }
344
345 return mem_block;
346#else
347 // Windows not supported for now
348 return NULL;
349#endif
350}
351
352// Is anyone listening for :c_call and :c_return event currently?
353bool
354rb_c_method_tracing_currently_enabled(const rb_execution_context_t *ec)
355{
356 rb_event_flag_t tracing_events;
357 if (rb_multi_ractor_p()) {
358 tracing_events = ruby_vm_event_enabled_global_flags;
359 }
360 else {
361 // At the time of writing, events are never removed from
362 // ruby_vm_event_enabled_global_flags so always checking using it would
363 // mean we don't compile even after tracing is disabled.
364 tracing_events = rb_ec_ractor_hooks(ec)->events;
365 }
366
367 return tracing_events & (RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN);
368}
369
370// The code we generate in gen_send_cfunc() doesn't fire the c_return TracePoint event
371// like the interpreter. When tracing for c_return is enabled, we patch the code after
372// the C method return to call into this to fire the event.
373void
374rb_full_cfunc_return(rb_execution_context_t *ec, VALUE return_value)
375{
376 rb_control_frame_t *cfp = ec->cfp;
377 RUBY_ASSERT_ALWAYS(cfp == GET_EC()->cfp);
378 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
379
380 RUBY_ASSERT_ALWAYS(RUBYVM_CFUNC_FRAME_P(cfp));
381 RUBY_ASSERT_ALWAYS(me->def->type == VM_METHOD_TYPE_CFUNC);
382
383 // CHECK_CFP_CONSISTENCY("full_cfunc_return"); TODO revive this
384
385 // Pop the C func's frame and fire the c_return TracePoint event
386 // Note that this is the same order as vm_call_cfunc_with_frame().
387 rb_vm_pop_frame(ec);
388 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, return_value);
389 // Note, this deviates from the interpreter in that users need to enable
390 // a c_return TracePoint for this DTrace hook to work. A reasonable change
391 // since the Ruby return event works this way as well.
392 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
393
394 // Push return value into the caller's stack. We know that it's a frame that
395 // uses cfp->sp because we are patching a call done with gen_send_cfunc().
396 ec->cfp->sp[0] = return_value;
397 ec->cfp->sp++;
398}
399
400// TODO(alan): consider using an opaque pointer for the payload rather than a void pointer
401void *
402rb_iseq_get_yjit_payload(const rb_iseq_t *iseq)
403{
404 RUBY_ASSERT_ALWAYS(IMEMO_TYPE_P(iseq, imemo_iseq));
405 if (iseq->body) {
406 return iseq->body->yjit_payload;
407 }
408 else {
409 // Body is NULL when constructing the iseq.
410 return NULL;
411 }
412}
413
414void
415rb_iseq_set_yjit_payload(const rb_iseq_t *iseq, void *payload)
416{
417 RUBY_ASSERT_ALWAYS(IMEMO_TYPE_P(iseq, imemo_iseq));
418 RUBY_ASSERT_ALWAYS(iseq->body);
419 RUBY_ASSERT_ALWAYS(NULL == iseq->body->yjit_payload);
420 iseq->body->yjit_payload = payload;
421}
422
423void
424rb_iseq_reset_jit_func(const rb_iseq_t *iseq)
425{
426 RUBY_ASSERT_ALWAYS(IMEMO_TYPE_P(iseq, imemo_iseq));
427 iseq->body->jit_entry = NULL;
428 iseq->body->jit_exception = NULL;
429 // Enable re-compiling this ISEQ. Event when it's invalidated for TracePoint,
430 // we'd like to re-compile ISEQs that haven't been converted to trace_* insns.
431 iseq->body->jit_entry_calls = 0;
432 iseq->body->jit_exception_calls = 0;
433}
434
435rb_proc_t *
436rb_yjit_get_proc_ptr(VALUE procv)
437{
438 rb_proc_t *proc;
439 GetProcPtr(procv, proc);
440 return proc;
441}
442
443// This is defined only as a named struct inside rb_iseq_constant_body.
444// By giving it a separate typedef, we make it nameable by rust-bindgen.
445// Bindgen's temp/anon name isn't guaranteed stable.
446typedef struct rb_iseq_param_keyword rb_seq_param_keyword_struct;
447
448ID rb_get_symbol_id(VALUE namep);
449
450VALUE
451rb_get_def_bmethod_proc(rb_method_definition_t *def)
452{
453 RUBY_ASSERT(def->type == VM_METHOD_TYPE_BMETHOD);
454 return def->body.bmethod.proc;
455}
456
457VALUE
458rb_optimized_call(VALUE *recv, rb_execution_context_t *ec, int argc, VALUE *argv, int kw_splat, VALUE block_handler)
459{
460 rb_proc_t *proc;
461 GetProcPtr(recv, proc);
462 return rb_vm_invoke_proc(ec, proc, argc, argv, kw_splat, block_handler);
463}
464
465unsigned int
466rb_yjit_iseq_builtin_attrs(const rb_iseq_t *iseq)
467{
468 return iseq->body->builtin_attrs;
469}
470
471// If true, the iseq has only opt_invokebuiltin_delegate(_leave) and leave insns.
472static bool
473invokebuiltin_delegate_leave_p(const rb_iseq_t *iseq)
474{
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))) {
477 return false;
478 }
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)) &&
481 insn2 == BIN(leave);
482}
483
484// Return an rb_builtin_function if the iseq contains only that builtin function.
485const struct rb_builtin_function *
486rb_yjit_builtin_function(const rb_iseq_t *iseq)
487{
488 if (invokebuiltin_delegate_leave_p(iseq)) {
489 return (const struct rb_builtin_function *)iseq->body->iseq_encoded[1];
490 }
491 else {
492 return NULL;
493 }
494}
495
496VALUE
497rb_yjit_str_simple_append(VALUE str1, VALUE str2)
498{
499 return rb_str_cat(str1, RSTRING_PTR(str2), RSTRING_LEN(str2));
500}
501
502void
503rb_set_cfp_pc(struct rb_control_frame_struct *cfp, const VALUE *pc)
504{
505 cfp->pc = pc;
506}
507
508void
509rb_set_cfp_sp(struct rb_control_frame_struct *cfp, VALUE *sp)
510{
511 cfp->sp = sp;
512}
513
514extern VALUE *rb_vm_base_ptr(struct rb_control_frame_struct *cfp);
515
516// YJIT needs this function to never allocate and never raise
517VALUE
518rb_yarv_str_eql_internal(VALUE str1, VALUE str2)
519{
520 // We wrap this since it's static inline
521 return rb_str_eql_internal(str1, str2);
522}
523
524VALUE
525rb_str_neq_internal(VALUE str1, VALUE str2)
526{
527 return rb_str_eql_internal(str1, str2) == Qtrue ? Qfalse : Qtrue;
528}
529
530extern VALUE rb_ary_unshift_m(int argc, VALUE *argv, VALUE ary);
531
532VALUE
533rb_yjit_rb_ary_subseq_length(VALUE ary, long beg)
534{
535 long len = RARRAY_LEN(ary);
536 return rb_ary_subseq(ary, beg, len);
537}
538
539VALUE
540rb_yjit_fix_div_fix(VALUE recv, VALUE obj)
541{
542 return rb_fix_div_fix(recv, obj);
543}
544
545VALUE
546rb_yjit_fix_mod_fix(VALUE recv, VALUE obj)
547{
548 return rb_fix_mod_fix(recv, obj);
549}
550
551// Return non-zero when `obj` is an array and its last item is a
552// `ruby2_keywords` hash. We don't support this kind of splat.
553size_t
554rb_yjit_ruby2_keywords_splat_p(VALUE obj)
555{
556 if (!RB_TYPE_P(obj, T_ARRAY)) return 0;
557 long len = RARRAY_LEN(obj);
558 if (len == 0) return 0;
559 VALUE last = RARRAY_AREF(obj, len - 1);
560 if (!RB_TYPE_P(last, T_HASH)) return 0;
561 return FL_TEST_RAW(last, RHASH_PASS_AS_KEYWORDS);
562}
563
564// Checks to establish preconditions for rb_yjit_splat_varg_cfunc()
565VALUE
566rb_yjit_splat_varg_checks(VALUE *sp, VALUE splat_array, rb_control_frame_t *cfp)
567{
568 // We inserted a T_ARRAY guard before this call
569 long len = RARRAY_LEN(splat_array);
570
571 // Large splat arrays need a separate allocation
572 if (len < 0 || len > VM_ARGC_STACK_MAX) return Qfalse;
573
574 // Would we overflow if we put the contents of the array onto the stack?
575 if (sp + len > (VALUE *)(cfp - 2)) return Qfalse;
576
577 // Reject keywords hash since that requires duping it sometimes
578 if (len > 0) {
579 VALUE last_hash = RARRAY_AREF(splat_array, len - 1);
580 if (RB_TYPE_P(last_hash, T_HASH) &&
581 FL_TEST_RAW(last_hash, RHASH_PASS_AS_KEYWORDS)) {
582 return Qfalse;
583 }
584 }
585
586 return Qtrue;
587}
588
589// Push array elements to the stack for a C method that has a variable number
590// of parameters. Returns the number of arguments the splat array contributes.
591int
592rb_yjit_splat_varg_cfunc(VALUE *stack_splat_array)
593{
594 VALUE splat_array = *stack_splat_array;
595 int len;
596
597 // We already checked that length fits in `int`
598 RUBY_ASSERT(RB_TYPE_P(splat_array, T_ARRAY));
599 len = (int)RARRAY_LEN(splat_array);
600
601 // Push the contents of the array onto the stack
602 MEMCPY(stack_splat_array, RARRAY_CONST_PTR(splat_array), VALUE, len);
603
604 return len;
605}
606
607// Print the Ruby source location of some ISEQ for debugging purposes
608void
609rb_yjit_dump_iseq_loc(const rb_iseq_t *iseq, uint32_t insn_idx)
610{
611 char *ptr;
612 long len;
613 VALUE path = rb_iseq_path(iseq);
614 RSTRING_GETMEM(path, ptr, len);
615 fprintf(stderr, "%s %.*s:%u\n", __func__, (int)len, ptr, rb_iseq_line_no(iseq, insn_idx));
616}
617
618// Get the number of digits required to print an integer
619static int
620num_digits(int integer)
621{
622 int num = 1;
623 while (integer /= 10) {
624 num++;
625 }
626 return num;
627}
628
629// Allocate a C string that formats an ISEQ label like iseq_inspect()
630char *
631rb_yjit_iseq_inspect(const rb_iseq_t *iseq)
632{
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;
636
637 char *buf = ZALLOC_N(char, strlen(label) + strlen(path) + num_digits(lineno) + 3);
638 sprintf(buf, "%s@%s:%d", label, path, lineno);
639 return buf;
640}
641
642// There are RSTRUCT_SETs in ruby/internal/core/rstruct.h and internal/struct.h
643// with different types (int vs long) for k. Here we use the one from ruby/internal/core/rstruct.h,
644// which takes an int.
645void
646rb_RSTRUCT_SET(VALUE st, int k, VALUE v)
647{
648 RSTRUCT_SET(st, k, v);
649}
650
651// Return the string encoding index
652int
653rb_ENCODING_GET(VALUE obj)
654{
655 return RB_ENCODING_GET(obj);
656}
657
658bool
659rb_yjit_multi_ractor_p(void)
660{
661 return rb_multi_ractor_p();
662}
663
664bool
665rb_yjit_constcache_shareable(const struct iseq_inline_constant_cache_entry *ice)
666{
667 return (ice->flags & IMEMO_CONST_CACHE_SHAREABLE) != 0;
668}
669
670// Used for passing a callback and other data over rb_objspace_each_objects
672 rb_iseq_callback callback;
673 void *data;
674};
675
676// Heap-walking callback for rb_yjit_for_each_iseq().
677static int
678for_each_iseq_i(void *vstart, void *vend, size_t stride, void *data)
679{
680 const struct iseq_callback_data *callback_data = (struct iseq_callback_data *)data;
681 VALUE v = (VALUE)vstart;
682 for (; v != (VALUE)vend; v += stride) {
683 void *ptr = rb_asan_poisoned_object_p(v);
684 rb_asan_unpoison_object(v, false);
685
686 if (rb_obj_is_iseq(v)) {
687 rb_iseq_t *iseq = (rb_iseq_t *)v;
688 callback_data->callback(iseq, callback_data->data);
689 }
690
691 asan_poison_object_if(ptr, v);
692 }
693 return 0;
694}
695
696// Iterate through the whole GC heap and invoke a callback for each iseq.
697// Used for global code invalidation.
698void
699rb_yjit_for_each_iseq(rb_iseq_callback callback, void *data)
700{
701 struct iseq_callback_data callback_data = { .callback = callback, .data = data };
702 rb_objspace_each_objects(for_each_iseq_i, (void *)&callback_data);
703}
704
705// For running write barriers from Rust. Required when we add a new edge in the
706// object graph from `old` to `young`.
707void
708rb_yjit_obj_written(VALUE old, VALUE young, const char *file, int line)
709{
710 rb_obj_written(old, Qundef, young, file, line);
711}
712
713// Acquire the VM lock and then signal all other Ruby threads (ractors) to
714// contend for the VM lock, putting them to sleep. YJIT uses this to evict
715// threads running inside generated code so among other things, it can
716// safely change memory protection of regions housing generated code.
717void
718rb_yjit_vm_lock_then_barrier(unsigned int *recursive_lock_level, const char *file, int line)
719{
720 rb_vm_lock_enter(recursive_lock_level, file, line);
721 rb_vm_barrier();
722}
723
724// Release the VM lock. The lock level must point to the same integer used to
725// acquire the lock.
726void
727rb_yjit_vm_unlock(unsigned int *recursive_lock_level, const char *file, int line)
728{
729 rb_vm_lock_leave(recursive_lock_level, file, line);
730}
731
732void
733rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception)
734{
735 RB_VM_LOCKING() { rb_vm_barrier();
736
737 // Compile a block version starting at the current instruction
738 uint8_t *rb_yjit_iseq_gen_entry_point(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception); // defined in Rust
739 uintptr_t code_ptr = (uintptr_t)rb_yjit_iseq_gen_entry_point(iseq, ec, jit_exception);
740
741 if (jit_exception) {
742 iseq->body->jit_exception = (rb_jit_func_t)code_ptr;
743 }
744 else {
745 iseq->body->jit_entry = (rb_jit_func_t)code_ptr;
746 }
747}
748}
749
750// GC root for interacting with the GC
752 bool unused; // empty structs are not legal in C99
753};
754
755// For dealing with refinements
756void
757rb_yjit_invalidate_all_method_lookup_assumptions(void)
758{
759 // It looks like Module#using actually doesn't need to invalidate all the
760 // method caches, so we do nothing here for now.
761}
762
763// Number of object shapes, which might be useful for investigating YJIT exit reasons.
764VALUE
765rb_object_shape_count(void)
766{
767 // next_shape_id starts from 0, so it's the same as the count
768 return ULONG2NUM((unsigned long)rb_shape_tree.next_shape_id);
769}
770
771bool
772rb_yjit_shape_too_complex_p(shape_id_t shape_id)
773{
774 return rb_shape_too_complex_p(shape_id);
775}
776
777bool
778rb_yjit_shape_obj_too_complex_p(VALUE obj)
779{
780 return rb_shape_obj_too_complex_p(obj);
781}
782
783attr_index_t
784rb_yjit_shape_capacity(shape_id_t shape_id)
785{
786 return RSHAPE_CAPACITY(shape_id);
787}
788
789attr_index_t
790rb_yjit_shape_index(shape_id_t shape_id)
791{
792 return RSHAPE_INDEX(shape_id);
793}
794
795// Assert that we have the VM lock. Relevant mostly for multi ractor situations.
796// The GC takes the lock before calling us, and this asserts that it indeed happens.
797void
798rb_yjit_assert_holding_vm_lock(void)
799{
800 ASSERT_vm_locking();
801}
802
803// The number of stack slots that vm_sendish() pops for send and invokesuper.
804size_t
805rb_yjit_sendish_sp_pops(const struct rb_callinfo *ci)
806{
807 return 1 - sp_inc_of_sendish(ci); // + 1 to ignore return value push
808}
809
810// The number of stack slots that vm_sendish() pops for invokeblock.
811size_t
812rb_yjit_invokeblock_sp_pops(const struct rb_callinfo *ci)
813{
814 return 1 - sp_inc_of_invokeblock(ci); // + 1 to ignore return value push
815}
816
817// Setup jit_return to avoid returning a non-Qundef value on a non-FINISH frame.
818// See [jit_compile_exception] for details.
819void
820rb_yjit_set_exception_return(rb_control_frame_t *cfp, void *leave_exit, void *leave_exception)
821{
822 if (VM_FRAME_FINISHED_P(cfp)) {
823 // If it's a FINISH frame, just normally exit with a non-Qundef value.
824 cfp->jit_return = leave_exit;
825 }
826 else if (cfp->jit_return) {
827 while (!VM_FRAME_FINISHED_P(cfp)) {
828 if (cfp->jit_return == leave_exit) {
829 // Unlike jit_exec(), leave_exit is not safe on a non-FINISH frame on
830 // jit_exec_exception(). See [jit_exec] and [jit_exec_exception] for
831 // details. Exit to the interpreter with Qundef to let it keep executing
832 // other Ruby frames.
833 cfp->jit_return = leave_exception;
834 return;
835 }
836 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
837 }
838 }
839 else {
840 // If the caller was not JIT code, exit to the interpreter with Qundef
841 // to keep executing Ruby frames with the interpreter.
842 cfp->jit_return = leave_exception;
843 }
844}
845
846// Primitives used by yjit.rb
847VALUE rb_yjit_stats_enabled_p(rb_execution_context_t *ec, VALUE self);
848VALUE rb_yjit_print_stats_p(rb_execution_context_t *ec, VALUE self);
849VALUE rb_yjit_log_enabled_p(rb_execution_context_t *c, VALUE self);
850VALUE rb_yjit_print_log_p(rb_execution_context_t *c, VALUE self);
851VALUE rb_yjit_trace_exit_locations_enabled_p(rb_execution_context_t *ec, VALUE self);
852VALUE rb_yjit_get_stats(rb_execution_context_t *ec, VALUE self, VALUE key);
853VALUE rb_yjit_reset_stats_bang(rb_execution_context_t *ec, VALUE self);
854VALUE rb_yjit_get_log(rb_execution_context_t *ec, VALUE self);
855VALUE rb_yjit_disasm_iseq(rb_execution_context_t *ec, VALUE self, VALUE iseq);
856VALUE rb_yjit_insns_compiled(rb_execution_context_t *ec, VALUE self, VALUE iseq);
857VALUE rb_yjit_code_gc(rb_execution_context_t *ec, VALUE self);
858VALUE rb_yjit_simulate_oom_bang(rb_execution_context_t *ec, VALUE self);
859VALUE rb_yjit_get_exit_locations(rb_execution_context_t *ec, VALUE self);
860VALUE rb_yjit_enable(rb_execution_context_t *ec, VALUE self, VALUE gen_stats, VALUE print_stats, VALUE gen_compilation_log, VALUE print_compilation_log, VALUE mem_size, VALUE call_threshold);
861VALUE rb_yjit_c_builtin_p(rb_execution_context_t *ec, VALUE self);
862
863// Allow YJIT_C_BUILTIN macro to force --yjit-c-builtin
864#ifdef YJIT_C_BUILTIN
865static VALUE yjit_c_builtin_p(rb_execution_context_t *ec, VALUE self) { return Qtrue; }
866#else
867#define yjit_c_builtin_p rb_yjit_c_builtin_p
868#endif
869
870// Preprocessed yjit.rb generated during build
871#include "yjit.rbinc"
872
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
Definition assert.h:199
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
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.
Definition event.h:43
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:401
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
static int RB_ENCODING_GET(VALUE obj)
Just another name of rb_enc_get_index.
Definition encoding.h:195
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.
Definition string.c:3523
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
static long rb_array_len(VALUE a)
Queries the length of the array.
Definition rarray.h:255
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:488
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#define RTEST
This is an old name of RB_TEST.
#define USE_FLONUM
Ruby's ordinal objects.
Definition robject.h:83
VALUE * fields
Pointer to a C array that holds instance variables.
Definition robject.h:97
struct RObject::@49::@50 heap
Object that use separated memory region for instance variables use this pattern.
Ruby's String.
Definition rstring.h:196
Definition vm_core.h:261
Definition method.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376