[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / vm_core.h
blob30e06c48ede53433195d3f4ecaeea26df0312991
1 #ifndef RUBY_VM_CORE_H
2 #define RUBY_VM_CORE_H
3 /**********************************************************************
5 vm_core.h -
7 $Author$
8 created at: 04/01/01 19:41:38 JST
10 Copyright (C) 2004-2007 Koichi Sasada
12 **********************************************************************/
15 * Enable check mode.
16 * 1: enable local assertions.
18 #ifndef VM_CHECK_MODE
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)
23 #define VM_CHECK_MODE N_OR_RUBY_DEBUG(0)
24 #endif
26 /**
27 * VM Debug Level
29 * debug level:
30 * 0: no debug output
31 * 1: show instruction name
32 * 2: show stack frame when control stack frame is changed
33 * 3: show stack status
34 * 4: show register
35 * 5:
36 * 10: gc check
39 #ifndef VMDEBUG
40 #define VMDEBUG 0
41 #endif
43 #if 0
44 #undef VMDEBUG
45 #define VMDEBUG 3
46 #endif
48 #include "ruby/internal/config.h"
50 #include <stddef.h>
51 #include <signal.h>
52 #include <stdarg.h>
54 #include "ruby_assert.h"
56 #define RVALUE_SIZE (sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]))
58 #if VM_CHECK_MODE > 0
59 #define VM_ASSERT(/*expr, */...) RUBY_ASSERT_WHEN(VM_CHECK_MODE > 0, __VA_ARGS__)
60 #define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
61 #define RUBY_ASSERT_CRITICAL_SECTION
62 #define RUBY_DEBUG_THREAD_SCHEDULE() rb_thread_schedule()
63 #else
64 #define VM_ASSERT(/*expr, */...) ((void)0)
65 #define VM_UNREACHABLE(func) UNREACHABLE
66 #define RUBY_DEBUG_THREAD_SCHEDULE()
67 #endif
69 #define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
71 #if defined(RUBY_ASSERT_CRITICAL_SECTION)
72 // TODO add documentation
73 extern int ruby_assert_critical_section_entered;
74 #define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{ruby_assert_critical_section_entered += 1;}while(false)
75 #define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{VM_ASSERT(ruby_assert_critical_section_entered > 0);ruby_assert_critical_section_entered -= 1;}while(false)
76 #else
77 #define RUBY_ASSERT_CRITICAL_SECTION_ENTER()
78 #define RUBY_ASSERT_CRITICAL_SECTION_LEAVE()
79 #endif
81 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
82 # include "wasm/setjmp.h"
83 #else
84 # include <setjmp.h>
85 #endif
87 #if defined(__linux__) || defined(__FreeBSD__)
88 # define RB_THREAD_T_HAS_NATIVE_ID
89 #endif
91 #include "ruby/internal/stdbool.h"
92 #include "ccan/list/list.h"
93 #include "id.h"
94 #include "internal.h"
95 #include "internal/array.h"
96 #include "internal/basic_operators.h"
97 #include "internal/sanitizers.h"
98 #include "internal/serial.h"
99 #include "internal/vm.h"
100 #include "method.h"
101 #include "node.h"
102 #include "ruby/ruby.h"
103 #include "ruby/st.h"
104 #include "ruby_atomic.h"
105 #include "vm_opts.h"
107 #include "ruby/thread_native.h"
109 #if USE_SHARED_GC
110 typedef struct gc_function_map {
111 void *(*objspace_alloc)(void);
112 } rb_gc_function_map_t;
114 #define rb_gc_functions (&GET_VM()->gc_functions_map)
115 #endif
118 * implementation selector of get_insn_info algorithm
119 * 0: linear search
120 * 1: binary search
121 * 2: succinct bitvector
123 #ifndef VM_INSN_INFO_TABLE_IMPL
124 # define VM_INSN_INFO_TABLE_IMPL 2
125 #endif
127 #if defined(NSIG_MAX) /* POSIX issue 8 */
128 # undef NSIG
129 # define NSIG NSIG_MAX
130 #elif defined(_SIG_MAXSIG) /* FreeBSD */
131 # undef NSIG
132 # define NSIG _SIG_MAXSIG
133 #elif defined(_SIGMAX) /* QNX */
134 # define NSIG (_SIGMAX + 1)
135 #elif defined(NSIG) /* 99% of everything else */
136 # /* take it */
137 #else /* Last resort */
138 # define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
139 #endif
141 #define RUBY_NSIG NSIG
143 #if defined(SIGCLD)
144 # define RUBY_SIGCHLD (SIGCLD)
145 #elif defined(SIGCHLD)
146 # define RUBY_SIGCHLD (SIGCHLD)
147 #endif
149 #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
150 # define USE_SIGALTSTACK
151 void *rb_allocate_sigaltstack(void);
152 void *rb_register_sigaltstack(void *);
153 # define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
154 # define RB_ALTSTACK_FREE(var) free(var)
155 # define RB_ALTSTACK(var) var
156 #else /* noop */
157 # define RB_ALTSTACK_INIT(var, altstack)
158 # define RB_ALTSTACK_FREE(var)
159 # define RB_ALTSTACK(var) (0)
160 #endif
162 #include THREAD_IMPL_H
163 #define RUBY_VM_THREAD_MODEL 2
165 /*****************/
166 /* configuration */
167 /*****************/
169 /* gcc ver. check */
170 #if defined(__GNUC__) && __GNUC__ >= 2
172 #if OPT_TOKEN_THREADED_CODE
173 #if OPT_DIRECT_THREADED_CODE
174 #undef OPT_DIRECT_THREADED_CODE
175 #endif
176 #endif
178 #else /* defined(__GNUC__) && __GNUC__ >= 2 */
180 /* disable threaded code options */
181 #if OPT_DIRECT_THREADED_CODE
182 #undef OPT_DIRECT_THREADED_CODE
183 #endif
184 #if OPT_TOKEN_THREADED_CODE
185 #undef OPT_TOKEN_THREADED_CODE
186 #endif
187 #endif
189 /* call threaded code */
190 #if OPT_CALL_THREADED_CODE
191 #if OPT_DIRECT_THREADED_CODE
192 #undef OPT_DIRECT_THREADED_CODE
193 #endif /* OPT_DIRECT_THREADED_CODE */
194 #endif /* OPT_CALL_THREADED_CODE */
196 void rb_vm_encoded_insn_data_table_init(void);
197 typedef unsigned long rb_num_t;
198 typedef signed long rb_snum_t;
200 enum ruby_tag_type {
201 RUBY_TAG_NONE = 0x0,
202 RUBY_TAG_RETURN = 0x1,
203 RUBY_TAG_BREAK = 0x2,
204 RUBY_TAG_NEXT = 0x3,
205 RUBY_TAG_RETRY = 0x4,
206 RUBY_TAG_REDO = 0x5,
207 RUBY_TAG_RAISE = 0x6,
208 RUBY_TAG_THROW = 0x7,
209 RUBY_TAG_FATAL = 0x8,
210 RUBY_TAG_MASK = 0xf
213 #define TAG_NONE RUBY_TAG_NONE
214 #define TAG_RETURN RUBY_TAG_RETURN
215 #define TAG_BREAK RUBY_TAG_BREAK
216 #define TAG_NEXT RUBY_TAG_NEXT
217 #define TAG_RETRY RUBY_TAG_RETRY
218 #define TAG_REDO RUBY_TAG_REDO
219 #define TAG_RAISE RUBY_TAG_RAISE
220 #define TAG_THROW RUBY_TAG_THROW
221 #define TAG_FATAL RUBY_TAG_FATAL
222 #define TAG_MASK RUBY_TAG_MASK
224 enum ruby_vm_throw_flags {
225 VM_THROW_NO_ESCAPE_FLAG = 0x8000,
226 VM_THROW_STATE_MASK = 0xff
229 /* forward declarations */
230 struct rb_thread_struct;
231 struct rb_control_frame_struct;
233 /* iseq data type */
234 typedef struct rb_compile_option_struct rb_compile_option_t;
236 union ic_serial_entry {
237 rb_serial_t raw;
238 VALUE data[2];
241 // imemo_constcache
242 struct iseq_inline_constant_cache_entry {
243 VALUE flags;
245 VALUE value; // v0
246 VALUE _unused1; // v1
247 VALUE _unused2; // v2
248 const rb_cref_t *ic_cref; // v3
250 STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
251 (offsetof(struct iseq_inline_constant_cache_entry, ic_cref) +
252 sizeof(const rb_cref_t *)) <= RVALUE_SIZE);
254 struct iseq_inline_constant_cache {
255 struct iseq_inline_constant_cache_entry *entry;
258 * A null-terminated list of ids, used to represent a constant's path
259 * idNULL is used to represent the :: prefix, and 0 is used to donate the end
260 * of the list.
262 * For example
263 * FOO {rb_intern("FOO"), 0}
264 * FOO::BAR {rb_intern("FOO"), rb_intern("BAR"), 0}
265 * ::FOO {idNULL, rb_intern("FOO"), 0}
266 * ::FOO::BAR {idNULL, rb_intern("FOO"), rb_intern("BAR"), 0}
268 const ID *segments;
271 struct iseq_inline_iv_cache_entry {
272 uintptr_t value; // attr_index in lower bits, dest_shape_id in upper bits
273 ID iv_set_name;
276 struct iseq_inline_cvar_cache_entry {
277 struct rb_cvar_class_tbl_entry *entry;
280 union iseq_inline_storage_entry {
281 struct {
282 struct rb_thread_struct *running_thread;
283 VALUE value;
284 } once;
285 struct iseq_inline_constant_cache ic_cache;
286 struct iseq_inline_iv_cache_entry iv_cache;
289 struct rb_calling_info {
290 const struct rb_call_data *cd;
291 const struct rb_callcache *cc;
292 VALUE block_handler;
293 VALUE recv;
294 int argc;
295 bool kw_splat;
296 VALUE heap_argv;
299 #ifndef VM_ARGC_STACK_MAX
300 #define VM_ARGC_STACK_MAX 128
301 #endif
303 # define CALLING_ARGC(calling) ((calling)->heap_argv ? RARRAY_LENINT((calling)->heap_argv) : (calling)->argc)
305 struct rb_execution_context_struct;
307 #if 1
308 #define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
309 #else
310 #define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
311 #endif
312 #define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
314 typedef struct rb_iseq_location_struct {
315 VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
316 VALUE base_label; /* String */
317 VALUE label; /* String */
318 int first_lineno;
319 int node_id;
320 rb_code_location_t code_location;
321 } rb_iseq_location_t;
323 #define PATHOBJ_PATH 0
324 #define PATHOBJ_REALPATH 1
326 static inline VALUE
327 pathobj_path(VALUE pathobj)
329 if (RB_TYPE_P(pathobj, T_STRING)) {
330 return pathobj;
332 else {
333 VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
334 return RARRAY_AREF(pathobj, PATHOBJ_PATH);
338 static inline VALUE
339 pathobj_realpath(VALUE pathobj)
341 if (RB_TYPE_P(pathobj, T_STRING)) {
342 return pathobj;
344 else {
345 VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
346 return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
350 /* Forward declarations */
351 struct rb_rjit_unit;
353 typedef uintptr_t iseq_bits_t;
355 #define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
357 /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
358 #define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
360 /* instruction sequence type */
361 enum rb_iseq_type {
362 ISEQ_TYPE_TOP,
363 ISEQ_TYPE_METHOD,
364 ISEQ_TYPE_BLOCK,
365 ISEQ_TYPE_CLASS,
366 ISEQ_TYPE_RESCUE,
367 ISEQ_TYPE_ENSURE,
368 ISEQ_TYPE_EVAL,
369 ISEQ_TYPE_MAIN,
370 ISEQ_TYPE_PLAIN
373 // Attributes specified by Primitive.attr!
374 enum rb_builtin_attr {
375 // The iseq does not call methods.
376 BUILTIN_ATTR_LEAF = 0x01,
377 // This iseq only contains single `opt_invokebuiltin_delegate_leave` instruction with 0 arguments.
378 BUILTIN_ATTR_SINGLE_NOARG_LEAF = 0x02,
379 // This attribute signals JIT to duplicate the iseq for each block iseq so that its `yield` will be monomorphic.
380 BUILTIN_ATTR_INLINE_BLOCK = 0x04,
383 typedef VALUE (*rb_jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *);
385 struct rb_iseq_constant_body {
386 enum rb_iseq_type type;
388 unsigned int iseq_size;
389 VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
392 * parameter information
394 * def m(a1, a2, ..., aM, # mandatory
395 * b1=(...), b2=(...), ..., bN=(...), # optional
396 * *c, # rest
397 * d1, d2, ..., dO, # post
398 * e1:(...), e2:(...), ..., eK:(...), # keyword
399 * **f, # keyword_rest
400 * &g) # block
401 * =>
403 * lead_num = M
404 * opt_num = N
405 * rest_start = M+N
406 * post_start = M+N+(*1)
407 * post_num = O
408 * keyword_num = K
409 * block_start = M+N+(*1)+O+K
410 * keyword_bits = M+N+(*1)+O+K+(&1)
411 * size = M+N+O+(*1)+K+(&1)+(**1) // parameter size.
414 struct {
415 struct {
416 unsigned int has_lead : 1;
417 unsigned int has_opt : 1;
418 unsigned int has_rest : 1;
419 unsigned int has_post : 1;
420 unsigned int has_kw : 1;
421 unsigned int has_kwrest : 1;
422 unsigned int has_block : 1;
424 unsigned int ambiguous_param0 : 1; /* {|a|} */
425 unsigned int accepts_no_kwarg : 1;
426 unsigned int ruby2_keywords: 1;
427 unsigned int anon_rest: 1;
428 unsigned int anon_kwrest: 1;
429 unsigned int use_block: 1;
430 } flags;
432 unsigned int size;
434 int lead_num;
435 int opt_num;
436 int rest_start;
437 int post_start;
438 int post_num;
439 int block_start;
441 const VALUE *opt_table; /* (opt_num + 1) entries. */
442 /* opt_num and opt_table:
444 * def foo o1=e1, o2=e2, ..., oN=eN
445 * #=>
446 * # prologue code
447 * A1: e1
448 * A2: e2
449 * ...
450 * AN: eN
451 * AL: body
452 * opt_num = N
453 * opt_table = [A1, A2, ..., AN, AL]
456 const struct rb_iseq_param_keyword {
457 int num;
458 int required_num;
459 int bits_start;
460 int rest_start;
461 const ID *table;
462 VALUE *default_values;
463 } *keyword;
464 } param;
466 rb_iseq_location_t location;
468 /* insn info, must be freed */
469 struct iseq_insn_info {
470 const struct iseq_insn_info_entry *body;
471 unsigned int *positions;
472 unsigned int size;
473 #if VM_INSN_INFO_TABLE_IMPL == 2
474 struct succ_index_table *succ_index_table;
475 #endif
476 } insns_info;
478 const ID *local_table; /* must free */
480 /* catch table */
481 struct iseq_catch_table *catch_table;
483 /* for child iseq */
484 const struct rb_iseq_struct *parent_iseq;
485 struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
487 union iseq_inline_storage_entry *is_entries; /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
488 struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
490 struct {
491 rb_snum_t flip_count;
492 VALUE script_lines;
493 VALUE coverage;
494 VALUE pc2branchindex;
495 VALUE *original_iseq;
496 } variable;
498 unsigned int local_table_size;
499 unsigned int ic_size; // Number of IC caches
500 unsigned int ise_size; // Number of ISE caches
501 unsigned int ivc_size; // Number of IVC caches
502 unsigned int icvarc_size; // Number of ICVARC caches
503 unsigned int ci_size;
504 unsigned int stack_max; /* for stack overflow check */
506 unsigned int builtin_attrs; // Union of rb_builtin_attr
508 bool prism; // ISEQ was generated from prism compiler
510 union {
511 iseq_bits_t * list; /* Find references for GC */
512 iseq_bits_t single;
513 } mark_bits;
515 struct rb_id_table *outer_variables;
517 const rb_iseq_t *mandatory_only_iseq;
519 #if USE_RJIT || USE_YJIT
520 // Function pointer for JIT code on jit_exec()
521 rb_jit_func_t jit_entry;
522 // Number of calls on jit_exec()
523 long unsigned jit_entry_calls;
524 #endif
526 #if USE_YJIT
527 // Function pointer for JIT code on jit_exec_exception()
528 rb_jit_func_t jit_exception;
529 // Number of calls on jit_exec_exception()
530 long unsigned jit_exception_calls;
531 #endif
533 #if USE_RJIT
534 // RJIT stores some data on each iseq.
535 VALUE rjit_blocks;
536 #endif
538 #if USE_YJIT
539 // YJIT stores some data on each iseq.
540 void *yjit_payload;
541 // Used to estimate how frequently this ISEQ gets called
542 uint64_t yjit_calls_at_interv;
543 #endif
546 /* T_IMEMO/iseq */
547 /* typedef rb_iseq_t is in method.h */
548 struct rb_iseq_struct {
549 VALUE flags; /* 1 */
550 VALUE wrapper; /* 2 */
552 struct rb_iseq_constant_body *body; /* 3 */
554 union { /* 4, 5 words */
555 struct iseq_compile_data *compile_data; /* used at compile time */
557 struct {
558 VALUE obj;
559 int index;
560 } loader;
562 struct {
563 struct rb_hook_list_struct *local_hooks;
564 rb_event_flag_t global_trace_events;
565 } exec;
566 } aux;
569 #define ISEQ_BODY(iseq) ((iseq)->body)
571 #if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
572 #define USE_LAZY_LOAD 0
573 #endif
575 #if !USE_LAZY_LOAD
576 static inline const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq) {return 0;}
577 #endif
578 const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
580 static inline const rb_iseq_t *
581 rb_iseq_check(const rb_iseq_t *iseq)
583 if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
584 rb_iseq_complete((rb_iseq_t *)iseq);
586 return iseq;
589 static inline const rb_iseq_t *
590 def_iseq_ptr(rb_method_definition_t *def)
592 //TODO: re-visit. to check the bug, enable this assertion.
593 #if VM_CHECK_MODE > 0
594 if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
595 #endif
596 return rb_iseq_check(def->body.iseq.iseqptr);
599 enum ruby_special_exceptions {
600 ruby_error_reenter,
601 ruby_error_nomemory,
602 ruby_error_sysstack,
603 ruby_error_stackfatal,
604 ruby_error_stream_closed,
605 ruby_special_error_count
608 #define GetVMPtr(obj, ptr) \
609 GetCoreDataFromValue((obj), rb_vm_t, (ptr))
611 struct rb_vm_struct;
612 typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
614 typedef struct rb_at_exit_list {
615 rb_vm_at_exit_func *func;
616 struct rb_at_exit_list *next;
617 } rb_at_exit_list;
619 struct rb_objspace;
620 struct rb_objspace *rb_objspace_alloc(void);
621 void rb_objspace_free(struct rb_objspace *);
622 void rb_objspace_call_finalizer(struct rb_objspace *);
624 typedef struct rb_hook_list_struct {
625 struct rb_event_hook_struct *hooks;
626 rb_event_flag_t events;
627 unsigned int running;
628 bool need_clean;
629 bool is_local;
630 } rb_hook_list_t;
633 // see builtin.h for definition
634 typedef const struct rb_builtin_function *RB_BUILTIN;
636 struct global_object_list {
637 VALUE *varptr;
638 struct global_object_list *next;
641 typedef struct rb_vm_struct {
642 VALUE self;
644 struct {
645 struct ccan_list_head set;
646 unsigned int cnt;
647 unsigned int blocking_cnt;
649 struct rb_ractor_struct *main_ractor;
650 struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
652 struct {
653 // monitor
654 rb_nativethread_lock_t lock;
655 struct rb_ractor_struct *lock_owner;
656 unsigned int lock_rec;
658 // join at exit
659 rb_nativethread_cond_t terminate_cond;
660 bool terminate_waiting;
662 #ifndef RUBY_THREAD_PTHREAD_H
663 bool barrier_waiting;
664 unsigned int barrier_cnt;
665 rb_nativethread_cond_t barrier_cond;
666 #endif
667 } sync;
669 // ractor scheduling
670 struct {
671 rb_nativethread_lock_t lock;
672 struct rb_ractor_struct *lock_owner;
673 bool locked;
675 rb_nativethread_cond_t cond; // GRQ
676 unsigned int snt_cnt; // count of shared NTs
677 unsigned int dnt_cnt; // count of dedicated NTs
679 unsigned int running_cnt;
681 unsigned int max_cpu;
682 struct ccan_list_head grq; // // Global Ready Queue
683 unsigned int grq_cnt;
685 // running threads
686 struct ccan_list_head running_threads;
688 // threads which switch context by timeslice
689 struct ccan_list_head timeslice_threads;
691 struct ccan_list_head zombie_threads;
693 // true if timeslice timer is not enable
694 bool timeslice_wait_inf;
696 // barrier
697 rb_nativethread_cond_t barrier_complete_cond;
698 rb_nativethread_cond_t barrier_release_cond;
699 bool barrier_waiting;
700 unsigned int barrier_waiting_cnt;
701 unsigned int barrier_serial;
702 } sched;
703 } ractor;
705 #ifdef USE_SIGALTSTACK
706 void *main_altstack;
707 #endif
709 rb_serial_t fork_gen;
710 struct ccan_list_head waiting_fds; /* <=> struct waiting_fd */
712 /* set in single-threaded processes only: */
713 volatile int ubf_async_safe;
715 unsigned int running: 1;
716 unsigned int thread_abort_on_exception: 1;
717 unsigned int thread_report_on_exception: 1;
718 unsigned int thread_ignore_deadlock: 1;
720 /* object management */
721 VALUE mark_object_ary;
722 struct global_object_list *global_object_list;
723 const VALUE special_exceptions[ruby_special_error_count];
725 /* load */
726 VALUE top_self;
727 VALUE load_path;
728 VALUE load_path_snapshot;
729 VALUE load_path_check_cache;
730 VALUE expanded_load_path;
731 VALUE loaded_features;
732 VALUE loaded_features_snapshot;
733 VALUE loaded_features_realpaths;
734 VALUE loaded_features_realpath_map;
735 struct st_table *loaded_features_index;
736 struct st_table *loading_table;
737 // For running the init function of statically linked
738 // extensions when they are loaded
739 struct st_table *static_ext_inits;
741 /* signal */
742 struct {
743 VALUE cmd[RUBY_NSIG];
744 } trap_list;
746 /* relation table of ensure - rollback for callcc */
747 struct st_table *ensure_rollback_table;
749 /* postponed_job (async-signal-safe, and thread-safe) */
750 struct rb_postponed_job_queue *postponed_job_queue;
752 int src_encoding_index;
754 /* workqueue (thread-safe, NOT async-signal-safe) */
755 struct ccan_list_head workqueue; /* <=> rb_workqueue_job.jnode */
756 rb_nativethread_lock_t workqueue_lock;
758 VALUE orig_progname, progname;
759 VALUE coverages, me2counter;
760 int coverage_mode;
762 struct rb_objspace *objspace;
763 #if USE_SHARED_GC
764 rb_gc_function_map_t gc_functions_map;
765 #endif
767 rb_at_exit_list *at_exit;
769 st_table *frozen_strings;
771 const struct rb_builtin_function *builtin_function_table;
773 st_table *ci_table;
774 struct rb_id_table *negative_cme_table;
775 st_table *overloaded_cme_table; // cme -> overloaded_cme
776 st_table *unused_block_warning_table;
777 bool unused_block_warning_strict;
779 // This id table contains a mapping from ID to ICs. It does this with ID
780 // keys and nested st_tables as values. The nested tables have ICs as keys
781 // and Qtrue as values. It is used when inline constant caches need to be
782 // invalidated or ISEQs are being freed.
783 struct rb_id_table *constant_cache;
785 #ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
786 #define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
787 #endif
788 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
790 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
791 uint32_t clock;
792 #endif
794 /* params */
795 struct { /* size in byte */
796 size_t thread_vm_stack_size;
797 size_t thread_machine_stack_size;
798 size_t fiber_vm_stack_size;
799 size_t fiber_machine_stack_size;
800 } default_params;
802 } rb_vm_t;
804 /* default values */
806 #define RUBY_VM_SIZE_ALIGN 4096
808 #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
809 #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
810 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
811 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
813 #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
814 #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
815 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
816 #if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
817 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
818 #else
819 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
820 #endif
822 #if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
823 /* It seems sanitizers consume A LOT of machine stacks */
824 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
825 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
826 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
827 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
828 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
829 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
830 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
831 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
832 #endif
834 #ifndef VM_DEBUG_BP_CHECK
835 #define VM_DEBUG_BP_CHECK 0
836 #endif
838 #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
839 #define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
840 #endif
842 struct rb_captured_block {
843 VALUE self;
844 const VALUE *ep;
845 union {
846 const rb_iseq_t *iseq;
847 const struct vm_ifunc *ifunc;
848 VALUE val;
849 } code;
852 enum rb_block_handler_type {
853 block_handler_type_iseq,
854 block_handler_type_ifunc,
855 block_handler_type_symbol,
856 block_handler_type_proc
859 enum rb_block_type {
860 block_type_iseq,
861 block_type_ifunc,
862 block_type_symbol,
863 block_type_proc
866 struct rb_block {
867 union {
868 struct rb_captured_block captured;
869 VALUE symbol;
870 VALUE proc;
871 } as;
872 enum rb_block_type type;
875 typedef struct rb_control_frame_struct {
876 const VALUE *pc; // cfp[0]
877 VALUE *sp; // cfp[1]
878 const rb_iseq_t *iseq; // cfp[2]
879 VALUE self; // cfp[3] / block[0]
880 const VALUE *ep; // cfp[4] / block[1]
881 const void *block_code; // cfp[5] / block[2] -- iseq, ifunc, or forwarded block handler
882 void *jit_return; // cfp[6] -- return address for JIT code
883 #if VM_DEBUG_BP_CHECK
884 VALUE *bp_check; // cfp[7]
885 #endif
886 } rb_control_frame_t;
888 extern const rb_data_type_t ruby_threadptr_data_type;
890 static inline struct rb_thread_struct *
891 rb_thread_ptr(VALUE thval)
893 return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
896 enum rb_thread_status {
897 THREAD_RUNNABLE,
898 THREAD_STOPPED,
899 THREAD_STOPPED_FOREVER,
900 THREAD_KILLED
903 #ifdef RUBY_JMP_BUF
904 typedef RUBY_JMP_BUF rb_jmpbuf_t;
905 #else
906 typedef void *rb_jmpbuf_t[5];
907 #endif
910 `rb_vm_tag_jmpbuf_t` type represents a buffer used to
911 long jump to a C frame associated with `rb_vm_tag`.
913 Use-site of `rb_vm_tag_jmpbuf_t` is responsible for calling the
914 following functions:
915 - `rb_vm_tag_jmpbuf_init` once `rb_vm_tag_jmpbuf_t` is allocated.
916 - `rb_vm_tag_jmpbuf_deinit` once `rb_vm_tag_jmpbuf_t` is no longer necessary.
918 `RB_VM_TAG_JMPBUF_GET` transforms a `rb_vm_tag_jmpbuf_t` into a
919 `rb_jmpbuf_t` to be passed to `rb_setjmp/rb_longjmp`.
921 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
923 WebAssembly target with Asyncify-based SJLJ needs
924 to capture the execution context by unwind/rewind-ing
925 call frames into a jump buffer. The buffer space tends
926 to be considerably large unlike other architectures'
927 register-based buffers.
928 Therefore, we allocates the buffer on the heap on such
929 environments.
931 typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
933 #define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
935 static inline void
936 rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
938 *jmpbuf = ruby_xmalloc(sizeof(rb_jmpbuf_t));
941 static inline void
942 rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
944 ruby_xfree(*jmpbuf);
946 #else
947 typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
949 #define RB_VM_TAG_JMPBUF_GET(buf) (buf)
951 static inline void
952 rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
954 // no-op
957 static inline void
958 rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
960 // no-op
962 #endif
965 the members which are written in EC_PUSH_TAG() should be placed at
966 the beginning and the end, so that entire region is accessible.
968 struct rb_vm_tag {
969 VALUE tag;
970 VALUE retval;
971 rb_vm_tag_jmpbuf_t buf;
972 struct rb_vm_tag *prev;
973 enum ruby_tag_type state;
974 unsigned int lock_rec;
977 STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
978 STATIC_ASSERT(rb_vm_tag_buf_end,
979 offsetof(struct rb_vm_tag, buf) + sizeof(rb_vm_tag_jmpbuf_t) <
980 sizeof(struct rb_vm_tag));
982 struct rb_unblock_callback {
983 rb_unblock_function_t *func;
984 void *arg;
987 struct rb_mutex_struct;
989 typedef struct rb_ensure_entry {
990 VALUE marker;
991 VALUE (*e_proc)(VALUE);
992 VALUE data2;
993 } rb_ensure_entry_t;
995 typedef struct rb_ensure_list {
996 struct rb_ensure_list *next;
997 struct rb_ensure_entry entry;
998 } rb_ensure_list_t;
1000 typedef struct rb_fiber_struct rb_fiber_t;
1002 struct rb_waiting_list {
1003 struct rb_waiting_list *next;
1004 struct rb_thread_struct *thread;
1005 struct rb_fiber_struct *fiber;
1008 struct rb_execution_context_struct {
1009 /* execution information */
1010 VALUE *vm_stack; /* must free, must mark */
1011 size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
1012 rb_control_frame_t *cfp;
1014 struct rb_vm_tag *tag;
1016 /* interrupt flags */
1017 rb_atomic_t interrupt_flag;
1018 rb_atomic_t interrupt_mask; /* size should match flag */
1019 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1020 uint32_t checked_clock;
1021 #endif
1023 rb_fiber_t *fiber_ptr;
1024 struct rb_thread_struct *thread_ptr;
1026 /* storage (ec (fiber) local) */
1027 struct rb_id_table *local_storage;
1028 VALUE local_storage_recursive_hash;
1029 VALUE local_storage_recursive_hash_for_trace;
1031 /* Inheritable fiber storage. */
1032 VALUE storage;
1034 /* eval env */
1035 const VALUE *root_lep;
1036 VALUE root_svar;
1038 /* ensure & callcc */
1039 rb_ensure_list_t *ensure_list;
1041 /* trace information */
1042 struct rb_trace_arg_struct *trace_arg;
1044 /* temporary places */
1045 VALUE errinfo;
1046 VALUE passed_block_handler; /* for rb_iterate */
1048 uint8_t raised_flag; /* only 3 bits needed */
1050 /* n.b. only 7 bits needed, really: */
1051 BITFIELD(enum method_missing_reason, method_missing_reason, 8);
1053 VALUE private_const_reference;
1055 /* for GC */
1056 struct {
1057 VALUE *stack_start;
1058 VALUE *stack_end;
1059 size_t stack_maxsize;
1060 RUBY_ALIGNAS(SIZEOF_VALUE) jmp_buf regs;
1062 #ifdef RUBY_ASAN_ENABLED
1063 void *asan_fake_stack_handle;
1064 #endif
1065 } machine;
1068 #ifndef rb_execution_context_t
1069 typedef struct rb_execution_context_struct rb_execution_context_t;
1070 #define rb_execution_context_t rb_execution_context_t
1071 #endif
1073 // for builtin.h
1074 #define VM_CORE_H_EC_DEFINED 1
1076 // Set the vm_stack pointer in the execution context.
1077 void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1079 // Initialize the vm_stack pointer in the execution context and push the initial stack frame.
1080 // @param ec the execution context to update.
1081 // @param stack a pointer to the stack to use.
1082 // @param size the size of the stack, as in `VALUE stack[size]`.
1083 void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1085 // Clear (set to `NULL`) the vm_stack pointer.
1086 // @param ec the execution context to update.
1087 void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
1089 struct rb_ext_config {
1090 bool ractor_safe;
1093 typedef struct rb_ractor_struct rb_ractor_t;
1095 struct rb_native_thread;
1097 typedef struct rb_thread_struct {
1098 struct ccan_list_node lt_node; // managed by a ractor
1099 VALUE self;
1100 rb_ractor_t *ractor;
1101 rb_vm_t *vm;
1102 struct rb_native_thread *nt;
1103 rb_execution_context_t *ec;
1105 struct rb_thread_sched_item sched;
1106 bool mn_schedulable;
1107 rb_atomic_t serial; // only for RUBY_DEBUG_LOG()
1109 VALUE last_status; /* $? */
1111 /* for cfunc */
1112 struct rb_calling_info *calling;
1114 /* for load(true) */
1115 VALUE top_self;
1116 VALUE top_wrapper;
1118 /* thread control */
1120 BITFIELD(enum rb_thread_status, status, 2);
1121 /* bit flags */
1122 unsigned int has_dedicated_nt : 1;
1123 unsigned int to_kill : 1;
1124 unsigned int abort_on_exception: 1;
1125 unsigned int report_on_exception: 1;
1126 unsigned int pending_interrupt_queue_checked: 1;
1127 int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1128 uint32_t running_time_us; /* 12500..800000 */
1130 void *blocking_region_buffer;
1132 VALUE thgroup;
1133 VALUE value;
1135 /* temporary place of retval on OPT_CALL_THREADED_CODE */
1136 #if OPT_CALL_THREADED_CODE
1137 VALUE retval;
1138 #endif
1140 /* async errinfo queue */
1141 VALUE pending_interrupt_queue;
1142 VALUE pending_interrupt_mask_stack;
1144 /* interrupt management */
1145 rb_nativethread_lock_t interrupt_lock;
1146 struct rb_unblock_callback unblock;
1147 VALUE locking_mutex;
1148 struct rb_mutex_struct *keeping_mutexes;
1150 struct rb_waiting_list *join_list;
1152 union {
1153 struct {
1154 VALUE proc;
1155 VALUE args;
1156 int kw_splat;
1157 } proc;
1158 struct {
1159 VALUE (*func)(void *);
1160 void *arg;
1161 } func;
1162 } invoke_arg;
1164 enum thread_invoke_type {
1165 thread_invoke_type_none = 0,
1166 thread_invoke_type_proc,
1167 thread_invoke_type_ractor_proc,
1168 thread_invoke_type_func
1169 } invoke_type;
1171 /* statistics data for profiler */
1172 VALUE stat_insn_usage;
1174 /* fiber */
1175 rb_fiber_t *root_fiber;
1177 VALUE scheduler;
1178 unsigned int blocking;
1180 /* misc */
1181 VALUE name;
1182 void **specific_storage;
1184 struct rb_ext_config ext_config;
1185 } rb_thread_t;
1187 static inline unsigned int
1188 rb_th_serial(const rb_thread_t *th)
1190 return th ? (unsigned int)th->serial : 0;
1193 typedef enum {
1194 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1195 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1196 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1197 /* 0x03..0x06 is reserved */
1198 VM_DEFINECLASS_TYPE_MASK = 0x07
1199 } rb_vm_defineclass_type_t;
1201 #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1202 #define VM_DEFINECLASS_FLAG_SCOPED 0x08
1203 #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1204 #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1205 #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1206 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1208 /* iseq.c */
1209 RUBY_SYMBOL_EXPORT_BEGIN
1211 /* node -> iseq */
1212 rb_iseq_t *rb_iseq_new (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type);
1213 rb_iseq_t *rb_iseq_new_top (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1214 rb_iseq_t *rb_iseq_new_main (const VALUE ast_value, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1215 rb_iseq_t *rb_iseq_new_eval (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth);
1216 rb_iseq_t *rb_iseq_new_with_opt(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1217 enum rb_iseq_type, const rb_compile_option_t*,
1218 VALUE script_lines);
1220 struct iseq_link_anchor;
1221 struct rb_iseq_new_with_callback_callback_func {
1222 VALUE flags;
1223 VALUE reserved;
1224 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1225 const void *data;
1227 static inline struct rb_iseq_new_with_callback_callback_func *
1228 rb_iseq_new_with_callback_new_callback(
1229 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1231 struct rb_iseq_new_with_callback_callback_func *memo =
1232 IMEMO_NEW(struct rb_iseq_new_with_callback_callback_func, imemo_ifunc, Qfalse);
1233 memo->func = func;
1234 memo->data = ptr;
1236 return memo;
1238 rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1239 VALUE name, VALUE path, VALUE realpath, int first_lineno,
1240 const rb_iseq_t *parent, enum rb_iseq_type, const rb_compile_option_t*);
1242 VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1243 int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1245 VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1247 RUBY_EXTERN VALUE rb_cISeq;
1248 RUBY_EXTERN VALUE rb_cRubyVM;
1249 RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1250 RUBY_EXTERN VALUE rb_block_param_proxy;
1251 RUBY_SYMBOL_EXPORT_END
1253 #define GetProcPtr(obj, ptr) \
1254 GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1256 typedef struct {
1257 const struct rb_block block;
1258 unsigned int is_from_method: 1; /* bool */
1259 unsigned int is_lambda: 1; /* bool */
1260 unsigned int is_isolated: 1; /* bool */
1261 } rb_proc_t;
1263 RUBY_SYMBOL_EXPORT_BEGIN
1264 VALUE rb_proc_isolate(VALUE self);
1265 VALUE rb_proc_isolate_bang(VALUE self);
1266 VALUE rb_proc_ractor_make_shareable(VALUE self);
1267 RUBY_SYMBOL_EXPORT_END
1269 typedef struct {
1270 VALUE flags; /* imemo header */
1271 rb_iseq_t *iseq;
1272 const VALUE *ep;
1273 const VALUE *env;
1274 unsigned int env_size;
1275 } rb_env_t;
1277 extern const rb_data_type_t ruby_binding_data_type;
1279 #define GetBindingPtr(obj, ptr) \
1280 GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1282 typedef struct {
1283 const struct rb_block block;
1284 const VALUE pathobj;
1285 int first_lineno;
1286 } rb_binding_t;
1288 /* used by compile time and send insn */
1290 enum vm_check_match_type {
1291 VM_CHECKMATCH_TYPE_WHEN = 1,
1292 VM_CHECKMATCH_TYPE_CASE = 2,
1293 VM_CHECKMATCH_TYPE_RESCUE = 3
1296 #define VM_CHECKMATCH_TYPE_MASK 0x03
1297 #define VM_CHECKMATCH_ARRAY 0x04
1299 enum vm_special_object_type {
1300 VM_SPECIAL_OBJECT_VMCORE = 1,
1301 VM_SPECIAL_OBJECT_CBASE,
1302 VM_SPECIAL_OBJECT_CONST_BASE
1305 enum vm_svar_index {
1306 VM_SVAR_LASTLINE = 0, /* $_ */
1307 VM_SVAR_BACKREF = 1, /* $~ */
1309 VM_SVAR_EXTRA_START = 2,
1310 VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1313 /* inline cache */
1314 typedef struct iseq_inline_constant_cache *IC;
1315 typedef struct iseq_inline_iv_cache_entry *IVC;
1316 typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1317 typedef union iseq_inline_storage_entry *ISE;
1318 typedef const struct rb_callinfo *CALL_INFO;
1319 typedef const struct rb_callcache *CALL_CACHE;
1320 typedef struct rb_call_data *CALL_DATA;
1322 typedef VALUE CDHASH;
1324 #ifndef FUNC_FASTCALL
1325 #define FUNC_FASTCALL(x) x
1326 #endif
1328 typedef rb_control_frame_t *
1329 (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1331 #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1332 #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1334 #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1335 #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1336 #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1338 enum vm_frame_env_flags {
1339 /* Frame/Environment flag bits:
1340 * MMMM MMMM MMMM MMMM ____ FFFF FFFE EEEX (LSB)
1342 * X : tag for GC marking (It seems as Fixnum)
1343 * EEE : 4 bits Env flags
1344 * FF..: 7 bits Frame flags
1345 * MM..: 15 bits frame magic (to check frame corruption)
1348 /* frame types */
1349 VM_FRAME_MAGIC_METHOD = 0x11110001,
1350 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1351 VM_FRAME_MAGIC_CLASS = 0x33330001,
1352 VM_FRAME_MAGIC_TOP = 0x44440001,
1353 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1354 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1355 VM_FRAME_MAGIC_EVAL = 0x77770001,
1356 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1357 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1359 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1361 /* frame flag */
1362 VM_FRAME_FLAG_FINISH = 0x0020,
1363 VM_FRAME_FLAG_BMETHOD = 0x0040,
1364 VM_FRAME_FLAG_CFRAME = 0x0080,
1365 VM_FRAME_FLAG_LAMBDA = 0x0100,
1366 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1367 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1368 VM_FRAME_FLAG_PASSED = 0x0800,
1370 /* env flag */
1371 VM_ENV_FLAG_LOCAL = 0x0002,
1372 VM_ENV_FLAG_ESCAPED = 0x0004,
1373 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1374 VM_ENV_FLAG_ISOLATED = 0x0010,
1377 #define VM_ENV_DATA_SIZE ( 3)
1379 #define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1380 #define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1381 #define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1382 #define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1384 #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1386 static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1388 static inline void
1389 VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1391 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1392 VM_ASSERT(FIXNUM_P(flags));
1393 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1396 static inline void
1397 VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1399 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1400 VM_ASSERT(FIXNUM_P(flags));
1401 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1404 static inline unsigned long
1405 VM_ENV_FLAGS(const VALUE *ep, long flag)
1407 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1408 VM_ASSERT(FIXNUM_P(flags));
1409 return flags & flag;
1412 static inline unsigned long
1413 VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1415 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1418 static inline int
1419 VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1421 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1424 static inline int
1425 VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1427 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1430 static inline int
1431 VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1433 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1436 static inline int
1437 VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1439 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1442 static inline int
1443 rb_obj_is_iseq(VALUE iseq)
1445 return imemo_type_p(iseq, imemo_iseq);
1448 #if VM_CHECK_MODE > 0
1449 #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1450 #endif
1452 static inline int
1453 VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1455 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1456 VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1457 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1458 return cframe_p;
1461 static inline int
1462 VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1464 return !VM_FRAME_CFRAME_P(cfp);
1467 #define RUBYVM_CFUNC_FRAME_P(cfp) \
1468 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1470 #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1471 #define VM_BLOCK_HANDLER_NONE 0
1473 static inline int
1474 VM_ENV_LOCAL_P(const VALUE *ep)
1476 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1479 static inline const VALUE *
1480 VM_ENV_PREV_EP(const VALUE *ep)
1482 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1483 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1486 static inline VALUE
1487 VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1489 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1490 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1493 #if VM_CHECK_MODE > 0
1494 int rb_vm_ep_in_heap_p(const VALUE *ep);
1495 #endif
1497 static inline int
1498 VM_ENV_ESCAPED_P(const VALUE *ep)
1500 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1501 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1504 #if VM_CHECK_MODE > 0
1505 static inline int
1506 vm_assert_env(VALUE obj)
1508 VM_ASSERT(imemo_type_p(obj, imemo_env));
1509 return 1;
1511 #endif
1513 RBIMPL_ATTR_NONNULL((1))
1514 static inline VALUE
1515 VM_ENV_ENVVAL(const VALUE *ep)
1517 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1518 VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1519 VM_ASSERT(vm_assert_env(envval));
1520 return envval;
1523 RBIMPL_ATTR_NONNULL((1))
1524 static inline const rb_env_t *
1525 VM_ENV_ENVVAL_PTR(const VALUE *ep)
1527 return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1530 static inline const rb_env_t *
1531 vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1533 rb_env_t *env = IMEMO_NEW(rb_env_t, imemo_env, (VALUE)iseq);
1534 env->ep = env_ep;
1535 env->env = env_body;
1536 env->env_size = env_size;
1537 env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1538 return env;
1541 static inline void
1542 VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1544 *((VALUE *)ptr) = v;
1547 static inline void
1548 VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1550 VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1551 VM_FORCE_WRITE(ptr, special_const_value);
1554 static inline void
1555 VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1557 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1558 VM_FORCE_WRITE(&ep[index], v);
1561 const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1562 const VALUE *rb_vm_proc_local_ep(VALUE proc);
1563 void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep);
1564 void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src);
1566 VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1568 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1569 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1571 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1572 ((void *)(ecfp) > (void *)(cfp))
1574 static inline const rb_control_frame_t *
1575 RUBY_VM_END_CONTROL_FRAME(const rb_execution_context_t *ec)
1577 return (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size);
1580 static inline int
1581 RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1583 return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1586 static inline int
1587 VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1589 if ((block_handler & 0x03) == 0x01) {
1590 #if VM_CHECK_MODE > 0
1591 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1592 VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1593 #endif
1594 return 1;
1596 else {
1597 return 0;
1601 static inline VALUE
1602 VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1604 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1605 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1606 return block_handler;
1609 static inline const struct rb_captured_block *
1610 VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1612 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1613 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1614 return captured;
1617 static inline int
1618 VM_BH_IFUNC_P(VALUE block_handler)
1620 if ((block_handler & 0x03) == 0x03) {
1621 #if VM_CHECK_MODE > 0
1622 struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1623 VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1624 #endif
1625 return 1;
1627 else {
1628 return 0;
1632 static inline VALUE
1633 VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1635 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1636 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1637 return block_handler;
1640 static inline const struct rb_captured_block *
1641 VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1643 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1644 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1645 return captured;
1648 static inline const struct rb_captured_block *
1649 VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1651 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1652 VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1653 return captured;
1656 static inline enum rb_block_handler_type
1657 vm_block_handler_type(VALUE block_handler)
1659 if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1660 return block_handler_type_iseq;
1662 else if (VM_BH_IFUNC_P(block_handler)) {
1663 return block_handler_type_ifunc;
1665 else if (SYMBOL_P(block_handler)) {
1666 return block_handler_type_symbol;
1668 else {
1669 VM_ASSERT(rb_obj_is_proc(block_handler));
1670 return block_handler_type_proc;
1674 static inline void
1675 vm_block_handler_verify(MAYBE_UNUSED(VALUE block_handler))
1677 VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1678 (vm_block_handler_type(block_handler), 1));
1681 static inline enum rb_block_type
1682 vm_block_type(const struct rb_block *block)
1684 #if VM_CHECK_MODE > 0
1685 switch (block->type) {
1686 case block_type_iseq:
1687 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1688 break;
1689 case block_type_ifunc:
1690 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1691 break;
1692 case block_type_symbol:
1693 VM_ASSERT(SYMBOL_P(block->as.symbol));
1694 break;
1695 case block_type_proc:
1696 VM_ASSERT(rb_obj_is_proc(block->as.proc));
1697 break;
1699 #endif
1700 return block->type;
1703 static inline void
1704 vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1706 struct rb_block *mb = (struct rb_block *)block;
1707 mb->type = type;
1710 static inline const struct rb_block *
1711 vm_proc_block(VALUE procval)
1713 VM_ASSERT(rb_obj_is_proc(procval));
1714 return &((rb_proc_t *)RTYPEDDATA_DATA(procval))->block;
1717 static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1718 static inline const VALUE *vm_block_ep(const struct rb_block *block);
1720 static inline const rb_iseq_t *
1721 vm_proc_iseq(VALUE procval)
1723 return vm_block_iseq(vm_proc_block(procval));
1726 static inline const VALUE *
1727 vm_proc_ep(VALUE procval)
1729 return vm_block_ep(vm_proc_block(procval));
1732 static inline const rb_iseq_t *
1733 vm_block_iseq(const struct rb_block *block)
1735 switch (vm_block_type(block)) {
1736 case block_type_iseq: return rb_iseq_check(block->as.captured.code.iseq);
1737 case block_type_proc: return vm_proc_iseq(block->as.proc);
1738 case block_type_ifunc:
1739 case block_type_symbol: return NULL;
1741 VM_UNREACHABLE(vm_block_iseq);
1742 return NULL;
1745 static inline const VALUE *
1746 vm_block_ep(const struct rb_block *block)
1748 switch (vm_block_type(block)) {
1749 case block_type_iseq:
1750 case block_type_ifunc: return block->as.captured.ep;
1751 case block_type_proc: return vm_proc_ep(block->as.proc);
1752 case block_type_symbol: return NULL;
1754 VM_UNREACHABLE(vm_block_ep);
1755 return NULL;
1758 static inline VALUE
1759 vm_block_self(const struct rb_block *block)
1761 switch (vm_block_type(block)) {
1762 case block_type_iseq:
1763 case block_type_ifunc:
1764 return block->as.captured.self;
1765 case block_type_proc:
1766 return vm_block_self(vm_proc_block(block->as.proc));
1767 case block_type_symbol:
1768 return Qundef;
1770 VM_UNREACHABLE(vm_block_self);
1771 return Qundef;
1774 static inline VALUE
1775 VM_BH_TO_SYMBOL(VALUE block_handler)
1777 VM_ASSERT(SYMBOL_P(block_handler));
1778 return block_handler;
1781 static inline VALUE
1782 VM_BH_FROM_SYMBOL(VALUE symbol)
1784 VM_ASSERT(SYMBOL_P(symbol));
1785 return symbol;
1788 static inline VALUE
1789 VM_BH_TO_PROC(VALUE block_handler)
1791 VM_ASSERT(rb_obj_is_proc(block_handler));
1792 return block_handler;
1795 static inline VALUE
1796 VM_BH_FROM_PROC(VALUE procval)
1798 VM_ASSERT(rb_obj_is_proc(procval));
1799 return procval;
1802 /* VM related object allocate functions */
1803 VALUE rb_thread_alloc(VALUE klass);
1804 VALUE rb_binding_alloc(VALUE klass);
1805 VALUE rb_proc_alloc(VALUE klass);
1806 VALUE rb_proc_dup(VALUE self);
1808 /* for debug */
1809 extern bool rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *);
1810 extern bool rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc, FILE *);
1811 extern bool rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *);
1813 #define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp, stderr)
1814 #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp), stderr)
1815 bool rb_vm_bugreport(const void *, FILE *);
1816 typedef void (*ruby_sighandler_t)(int);
1817 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 5)
1818 NORETURN(void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *, const char *fmt, ...));
1820 /* functions about thread/vm execution */
1821 RUBY_SYMBOL_EXPORT_BEGIN
1822 VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1823 VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1824 VALUE rb_iseq_path(const rb_iseq_t *iseq);
1825 VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
1826 RUBY_SYMBOL_EXPORT_END
1828 VALUE rb_iseq_pathobj_new(VALUE path, VALUE realpath);
1829 void rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath);
1831 int rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp);
1832 void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause);
1834 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);
1836 VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
1837 static inline VALUE
1838 rb_vm_make_proc(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1840 return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1843 static inline VALUE
1844 rb_vm_make_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1846 return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1849 VALUE rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp);
1850 VALUE rb_vm_env_local_variables(const rb_env_t *env);
1851 const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
1852 const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars);
1853 void rb_vm_inc_const_missing_count(void);
1854 VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc,
1855 const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat);
1856 void rb_vm_pop_frame_no_int(rb_execution_context_t *ec);
1857 void rb_vm_pop_frame(rb_execution_context_t *ec);
1859 void rb_thread_start_timer_thread(void);
1860 void rb_thread_stop_timer_thread(void);
1861 void rb_thread_reset_timer_thread(void);
1862 void rb_thread_wakeup_timer_thread(int);
1864 static inline void
1865 rb_vm_living_threads_init(rb_vm_t *vm)
1867 ccan_list_head_init(&vm->waiting_fds);
1868 ccan_list_head_init(&vm->workqueue);
1869 ccan_list_head_init(&vm->ractor.set);
1870 ccan_list_head_init(&vm->ractor.sched.zombie_threads);
1873 typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
1874 rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1875 rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1876 VALUE *rb_vm_svar_lep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1877 int rb_vm_get_sourceline(const rb_control_frame_t *);
1878 void rb_vm_stack_to_heap(rb_execution_context_t *ec);
1879 void ruby_thread_init_stack(rb_thread_t *th, void *local_in_parent_frame);
1880 rb_thread_t * ruby_thread_from_native(void);
1881 int ruby_thread_set_native(rb_thread_t *th);
1882 int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
1883 void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp);
1884 void rb_vm_env_write(const VALUE *ep, int index, VALUE v);
1885 VALUE rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler);
1887 void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE exception_class, VALUE mesg);
1889 #define rb_vm_register_special_exception(sp, e, m) \
1890 rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1892 void rb_gc_mark_machine_context(const rb_execution_context_t *ec);
1894 void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);
1896 const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
1898 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1900 #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1901 STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1902 STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1903 const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1904 if (UNLIKELY((cfp) <= &bound[1])) { \
1905 vm_stackoverflow(); \
1907 } while (0)
1909 #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1910 CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1912 VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr);
1914 rb_execution_context_t *rb_vm_main_ractor_ec(rb_vm_t *vm); // ractor.c
1916 /* for thread */
1918 #if RUBY_VM_THREAD_MODEL == 2
1920 RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
1921 RUBY_EXTERN rb_vm_t *ruby_current_vm_ptr;
1922 RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
1923 RUBY_EXTERN rb_event_flag_t ruby_vm_event_enabled_global_flags;
1924 RUBY_EXTERN unsigned int ruby_vm_event_local_num;
1926 #define GET_VM() rb_current_vm()
1927 #define GET_RACTOR() rb_current_ractor()
1928 #define GET_THREAD() rb_current_thread()
1929 #define GET_EC() rb_current_execution_context(true)
1931 static inline rb_thread_t *
1932 rb_ec_thread_ptr(const rb_execution_context_t *ec)
1934 return ec->thread_ptr;
1937 static inline rb_ractor_t *
1938 rb_ec_ractor_ptr(const rb_execution_context_t *ec)
1940 const rb_thread_t *th = rb_ec_thread_ptr(ec);
1941 if (th) {
1942 VM_ASSERT(th->ractor != NULL);
1943 return th->ractor;
1945 else {
1946 return NULL;
1950 static inline rb_vm_t *
1951 rb_ec_vm_ptr(const rb_execution_context_t *ec)
1953 const rb_thread_t *th = rb_ec_thread_ptr(ec);
1954 if (th) {
1955 return th->vm;
1957 else {
1958 return NULL;
1962 static inline rb_execution_context_t *
1963 rb_current_execution_context(bool expect_ec)
1965 #ifdef RB_THREAD_LOCAL_SPECIFIER
1966 #ifdef __APPLE__
1967 rb_execution_context_t *ec = rb_current_ec();
1968 #else
1969 rb_execution_context_t *ec = ruby_current_ec;
1970 #endif
1972 /* On the shared objects, `__tls_get_addr()` is used to access the TLS
1973 * and the address of the `ruby_current_ec` can be stored on a function
1974 * frame. However, this address can be mis-used after native thread
1975 * migration of a coroutine.
1976 * 1) Get `ptr =&ruby_current_ec` op NT1 and store it on the frame.
1977 * 2) Context switch and resume it on the NT2.
1978 * 3) `ptr` is used on NT2 but it accesses to the TLS on NT1.
1979 * This assertion checks such misusage.
1981 * To avoid accidents, `GET_EC()` should be called once on the frame.
1982 * Note that inlining can produce the problem.
1984 VM_ASSERT(ec == rb_current_ec_noinline());
1985 #else
1986 rb_execution_context_t *ec = native_tls_get(ruby_current_ec_key);
1987 #endif
1988 VM_ASSERT(!expect_ec || ec != NULL);
1989 return ec;
1992 static inline rb_thread_t *
1993 rb_current_thread(void)
1995 const rb_execution_context_t *ec = GET_EC();
1996 return rb_ec_thread_ptr(ec);
1999 static inline rb_ractor_t *
2000 rb_current_ractor_raw(bool expect)
2002 if (ruby_single_main_ractor) {
2003 return ruby_single_main_ractor;
2005 else {
2006 const rb_execution_context_t *ec = rb_current_execution_context(expect);
2007 return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
2011 static inline rb_ractor_t *
2012 rb_current_ractor(void)
2014 return rb_current_ractor_raw(true);
2017 static inline rb_vm_t *
2018 rb_current_vm(void)
2020 #if 0 // TODO: reconsider the assertions
2021 VM_ASSERT(ruby_current_vm_ptr == NULL ||
2022 ruby_current_execution_context_ptr == NULL ||
2023 rb_ec_thread_ptr(GET_EC()) == NULL ||
2024 rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
2025 rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
2026 #endif
2028 return ruby_current_vm_ptr;
2031 void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
2032 unsigned int recorded_lock_rec,
2033 unsigned int current_lock_rec);
2035 static inline unsigned int
2036 rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
2038 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2040 if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
2041 return 0;
2043 else {
2044 return vm->ractor.sync.lock_rec;
2048 #else
2049 #error "unsupported thread model"
2050 #endif
2052 enum {
2053 TIMER_INTERRUPT_MASK = 0x01,
2054 PENDING_INTERRUPT_MASK = 0x02,
2055 POSTPONED_JOB_INTERRUPT_MASK = 0x04,
2056 TRAP_INTERRUPT_MASK = 0x08,
2057 TERMINATE_INTERRUPT_MASK = 0x10,
2058 VM_BARRIER_INTERRUPT_MASK = 0x20,
2061 #define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
2062 #define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
2063 #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
2064 #define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
2065 #define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
2066 #define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
2067 #define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
2068 (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
2070 static inline bool
2071 RUBY_VM_INTERRUPTED_ANY(rb_execution_context_t *ec)
2073 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
2074 uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
2076 if (current_clock != ec->checked_clock) {
2077 ec->checked_clock = current_clock;
2078 RUBY_VM_SET_TIMER_INTERRUPT(ec);
2080 #endif
2081 return ec->interrupt_flag & ~(ec)->interrupt_mask;
2084 VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
2085 int rb_signal_buff_size(void);
2086 int rb_signal_exec(rb_thread_t *th, int sig);
2087 void rb_threadptr_check_signal(rb_thread_t *mth);
2088 void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
2089 void rb_threadptr_signal_exit(rb_thread_t *th);
2090 int rb_threadptr_execute_interrupts(rb_thread_t *, int);
2091 void rb_threadptr_interrupt(rb_thread_t *th);
2092 void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
2093 void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
2094 void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
2095 VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec);
2096 void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo);
2097 void rb_execution_context_update(rb_execution_context_t *ec);
2098 void rb_execution_context_mark(const rb_execution_context_t *ec);
2099 void rb_fiber_close(rb_fiber_t *fib);
2100 void Init_native_thread(rb_thread_t *th);
2101 int rb_vm_check_ints_blocking(rb_execution_context_t *ec);
2103 // vm_sync.h
2104 void rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond);
2105 void rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec);
2107 #define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
2108 static inline void
2109 rb_vm_check_ints(rb_execution_context_t *ec)
2111 #ifdef RUBY_ASSERT_CRITICAL_SECTION
2112 VM_ASSERT(ruby_assert_critical_section_entered == 0);
2113 #endif
2115 VM_ASSERT(ec == GET_EC());
2117 if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2118 rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2122 /* tracer */
2124 struct rb_trace_arg_struct {
2125 rb_event_flag_t event;
2126 rb_execution_context_t *ec;
2127 const rb_control_frame_t *cfp;
2128 VALUE self;
2129 ID id;
2130 ID called_id;
2131 VALUE klass;
2132 VALUE data;
2134 int klass_solved;
2136 /* calc from cfp */
2137 int lineno;
2138 VALUE path;
2141 void rb_hook_list_mark(rb_hook_list_t *hooks);
2142 void rb_hook_list_mark_and_update(rb_hook_list_t *hooks);
2143 void rb_hook_list_free(rb_hook_list_t *hooks);
2144 void rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line);
2145 void rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval);
2147 void rb_exec_event_hooks(struct rb_trace_arg_struct *trace_arg, rb_hook_list_t *hooks, int pop_p);
2149 #define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2150 const rb_event_flag_t flag_arg_ = (flag_); \
2151 rb_hook_list_t *hooks_arg_ = (hooks_); \
2152 if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2153 /* defer evaluating the other arguments */ \
2154 rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2156 } while (0)
2158 static inline void
2159 rb_exec_event_hook_orig(rb_execution_context_t *ec, rb_hook_list_t *hooks, rb_event_flag_t flag,
2160 VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
2162 struct rb_trace_arg_struct trace_arg;
2164 VM_ASSERT((hooks->events & flag) != 0);
2166 trace_arg.event = flag;
2167 trace_arg.ec = ec;
2168 trace_arg.cfp = ec->cfp;
2169 trace_arg.self = self;
2170 trace_arg.id = id;
2171 trace_arg.called_id = called_id;
2172 trace_arg.klass = klass;
2173 trace_arg.data = data;
2174 trace_arg.path = Qundef;
2175 trace_arg.klass_solved = 0;
2177 rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2180 struct rb_ractor_pub {
2181 VALUE self;
2182 uint32_t id;
2183 rb_hook_list_t hooks;
2186 static inline rb_hook_list_t *
2187 rb_ec_ractor_hooks(const rb_execution_context_t *ec)
2189 struct rb_ractor_pub *cr_pub = (struct rb_ractor_pub *)rb_ec_ractor_ptr(ec);
2190 return &cr_pub->hooks;
2193 #define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2194 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2196 #define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2197 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2199 static inline void
2200 rb_exec_event_hook_script_compiled(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE eval_script)
2202 EXEC_EVENT_HOOK(ec, RUBY_EVENT_SCRIPT_COMPILED, ec->cfp->self, 0, 0, 0,
2203 NIL_P(eval_script) ? (VALUE)iseq :
2204 rb_ary_new_from_args(2, eval_script, (VALUE)iseq));
2207 void rb_vm_trap_exit(rb_vm_t *vm);
2208 void rb_vm_postponed_job_atfork(void); /* vm_trace.c */
2209 void rb_vm_postponed_job_free(void); /* vm_trace.c */
2210 size_t rb_vm_memsize_postponed_job_queue(void); /* vm_trace.c */
2211 void rb_vm_postponed_job_queue_init(rb_vm_t *vm); /* vm_trace.c */
2213 RUBY_SYMBOL_EXPORT_BEGIN
2215 int rb_thread_check_trap_pending(void);
2217 /* #define RUBY_EVENT_RESERVED_FOR_INTERNAL_USE 0x030000 */ /* from vm_core.h */
2218 #define RUBY_EVENT_COVERAGE_LINE 0x010000
2219 #define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2221 extern VALUE rb_get_coverages(void);
2222 extern void rb_set_coverages(VALUE, int, VALUE);
2223 extern void rb_clear_coverages(void);
2224 extern void rb_reset_coverages(void);
2225 extern void rb_resume_coverages(void);
2226 extern void rb_suspend_coverages(void);
2228 void rb_postponed_job_flush(rb_vm_t *vm);
2230 // ractor.c
2231 RUBY_EXTERN VALUE rb_eRactorUnsafeError;
2232 RUBY_EXTERN VALUE rb_eRactorIsolationError;
2234 RUBY_SYMBOL_EXPORT_END
2236 #endif /* RUBY_VM_CORE_H */