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