Ruby: vm.c Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

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