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