Ruby: vm.c Source File

Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)

vm.c
1/**********************************************************************
2
3 Vm.c -
4
5 $Author$
6
7 Copyright (C) 2004-2007 Koichi Sasada
8
9**********************************************************************/
10
11#define vm_exec rb_vm_exec
12
13#include "eval_intern.h"
14#include "internal.h"
15#include "internal/class.h"
16#include "internal/compile.h"
17#include "internal/cont.h"
18#include "internal/error.h"
19#include "internal/encoding.h"
20#include "internal/eval.h"
21#include "internal/gc.h"
22#include "internal/inits.h"
23#include "internal/missing.h"
24#include "internal/object.h"
25#include "internal/proc.h"
26#include "internal/re.h"
27#include "internal/ruby_parser.h"
28#include "internal/symbol.h"
29#include "internal/thread.h"
30#include "internal/transcode.h"
31#include "internal/vm.h"
32#include "internal/sanitizers.h"
33#include "internal/variable.h"
34#include "iseq.h"
35#include "rjit.h"
36#include "symbol.h" // This includes a macro for a more performant rb_id2sym.
37#include "yjit.h"
38#include "ruby/st.h"
39#include "ruby/vm.h"
40#include "vm_core.h"
41#include "vm_callinfo.h"
42#include "vm_debug.h"
43#include "vm_exec.h"
44#include "vm_insnhelper.h"
45#include "ractor_core.h"
46#include "vm_sync.h"
47#include "shape.h"
48
49#include "builtin.h"
50
51#include "probes.h"
52#include "probes_helper.h"
53
54#ifdef RUBY_ASSERT_CRITICAL_SECTION
55int ruby_assert_critical_section_entered = 0;
56#endif
57
58static void *native_main_thread_stack_top;
59
60 VALUE rb_str_concat_literals(size_t, const VALUE*);
61
62 VALUE vm_exec(rb_execution_context_t *);
63
64extern const char *const rb_debug_counter_names[];
65
66PUREFUNC(static inline const VALUE *VM_EP_LEP(const VALUE *));
67static inline const VALUE *
68VM_EP_LEP(const VALUE *ep)
69{
70 while (!VM_ENV_LOCAL_P(ep)) {
71 ep = VM_ENV_PREV_EP(ep);
72 }
73 return ep;
74}
75
76static inline const rb_control_frame_t *
77rb_vm_search_cf_from_ep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE * const ep)
78{
79 if (!ep) {
80 return NULL;
81 }
82 else {
83 const rb_control_frame_t * const eocfp = RUBY_VM_END_CONTROL_FRAME(ec); /* end of control frame pointer */
84
85 while (cfp < eocfp) {
86 if (cfp->ep == ep) {
87 return cfp;
88 }
89 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
90 }
91
92 return NULL;
93 }
94}
95
96const VALUE *
97rb_vm_ep_local_ep(const VALUE *ep)
98{
99 return VM_EP_LEP(ep);
100}
101
102PUREFUNC(static inline const VALUE *VM_CF_LEP(const rb_control_frame_t * const cfp));
103static inline const VALUE *
104VM_CF_LEP(const rb_control_frame_t * const cfp)
105{
106 return VM_EP_LEP(cfp->ep);
107}
108
109static inline const VALUE *
110VM_CF_PREV_EP(const rb_control_frame_t * const cfp)
111{
112 return VM_ENV_PREV_EP(cfp->ep);
113}
114
115PUREFUNC(static inline VALUE VM_CF_BLOCK_HANDLER(const rb_control_frame_t * const cfp));
116static inline VALUE
117VM_CF_BLOCK_HANDLER(const rb_control_frame_t * const cfp)
118{
119 const VALUE *ep = VM_CF_LEP(cfp);
120 return VM_ENV_BLOCK_HANDLER(ep);
121}
122
123int
124rb_vm_cframe_keyword_p(const rb_control_frame_t *cfp)
125{
126 return VM_FRAME_CFRAME_KW_P(cfp);
127}
128
129 VALUE
130rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
131{
132 return VM_CF_BLOCK_HANDLER(cfp);
133}
134
135#if VM_CHECK_MODE > 0
136static int
137VM_CFP_IN_HEAP_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
138{
139 const VALUE *start = ec->vm_stack;
140 const VALUE *end = (VALUE *)ec->vm_stack + ec->vm_stack_size;
141 VM_ASSERT(start != NULL);
142
143 if (start <= (VALUE *)cfp && (VALUE *)cfp < end) {
144 return FALSE;
145 }
146 else {
147 return TRUE;
148 }
149}
150
151static int
152VM_EP_IN_HEAP_P(const rb_execution_context_t *ec, const VALUE *ep)
153{
154 const VALUE *start = ec->vm_stack;
155 const VALUE *end = (VALUE *)ec->cfp;
156 VM_ASSERT(start != NULL);
157
158 if (start <= ep && ep < end) {
159 return FALSE;
160 }
161 else {
162 return TRUE;
163 }
164}
165
166static int
167vm_ep_in_heap_p_(const rb_execution_context_t *ec, const VALUE *ep)
168{
169 if (VM_EP_IN_HEAP_P(ec, ep)) {
170 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV]; /* VM_ENV_ENVVAL(ep); */
171
172 if (!UNDEF_P(envval)) {
173 const rb_env_t *env = (const rb_env_t *)envval;
174
175 VM_ASSERT(imemo_type_p(envval, imemo_env));
176 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
177 VM_ASSERT(env->ep == ep);
178 }
179 return TRUE;
180 }
181 else {
182 return FALSE;
183 }
184}
185
186int
187rb_vm_ep_in_heap_p(const VALUE *ep)
188{
189 const rb_execution_context_t *ec = GET_EC();
190 if (ec->vm_stack == NULL) return TRUE;
191 return vm_ep_in_heap_p_(ec, ep);
192}
193#endif
194
195static struct rb_captured_block *
196VM_CFP_TO_CAPTURED_BLOCK(const rb_control_frame_t *cfp)
197{
198 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
199 return (struct rb_captured_block *)&cfp->self;
200}
201
202static rb_control_frame_t *
203VM_CAPTURED_BLOCK_TO_CFP(const struct rb_captured_block *captured)
204{
205 rb_control_frame_t *cfp = ((rb_control_frame_t *)((VALUE *)(captured) - 3));
206 VM_ASSERT(!VM_CFP_IN_HEAP_P(GET_EC(), cfp));
207 VM_ASSERT(sizeof(rb_control_frame_t)/sizeof(VALUE) == 7 + VM_DEBUG_BP_CHECK ? 1 : 0);
208 return cfp;
209}
210
211static int
212VM_BH_FROM_CFP_P(VALUE block_handler, const rb_control_frame_t *cfp)
213{
214 const struct rb_captured_block *captured = VM_CFP_TO_CAPTURED_BLOCK(cfp);
215 return VM_TAGGED_PTR_REF(block_handler, 0x03) == captured;
216}
217
218static VALUE
219vm_passed_block_handler(rb_execution_context_t *ec)
220{
221 VALUE block_handler = ec->passed_block_handler;
222 ec->passed_block_handler = VM_BLOCK_HANDLER_NONE;
223 vm_block_handler_verify(block_handler);
224 return block_handler;
225}
226
227static rb_cref_t *
228vm_cref_new0(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval, int use_prev_prev, int singleton)
229{
230 VALUE refinements = Qnil;
231 int omod_shared = FALSE;
232
233 /* scope */
234 union {
235 rb_scope_visibility_t visi;
236 VALUE value;
237 } scope_visi;
238
239 scope_visi.visi.method_visi = visi;
240 scope_visi.visi.module_func = module_func;
241
242 /* refinements */
243 if (prev_cref != NULL && prev_cref != (void *)1 /* TODO: why CREF_NEXT(cref) is 1? */) {
244 refinements = CREF_REFINEMENTS(prev_cref);
245
246 if (!NIL_P(refinements)) {
247 omod_shared = TRUE;
248 CREF_OMOD_SHARED_SET(prev_cref);
249 }
250 }
251
252 VM_ASSERT(singleton || klass);
253
254 rb_cref_t *cref = IMEMO_NEW(rb_cref_t, imemo_cref, refinements);
255 cref->klass_or_self = klass;
256 cref->next = use_prev_prev ? CREF_NEXT(prev_cref) : prev_cref;
257 *((rb_scope_visibility_t *)&cref->scope_visi) = scope_visi.visi;
258
259 if (pushed_by_eval) CREF_PUSHED_BY_EVAL_SET(cref);
260 if (omod_shared) CREF_OMOD_SHARED_SET(cref);
261 if (singleton) CREF_SINGLETON_SET(cref);
262
263 return cref;
264}
265
266static rb_cref_t *
267vm_cref_new(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval, int singleton)
268{
269 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, FALSE, singleton);
270}
271
272static rb_cref_t *
273vm_cref_new_use_prev(VALUE klass, rb_method_visibility_t visi, int module_func, rb_cref_t *prev_cref, int pushed_by_eval)
274{
275 return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, TRUE, FALSE);
276}
277
278static int
279ref_delete_symkey(VALUE key, VALUE value, VALUE unused)
280{
281 return SYMBOL_P(key) ? ST_DELETE : ST_CONTINUE;
282}
283
284static rb_cref_t *
285vm_cref_dup(const rb_cref_t *cref)
286{
287 const rb_scope_visibility_t *visi = CREF_SCOPE_VISI(cref);
288 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
289 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
290 int singleton = CREF_SINGLETON(cref);
291
292 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
293
294 if (!NIL_P(CREF_REFINEMENTS(cref))) {
295 VALUE ref = rb_hash_dup(CREF_REFINEMENTS(cref));
296 rb_hash_foreach(ref, ref_delete_symkey, Qnil);
297 CREF_REFINEMENTS_SET(new_cref, ref);
298 CREF_OMOD_SHARED_UNSET(new_cref);
299 }
300
301 return new_cref;
302}
303
304
305rb_cref_t *
306rb_vm_cref_dup_without_refinements(const rb_cref_t *cref)
307{
308 const rb_scope_visibility_t *visi = CREF_SCOPE_VISI(cref);
309 rb_cref_t *next_cref = CREF_NEXT(cref), *new_cref;
310 int pushed_by_eval = CREF_PUSHED_BY_EVAL(cref);
311 int singleton = CREF_SINGLETON(cref);
312
313 new_cref = vm_cref_new(cref->klass_or_self, visi->method_visi, visi->module_func, next_cref, pushed_by_eval, singleton);
314
315 if (!NIL_P(CREF_REFINEMENTS(cref))) {
316 CREF_REFINEMENTS_SET(new_cref, Qnil);
317 CREF_OMOD_SHARED_UNSET(new_cref);
318 }
319
320 return new_cref;
321}
322
323static rb_cref_t *
324vm_cref_new_toplevel(rb_execution_context_t *ec)
325{
326 rb_cref_t *cref = vm_cref_new(rb_cObject, METHOD_VISI_PRIVATE /* toplevel visibility is private */, FALSE, NULL, FALSE, FALSE);
327 VALUE top_wrapper = rb_ec_thread_ptr(ec)->top_wrapper;
328
329 if (top_wrapper) {
330 cref = vm_cref_new(top_wrapper, METHOD_VISI_PRIVATE, FALSE, cref, FALSE, FALSE);
331 }
332
333 return cref;
334}
335
336rb_cref_t *
337rb_vm_cref_new_toplevel(void)
338{
339 return vm_cref_new_toplevel(GET_EC());
340}
341
342static void
343vm_cref_dump(const char *mesg, const rb_cref_t *cref)
344{
345 ruby_debug_printf("vm_cref_dump: %s (%p)\n", mesg, (void *)cref);
346
347 while (cref) {
348 ruby_debug_printf("= cref| klass: %s\n", RSTRING_PTR(rb_class_path(CREF_CLASS(cref))));
349 cref = CREF_NEXT(cref);
350 }
351}
352
353void
354rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep)
355{
356 *((const VALUE **)&dst->as.captured.ep) = ep;
357 RB_OBJ_WRITTEN(obj, Qundef, VM_ENV_ENVVAL(ep));
358}
359
360static void
361vm_bind_update_env(VALUE bindval, rb_binding_t *bind, VALUE envval)
362{
363 const rb_env_t *env = (rb_env_t *)envval;
364 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, env->iseq);
365 rb_vm_block_ep_update(bindval, &bind->block, env->ep);
366}
367
368#if VM_COLLECT_USAGE_DETAILS
369static void vm_collect_usage_operand(int insn, int n, VALUE op);
370static void vm_collect_usage_insn(int insn);
371static void vm_collect_usage_register(int reg, int isset);
372#endif
373
374static VALUE vm_make_env_object(const rb_execution_context_t *ec, rb_control_frame_t *cfp);
375static VALUE vm_invoke_bmethod(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
376 int argc, const VALUE *argv, int kw_splat, VALUE block_handler,
377 const rb_callable_method_entry_t *me);
378static VALUE vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE block_handler);
379
380#if USE_YJIT
381// Counter to serve as a proxy for execution time, total number of calls
382static uint64_t yjit_total_entry_hits = 0;
383
384// Number of calls used to estimate how hot an ISEQ is
385#define YJIT_CALL_COUNT_INTERV 20u
386
388static inline bool
389rb_yjit_threshold_hit(const rb_iseq_t *iseq, uint64_t entry_calls)
390{
391 yjit_total_entry_hits += 1;
392
393 // Record the number of calls at the beginning of the interval
394 if (entry_calls + YJIT_CALL_COUNT_INTERV == rb_yjit_call_threshold) {
395 iseq->body->yjit_calls_at_interv = yjit_total_entry_hits;
396 }
397
398 // Try to estimate the total time taken (total number of calls) to reach 20 calls to this ISEQ
399 // This give us a ratio of how hot/cold this ISEQ is
400 if (entry_calls == rb_yjit_call_threshold) {
401 // We expect threshold 1 to compile everything immediately
402 if (rb_yjit_call_threshold < YJIT_CALL_COUNT_INTERV) {
403 return true;
404 }
405
406 uint64_t num_calls = yjit_total_entry_hits - iseq->body->yjit_calls_at_interv;
407
408 // Reject ISEQs that don't get called often enough
409 if (num_calls > rb_yjit_cold_threshold) {
410 rb_yjit_incr_counter("cold_iseq_entry");
411 return false;
412 }
413
414 return true;
415 }
416
417 return false;
418}
419#else
420#define rb_yjit_threshold_hit(iseq, entry_calls) false
421#endif
422
423#if USE_RJIT || USE_YJIT
424// Generate JIT code that supports the following kinds of ISEQ entries:
425// * The first ISEQ on vm_exec (e.g. <main>, or Ruby methods/blocks
426// called by a C method). The current frame has VM_FRAME_FLAG_FINISH.
427// The current vm_exec stops if JIT code returns a non-Qundef value.
428// * ISEQs called by the interpreter on vm_sendish (e.g. Ruby methods or
429// blocks called by a Ruby frame that isn't compiled or side-exited).
430// The current frame doesn't have VM_FRAME_FLAG_FINISH. The current
431// vm_exec does NOT stop whether JIT code returns Qundef or not.
432static inline rb_jit_func_t
433jit_compile(rb_execution_context_t *ec)
434{
435 const rb_iseq_t *iseq = ec->cfp->iseq;
436 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
437 bool yjit_enabled = rb_yjit_enabled_p;
438 if (!(yjit_enabled || rb_rjit_call_p)) {
439 return NULL;
440 }
441
442 // Increment the ISEQ's call counter and trigger JIT compilation if not compiled
443 if (body->jit_entry == NULL) {
444 body->jit_entry_calls++;
445 if (yjit_enabled) {
446 if (rb_yjit_threshold_hit(iseq, body->jit_entry_calls)) {
447 rb_yjit_compile_iseq(iseq, ec, false);
448 }
449 }
450 else if (body->jit_entry_calls == rb_rjit_call_threshold()) {
451 rb_rjit_compile(iseq);
452 }
453 }
454 return body->jit_entry;
455}
456
457// Execute JIT code compiled by jit_compile()
458static inline VALUE
459jit_exec(rb_execution_context_t *ec)
460{
461 rb_jit_func_t func = jit_compile(ec);
462 if (func) {
463 // Call the JIT code
464 return func(ec, ec->cfp);
465 }
466 else {
467 return Qundef;
468 }
469}
470#else
471# define jit_compile(ec) ((rb_jit_func_t)0)
472# define jit_exec(ec) Qundef
473#endif
474
475#if USE_YJIT
476// Generate JIT code that supports the following kind of ISEQ entry:
477// * The first ISEQ pushed by vm_exec_handle_exception. The frame would
478// point to a location specified by a catch table, and it doesn't have
479// VM_FRAME_FLAG_FINISH. The current vm_exec stops if JIT code returns
480// a non-Qundef value. So you should not return a non-Qundef value
481// until ec->cfp is changed to a frame with VM_FRAME_FLAG_FINISH.
482static inline rb_jit_func_t
483jit_compile_exception(rb_execution_context_t *ec)
484{
485 const rb_iseq_t *iseq = ec->cfp->iseq;
486 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
487 if (!rb_yjit_enabled_p) {
488 return NULL;
489 }
490
491 // Increment the ISEQ's call counter and trigger JIT compilation if not compiled
492 if (body->jit_exception == NULL) {
493 body->jit_exception_calls++;
494 if (body->jit_exception_calls == rb_yjit_call_threshold) {
495 rb_yjit_compile_iseq(iseq, ec, true);
496 }
497 }
498
499 return body->jit_exception;
500}
501
502// Execute JIT code compiled by jit_compile_exception()
503static inline VALUE
504jit_exec_exception(rb_execution_context_t *ec)
505{
506 rb_jit_func_t func = jit_compile_exception(ec);
507 if (func) {
508 // Call the JIT code
509 return func(ec, ec->cfp);
510 }
511 else {
512 return Qundef;
513 }
514}
515#else
516# define jit_compile_exception(ec) ((rb_jit_func_t)0)
517# define jit_exec_exception(ec) Qundef
518#endif
519
520static void add_opt_method_entry(const rb_method_entry_t *me);
521
522#define RB_TYPE_2_P(obj, type1, type2) \
523 (RB_TYPE_P(obj, type1) || RB_TYPE_P(obj, type2))
524#define RB_TYPE_3_P(obj, type1, type2, type3) \
525 (RB_TYPE_P(obj, type1) || RB_TYPE_P(obj, type2) || RB_TYPE_P(obj, type3))
526
527#define VM_ASSERT_TYPE(obj, type) \
528 VM_ASSERT(RB_TYPE_P(obj, type), #obj ": %s", rb_obj_info(obj))
529#define VM_ASSERT_TYPE2(obj, type1, type2) \
530 VM_ASSERT(RB_TYPE_2_P(obj, type1, type2), #obj ": %s", rb_obj_info(obj))
531#define VM_ASSERT_TYPE3(obj, type1, type2, type3) \
532 VM_ASSERT(RB_TYPE_3_P(obj, type1, type2, type3), #obj ": %s", rb_obj_info(obj))
533
534#include "vm_insnhelper.c"
535
536#include "vm_exec.c"
537
538#include "vm_method.c"
539#include "vm_eval.c"
540
541#define PROCDEBUG 0
542
543 VALUE rb_cRubyVM;
544 VALUE rb_cThread;
545 VALUE rb_mRubyVMFrozenCore;
546 VALUE rb_block_param_proxy;
547
548 VALUE ruby_vm_const_missing_count = 0;
549rb_vm_t *ruby_current_vm_ptr = NULL;
550rb_ractor_t *ruby_single_main_ractor;
551bool ruby_vm_keep_script_lines;
552
553#ifdef RB_THREAD_LOCAL_SPECIFIER
554RB_THREAD_LOCAL_SPECIFIER rb_execution_context_t *ruby_current_ec;
555
556#ifdef RUBY_NT_SERIAL
557RB_THREAD_LOCAL_SPECIFIER rb_atomic_t ruby_nt_serial;
558#endif
559
560// no-inline decl on vm_core.h
561rb_execution_context_t *
562rb_current_ec_noinline(void)
563{
564 return ruby_current_ec;
565}
566
567void
568rb_current_ec_set(rb_execution_context_t *ec)
569{
570 ruby_current_ec = ec;
571}
572
573
574#ifdef __APPLE__
575rb_execution_context_t *
576rb_current_ec(void)
577{
578 return ruby_current_ec;
579}
580
581#endif
582#else
583native_tls_key_t ruby_current_ec_key;
584
585// no-inline decl on vm_core.h
586rb_execution_context_t *
587rb_current_ec_noinline(void)
588{
589 return native_tls_get(ruby_current_ec_key);
590}
591
592#endif
593
594 rb_event_flag_t ruby_vm_event_flags;
595 rb_event_flag_t ruby_vm_event_enabled_global_flags;
596unsigned int ruby_vm_event_local_num;
597
598rb_serial_t ruby_vm_constant_cache_invalidations = 0;
599rb_serial_t ruby_vm_constant_cache_misses = 0;
600rb_serial_t ruby_vm_global_cvar_state = 1;
601
602static const struct rb_callcache vm_empty_cc = {
603 .flags = T_IMEMO | (imemo_callcache << FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
604 .klass = Qfalse,
605 .cme_ = NULL,
606 .call_ = vm_call_general,
607 .aux_ = {
608 .v = Qfalse,
609 }
610};
611
612static const struct rb_callcache vm_empty_cc_for_super = {
613 .flags = T_IMEMO | (imemo_callcache << FL_USHIFT) | VM_CALLCACHE_UNMARKABLE,
614 .klass = Qfalse,
615 .cme_ = NULL,
616 .call_ = vm_call_super_method,
617 .aux_ = {
618 .v = Qfalse,
619 }
620};
621
622static void thread_free(void *ptr);
623
624void
625rb_vm_inc_const_missing_count(void)
626{
627 ruby_vm_const_missing_count +=1;
628}
629
630int
631rb_dtrace_setup(rb_execution_context_t *ec, VALUE klass, ID id,
632 struct ruby_dtrace_method_hook_args *args)
633{
634 enum ruby_value_type type;
635 if (!klass) {
636 if (!ec) ec = GET_EC();
637 if (!rb_ec_frame_method_id_and_class(ec, &id, 0, &klass) || !klass)
638 return FALSE;
639 }
640 if (RB_TYPE_P(klass, T_ICLASS)) {
641 klass = RBASIC(klass)->klass;
642 }
643 else if (RCLASS_SINGLETON_P(klass)) {
644 klass = RCLASS_ATTACHED_OBJECT(klass);
645 if (NIL_P(klass)) return FALSE;
646 }
647 type = BUILTIN_TYPE(klass);
648 if (type == T_CLASS || type == T_ICLASS || type == T_MODULE) {
649 VALUE name = rb_class_path(klass);
650 const char *classname, *filename;
651 const char *methodname = rb_id2name(id);
652 if (methodname && (filename = rb_source_location_cstr(&args->line_no)) != 0) {
653 if (NIL_P(name) || !(classname = StringValuePtr(name)))
654 classname = "<unknown>";
655 args->classname = classname;
656 args->methodname = methodname;
657 args->filename = filename;
658 args->klass = klass;
659 args->name = name;
660 return TRUE;
661 }
662 }
663 return FALSE;
664}
665
666extern unsigned int redblack_buffer_size;
667
668/*
669 * call-seq:
670 * RubyVM.stat -> Hash
671 * RubyVM.stat(hsh) -> hsh
672 * RubyVM.stat(Symbol) -> Numeric
673 *
674 * Returns a Hash containing implementation-dependent counters inside the VM.
675 *
676 * This hash includes information about method/constant caches:
677 *
678 * {
679 * :constant_cache_invalidations=>2,
680 * :constant_cache_misses=>14,
681 * :global_cvar_state=>27
682 * }
683 *
684 * If <tt>USE_DEBUG_COUNTER</tt> is enabled, debug counters will be included.
685 *
686 * The contents of the hash are implementation specific and may be changed in
687 * the future.
688 *
689 * This method is only expected to work on C Ruby.
690 */
691static VALUE
692vm_stat(int argc, VALUE *argv, VALUE self)
693{
694 static VALUE sym_constant_cache_invalidations, sym_constant_cache_misses, sym_global_cvar_state, sym_next_shape_id;
695 static VALUE sym_shape_cache_size;
696 VALUE arg = Qnil;
697 VALUE hash = Qnil, key = Qnil;
698
699 if (rb_check_arity(argc, 0, 1) == 1) {
700 arg = argv[0];
701 if (SYMBOL_P(arg))
702 key = arg;
703 else if (RB_TYPE_P(arg, T_HASH))
704 hash = arg;
705 else
706 rb_raise(rb_eTypeError, "non-hash or symbol given");
707 }
708 else {
709 hash = rb_hash_new();
710 }
711
712#define S(s) sym_##s = ID2SYM(rb_intern_const(#s))
713 S(constant_cache_invalidations);
714 S(constant_cache_misses);
715 S(global_cvar_state);
716 S(next_shape_id);
717 S(shape_cache_size);
718#undef S
719
720#define SET(name, attr) \
721 if (key == sym_##name) \
722 return SERIALT2NUM(attr); \
723 else if (hash != Qnil) \
724 rb_hash_aset(hash, sym_##name, SERIALT2NUM(attr));
725
726 SET(constant_cache_invalidations, ruby_vm_constant_cache_invalidations);
727 SET(constant_cache_misses, ruby_vm_constant_cache_misses);
728 SET(global_cvar_state, ruby_vm_global_cvar_state);
729 SET(next_shape_id, (rb_serial_t)GET_SHAPE_TREE()->next_shape_id);
730 SET(shape_cache_size, (rb_serial_t)GET_SHAPE_TREE()->cache_size);
731#undef SET
732
733#if USE_DEBUG_COUNTER
734 ruby_debug_counter_show_at_exit(FALSE);
735 for (size_t i = 0; i < RB_DEBUG_COUNTER_MAX; i++) {
736 const VALUE name = rb_sym_intern_ascii_cstr(rb_debug_counter_names[i]);
737 const VALUE boxed_value = SIZET2NUM(rb_debug_counter[i]);
738
739 if (key == name) {
740 return boxed_value;
741 }
742 else if (hash != Qnil) {
743 rb_hash_aset(hash, name, boxed_value);
744 }
745 }
746#endif
747
748 if (!NIL_P(key)) { /* matched key should return above */
749 rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key));
750 }
751
752 return hash;
753}
754
755/* control stack frame */
756
757static void
758vm_set_top_stack(rb_execution_context_t *ec, const rb_iseq_t *iseq)
759{
760 if (ISEQ_BODY(iseq)->type != ISEQ_TYPE_TOP) {
761 rb_raise(rb_eTypeError, "Not a toplevel InstructionSequence");
762 }
763
764 /* for return */
765 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH, rb_ec_thread_ptr(ec)->top_self,
766 VM_BLOCK_HANDLER_NONE,
767 (VALUE)vm_cref_new_toplevel(ec), /* cref or me */
768 ISEQ_BODY(iseq)->iseq_encoded, ec->cfp->sp,
769 ISEQ_BODY(iseq)->local_table_size, ISEQ_BODY(iseq)->stack_max);
770}
771
772static void
773vm_set_eval_stack(rb_execution_context_t *ec, const rb_iseq_t *iseq, const rb_cref_t *cref, const struct rb_block *base_block)
774{
775 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_EVAL | VM_FRAME_FLAG_FINISH,
776 vm_block_self(base_block), VM_GUARDED_PREV_EP(vm_block_ep(base_block)),
777 (VALUE)cref, /* cref or me */
778 ISEQ_BODY(iseq)->iseq_encoded,
779 ec->cfp->sp, ISEQ_BODY(iseq)->local_table_size,
780 ISEQ_BODY(iseq)->stack_max);
781}
782
783static void
784vm_set_main_stack(rb_execution_context_t *ec, const rb_iseq_t *iseq)
785{
786 VALUE toplevel_binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
787 rb_binding_t *bind;
788
789 GetBindingPtr(toplevel_binding, bind);
790 RUBY_ASSERT_MESG(bind, "TOPLEVEL_BINDING is not built");
791
792 vm_set_eval_stack(ec, iseq, 0, &bind->block);
793
794 /* save binding */
795 if (ISEQ_BODY(iseq)->local_table_size > 0) {
796 vm_bind_update_env(toplevel_binding, bind, vm_make_env_object(ec, ec->cfp));
797 }
798}
799
800rb_control_frame_t *
801rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
802{
803 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
804 if (cfp->iseq) {
805 return (rb_control_frame_t *)cfp;
806 }
807 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
808 }
809 return 0;
810}
811
812rb_control_frame_t *
813rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
814{
815 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
816 if (VM_FRAME_RUBYFRAME_P(cfp)) {
817 return (rb_control_frame_t *)cfp;
818 }
819 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
820 }
821 return 0;
822}
823
824static rb_control_frame_t *
825vm_get_ruby_level_caller_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
826{
827 if (VM_FRAME_RUBYFRAME_P(cfp)) {
828 return (rb_control_frame_t *)cfp;
829 }
830
831 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
832
833 while (!RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
834 if (VM_FRAME_RUBYFRAME_P(cfp)) {
835 return (rb_control_frame_t *)cfp;
836 }
837
838 if (VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_PASSED) == FALSE) {
839 break;
840 }
841 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
842 }
843 return 0;
844}
845
846void
847rb_vm_pop_cfunc_frame(void)
848{
849 rb_execution_context_t *ec = GET_EC();
850 rb_control_frame_t *cfp = ec->cfp;
851 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
852
853 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, Qnil);
854 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
855 vm_pop_frame(ec, cfp, cfp->ep);
856}
857
858void
859rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp)
860{
861 /* check skipped frame */
862 while (ec->cfp != cfp) {
863#if VMDEBUG
864 printf("skipped frame: %s\n", vm_frametype_name(ec->cfp));
865#endif
866 if (VM_FRAME_TYPE(ec->cfp) != VM_FRAME_MAGIC_CFUNC) {
867 rb_vm_pop_frame(ec);
868 }
869 else { /* unlikely path */
870 rb_vm_pop_cfunc_frame();
871 }
872 }
873}
874
875/* at exit */
876
877void
878ruby_vm_at_exit(void (*func)(rb_vm_t *))
879{
880 rb_vm_t *vm = GET_VM();
881 rb_at_exit_list *nl = ALLOC(rb_at_exit_list);
882 nl->func = func;
883 nl->next = vm->at_exit;
884 vm->at_exit = nl;
885}
886
887static void
888ruby_vm_run_at_exit_hooks(rb_vm_t *vm)
889{
890 rb_at_exit_list *l = vm->at_exit;
891
892 while (l) {
893 rb_at_exit_list* t = l->next;
894 rb_vm_at_exit_func *func = l->func;
895 ruby_xfree(l);
896 l = t;
897 (*func)(vm);
898 }
899}
900
901/* Env */
902
903static VALUE check_env_value(const rb_env_t *env);
904
905static int
906check_env(const rb_env_t *env)
907{
908 fputs("---\n", stderr);
909 ruby_debug_printf("envptr: %p\n", (void *)&env->ep[0]);
910 ruby_debug_printf("envval: %10p ", (void *)env->ep[1]);
911 dp(env->ep[1]);
912 ruby_debug_printf("ep: %10p\n", (void *)env->ep);
913 if (rb_vm_env_prev_env(env)) {
914 fputs(">>\n", stderr);
915 check_env_value(rb_vm_env_prev_env(env));
916 fputs("<<\n", stderr);
917 }
918 return 1;
919}
920
921static VALUE
922check_env_value(const rb_env_t *env)
923{
924 if (check_env(env)) {
925 return (VALUE)env;
926 }
927 rb_bug("invalid env");
928 return Qnil; /* unreachable */
929}
930
931static VALUE
932vm_block_handler_escape(const rb_execution_context_t *ec, VALUE block_handler)
933{
934 switch (vm_block_handler_type(block_handler)) {
935 case block_handler_type_ifunc:
936 case block_handler_type_iseq:
937 return rb_vm_make_proc(ec, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc);
938
939 case block_handler_type_symbol:
940 case block_handler_type_proc:
941 return block_handler;
942 }
943 VM_UNREACHABLE(vm_block_handler_escape);
944 return Qnil;
945}
946
947static VALUE
948vm_make_env_each(const rb_execution_context_t * const ec, rb_control_frame_t *const cfp)
949{
950 const VALUE * const ep = cfp->ep;
951 VALUE *env_body, *env_ep;
952 int local_size, env_size;
953
954 if (VM_ENV_ESCAPED_P(ep)) {
955 return VM_ENV_ENVVAL(ep);
956 }
957
958 if (!VM_ENV_LOCAL_P(ep)) {
959 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
960 if (!VM_ENV_ESCAPED_P(prev_ep)) {
961 rb_control_frame_t *prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
962
963 while (prev_cfp->ep != prev_ep) {
964 prev_cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(prev_cfp);
965 VM_ASSERT(prev_cfp->ep != NULL);
966 }
967
968 vm_make_env_each(ec, prev_cfp);
969 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_SPECVAL], VM_GUARDED_PREV_EP(prev_cfp->ep));
970 }
971 }
972 else {
973 VALUE block_handler = VM_ENV_BLOCK_HANDLER(ep);
974
975 if (block_handler != VM_BLOCK_HANDLER_NONE) {
976 VALUE blockprocval = vm_block_handler_escape(ec, block_handler);
977 VM_STACK_ENV_WRITE(ep, VM_ENV_DATA_INDEX_SPECVAL, blockprocval);
978 }
979 }
980
981 if (!VM_FRAME_RUBYFRAME_P(cfp)) {
982 local_size = VM_ENV_DATA_SIZE;
983 }
984 else {
985 local_size = ISEQ_BODY(cfp->iseq)->local_table_size;
986 if (ISEQ_BODY(cfp->iseq)->param.flags.forwardable && VM_ENV_LOCAL_P(cfp->ep)) {
987 int ci_offset = local_size - ISEQ_BODY(cfp->iseq)->param.size + VM_ENV_DATA_SIZE;
988
989 CALL_INFO ci = (CALL_INFO)VM_CF_LEP(cfp)[-ci_offset];
990 local_size += vm_ci_argc(ci);
991 }
992 local_size += VM_ENV_DATA_SIZE;
993 }
994
995 /*
996 * # local variables on a stack frame (N == local_size)
997 * [lvar1, lvar2, ..., lvarN, SPECVAL]
998 * ^
999 * ep[0]
1000 *
1001 * # moved local variables
1002 * [lvar1, lvar2, ..., lvarN, SPECVAL, Envval, BlockProcval (if needed)]
1003 * ^ ^
1004 * env->env[0] ep[0]
1005 */
1006
1007 env_size = local_size +
1008 1 /* envval */;
1009
1010 // Careful with order in the following sequence. Each allocation can move objects.
1011 env_body = ALLOC_N(VALUE, env_size);
1012 rb_env_t *env = IMEMO_NEW(rb_env_t, imemo_env, 0);
1013
1014 // Set up env without WB since it's brand new (similar to newobj_init(), newobj_fill())
1015 MEMCPY(env_body, ep - (local_size - 1 /* specval */), VALUE, local_size);
1016
1017 env_ep = &env_body[local_size - 1 /* specval */];
1018 env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1019
1020 env->iseq = (rb_iseq_t *)(VM_FRAME_RUBYFRAME_P(cfp) ? cfp->iseq : NULL);
1021 env->ep = env_ep;
1022 env->env = env_body;
1023 env->env_size = env_size;
1024
1025 cfp->ep = env_ep;
1026 VM_ENV_FLAGS_SET(env_ep, VM_ENV_FLAG_ESCAPED | VM_ENV_FLAG_WB_REQUIRED);
1027 VM_STACK_ENV_WRITE(ep, 0, (VALUE)env); /* GC mark */
1028
1029#if 0
1030 for (i = 0; i < local_size; i++) {
1031 if (VM_FRAME_RUBYFRAME_P(cfp)) {
1032 /* clear value stack for GC */
1033 ep[-local_size + i] = 0;
1034 }
1035 }
1036#endif
1037
1038 // Invalidate JIT code that assumes cfp->ep == vm_base_ptr(cfp).
1039 if (env->iseq) {
1040 rb_yjit_invalidate_ep_is_bp(env->iseq);
1041 }
1042
1043 return (VALUE)env;
1044}
1045
1046static VALUE
1047vm_make_env_object(const rb_execution_context_t *ec, rb_control_frame_t *cfp)
1048{
1049 VALUE envval = vm_make_env_each(ec, cfp);
1050
1051 if (PROCDEBUG) {
1052 check_env_value((const rb_env_t *)envval);
1053 }
1054
1055 return envval;
1056}
1057
1058void
1059rb_vm_stack_to_heap(rb_execution_context_t *ec)
1060{
1061 rb_control_frame_t *cfp = ec->cfp;
1062 while ((cfp = rb_vm_get_binding_creatable_next_cfp(ec, cfp)) != 0) {
1063 vm_make_env_object(ec, cfp);
1064 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1065 }
1066}
1067
1068const rb_env_t *
1069rb_vm_env_prev_env(const rb_env_t *env)
1070{
1071 const VALUE *ep = env->ep;
1072
1073 if (VM_ENV_LOCAL_P(ep)) {
1074 return NULL;
1075 }
1076 else {
1077 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
1078 return VM_ENV_ENVVAL_PTR(prev_ep);
1079 }
1080}
1081
1082static int
1083collect_local_variables_in_iseq(const rb_iseq_t *iseq, const struct local_var_list *vars)
1084{
1085 unsigned int i;
1086 if (!iseq) return 0;
1087 for (i = 0; i < ISEQ_BODY(iseq)->local_table_size; i++) {
1088 local_var_list_add(vars, ISEQ_BODY(iseq)->local_table[i]);
1089 }
1090 return 1;
1091}
1092
1093static void
1094collect_local_variables_in_env(const rb_env_t *env, const struct local_var_list *vars)
1095{
1096 do {
1097 if (VM_ENV_FLAGS(env->ep, VM_ENV_FLAG_ISOLATED)) break;
1098 collect_local_variables_in_iseq(env->iseq, vars);
1099 } while ((env = rb_vm_env_prev_env(env)) != NULL);
1100}
1101
1102static int
1103vm_collect_local_variables_in_heap(const VALUE *ep, const struct local_var_list *vars)
1104{
1105 if (VM_ENV_ESCAPED_P(ep)) {
1106 collect_local_variables_in_env(VM_ENV_ENVVAL_PTR(ep), vars);
1107 return 1;
1108 }
1109 else {
1110 return 0;
1111 }
1112}
1113
1114 VALUE
1115rb_vm_env_local_variables(const rb_env_t *env)
1116{
1117 struct local_var_list vars;
1118 local_var_list_init(&vars);
1119 collect_local_variables_in_env(env, &vars);
1120 return local_var_list_finish(&vars);
1121}
1122
1123 VALUE
1124rb_iseq_local_variables(const rb_iseq_t *iseq)
1125{
1126 struct local_var_list vars;
1127 local_var_list_init(&vars);
1128 while (collect_local_variables_in_iseq(iseq, &vars)) {
1129 iseq = ISEQ_BODY(iseq)->parent_iseq;
1130 }
1131 return local_var_list_finish(&vars);
1132}
1133
1134/* Proc */
1135
1136static VALUE
1137vm_proc_create_from_captured(VALUE klass,
1138 const struct rb_captured_block *captured,
1139 enum rb_block_type block_type,
1140 int8_t is_from_method, int8_t is_lambda)
1141{
1142 VALUE procval = rb_proc_alloc(klass);
1143 rb_proc_t *proc = RTYPEDDATA_DATA(procval);
1144
1145 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), captured->ep));
1146
1147 /* copy block */
1148 RB_OBJ_WRITE(procval, &proc->block.as.captured.code.val, captured->code.val);
1149 RB_OBJ_WRITE(procval, &proc->block.as.captured.self, captured->self);
1150 rb_vm_block_ep_update(procval, &proc->block, captured->ep);
1151
1152 vm_block_type_set(&proc->block, block_type);
1153 proc->is_from_method = is_from_method;
1154 proc->is_lambda = is_lambda;
1155
1156 return procval;
1157}
1158
1159void
1160rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src)
1161{
1162 /* copy block */
1163 switch (vm_block_type(src)) {
1164 case block_type_iseq:
1165 case block_type_ifunc:
1166 RB_OBJ_WRITE(obj, &dst->as.captured.self, src->as.captured.self);
1167 RB_OBJ_WRITE(obj, &dst->as.captured.code.val, src->as.captured.code.val);
1168 rb_vm_block_ep_update(obj, dst, src->as.captured.ep);
1169 break;
1170 case block_type_symbol:
1171 RB_OBJ_WRITE(obj, &dst->as.symbol, src->as.symbol);
1172 break;
1173 case block_type_proc:
1174 RB_OBJ_WRITE(obj, &dst->as.proc, src->as.proc);
1175 break;
1176 }
1177}
1178
1179static VALUE
1180proc_create(VALUE klass, const struct rb_block *block, int8_t is_from_method, int8_t is_lambda)
1181{
1182 VALUE procval = rb_proc_alloc(klass);
1183 rb_proc_t *proc = RTYPEDDATA_DATA(procval);
1184
1185 VM_ASSERT(VM_EP_IN_HEAP_P(GET_EC(), vm_block_ep(block)));
1186 rb_vm_block_copy(procval, &proc->block, block);
1187 vm_block_type_set(&proc->block, block->type);
1188 proc->is_from_method = is_from_method;
1189 proc->is_lambda = is_lambda;
1190
1191 return procval;
1192}
1193
1194 VALUE
1195rb_proc_dup(VALUE self)
1196{
1197 VALUE procval;
1198 rb_proc_t *src;
1199
1200 GetProcPtr(self, src);
1201
1202 switch (vm_block_type(&src->block)) {
1203 case block_type_ifunc:
1204 procval = rb_func_proc_dup(self);
1205 break;
1206 default:
1207 procval = proc_create(rb_obj_class(self), &src->block, src->is_from_method, src->is_lambda);
1208 break;
1209 }
1210
1211 if (RB_OBJ_SHAREABLE_P(self)) FL_SET_RAW(procval, RUBY_FL_SHAREABLE);
1212 RB_GC_GUARD(self); /* for: body = rb_proc_dup(body) */
1213 return procval;
1214}
1215
1216 struct collect_outer_variable_name_data {
1217 VALUE ary;
1218 VALUE read_only;
1219 bool yield;
1220 bool isolate;
1221};
1222
1223static VALUE
1224ID2NUM(ID id)
1225{
1226 if (SIZEOF_VOIDP > SIZEOF_LONG)
1227 return ULL2NUM(id);
1228 else
1229 return ULONG2NUM(id);
1230}
1231
1232static ID
1233NUM2ID(VALUE num)
1234{
1235 if (SIZEOF_VOIDP > SIZEOF_LONG)
1236 return (ID)NUM2ULL(num);
1237 else
1238 return (ID)NUM2ULONG(num);
1239}
1240
1241static enum rb_id_table_iterator_result
1242collect_outer_variable_names(ID id, VALUE val, void *ptr)
1243{
1244 struct collect_outer_variable_name_data *data = (struct collect_outer_variable_name_data *)ptr;
1245
1246 if (id == rb_intern("yield")) {
1247 data->yield = true;
1248 }
1249 else {
1250 VALUE *store;
1251 if (data->isolate ||
1252 val == Qtrue /* write */) {
1253 store = &data->ary;
1254 }
1255 else {
1256 store = &data->read_only;
1257 }
1258 if (*store == Qfalse) *store = rb_ary_new();
1259 rb_ary_push(*store, ID2NUM(id));
1260 }
1261 return ID_TABLE_CONTINUE;
1262}
1263
1264static const rb_env_t *
1265env_copy(const VALUE *src_ep, VALUE read_only_variables)
1266{
1267 const rb_env_t *src_env = (rb_env_t *)VM_ENV_ENVVAL(src_ep);
1268 VM_ASSERT(src_env->ep == src_ep);
1269
1270 VALUE *env_body = ZALLOC_N(VALUE, src_env->env_size); // fill with Qfalse
1271 VALUE *ep = &env_body[src_env->env_size - 2];
1272 const rb_env_t *copied_env = vm_env_new(ep, env_body, src_env->env_size, src_env->iseq);
1273
1274 // Copy after allocations above, since they can move objects in src_ep.
1275 RB_OBJ_WRITE(copied_env, &ep[VM_ENV_DATA_INDEX_ME_CREF], src_ep[VM_ENV_DATA_INDEX_ME_CREF]);
1276 ep[VM_ENV_DATA_INDEX_FLAGS] = src_ep[VM_ENV_DATA_INDEX_FLAGS] | VM_ENV_FLAG_ISOLATED;
1277 if (!VM_ENV_LOCAL_P(src_ep)) {
1278 VM_ENV_FLAGS_SET(ep, VM_ENV_FLAG_LOCAL);
1279 }
1280
1281 if (read_only_variables) {
1282 for (int i=RARRAY_LENINT(read_only_variables)-1; i>=0; i--) {
1283 ID id = NUM2ID(RARRAY_AREF(read_only_variables, i));
1284
1285 for (unsigned int j=0; j<ISEQ_BODY(src_env->iseq)->local_table_size; j++) {
1286 if (id == ISEQ_BODY(src_env->iseq)->local_table[j]) {
1287 VALUE v = src_env->env[j];
1288 if (!rb_ractor_shareable_p(v)) {
1289 VALUE name = rb_id2str(id);
1290 VALUE msg = rb_sprintf("can not make shareable Proc because it can refer"
1291 " unshareable object %+" PRIsVALUE " from ", v);
1292 if (name)
1293 rb_str_catf(msg, "variable '%" PRIsVALUE "'", name);
1294 else
1295 rb_str_cat_cstr(msg, "a hidden variable");
1296 rb_exc_raise(rb_exc_new_str(rb_eRactorIsolationError, msg));
1297 }
1298 RB_OBJ_WRITE((VALUE)copied_env, &env_body[j], v);
1299 rb_ary_delete_at(read_only_variables, i);
1300 break;
1301 }
1302 }
1303 }
1304 }
1305
1306 if (!VM_ENV_LOCAL_P(src_ep)) {
1307 const VALUE *prev_ep = VM_ENV_PREV_EP(src_env->ep);
1308 const rb_env_t *new_prev_env = env_copy(prev_ep, read_only_variables);
1309 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_GUARDED_PREV_EP(new_prev_env->ep);
1310 RB_OBJ_WRITTEN(copied_env, Qundef, new_prev_env);
1311 VM_ENV_FLAGS_UNSET(ep, VM_ENV_FLAG_LOCAL);
1312 }
1313 else {
1314 ep[VM_ENV_DATA_INDEX_SPECVAL] = VM_BLOCK_HANDLER_NONE;
1315 }
1316
1317 return copied_env;
1318}
1319
1320static void
1321proc_isolate_env(VALUE self, rb_proc_t *proc, VALUE read_only_variables)
1322{
1323 const struct rb_captured_block *captured = &proc->block.as.captured;
1324 const rb_env_t *env = env_copy(captured->ep, read_only_variables);
1325 *((const VALUE **)&proc->block.as.captured.ep) = env->ep;
1326 RB_OBJ_WRITTEN(self, Qundef, env);
1327}
1328
1329static VALUE
1330proc_shared_outer_variables(struct rb_id_table *outer_variables, bool isolate, const char *message)
1331{
1332 struct collect_outer_variable_name_data data = {
1333 .isolate = isolate,
1334 .ary = Qfalse,
1335 .read_only = Qfalse,
1336 .yield = false,
1337 };
1338 rb_id_table_foreach(outer_variables, collect_outer_variable_names, (void *)&data);
1339
1340 if (data.ary != Qfalse) {
1341 VALUE str = rb_sprintf("can not %s because it accesses outer variables", message);
1342 VALUE ary = data.ary;
1343 const char *sep = " (";
1344 for (long i = 0; i < RARRAY_LEN(ary); i++) {
1345 VALUE name = rb_id2str(NUM2ID(RARRAY_AREF(ary, i)));
1346 if (!name) continue;
1347 rb_str_cat_cstr(str, sep);
1348 sep = ", ";
1349 rb_str_append(str, name);
1350 }
1351 if (*sep == ',') rb_str_cat_cstr(str, ")");
1352 rb_str_cat_cstr(str, data.yield ? " and uses 'yield'." : ".");
1353 rb_exc_raise(rb_exc_new_str(rb_eArgError, str));
1354 }
1355 else if (data.yield) {
1356 rb_raise(rb_eArgError, "can not %s because it uses 'yield'.", message);
1357 }
1358
1359 return data.read_only;
1360}
1361
1362 VALUE
1363rb_proc_isolate_bang(VALUE self)
1364{
1365 const rb_iseq_t *iseq = vm_proc_iseq(self);
1366
1367 if (iseq) {
1368 rb_proc_t *proc = (rb_proc_t *)RTYPEDDATA_DATA(self);
1369 if (proc->block.type != block_type_iseq) rb_raise(rb_eRuntimeError, "not supported yet");
1370
1371 if (ISEQ_BODY(iseq)->outer_variables) {
1372 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables, true, "isolate a Proc");
1373 }
1374
1375 proc_isolate_env(self, proc, Qfalse);
1376 proc->is_isolated = TRUE;
1377 }
1378
1379 FL_SET_RAW(self, RUBY_FL_SHAREABLE);
1380 return self;
1381}
1382
1383 VALUE
1384rb_proc_isolate(VALUE self)
1385{
1386 VALUE dst = rb_proc_dup(self);
1387 rb_proc_isolate_bang(dst);
1388 return dst;
1389}
1390
1391 VALUE
1392rb_proc_ractor_make_shareable(VALUE self)
1393{
1394 const rb_iseq_t *iseq = vm_proc_iseq(self);
1395
1396 if (iseq) {
1397 rb_proc_t *proc = (rb_proc_t *)RTYPEDDATA_DATA(self);
1398 if (proc->block.type != block_type_iseq) rb_raise(rb_eRuntimeError, "not supported yet");
1399
1400 if (!rb_ractor_shareable_p(vm_block_self(&proc->block))) {
1401 rb_raise(rb_eRactorIsolationError,
1402 "Proc's self is not shareable: %" PRIsVALUE,
1403 self);
1404 }
1405
1406 VALUE read_only_variables = Qfalse;
1407
1408 if (ISEQ_BODY(iseq)->outer_variables) {
1409 read_only_variables =
1410 proc_shared_outer_variables(ISEQ_BODY(iseq)->outer_variables, false, "make a Proc shareable");
1411 }
1412
1413 proc_isolate_env(self, proc, read_only_variables);
1414 proc->is_isolated = TRUE;
1415 }
1416
1417 FL_SET_RAW(self, RUBY_FL_SHAREABLE);
1418 return self;
1419}
1420
1421 VALUE
1422rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda)
1423{
1424 VALUE procval;
1425 enum imemo_type code_type = imemo_type(captured->code.val);
1426
1427 if (!VM_ENV_ESCAPED_P(captured->ep)) {
1428 rb_control_frame_t *cfp = VM_CAPTURED_BLOCK_TO_CFP(captured);
1429 vm_make_env_object(ec, cfp);
1430 }
1431
1432 VM_ASSERT(VM_EP_IN_HEAP_P(ec, captured->ep));
1433 VM_ASSERT(code_type == imemo_iseq || code_type == imemo_ifunc);
1434
1435 procval = vm_proc_create_from_captured(klass, captured,
1436 code_type == imemo_iseq ? block_type_iseq : block_type_ifunc,
1437 FALSE, is_lambda);
1438
1439 if (code_type == imemo_ifunc) {
1440 struct vm_ifunc *ifunc = (struct vm_ifunc *)captured->code.val;
1441 if (ifunc->svar_lep) {
1442 VALUE ep0 = ifunc->svar_lep[0];
1443 if (RB_TYPE_P(ep0, T_IMEMO) && imemo_type_p(ep0, imemo_env)) {
1444 // `ep0 == imemo_env` means this ep is escaped to heap (in env object).
1445 const rb_env_t *env = (const rb_env_t *)ep0;
1446 ifunc->svar_lep = (VALUE *)env->ep;
1447 }
1448 else {
1449 VM_ASSERT(FIXNUM_P(ep0));
1450 if (ep0 & VM_ENV_FLAG_ESCAPED) {
1451 // ok. do nothing
1452 }
1453 else {
1454 ifunc->svar_lep = NULL;
1455 }
1456 }
1457 }
1458 }
1459
1460 return procval;
1461}
1462
1463/* Binding */
1464
1465 VALUE
1466rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp)
1467{
1468 rb_control_frame_t *cfp = rb_vm_get_binding_creatable_next_cfp(ec, src_cfp);
1469 rb_control_frame_t *ruby_level_cfp = rb_vm_get_ruby_level_next_cfp(ec, src_cfp);
1470 VALUE bindval, envval;
1471 rb_binding_t *bind;
1472
1473 if (cfp == 0 || ruby_level_cfp == 0) {
1474 rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
1475 }
1476 if (!VM_FRAME_RUBYFRAME_P(src_cfp) &&
1477 !VM_FRAME_RUBYFRAME_P(RUBY_VM_PREVIOUS_CONTROL_FRAME(src_cfp))) {
1478 rb_raise(rb_eRuntimeError, "Cannot create Binding object for non-Ruby caller");
1479 }
1480
1481 envval = vm_make_env_object(ec, cfp);
1482 bindval = rb_binding_alloc(rb_cBinding);
1483 GetBindingPtr(bindval, bind);
1484 vm_bind_update_env(bindval, bind, envval);
1485 RB_OBJ_WRITE(bindval, &bind->block.as.captured.self, cfp->self);
1486 RB_OBJ_WRITE(bindval, &bind->block.as.captured.code.iseq, cfp->iseq);
1487 RB_OBJ_WRITE(bindval, &bind->pathobj, ISEQ_BODY(ruby_level_cfp->iseq)->location.pathobj);
1488 bind->first_lineno = rb_vm_get_sourceline(ruby_level_cfp);
1489
1490 return bindval;
1491}
1492
1493const VALUE *
1494rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars)
1495{
1496 VALUE envval, pathobj = bind->pathobj;
1497 VALUE path = pathobj_path(pathobj);
1498 VALUE realpath = pathobj_realpath(pathobj);
1499 const struct rb_block *base_block;
1500 const rb_env_t *env;
1501 rb_execution_context_t *ec = GET_EC();
1502 const rb_iseq_t *base_iseq, *iseq;
1503 rb_node_scope_t tmp_node;
1504
1505 if (dyncount < 0) return 0;
1506
1507 base_block = &bind->block;
1508 base_iseq = vm_block_iseq(base_block);
1509
1510 VALUE idtmp = 0;
1511 rb_ast_id_table_t *dyns = ALLOCV(idtmp, sizeof(rb_ast_id_table_t) + dyncount * sizeof(ID));
1512 dyns->size = dyncount;
1513 MEMCPY(dyns->ids, dynvars, ID, dyncount);
1514
1515 rb_node_init(RNODE(&tmp_node), NODE_SCOPE);
1516 tmp_node.nd_tbl = dyns;
1517 tmp_node.nd_body = 0;
1518 tmp_node.nd_args = 0;
1519
1520 VALUE ast_value = rb_ruby_ast_new(RNODE(&tmp_node));
1521
1522 if (base_iseq) {
1523 iseq = rb_iseq_new(ast_value, ISEQ_BODY(base_iseq)->location.label, path, realpath, base_iseq, ISEQ_TYPE_EVAL);
1524 }
1525 else {
1526 VALUE tempstr = rb_fstring_lit("<temp>");
1527 iseq = rb_iseq_new_top(ast_value, tempstr, tempstr, tempstr, NULL);
1528 }
1529 tmp_node.nd_tbl = 0; /* reset table */
1530 ALLOCV_END(idtmp);
1531
1532 vm_set_eval_stack(ec, iseq, 0, base_block);
1533 vm_bind_update_env(bindval, bind, envval = vm_make_env_object(ec, ec->cfp));
1534 rb_vm_pop_frame(ec);
1535
1536 env = (const rb_env_t *)envval;
1537 return env->env;
1538}
1539
1540/* C -> Ruby: block */
1541
1542static inline void
1543invoke_block(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE self, const struct rb_captured_block *captured, const rb_cref_t *cref, VALUE type, int opt_pc)
1544{
1545 int arg_size = ISEQ_BODY(iseq)->param.size;
1546
1547 vm_push_frame(ec, iseq, type | VM_FRAME_FLAG_FINISH, self,
1548 VM_GUARDED_PREV_EP(captured->ep),
1549 (VALUE)cref, /* cref or method */
1550 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1551 ec->cfp->sp + arg_size,
1552 ISEQ_BODY(iseq)->local_table_size - arg_size,
1553 ISEQ_BODY(iseq)->stack_max);
1554}
1555
1556static inline void
1557invoke_bmethod(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE self, const struct rb_captured_block *captured, const rb_callable_method_entry_t *me, VALUE type, int opt_pc)
1558{
1559 /* bmethod call from outside the VM */
1560 int arg_size = ISEQ_BODY(iseq)->param.size;
1561
1562 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
1563
1564 vm_push_frame(ec, iseq, type | VM_FRAME_FLAG_BMETHOD, self,
1565 VM_GUARDED_PREV_EP(captured->ep),
1566 (VALUE)me,
1567 ISEQ_BODY(iseq)->iseq_encoded + opt_pc,
1568 ec->cfp->sp + 1 /* self */ + arg_size,
1569 ISEQ_BODY(iseq)->local_table_size - arg_size,
1570 ISEQ_BODY(iseq)->stack_max);
1571
1572 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH);
1573}
1574
1575ALWAYS_INLINE(static VALUE
1576 invoke_iseq_block_from_c(rb_execution_context_t *ec, const struct rb_captured_block *captured,
1577 VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler,
1578 const rb_cref_t *cref, int is_lambda, const rb_callable_method_entry_t *me));
1579
1580static inline VALUE
1581invoke_iseq_block_from_c(rb_execution_context_t *ec, const struct rb_captured_block *captured,
1582 VALUE self, int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler,
1583 const rb_cref_t *cref, int is_lambda, const rb_callable_method_entry_t *me)
1584{
1585 const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
1586 int opt_pc;
1587 VALUE type = VM_FRAME_MAGIC_BLOCK | (is_lambda ? VM_FRAME_FLAG_LAMBDA : 0);
1588 rb_control_frame_t *cfp = ec->cfp;
1589 VALUE *sp = cfp->sp;
1590 int flags = (kw_splat ? VM_CALL_KW_SPLAT : 0);
1591 VALUE *use_argv = (VALUE *)argv;
1592 VALUE av[2];
1593
1594 stack_check(ec);
1595
1596 if (UNLIKELY(argc > VM_ARGC_STACK_MAX) &&
1597 (VM_ARGC_STACK_MAX >= 1 ||
1598 /* Skip ruby array for potential autosplat case */
1599 (argc != 1 || is_lambda))) {
1600 use_argv = vm_argv_ruby_array(av, argv, &flags, &argc, kw_splat);
1601 }
1602
1603 CHECK_VM_STACK_OVERFLOW(cfp, argc + 1);
1604 vm_check_canary(ec, sp);
1605
1606 VALUE *stack_argv = sp;
1607 if (me) {
1608 *sp = self; // bemthods need `self` on the VM stack
1609 stack_argv++;
1610 }
1611 cfp->sp = stack_argv + argc;
1612 MEMCPY(stack_argv, use_argv, VALUE, argc); // restrict: new stack space
1613
1614 opt_pc = vm_yield_setup_args(ec, iseq, argc, stack_argv, flags, passed_block_handler,
1615 (is_lambda ? arg_setup_method : arg_setup_block));
1616 cfp->sp = sp;
1617
1618 if (me == NULL) {
1619 invoke_block(ec, iseq, self, captured, cref, type, opt_pc);
1620 }
1621 else {
1622 invoke_bmethod(ec, iseq, self, captured, me, type, opt_pc);
1623 }
1624
1625 return vm_exec(ec);
1626}
1627
1628static VALUE
1629invoke_block_from_c_bh(rb_execution_context_t *ec, VALUE block_handler,
1630 int argc, const VALUE *argv,
1631 int kw_splat, VALUE passed_block_handler, const rb_cref_t *cref,
1632 int is_lambda, int force_blockarg)
1633{
1634 again:
1635 switch (vm_block_handler_type(block_handler)) {
1636 case block_handler_type_iseq:
1637 {
1638 const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(block_handler);
1639 return invoke_iseq_block_from_c(ec, captured, captured->self,
1640 argc, argv, kw_splat, passed_block_handler,
1641 cref, is_lambda, NULL);
1642 }
1643 case block_handler_type_ifunc:
1644 return vm_yield_with_cfunc(ec, VM_BH_TO_IFUNC_BLOCK(block_handler),
1645 VM_BH_TO_IFUNC_BLOCK(block_handler)->self,
1646 argc, argv, kw_splat, passed_block_handler, NULL);
1647 case block_handler_type_symbol:
1648 return vm_yield_with_symbol(ec, VM_BH_TO_SYMBOL(block_handler),
1649 argc, argv, kw_splat, passed_block_handler);
1650 case block_handler_type_proc:
1651 if (force_blockarg == FALSE) {
1652 is_lambda = block_proc_is_lambda(VM_BH_TO_PROC(block_handler));
1653 }
1654 block_handler = vm_proc_to_block_handler(VM_BH_TO_PROC(block_handler));
1655 goto again;
1656 }
1657 VM_UNREACHABLE(invoke_block_from_c_splattable);
1658 return Qundef;
1659}
1660
1661static inline VALUE
1662check_block_handler(rb_execution_context_t *ec)
1663{
1664 VALUE block_handler = VM_CF_BLOCK_HANDLER(ec->cfp);
1665 vm_block_handler_verify(block_handler);
1666 if (UNLIKELY(block_handler == VM_BLOCK_HANDLER_NONE)) {
1667 rb_vm_localjump_error("no block given", Qnil, 0);
1668 }
1669
1670 return block_handler;
1671}
1672
1673static VALUE
1674vm_yield_with_cref(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat, const rb_cref_t *cref, int is_lambda)
1675{
1676 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1677 argc, argv, kw_splat, VM_BLOCK_HANDLER_NONE,
1678 cref, is_lambda, FALSE);
1679}
1680
1681static VALUE
1682vm_yield(rb_execution_context_t *ec, int argc, const VALUE *argv, int kw_splat)
1683{
1684 return vm_yield_with_cref(ec, argc, argv, kw_splat, NULL, FALSE);
1685}
1686
1687static VALUE
1688vm_yield_with_block(rb_execution_context_t *ec, int argc, const VALUE *argv, VALUE block_handler, int kw_splat)
1689{
1690 return invoke_block_from_c_bh(ec, check_block_handler(ec),
1691 argc, argv, kw_splat, block_handler,
1692 NULL, FALSE, FALSE);
1693}
1694
1695static VALUE
1696vm_yield_force_blockarg(rb_execution_context_t *ec, VALUE args)
1697{
1698 return invoke_block_from_c_bh(ec, check_block_handler(ec), 1, &args,
1699 RB_NO_KEYWORDS, VM_BLOCK_HANDLER_NONE, NULL, FALSE, TRUE);
1700}
1701
1702ALWAYS_INLINE(static VALUE
1703 invoke_block_from_c_proc(rb_execution_context_t *ec, const rb_proc_t *proc,
1704 VALUE self, int argc, const VALUE *argv,
1705 int kw_splat, VALUE passed_block_handler, int is_lambda,
1706 const rb_callable_method_entry_t *me));
1707
1708static inline VALUE
1709invoke_block_from_c_proc(rb_execution_context_t *ec, const rb_proc_t *proc,
1710 VALUE self, int argc, const VALUE *argv,
1711 int kw_splat, VALUE passed_block_handler, int is_lambda,
1712 const rb_callable_method_entry_t *me)
1713{
1714 const struct rb_block *block = &proc->block;
1715
1716 again:
1717 switch (vm_block_type(block)) {
1718 case block_type_iseq:
1719 return invoke_iseq_block_from_c(ec, &block->as.captured, self, argc, argv, kw_splat, passed_block_handler, NULL, is_lambda, me);
1720 case block_type_ifunc:
1721 if (kw_splat == 1) {
1722 VALUE keyword_hash = argv[argc-1];
1723 if (!RB_TYPE_P(keyword_hash, T_HASH)) {
1724 keyword_hash = rb_to_hash_type(keyword_hash);
1725 }
1726 if (RHASH_EMPTY_P(keyword_hash)) {
1727 argc--;
1728 }
1729 else {
1730 ((VALUE *)argv)[argc-1] = rb_hash_dup(keyword_hash);
1731 }
1732 }
1733 return vm_yield_with_cfunc(ec, &block->as.captured, self, argc, argv, kw_splat, passed_block_handler, me);
1734 case block_type_symbol:
1735 return vm_yield_with_symbol(ec, block->as.symbol, argc, argv, kw_splat, passed_block_handler);
1736 case block_type_proc:
1737 is_lambda = block_proc_is_lambda(block->as.proc);
1738 block = vm_proc_block(block->as.proc);
1739 goto again;
1740 }
1741 VM_UNREACHABLE(invoke_block_from_c_proc);
1742 return Qundef;
1743}
1744
1745static VALUE
1746vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
1747 int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler)
1748{
1749 return invoke_block_from_c_proc(ec, proc, self, argc, argv, kw_splat, passed_block_handler, proc->is_lambda, NULL);
1750}
1751
1752static VALUE
1753vm_invoke_bmethod(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
1754 int argc, const VALUE *argv, int kw_splat, VALUE block_handler, const rb_callable_method_entry_t *me)
1755{
1756 return invoke_block_from_c_proc(ec, proc, self, argc, argv, kw_splat, block_handler, TRUE, me);
1757}
1758
1759 VALUE
1760rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc,
1761 int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler)
1762{
1763 VALUE self = vm_block_self(&proc->block);
1764 vm_block_handler_verify(passed_block_handler);
1765
1766 if (proc->is_from_method) {
1767 return vm_invoke_bmethod(ec, proc, self, argc, argv, kw_splat, passed_block_handler, NULL);
1768 }
1769 else {
1770 return vm_invoke_proc(ec, proc, self, argc, argv, kw_splat, passed_block_handler);
1771 }
1772}
1773
1774 VALUE
1775rb_vm_invoke_proc_with_self(rb_execution_context_t *ec, rb_proc_t *proc, VALUE self,
1776 int argc, const VALUE *argv, int kw_splat, VALUE passed_block_handler)
1777{
1778 vm_block_handler_verify(passed_block_handler);
1779
1780 if (proc->is_from_method) {
1781 return vm_invoke_bmethod(ec, proc, self, argc, argv, kw_splat, passed_block_handler, NULL);
1782 }
1783 else {
1784 return vm_invoke_proc(ec, proc, self, argc, argv, kw_splat, passed_block_handler);
1785 }
1786}
1787
1788/* special variable */
1789
1790 VALUE *
1791rb_vm_svar_lep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1792{
1793 while (cfp->pc == 0 || cfp->iseq == 0) {
1794 if (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_IFUNC) {
1795 struct vm_ifunc *ifunc = (struct vm_ifunc *)cfp->iseq;
1796 return ifunc->svar_lep;
1797 }
1798 else {
1799 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1800 }
1801
1802 if (RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(ec, cfp)) {
1803 return NULL;
1804 }
1805 }
1806
1807 return (VALUE *)VM_CF_LEP(cfp);
1808}
1809
1810static VALUE
1811vm_cfp_svar_get(const rb_execution_context_t *ec, rb_control_frame_t *cfp, VALUE key)
1812{
1813 return lep_svar_get(ec, rb_vm_svar_lep(ec, cfp), key);
1814}
1815
1816static void
1817vm_cfp_svar_set(const rb_execution_context_t *ec, rb_control_frame_t *cfp, VALUE key, const VALUE val)
1818{
1819 lep_svar_set(ec, rb_vm_svar_lep(ec, cfp), key, val);
1820}
1821
1822static VALUE
1823vm_svar_get(const rb_execution_context_t *ec, VALUE key)
1824{
1825 return vm_cfp_svar_get(ec, ec->cfp, key);
1826}
1827
1828static void
1829vm_svar_set(const rb_execution_context_t *ec, VALUE key, VALUE val)
1830{
1831 vm_cfp_svar_set(ec, ec->cfp, key, val);
1832}
1833
1834 VALUE
1835rb_backref_get(void)
1836{
1837 return vm_svar_get(GET_EC(), VM_SVAR_BACKREF);
1838}
1839
1840void
1841rb_backref_set(VALUE val)
1842{
1843 vm_svar_set(GET_EC(), VM_SVAR_BACKREF, val);
1844}
1845
1846 VALUE
1847rb_lastline_get(void)
1848{
1849 return vm_svar_get(GET_EC(), VM_SVAR_LASTLINE);
1850}
1851
1852void
1853rb_lastline_set(VALUE val)
1854{
1855 vm_svar_set(GET_EC(), VM_SVAR_LASTLINE, val);
1856}
1857
1858void
1859rb_lastline_set_up(VALUE val, unsigned int up)
1860{
1861 rb_control_frame_t * cfp = GET_EC()->cfp;
1862
1863 for(unsigned int i = 0; i < up; i++) {
1864 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
1865 }
1866 vm_cfp_svar_set(GET_EC(), cfp, VM_SVAR_LASTLINE, val);
1867}
1868
1869/* misc */
1870
1871const char *
1872rb_sourcefile(void)
1873{
1874 const rb_execution_context_t *ec = GET_EC();
1875 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1876
1877 if (cfp) {
1878 return RSTRING_PTR(rb_iseq_path(cfp->iseq));
1879 }
1880 else {
1881 return 0;
1882 }
1883}
1884
1885int
1886rb_sourceline(void)
1887{
1888 const rb_execution_context_t *ec = GET_EC();
1889 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1890
1891 if (cfp) {
1892 return rb_vm_get_sourceline(cfp);
1893 }
1894 else {
1895 return 0;
1896 }
1897}
1898
1899 VALUE
1900rb_source_location(int *pline)
1901{
1902 const rb_execution_context_t *ec = GET_EC();
1903 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1904
1905 if (cfp && VM_FRAME_RUBYFRAME_P(cfp)) {
1906 if (pline) *pline = rb_vm_get_sourceline(cfp);
1907 return rb_iseq_path(cfp->iseq);
1908 }
1909 else {
1910 if (pline) *pline = 0;
1911 return Qnil;
1912 }
1913}
1914
1915const char *
1916rb_source_location_cstr(int *pline)
1917{
1918 VALUE path = rb_source_location(pline);
1919 if (NIL_P(path)) return NULL;
1920 return RSTRING_PTR(path);
1921}
1922
1923rb_cref_t *
1924rb_vm_cref(void)
1925{
1926 const rb_execution_context_t *ec = GET_EC();
1927 return vm_ec_cref(ec);
1928}
1929
1930rb_cref_t *
1931rb_vm_cref_replace_with_duplicated_cref(void)
1932{
1933 const rb_execution_context_t *ec = GET_EC();
1934 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1935 rb_cref_t *cref = vm_cref_replace_with_duplicated_cref(cfp->ep);
1936 ASSUME(cref);
1937 return cref;
1938}
1939
1940const rb_cref_t *
1941rb_vm_cref_in_context(VALUE self, VALUE cbase)
1942{
1943 const rb_execution_context_t *ec = GET_EC();
1944 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1945 const rb_cref_t *cref;
1946 if (!cfp || cfp->self != self) return NULL;
1947 if (!vm_env_cref_by_cref(cfp->ep)) return NULL;
1948 cref = vm_get_cref(cfp->ep);
1949 if (CREF_CLASS(cref) != cbase) return NULL;
1950 return cref;
1951}
1952
1953#if 0
1954void
1955debug_cref(rb_cref_t *cref)
1956{
1957 while (cref) {
1958 dp(CREF_CLASS(cref));
1959 printf("%ld\n", CREF_VISI(cref));
1960 cref = CREF_NEXT(cref);
1961 }
1962}
1963#endif
1964
1965 VALUE
1966rb_vm_cbase(void)
1967{
1968 const rb_execution_context_t *ec = GET_EC();
1969 const rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);
1970
1971 if (cfp == 0) {
1972 rb_raise(rb_eRuntimeError, "Can't call on top of Fiber or Thread");
1973 }
1974 return vm_get_cbase(cfp->ep);
1975}
1976
1977/* jump */
1978
1979static VALUE
1980make_localjump_error(const char *mesg, VALUE value, int reason)
1981{
1982 extern VALUE rb_eLocalJumpError;
1983 VALUE exc = rb_exc_new2(rb_eLocalJumpError, mesg);
1984 ID id;
1985
1986 switch (reason) {
1987 case TAG_BREAK:
1988 CONST_ID(id, "break");
1989 break;
1990 case TAG_REDO:
1991 CONST_ID(id, "redo");
1992 break;
1993 case TAG_RETRY:
1994 CONST_ID(id, "retry");
1995 break;
1996 case TAG_NEXT:
1997 CONST_ID(id, "next");
1998 break;
1999 case TAG_RETURN:
2000 CONST_ID(id, "return");
2001 break;
2002 default:
2003 CONST_ID(id, "noreason");
2004 break;
2005 }
2006 rb_iv_set(exc, "@exit_value", value);
2007 rb_iv_set(exc, "@reason", ID2SYM(id));
2008 return exc;
2009}
2010
2011void
2012rb_vm_localjump_error(const char *mesg, VALUE value, int reason)
2013{
2014 VALUE exc = make_localjump_error(mesg, value, reason);
2015 rb_exc_raise(exc);
2016}
2017
2018 VALUE
2019rb_vm_make_jump_tag_but_local_jump(enum ruby_tag_type state, VALUE val)
2020{
2021 const char *mesg;
2022
2023 switch (state) {
2024 case TAG_RETURN:
2025 mesg = "unexpected return";
2026 break;
2027 case TAG_BREAK:
2028 mesg = "unexpected break";
2029 break;
2030 case TAG_NEXT:
2031 mesg = "unexpected next";
2032 break;
2033 case TAG_REDO:
2034 mesg = "unexpected redo";
2035 val = Qnil;
2036 break;
2037 case TAG_RETRY:
2038 mesg = "retry outside of rescue clause";
2039 val = Qnil;
2040 break;
2041 default:
2042 return Qnil;
2043 }
2044 if (UNDEF_P(val)) {
2045 val = GET_EC()->tag->retval;
2046 }
2047 return make_localjump_error(mesg, val, state);
2048}
2049
2050void
2051rb_vm_jump_tag_but_local_jump(enum ruby_tag_type state)
2052{
2053 VALUE exc = rb_vm_make_jump_tag_but_local_jump(state, Qundef);
2054 if (!NIL_P(exc)) rb_exc_raise(exc);
2055 EC_JUMP_TAG(GET_EC(), state);
2056}
2057
2058static rb_control_frame_t *
2059next_not_local_frame(rb_control_frame_t *cfp)
2060{
2061 while (VM_ENV_LOCAL_P(cfp->ep)) {
2062 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
2063 }
2064 return cfp;
2065}
2066
2067NORETURN(static void vm_iter_break(rb_execution_context_t *ec, VALUE val));
2068
2069static void
2070vm_iter_break(rb_execution_context_t *ec, VALUE val)
2071{
2072 rb_control_frame_t *cfp = next_not_local_frame(ec->cfp);
2073 const VALUE *ep = VM_CF_PREV_EP(cfp);
2074 const rb_control_frame_t *target_cfp = rb_vm_search_cf_from_ep(ec, cfp, ep);
2075
2076 if (!target_cfp) {
2077 rb_vm_localjump_error("unexpected break", val, TAG_BREAK);
2078 }
2079
2080 ec->errinfo = (VALUE)THROW_DATA_NEW(val, target_cfp, TAG_BREAK);
2081 EC_JUMP_TAG(ec, TAG_BREAK);
2082}
2083
2084void
2085rb_iter_break(void)
2086{
2087 vm_iter_break(GET_EC(), Qnil);
2088}
2089
2090void
2091rb_iter_break_value(VALUE val)
2092{
2093 vm_iter_break(GET_EC(), val);
2094}
2095
2096/* optimization: redefine management */
2097
2098short ruby_vm_redefined_flag[BOP_LAST_];
2099static st_table *vm_opt_method_def_table = 0;
2100static st_table *vm_opt_mid_table = 0;
2101
2102void
2103rb_free_vm_opt_tables(void)
2104{
2105 st_free_table(vm_opt_method_def_table);
2106 st_free_table(vm_opt_mid_table);
2107}
2108
2109static int
2110vm_redefinition_check_flag(VALUE klass)
2111{
2112 if (klass == rb_cInteger) return INTEGER_REDEFINED_OP_FLAG;
2113 if (klass == rb_cFloat) return FLOAT_REDEFINED_OP_FLAG;
2114 if (klass == rb_cString) return STRING_REDEFINED_OP_FLAG;
2115 if (klass == rb_cArray) return ARRAY_REDEFINED_OP_FLAG;
2116 if (klass == rb_cHash) return HASH_REDEFINED_OP_FLAG;
2117 if (klass == rb_cSymbol) return SYMBOL_REDEFINED_OP_FLAG;
2118#if 0
2119 if (klass == rb_cTime) return TIME_REDEFINED_OP_FLAG;
2120#endif
2121 if (klass == rb_cRegexp) return REGEXP_REDEFINED_OP_FLAG;
2122 if (klass == rb_cNilClass) return NIL_REDEFINED_OP_FLAG;
2123 if (klass == rb_cTrueClass) return TRUE_REDEFINED_OP_FLAG;
2124 if (klass == rb_cFalseClass) return FALSE_REDEFINED_OP_FLAG;
2125 if (klass == rb_cProc) return PROC_REDEFINED_OP_FLAG;
2126 return 0;
2127}
2128
2129int
2130rb_vm_check_optimizable_mid(VALUE mid)
2131{
2132 if (!vm_opt_mid_table) {
2133 return FALSE;
2134 }
2135
2136 return st_lookup(vm_opt_mid_table, mid, NULL);
2137}
2138
2139static int
2140vm_redefinition_check_method_type(const rb_method_entry_t *me)
2141{
2142 if (me->called_id != me->def->original_id) {
2143 return FALSE;
2144 }
2145
2146 if (METHOD_ENTRY_BASIC(me)) return TRUE;
2147
2148 const rb_method_definition_t *def = me->def;
2149 switch (def->type) {
2150 case VM_METHOD_TYPE_CFUNC:
2151 case VM_METHOD_TYPE_OPTIMIZED:
2152 return TRUE;
2153 default:
2154 return FALSE;
2155 }
2156}
2157
2158static void
2159rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass)
2160{
2161 st_data_t bop;
2162 if (RB_TYPE_P(klass, T_ICLASS) && FL_TEST(klass, RICLASS_IS_ORIGIN) &&
2163 RB_TYPE_P(RBASIC_CLASS(klass), T_CLASS)) {
2164 klass = RBASIC_CLASS(klass);
2165 }
2166 if (vm_redefinition_check_method_type(me)) {
2167 if (st_lookup(vm_opt_method_def_table, (st_data_t)me->def, &bop)) {
2168 int flag = vm_redefinition_check_flag(klass);
2169 if (flag != 0) {
2170 rb_category_warn(
2171 RB_WARN_CATEGORY_PERFORMANCE,
2172 "Redefining '%s#%s' disables interpreter and JIT optimizations",
2173 rb_class2name(me->owner),
2174 rb_id2name(me->called_id)
2175 );
2176 rb_yjit_bop_redefined(flag, (enum ruby_basic_operators)bop);
2177 rb_rjit_bop_redefined(flag, (enum ruby_basic_operators)bop);
2178 ruby_vm_redefined_flag[bop] |= flag;
2179 }
2180 }
2181 }
2182}
2183
2184static enum rb_id_table_iterator_result
2185check_redefined_method(ID mid, VALUE value, void *data)
2186{
2187 VALUE klass = (VALUE)data;
2188 const rb_method_entry_t *me = (rb_method_entry_t *)value;
2189 const rb_method_entry_t *newme = rb_method_entry(klass, mid);
2190
2191 if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
2192
2193 return ID_TABLE_CONTINUE;
2194}
2195
2196void
2197rb_vm_check_redefinition_by_prepend(VALUE klass)
2198{
2199 if (!vm_redefinition_check_flag(klass)) return;
2200 rb_id_table_foreach(RCLASS_M_TBL(RCLASS_ORIGIN(klass)), check_redefined_method, (void *)klass);
2201}
2202
2203static void
2204add_opt_method_entry_bop(const rb_method_entry_t *me, ID mid, enum ruby_basic_operators bop)
2205{
2206 st_insert(vm_opt_method_def_table, (st_data_t)me->def, (st_data_t)bop);
2207 st_insert(vm_opt_mid_table, (st_data_t)mid, (st_data_t)Qtrue);
2208}
2209
2210static void
2211add_opt_method(VALUE klass, ID mid, enum ruby_basic_operators bop)
2212{
2213 const rb_method_entry_t *me = rb_method_entry_at(klass, mid);
2214
2215 if (me && vm_redefinition_check_method_type(me)) {
2216 add_opt_method_entry_bop(me, mid, bop);
2217 }
2218 else {
2219 rb_bug("undefined optimized method: %s", rb_id2name(mid));
2220 }
2221}
2222
2223static enum ruby_basic_operators vm_redefinition_bop_for_id(ID mid);
2224
2225static void
2226add_opt_method_entry(const rb_method_entry_t *me)
2227{
2228 if (me && vm_redefinition_check_method_type(me)) {
2229 ID mid = me->called_id;
2230 enum ruby_basic_operators bop = vm_redefinition_bop_for_id(mid);
2231 if ((int)bop >= 0) {
2232 add_opt_method_entry_bop(me, mid, bop);
2233 }
2234 }
2235}
2236
2237static void
2238vm_init_redefined_flag(void)
2239{
2240 ID mid;
2241 enum ruby_basic_operators bop;
2242
2243#define OP(mid_, bop_) (mid = id##mid_, bop = BOP_##bop_, ruby_vm_redefined_flag[bop] = 0)
2244#define C(k) add_opt_method(rb_c##k, mid, bop)
2245 OP(PLUS, PLUS), (C(Integer), C(Float), C(String), C(Array));
2246 OP(MINUS, MINUS), (C(Integer), C(Float));
2247 OP(MULT, MULT), (C(Integer), C(Float));
2248 OP(DIV, DIV), (C(Integer), C(Float));
2249 OP(MOD, MOD), (C(Integer), C(Float));
2250 OP(Eq, EQ), (C(Integer), C(Float), C(String), C(Symbol));
2251 OP(Eqq, EQQ), (C(Integer), C(Float), C(Symbol), C(String),
2252 C(NilClass), C(TrueClass), C(FalseClass));
2253 OP(LT, LT), (C(Integer), C(Float));
2254 OP(LE, LE), (C(Integer), C(Float));
2255 OP(GT, GT), (C(Integer), C(Float));
2256 OP(GE, GE), (C(Integer), C(Float));
2257 OP(LTLT, LTLT), (C(String), C(Array));
2258 OP(AREF, AREF), (C(Array), C(Hash), C(Integer));
2259 OP(ASET, ASET), (C(Array), C(Hash));
2260 OP(Length, LENGTH), (C(Array), C(String), C(Hash));
2261 OP(Size, SIZE), (C(Array), C(String), C(Hash));
2262 OP(EmptyP, EMPTY_P), (C(Array), C(String), C(Hash));
2263 OP(Succ, SUCC), (C(Integer), C(String));
2264 OP(EqTilde, MATCH), (C(Regexp), C(String));
2265 OP(Freeze, FREEZE), (C(String), C(Array), C(Hash));
2266 OP(UMinus, UMINUS), (C(String));
2267 OP(Max, MAX), (C(Array));
2268 OP(Min, MIN), (C(Array));
2269 OP(Hash, HASH), (C(Array));
2270 OP(Call, CALL), (C(Proc));
2271 OP(And, AND), (C(Integer));
2272 OP(Or, OR), (C(Integer));
2273 OP(NilP, NIL_P), (C(NilClass));
2274 OP(Cmp, CMP), (C(Integer), C(Float), C(String));
2275 OP(Default, DEFAULT), (C(Hash));
2276 OP(IncludeP, INCLUDE_P), (C(Array));
2277#undef C
2278#undef OP
2279}
2280
2281static enum ruby_basic_operators
2282vm_redefinition_bop_for_id(ID mid)
2283{
2284 switch (mid) {
2285#define OP(mid_, bop_) case id##mid_: return BOP_##bop_
2286 OP(PLUS, PLUS);
2287 OP(MINUS, MINUS);
2288 OP(MULT, MULT);
2289 OP(DIV, DIV);
2290 OP(MOD, MOD);
2291 OP(Eq, EQ);
2292 OP(Eqq, EQQ);
2293 OP(LT, LT);
2294 OP(LE, LE);
2295 OP(GT, GT);
2296 OP(GE, GE);
2297 OP(LTLT, LTLT);
2298 OP(AREF, AREF);
2299 OP(ASET, ASET);
2300 OP(Length, LENGTH);
2301 OP(Size, SIZE);
2302 OP(EmptyP, EMPTY_P);
2303 OP(Succ, SUCC);
2304 OP(EqTilde, MATCH);
2305 OP(Freeze, FREEZE);
2306 OP(UMinus, UMINUS);
2307 OP(Max, MAX);
2308 OP(Min, MIN);
2309 OP(Hash, HASH);
2310 OP(Call, CALL);
2311 OP(And, AND);
2312 OP(Or, OR);
2313 OP(NilP, NIL_P);
2314 OP(Cmp, CMP);
2315 OP(Default, DEFAULT);
2316 OP(Pack, PACK);
2317#undef OP
2318 }
2319 return -1;
2320}
2321
2322/* for vm development */
2323
2324#if VMDEBUG
2325static const char *
2326vm_frametype_name(const rb_control_frame_t *cfp)
2327{
2328 switch (VM_FRAME_TYPE(cfp)) {
2329 case VM_FRAME_MAGIC_METHOD: return "method";
2330 case VM_FRAME_MAGIC_BLOCK: return "block";
2331 case VM_FRAME_MAGIC_CLASS: return "class";
2332 case VM_FRAME_MAGIC_TOP: return "top";
2333 case VM_FRAME_MAGIC_CFUNC: return "cfunc";
2334 case VM_FRAME_MAGIC_IFUNC: return "ifunc";
2335 case VM_FRAME_MAGIC_EVAL: return "eval";
2336 case VM_FRAME_MAGIC_RESCUE: return "rescue";
2337 default:
2338 rb_bug("unknown frame");
2339 }
2340}
2341#endif
2342
2343static VALUE
2344frame_return_value(const struct vm_throw_data *err)
2345{
2346 if (THROW_DATA_P(err) &&
2347 THROW_DATA_STATE(err) == TAG_BREAK &&
2348 THROW_DATA_CONSUMED_P(err) == FALSE) {
2349 return THROW_DATA_VAL(err);
2350 }
2351 else {
2352 return Qnil;
2353 }
2354}
2355
2356#if 0
2357/* for debug */
2358static const char *
2359frame_name(const rb_control_frame_t *cfp)
2360{
2361 unsigned long type = VM_FRAME_TYPE(cfp);
2362#define C(t) if (type == VM_FRAME_MAGIC_##t) return #t
2363 C(METHOD);
2364 C(BLOCK);
2365 C(CLASS);
2366 C(TOP);
2367 C(CFUNC);
2368 C(PROC);
2369 C(IFUNC);
2370 C(EVAL);
2371 C(LAMBDA);
2372 C(RESCUE);
2373 C(DUMMY);
2374#undef C
2375 return "unknown";
2376}
2377#endif
2378
2379// cfp_returning_with_value:
2380// Whether cfp is the last frame in the unwinding process for a non-local return.
2381static void
2382hook_before_rewind(rb_execution_context_t *ec, bool cfp_returning_with_value, int state, struct vm_throw_data *err)
2383{
2384 if (state == TAG_RAISE && RBASIC(err)->klass == rb_eSysStackError) {
2385 return;
2386 }
2387 else {
2388 const rb_iseq_t *iseq = ec->cfp->iseq;
2389 rb_hook_list_t *local_hooks = iseq->aux.exec.local_hooks;
2390
2391 switch (VM_FRAME_TYPE(ec->cfp)) {
2392 case VM_FRAME_MAGIC_METHOD:
2393 RUBY_DTRACE_METHOD_RETURN_HOOK(ec, 0, 0);
2394 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2395
2396 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_RETURN)) {
2397 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_RETURN,
2398 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2399 }
2400
2401 THROW_DATA_CONSUMED_SET(err);
2402 break;
2403 case VM_FRAME_MAGIC_BLOCK:
2404 if (VM_FRAME_BMETHOD_P(ec->cfp)) {
2405 VALUE bmethod_return_value = frame_return_value(err);
2406 if (cfp_returning_with_value) {
2407 // Non-local return terminating at a BMETHOD control frame.
2408 bmethod_return_value = THROW_DATA_VAL(err);
2409 }
2410
2411
2412 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, bmethod_return_value);
2413 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_B_RETURN)) {
2414 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_B_RETURN,
2415 ec->cfp->self, 0, 0, 0, bmethod_return_value, TRUE);
2416 }
2417
2418 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(ec->cfp);
2419
2420 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_RETURN, ec->cfp->self,
2421 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2422 rb_vm_frame_method_entry(ec->cfp)->called_id,
2423 rb_vm_frame_method_entry(ec->cfp)->owner,
2424 bmethod_return_value);
2425
2426 VM_ASSERT(me->def->type == VM_METHOD_TYPE_BMETHOD);
2427 local_hooks = me->def->body.bmethod.hooks;
2428
2429 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_RETURN)) {
2430 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_RETURN, ec->cfp->self,
2431 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2432 rb_vm_frame_method_entry(ec->cfp)->called_id,
2433 rb_vm_frame_method_entry(ec->cfp)->owner,
2434 bmethod_return_value, TRUE);
2435 }
2436 THROW_DATA_CONSUMED_SET(err);
2437 }
2438 else {
2439 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_B_RETURN, ec->cfp->self, 0, 0, 0, frame_return_value(err));
2440 if (UNLIKELY(local_hooks && local_hooks->events & RUBY_EVENT_B_RETURN)) {
2441 rb_exec_event_hook_orig(ec, local_hooks, RUBY_EVENT_B_RETURN,
2442 ec->cfp->self, 0, 0, 0, frame_return_value(err), TRUE);
2443 }
2444 THROW_DATA_CONSUMED_SET(err);
2445 }
2446 break;
2447 case VM_FRAME_MAGIC_CLASS:
2448 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_END, ec->cfp->self, 0, 0, 0, Qnil);
2449 break;
2450 }
2451 }
2452}
2453
2454/* evaluator body */
2455
2456/* finish
2457 VMe (h1) finish
2458 VM finish F1 F2
2459 cfunc finish F1 F2 C1
2460 rb_funcall finish F1 F2 C1
2461 VMe finish F1 F2 C1
2462 VM finish F1 F2 C1 F3
2463
2464 F1 - F3 : pushed by VM
2465 C1 : pushed by send insn (CFUNC)
2466
2467 struct CONTROL_FRAME {
2468 VALUE *pc; // cfp[0], program counter
2469 VALUE *sp; // cfp[1], stack pointer
2470 rb_iseq_t *iseq; // cfp[2], iseq
2471 VALUE self; // cfp[3], self
2472 const VALUE *ep; // cfp[4], env pointer
2473 const void *block_code; // cfp[5], block code
2474 };
2475
2476 struct rb_captured_block {
2477 VALUE self;
2478 VALUE *ep;
2479 union code;
2480 };
2481
2482 struct METHOD_ENV {
2483 VALUE param0;
2484 ...
2485 VALUE paramN;
2486 VALUE lvar1;
2487 ...
2488 VALUE lvarM;
2489 VALUE cref; // ep[-2]
2490 VALUE special; // ep[-1]
2491 VALUE flags; // ep[ 0] == lep[0]
2492 };
2493
2494 struct BLOCK_ENV {
2495 VALUE block_param0;
2496 ...
2497 VALUE block_paramN;
2498 VALUE block_lvar1;
2499 ...
2500 VALUE block_lvarM;
2501 VALUE cref; // ep[-2]
2502 VALUE special; // ep[-1]
2503 VALUE flags; // ep[ 0]
2504 };
2505
2506 struct CLASS_ENV {
2507 VALUE class_lvar0;
2508 ...
2509 VALUE class_lvarN;
2510 VALUE cref;
2511 VALUE prev_ep; // for frame jump
2512 VALUE flags;
2513 };
2514
2515 struct C_METHOD_CONTROL_FRAME {
2516 VALUE *pc; // 0
2517 VALUE *sp; // stack pointer
2518 rb_iseq_t *iseq; // cmi
2519 VALUE self; // ?
2520 VALUE *ep; // ep == lep
2521 void *code; //
2522 };
2523
2524 struct C_BLOCK_CONTROL_FRAME {
2525 VALUE *pc; // point only "finish" insn
2526 VALUE *sp; // sp
2527 rb_iseq_t *iseq; // ?
2528 VALUE self; //
2529 VALUE *ep; // ep
2530 void *code; //
2531 };
2532 */
2533
2534static inline VALUE
2535vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state, VALUE errinfo);
2536static inline VALUE
2537vm_exec_loop(rb_execution_context_t *ec, enum ruby_tag_type state, struct rb_vm_tag *tag, VALUE result);
2538
2539// for non-Emscripten Wasm build, use vm_exec with optimized setjmp for runtime performance
2540#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2541
2542struct rb_vm_exec_context {
2543 rb_execution_context_t *const ec;
2544 struct rb_vm_tag *const tag;
2545
2546 VALUE result;
2547};
2548
2549static void
2550vm_exec_bottom_main(void *context)
2551{
2552 struct rb_vm_exec_context *ctx = context;
2553 rb_execution_context_t *ec = ctx->ec;
2554
2555 ctx->result = vm_exec_loop(ec, TAG_NONE, ctx->tag, vm_exec_core(ec));
2556}
2557
2558static void
2559vm_exec_bottom_rescue(void *context)
2560{
2561 struct rb_vm_exec_context *ctx = context;
2562 rb_execution_context_t *ec = ctx->ec;
2563
2564 ctx->result = vm_exec_loop(ec, rb_ec_tag_state(ec), ctx->tag, ec->errinfo);
2565}
2566#endif
2567
2568 VALUE
2569vm_exec(rb_execution_context_t *ec)
2570{
2571 VALUE result = Qundef;
2572
2573 EC_PUSH_TAG(ec);
2574
2575 _tag.retval = Qnil;
2576
2577#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
2578 struct rb_vm_exec_context ctx = {
2579 .ec = ec,
2580 .tag = &_tag,
2581 };
2582 struct rb_wasm_try_catch try_catch;
2583
2584 EC_REPUSH_TAG();
2585
2586 rb_wasm_try_catch_init(&try_catch, vm_exec_bottom_main, vm_exec_bottom_rescue, &ctx);
2587
2588 rb_wasm_try_catch_loop_run(&try_catch, &RB_VM_TAG_JMPBUF_GET(_tag.buf));
2589
2590 result = ctx.result;
2591#else
2592 enum ruby_tag_type state;
2593 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
2594 if (UNDEF_P(result = jit_exec(ec))) {
2595 result = vm_exec_core(ec);
2596 }
2597 /* fallback to the VM */
2598 result = vm_exec_loop(ec, TAG_NONE, &_tag, result);
2599 }
2600 else {
2601 result = vm_exec_loop(ec, state, &_tag, ec->errinfo);
2602 }
2603#endif
2604
2605 EC_POP_TAG();
2606 return result;
2607}
2608
2609static inline VALUE
2610vm_exec_loop(rb_execution_context_t *ec, enum ruby_tag_type state,
2611 struct rb_vm_tag *tag, VALUE result)
2612{
2613 if (state == TAG_NONE) { /* no jumps, result is discarded */
2614 goto vm_loop_start;
2615 }
2616
2617 rb_ec_raised_reset(ec, RAISED_STACKOVERFLOW | RAISED_NOMEMORY);
2618 while (UNDEF_P(result = vm_exec_handle_exception(ec, state, result))) {
2619 // caught a jump, exec the handler. JIT code in jit_exec_exception()
2620 // may return Qundef to run remaining frames with vm_exec_core().
2621 if (UNDEF_P(result = jit_exec_exception(ec))) {
2622 result = vm_exec_core(ec);
2623 }
2624 vm_loop_start:
2625 VM_ASSERT(ec->tag == tag);
2626 /* when caught `throw`, `tag.state` is set. */
2627 if ((state = tag->state) == TAG_NONE) break;
2628 tag->state = TAG_NONE;
2629 }
2630
2631 return result;
2632}
2633
2634static inline VALUE
2635vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state, VALUE errinfo)
2636{
2637 struct vm_throw_data *err = (struct vm_throw_data *)errinfo;
2638
2639 for (;;) {
2640 unsigned int i;
2641 const struct iseq_catch_table_entry *entry;
2642 const struct iseq_catch_table *ct;
2643 unsigned long epc, cont_pc, cont_sp;
2644 const rb_iseq_t *catch_iseq;
2645 VALUE type;
2646 const rb_control_frame_t *escape_cfp;
2647
2648 cont_pc = cont_sp = 0;
2649 catch_iseq = NULL;
2650
2651 while (ec->cfp->pc == 0 || ec->cfp->iseq == 0) {
2652 if (UNLIKELY(VM_FRAME_TYPE(ec->cfp) == VM_FRAME_MAGIC_CFUNC)) {
2653 EXEC_EVENT_HOOK_AND_POP_FRAME(ec, RUBY_EVENT_C_RETURN, ec->cfp->self,
2654 rb_vm_frame_method_entry(ec->cfp)->def->original_id,
2655 rb_vm_frame_method_entry(ec->cfp)->called_id,
2656 rb_vm_frame_method_entry(ec->cfp)->owner, Qnil);
2657 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec,
2658 rb_vm_frame_method_entry(ec->cfp)->owner,
2659 rb_vm_frame_method_entry(ec->cfp)->def->original_id);
2660 }
2661 rb_vm_pop_frame(ec);
2662 }
2663
2664 rb_control_frame_t *const cfp = ec->cfp;
2665 epc = cfp->pc - ISEQ_BODY(cfp->iseq)->iseq_encoded;
2666
2667 escape_cfp = NULL;
2668 if (state == TAG_BREAK || state == TAG_RETURN) {
2669 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2670
2671 if (cfp == escape_cfp) {
2672 if (state == TAG_RETURN) {
2673 if (!VM_FRAME_FINISHED_P(cfp)) {
2674 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2675 THROW_DATA_STATE_SET(err, state = TAG_BREAK);
2676 }
2677 else {
2678 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2679 if (ct) for (i = 0; i < ct->size; i++) {
2680 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2681 if (entry->start < epc && entry->end >= epc) {
2682 if (entry->type == CATCH_TYPE_ENSURE) {
2683 catch_iseq = entry->iseq;
2684 cont_pc = entry->cont;
2685 cont_sp = entry->sp;
2686 break;
2687 }
2688 }
2689 }
2690 if (catch_iseq == NULL) {
2691 ec->errinfo = Qnil;
2692 THROW_DATA_CATCH_FRAME_SET(err, cfp + 1);
2693 // cfp == escape_cfp here so calling with cfp_returning_with_value = true
2694 hook_before_rewind(ec, true, state, err);
2695 rb_vm_pop_frame(ec);
2696 return THROW_DATA_VAL(err);
2697 }
2698 }
2699 /* through */
2700 }
2701 else {
2702 /* TAG_BREAK */
2703 *cfp->sp++ = THROW_DATA_VAL(err);
2704 ec->errinfo = Qnil;
2705 return Qundef;
2706 }
2707 }
2708 }
2709
2710 if (state == TAG_RAISE) {
2711 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2712 if (ct) for (i = 0; i < ct->size; i++) {
2713 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2714 if (entry->start < epc && entry->end >= epc) {
2715
2716 if (entry->type == CATCH_TYPE_RESCUE ||
2717 entry->type == CATCH_TYPE_ENSURE) {
2718 catch_iseq = entry->iseq;
2719 cont_pc = entry->cont;
2720 cont_sp = entry->sp;
2721 break;
2722 }
2723 }
2724 }
2725 }
2726 else if (state == TAG_RETRY) {
2727 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2728 if (ct) for (i = 0; i < ct->size; i++) {
2729 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2730 if (entry->start < epc && entry->end >= epc) {
2731
2732 if (entry->type == CATCH_TYPE_ENSURE) {
2733 catch_iseq = entry->iseq;
2734 cont_pc = entry->cont;
2735 cont_sp = entry->sp;
2736 break;
2737 }
2738 else if (entry->type == CATCH_TYPE_RETRY) {
2739 const rb_control_frame_t *escape_cfp;
2740 escape_cfp = THROW_DATA_CATCH_FRAME(err);
2741 if (cfp == escape_cfp) {
2742 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2743 ec->errinfo = Qnil;
2744 return Qundef;
2745 }
2746 }
2747 }
2748 }
2749 }
2750 else if ((state == TAG_BREAK && !escape_cfp) ||
2751 (state == TAG_REDO) ||
2752 (state == TAG_NEXT)) {
2753 type = (const enum rb_catch_type[TAG_MASK]) {
2754 [TAG_BREAK] = CATCH_TYPE_BREAK,
2755 [TAG_NEXT] = CATCH_TYPE_NEXT,
2756 [TAG_REDO] = CATCH_TYPE_REDO,
2757 /* otherwise = dontcare */
2758 }[state];
2759
2760 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2761 if (ct) for (i = 0; i < ct->size; i++) {
2762 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2763
2764 if (entry->start < epc && entry->end >= epc) {
2765 if (entry->type == CATCH_TYPE_ENSURE) {
2766 catch_iseq = entry->iseq;
2767 cont_pc = entry->cont;
2768 cont_sp = entry->sp;
2769 break;
2770 }
2771 else if (entry->type == type) {
2772 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + entry->cont;
2773 cfp->sp = vm_base_ptr(cfp) + entry->sp;
2774
2775 if (state != TAG_REDO) {
2776 *cfp->sp++ = THROW_DATA_VAL(err);
2777 }
2778 ec->errinfo = Qnil;
2779 VM_ASSERT(ec->tag->state == TAG_NONE);
2780 return Qundef;
2781 }
2782 }
2783 }
2784 }
2785 else {
2786 ct = ISEQ_BODY(cfp->iseq)->catch_table;
2787 if (ct) for (i = 0; i < ct->size; i++) {
2788 entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
2789 if (entry->start < epc && entry->end >= epc) {
2790
2791 if (entry->type == CATCH_TYPE_ENSURE) {
2792 catch_iseq = entry->iseq;
2793 cont_pc = entry->cont;
2794 cont_sp = entry->sp;
2795 break;
2796 }
2797 }
2798 }
2799 }
2800
2801 if (catch_iseq != NULL) { /* found catch table */
2802 /* enter catch scope */
2803 const int arg_size = 1;
2804
2805 rb_iseq_check(catch_iseq);
2806 cfp->sp = vm_base_ptr(cfp) + cont_sp;
2807 cfp->pc = ISEQ_BODY(cfp->iseq)->iseq_encoded + cont_pc;
2808
2809 /* push block frame */
2810 cfp->sp[0] = (VALUE)err;
2811 vm_push_frame(ec, catch_iseq, VM_FRAME_MAGIC_RESCUE,
2812 cfp->self,
2813 VM_GUARDED_PREV_EP(cfp->ep),
2814 0, /* cref or me */
2815 ISEQ_BODY(catch_iseq)->iseq_encoded,
2816 cfp->sp + arg_size /* push value */,
2817 ISEQ_BODY(catch_iseq)->local_table_size - arg_size,
2818 ISEQ_BODY(catch_iseq)->stack_max);
2819
2820 state = 0;
2821 ec->tag->state = TAG_NONE;
2822 ec->errinfo = Qnil;
2823
2824 return Qundef;
2825 }
2826 else {
2827 hook_before_rewind(ec, (cfp == escape_cfp), state, err);
2828
2829 if (VM_FRAME_FINISHED_P(ec->cfp)) {
2830 rb_vm_pop_frame(ec);
2831 ec->errinfo = (VALUE)err;
2832 ec->tag = ec->tag->prev;
2833 EC_JUMP_TAG(ec, state);
2834 }
2835 else {
2836 rb_vm_pop_frame(ec);
2837 }
2838 }
2839 }
2840}
2841
2842/* misc */
2843
2844 VALUE
2845rb_iseq_eval(const rb_iseq_t *iseq)
2846{
2847 rb_execution_context_t *ec = GET_EC();
2848 VALUE val;
2849 vm_set_top_stack(ec, iseq);
2850 val = vm_exec(ec);
2851 return val;
2852}
2853
2854 VALUE
2855rb_iseq_eval_main(const rb_iseq_t *iseq)
2856{
2857 rb_execution_context_t *ec = GET_EC();
2858 VALUE val;
2859
2860 vm_set_main_stack(ec, iseq);
2861 val = vm_exec(ec);
2862 return val;
2863}
2864
2865int
2866rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp)
2867{
2868 const rb_callable_method_entry_t *me = rb_vm_frame_method_entry(cfp);
2869
2870 if (me) {
2871 if (idp) *idp = me->def->original_id;
2872 if (called_idp) *called_idp = me->called_id;
2873 if (klassp) *klassp = me->owner;
2874 return TRUE;
2875 }
2876 else {
2877 return FALSE;
2878 }
2879}
2880
2881int
2882rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp)
2883{
2884 return rb_vm_control_frame_id_and_class(ec->cfp, idp, called_idp, klassp);
2885}
2886
2887int
2888rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
2889{
2890 return rb_ec_frame_method_id_and_class(GET_EC(), idp, 0, klassp);
2891}
2892
2893 VALUE
2894rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg,
2895 VALUE block_handler, VALUE filename)
2896{
2897 rb_execution_context_t *ec = GET_EC();
2898 const rb_control_frame_t *reg_cfp = ec->cfp;
2899 const rb_iseq_t *iseq = rb_iseq_new(Qnil, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
2900 VALUE val;
2901
2902 vm_push_frame(ec, iseq, VM_FRAME_MAGIC_TOP | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH,
2903 recv, block_handler,
2904 (VALUE)vm_cref_new_toplevel(ec), /* cref or me */
2905 0, reg_cfp->sp, 0, 0);
2906
2907 val = (*func)(arg);
2908
2909 rb_vm_pop_frame(ec);
2910 return val;
2911}
2912
2913/* vm */
2914
2915void
2916rb_vm_update_references(void *ptr)
2917{
2918 if (ptr) {
2919 rb_vm_t *vm = ptr;
2920
2921 rb_gc_update_tbl_refs(vm->ci_table);
2922 rb_gc_update_tbl_refs(vm->frozen_strings);
2923 vm->mark_object_ary = rb_gc_location(vm->mark_object_ary);
2924 vm->load_path = rb_gc_location(vm->load_path);
2925 vm->load_path_snapshot = rb_gc_location(vm->load_path_snapshot);
2926
2927 if (vm->load_path_check_cache) {
2928 vm->load_path_check_cache = rb_gc_location(vm->load_path_check_cache);
2929 }
2930
2931 vm->expanded_load_path = rb_gc_location(vm->expanded_load_path);
2932 vm->loaded_features = rb_gc_location(vm->loaded_features);
2933 vm->loaded_features_snapshot = rb_gc_location(vm->loaded_features_snapshot);
2934 vm->loaded_features_realpaths = rb_gc_location(vm->loaded_features_realpaths);
2935 vm->loaded_features_realpath_map = rb_gc_location(vm->loaded_features_realpath_map);
2936 vm->top_self = rb_gc_location(vm->top_self);
2937 vm->orig_progname = rb_gc_location(vm->orig_progname);
2938
2939 rb_gc_update_tbl_refs(vm->overloaded_cme_table);
2940
2941 rb_gc_update_values(RUBY_NSIG, vm->trap_list.cmd);
2942
2943 if (vm->coverages) {
2944 vm->coverages = rb_gc_location(vm->coverages);
2945 vm->me2counter = rb_gc_location(vm->me2counter);
2946 }
2947 }
2948}
2949
2950void
2951rb_vm_each_stack_value(void *ptr, void (*cb)(VALUE, void*), void *ctx)
2952{
2953 if (ptr) {
2954 rb_vm_t *vm = ptr;
2955 rb_ractor_t *r = 0;
2956 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2957 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
2958 rb_ractor_status_p(r, ractor_running));
2959 if (r->threads.cnt > 0) {
2960 rb_thread_t *th = 0;
2961 ccan_list_for_each(&r->threads.set, th, lt_node) {
2962 VM_ASSERT(th != NULL);
2963 rb_execution_context_t * ec = th->ec;
2964 if (ec->vm_stack) {
2965 VALUE *p = ec->vm_stack;
2966 VALUE *sp = ec->cfp->sp;
2967 while (p < sp) {
2968 if (!RB_SPECIAL_CONST_P(*p)) {
2969 cb(*p, ctx);
2970 }
2971 p++;
2972 }
2973 }
2974 }
2975 }
2976 }
2977 }
2978}
2979
2980static enum rb_id_table_iterator_result
2981vm_mark_negative_cme(VALUE val, void *dmy)
2982{
2983 rb_gc_mark(val);
2984 return ID_TABLE_CONTINUE;
2985}
2986
2987void rb_thread_sched_mark_zombies(rb_vm_t *vm);
2988
2989void
2990rb_vm_mark(void *ptr)
2991{
2992 RUBY_MARK_ENTER("vm");
2993 RUBY_GC_INFO("-------------------------------------------------\n");
2994 if (ptr) {
2995 rb_vm_t *vm = ptr;
2996 rb_ractor_t *r = 0;
2997 long i;
2998
2999 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
3000 // ractor.set only contains blocking or running ractors
3001 VM_ASSERT(rb_ractor_status_p(r, ractor_blocking) ||
3002 rb_ractor_status_p(r, ractor_running));
3003 rb_gc_mark(rb_ractor_self(r));
3004 }
3005
3006 for (struct global_object_list *list = vm->global_object_list; list; list = list->next) {
3007 rb_gc_mark_maybe(*list->varptr);
3008 }
3009
3010 rb_gc_mark_movable(vm->mark_object_ary);
3011 rb_gc_mark_movable(vm->load_path);
3012 rb_gc_mark_movable(vm->load_path_snapshot);
3013 rb_gc_mark_movable(vm->load_path_check_cache);
3014 rb_gc_mark_movable(vm->expanded_load_path);
3015 rb_gc_mark_movable(vm->loaded_features);
3016 rb_gc_mark_movable(vm->loaded_features_snapshot);
3017 rb_gc_mark_movable(vm->loaded_features_realpaths);
3018 rb_gc_mark_movable(vm->loaded_features_realpath_map);
3019 rb_gc_mark_movable(vm->top_self);
3020 rb_gc_mark_movable(vm->orig_progname);
3021 rb_gc_mark_movable(vm->coverages);
3022 rb_gc_mark_movable(vm->me2counter);
3023
3024 if (vm->loading_table) {
3025 rb_mark_tbl(vm->loading_table);
3026 }
3027
3028 rb_gc_mark_values(RUBY_NSIG, vm->trap_list.cmd);
3029
3030 rb_id_table_foreach_values(vm->negative_cme_table, vm_mark_negative_cme, NULL);
3031 rb_mark_tbl_no_pin(vm->overloaded_cme_table);
3032 for (i=0; i<VM_GLOBAL_CC_CACHE_TABLE_SIZE; i++) {
3033 const struct rb_callcache *cc = vm->global_cc_cache_table[i];
3034
3035 if (cc != NULL) {
3036 if (!vm_cc_invalidated_p(cc)) {
3037 rb_gc_mark((VALUE)cc);
3038 }
3039 else {
3040 vm->global_cc_cache_table[i] = NULL;
3041 }
3042 }
3043 }
3044
3045 rb_thread_sched_mark_zombies(vm);
3046 rb_rjit_mark();
3047 }
3048
3049 RUBY_MARK_LEAVE("vm");
3050}
3051
3052#undef rb_vm_register_special_exception
3053void
3054rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE cls, VALUE mesg)
3055{
3056 rb_vm_t *vm = GET_VM();
3057 VALUE exc = rb_exc_new3(cls, rb_obj_freeze(mesg));
3058 OBJ_FREEZE(exc);
3059 ((VALUE *)vm->special_exceptions)[sp] = exc;
3060 rb_vm_register_global_object(exc);
3061}
3062
3063static int
3064free_loading_table_entry(st_data_t key, st_data_t value, st_data_t arg)
3065{
3066 xfree((char *)key);
3067 return ST_DELETE;
3068}
3069
3070void rb_free_loaded_features_index(rb_vm_t *vm);
3071void rb_objspace_free_objects(void *objspace);
3072
3073int
3074ruby_vm_destruct(rb_vm_t *vm)
3075{
3076 RUBY_FREE_ENTER("vm");
3077
3078 if (vm) {
3079 rb_thread_t *th = vm->ractor.main_thread;
3080 VALUE *stack = th->ec->vm_stack;
3081 if (rb_free_at_exit) {
3082 rb_free_encoded_insn_data();
3083 rb_free_global_enc_table();
3084 rb_free_loaded_builtin_table();
3085
3086 rb_free_shared_fiber_pool();
3087 rb_free_static_symid_str();
3088 rb_free_transcoder_table();
3089 rb_free_vm_opt_tables();
3090 rb_free_warning();
3091 rb_free_rb_global_tbl();
3092 rb_free_loaded_features_index(vm);
3093
3094 rb_id_table_free(vm->negative_cme_table);
3095 st_free_table(vm->overloaded_cme_table);
3096
3097 rb_id_table_free(RCLASS(rb_mRubyVMFrozenCore)->m_tbl);
3098
3099 rb_shape_t *cursor = rb_shape_get_root_shape();
3100 rb_shape_t *end = rb_shape_get_shape_by_id(GET_SHAPE_TREE()->next_shape_id);
3101 while (cursor < end) {
3102 // 0x1 == SINGLE_CHILD_P
3103 if (cursor->edges && !(((uintptr_t)cursor->edges) & 0x1))
3104 rb_id_table_free(cursor->edges);
3105 cursor += 1;
3106 }
3107
3108 xfree(GET_SHAPE_TREE());
3109
3110 st_free_table(vm->static_ext_inits);
3111
3112 rb_vm_postponed_job_free();
3113
3114 rb_id_table_free(vm->constant_cache);
3115 st_free_table(vm->unused_block_warning_table);
3116
3117 xfree(th->nt);
3118 th->nt = NULL;
3119
3120#ifndef HAVE_SETPROCTITLE
3121 ruby_free_proctitle();
3122#endif
3123 }
3124 else {
3125 rb_fiber_reset_root_local_storage(th);
3126 thread_free(th);
3127 }
3128
3129 struct rb_objspace *objspace = vm->gc.objspace;
3130
3131 rb_vm_living_threads_init(vm);
3132 ruby_vm_run_at_exit_hooks(vm);
3133 if (vm->loading_table) {
3134 st_foreach(vm->loading_table, free_loading_table_entry, 0);
3135 st_free_table(vm->loading_table);
3136 vm->loading_table = 0;
3137 }
3138 if (vm->ci_table) {
3139 st_free_table(vm->ci_table);
3140 vm->ci_table = NULL;
3141 }
3142 if (vm->frozen_strings) {
3143 st_free_table(vm->frozen_strings);
3144 vm->frozen_strings = 0;
3145 }
3146 RB_ALTSTACK_FREE(vm->main_altstack);
3147
3148 struct global_object_list *next;
3149 for (struct global_object_list *list = vm->global_object_list; list; list = next) {
3150 next = list->next;
3151 xfree(list);
3152 }
3153
3154 if (objspace) {
3155 if (rb_free_at_exit) {
3156 rb_objspace_free_objects(objspace);
3157 rb_free_generic_iv_tbl_();
3158 rb_free_default_rand_key();
3159 if (th && vm->fork_gen == 0) {
3160 /* If we have forked, main_thread may not be the initial thread */
3161 xfree(stack);
3162 ruby_mimfree(th);
3163 }
3164 }
3165 rb_objspace_free(objspace);
3166 }
3167 rb_native_mutex_destroy(&vm->workqueue_lock);
3168 /* after freeing objspace, you *can't* use ruby_xfree() */
3169 ruby_mimfree(vm);
3170 ruby_current_vm_ptr = NULL;
3171
3172#if USE_YJIT
3173 if (rb_free_at_exit) {
3174 rb_yjit_free_at_exit();
3175 }
3176#endif
3177 }
3178 RUBY_FREE_LEAVE("vm");
3179 return 0;
3180}
3181
3182size_t rb_vm_memsize_waiting_fds(struct ccan_list_head *waiting_fds); // thread.c
3183size_t rb_vm_memsize_workqueue(struct ccan_list_head *workqueue); // vm_trace.c
3184
3185// Used for VM memsize reporting. Returns the size of the at_exit list by
3186// looping through the linked list and adding up the size of the structs.
3187static enum rb_id_table_iterator_result
3188vm_memsize_constant_cache_i(ID id, VALUE ics, void *size)
3189{
3190 *((size_t *) size) += rb_st_memsize((st_table *) ics);
3191 return ID_TABLE_CONTINUE;
3192}
3193
3194// Returns a size_t representing the memory footprint of the VM's constant
3195// cache, which is the memsize of the table as well as the memsize of all of the
3196// nested tables.
3197static size_t
3198vm_memsize_constant_cache(void)
3199{
3200 rb_vm_t *vm = GET_VM();
3201 size_t size = rb_id_table_memsize(vm->constant_cache);
3202
3203 rb_id_table_foreach(vm->constant_cache, vm_memsize_constant_cache_i, &size);
3204 return size;
3205}
3206
3207static size_t
3208vm_memsize_at_exit_list(rb_at_exit_list *at_exit)
3209{
3210 size_t size = 0;
3211
3212 while (at_exit) {
3213 size += sizeof(rb_at_exit_list);
3214 at_exit = at_exit->next;
3215 }
3216
3217 return size;
3218}
3219
3220// Used for VM memsize reporting. Returns the size of the builtin function
3221// table if it has been defined.
3222static size_t
3223vm_memsize_builtin_function_table(const struct rb_builtin_function *builtin_function_table)
3224{
3225 return builtin_function_table == NULL ? 0 : sizeof(struct rb_builtin_function);
3226}
3227
3228// Reports the memsize of the VM struct object and the structs that are
3229// associated with it.
3230static size_t
3231vm_memsize(const void *ptr)
3232{
3233 rb_vm_t *vm = GET_VM();
3234
3235 return (
3236 sizeof(rb_vm_t) +
3237 rb_vm_memsize_waiting_fds(&vm->waiting_fds) +
3238 rb_st_memsize(vm->loaded_features_index) +
3239 rb_st_memsize(vm->loading_table) +
3240 rb_vm_memsize_postponed_job_queue() +
3241 rb_vm_memsize_workqueue(&vm->workqueue) +
3242 vm_memsize_at_exit_list(vm->at_exit) +
3243 rb_st_memsize(vm->ci_table) +
3244 rb_st_memsize(vm->frozen_strings) +
3245 vm_memsize_builtin_function_table(vm->builtin_function_table) +
3246 rb_id_table_memsize(vm->negative_cme_table) +
3247 rb_st_memsize(vm->overloaded_cme_table) +
3248 vm_memsize_constant_cache() +
3249 GET_SHAPE_TREE()->cache_size * sizeof(redblack_node_t)
3250 );
3251
3252 // TODO
3253 // struct { struct ccan_list_head set; } ractor;
3254 // void *main_altstack; #ifdef USE_SIGALTSTACK
3255 // struct rb_objspace *objspace;
3256}
3257
3258static const rb_data_type_t vm_data_type = {
3259 "VM",
3260 {0, 0, vm_memsize,},
3261 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3262};
3263
3264
3265static VALUE
3266vm_default_params(void)
3267{
3268 rb_vm_t *vm = GET_VM();
3269 VALUE result = rb_hash_new_with_size(4);
3270#define SET(name) rb_hash_aset(result, ID2SYM(rb_intern(#name)), SIZET2NUM(vm->default_params.name));
3271 SET(thread_vm_stack_size);
3272 SET(thread_machine_stack_size);
3273 SET(fiber_vm_stack_size);
3274 SET(fiber_machine_stack_size);
3275#undef SET
3276 rb_obj_freeze(result);
3277 return result;
3278}
3279
3280static size_t
3281get_param(const char *name, size_t default_value, size_t min_value)
3282{
3283 const char *envval;
3284 size_t result = default_value;
3285 if ((envval = getenv(name)) != 0) {
3286 long val = atol(envval);
3287 if (val < (long)min_value) {
3288 val = (long)min_value;
3289 }
3290 result = (size_t)(((val -1 + RUBY_VM_SIZE_ALIGN) / RUBY_VM_SIZE_ALIGN) * RUBY_VM_SIZE_ALIGN);
3291 }
3292 if (0) ruby_debug_printf("%s: %"PRIuSIZE"\n", name, result); /* debug print */
3293
3294 return result;
3295}
3296
3297static void
3298check_machine_stack_size(size_t *sizep)
3299{
3300#ifdef PTHREAD_STACK_MIN
3301 size_t size = *sizep;
3302#endif
3303
3304#ifdef PTHREAD_STACK_MIN
3305 if (size < (size_t)PTHREAD_STACK_MIN) {
3306 *sizep = (size_t)PTHREAD_STACK_MIN * 2;
3307 }
3308#endif
3309}
3310
3311static void
3312vm_default_params_setup(rb_vm_t *vm)
3313{
3314 vm->default_params.thread_vm_stack_size =
3315 get_param("RUBY_THREAD_VM_STACK_SIZE",
3316 RUBY_VM_THREAD_VM_STACK_SIZE,
3317 RUBY_VM_THREAD_VM_STACK_SIZE_MIN);
3318
3319 vm->default_params.thread_machine_stack_size =
3320 get_param("RUBY_THREAD_MACHINE_STACK_SIZE",
3321 RUBY_VM_THREAD_MACHINE_STACK_SIZE,
3322 RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN);
3323
3324 vm->default_params.fiber_vm_stack_size =
3325 get_param("RUBY_FIBER_VM_STACK_SIZE",
3326 RUBY_VM_FIBER_VM_STACK_SIZE,
3327 RUBY_VM_FIBER_VM_STACK_SIZE_MIN);
3328
3329 vm->default_params.fiber_machine_stack_size =
3330 get_param("RUBY_FIBER_MACHINE_STACK_SIZE",
3331 RUBY_VM_FIBER_MACHINE_STACK_SIZE,
3332 RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN);
3333
3334 /* environment dependent check */
3335 check_machine_stack_size(&vm->default_params.thread_machine_stack_size);
3336 check_machine_stack_size(&vm->default_params.fiber_machine_stack_size);
3337}
3338
3339static void
3340vm_init2(rb_vm_t *vm)
3341{
3342 rb_vm_living_threads_init(vm);
3343 vm->thread_report_on_exception = 1;
3344 vm->src_encoding_index = -1;
3345
3346 vm_default_params_setup(vm);
3347}
3348
3349void
3350rb_execution_context_update(rb_execution_context_t *ec)
3351{
3352 /* update VM stack */
3353 if (ec->vm_stack) {
3354 long i;
3355 VM_ASSERT(ec->cfp);
3356 VALUE *p = ec->vm_stack;
3357 VALUE *sp = ec->cfp->sp;
3358 rb_control_frame_t *cfp = ec->cfp;
3359 rb_control_frame_t *limit_cfp = (void *)(ec->vm_stack + ec->vm_stack_size);
3360
3361 for (i = 0; i < (long)(sp - p); i++) {
3362 VALUE ref = p[i];
3363 VALUE update = rb_gc_location(ref);
3364 if (ref != update) {
3365 p[i] = update;
3366 }
3367 }
3368
3369 while (cfp != limit_cfp) {
3370 if (VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
3371 const VALUE *ep = cfp->ep;
3372 cfp->self = rb_gc_location(cfp->self);
3373 cfp->iseq = (rb_iseq_t *)rb_gc_location((VALUE)cfp->iseq);
3374 cfp->block_code = (void *)rb_gc_location((VALUE)cfp->block_code);
3375
3376 if (!VM_ENV_LOCAL_P(ep)) {
3377 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3378 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3379 VM_FORCE_WRITE(&prev_ep[VM_ENV_DATA_INDEX_ENV], rb_gc_location(prev_ep[VM_ENV_DATA_INDEX_ENV]));
3380 }
3381
3382 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3383 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ENV], rb_gc_location(ep[VM_ENV_DATA_INDEX_ENV]));
3384 VM_FORCE_WRITE(&ep[VM_ENV_DATA_INDEX_ME_CREF], rb_gc_location(ep[VM_ENV_DATA_INDEX_ME_CREF]));
3385 }
3386 }
3387 }
3388
3389 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3390 }
3391 }
3392
3393 ec->storage = rb_gc_location(ec->storage);
3394}
3395
3396static enum rb_id_table_iterator_result
3397mark_local_storage_i(VALUE local, void *data)
3398{
3399 rb_gc_mark(local);
3400 return ID_TABLE_CONTINUE;
3401}
3402
3403void
3404rb_execution_context_mark(const rb_execution_context_t *ec)
3405{
3406 /* mark VM stack */
3407 if (ec->vm_stack) {
3408 VM_ASSERT(ec->cfp);
3409 VALUE *p = ec->vm_stack;
3410 VALUE *sp = ec->cfp->sp;
3411 rb_control_frame_t *cfp = ec->cfp;
3412 rb_control_frame_t *limit_cfp = (void *)(ec->vm_stack + ec->vm_stack_size);
3413
3414 VM_ASSERT(sp == ec->cfp->sp);
3415 rb_gc_mark_vm_stack_values((long)(sp - p), p);
3416
3417 while (cfp != limit_cfp) {
3418 const VALUE *ep = cfp->ep;
3419 VM_ASSERT(!!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) == vm_ep_in_heap_p_(ec, ep));
3420
3421 if (VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
3422 rb_gc_mark_movable(cfp->self);
3423 rb_gc_mark_movable((VALUE)cfp->iseq);
3424 rb_gc_mark_movable((VALUE)cfp->block_code);
3425
3426 if (!VM_ENV_LOCAL_P(ep)) {
3427 const VALUE *prev_ep = VM_ENV_PREV_EP(ep);
3428 if (VM_ENV_FLAGS(prev_ep, VM_ENV_FLAG_ESCAPED)) {
3429 rb_gc_mark_movable(prev_ep[VM_ENV_DATA_INDEX_ENV]);
3430 }
3431
3432 if (VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED)) {
3433 rb_gc_mark_movable(ep[VM_ENV_DATA_INDEX_ENV]);
3434 rb_gc_mark(ep[VM_ENV_DATA_INDEX_ME_CREF]);
3435 }
3436 }
3437 }
3438
3439 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
3440 }
3441 }
3442
3443 /* mark machine stack */
3444 if (ec->machine.stack_start && ec->machine.stack_end &&
3445 ec != GET_EC() /* marked for current ec at the first stage of marking */
3446 ) {
3447 rb_gc_mark_machine_context(ec);
3448 }
3449
3450 rb_gc_mark(ec->errinfo);
3451 rb_gc_mark(ec->root_svar);
3452 if (ec->local_storage) {
3453 rb_id_table_foreach_values(ec->local_storage, mark_local_storage_i, NULL);
3454 }
3455 rb_gc_mark(ec->local_storage_recursive_hash);
3456 rb_gc_mark(ec->local_storage_recursive_hash_for_trace);
3457 rb_gc_mark(ec->private_const_reference);
3458
3459 rb_gc_mark_movable(ec->storage);
3460}
3461
3462void rb_fiber_mark_self(rb_fiber_t *fib);
3463void rb_fiber_update_self(rb_fiber_t *fib);
3464void rb_threadptr_root_fiber_setup(rb_thread_t *th);
3465void rb_threadptr_root_fiber_release(rb_thread_t *th);
3466
3467static void
3468thread_compact(void *ptr)
3469{
3470 rb_thread_t *th = ptr;
3471
3472 th->self = rb_gc_location(th->self);
3473
3474 if (!th->root_fiber) {
3475 rb_execution_context_update(th->ec);
3476 }
3477}
3478
3479static void
3480thread_mark(void *ptr)
3481{
3482 rb_thread_t *th = ptr;
3483 RUBY_MARK_ENTER("thread");
3484 rb_fiber_mark_self(th->ec->fiber_ptr);
3485
3486 /* mark ruby objects */
3487 switch (th->invoke_type) {
3488 case thread_invoke_type_proc:
3489 case thread_invoke_type_ractor_proc:
3490 rb_gc_mark(th->invoke_arg.proc.proc);
3491 rb_gc_mark(th->invoke_arg.proc.args);
3492 break;
3493 case thread_invoke_type_func:
3494 rb_gc_mark_maybe((VALUE)th->invoke_arg.func.arg);
3495 break;
3496 default:
3497 break;
3498 }
3499
3500 rb_gc_mark(rb_ractor_self(th->ractor));
3501 rb_gc_mark(th->thgroup);
3502 rb_gc_mark(th->value);
3503 rb_gc_mark(th->pending_interrupt_queue);
3504 rb_gc_mark(th->pending_interrupt_mask_stack);
3505 rb_gc_mark(th->top_self);
3506 rb_gc_mark(th->top_wrapper);
3507 if (th->root_fiber) rb_fiber_mark_self(th->root_fiber);
3508
3509 RUBY_ASSERT(th->ec == rb_fiberptr_get_ec(th->ec->fiber_ptr));
3510 rb_gc_mark(th->stat_insn_usage);
3511 rb_gc_mark(th->last_status);
3512 rb_gc_mark(th->locking_mutex);
3513 rb_gc_mark(th->name);
3514
3515 rb_gc_mark(th->scheduler);
3516
3517 rb_threadptr_interrupt_exec_task_mark(th);
3518
3519 RUBY_MARK_LEAVE("thread");
3520}
3521
3522void rb_threadptr_sched_free(rb_thread_t *th); // thread_*.c
3523
3524static void
3525thread_free(void *ptr)
3526{
3527 rb_thread_t *th = ptr;
3528 RUBY_FREE_ENTER("thread");
3529
3530 rb_threadptr_sched_free(th);
3531
3532 if (th->locking_mutex != Qfalse) {
3533 rb_bug("thread_free: locking_mutex must be NULL (%p:%p)", (void *)th, (void *)th->locking_mutex);
3534 }
3535 if (th->keeping_mutexes != NULL) {
3536 rb_bug("thread_free: keeping_mutexes must be NULL (%p:%p)", (void *)th, (void *)th->keeping_mutexes);
3537 }
3538
3539 ruby_xfree(th->specific_storage);
3540
3541 rb_threadptr_root_fiber_release(th);
3542
3543 if (th->vm && th->vm->ractor.main_thread == th) {
3544 RUBY_GC_INFO("MRI main thread\n");
3545 }
3546 else {
3547 // ruby_xfree(th->nt);
3548 // TODO: MN system collect nt, but without MN system it should be freed here.
3549 ruby_xfree(th);
3550 }
3551
3552 RUBY_FREE_LEAVE("thread");
3553}
3554
3555static size_t
3556thread_memsize(const void *ptr)
3557{
3558 const rb_thread_t *th = ptr;
3559 size_t size = sizeof(rb_thread_t);
3560
3561 if (!th->root_fiber) {
3562 size += th->ec->vm_stack_size * sizeof(VALUE);
3563 }
3564 if (th->ec->local_storage) {
3565 size += rb_id_table_memsize(th->ec->local_storage);
3566 }
3567 return size;
3568}
3569
3570#define thread_data_type ruby_threadptr_data_type
3571const rb_data_type_t ruby_threadptr_data_type = {
3572 "VM/thread",
3573 {
3574 thread_mark,
3575 thread_free,
3576 thread_memsize,
3577 thread_compact,
3578 },
3579 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3580};
3581
3582 VALUE
3583rb_obj_is_thread(VALUE obj)
3584{
3585 return RBOOL(rb_typeddata_is_kind_of(obj, &thread_data_type));
3586}
3587
3588static VALUE
3589thread_alloc(VALUE klass)
3590{
3591 rb_thread_t *th;
3592 return TypedData_Make_Struct(klass, rb_thread_t, &thread_data_type, th);
3593}
3594
3595void
3596rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
3597{
3598 ec->vm_stack = stack;
3599 ec->vm_stack_size = size;
3600}
3601
3602void
3603rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
3604{
3605 rb_ec_set_vm_stack(ec, stack, size);
3606
3607#if VM_CHECK_MODE > 0
3608 MEMZERO(stack, VALUE, size); // malloc memory could have the VM canary in it
3609#endif
3610
3611 ec->cfp = (void *)(ec->vm_stack + ec->vm_stack_size);
3612
3613 vm_push_frame(ec,
3614 NULL /* dummy iseq */,
3615 VM_FRAME_MAGIC_DUMMY | VM_ENV_FLAG_LOCAL | VM_FRAME_FLAG_FINISH | VM_FRAME_FLAG_CFRAME /* dummy frame */,
3616 Qnil /* dummy self */, VM_BLOCK_HANDLER_NONE /* dummy block ptr */,
3617 0 /* dummy cref/me */,
3618 0 /* dummy pc */, ec->vm_stack, 0, 0
3619 );
3620}
3621
3622void
3623rb_ec_clear_vm_stack(rb_execution_context_t *ec)
3624{
3625 rb_ec_set_vm_stack(ec, NULL, 0);
3626
3627 // Avoid dangling pointers:
3628 ec->cfp = NULL;
3629}
3630
3631static void
3632th_init(rb_thread_t *th, VALUE self, rb_vm_t *vm)
3633{
3634 th->self = self;
3635
3636 rb_threadptr_root_fiber_setup(th);
3637
3638 /* All threads are blocking until a non-blocking fiber is scheduled */
3639 th->blocking = 1;
3640 th->scheduler = Qnil;
3641
3642 if (self == 0) {
3643 size_t size = vm->default_params.thread_vm_stack_size / sizeof(VALUE);
3644 rb_ec_initialize_vm_stack(th->ec, ALLOC_N(VALUE, size), size);
3645 }
3646 else {
3647 VM_ASSERT(th->ec->cfp == NULL);
3648 VM_ASSERT(th->ec->vm_stack == NULL);
3649 VM_ASSERT(th->ec->vm_stack_size == 0);
3650 }
3651
3652 th->status = THREAD_RUNNABLE;
3653 th->last_status = Qnil;
3654 th->top_wrapper = 0;
3655 th->top_self = vm->top_self; // 0 while self == 0
3656 th->value = Qundef;
3657
3658 th->ec->errinfo = Qnil;
3659 th->ec->root_svar = Qfalse;
3660 th->ec->local_storage_recursive_hash = Qnil;
3661 th->ec->local_storage_recursive_hash_for_trace = Qnil;
3662
3663 th->ec->storage = Qnil;
3664
3665#if OPT_CALL_THREADED_CODE
3666 th->retval = Qundef;
3667#endif
3668 th->name = Qnil;
3669 th->report_on_exception = vm->thread_report_on_exception;
3670 th->ext_config.ractor_safe = true;
3671
3672 ccan_list_head_init(&th->interrupt_exec_tasks);
3673
3674#if USE_RUBY_DEBUG_LOG
3675 static rb_atomic_t thread_serial = 1;
3676 th->serial = RUBY_ATOMIC_FETCH_ADD(thread_serial, 1);
3677
3678 RUBY_DEBUG_LOG("th:%u", th->serial);
3679#endif
3680}
3681
3682 VALUE
3683rb_thread_alloc(VALUE klass)
3684{
3685 VALUE self = thread_alloc(klass);
3686 rb_thread_t *target_th = rb_thread_ptr(self);
3687 target_th->ractor = GET_RACTOR();
3688 th_init(target_th, self, target_th->vm = GET_VM());
3689 return self;
3690}
3691
3692#define REWIND_CFP(expr) do { \
3693 rb_execution_context_t *ec__ = GET_EC(); \
3694 VALUE *const curr_sp = (ec__->cfp++)->sp; \
3695 VALUE *const saved_sp = ec__->cfp->sp; \
3696 ec__->cfp->sp = curr_sp; \
3697 expr; \
3698 (ec__->cfp--)->sp = saved_sp; \
3699} while (0)
3700
3701static VALUE
3702m_core_set_method_alias(VALUE self, VALUE cbase, VALUE sym1, VALUE sym2)
3703{
3704 REWIND_CFP({
3705 rb_alias(cbase, SYM2ID(sym1), SYM2ID(sym2));
3706 });
3707 return Qnil;
3708}
3709
3710static VALUE
3711m_core_set_variable_alias(VALUE self, VALUE sym1, VALUE sym2)
3712{
3713 REWIND_CFP({
3714 rb_alias_variable(SYM2ID(sym1), SYM2ID(sym2));
3715 });
3716 return Qnil;
3717}
3718
3719static VALUE
3720m_core_undef_method(VALUE self, VALUE cbase, VALUE sym)
3721{
3722 REWIND_CFP({
3723 ID mid = SYM2ID(sym);
3724 rb_undef(cbase, mid);
3725 rb_clear_method_cache(self, mid);
3726 });
3727 return Qnil;
3728}
3729
3730static VALUE
3731m_core_set_postexe(VALUE self)
3732{
3733 rb_set_end_proc(rb_call_end_proc, rb_block_proc());
3734 return Qnil;
3735}
3736
3737static VALUE core_hash_merge_kwd(VALUE hash, VALUE kw);
3738
3739static VALUE
3740core_hash_merge(VALUE hash, long argc, const VALUE *argv)
3741{
3742 Check_Type(hash, T_HASH);
3743 VM_ASSERT(argc % 2 == 0);
3744 rb_hash_bulk_insert(argc, argv, hash);
3745 return hash;
3746}
3747
3748static VALUE
3749m_core_hash_merge_ptr(int argc, VALUE *argv, VALUE recv)
3750{
3751 VALUE hash = argv[0];
3752
3753 REWIND_CFP(hash = core_hash_merge(hash, argc-1, argv+1));
3754
3755 return hash;
3756}
3757
3758static int
3759kwmerge_i(VALUE key, VALUE value, VALUE hash)
3760{
3761 rb_hash_aset(hash, key, value);
3762 return ST_CONTINUE;
3763}
3764
3765static VALUE
3766m_core_hash_merge_kwd(VALUE recv, VALUE hash, VALUE kw)
3767{
3768 if (!NIL_P(kw)) {
3769 REWIND_CFP(hash = core_hash_merge_kwd(hash, kw));
3770 }
3771 return hash;
3772}
3773
3774static VALUE
3775m_core_make_shareable(VALUE recv, VALUE obj)
3776{
3777 return rb_ractor_make_shareable(obj);
3778}
3779
3780static VALUE
3781m_core_make_shareable_copy(VALUE recv, VALUE obj)
3782{
3783 return rb_ractor_make_shareable_copy(obj);
3784}
3785
3786static VALUE
3787m_core_ensure_shareable(VALUE recv, VALUE obj, VALUE name)
3788{
3789 return rb_ractor_ensure_shareable(obj, name);
3790}
3791
3792static VALUE
3793core_hash_merge_kwd(VALUE hash, VALUE kw)
3794{
3795 rb_hash_foreach(rb_to_hash_type(kw), kwmerge_i, hash);
3796 return hash;
3797}
3798
3799extern VALUE *rb_gc_stack_start;
3800extern size_t rb_gc_stack_maxsize;
3801
3802/* debug functions */
3803
3804/* :nodoc: */
3805static VALUE
3806sdr(VALUE self)
3807{
3808 rb_vm_bugreport(NULL, stderr);
3809 return Qnil;
3810}
3811
3812/* :nodoc: */
3813static VALUE
3814nsdr(VALUE self)
3815{
3816 VALUE ary = rb_ary_new();
3817#ifdef HAVE_BACKTRACE
3818#include <execinfo.h>
3819#define MAX_NATIVE_TRACE 1024
3820 static void *trace[MAX_NATIVE_TRACE];
3821 int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
3822 char **syms = backtrace_symbols(trace, n);
3823 int i;
3824
3825 if (syms == 0) {
3826 rb_memerror();
3827 }
3828
3829 for (i=0; i<n; i++) {
3830 rb_ary_push(ary, rb_str_new2(syms[i]));
3831 }
3832 free(syms); /* OK */
3833#endif
3834 return ary;
3835}
3836
3837#if VM_COLLECT_USAGE_DETAILS
3838static VALUE usage_analysis_insn_start(VALUE self);
3839static VALUE usage_analysis_operand_start(VALUE self);
3840static VALUE usage_analysis_register_start(VALUE self);
3841static VALUE usage_analysis_insn_stop(VALUE self);
3842static VALUE usage_analysis_operand_stop(VALUE self);
3843static VALUE usage_analysis_register_stop(VALUE self);
3844static VALUE usage_analysis_insn_running(VALUE self);
3845static VALUE usage_analysis_operand_running(VALUE self);
3846static VALUE usage_analysis_register_running(VALUE self);
3847static VALUE usage_analysis_insn_clear(VALUE self);
3848static VALUE usage_analysis_operand_clear(VALUE self);
3849static VALUE usage_analysis_register_clear(VALUE self);
3850#endif
3851
3852static VALUE
3853f_raise(int c, VALUE *v, VALUE _)
3854{
3855 return rb_f_raise(c, v);
3856}
3857
3858static VALUE
3859f_proc(VALUE _)
3860{
3861 return rb_block_proc();
3862}
3863
3864static VALUE
3865f_lambda(VALUE _)
3866{
3867 return rb_block_lambda();
3868}
3869
3870static VALUE
3871f_sprintf(int c, const VALUE *v, VALUE _)
3872{
3873 return rb_f_sprintf(c, v);
3874}
3875
3876/* :nodoc: */
3877static VALUE
3878vm_mtbl(VALUE self, VALUE obj, VALUE sym)
3879{
3880 vm_mtbl_dump(CLASS_OF(obj), RTEST(sym) ? SYM2ID(sym) : 0);
3881 return Qnil;
3882}
3883
3884/* :nodoc: */
3885static VALUE
3886vm_mtbl2(VALUE self, VALUE obj, VALUE sym)
3887{
3888 vm_mtbl_dump(obj, RTEST(sym) ? SYM2ID(sym) : 0);
3889 return Qnil;
3890}
3891
3892/*
3893 * call-seq:
3894 * RubyVM.keep_script_lines -> true or false
3895 *
3896 * Return current +keep_script_lines+ status. Now it only returns
3897 * +true+ of +false+, but it can return other objects in future.
3898 *
3899 * Note that this is an API for ruby internal use, debugging,
3900 * and research. Do not use this for any other purpose.
3901 * The compatibility is not guaranteed.
3902 */
3903static VALUE
3904vm_keep_script_lines(VALUE self)
3905{
3906 return RBOOL(ruby_vm_keep_script_lines);
3907}
3908
3909/*
3910 * call-seq:
3911 * RubyVM.keep_script_lines = true / false
3912 *
3913 * It set +keep_script_lines+ flag. If the flag is set, all
3914 * loaded scripts are recorded in a interpreter process.
3915 *
3916 * Note that this is an API for ruby internal use, debugging,
3917 * and research. Do not use this for any other purpose.
3918 * The compatibility is not guaranteed.
3919 */
3920static VALUE
3921vm_keep_script_lines_set(VALUE self, VALUE flags)
3922{
3923 ruby_vm_keep_script_lines = RTEST(flags);
3924 return flags;
3925}
3926
3927void
3928Init_VM(void)
3929{
3930 VALUE opts;
3931 VALUE klass;
3932 VALUE fcore;
3933
3934 /*
3935 * Document-class: RubyVM
3936 *
3937 * The RubyVM module only exists on MRI. +RubyVM+ is not defined in
3938 * other Ruby implementations such as JRuby and TruffleRuby.
3939 *
3940 * The RubyVM module provides some access to MRI internals.
3941 * This module is for very limited purposes, such as debugging,
3942 * prototyping, and research. Normal users must not use it.
3943 * This module is not portable between Ruby implementations.
3944 */
3945 rb_cRubyVM = rb_define_class("RubyVM", rb_cObject);
3946 rb_undef_alloc_func(rb_cRubyVM);
3947 rb_undef_method(CLASS_OF(rb_cRubyVM), "new");
3948 rb_define_singleton_method(rb_cRubyVM, "stat", vm_stat, -1);
3949 rb_define_singleton_method(rb_cRubyVM, "keep_script_lines", vm_keep_script_lines, 0);
3950 rb_define_singleton_method(rb_cRubyVM, "keep_script_lines=", vm_keep_script_lines_set, 1);
3951
3952#if USE_DEBUG_COUNTER
3953 rb_define_singleton_method(rb_cRubyVM, "reset_debug_counters", rb_debug_counter_reset, 0);
3954 rb_define_singleton_method(rb_cRubyVM, "show_debug_counters", rb_debug_counter_show, 0);
3955#endif
3956
3957 /* FrozenCore (hidden) */
3958 fcore = rb_class_new(rb_cBasicObject);
3959 rb_set_class_path(fcore, rb_cRubyVM, "FrozenCore");
3960 rb_vm_register_global_object(rb_class_path_cached(fcore));
3961 RBASIC(fcore)->flags = T_ICLASS;
3962 klass = rb_singleton_class(fcore);
3963 rb_define_method_id(klass, id_core_set_method_alias, m_core_set_method_alias, 3);
3964 rb_define_method_id(klass, id_core_set_variable_alias, m_core_set_variable_alias, 2);
3965 rb_define_method_id(klass, id_core_undef_method, m_core_undef_method, 2);
3966 rb_define_method_id(klass, id_core_set_postexe, m_core_set_postexe, 0);
3967 rb_define_method_id(klass, id_core_hash_merge_ptr, m_core_hash_merge_ptr, -1);
3968 rb_define_method_id(klass, id_core_hash_merge_kwd, m_core_hash_merge_kwd, 2);
3969 rb_define_method_id(klass, id_core_raise, f_raise, -1);
3970 rb_define_method_id(klass, id_core_sprintf, f_sprintf, -1);
3971 rb_define_method_id(klass, idProc, f_proc, 0);
3972 rb_define_method_id(klass, idLambda, f_lambda, 0);
3973 rb_define_method(klass, "make_shareable", m_core_make_shareable, 1);
3974 rb_define_method(klass, "make_shareable_copy", m_core_make_shareable_copy, 1);
3975 rb_define_method(klass, "ensure_shareable", m_core_ensure_shareable, 2);
3976 rb_obj_freeze(fcore);
3977 RBASIC_CLEAR_CLASS(klass);
3978 rb_obj_freeze(klass);
3979 rb_vm_register_global_object(fcore);
3980 rb_mRubyVMFrozenCore = fcore;
3981
3982 /*
3983 * Document-class: Thread
3984 *
3985 * Threads are the Ruby implementation for a concurrent programming model.
3986 *
3987 * Programs that require multiple threads of execution are a perfect
3988 * candidate for Ruby's Thread class.
3989 *
3990 * For example, we can create a new thread separate from the main thread's
3991 * execution using ::new.
3992 *
3993 * thr = Thread.new { puts "What's the big deal" }
3994 *
3995 * Then we are able to pause the execution of the main thread and allow
3996 * our new thread to finish, using #join:
3997 *
3998 * thr.join #=> "What's the big deal"
3999 *
4000 * If we don't call +thr.join+ before the main thread terminates, then all
4001 * other threads including +thr+ will be killed.
4002 *
4003 * Alternatively, you can use an array for handling multiple threads at
4004 * once, like in the following example:
4005 *
4006 * threads = []
4007 * threads << Thread.new { puts "What's the big deal" }
4008 * threads << Thread.new { 3.times { puts "Threads are fun!" } }
4009 *
4010 * After creating a few threads we wait for them all to finish
4011 * consecutively.
4012 *
4013 * threads.each { |thr| thr.join }
4014 *
4015 * To retrieve the last value of a thread, use #value
4016 *
4017 * thr = Thread.new { sleep 1; "Useful value" }
4018 * thr.value #=> "Useful value"
4019 *
4020 * === Thread initialization
4021 *
4022 * In order to create new threads, Ruby provides ::new, ::start, and
4023 * ::fork. A block must be provided with each of these methods, otherwise
4024 * a ThreadError will be raised.
4025 *
4026 * When subclassing the Thread class, the +initialize+ method of your
4027 * subclass will be ignored by ::start and ::fork. Otherwise, be sure to
4028 * call super in your +initialize+ method.
4029 *
4030 * === Thread termination
4031 *
4032 * For terminating threads, Ruby provides a variety of ways to do this.
4033 *
4034 * The class method ::kill, is meant to exit a given thread:
4035 *
4036 * thr = Thread.new { sleep }
4037 * Thread.kill(thr) # sends exit() to thr
4038 *
4039 * Alternatively, you can use the instance method #exit, or any of its
4040 * aliases #kill or #terminate.
4041 *
4042 * thr.exit
4043 *
4044 * === Thread status
4045 *
4046 * Ruby provides a few instance methods for querying the state of a given
4047 * thread. To get a string with the current thread's state use #status
4048 *
4049 * thr = Thread.new { sleep }
4050 * thr.status # => "sleep"
4051 * thr.exit
4052 * thr.status # => false
4053 *
4054 * You can also use #alive? to tell if the thread is running or sleeping,
4055 * and #stop? if the thread is dead or sleeping.
4056 *
4057 * === Thread variables and scope
4058 *
4059 * Since threads are created with blocks, the same rules apply to other
4060 * Ruby blocks for variable scope. Any local variables created within this
4061 * block are accessible to only this thread.
4062 *
4063 * ==== Fiber-local vs. Thread-local
4064 *
4065 * Each fiber has its own bucket for Thread#[] storage. When you set a
4066 * new fiber-local it is only accessible within this Fiber. To illustrate:
4067 *
4068 * Thread.new {
4069 * Thread.current[:foo] = "bar"
4070 * Fiber.new {
4071 * p Thread.current[:foo] # => nil
4072 * }.resume
4073 * }.join
4074 *
4075 * This example uses #[] for getting and #[]= for setting fiber-locals,
4076 * you can also use #keys to list the fiber-locals for a given
4077 * thread and #key? to check if a fiber-local exists.
4078 *
4079 * When it comes to thread-locals, they are accessible within the entire
4080 * scope of the thread. Given the following example:
4081 *
4082 * Thread.new{
4083 * Thread.current.thread_variable_set(:foo, 1)
4084 * p Thread.current.thread_variable_get(:foo) # => 1
4085 * Fiber.new{
4086 * Thread.current.thread_variable_set(:foo, 2)
4087 * p Thread.current.thread_variable_get(:foo) # => 2
4088 * }.resume
4089 * p Thread.current.thread_variable_get(:foo) # => 2
4090 * }.join
4091 *
4092 * You can see that the thread-local +:foo+ carried over into the fiber
4093 * and was changed to +2+ by the end of the thread.
4094 *
4095 * This example makes use of #thread_variable_set to create new
4096 * thread-locals, and #thread_variable_get to reference them.
4097 *
4098 * There is also #thread_variables to list all thread-locals, and
4099 * #thread_variable? to check if a given thread-local exists.
4100 *
4101 * === Exception handling
4102 *
4103 * When an unhandled exception is raised inside a thread, it will
4104 * terminate. By default, this exception will not propagate to other
4105 * threads. The exception is stored and when another thread calls #value
4106 * or #join, the exception will be re-raised in that thread.
4107 *
4108 * t = Thread.new{ raise 'something went wrong' }
4109 * t.value #=> RuntimeError: something went wrong
4110 *
4111 * An exception can be raised from outside the thread using the
4112 * Thread#raise instance method, which takes the same parameters as
4113 * Kernel#raise.
4114 *
4115 * Setting Thread.abort_on_exception = true, Thread#abort_on_exception =
4116 * true, or $DEBUG = true will cause a subsequent unhandled exception
4117 * raised in a thread to be automatically re-raised in the main thread.
4118 *
4119 * With the addition of the class method ::handle_interrupt, you can now
4120 * handle exceptions asynchronously with threads.
4121 *
4122 * === Scheduling
4123 *
4124 * Ruby provides a few ways to support scheduling threads in your program.
4125 *
4126 * The first way is by using the class method ::stop, to put the current
4127 * running thread to sleep and schedule the execution of another thread.
4128 *
4129 * Once a thread is asleep, you can use the instance method #wakeup to
4130 * mark your thread as eligible for scheduling.
4131 *
4132 * You can also try ::pass, which attempts to pass execution to another
4133 * thread but is dependent on the OS whether a running thread will switch
4134 * or not. The same goes for #priority, which lets you hint to the thread
4135 * scheduler which threads you want to take precedence when passing
4136 * execution. This method is also dependent on the OS and may be ignored
4137 * on some platforms.
4138 *
4139 */
4140 rb_cThread = rb_define_class("Thread", rb_cObject);
4141 rb_undef_alloc_func(rb_cThread);
4142
4143#if VM_COLLECT_USAGE_DETAILS
4144 /* ::RubyVM::USAGE_ANALYSIS_* */
4145#define define_usage_analysis_hash(name) /* shut up rdoc -C */ \
4146 rb_define_const(rb_cRubyVM, "USAGE_ANALYSIS_" #name, rb_hash_new())
4147 define_usage_analysis_hash(INSN);
4148 define_usage_analysis_hash(REGS);
4149 define_usage_analysis_hash(INSN_BIGRAM);
4150
4151 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_START", usage_analysis_insn_start, 0);
4152 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_START", usage_analysis_operand_start, 0);
4153 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_START", usage_analysis_register_start, 0);
4154 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_STOP", usage_analysis_insn_stop, 0);
4155 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_STOP", usage_analysis_operand_stop, 0);
4156 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_STOP", usage_analysis_register_stop, 0);
4157 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_RUNNING", usage_analysis_insn_running, 0);
4158 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_RUNNING", usage_analysis_operand_running, 0);
4159 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_RUNNING", usage_analysis_register_running, 0);
4160 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_INSN_CLEAR", usage_analysis_insn_clear, 0);
4161 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_OPERAND_CLEAR", usage_analysis_operand_clear, 0);
4162 rb_define_singleton_method(rb_cRubyVM, "USAGE_ANALYSIS_REGISTER_CLEAR", usage_analysis_register_clear, 0);
4163#endif
4164
4165 /* ::RubyVM::OPTS
4166 * An Array of VM build options.
4167 * This constant is MRI specific.
4168 */
4169 rb_define_const(rb_cRubyVM, "OPTS", opts = rb_ary_new());
4170
4171#if OPT_DIRECT_THREADED_CODE
4172 rb_ary_push(opts, rb_str_new2("direct threaded code"));
4173#elif OPT_TOKEN_THREADED_CODE
4174 rb_ary_push(opts, rb_str_new2("token threaded code"));
4175#elif OPT_CALL_THREADED_CODE
4176 rb_ary_push(opts, rb_str_new2("call threaded code"));
4177#endif
4178
4179#if OPT_OPERANDS_UNIFICATION
4180 rb_ary_push(opts, rb_str_new2("operands unification"));
4181#endif
4182#if OPT_INSTRUCTIONS_UNIFICATION
4183 rb_ary_push(opts, rb_str_new2("instructions unification"));
4184#endif
4185#if OPT_INLINE_METHOD_CACHE
4186 rb_ary_push(opts, rb_str_new2("inline method cache"));
4187#endif
4188
4189 /* ::RubyVM::INSTRUCTION_NAMES
4190 * A list of bytecode instruction names in MRI.
4191 * This constant is MRI specific.
4192 */
4193 rb_define_const(rb_cRubyVM, "INSTRUCTION_NAMES", rb_insns_name_array());
4194
4195 /* ::RubyVM::DEFAULT_PARAMS
4196 * This constant exposes the VM's default parameters.
4197 * Note that changing these values does not affect VM execution.
4198 * Specification is not stable and you should not depend on this value.
4199 * Of course, this constant is MRI specific.
4200 */
4201 rb_define_const(rb_cRubyVM, "DEFAULT_PARAMS", vm_default_params());
4202
4203 /* debug functions ::RubyVM::SDR(), ::RubyVM::NSDR() */
4204#if VMDEBUG
4205 rb_define_singleton_method(rb_cRubyVM, "SDR", sdr, 0);
4206 rb_define_singleton_method(rb_cRubyVM, "NSDR", nsdr, 0);
4207 rb_define_singleton_method(rb_cRubyVM, "mtbl", vm_mtbl, 2);
4208 rb_define_singleton_method(rb_cRubyVM, "mtbl2", vm_mtbl2, 2);
4209#else
4210 (void)sdr;
4211 (void)nsdr;
4212 (void)vm_mtbl;
4213 (void)vm_mtbl2;
4214#endif
4215
4216 /* VM bootstrap: phase 2 */
4217 {
4218 rb_vm_t *vm = ruby_current_vm_ptr;
4219 rb_thread_t *th = GET_THREAD();
4220 VALUE filename = rb_fstring_lit("<main>");
4221 const rb_iseq_t *iseq = rb_iseq_new(Qnil, filename, filename, Qnil, 0, ISEQ_TYPE_TOP);
4222
4223 // Ractor setup
4224 rb_ractor_main_setup(vm, th->ractor, th);
4225
4226 /* create vm object */
4227 vm->self = TypedData_Wrap_Struct(rb_cRubyVM, &vm_data_type, vm);
4228
4229 /* create main thread */
4230 th->self = TypedData_Wrap_Struct(rb_cThread, &thread_data_type, th);
4231 vm->ractor.main_thread = th;
4232 vm->ractor.main_ractor = th->ractor;
4233 th->vm = vm;
4234 th->top_wrapper = 0;
4235 th->top_self = rb_vm_top_self();
4236
4237 rb_vm_register_global_object((VALUE)iseq);
4238 th->ec->cfp->iseq = iseq;
4239 th->ec->cfp->pc = ISEQ_BODY(iseq)->iseq_encoded;
4240 th->ec->cfp->self = th->top_self;
4241
4242 VM_ENV_FLAGS_UNSET(th->ec->cfp->ep, VM_FRAME_FLAG_CFRAME);
4243 VM_STACK_ENV_WRITE(th->ec->cfp->ep, VM_ENV_DATA_INDEX_ME_CREF, (VALUE)vm_cref_new(rb_cObject, METHOD_VISI_PRIVATE, FALSE, NULL, FALSE, FALSE));
4244
4245 /*
4246 * The Binding of the top level scope
4247 */
4248 rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new());
4249
4250#ifdef _WIN32
4251 rb_objspace_gc_enable(vm->gc.objspace);
4252#endif
4253 }
4254 vm_init_redefined_flag();
4255
4256 rb_block_param_proxy = rb_obj_alloc(rb_cObject);
4257 rb_add_method_optimized(rb_singleton_class(rb_block_param_proxy), idCall,
4258 OPTIMIZED_METHOD_TYPE_BLOCK_CALL, 0, METHOD_VISI_PUBLIC);
4259 rb_obj_freeze(rb_block_param_proxy);
4260 rb_vm_register_global_object(rb_block_param_proxy);
4261
4262 /* vm_backtrace.c */
4263 Init_vm_backtrace();
4264}
4265
4266void
4267rb_vm_set_progname(VALUE filename)
4268{
4269 rb_thread_t *th = GET_VM()->ractor.main_thread;
4270 rb_control_frame_t *cfp = (void *)(th->ec->vm_stack + th->ec->vm_stack_size);
4271 --cfp;
4272
4273 filename = rb_str_new_frozen(filename);
4274 rb_iseq_pathobj_set(cfp->iseq, filename, rb_iseq_realpath(cfp->iseq));
4275}
4276
4277extern const struct st_hash_type rb_fstring_hash_type;
4278
4279void
4280Init_BareVM(void)
4281{
4282 /* VM bootstrap: phase 1 */
4283 rb_vm_t *vm = ruby_mimcalloc(1, sizeof(*vm));
4284 rb_thread_t *th = ruby_mimcalloc(1, sizeof(*th));
4285 if (!vm || !th) {
4286 fputs("[FATAL] failed to allocate memory\n", stderr);
4287 exit(EXIT_FAILURE);
4288 }
4289
4290 // setup the VM
4291 vm_init2(vm);
4292
4293 rb_vm_postponed_job_queue_init(vm);
4294 ruby_current_vm_ptr = vm;
4295 rb_objspace_alloc();
4296 vm->negative_cme_table = rb_id_table_create(16);
4297 vm->overloaded_cme_table = st_init_numtable();
4298 vm->constant_cache = rb_id_table_create(0);
4299 vm->unused_block_warning_table = st_init_numtable();
4300
4301 // setup main thread
4302 th->nt = ZALLOC(struct rb_native_thread);
4303 th->vm = vm;
4304 th->ractor = vm->ractor.main_ractor = rb_ractor_main_alloc();
4305 Init_native_thread(th);
4306 rb_jit_cont_init();
4307 th_init(th, 0, vm);
4308
4309 rb_ractor_set_current_ec(th->ractor, th->ec);
4310 /* n.b. native_main_thread_stack_top is set by the INIT_STACK macro */
4311 ruby_thread_init_stack(th, native_main_thread_stack_top);
4312
4313 // setup ractor system
4314 rb_native_mutex_initialize(&vm->ractor.sync.lock);
4315 rb_native_cond_initialize(&vm->ractor.sync.terminate_cond);
4316
4317 vm_opt_method_def_table = st_init_numtable();
4318 vm_opt_mid_table = st_init_numtable();
4319
4320#ifdef RUBY_THREAD_WIN32_H
4321 rb_native_cond_initialize(&vm->ractor.sync.barrier_cond);
4322#endif
4323}
4324
4325void
4326ruby_init_stack(void *addr)
4327{
4328 native_main_thread_stack_top = addr;
4329}
4330
4331#ifndef _WIN32
4332#include <unistd.h>
4333#include <sys/mman.h>
4334#endif
4335
4336
4337#ifndef MARK_OBJECT_ARY_BUCKET_SIZE
4338#define MARK_OBJECT_ARY_BUCKET_SIZE 1024
4339#endif
4340
4341 struct pin_array_list {
4342 VALUE next;
4343 long len;
4344 VALUE *array;
4345};
4346
4347static void
4348pin_array_list_mark(void *data)
4349{
4350 struct pin_array_list *array = (struct pin_array_list *)data;
4351 rb_gc_mark_movable(array->next);
4352
4353 rb_gc_mark_vm_stack_values(array->len, array->array);
4354}
4355
4356static void
4357pin_array_list_free(void *data)
4358{
4359 struct pin_array_list *array = (struct pin_array_list *)data;
4360 xfree(array->array);
4361}
4362
4363static size_t
4364pin_array_list_memsize(const void *data)
4365{
4366 return sizeof(struct pin_array_list) + (MARK_OBJECT_ARY_BUCKET_SIZE * sizeof(VALUE));
4367}
4368
4369static void
4370pin_array_list_update_references(void *data)
4371{
4372 struct pin_array_list *array = (struct pin_array_list *)data;
4373 array->next = rb_gc_location(array->next);
4374}
4375
4376static const rb_data_type_t pin_array_list_type = {
4377 .wrap_struct_name = "VM/pin_array_list",
4378 .function = {
4379 .dmark = pin_array_list_mark,
4380 .dfree = pin_array_list_free,
4381 .dsize = pin_array_list_memsize,
4382 .dcompact = pin_array_list_update_references,
4383 },
4384 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
4385};
4386
4387static VALUE
4388pin_array_list_new(VALUE next)
4389{
4390 struct pin_array_list *array_list;
4391 VALUE obj = TypedData_Make_Struct(0, struct pin_array_list, &pin_array_list_type, array_list);
4392 RB_OBJ_WRITE(obj, &array_list->next, next);
4393 array_list->array = ALLOC_N(VALUE, MARK_OBJECT_ARY_BUCKET_SIZE);
4394 return obj;
4395}
4396
4397static VALUE
4398pin_array_list_append(VALUE obj, VALUE item)
4399{
4400 struct pin_array_list *array_list;
4401 TypedData_Get_Struct(obj, struct pin_array_list, &pin_array_list_type, array_list);
4402
4403 if (array_list->len >= MARK_OBJECT_ARY_BUCKET_SIZE) {
4404 obj = pin_array_list_new(obj);
4405 TypedData_Get_Struct(obj, struct pin_array_list, &pin_array_list_type, array_list);
4406 }
4407
4408 RB_OBJ_WRITE(obj, &array_list->array[array_list->len], item);
4409 array_list->len++;
4410 return obj;
4411}
4412
4413void
4414rb_vm_register_global_object(VALUE obj)
4415{
4416 RUBY_ASSERT(!RB_SPECIAL_CONST_P(obj));
4417 if (RB_SPECIAL_CONST_P(obj)) {
4418 return;
4419 }
4420
4421 switch (RB_BUILTIN_TYPE(obj)) {
4422 case T_CLASS:
4423 case T_MODULE:
4424 if (FL_TEST(obj, RCLASS_IS_ROOT)) {
4425 return;
4426 }
4427 FL_SET(obj, RCLASS_IS_ROOT);
4428 break;
4429 default:
4430 break;
4431 }
4432 RB_VM_LOCK_ENTER();
4433 {
4434 VALUE list = GET_VM()->mark_object_ary;
4435 VALUE head = pin_array_list_append(list, obj);
4436 if (head != list) {
4437 GET_VM()->mark_object_ary = head;
4438 }
4439 RB_GC_GUARD(obj);
4440 }
4441 RB_VM_LOCK_LEAVE();
4442}
4443
4444void
4445Init_vm_objects(void)
4446{
4447 rb_vm_t *vm = GET_VM();
4448
4449 /* initialize mark object array, hash */
4450 vm->mark_object_ary = pin_array_list_new(Qnil);
4451 vm->loading_table = st_init_strtable();
4452 vm->ci_table = st_init_table(&vm_ci_hashtype);
4453 vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000);
4454}
4455
4456// Stub for builtin function when not building YJIT units
4457#if !USE_YJIT
4458void Init_builtin_yjit(void) {}
4459#endif
4460
4461// Whether YJIT is enabled or not, we load yjit_hook.rb to remove Kernel#with_yjit.
4462#include "yjit_hook.rbinc"
4463
4464// Stub for builtin function when not building RJIT units
4465#if !USE_RJIT
4466void Init_builtin_rjit(void) {}
4467void Init_builtin_rjit_c(void) {}
4468#endif
4469
4470/* top self */
4471
4472static VALUE
4473main_to_s(VALUE obj)
4474{
4475 return rb_str_new2("main");
4476}
4477
4478 VALUE
4479rb_vm_top_self(void)
4480{
4481 return GET_VM()->top_self;
4482}
4483
4484void
4485Init_top_self(void)
4486{
4487 rb_vm_t *vm = GET_VM();
4488
4489 vm->top_self = rb_obj_alloc(rb_cObject);
4490 rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s, 0);
4491 rb_define_alias(rb_singleton_class(rb_vm_top_self()), "inspect", "to_s");
4492}
4493
4494 VALUE *
4495rb_ruby_verbose_ptr(void)
4496{
4497 rb_ractor_t *cr = GET_RACTOR();
4498 return &cr->verbose;
4499}
4500
4501 VALUE *
4502rb_ruby_debug_ptr(void)
4503{
4504 rb_ractor_t *cr = GET_RACTOR();
4505 return &cr->debug;
4506}
4507
4508bool rb_free_at_exit = false;
4509
4510bool
4511ruby_free_at_exit_p(void)
4512{
4513 return rb_free_at_exit;
4514}
4515
4516/* iseq.c */
4517 VALUE rb_insn_operand_intern(const rb_iseq_t *iseq,
4518 VALUE insn, int op_no, VALUE op,
4519 int len, size_t pos, VALUE *pnop, VALUE child);
4520
4521st_table *
4522rb_vm_fstring_table(void)
4523{
4524 return GET_VM()->frozen_strings;
4525}
4526
4527#if VM_COLLECT_USAGE_DETAILS
4528
4529#define HASH_ASET(h, k, v) rb_hash_aset((h), (st_data_t)(k), (st_data_t)(v))
4530
4531/* uh = {
4532 * insn(Fixnum) => ihash(Hash)
4533 * }
4534 * ihash = {
4535 * -1(Fixnum) => count, # insn usage
4536 * 0(Fixnum) => ophash, # operand usage
4537 * }
4538 * ophash = {
4539 * val(interned string) => count(Fixnum)
4540 * }
4541 */
4542static void
4543vm_analysis_insn(int insn)
4544{
4545 ID usage_hash;
4546 ID bigram_hash;
4547 static int prev_insn = -1;
4548
4549 VALUE uh;
4550 VALUE ihash;
4551 VALUE cv;
4552
4553 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4554 CONST_ID(bigram_hash, "USAGE_ANALYSIS_INSN_BIGRAM");
4555 uh = rb_const_get(rb_cRubyVM, usage_hash);
4556 if (NIL_P(ihash = rb_hash_aref(uh, INT2FIX(insn)))) {
4557 ihash = rb_hash_new();
4558 HASH_ASET(uh, INT2FIX(insn), ihash);
4559 }
4560 if (NIL_P(cv = rb_hash_aref(ihash, INT2FIX(-1)))) {
4561 cv = INT2FIX(0);
4562 }
4563 HASH_ASET(ihash, INT2FIX(-1), INT2FIX(FIX2INT(cv) + 1));
4564
4565 /* calc bigram */
4566 if (prev_insn != -1) {
4567 VALUE bi;
4568 VALUE ary[2];
4569 VALUE cv;
4570
4571 ary[0] = INT2FIX(prev_insn);
4572 ary[1] = INT2FIX(insn);
4573 bi = rb_ary_new4(2, &ary[0]);
4574
4575 uh = rb_const_get(rb_cRubyVM, bigram_hash);
4576 if (NIL_P(cv = rb_hash_aref(uh, bi))) {
4577 cv = INT2FIX(0);
4578 }
4579 HASH_ASET(uh, bi, INT2FIX(FIX2INT(cv) + 1));
4580 }
4581 prev_insn = insn;
4582}
4583
4584static void
4585vm_analysis_operand(int insn, int n, VALUE op)
4586{
4587 ID usage_hash;
4588
4589 VALUE uh;
4590 VALUE ihash;
4591 VALUE ophash;
4592 VALUE valstr;
4593 VALUE cv;
4594
4595 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4596
4597 uh = rb_const_get(rb_cRubyVM, usage_hash);
4598 if (NIL_P(ihash = rb_hash_aref(uh, INT2FIX(insn)))) {
4599 ihash = rb_hash_new();
4600 HASH_ASET(uh, INT2FIX(insn), ihash);
4601 }
4602 if (NIL_P(ophash = rb_hash_aref(ihash, INT2FIX(n)))) {
4603 ophash = rb_hash_new();
4604 HASH_ASET(ihash, INT2FIX(n), ophash);
4605 }
4606 /* intern */
4607 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4608
4609 /* set count */
4610 if (NIL_P(cv = rb_hash_aref(ophash, valstr))) {
4611 cv = INT2FIX(0);
4612 }
4613 HASH_ASET(ophash, valstr, INT2FIX(FIX2INT(cv) + 1));
4614}
4615
4616static void
4617vm_analysis_register(int reg, int isset)
4618{
4619 ID usage_hash;
4620 VALUE uh;
4621 VALUE valstr;
4622 static const char regstrs[][5] = {
4623 "pc", /* 0 */
4624 "sp", /* 1 */
4625 "ep", /* 2 */
4626 "cfp", /* 3 */
4627 "self", /* 4 */
4628 "iseq", /* 5 */
4629 };
4630 static const char getsetstr[][4] = {
4631 "get",
4632 "set",
4633 };
4634 static VALUE syms[sizeof(regstrs) / sizeof(regstrs[0])][2];
4635
4636 VALUE cv;
4637
4638 CONST_ID(usage_hash, "USAGE_ANALYSIS_REGS");
4639 if (syms[0] == 0) {
4640 char buff[0x10];
4641 int i;
4642
4643 for (i = 0; i < (int)(sizeof(regstrs) / sizeof(regstrs[0])); i++) {
4644 int j;
4645 for (j = 0; j < 2; j++) {
4646 snprintf(buff, 0x10, "%d %s %-4s", i, getsetstr[j], regstrs[i]);
4647 syms[i][j] = ID2SYM(rb_intern(buff));
4648 }
4649 }
4650 }
4651 valstr = syms[reg][isset];
4652
4653 uh = rb_const_get(rb_cRubyVM, usage_hash);
4654 if (NIL_P(cv = rb_hash_aref(uh, valstr))) {
4655 cv = INT2FIX(0);
4656 }
4657 HASH_ASET(uh, valstr, INT2FIX(FIX2INT(cv) + 1));
4658}
4659
4660#undef HASH_ASET
4661
4662static void (*ruby_vm_collect_usage_func_insn)(int insn) = NULL;
4663static void (*ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op) = NULL;
4664static void (*ruby_vm_collect_usage_func_register)(int reg, int isset) = NULL;
4665
4666/* :nodoc: */
4667static VALUE
4668usage_analysis_insn_start(VALUE self)
4669{
4670 ruby_vm_collect_usage_func_insn = vm_analysis_insn;
4671 return Qnil;
4672}
4673
4674/* :nodoc: */
4675static VALUE
4676usage_analysis_operand_start(VALUE self)
4677{
4678 ruby_vm_collect_usage_func_operand = vm_analysis_operand;
4679 return Qnil;
4680}
4681
4682/* :nodoc: */
4683static VALUE
4684usage_analysis_register_start(VALUE self)
4685{
4686 ruby_vm_collect_usage_func_register = vm_analysis_register;
4687 return Qnil;
4688}
4689
4690/* :nodoc: */
4691static VALUE
4692usage_analysis_insn_stop(VALUE self)
4693{
4694 ruby_vm_collect_usage_func_insn = 0;
4695 return Qnil;
4696}
4697
4698/* :nodoc: */
4699static VALUE
4700usage_analysis_operand_stop(VALUE self)
4701{
4702 ruby_vm_collect_usage_func_operand = 0;
4703 return Qnil;
4704}
4705
4706/* :nodoc: */
4707static VALUE
4708usage_analysis_register_stop(VALUE self)
4709{
4710 ruby_vm_collect_usage_func_register = 0;
4711 return Qnil;
4712}
4713
4714/* :nodoc: */
4715static VALUE
4716usage_analysis_insn_running(VALUE self)
4717{
4718 return RBOOL(ruby_vm_collect_usage_func_insn != 0);
4719}
4720
4721/* :nodoc: */
4722static VALUE
4723usage_analysis_operand_running(VALUE self)
4724{
4725 return RBOOL(ruby_vm_collect_usage_func_operand != 0);
4726}
4727
4728/* :nodoc: */
4729static VALUE
4730usage_analysis_register_running(VALUE self)
4731{
4732 return RBOOL(ruby_vm_collect_usage_func_register != 0);
4733}
4734
4735static VALUE
4736usage_analysis_clear(VALUE self, ID usage_hash)
4737{
4738 VALUE uh;
4739 uh = rb_const_get(self, usage_hash);
4740 rb_hash_clear(uh);
4741
4742 return Qtrue;
4743}
4744
4745
4746/* :nodoc: */
4747static VALUE
4748usage_analysis_insn_clear(VALUE self)
4749{
4750 ID usage_hash;
4751 ID bigram_hash;
4752
4753 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4754 CONST_ID(bigram_hash, "USAGE_ANALYSIS_INSN_BIGRAM");
4755 usage_analysis_clear(rb_cRubyVM, usage_hash);
4756 return usage_analysis_clear(rb_cRubyVM, bigram_hash);
4757}
4758
4759/* :nodoc: */
4760static VALUE
4761usage_analysis_operand_clear(VALUE self)
4762{
4763 ID usage_hash;
4764
4765 CONST_ID(usage_hash, "USAGE_ANALYSIS_INSN");
4766 return usage_analysis_clear(self, usage_hash);
4767}
4768
4769/* :nodoc: */
4770static VALUE
4771usage_analysis_register_clear(VALUE self)
4772{
4773 ID usage_hash;
4774
4775 CONST_ID(usage_hash, "USAGE_ANALYSIS_REGS");
4776 return usage_analysis_clear(self, usage_hash);
4777}
4778
4779#else
4780
4781MAYBE_UNUSED(static void (*ruby_vm_collect_usage_func_insn)(int insn)) = 0;
4782MAYBE_UNUSED(static void (*ruby_vm_collect_usage_func_operand)(int insn, int n, VALUE op)) = 0;
4783MAYBE_UNUSED(static void (*ruby_vm_collect_usage_func_register)(int reg, int isset)) = 0;
4784
4785#endif
4786
4787#if VM_COLLECT_USAGE_DETAILS
4788/* @param insn instruction number */
4789static void
4790vm_collect_usage_insn(int insn)
4791{
4792 if (RUBY_DTRACE_INSN_ENABLED()) {
4793 RUBY_DTRACE_INSN(rb_insns_name(insn));
4794 }
4795 if (ruby_vm_collect_usage_func_insn)
4796 (*ruby_vm_collect_usage_func_insn)(insn);
4797}
4798
4799/* @param insn instruction number
4800 * @param n n-th operand
4801 * @param op operand value
4802 */
4803static void
4804vm_collect_usage_operand(int insn, int n, VALUE op)
4805{
4806 if (RUBY_DTRACE_INSN_OPERAND_ENABLED()) {
4807 VALUE valstr;
4808
4809 valstr = rb_insn_operand_intern(GET_EC()->cfp->iseq, insn, n, op, 0, 0, 0, 0);
4810
4811 RUBY_DTRACE_INSN_OPERAND(RSTRING_PTR(valstr), rb_insns_name(insn));
4812 RB_GC_GUARD(valstr);
4813 }
4814 if (ruby_vm_collect_usage_func_operand)
4815 (*ruby_vm_collect_usage_func_operand)(insn, n, op);
4816}
4817
4818/* @param reg register id. see code of vm_analysis_register() */
4819/* @param isset 0: read, 1: write */
4820static void
4821vm_collect_usage_register(int reg, int isset)
4822{
4823 if (ruby_vm_collect_usage_func_register)
4824 (*ruby_vm_collect_usage_func_register)(reg, isset);
4825}
4826#endif
4827
4828const struct rb_callcache *
4829rb_vm_empty_cc(void)
4830{
4831 return &vm_empty_cc;
4832}
4833
4834const struct rb_callcache *
4835rb_vm_empty_cc_for_super(void)
4836{
4837 return &vm_empty_cc_for_super;
4838}
4839
4840#include "vm_call_iseq_optimized.inc" /* required from vm_insnhelper.c */
RUBY_ASSERT_MESG
#define RUBY_ASSERT_MESG(expr,...)
Asserts that the expression is truthy.
Definition assert.h:186
RUBY_ASSERT
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
rb_atomic_t
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
RUBY_ATOMIC_FETCH_ADD
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
Definition atomic.h:93
rb_define_method
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:670
rb_define_method_id
#define rb_define_method_id(klass, mid, func, arity)
Defines klass#mid.
Definition cxxanyargs.hpp:673
rb_define_singleton_method
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition cxxanyargs.hpp:685
RUBY_EVENT_END
#define RUBY_EVENT_END
Encountered an end of a class clause.
Definition event.h:40
RUBY_EVENT_B_RETURN
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
Definition event.h:56
RUBY_EVENT_RETURN
#define RUBY_EVENT_RETURN
Encountered a return statement.
Definition event.h:42
RUBY_EVENT_C_RETURN
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
rb_event_flag_t
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
RUBY_FL_SHAREABLE
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:266
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:980
rb_class_new
VALUE rb_class_new(VALUE super)
Creates a new, anonymous class.
Definition class.c:359
rb_singleton_class
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2297
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition class.c:2345
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2166
rb_str_new2
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
Regexp
struct re_pattern_buffer Regexp
Old name of re_pattern_buffer.
Definition rmatch.h:52
NUM2ULONG
#define NUM2ULONG
Old name of RB_NUM2ULONG.
Definition long.h:52
ALLOCV
#define ALLOCV
Old name of RB_ALLOCV.
Definition memory.h:404
ALLOC
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
T_IMEMO
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
OBJ_FREEZE
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:135
ULONG2NUM
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
SYM2ID
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
CLASS_OF
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
rb_ary_new4
#define rb_ary_new4
Old name of rb_ary_new_from_values.
Definition array.h:659
SIZET2NUM
#define SIZET2NUM
Old name of RB_SIZE2NUM.
Definition size_t.h:62
rb_exc_new2
#define rb_exc_new2
Old name of rb_exc_new_cstr.
Definition error.h:37
FIX2INT
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
T_MODULE
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
ZALLOC_N
#define ZALLOC_N
Old name of RB_ZALLOC_N.
Definition memory.h:401
ASSUME
#define ASSUME
Old name of RBIMPL_ASSUME.
Definition assume.h:27
T_ICLASS
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
T_HASH
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
ALLOC_N
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
FL_SET
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:129
rb_exc_new3
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
ULL2NUM
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition long_long.h:31
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
NUM2ULL
#define NUM2ULL
Old name of RB_NUM2ULL.
Definition long_long.h:35
T_CLASS
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
FL_TEST
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:131
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
FL_USHIFT
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:69
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
FL_SET_RAW
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:130
ALLOCV_END
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
ruby_init_stack
void ruby_init_stack(void *addr)
Set stack bottom of Ruby implementation.
Definition vm.c:4326
rb_eLocalJumpError
VALUE rb_eLocalJumpError
LocalJumpError exception.
Definition eval.c:49
rb_category_warn
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:476
rb_typeddata_is_kind_of
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition error.c:1380
rb_iter_break
void rb_iter_break(void)
Breaks from a block.
Definition vm.c:2085
rb_eTypeError
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
rb_iter_break_value
void rb_iter_break_value(VALUE val)
Identical to rb_iter_break(), except it additionally takes the "value" of this breakage.
Definition vm.c:2091
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1428
rb_ruby_verbose_ptr
VALUE * rb_ruby_verbose_ptr(void)
This is an implementation detail of ruby_verbose.
Definition vm.c:4495
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1481
rb_ruby_debug_ptr
VALUE * rb_ruby_debug_ptr(void)
This is an implementation detail of ruby_debug.
Definition vm.c:4502
rb_eSysStackError
VALUE rb_eSysStackError
SystemStackError exception.
Definition eval.c:50
RB_WARN_CATEGORY_PERFORMANCE
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
Definition error.h:54
rb_cTime
VALUE rb_cTime
Time class.
Definition time.c:674
rb_cArray
VALUE rb_cArray
Array class.
Definition array.c:40
rb_obj_alloc
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2097
rb_cInteger
VALUE rb_cInteger
Module class.
Definition numeric.c:198
rb_cNilClass
VALUE rb_cNilClass
NilClass class.
Definition object.c:71
rb_cBinding
VALUE rb_cBinding
Binding class.
Definition proc.c:43
rb_cRegexp
VALUE rb_cRegexp
Regexp class.
Definition re.c:2640
rb_cHash
VALUE rb_cHash
Hash class.
Definition hash.c:113
rb_cFalseClass
VALUE rb_cFalseClass
FalseClass class.
Definition object.c:73
rb_obj_class
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:247
rb_cSymbol
VALUE rb_cSymbol
Symbol class.
Definition string.c:79
rb_cBasicObject
VALUE rb_cBasicObject
BasicObject class.
Definition object.c:64
rb_cThread
VALUE rb_cThread
Thread class.
Definition vm.c:544
rb_obj_freeze
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1260
rb_cFloat
VALUE rb_cFloat
Float class.
Definition numeric.c:197
rb_cProc
VALUE rb_cProc
Proc class.
Definition proc.c:44
rb_cTrueClass
VALUE rb_cTrueClass
TrueClass class.
Definition object.c:72
rb_cString
VALUE rb_cString
String class.
Definition string.c:78
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:615
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
rb_undef
void rb_undef(VALUE mod, ID mid)
Inserts a method entry that hides previous method definition of the given name.
Definition vm_method.c:1906
rb_check_arity
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
rb_backref_get
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:1835
rb_lastline_set
void rb_lastline_set(VALUE str)
Updates $_.
Definition vm.c:1853
rb_lastline_get
VALUE rb_lastline_get(void)
Queries the last line, or the $_.
Definition vm.c:1847
rb_backref_set
void rb_backref_set(VALUE md)
Updates $~.
Definition vm.c:1841
rb_block_proc
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:836
rb_block_lambda
VALUE rb_block_lambda(void)
Identical to rb_proc_new(), except it returns a lambda.
Definition proc.c:855
rb_binding_new
VALUE rb_binding_new(void)
Snapshots the current execution context and turn it into an instance of rb_cBinding.
Definition proc.c:324
rb_str_append
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3676
rb_str_new_frozen
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1461
rb_str_cat_cstr
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1656
rb_const_get
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3163
rb_set_class_path
void rb_set_class_path(VALUE klass, VALUE space, const char *name)
Names a class.
Definition variable.c:353
rb_class_path_cached
VALUE rb_class_path_cached(VALUE mod)
Just another name of rb_mod_name.
Definition variable.c:302
rb_alias_variable
void rb_alias_variable(ID dst, ID src)
Aliases a global variable.
Definition variable.c:999
rb_class_path
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:293
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1291
rb_sourcefile
const char * rb_sourcefile(void)
Resembles __FILE__.
Definition vm.c:1872
rb_alias
void rb_alias(VALUE klass, ID dst, ID src)
Resembles alias.
Definition vm_method.c:2289
rb_frame_method_id_and_class
int rb_frame_method_id_and_class(ID *idp, VALUE *klassp)
Resembles __method__.
Definition vm.c:2888
rb_sourceline
int rb_sourceline(void)
Resembles __LINE__.
Definition vm.c:1886
rb_sym2str
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:970
rb_define_global_const
void rb_define_global_const(const char *name, VALUE val)
Identical to rb_define_const(), except it defines that of "global", i.e.
Definition variable.c:3743
rb_define_const
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
Definition variable.c:3729
rb_iv_set
VALUE rb_iv_set(VALUE obj, const char *name, VALUE val)
Assigns to an instance variable.
Definition variable.c:4227
len
int len
Length of the buffer.
Definition io.h:8
rb_ractor_make_shareable_copy
VALUE rb_ractor_make_shareable_copy(VALUE obj)
Identical to rb_ractor_make_shareable(), except it returns a (deep) copy of the passed one instead of...
Definition ractor.c:3124
rb_ractor_shareable_p
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
RB_OBJ_SHAREABLE_P
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
rb_ractor_make_shareable
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition ractor.c:3115
vm.h
ruby_vm_at_exit
void ruby_vm_at_exit(void(*func)(ruby_vm_t *))
ruby_vm_at_exit registers a function func to be invoked when a VM passed away.
Definition vm.c:878
ruby_vm_destruct
int ruby_vm_destruct(ruby_vm_t *vm)
Destructs the passed VM.
Definition vm.c:3074
rb_f_sprintf
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
Definition sprintf.c:209
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
MEMZERO
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:360
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
ruby::backward::cxxanyargs::rb_hash_foreach
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
Definition cxxanyargs.hpp:482
RARRAY_LEN
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
RARRAY_LENINT
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:281
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RBASIC_CLASS
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:150
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
RCLASS
#define RCLASS(obj)
Convenient casting macro.
Definition rclass.h:38
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
StringValuePtr
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition rstring.h:76
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition rtypeddata.h:102
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:515
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:449
rb_data_type_t
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:197
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:497
rb_class2name
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
Definition variable.c:418
RB_NO_KEYWORDS
#define RB_NO_KEYWORDS
Do not pass keywords.
Definition scan_args.h:69
RB_SPECIAL_CONST_P
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Definition special_consts.h:327
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
_
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
METHOD
Definition proc.c:29
collect_outer_variable_name_data
Definition vm.c:1216
global_object_list
Definition vm_core.h:661
iseq_catch_table_entry
Definition iseq.h:269
iseq_catch_table
Definition iseq.h:280
local_var_list
Definition vm_eval.c:15
objspace
Definition mmtk.c:19
pin_array_list
Definition vm.c:4341
rb_at_exit_list
Definition vm_core.h:640
rb_binding_t
Definition vm_core.h:1294
rb_block
Definition vm_core.h:891
rb_builtin_function
Definition builtin.h:6
rb_callcache
Definition vm_callinfo.h:278
rb_captured_block
Definition vm_core.h:867
rb_env_t
Definition vm_core.h:1281
rb_id_table
Definition id_table.c:41
rb_iseq_constant_body
Definition vm_core.h:404
rb_native_thread
Definition thread_none.h:11
rb_objspace
Definition default.c:445
rb_proc_t
Definition vm_core.h:1268
rb_vm_tag
Definition vm_core.h:993
rb_wasm_try_catch
Definition setjmp.h:64
ruby_dtrace_method_hook_args
Definition probes_helper.h:6
vm_ifunc
IFUNC (Internal FUNCtion)
Definition imemo.h:88
vm_throw_data
THROW_DATA.
Definition imemo.h:61
rb_native_cond_initialize
void rb_native_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
rb_native_mutex_initialize
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
rb_native_mutex_destroy
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_BUILTIN_TYPE
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
Check_Type
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
RB_TYPE_P
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
ruby_value_type
ruby_value_type
C-level type of an object.
Definition value_type.h:113