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