Ruby 4.1.0dev (2026-11-17 revision 3a19e2efcf133ee35daaeffc36633cd99a9f2c47)
yjit.c (3a19e2efcf133ee35daaeffc36633cd99a9f2c47)
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 "yjit.h"
25#include "zjit.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 "internal/jit.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// We need size_t to have a known size to simplify code generation and FFI.
43// TODO(alan): check this in configure.ac to fail fast on 32 bit platforms.
44STATIC_ASSERT(64b_size_t, SIZE_MAX == UINT64_MAX);
45// I don't know any C implementation that has uint64_t and puts padding bits
46// into size_t but the standard seems to allow it.
47STATIC_ASSERT(size_t_no_padding_bits, sizeof(size_t) == sizeof(uint64_t));
48
49// This build config impacts the pointer tagging scheme and we only want to
50// support one scheme for simplicity.
51STATIC_ASSERT(pointer_tagging_scheme, USE_FLONUM);
52
53// The bindings declare rb_len_t as i64, and codegen reads String and Array
54// lengths at that width.
55STATIC_ASSERT(rb_len_t_matches_bindings, sizeof(rb_len_t) == sizeof(int64_t));
56
57enum yjit_bindgen_constants {
58 // ISEQ_TRANSLATED expands to an enum value through a chain of macros,
59 // which bindgen cannot evaluate, so it needs to be re-exposed here.
60 YJIT_ISEQ_TRANSLATED = ISEQ_TRANSLATED,
61};
62
63// NOTE: We can trust that uint8_t has no "padding bits" since the C spec
64// guarantees it. Wording about padding bits is more explicit in C11 compared
65// to C99. See C11 7.20.1.1p2. All this is to say we have _some_ standards backing to
66// use a Rust `*mut u8` to represent a C `uint8_t *`.
67//
68// If we don't want to trust that we can interpreter the C standard correctly, we
69// could outsource that work to the Rust standard library by sticking to fundamental
70// types in C such as int, long, etc. and use `std::os::raw::c_long` and friends on
71// the Rust side.
72//
73// What's up with the long prefix? Even though we build with `-fvisibility=hidden`
74// we are sometimes a static library where the option doesn't prevent name collision.
75// The "_yjit_" part is for trying to be informative. We might want different
76// suffixes for symbols meant for Rust and symbols meant for broader CRuby.
77
78// For a given raw_sample (frame), set the hash with the caller's
79// name, file, and line number. Return the hash with collected frame_info.
80static void
81rb_yjit_add_frame(VALUE hash, VALUE frame)
82{
83 VALUE frame_id = PTR2NUM(frame);
84
85 if (RTEST(rb_hash_aref(hash, frame_id))) {
86 return;
87 }
88 else {
89 VALUE frame_info = rb_hash_new();
90 // Full label for the frame
92 // Absolute path of the frame from rb_iseq_realpath
94 // Line number of the frame
96
97 // If absolute path isn't available use the rb_iseq_path
98 if (NIL_P(file)) {
99 file = rb_profile_frame_path(frame);
100 }
101
102 rb_hash_aset(frame_info, ID2SYM(rb_intern("name")), name);
103 rb_hash_aset(frame_info, ID2SYM(rb_intern("file")), file);
104 rb_hash_aset(frame_info, ID2SYM(rb_intern("samples")), INT2NUM(0));
105 rb_hash_aset(frame_info, ID2SYM(rb_intern("total_samples")), INT2NUM(0));
106 rb_hash_aset(frame_info, ID2SYM(rb_intern("edges")), rb_hash_new());
107 rb_hash_aset(frame_info, ID2SYM(rb_intern("lines")), rb_hash_new());
108
109 if (line != INT2FIX(0)) {
110 rb_hash_aset(frame_info, ID2SYM(rb_intern("line")), line);
111 }
112
113 rb_hash_aset(hash, frame_id, frame_info);
114 }
115}
116
117// Parses the YjitExitLocations raw_samples and line_samples collected by
118// rb_yjit_record_exit_stack and turns them into 3 hashes (raw, lines, and frames) to
119// be used by RubyVM::YJIT.exit_locations. yjit_raw_samples represents the raw frames information
120// (without name, file, and line), and yjit_line_samples represents the line information
121// of the iseq caller.
122VALUE
123rb_yjit_exit_locations_dict(VALUE *yjit_raw_samples, int *yjit_line_samples, int samples_len)
124{
125 VALUE result = rb_hash_new();
126 VALUE raw_samples = rb_ary_new_capa(samples_len);
127 VALUE line_samples = rb_ary_new_capa(samples_len);
128 VALUE frames = rb_hash_new();
129 int idx = 0;
130
131 // While the index is less than samples_len, parse yjit_raw_samples and
132 // yjit_line_samples, then add casted values to raw_samples and line_samples array.
133 while (idx < samples_len) {
134 int num = (int)yjit_raw_samples[idx];
135 int line_num = (int)yjit_line_samples[idx];
136 idx++;
137
138 // + 1 as we append an additional sample for the insn
139 rb_ary_push(raw_samples, SIZET2NUM(num + 1));
140 rb_ary_push(line_samples, INT2NUM(line_num + 1));
141
142 // Loop through the length of samples_len and add data to the
143 // frames hash. Also push the current value onto the raw_samples
144 // and line_samples array respectively.
145 for (int o = 0; o < num; o++) {
146 rb_yjit_add_frame(frames, yjit_raw_samples[idx]);
147 rb_ary_push(raw_samples, SIZET2NUM(yjit_raw_samples[idx]));
148 rb_ary_push(line_samples, INT2NUM(yjit_line_samples[idx]));
149 idx++;
150 }
151
152 rb_ary_push(raw_samples, SIZET2NUM(yjit_raw_samples[idx]));
153 rb_ary_push(line_samples, INT2NUM(yjit_line_samples[idx]));
154 idx++;
155
156 rb_ary_push(raw_samples, SIZET2NUM(yjit_raw_samples[idx]));
157 rb_ary_push(line_samples, INT2NUM(yjit_line_samples[idx]));
158 idx++;
159 }
160
161 // Set add the raw_samples, line_samples, and frames to the results
162 // hash.
163 rb_hash_aset(result, ID2SYM(rb_intern("raw")), raw_samples);
164 rb_hash_aset(result, ID2SYM(rb_intern("lines")), line_samples);
165 rb_hash_aset(result, ID2SYM(rb_intern("frames")), frames);
166
167 return result;
168}
169
170// Is anyone listening for :c_call and :c_return event currently?
171bool
172rb_c_method_tracing_currently_enabled(const rb_execution_context_t *ec)
173{
174 return ruby_vm_c_events_enabled > 0;
175}
176
177// The code we generate in gen_send_cfunc() doesn't fire the c_return TracePoint event
178// like the interpreter. When tracing for c_return is enabled, we patch the code after
179// the C method return to call into this to fire the event.
180void
181rb_full_cfunc_return(rb_execution_context_t *ec, VALUE return_value)
182{
183 rb_control_frame_t *cfp = ec->cfp;
184 RUBY_ASSERT_ALWAYS(cfp == GET_EC()->cfp);
185 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
186
187 RUBY_ASSERT_ALWAYS(RUBYVM_CFUNC_FRAME_P(cfp));
188 RUBY_ASSERT_ALWAYS(me->def->type == VM_METHOD_TYPE_CFUNC);
189
190 // CHECK_CFP_CONSISTENCY("full_cfunc_return"); TODO revive this
191
192 // Pop the C func's frame and fire the c_return TracePoint event
193 // Note that this is the same order as vm_call_cfunc_with_frame().
194 rb_vm_pop_frame(ec);
195 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, return_value);
196 // Note, this deviates from the interpreter in that users need to enable
197 // a c_return TracePoint for this DTrace hook to work. A reasonable change
198 // since the Ruby return event works this way as well.
199 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
200
201 // Push return value into the caller's stack. We know that it's a frame that
202 // uses cfp->sp because we are patching a call done with gen_send_cfunc().
203 ec->cfp->sp[0] = return_value;
204 ec->cfp->sp++;
205}
206
207// This is defined only as a named struct inside rb_iseq_constant_body.
208// By giving it a separate typedef, we make it nameable by rust-bindgen.
209// Bindgen's temp/anon name isn't guaranteed stable.
210typedef struct rb_iseq_param_keyword rb_seq_param_keyword_struct;
211
212ID rb_get_symbol_id(VALUE namep);
213
214// If true, the iseq has only opt_invokebuiltin_delegate(_leave) and leave insns.
215static bool
216invokebuiltin_delegate_leave_p(const rb_iseq_t *iseq)
217{
218 int insn1 = rb_vm_insn_addr2opcode((void *)ISEQ_BODY(iseq)->iseq_encoded[0]);
219 if ((int)ISEQ_BODY(iseq)->iseq_size != insn_len(insn1) + insn_len(BIN(leave))) {
220 return false;
221 }
222 int insn2 = rb_vm_insn_addr2opcode((void *)ISEQ_BODY(iseq)->iseq_encoded[insn_len(insn1)]);
223 return (insn1 == BIN(opt_invokebuiltin_delegate) || insn1 == BIN(opt_invokebuiltin_delegate_leave)) &&
224 insn2 == BIN(leave);
225}
226
227// Return an rb_builtin_function if the iseq contains only that builtin function.
228const struct rb_builtin_function *
229rb_yjit_builtin_function(const rb_iseq_t *iseq)
230{
231 if (invokebuiltin_delegate_leave_p(iseq)) {
232 return (const struct rb_builtin_function *)ISEQ_BODY(iseq)->iseq_encoded[1];
233 }
234 else {
235 return NULL;
236 }
237}
238
239extern VALUE *rb_vm_base_ptr(struct rb_control_frame_struct *cfp);
240
241VALUE
242rb_str_neq_internal(VALUE str1, VALUE str2)
243{
244 return rb_str_eql_internal(str1, str2) == Qtrue ? Qfalse : Qtrue;
245}
246
247extern VALUE rb_ary_unshift_m(int argc, VALUE *argv, VALUE ary);
248
249VALUE
250rb_yjit_rb_ary_subseq_length(VALUE ary, rb_len_t beg)
251{
252 rb_len_t len = RARRAY_LEN(ary);
253 return rb_ary_subseq(ary, beg, len);
254}
255
256// Checks to establish preconditions for rb_yjit_splat_varg_cfunc()
257VALUE
258rb_yjit_splat_varg_checks(VALUE *sp, VALUE splat_array, rb_control_frame_t *cfp)
259{
260 // We inserted a T_ARRAY guard before this call
261 rb_len_t len = RARRAY_LEN(splat_array);
262
263 // Large splat arrays need a separate allocation
264 if (len < 0 || len > VM_ARGC_STACK_MAX) return Qfalse;
265
266 // Would we overflow if we put the contents of the array onto the stack?
267 if (sp + len > (VALUE *)(cfp - 2)) return Qfalse;
268
269 // Reject keywords hash since that requires duping it sometimes
270 if (len > 0) {
271 VALUE last_hash = RARRAY_AREF(splat_array, len - 1);
272 if (RB_TYPE_P(last_hash, T_HASH) &&
273 FL_TEST_RAW(last_hash, RHASH_PASS_AS_KEYWORDS)) {
274 return Qfalse;
275 }
276 }
277
278 return Qtrue;
279}
280
281// Push array elements to the stack for a C method that has a variable number
282// of parameters. Returns the number of arguments the splat array contributes.
283int
284rb_yjit_splat_varg_cfunc(VALUE *stack_splat_array)
285{
286 VALUE splat_array = *stack_splat_array;
287 int len;
288
289 // We already checked that length fits in `int`
290 RUBY_ASSERT(RB_TYPE_P(splat_array, T_ARRAY));
291 len = (int)RARRAY_LEN(splat_array);
292
293 // Push the contents of the array onto the stack
294 MEMCPY(stack_splat_array, RARRAY_CONST_PTR(splat_array), VALUE, len);
295
296 return len;
297}
298
299// Print the Ruby source location of some ISEQ for debugging purposes
300void
301rb_yjit_dump_iseq_loc(const rb_iseq_t *iseq, uint32_t insn_idx)
302{
303 char *ptr;
305 VALUE path = rb_iseq_path(iseq);
306 RSTRING_GETMEM(path, ptr, len);
307 fprintf(stderr, "%s %.*s:%u\n", __func__, (int)len, ptr, rb_iseq_line_no(iseq, insn_idx));
308}
309
310// Get the number of digits required to print an integer
311static int
312num_digits(int integer)
313{
314 int num = 1;
315 while (integer /= 10) {
316 num++;
317 }
318 return num;
319}
320
321// Allocate a C string that formats an ISEQ label like iseq_inspect()
322char *
323rb_yjit_iseq_inspect(const rb_iseq_t *iseq)
324{
325 const char *label = RSTRING_PTR(ISEQ_BODY(iseq)->location.label);
326 const char *path = RSTRING_PTR(rb_iseq_path(iseq));
327 int lineno = ISEQ_BODY(iseq)->location.code_location.beg_pos.lineno;
328
329 const size_t size = strlen(label) + strlen(path) + num_digits(lineno) + 3;
330 char *buf = ZALLOC_N(char, size);
331 snprintf(buf, size, "%s@%s:%d", label, path, lineno);
332 return buf;
333}
334
335// There are RSTRUCT_SETs in ruby/internal/core/rstruct.h and internal/struct.h
336// with different types (int vs long) for k. Here we use the one from ruby/internal/core/rstruct.h,
337// which takes an int.
338void
339rb_RSTRUCT_SET(VALUE st, int k, VALUE v)
340{
341 RSTRUCT_SET(st, k, v);
342}
343
344// Return the string encoding index
345int
346rb_ENCODING_GET(VALUE obj)
347{
348 return RB_ENCODING_GET(obj);
349}
350
351// For running write barriers from Rust. Required when we add a new edge in the
352// object graph from `old` to `young`.
353void
354rb_yjit_obj_written(VALUE old, VALUE young, const char *file, int line)
355{
356 rb_obj_written(old, Qundef, young, file, line);
357}
358
359void
360rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception)
361{
362 RB_VM_LOCKING() {
363 rb_vm_barrier();
364
365 // Compile a block version starting at the current instruction
366 uint8_t *rb_yjit_iseq_gen_entry_point(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception); // defined in Rust
367 uintptr_t code_ptr = (uintptr_t)rb_yjit_iseq_gen_entry_point(iseq, ec, jit_exception);
368
369 if (jit_exception) {
370 ISEQ_BODY(iseq)->jit_exception = (rb_jit_func_t)code_ptr;
371 }
372 else {
373 ISEQ_BODY(iseq)->jit_entry = (rb_jit_func_t)code_ptr;
374 }
375 }
376}
377
378// GC root for interacting with the GC
380 bool unused; // empty structs are not legal in C99
381};
382
383// For dealing with refinements
384void
385rb_yjit_invalidate_all_method_lookup_assumptions(void)
386{
387 // It looks like Module#using actually doesn't need to invalidate all the
388 // method caches, so we do nothing here for now.
389}
390
391// Number of object shapes, which might be useful for investigating YJIT exit reasons.
392VALUE
393rb_object_shape_count(void)
394{
395 // next_shape_id starts from 0, so it's the same as the count
396 return ULONG2NUM((unsigned long)rb_shapes_count());
397}
398
399bool
400rb_yjit_shape_obj_complex_p(VALUE obj)
401{
402 return rb_obj_shape_complex_p(obj);
403}
404
405bool
406rb_yjit_shape_obj_embedded_p(VALUE obj)
407{
408 return rb_obj_shape_embedded_p(obj);
409}
410
411attr_index_t
412rb_yjit_shape_capacity(shape_id_t shape_id)
413{
414 return RSHAPE_CAPACITY(shape_id);
415}
416
417attr_index_t
418rb_yjit_shape_index(shape_id_t shape_id)
419{
420 return RSHAPE_INDEX(shape_id);
421}
422
423// The number of stack slots that vm_sendish() pops for send and invokesuper.
424size_t
425rb_yjit_sendish_sp_pops(const struct rb_callinfo *ci)
426{
427 return 1 - sp_inc_of_sendish(ci); // + 1 to ignore return value push
428}
429
430// The number of stack slots that vm_sendish() pops for invokeblock.
431size_t
432rb_yjit_invokeblock_sp_pops(const struct rb_callinfo *ci)
433{
434 return 1 - sp_inc_of_invokeblock(ci); // + 1 to ignore return value push
435}
436
437rb_serial_t
438rb_yjit_cme_ractor_serial(const rb_callable_method_entry_t *cme)
439{
440 return cme->def->body.bmethod.defined_ractor_id;
441}
442
443bool
444rb_yjit_attached_object_is(VALUE klass, VALUE object)
445{
446 return RCLASS_SINGLETON_P(klass) && RCLASS_ATTACHED_OBJECT(klass) == object;
447}
448
449// Setup jit_return to avoid returning a non-Qundef value on a non-FINISH frame.
450// See [jit_compile_exception] for details.
451void
452rb_yjit_set_exception_return(rb_control_frame_t *cfp, void *leave_exit, void *leave_exception)
453{
454 if (VM_FRAME_FINISHED_P(cfp)) {
455 // If it's a FINISH frame, just normally exit with a non-Qundef value.
456 cfp->jit_return = leave_exit;
457 }
458 else if (cfp->jit_return) {
459 while (!VM_FRAME_FINISHED_P(cfp)) {
460 if (cfp->jit_return == leave_exit) {
461 // Unlike jit_exec(), leave_exit is not safe on a non-FINISH frame on
462 // jit_exec_exception(). See [jit_exec] and [jit_exec_exception] for
463 // details. Exit to the interpreter with Qundef to let it keep executing
464 // other Ruby frames.
465 cfp->jit_return = leave_exception;
466 return;
467 }
468 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
469 }
470 }
471 else {
472 // If the caller was not JIT code, exit to the interpreter with Qundef
473 // to keep executing Ruby frames with the interpreter.
474 cfp->jit_return = leave_exception;
475 }
476}
477
478#if defined(_WIN32) && defined(_M_X64)
479// RtlUnwindEx(), which longjmp() and C++ exceptions use, needs unwind info for
480// every frame. JIT code calls out only inside the frame gen_entry_prologue()
481// sets up with frame_pointer, so a single UNWIND_INFO based on RBP is right at
482// every call in the code region. It is wrong within the few instructions that
483// set up and tear down the frame, which make no calls, so only an asynchronous
484// exception there, such as a stack overflow on the push, would unwind wrongly:
485//
486// [RBP] caller's RBP
487// [RBP - 8] copy of RBP
488// [RBP - 16] R13
489// [RBP - 24] R12
490// [RBP - 32] RBX
491static const uint8_t yjit_unwind_info[] = {
492 1, // Version 1, no handler
493 0, // No prologue, since every call happens after it
494 9, // Count of unwind code slots
495 0x25, // Frame register RBP at 2 * 16 bytes above RBX
496 0, 0x34, 0, 0, // UWOP_SAVE_NONVOL RBX at [RBP - 32]
497 0, 0xc4, 1, 0, // UWOP_SAVE_NONVOL R12 at [RBP - 24]
498 0, 0xd4, 2, 0, // UWOP_SAVE_NONVOL R13 at [RBP - 16]
499 0, 0x03, // UWOP_SET_FPREG
500 0, 0x32, // UWOP_ALLOC_SMALL 32 bytes from [RBP - 8] down to RBX
501 0, 0x50, // UWOP_PUSH_NONVOL RBP
502 0, 0, // Padding to an even count
503};
504
505static RUNTIME_FUNCTION yjit_runtime_function;
506
507// Reserve the JIT region after a page holding the unwind info, which has to be
508// within 4GiB above the base address of the registered function table.
509uint8_t *
510rb_yjit_reserve_addr_space(uint32_t mem_size)
511{
512 uint32_t page_size = rb_jit_get_page_size();
513 uint8_t *base = rb_jit_reserve_addr_space(page_size + mem_size);
514 DWORD old_protect;
515
516 if (!VirtualAlloc(base, page_size, MEM_COMMIT, PAGE_READWRITE)) {
517 rb_bug("yjit: failed to commit the unwind info page, error: %lu", GetLastError());
518 }
519 memcpy(base, yjit_unwind_info, sizeof(yjit_unwind_info));
520 VirtualProtect(base, page_size, PAGE_READONLY, &old_protect);
521
522 // RtlVirtualUnwind() takes a call followed by a jmp to the start of the
523 // function for an epilogue, so start it at the unwind info no code jumps to.
524 yjit_runtime_function.BeginAddress = 0;
525 yjit_runtime_function.EndAddress = page_size + mem_size;
526 yjit_runtime_function.UnwindData = 0;
527 if (!RtlAddFunctionTable(&yjit_runtime_function, 1, (DWORD64)base)) {
528 rb_bug("yjit: failed to register unwind info for JIT code");
529 }
530
531 return base + page_size;
532}
533#elif defined(_WIN32) && defined(_M_ARM64)
534// The same as on x64. X29 stays at the saved pair while the code moves SP:
535//
536// [X29 + 8] LR
537// [X29] caller's X29
538// [X29 - 16] X19
539// [X29 - 32] X20
540// [X29 - 48] X21
541//
542// The codes undo a prolog that builds the same frame but sets X29 last, so
543// that the unwinder restores SP from X29 first. An .xdata record covers at
544// most 1MiB, so the region is split into fragments, and the leading end_c
545// gives each fragment an empty prolog, where every PC unwinds as in the body.
546static const uint8_t yjit_unwind_codes[] = {
547 0xe5, // end_c
548 0xe2, 0x06, // add_fp: X29 = SP + 48
549 0xd0, 0x80, // save_reg X21 at [SP]
550 0xd0, 0x42, // save_reg X20 at [SP + 16]
551 0xd0, 0x04, // save_reg X19 at [SP + 32]
552 0x03, // alloc_s 48
553 0x81, // save_fplr_x <X29, LR> at [SP - 16]!
554 0xe4, // end
555};
556
557#define YJIT_UNWIND_FRAGMENT_SIZE (512 * 1024)
558
559// Write an .xdata record for a fragment of `length` bytes with no epilog.
560static uint8_t *
561yjit_write_xdata(uint8_t *xdata, uint32_t length)
562{
563 uint32_t header = length / 4 | (uint32_t)(sizeof(yjit_unwind_codes) / 4) << 27;
564 memcpy(xdata, &header, sizeof(header));
565 memcpy(xdata + sizeof(header), yjit_unwind_codes, sizeof(yjit_unwind_codes));
566 return xdata + sizeof(header) + sizeof(yjit_unwind_codes);
567}
568
569// Reserve the JIT region after a page holding the unwind info, which has to be
570// above the base address of the registered function table.
571uint8_t *
572rb_yjit_reserve_addr_space(uint32_t mem_size)
573{
574 uint32_t page_size = rb_jit_get_page_size();
575 uint32_t region_size = page_size + mem_size;
576 uint32_t count = (region_size + YJIT_UNWIND_FRAGMENT_SIZE - 1) / YJIT_UNWIND_FRAGMENT_SIZE;
577 uint8_t *base = rb_jit_reserve_addr_space(region_size);
578 DWORD old_protect;
579
580 if (!VirtualAlloc(base, page_size, MEM_COMMIT, PAGE_READWRITE)) {
581 rb_bug("yjit: failed to commit the unwind info page, error: %lu", GetLastError());
582 }
583 // One record for the full fragments and one for the last, which may be shorter
584 uint8_t *last_xdata = yjit_write_xdata(base, YJIT_UNWIND_FRAGMENT_SIZE);
585 yjit_write_xdata(last_xdata, region_size - (count - 1) * YJIT_UNWIND_FRAGMENT_SIZE);
586 VirtualProtect(base, page_size, PAGE_READONLY, &old_protect);
587
588 // Not ZALLOC_N, whose NoMemoryError would longjmp across Rust frames
589 RUNTIME_FUNCTION *table = calloc(count, sizeof(RUNTIME_FUNCTION));
590 if (!table) {
591 rb_bug("yjit: failed to allocate the function table for unwind info");
592 }
593 for (uint32_t i = 0; i < count; i++) {
594 table[i].BeginAddress = i * YJIT_UNWIND_FRAGMENT_SIZE;
595 table[i].UnwindData = i + 1 < count ? 0 : (DWORD)(last_xdata - base);
596 }
597 if (!RtlAddFunctionTable(table, count, (DWORD64)base)) {
598 rb_bug("yjit: failed to register unwind info for JIT code");
599 }
600
601 return base + page_size;
602}
603#endif
604
605// VM_INSTRUCTION_SIZE changes depending on if ZJIT is in the build. Since
606// bindgen can only grab one version of the constant and copy that to rust,
607// we make that the upper bound and this the accurate value.
608uint32_t
609rb_vm_instruction_size(void)
610{
611 return VM_INSTRUCTION_SIZE;
612}
613
614static int
615yjit_cdhash_all_fixnum_i(st_data_t key, st_data_t _val, st_data_t data)
616{
617 if (!FIXNUM_P((VALUE)key)) {
618 *((bool *)data) = false;
619 return ST_STOP;
620 }
621 return ST_CONTINUE;
622}
623
624bool
625rb_yjit_cdhash_all_fixnum_p(VALUE cdhash)
626{
627 bool all_fixnum = true;
628 st_foreach(rb_imemo_cdhash_tbl(cdhash), yjit_cdhash_all_fixnum_i, (st_data_t)&all_fixnum);
629 return all_fixnum;
630}
631
632int
633rb_yjit_cdhash_lookup(VALUE cdhash, st_data_t key, st_data_t *val)
634{
635 return st_lookup(rb_imemo_cdhash_tbl(cdhash), key, val);
636}
637
638// Primitives used by yjit.rb
639VALUE rb_yjit_stats_enabled_p(rb_execution_context_t *ec, VALUE self);
640VALUE rb_yjit_print_stats_p(rb_execution_context_t *ec, VALUE self);
641VALUE rb_yjit_log_enabled_p(rb_execution_context_t *c, VALUE self);
642VALUE rb_yjit_print_log_p(rb_execution_context_t *c, VALUE self);
643VALUE rb_yjit_trace_exit_locations_enabled_p(rb_execution_context_t *ec, VALUE self);
644VALUE rb_yjit_get_stats(rb_execution_context_t *ec, VALUE self, VALUE key);
645VALUE rb_yjit_reset_stats_bang(rb_execution_context_t *ec, VALUE self);
646void rb_yjit_update_max_compile_time_ns(uint64_t max_compile_time_ns);
647VALUE rb_yjit_get_log(rb_execution_context_t *ec, VALUE self);
648VALUE rb_yjit_disasm_iseq(rb_execution_context_t *ec, VALUE self, VALUE iseq);
649VALUE rb_yjit_insns_compiled(rb_execution_context_t *ec, VALUE self, VALUE iseq);
650VALUE rb_yjit_code_gc(rb_execution_context_t *ec, VALUE self);
651VALUE rb_yjit_simulate_oom_bang(rb_execution_context_t *ec, VALUE self);
652VALUE rb_yjit_get_exit_locations(rb_execution_context_t *ec, VALUE self);
653VALUE 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);
654VALUE rb_yjit_c_builtin_p(rb_execution_context_t *ec, VALUE self);
655
656static VALUE
657rb_yjit_total_compile_time_ns_get(rb_execution_context_t *ec, VALUE self)
658{
659 return ULL2NUM(rb_yjit_total_compile_time_ns);
660}
661
662static VALUE
663rb_yjit_max_compile_time_ns_get(rb_execution_context_t *ec, VALUE self)
664{
665 return ULL2NUM(rb_yjit_max_compile_time_ns);
666}
667
668static VALUE
669rb_yjit_max_compile_time_ns_set(rb_execution_context_t *ec, VALUE self, VALUE max_compile_time_ns)
670{
671 rb_yjit_update_max_compile_time_ns(NUM2ULL(max_compile_time_ns));
672 return max_compile_time_ns;
673}
674
675// Allow YJIT_C_BUILTIN macro to force --yjit-c-builtin
676#ifdef YJIT_C_BUILTIN
677static VALUE yjit_c_builtin_p(rb_execution_context_t *ec, VALUE self) { return Qtrue; }
678#else
679#define yjit_c_builtin_p rb_yjit_c_builtin_p
680#endif
681
682// Preprocessed yjit.rb generated during build
683#include "yjit.rbinc"
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_RETURN
Return from a method, written in C.
Definition event.h:44
#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:404
#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:128
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
#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.
#define NUM2ULL
Old name of RB_NUM2ULL.
Definition long_long.h:35
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#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
static int RB_ENCODING_GET(VALUE obj)
Just another name of rb_enc_get_index.
Definition encoding.h:195
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_subseq(VALUE ary, rb_len_t beg, rb_len_t len)
Obtains a part of the passed array.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition hash.c:1646
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:422
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
static VALUE RSTRUCT_SET(VALUE st, int k, VALUE v)
Resembles Struct#[]=.
Definition rstruct.h:92
#define RTEST
This is an old name of RB_TEST.
#define USE_FLONUM
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
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