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