Ruby  3.1.4p223 (2023-03-30 revision HEAD)
vm_core.h
1 #ifndef RUBY_VM_CORE_H
2 #define RUBY_VM_CORE_H
3 /**********************************************************************
4 
5  vm_core.h -
6 
7  $Author$
8  created at: 04/01/01 19:41:38 JST
9 
10  Copyright (C) 2004-2007 Koichi Sasada
11 
12 **********************************************************************/
13 
14 /*
15  * Enable check mode.
16  * 1: enable local assertions.
17  */
18 #ifndef VM_CHECK_MODE
19 
20 // respect RUBY_DUBUG: if given n is 0, then use RUBY_DEBUG
21 #define N_OR_RUBY_DEBUG(n) (((n) > 0) ? (n) : RUBY_DEBUG)
22 
23 #define VM_CHECK_MODE N_OR_RUBY_DEBUG(0)
24 #endif
25 
39 #ifndef VMDEBUG
40 #define VMDEBUG 0
41 #endif
42 
43 #if 0
44 #undef VMDEBUG
45 #define VMDEBUG 3
46 #endif
47 
48 #include "ruby/internal/config.h"
49 
50 #include <stddef.h>
51 #include <signal.h>
52 #include <stdarg.h>
53 
54 #include "ruby_assert.h"
55 
56 #if VM_CHECK_MODE > 0
57 #define VM_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr)
58 #define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
59 
60 #else
61 #define VM_ASSERT(expr) ((void)0)
62 #define VM_UNREACHABLE(func) UNREACHABLE
63 #endif
64 
65 #include <setjmp.h>
66 
67 #include "ruby/internal/stdbool.h"
68 #include "ccan/list/list.h"
69 #include "id.h"
70 #include "internal.h"
71 #include "internal/array.h"
72 #include "internal/serial.h"
73 #include "internal/vm.h"
74 #include "method.h"
75 #include "node.h"
76 #include "ruby/ruby.h"
77 #include "ruby/st.h"
78 #include "ruby_atomic.h"
79 #include "vm_opts.h"
80 #include "darray.h"
81 
82 #include "ruby/thread_native.h"
83 #include THREAD_IMPL_H
84 
85 #define RUBY_VM_THREAD_MODEL 2
86 
87 /*
88  * implementation selector of get_insn_info algorithm
89  * 0: linear search
90  * 1: binary search
91  * 2: succinct bitvector
92  */
93 #ifndef VM_INSN_INFO_TABLE_IMPL
94 # define VM_INSN_INFO_TABLE_IMPL 2
95 #endif
96 
97 #if defined(NSIG_MAX) /* POSIX issue 8 */
98 # undef NSIG
99 # define NSIG NSIG_MAX
100 #elif defined(_SIG_MAXSIG) /* FreeBSD */
101 # undef NSIG
102 # define NSIG _SIG_MAXSIG
103 #elif defined(_SIGMAX) /* QNX */
104 # define NSIG (_SIGMAX + 1)
105 #elif defined(NSIG) /* 99% of everything else */
106 # /* take it */
107 #else /* Last resort */
108 # define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
109 #endif
110 
111 #define RUBY_NSIG NSIG
112 
113 #if defined(SIGCLD)
114 # define RUBY_SIGCHLD (SIGCLD)
115 #elif defined(SIGCHLD)
116 # define RUBY_SIGCHLD (SIGCHLD)
117 #else
118 # define RUBY_SIGCHLD (0)
119 #endif
120 
121 /* platforms with broken or non-existent SIGCHLD work by polling */
122 #if defined(__APPLE__)
123 # define SIGCHLD_LOSSY (1)
124 #else
125 # define SIGCHLD_LOSSY (0)
126 #endif
127 
128 /* define to 0 to test old code path */
129 #define WAITPID_USE_SIGCHLD (RUBY_SIGCHLD || SIGCHLD_LOSSY)
130 
131 #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
132 # define USE_SIGALTSTACK
133 void *rb_allocate_sigaltstack(void);
134 void *rb_register_sigaltstack(void *);
135 # define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
136 # define RB_ALTSTACK_FREE(var) free(var)
137 # define RB_ALTSTACK(var) var
138 #else /* noop */
139 # define RB_ALTSTACK_INIT(var, altstack)
140 # define RB_ALTSTACK_FREE(var)
141 # define RB_ALTSTACK(var) (0)
142 #endif
143 
144 /*****************/
145 /* configuration */
146 /*****************/
147 
148 /* gcc ver. check */
149 #if defined(__GNUC__) && __GNUC__ >= 2
150 
151 #if OPT_TOKEN_THREADED_CODE
152 #if OPT_DIRECT_THREADED_CODE
153 #undef OPT_DIRECT_THREADED_CODE
154 #endif
155 #endif
156 
157 #else /* defined(__GNUC__) && __GNUC__ >= 2 */
158 
159 /* disable threaded code options */
160 #if OPT_DIRECT_THREADED_CODE
161 #undef OPT_DIRECT_THREADED_CODE
162 #endif
163 #if OPT_TOKEN_THREADED_CODE
164 #undef OPT_TOKEN_THREADED_CODE
165 #endif
166 #endif
167 
168 /* call threaded code */
169 #if OPT_CALL_THREADED_CODE
170 #if OPT_DIRECT_THREADED_CODE
171 #undef OPT_DIRECT_THREADED_CODE
172 #endif /* OPT_DIRECT_THREADED_CODE */
173 #if OPT_STACK_CACHING
174 #undef OPT_STACK_CACHING
175 #endif /* OPT_STACK_CACHING */
176 #endif /* OPT_CALL_THREADED_CODE */
177 
178 void rb_vm_encoded_insn_data_table_init(void);
179 typedef unsigned long rb_num_t;
180 typedef signed long rb_snum_t;
181 
182 enum ruby_tag_type {
183  RUBY_TAG_NONE = 0x0,
184  RUBY_TAG_RETURN = 0x1,
185  RUBY_TAG_BREAK = 0x2,
186  RUBY_TAG_NEXT = 0x3,
187  RUBY_TAG_RETRY = 0x4,
188  RUBY_TAG_REDO = 0x5,
189  RUBY_TAG_RAISE = 0x6,
190  RUBY_TAG_THROW = 0x7,
191  RUBY_TAG_FATAL = 0x8,
192  RUBY_TAG_MASK = 0xf
193 };
194 
195 #define TAG_NONE RUBY_TAG_NONE
196 #define TAG_RETURN RUBY_TAG_RETURN
197 #define TAG_BREAK RUBY_TAG_BREAK
198 #define TAG_NEXT RUBY_TAG_NEXT
199 #define TAG_RETRY RUBY_TAG_RETRY
200 #define TAG_REDO RUBY_TAG_REDO
201 #define TAG_RAISE RUBY_TAG_RAISE
202 #define TAG_THROW RUBY_TAG_THROW
203 #define TAG_FATAL RUBY_TAG_FATAL
204 #define TAG_MASK RUBY_TAG_MASK
205 
206 enum ruby_vm_throw_flags {
207  VM_THROW_NO_ESCAPE_FLAG = 0x8000,
208  VM_THROW_STATE_MASK = 0xff
209 };
210 
211 /* forward declarations */
212 struct rb_thread_struct;
214 
215 /* iseq data type */
217 
219  rb_serial_t raw;
220  VALUE data[2];
221 };
222 
223 // imemo_constcache
225  VALUE flags;
226 
227  VALUE value; // v0
228  union ic_serial_entry ic_serial; // v1, v2
229  const rb_cref_t *ic_cref; // v3
230 };
231 STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
232  (offsetof(struct iseq_inline_constant_cache_entry, ic_cref) +
233  sizeof(const rb_cref_t *)) <= sizeof(struct RObject));
234 
235 #if SIZEOF_SERIAL_T <= SIZEOF_VALUE
236 
237 #define GET_IC_SERIAL(ice) (ice)->ic_serial.raw
238 #define SET_IC_SERIAL(ice, v) (ice)->ic_serial.raw = (v)
239 
240 #else
241 
242 static inline rb_serial_t
243 get_ic_serial(const struct iseq_inline_constant_cache_entry *ice)
244 {
245  union ic_serial_entry tmp;
246  tmp.data[0] = ice->ic_serial.data[0];
247  tmp.data[1] = ice->ic_serial.data[1];
248  return tmp.raw;
249 }
250 
251 #define GET_IC_SERIAL(ice) get_ic_serial(ice)
252 
253 static inline void
254 set_ic_serial(struct iseq_inline_constant_cache_entry *ice, rb_serial_t v)
255 {
256  union ic_serial_entry tmp;
257  tmp.raw = v;
258  ice->ic_serial.data[0] = tmp.data[0];
259  ice->ic_serial.data[1] = tmp.data[1];
260 }
261 
262 #define SET_IC_SERIAL(ice, v) set_ic_serial((ice), (v))
263 
264 #endif
265 
267  struct iseq_inline_constant_cache_entry *entry;
268  // For YJIT: the index to the opt_getinlinecache instruction in the same iseq.
269  // It's set during compile time and constant once set.
270  unsigned get_insn_idx;
271 };
272 
274  struct rb_iv_index_tbl_entry *entry;
275 };
276 
278  struct rb_cvar_class_tbl_entry *entry;
279 };
280 
282  struct {
283  struct rb_thread_struct *running_thread;
284  VALUE value;
285  } once;
286  struct iseq_inline_constant_cache ic_cache;
287  struct iseq_inline_iv_cache_entry iv_cache;
288 };
289 
291  const struct rb_callinfo *ci;
292  const struct rb_callcache *cc;
293  VALUE block_handler;
294  VALUE recv;
295  int argc;
296  int kw_splat;
297 };
298 
300 
301 #if 1
302 #define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
303 #else
304 #define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
305 #endif
306 #define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
307 
308 typedef struct rb_iseq_location_struct {
309  VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
310  VALUE base_label; /* String */
311  VALUE label; /* String */
312  VALUE first_lineno; /* TODO: may be unsigned short */
313  int node_id;
314  rb_code_location_t code_location;
316 
317 #define PATHOBJ_PATH 0
318 #define PATHOBJ_REALPATH 1
319 
320 static inline VALUE
321 pathobj_path(VALUE pathobj)
322 {
323  if (RB_TYPE_P(pathobj, T_STRING)) {
324  return pathobj;
325  }
326  else {
327  VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
328  return RARRAY_AREF(pathobj, PATHOBJ_PATH);
329  }
330 }
331 
332 static inline VALUE
333 pathobj_realpath(VALUE pathobj)
334 {
335  if (RB_TYPE_P(pathobj, T_STRING)) {
336  return pathobj;
337  }
338  else {
339  VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
340  return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
341  }
342 }
343 
344 /* Forward declarations */
345 struct rb_mjit_unit;
346 
347 // List of YJIT block versions
348 typedef rb_darray(struct yjit_block_version *) rb_yjit_block_array_t;
349 typedef rb_darray(rb_yjit_block_array_t) rb_yjit_block_array_array_t;
350 
352  enum iseq_type {
353  ISEQ_TYPE_TOP,
354  ISEQ_TYPE_METHOD,
355  ISEQ_TYPE_BLOCK,
356  ISEQ_TYPE_CLASS,
357  ISEQ_TYPE_RESCUE,
358  ISEQ_TYPE_ENSURE,
359  ISEQ_TYPE_EVAL,
360  ISEQ_TYPE_MAIN,
361  ISEQ_TYPE_PLAIN
362  } type; /* instruction sequence type */
363 
364  unsigned int iseq_size;
365  VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
366 
390  struct {
391  struct {
392  unsigned int has_lead : 1;
393  unsigned int has_opt : 1;
394  unsigned int has_rest : 1;
395  unsigned int has_post : 1;
396  unsigned int has_kw : 1;
397  unsigned int has_kwrest : 1;
398  unsigned int has_block : 1;
399 
400  unsigned int ambiguous_param0 : 1; /* {|a|} */
401  unsigned int accepts_no_kwarg : 1;
402  unsigned int ruby2_keywords: 1;
403  } flags;
404 
405  unsigned int size;
406 
407  int lead_num;
408  int opt_num;
409  int rest_start;
410  int post_start;
411  int post_num;
412  int block_start;
413 
414  const VALUE *opt_table; /* (opt_num + 1) entries. */
415  /* opt_num and opt_table:
416  *
417  * def foo o1=e1, o2=e2, ..., oN=eN
418  * #=>
419  * # prologue code
420  * A1: e1
421  * A2: e2
422  * ...
423  * AN: eN
424  * AL: body
425  * opt_num = N
426  * opt_table = [A1, A2, ..., AN, AL]
427  */
428 
429  const struct rb_iseq_param_keyword {
430  int num;
431  int required_num;
432  int bits_start;
433  int rest_start;
434  const ID *table;
435  VALUE *default_values;
436  } *keyword;
437  } param;
438 
439  rb_iseq_location_t location;
440 
441  /* insn info, must be freed */
442  struct iseq_insn_info {
443  const struct iseq_insn_info_entry *body;
444  unsigned int *positions;
445  unsigned int size;
446 #if VM_INSN_INFO_TABLE_IMPL == 2
448 #endif
449  } insns_info;
450 
451  const ID *local_table; /* must free */
452 
453  /* catch table */
454  struct iseq_catch_table *catch_table;
455 
456  /* for child iseq */
457  const struct rb_iseq_struct *parent_iseq;
458  struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
459 
460  union iseq_inline_storage_entry *is_entries;
461  struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
462 
463  struct {
464  rb_snum_t flip_count;
465  VALUE script_lines;
466  VALUE coverage;
467  VALUE pc2branchindex;
468  VALUE *original_iseq;
469  } variable;
470 
471  unsigned int local_table_size;
472  unsigned int is_size;
473  unsigned int ci_size;
474  unsigned int stack_max; /* for stack overflow check */
475 
476  char catch_except_p; /* If a frame of this ISeq may catch exception, set TRUE */
477  // If true, this ISeq is leaf *and* backtraces are not used, for example,
478  // by rb_profile_frames. We verify only leafness on VM_CHECK_MODE though.
479  // Note that GC allocations might use backtraces due to
480  // ObjectSpace#trace_object_allocations.
481  // For more details, see: https://bugs.ruby-lang.org/issues/16956
482  bool builtin_inline_p;
483  struct rb_id_table *outer_variables;
484 
485  const rb_iseq_t *mandatory_only_iseq;
486 
487 #if USE_MJIT
488  /* The following fields are MJIT related info. */
489  VALUE (*jit_func)(struct rb_execution_context_struct *,
490  struct rb_control_frame_struct *); /* function pointer for loaded native code */
491  long unsigned total_calls; /* number of total calls with `mjit_exec()` */
492  struct rb_mjit_unit *jit_unit;
493 #endif
494 
495  rb_yjit_block_array_array_t yjit_blocks; // empty, or has a size equal to iseq_size
496 };
497 
498 /* T_IMEMO/iseq */
499 /* typedef rb_iseq_t is in method.h */
501  VALUE flags; /* 1 */
502  VALUE wrapper; /* 2 */
503 
504  struct rb_iseq_constant_body *body; /* 3 */
505 
506  union { /* 4, 5 words */
507  struct iseq_compile_data *compile_data; /* used at compile time */
508 
509  struct {
510  VALUE obj;
511  int index;
512  } loader;
513 
514  struct {
515  struct rb_hook_list_struct *local_hooks;
516  rb_event_flag_t global_trace_events;
517  } exec;
518  } aux;
519 };
520 
521 #ifndef EXTSTATIC
522 #define EXTSTATIC 0
523 #endif
524 
525 #ifndef USE_LAZY_LOAD
526 #define USE_LAZY_LOAD 0
527 #endif
528 
529 #if USE_LAZY_LOAD
530 const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
531 #endif
532 
533 static inline const rb_iseq_t *
534 rb_iseq_check(const rb_iseq_t *iseq)
535 {
536 #if USE_LAZY_LOAD
537  if (iseq->body == NULL) {
538  rb_iseq_complete((rb_iseq_t *)iseq);
539  }
540 #endif
541  return iseq;
542 }
543 
544 static inline const rb_iseq_t *
545 def_iseq_ptr(rb_method_definition_t *def)
546 {
547 //TODO: re-visit. to check the bug, enable this assertion.
548 #if VM_CHECK_MODE > 0
549  if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
550 #endif
551  return rb_iseq_check(def->body.iseq.iseqptr);
552 }
553 
554 enum ruby_special_exceptions {
555  ruby_error_reenter,
556  ruby_error_nomemory,
557  ruby_error_sysstack,
558  ruby_error_stackfatal,
559  ruby_error_stream_closed,
560  ruby_special_error_count
561 };
562 
563 enum ruby_basic_operators {
564  BOP_PLUS,
565  BOP_MINUS,
566  BOP_MULT,
567  BOP_DIV,
568  BOP_MOD,
569  BOP_EQ,
570  BOP_EQQ,
571  BOP_LT,
572  BOP_LE,
573  BOP_LTLT,
574  BOP_AREF,
575  BOP_ASET,
576  BOP_LENGTH,
577  BOP_SIZE,
578  BOP_EMPTY_P,
579  BOP_NIL_P,
580  BOP_SUCC,
581  BOP_GT,
582  BOP_GE,
583  BOP_NOT,
584  BOP_NEQ,
585  BOP_MATCH,
586  BOP_FREEZE,
587  BOP_UMINUS,
588  BOP_MAX,
589  BOP_MIN,
590  BOP_CALL,
591  BOP_AND,
592  BOP_OR,
593 
594  BOP_LAST_
595 };
596 
597 #define GetVMPtr(obj, ptr) \
598  GetCoreDataFromValue((obj), rb_vm_t, (ptr))
599 
600 struct rb_vm_struct;
601 typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
602 
603 typedef struct rb_at_exit_list {
604  rb_vm_at_exit_func *func;
605  struct rb_at_exit_list *next;
607 
608 struct rb_objspace;
609 struct rb_objspace *rb_objspace_alloc(void);
610 void rb_objspace_free(struct rb_objspace *);
611 void rb_objspace_call_finalizer(struct rb_objspace *);
612 
613 typedef struct rb_hook_list_struct {
614  struct rb_event_hook_struct *hooks;
615  rb_event_flag_t events;
616  unsigned int running;
617  bool need_clean;
618  bool is_local;
620 
621 
622 // see builtin.h for definition
623 typedef const struct rb_builtin_function *RB_BUILTIN;
624 
625 typedef struct rb_vm_struct {
626  VALUE self;
627 
628  struct {
629  struct list_head set;
630  unsigned int cnt;
631  unsigned int blocking_cnt;
632 
633  struct rb_ractor_struct *main_ractor;
634  struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
635 
636  struct {
637  // monitor
638  rb_nativethread_lock_t lock;
639  struct rb_ractor_struct *lock_owner;
640  unsigned int lock_rec;
641 
642  // barrier
643  bool barrier_waiting;
644  unsigned int barrier_cnt;
645  rb_nativethread_cond_t barrier_cond;
646 
647  // join at exit
648  rb_nativethread_cond_t terminate_cond;
649  bool terminate_waiting;
650  } sync;
651  } ractor;
652 
653 #ifdef USE_SIGALTSTACK
654  void *main_altstack;
655 #endif
656 
657  rb_serial_t fork_gen;
658  rb_nativethread_lock_t waitpid_lock;
659  struct list_head waiting_pids; /* PID > 0: <=> struct waitpid_state */
660  struct list_head waiting_grps; /* PID <= 0: <=> struct waitpid_state */
661  struct list_head waiting_fds; /* <=> struct waiting_fd */
662 
663  /* set in single-threaded processes only: */
664  volatile int ubf_async_safe;
665 
666  unsigned int running: 1;
667  unsigned int thread_abort_on_exception: 1;
668  unsigned int thread_report_on_exception: 1;
669  unsigned int thread_ignore_deadlock: 1;
670 
671  /* object management */
672  VALUE mark_object_ary;
673  const VALUE special_exceptions[ruby_special_error_count];
674 
675  /* load */
676  VALUE top_self;
677  VALUE load_path;
678  VALUE load_path_snapshot;
679  VALUE load_path_check_cache;
680  VALUE expanded_load_path;
681  VALUE loaded_features;
682  VALUE loaded_features_snapshot;
683  VALUE loaded_features_realpaths;
684  struct st_table *loaded_features_index;
685  struct st_table *loading_table;
686 #if EXTSTATIC
687  // For running the init function of statically linked
688  // extensions when they are loaded
689  struct st_table *static_ext_inits;
690 #endif
691 
692  /* signal */
693  struct {
694  VALUE cmd[RUBY_NSIG];
695  } trap_list;
696 
697  /* relation table of ensure - rollback for callcc */
698  struct st_table *ensure_rollback_table;
699 
700  /* postponed_job (async-signal-safe, NOT thread-safe) */
701  struct rb_postponed_job_struct *postponed_job_buffer;
702  rb_atomic_t postponed_job_index;
703 
704  int src_encoding_index;
705 
706  /* workqueue (thread-safe, NOT async-signal-safe) */
707  struct list_head workqueue; /* <=> rb_workqueue_job.jnode */
708  rb_nativethread_lock_t workqueue_lock;
709 
710  VALUE orig_progname, progname;
711  VALUE coverages, me2counter;
712  int coverage_mode;
713 
714  st_table * defined_module_hash;
715 
716  struct rb_objspace *objspace;
717 
718  rb_at_exit_list *at_exit;
719 
720  st_table *frozen_strings;
721 
722  const struct rb_builtin_function *builtin_function_table;
723  int builtin_inline_index;
724 
725  struct rb_id_table *negative_cme_table;
726  st_table *overloaded_cme_table; // cme -> overloaded_cme
727 
728 #ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
729 #define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
730 #endif
731  const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
732 
733 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
734  uint32_t clock;
735 #endif
736 
737  /* params */
738  struct { /* size in byte */
739  size_t thread_vm_stack_size;
740  size_t thread_machine_stack_size;
741  size_t fiber_vm_stack_size;
742  size_t fiber_machine_stack_size;
743  } default_params;
744 
745  short redefined_flag[BOP_LAST_];
746 } rb_vm_t;
747 
748 /* default values */
749 
750 #define RUBY_VM_SIZE_ALIGN 4096
751 
752 #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
753 #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
754 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
755 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
756 
757 #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
758 #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
759 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
760 #if defined(__powerpc64__)
761 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
762 #else
763 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
764 #endif
765 
766 #if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
767 /* It seems sanitizers consume A LOT of machine stacks */
768 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
769 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
770 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
771 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
772 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
773 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
774 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
775 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
776 #endif
777 
778 /* optimize insn */
779 #define INTEGER_REDEFINED_OP_FLAG (1 << 0)
780 #define FLOAT_REDEFINED_OP_FLAG (1 << 1)
781 #define STRING_REDEFINED_OP_FLAG (1 << 2)
782 #define ARRAY_REDEFINED_OP_FLAG (1 << 3)
783 #define HASH_REDEFINED_OP_FLAG (1 << 4)
784 /* #define BIGNUM_REDEFINED_OP_FLAG (1 << 5) */
785 #define SYMBOL_REDEFINED_OP_FLAG (1 << 6)
786 #define TIME_REDEFINED_OP_FLAG (1 << 7)
787 #define REGEXP_REDEFINED_OP_FLAG (1 << 8)
788 #define NIL_REDEFINED_OP_FLAG (1 << 9)
789 #define TRUE_REDEFINED_OP_FLAG (1 << 10)
790 #define FALSE_REDEFINED_OP_FLAG (1 << 11)
791 #define PROC_REDEFINED_OP_FLAG (1 << 12)
792 
793 #define BASIC_OP_UNREDEFINED_P(op, klass) (LIKELY((GET_VM()->redefined_flag[(op)]&(klass)) == 0))
794 
795 #ifndef VM_DEBUG_BP_CHECK
796 #define VM_DEBUG_BP_CHECK 0
797 #endif
798 
799 #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
800 #define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
801 #endif
802 
804  VALUE self;
805  const VALUE *ep;
806  union {
807  const rb_iseq_t *iseq;
808  const struct vm_ifunc *ifunc;
809  VALUE val;
810  } code;
811 };
812 
813 enum rb_block_handler_type {
814  block_handler_type_iseq,
815  block_handler_type_ifunc,
816  block_handler_type_symbol,
817  block_handler_type_proc
818 };
819 
820 enum rb_block_type {
821  block_type_iseq,
822  block_type_ifunc,
823  block_type_symbol,
824  block_type_proc
825 };
826 
827 struct rb_block {
828  union {
829  struct rb_captured_block captured;
830  VALUE symbol;
831  VALUE proc;
832  } as;
833  enum rb_block_type type;
834 };
835 
836 typedef struct rb_control_frame_struct {
837  const VALUE *pc; /* cfp[0] */
838  VALUE *sp; /* cfp[1] */
839  const rb_iseq_t *iseq; /* cfp[2] */
840  VALUE self; /* cfp[3] / block[0] */
841  const VALUE *ep; /* cfp[4] / block[1] */
842  const void *block_code; /* cfp[5] / block[2] */ /* iseq or ifunc or forwarded block handler */
843  VALUE *__bp__; /* cfp[6] */ /* outside vm_push_frame, use vm_base_ptr instead. */
844 
845 #if VM_DEBUG_BP_CHECK
846  VALUE *bp_check; /* cfp[7] */
847 #endif
848  // Return address for YJIT code
849  void *jit_return;
851 
852 extern const rb_data_type_t ruby_threadptr_data_type;
853 
854 static inline struct rb_thread_struct *
855 rb_thread_ptr(VALUE thval)
856 {
857  return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
858 }
859 
860 enum rb_thread_status {
861  THREAD_RUNNABLE,
862  THREAD_STOPPED,
863  THREAD_STOPPED_FOREVER,
864  THREAD_KILLED
865 };
866 
867 #ifdef RUBY_JMP_BUF
868 typedef RUBY_JMP_BUF rb_jmpbuf_t;
869 #else
870 typedef void *rb_jmpbuf_t[5];
871 #endif
872 
873 /*
874  the members which are written in EC_PUSH_TAG() should be placed at
875  the beginning and the end, so that entire region is accessible.
876 */
877 struct rb_vm_tag {
878  VALUE tag;
879  VALUE retval;
880  rb_jmpbuf_t buf;
881  struct rb_vm_tag *prev;
882  enum ruby_tag_type state;
883  unsigned int lock_rec;
884 };
885 
886 STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
887 STATIC_ASSERT(rb_vm_tag_buf_end,
888  offsetof(struct rb_vm_tag, buf) + sizeof(rb_jmpbuf_t) <
889  sizeof(struct rb_vm_tag));
890 
892  rb_unblock_function_t *func;
893  void *arg;
894 };
895 
896 struct rb_mutex_struct;
897 
898 typedef struct rb_ensure_entry {
899  VALUE marker;
900  VALUE (*e_proc)(VALUE);
901  VALUE data2;
903 
904 typedef struct rb_ensure_list {
905  struct rb_ensure_list *next;
906  struct rb_ensure_entry entry;
908 
909 typedef char rb_thread_id_string_t[sizeof(rb_nativethread_id_t) * 2 + 3];
910 
911 typedef struct rb_fiber_struct rb_fiber_t;
912 
914  struct rb_waiting_list *next;
915  struct rb_thread_struct *thread;
916  struct rb_fiber_struct *fiber;
917 };
918 
920  /* execution information */
921  VALUE *vm_stack; /* must free, must mark */
922  size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
923  rb_control_frame_t *cfp;
924 
925  struct rb_vm_tag *tag;
926 
927  /* interrupt flags */
928  rb_atomic_t interrupt_flag;
929  rb_atomic_t interrupt_mask; /* size should match flag */
930 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
931  uint32_t checked_clock;
932 #endif
933 
934  rb_fiber_t *fiber_ptr;
935  struct rb_thread_struct *thread_ptr;
936 
937  /* storage (ec (fiber) local) */
938  struct rb_id_table *local_storage;
939  VALUE local_storage_recursive_hash;
940  VALUE local_storage_recursive_hash_for_trace;
941 
942  /* eval env */
943  const VALUE *root_lep;
944  VALUE root_svar;
945 
946  /* ensure & callcc */
947  rb_ensure_list_t *ensure_list;
948 
949  /* trace information */
950  struct rb_trace_arg_struct *trace_arg;
951 
952  /* temporary places */
953  VALUE errinfo;
954  VALUE passed_block_handler; /* for rb_iterate */
955 
956  uint8_t raised_flag; /* only 3 bits needed */
957 
958  /* n.b. only 7 bits needed, really: */
959  BITFIELD(enum method_missing_reason, method_missing_reason, 8);
960 
961  VALUE private_const_reference;
962 
963  /* for GC */
964  struct {
965  VALUE *stack_start;
966  VALUE *stack_end;
967  size_t stack_maxsize;
968  RUBY_ALIGNAS(SIZEOF_VALUE) jmp_buf regs;
969  } machine;
970 };
971 
972 #ifndef rb_execution_context_t
974 #define rb_execution_context_t rb_execution_context_t
975 #endif
976 
977 // for builtin.h
978 #define VM_CORE_H_EC_DEFINED 1
979 
980 // Set the vm_stack pointer in the execution context.
981 void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
982 
983 // Initialize the vm_stack pointer in the execution context and push the initial stack frame.
984 // @param ec the execution context to update.
985 // @param stack a pointer to the stack to use.
986 // @param size the size of the stack, as in `VALUE stack[size]`.
987 void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
988 
989 // Clear (set to `NULL`) the vm_stack pointer.
990 // @param ec the execution context to update.
991 void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
992 
994  bool ractor_safe;
995 };
996 
997 typedef struct rb_ractor_struct rb_ractor_t;
998 
999 #if defined(__linux__) || defined(__FreeBSD__)
1000 # define RB_THREAD_T_HAS_NATIVE_ID
1001 #endif
1002 
1003 typedef struct rb_thread_struct {
1004  struct list_node lt_node; // managed by a ractor
1005  VALUE self;
1006  rb_ractor_t *ractor;
1007  rb_vm_t *vm;
1008 
1010 
1011  VALUE last_status; /* $? */
1012 
1013  /* for cfunc */
1014  struct rb_calling_info *calling;
1015 
1016  /* for load(true) */
1017  VALUE top_self;
1018  VALUE top_wrapper;
1019 
1020  /* thread control */
1021  rb_nativethread_id_t thread_id;
1022 #ifdef NON_SCALAR_THREAD_ID
1023  rb_thread_id_string_t thread_id_string;
1024 #endif
1025 #ifdef RB_THREAD_T_HAS_NATIVE_ID
1026  int tid;
1027 #endif
1028  BITFIELD(enum rb_thread_status, status, 2);
1029  /* bit flags */
1030  unsigned int to_kill : 1;
1031  unsigned int abort_on_exception: 1;
1032  unsigned int report_on_exception: 1;
1033  unsigned int pending_interrupt_queue_checked: 1;
1034  int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1035  uint32_t running_time_us; /* 12500..800000 */
1036 
1037  native_thread_data_t native_thread_data;
1038  void *blocking_region_buffer;
1039 
1040  VALUE thgroup;
1041  VALUE value;
1042 
1043  /* temporary place of retval on OPT_CALL_THREADED_CODE */
1044 #if OPT_CALL_THREADED_CODE
1045  VALUE retval;
1046 #endif
1047 
1048  /* async errinfo queue */
1049  VALUE pending_interrupt_queue;
1050  VALUE pending_interrupt_mask_stack;
1051 
1052  /* interrupt management */
1053  rb_nativethread_lock_t interrupt_lock;
1054  struct rb_unblock_callback unblock;
1055  VALUE locking_mutex;
1056  struct rb_mutex_struct *keeping_mutexes;
1057 
1058  struct rb_waiting_list *join_list;
1059 
1060  union {
1061  struct {
1062  VALUE proc;
1063  VALUE args;
1064  int kw_splat;
1065  } proc;
1066  struct {
1067  VALUE (*func)(void *);
1068  void *arg;
1069  } func;
1070  } invoke_arg;
1071 
1072  enum thread_invoke_type {
1073  thread_invoke_type_none = 0,
1074  thread_invoke_type_proc,
1075  thread_invoke_type_ractor_proc,
1076  thread_invoke_type_func
1077  } invoke_type;
1078 
1079  /* statistics data for profiler */
1080  VALUE stat_insn_usage;
1081 
1082  /* fiber */
1083  rb_fiber_t *root_fiber;
1084 
1085  VALUE scheduler;
1086  unsigned blocking;
1087 
1088  /* misc */
1089  VALUE name;
1090 
1091  struct rb_ext_config ext_config;
1092 
1093 #ifdef USE_SIGALTSTACK
1094  void *altstack;
1095 #endif
1096 } rb_thread_t;
1097 
1098 typedef enum {
1099  VM_DEFINECLASS_TYPE_CLASS = 0x00,
1100  VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1101  VM_DEFINECLASS_TYPE_MODULE = 0x02,
1102  /* 0x03..0x06 is reserved */
1103  VM_DEFINECLASS_TYPE_MASK = 0x07
1104 } rb_vm_defineclass_type_t;
1105 
1106 #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1107 #define VM_DEFINECLASS_FLAG_SCOPED 0x08
1108 #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1109 #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1110 #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1111  ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1112 
1113 /* iseq.c */
1114 RUBY_SYMBOL_EXPORT_BEGIN
1115 
1116 /* node -> iseq */
1117 rb_iseq_t *rb_iseq_new (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum iseq_type);
1118 rb_iseq_t *rb_iseq_new_top (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1119 rb_iseq_t *rb_iseq_new_main (const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1120 rb_iseq_t *rb_iseq_new_eval (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_iseq_t *parent, int isolated_depth);
1121 rb_iseq_t *rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_iseq_t *parent, int isolated_depth,
1122  enum iseq_type, const rb_compile_option_t*);
1123 
1124 struct iseq_link_anchor;
1126  VALUE flags;
1127  VALUE reserved;
1128  void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1129  const void *data;
1130 };
1131 static inline struct rb_iseq_new_with_callback_callback_func *
1132 rb_iseq_new_with_callback_new_callback(
1133  void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1134 {
1135  VALUE memo = rb_imemo_new(imemo_ifunc, (VALUE)func, (VALUE)ptr, Qundef, Qfalse);
1136  return (struct rb_iseq_new_with_callback_callback_func *)memo;
1137 }
1138 rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1139  VALUE name, VALUE path, VALUE realpath, VALUE first_lineno,
1140  const rb_iseq_t *parent, enum iseq_type, const rb_compile_option_t*);
1141 
1142 VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1143 int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1144 
1145 VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1146 
1147 RUBY_EXTERN VALUE rb_cISeq;
1148 RUBY_EXTERN VALUE rb_cRubyVM;
1149 RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1150 RUBY_EXTERN VALUE rb_block_param_proxy;
1151 RUBY_SYMBOL_EXPORT_END
1152 
1153 #define GetProcPtr(obj, ptr) \
1154  GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1155 
1156 typedef struct {
1157  const struct rb_block block;
1158  unsigned int is_from_method: 1; /* bool */
1159  unsigned int is_lambda: 1; /* bool */
1160  unsigned int is_isolated: 1; /* bool */
1161 } rb_proc_t;
1162 
1163 RUBY_SYMBOL_EXPORT_BEGIN
1164 VALUE rb_proc_isolate(VALUE self);
1165 VALUE rb_proc_isolate_bang(VALUE self);
1166 VALUE rb_proc_ractor_make_shareable(VALUE self);
1167 RUBY_SYMBOL_EXPORT_END
1168 
1169 typedef struct {
1170  VALUE flags; /* imemo header */
1171  rb_iseq_t *iseq;
1172  const VALUE *ep;
1173  const VALUE *env;
1174  unsigned int env_size;
1175 } rb_env_t;
1176 
1177 extern const rb_data_type_t ruby_binding_data_type;
1178 
1179 #define GetBindingPtr(obj, ptr) \
1180  GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1181 
1182 typedef struct {
1183  const struct rb_block block;
1184  const VALUE pathobj;
1185  unsigned short first_lineno;
1186 } rb_binding_t;
1187 
1188 /* used by compile time and send insn */
1189 
1190 enum vm_check_match_type {
1191  VM_CHECKMATCH_TYPE_WHEN = 1,
1192  VM_CHECKMATCH_TYPE_CASE = 2,
1193  VM_CHECKMATCH_TYPE_RESCUE = 3
1194 };
1195 
1196 #define VM_CHECKMATCH_TYPE_MASK 0x03
1197 #define VM_CHECKMATCH_ARRAY 0x04
1198 
1199 enum vm_special_object_type {
1200  VM_SPECIAL_OBJECT_VMCORE = 1,
1201  VM_SPECIAL_OBJECT_CBASE,
1202  VM_SPECIAL_OBJECT_CONST_BASE
1203 };
1204 
1205 enum vm_svar_index {
1206  VM_SVAR_LASTLINE = 0, /* $_ */
1207  VM_SVAR_BACKREF = 1, /* $~ */
1208 
1209  VM_SVAR_EXTRA_START = 2,
1210  VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1211 };
1212 
1213 /* inline cache */
1214 typedef struct iseq_inline_constant_cache *IC;
1215 typedef struct iseq_inline_iv_cache_entry *IVC;
1216 typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1217 typedef union iseq_inline_storage_entry *ISE;
1218 typedef const struct rb_callinfo *CALL_INFO;
1219 typedef const struct rb_callcache *CALL_CACHE;
1220 typedef struct rb_call_data *CALL_DATA;
1221 
1222 typedef VALUE CDHASH;
1223 
1224 #ifndef FUNC_FASTCALL
1225 #define FUNC_FASTCALL(x) x
1226 #endif
1227 
1228 typedef rb_control_frame_t *
1229  (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1230 
1231 #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1232 #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1233 
1234 #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1235 #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1236 #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1237 
1238 enum {
1239  /* Frame/Environment flag bits:
1240  * MMMM MMMM MMMM MMMM ____ _FFF FFFF EEEX (LSB)
1241  *
1242  * X : tag for GC marking (It seems as Fixnum)
1243  * EEE : 3 bits Env flags
1244  * FF..: 7 bits Frame flags
1245  * MM..: 15 bits frame magic (to check frame corruption)
1246  */
1247 
1248  /* frame types */
1249  VM_FRAME_MAGIC_METHOD = 0x11110001,
1250  VM_FRAME_MAGIC_BLOCK = 0x22220001,
1251  VM_FRAME_MAGIC_CLASS = 0x33330001,
1252  VM_FRAME_MAGIC_TOP = 0x44440001,
1253  VM_FRAME_MAGIC_CFUNC = 0x55550001,
1254  VM_FRAME_MAGIC_IFUNC = 0x66660001,
1255  VM_FRAME_MAGIC_EVAL = 0x77770001,
1256  VM_FRAME_MAGIC_RESCUE = 0x78880001,
1257  VM_FRAME_MAGIC_DUMMY = 0x79990001,
1258 
1259  VM_FRAME_MAGIC_MASK = 0x7fff0001,
1260 
1261  /* frame flag */
1262  VM_FRAME_FLAG_FINISH = 0x0020,
1263  VM_FRAME_FLAG_BMETHOD = 0x0040,
1264  VM_FRAME_FLAG_CFRAME = 0x0080,
1265  VM_FRAME_FLAG_LAMBDA = 0x0100,
1266  VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1267  VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1268  VM_FRAME_FLAG_PASSED = 0x0800,
1269 
1270  /* env flag */
1271  VM_ENV_FLAG_LOCAL = 0x0002,
1272  VM_ENV_FLAG_ESCAPED = 0x0004,
1273  VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1274  VM_ENV_FLAG_ISOLATED = 0x0010,
1275 };
1276 
1277 #define VM_ENV_DATA_SIZE ( 3)
1278 
1279 #define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1280 #define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1281 #define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1282 #define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1283 
1284 #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1285 
1286 static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1287 
1288 static inline void
1289 VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1290 {
1291  VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1292  VM_ASSERT(FIXNUM_P(flags));
1293  VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1294 }
1295 
1296 static inline void
1297 VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1298 {
1299  VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1300  VM_ASSERT(FIXNUM_P(flags));
1301  VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1302 }
1303 
1304 static inline unsigned long
1305 VM_ENV_FLAGS(const VALUE *ep, long flag)
1306 {
1307  VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1308  VM_ASSERT(FIXNUM_P(flags));
1309  return flags & flag;
1310 }
1311 
1312 static inline unsigned long
1313 VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1314 {
1315  return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1316 }
1317 
1318 static inline int
1319 VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1320 {
1321  return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1322 }
1323 
1324 static inline int
1325 VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1326 {
1327  return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1328 }
1329 
1330 static inline int
1331 VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1332 {
1333  return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1334 }
1335 
1336 static inline int
1337 VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1338 {
1339  return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1340 }
1341 
1342 static inline int
1343 rb_obj_is_iseq(VALUE iseq)
1344 {
1345  return imemo_type_p(iseq, imemo_iseq);
1346 }
1347 
1348 #if VM_CHECK_MODE > 0
1349 #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1350 #endif
1351 
1352 static inline int
1353 VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1354 {
1355  int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1356  VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p);
1357  return cframe_p;
1358 }
1359 
1360 static inline int
1361 VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1362 {
1363  return !VM_FRAME_CFRAME_P(cfp);
1364 }
1365 
1366 #define RUBYVM_CFUNC_FRAME_P(cfp) \
1367  (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1368 
1369 #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1370 #define VM_BLOCK_HANDLER_NONE 0
1371 
1372 static inline int
1373 VM_ENV_LOCAL_P(const VALUE *ep)
1374 {
1375  return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1376 }
1377 
1378 static inline const VALUE *
1379 VM_ENV_PREV_EP(const VALUE *ep)
1380 {
1381  VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1382  return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1383 }
1384 
1385 static inline VALUE
1386 VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1387 {
1388  VM_ASSERT(VM_ENV_LOCAL_P(ep));
1389  return ep[VM_ENV_DATA_INDEX_SPECVAL];
1390 }
1391 
1392 #if VM_CHECK_MODE > 0
1393 int rb_vm_ep_in_heap_p(const VALUE *ep);
1394 #endif
1395 
1396 static inline int
1397 VM_ENV_ESCAPED_P(const VALUE *ep)
1398 {
1399  VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1400  return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1401 }
1402 
1403 #if VM_CHECK_MODE > 0
1404 static inline int
1405 vm_assert_env(VALUE obj)
1406 {
1407  VM_ASSERT(imemo_type_p(obj, imemo_env));
1408  return 1;
1409 }
1410 #endif
1411 
1413 static inline VALUE
1414 VM_ENV_ENVVAL(const VALUE *ep)
1415 {
1416  VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1417  VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1418  VM_ASSERT(vm_assert_env(envval));
1419  return envval;
1420 }
1421 
1423 static inline const rb_env_t *
1424 VM_ENV_ENVVAL_PTR(const VALUE *ep)
1425 {
1426  return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1427 }
1428 
1429 static inline const rb_env_t *
1430 vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1431 {
1432  rb_env_t *env = (rb_env_t *)rb_imemo_new(imemo_env, (VALUE)env_ep, (VALUE)env_body, 0, (VALUE)iseq);
1433  env->env_size = env_size;
1434  env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1435  return env;
1436 }
1437 
1438 static inline void
1439 VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1440 {
1441  *((VALUE *)ptr) = v;
1442 }
1443 
1444 static inline void
1445 VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1446 {
1447  VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1448  VM_FORCE_WRITE(ptr, special_const_value);
1449 }
1450 
1451 static inline void
1452 VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1453 {
1454  VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1455  VM_FORCE_WRITE(&ep[index], v);
1456 }
1457 
1458 const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1459 const VALUE *rb_vm_proc_local_ep(VALUE proc);
1460 void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep);
1461 void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src);
1462 
1463 VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1464 
1465 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1466 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1467 
1468 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1469  ((void *)(ecfp) > (void *)(cfp))
1470 
1471 static inline const rb_control_frame_t *
1472 RUBY_VM_END_CONTROL_FRAME(const rb_execution_context_t *ec)
1473 {
1474  return (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size);
1475 }
1476 
1477 static inline int
1478 RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1479 {
1480  return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1481 }
1482 
1483 static inline int
1484 VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1485 {
1486  if ((block_handler & 0x03) == 0x01) {
1487 #if VM_CHECK_MODE > 0
1488  struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1489  VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1490 #endif
1491  return 1;
1492  }
1493  else {
1494  return 0;
1495  }
1496 }
1497 
1498 static inline VALUE
1499 VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1500 {
1501  VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1502  VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1503  return block_handler;
1504 }
1505 
1506 static inline const struct rb_captured_block *
1507 VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1508 {
1509  struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1510  VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1511  return captured;
1512 }
1513 
1514 static inline int
1515 VM_BH_IFUNC_P(VALUE block_handler)
1516 {
1517  if ((block_handler & 0x03) == 0x03) {
1518 #if VM_CHECK_MODE > 0
1519  struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1520  VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1521 #endif
1522  return 1;
1523  }
1524  else {
1525  return 0;
1526  }
1527 }
1528 
1529 static inline VALUE
1530 VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1531 {
1532  VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1533  VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1534  return block_handler;
1535 }
1536 
1537 static inline const struct rb_captured_block *
1538 VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1539 {
1540  struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1541  VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1542  return captured;
1543 }
1544 
1545 static inline const struct rb_captured_block *
1546 VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1547 {
1548  struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1549  VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1550  return captured;
1551 }
1552 
1553 static inline enum rb_block_handler_type
1554 vm_block_handler_type(VALUE block_handler)
1555 {
1556  if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1557  return block_handler_type_iseq;
1558  }
1559  else if (VM_BH_IFUNC_P(block_handler)) {
1560  return block_handler_type_ifunc;
1561  }
1562  else if (SYMBOL_P(block_handler)) {
1563  return block_handler_type_symbol;
1564  }
1565  else {
1566  VM_ASSERT(rb_obj_is_proc(block_handler));
1567  return block_handler_type_proc;
1568  }
1569 }
1570 
1571 static inline void
1572 vm_block_handler_verify(MAYBE_UNUSED(VALUE block_handler))
1573 {
1574  VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1575  (vm_block_handler_type(block_handler), 1));
1576 }
1577 
1578 static inline int
1579 vm_cfp_forwarded_bh_p(const rb_control_frame_t *cfp, VALUE block_handler)
1580 {
1581  return ((VALUE) cfp->block_code) == block_handler;
1582 }
1583 
1584 static inline enum rb_block_type
1585 vm_block_type(const struct rb_block *block)
1586 {
1587 #if VM_CHECK_MODE > 0
1588  switch (block->type) {
1589  case block_type_iseq:
1590  VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1591  break;
1592  case block_type_ifunc:
1593  VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1594  break;
1595  case block_type_symbol:
1596  VM_ASSERT(SYMBOL_P(block->as.symbol));
1597  break;
1598  case block_type_proc:
1599  VM_ASSERT(rb_obj_is_proc(block->as.proc));
1600  break;
1601  }
1602 #endif
1603  return block->type;
1604 }
1605 
1606 static inline void
1607 vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1608 {
1609  struct rb_block *mb = (struct rb_block *)block;
1610  mb->type = type;
1611 }
1612 
1613 static inline const struct rb_block *
1614 vm_proc_block(VALUE procval)
1615 {
1616  VM_ASSERT(rb_obj_is_proc(procval));
1617  return &((rb_proc_t *)RTYPEDDATA_DATA(procval))->block;
1618 }
1619 
1620 static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1621 static inline const VALUE *vm_block_ep(const struct rb_block *block);
1622 
1623 static inline const rb_iseq_t *
1624 vm_proc_iseq(VALUE procval)
1625 {
1626  return vm_block_iseq(vm_proc_block(procval));
1627 }
1628 
1629 static inline const VALUE *
1630 vm_proc_ep(VALUE procval)
1631 {
1632  return vm_block_ep(vm_proc_block(procval));
1633 }
1634 
1635 static inline const rb_iseq_t *
1636 vm_block_iseq(const struct rb_block *block)
1637 {
1638  switch (vm_block_type(block)) {
1639  case block_type_iseq: return rb_iseq_check(block->as.captured.code.iseq);
1640  case block_type_proc: return vm_proc_iseq(block->as.proc);
1641  case block_type_ifunc:
1642  case block_type_symbol: return NULL;
1643  }
1644  VM_UNREACHABLE(vm_block_iseq);
1645  return NULL;
1646 }
1647 
1648 static inline const VALUE *
1649 vm_block_ep(const struct rb_block *block)
1650 {
1651  switch (vm_block_type(block)) {
1652  case block_type_iseq:
1653  case block_type_ifunc: return block->as.captured.ep;
1654  case block_type_proc: return vm_proc_ep(block->as.proc);
1655  case block_type_symbol: return NULL;
1656  }
1657  VM_UNREACHABLE(vm_block_ep);
1658  return NULL;
1659 }
1660 
1661 static inline VALUE
1662 vm_block_self(const struct rb_block *block)
1663 {
1664  switch (vm_block_type(block)) {
1665  case block_type_iseq:
1666  case block_type_ifunc:
1667  return block->as.captured.self;
1668  case block_type_proc:
1669  return vm_block_self(vm_proc_block(block->as.proc));
1670  case block_type_symbol:
1671  return Qundef;
1672  }
1673  VM_UNREACHABLE(vm_block_self);
1674  return Qundef;
1675 }
1676 
1677 static inline VALUE
1678 VM_BH_TO_SYMBOL(VALUE block_handler)
1679 {
1680  VM_ASSERT(SYMBOL_P(block_handler));
1681  return block_handler;
1682 }
1683 
1684 static inline VALUE
1685 VM_BH_FROM_SYMBOL(VALUE symbol)
1686 {
1687  VM_ASSERT(SYMBOL_P(symbol));
1688  return symbol;
1689 }
1690 
1691 static inline VALUE
1692 VM_BH_TO_PROC(VALUE block_handler)
1693 {
1694  VM_ASSERT(rb_obj_is_proc(block_handler));
1695  return block_handler;
1696 }
1697 
1698 static inline VALUE
1699 VM_BH_FROM_PROC(VALUE procval)
1700 {
1701  VM_ASSERT(rb_obj_is_proc(procval));
1702  return procval;
1703 }
1704 
1705 /* VM related object allocate functions */
1706 VALUE rb_thread_alloc(VALUE klass);
1707 VALUE rb_binding_alloc(VALUE klass);
1708 VALUE rb_proc_alloc(VALUE klass);
1709 VALUE rb_proc_dup(VALUE self);
1710 
1711 /* for debug */
1712 extern void rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1713 extern void rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc);
1714 extern void rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp
1715 #if OPT_STACK_CACHING
1716  , VALUE reg_a, VALUE reg_b
1717 #endif
1718 );
1719 
1720 #define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp)
1721 #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp))
1722 void rb_vm_bugreport(const void *);
1723 typedef void (*ruby_sighandler_t)(int);
1724 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 5)
1725 NORETURN(void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *, const char *fmt, ...));
1726 
1727 /* functions about thread/vm execution */
1728 RUBY_SYMBOL_EXPORT_BEGIN
1729 VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1730 VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1731 VALUE rb_iseq_path(const rb_iseq_t *iseq);
1732 VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
1733 RUBY_SYMBOL_EXPORT_END
1734 
1735 VALUE rb_iseq_pathobj_new(VALUE path, VALUE realpath);
1736 void rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath);
1737 
1738 int rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp);
1739 void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause);
1740 
1741 VALUE rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, int argc, const VALUE *argv, int kw_splat, VALUE block_handler);
1742 
1743 VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
1744 static inline VALUE
1745 rb_vm_make_proc(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1746 {
1747  return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1748 }
1749 
1750 static inline VALUE
1751 rb_vm_make_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1752 {
1753  return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1754 }
1755 
1756 VALUE rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp);
1757 VALUE rb_vm_env_local_variables(const rb_env_t *env);
1758 const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
1759 const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars);
1760 void rb_vm_inc_const_missing_count(void);
1761 VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc,
1762  const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat);
1763 MJIT_STATIC void rb_vm_pop_frame(rb_execution_context_t *ec);
1764 
1765 void rb_gvl_destroy(rb_global_vm_lock_t *gvl);
1766 
1767 void rb_thread_start_timer_thread(void);
1768 void rb_thread_stop_timer_thread(void);
1769 void rb_thread_reset_timer_thread(void);
1770 void rb_thread_wakeup_timer_thread(int);
1771 
1772 static inline void
1773 rb_vm_living_threads_init(rb_vm_t *vm)
1774 {
1775  list_head_init(&vm->waiting_fds);
1776  list_head_init(&vm->waiting_pids);
1777  list_head_init(&vm->workqueue);
1778  list_head_init(&vm->waiting_grps);
1779  list_head_init(&vm->ractor.set);
1780 }
1781 
1782 typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
1783 rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1784 rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1785 int rb_vm_get_sourceline(const rb_control_frame_t *);
1786 void rb_vm_stack_to_heap(rb_execution_context_t *ec);
1787 void ruby_thread_init_stack(rb_thread_t *th);
1788 rb_thread_t * ruby_thread_from_native(void);
1789 int ruby_thread_set_native(rb_thread_t *th);
1790 int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
1791 void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp);
1792 MJIT_STATIC VALUE rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler);
1793 
1794 void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE exception_class, VALUE mesg);
1795 
1796 #define rb_vm_register_special_exception(sp, e, m) \
1797  rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1798 
1799 void rb_gc_mark_machine_stack(const rb_execution_context_t *ec);
1800 
1801 void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);
1802 
1803 MJIT_STATIC const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
1804 
1805 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1806 
1807 #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1808  STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1809  STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1810  const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1811  if (UNLIKELY((cfp) <= &bound[1])) { \
1812  vm_stackoverflow(); \
1813  } \
1814 } while (0)
1815 
1816 #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1817  CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1818 
1819 VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr);
1820 
1821 rb_execution_context_t *rb_vm_main_ractor_ec(rb_vm_t *vm); // ractor.c
1822 
1823 /* for thread */
1824 
1825 #if RUBY_VM_THREAD_MODEL == 2
1826 MJIT_SYMBOL_EXPORT_BEGIN
1827 
1828 RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
1829 RUBY_EXTERN rb_vm_t *ruby_current_vm_ptr;
1830 RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
1831 RUBY_EXTERN rb_event_flag_t ruby_vm_event_enabled_global_flags;
1832 RUBY_EXTERN unsigned int ruby_vm_event_local_num;
1833 
1834 MJIT_SYMBOL_EXPORT_END
1835 
1836 #define GET_VM() rb_current_vm()
1837 #define GET_RACTOR() rb_current_ractor()
1838 #define GET_THREAD() rb_current_thread()
1839 #define GET_EC() rb_current_execution_context(true)
1840 
1841 static inline rb_thread_t *
1842 rb_ec_thread_ptr(const rb_execution_context_t *ec)
1843 {
1844  return ec->thread_ptr;
1845 }
1846 
1847 static inline rb_ractor_t *
1848 rb_ec_ractor_ptr(const rb_execution_context_t *ec)
1849 {
1850  const rb_thread_t *th = rb_ec_thread_ptr(ec);
1851  if (th) {
1852  VM_ASSERT(th->ractor != NULL);
1853  return th->ractor;
1854  }
1855  else {
1856  return NULL;
1857  }
1858 }
1859 
1860 static inline rb_vm_t *
1861 rb_ec_vm_ptr(const rb_execution_context_t *ec)
1862 {
1863  const rb_thread_t *th = rb_ec_thread_ptr(ec);
1864  if (th) {
1865  return th->vm;
1866  }
1867  else {
1868  return NULL;
1869  }
1870 }
1871 
1872 static inline rb_execution_context_t *
1873 rb_current_execution_context(bool expect_ec)
1874 {
1875 #ifdef RB_THREAD_LOCAL_SPECIFIER
1876  #ifdef __APPLE__
1877  rb_execution_context_t *ec = rb_current_ec();
1878  #else
1879  rb_execution_context_t *ec = ruby_current_ec;
1880  #endif
1881 #else
1882  rb_execution_context_t *ec = native_tls_get(ruby_current_ec_key);
1883 #endif
1884  VM_ASSERT(!expect_ec || ec != NULL);
1885  return ec;
1886 }
1887 
1888 static inline rb_thread_t *
1889 rb_current_thread(void)
1890 {
1891  const rb_execution_context_t *ec = GET_EC();
1892  return rb_ec_thread_ptr(ec);
1893 }
1894 
1895 static inline rb_ractor_t *
1896 rb_current_ractor(void)
1897 {
1898  if (ruby_single_main_ractor) {
1899  return ruby_single_main_ractor;
1900  }
1901  else {
1902  const rb_execution_context_t *ec = GET_EC();
1903  return rb_ec_ractor_ptr(ec);
1904  }
1905 }
1906 
1907 static inline rb_vm_t *
1908 rb_current_vm(void)
1909 {
1910 #if 0 // TODO: reconsider the assertions
1911  VM_ASSERT(ruby_current_vm_ptr == NULL ||
1912  ruby_current_execution_context_ptr == NULL ||
1913  rb_ec_thread_ptr(GET_EC()) == NULL ||
1914  rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
1915  rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
1916 #endif
1917 
1918  return ruby_current_vm_ptr;
1919 }
1920 
1921 void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
1922  unsigned int recorded_lock_rec,
1923  unsigned int current_lock_rec);
1924 
1925 static inline unsigned int
1926 rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
1927 {
1928  rb_vm_t *vm = rb_ec_vm_ptr(ec);
1929 
1930  if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
1931  return 0;
1932  }
1933  else {
1934  return vm->ractor.sync.lock_rec;
1935  }
1936 }
1937 
1938 #else
1939 #error "unsupported thread model"
1940 #endif
1941 
1942 enum {
1943  TIMER_INTERRUPT_MASK = 0x01,
1944  PENDING_INTERRUPT_MASK = 0x02,
1945  POSTPONED_JOB_INTERRUPT_MASK = 0x04,
1946  TRAP_INTERRUPT_MASK = 0x08,
1947  TERMINATE_INTERRUPT_MASK = 0x10,
1948  VM_BARRIER_INTERRUPT_MASK = 0x20,
1949 };
1950 
1951 #define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
1952 #define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
1953 #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
1954 #define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
1955 #define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
1956 #define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
1957 #define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
1958  (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
1959 
1960 static inline bool
1961 RUBY_VM_INTERRUPTED_ANY(rb_execution_context_t *ec)
1962 {
1963 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1964  uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
1965 
1966  if (current_clock != ec->checked_clock) {
1967  ec->checked_clock = current_clock;
1968  RUBY_VM_SET_TIMER_INTERRUPT(ec);
1969  }
1970 #endif
1971  return ec->interrupt_flag & ~(ec)->interrupt_mask;
1972 }
1973 
1974 VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
1975 int rb_signal_buff_size(void);
1976 int rb_signal_exec(rb_thread_t *th, int sig);
1977 void rb_threadptr_check_signal(rb_thread_t *mth);
1978 void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
1979 void rb_threadptr_signal_exit(rb_thread_t *th);
1980 int rb_threadptr_execute_interrupts(rb_thread_t *, int);
1981 void rb_threadptr_interrupt(rb_thread_t *th);
1982 void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
1983 void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
1984 void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
1985 VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec);
1986 void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo);
1987 void rb_execution_context_update(const rb_execution_context_t *ec);
1988 void rb_execution_context_mark(const rb_execution_context_t *ec);
1989 void rb_fiber_close(rb_fiber_t *fib);
1990 void Init_native_thread(rb_thread_t *th);
1991 int rb_vm_check_ints_blocking(rb_execution_context_t *ec);
1992 
1993 // vm_sync.h
1994 void rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond);
1995 void rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec);
1996 
1997 #define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
1998 static inline void
1999 rb_vm_check_ints(rb_execution_context_t *ec)
2000 {
2001  VM_ASSERT(ec == GET_EC());
2002  if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2003  rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2004  }
2005 }
2006 
2007 /* tracer */
2008 
2010  rb_event_flag_t event;
2012  const rb_control_frame_t *cfp;
2013  VALUE self;
2014  ID id;
2015  ID called_id;
2016  VALUE klass;
2017  VALUE data;
2018 
2019  int klass_solved;
2020 
2021  /* calc from cfp */
2022  int lineno;
2023  VALUE path;
2024 };
2025 
2026 void rb_hook_list_mark(rb_hook_list_t *hooks);
2027 void rb_hook_list_free(rb_hook_list_t *hooks);
2028 void rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line);
2029 void rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval);
2030 
2031 void rb_exec_event_hooks(struct rb_trace_arg_struct *trace_arg, rb_hook_list_t *hooks, int pop_p);
2032 
2033 #define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2034  const rb_event_flag_t flag_arg_ = (flag_); \
2035  rb_hook_list_t *hooks_arg_ = (hooks_); \
2036  if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2037  /* defer evaluating the other arguments */ \
2038  rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2039  } \
2040 } while (0)
2041 
2042 static inline void
2043 rb_exec_event_hook_orig(rb_execution_context_t *ec, rb_hook_list_t *hooks, rb_event_flag_t flag,
2044  VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
2045 {
2046  struct rb_trace_arg_struct trace_arg;
2047 
2048  VM_ASSERT((hooks->events & flag) != 0);
2049 
2050  trace_arg.event = flag;
2051  trace_arg.ec = ec;
2052  trace_arg.cfp = ec->cfp;
2053  trace_arg.self = self;
2054  trace_arg.id = id;
2055  trace_arg.called_id = called_id;
2056  trace_arg.klass = klass;
2057  trace_arg.data = data;
2058  trace_arg.path = Qundef;
2059  trace_arg.klass_solved = 0;
2060 
2061  rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2062 }
2063 
2065  VALUE self;
2066  uint32_t id;
2067  rb_hook_list_t hooks;
2068 };
2069 
2070 static inline rb_hook_list_t *
2071 rb_ec_ractor_hooks(const rb_execution_context_t *ec)
2072 {
2073  struct rb_ractor_pub *cr_pub = (struct rb_ractor_pub *)rb_ec_ractor_ptr(ec);
2074  return &cr_pub->hooks;
2075 }
2076 
2077 #define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2078  EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2079 
2080 #define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2081  EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2082 
2083 static inline void
2084 rb_exec_event_hook_script_compiled(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE eval_script)
2085 {
2086  EXEC_EVENT_HOOK(ec, RUBY_EVENT_SCRIPT_COMPILED, ec->cfp->self, 0, 0, 0,
2087  NIL_P(eval_script) ? (VALUE)iseq :
2088  rb_ary_new_from_args(2, eval_script, (VALUE)iseq));
2089 }
2090 
2091 void rb_vm_trap_exit(rb_vm_t *vm);
2092 
2093 RUBY_SYMBOL_EXPORT_BEGIN
2094 
2095 int rb_thread_check_trap_pending(void);
2096 
2097 /* #define RUBY_EVENT_RESERVED_FOR_INTERNAL_USE 0x030000 */ /* from vm_core.h */
2098 #define RUBY_EVENT_COVERAGE_LINE 0x010000
2099 #define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2100 
2101 extern VALUE rb_get_coverages(void);
2102 extern void rb_set_coverages(VALUE, int, VALUE);
2103 extern void rb_clear_coverages(void);
2104 extern void rb_reset_coverages(void);
2105 extern void rb_resume_coverages(void);
2106 extern void rb_suspend_coverages(void);
2107 
2108 void rb_postponed_job_flush(rb_vm_t *vm);
2109 
2110 // ractor.c
2111 RUBY_EXTERN VALUE rb_eRactorUnsafeError;
2112 RUBY_EXTERN VALUE rb_eRactorIsolationError;
2113 
2114 RUBY_SYMBOL_EXPORT_END
2115 
2116 #endif /* RUBY_VM_CORE_H */
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition: atomic.h:69
#define RUBY_ALIGNAS
Wraps (or simulates) alignas.
Definition: stdalign.h:27
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
#define RUBY_EVENT_SCRIPT_COMPILED
Encountered an eval.
Definition: event.h:56
uint32_t rb_event_flag_t
Represents event(s).
Definition: event.h:103
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition: format.h:27
#define T_STRING
Old name of RUBY_T_STRING.
Definition: value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition: value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition: value_type.h:88
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
Definition: error.c:1066
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:802
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
Definition: array.c:756
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition: proc.c:175
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition: thread.h:336
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
This is the type of a function that the interpreter expect for C-backended blocks.
Definition: iterator.h:83
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
#define RARRAY_AREF(a, i)
Definition: rarray.h:588
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition: rtypeddata.h:102
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Defines old _.
C99 shim for <stdbool.h>
Ruby's ordinal objects.
Definition: robject.h:93
Definition: vm_core.h:224
Definition: vm_core.h:277
Definition: vm_core.h:273
Definition: iseq.h:228
Definition: method.h:62
CREF (Class REFerence)
Definition: method.h:44
Definition: class.h:34
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:190
Definition: vm_core.h:898
struct rb_iseq_constant_body::@152 param
parameter information
Definition: class.h:28
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition: method.h:135
Definition: st.h:79
IFUNC (Internal FUNCtion)
Definition: imemo.h:84
Basic block version Represents a portion of an iseq compiled with a given context Note: care must be ...
Definition: yjit_core.h:237
Definition: vm_core.h:218
Definition: vm_core.h:281
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition: value.h:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
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:375