Ruby 3.3.0p0 (2023-12-25 revision 5124f9ac7513eb590c37717337c430cb93caa151)
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 struct rb_id_table *negative_cme_table;
754 st_table *overloaded_cme_table; // cme -> overloaded_cme
755
756 // This id table contains a mapping from ID to ICs. It does this with ID
757 // keys and nested st_tables as values. The nested tables have ICs as keys
758 // and Qtrue as values. It is used when inline constant caches need to be
759 // invalidated or ISEQs are being freed.
760 struct rb_id_table *constant_cache;
761
762#ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
763#define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
764#endif
765 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
766
767#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
768 uint32_t clock;
769#endif
770
771 /* params */
772 struct { /* size in byte */
773 size_t thread_vm_stack_size;
774 size_t thread_machine_stack_size;
775 size_t fiber_vm_stack_size;
776 size_t fiber_machine_stack_size;
777 } default_params;
778
779} rb_vm_t;
780
781/* default values */
782
783#define RUBY_VM_SIZE_ALIGN 4096
784
785#define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
786#define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
787#define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
788#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
789
790#define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
791#define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
792#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
793#if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
794#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
795#else
796#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
797#endif
798
799#if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
800/* It seems sanitizers consume A LOT of machine stacks */
801#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
802#define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
803#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
804#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
805#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
806#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
807#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
808#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
809#endif
810
811#ifndef VM_DEBUG_BP_CHECK
812#define VM_DEBUG_BP_CHECK 0
813#endif
814
815#ifndef VM_DEBUG_VERIFY_METHOD_CACHE
816#define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
817#endif
818
820 VALUE self;
821 const VALUE *ep;
822 union {
823 const rb_iseq_t *iseq;
824 const struct vm_ifunc *ifunc;
825 VALUE val;
826 } code;
827};
828
829enum rb_block_handler_type {
830 block_handler_type_iseq,
831 block_handler_type_ifunc,
832 block_handler_type_symbol,
833 block_handler_type_proc
834};
835
836enum rb_block_type {
837 block_type_iseq,
838 block_type_ifunc,
839 block_type_symbol,
840 block_type_proc
841};
842
843struct rb_block {
844 union {
845 struct rb_captured_block captured;
846 VALUE symbol;
847 VALUE proc;
848 } as;
849 enum rb_block_type type;
850};
851
853 const VALUE *pc; // cfp[0]
854 VALUE *sp; // cfp[1]
855 const rb_iseq_t *iseq; // cfp[2]
856 VALUE self; // cfp[3] / block[0]
857 const VALUE *ep; // cfp[4] / block[1]
858 const void *block_code; // cfp[5] / block[2] -- iseq, ifunc, or forwarded block handler
859 void *jit_return; // cfp[6] -- return address for JIT code
860#if VM_DEBUG_BP_CHECK
861 VALUE *bp_check; // cfp[7]
862#endif
864
865extern const rb_data_type_t ruby_threadptr_data_type;
866
867static inline struct rb_thread_struct *
868rb_thread_ptr(VALUE thval)
869{
870 return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
871}
872
873enum rb_thread_status {
874 THREAD_RUNNABLE,
875 THREAD_STOPPED,
876 THREAD_STOPPED_FOREVER,
877 THREAD_KILLED
878};
879
880#ifdef RUBY_JMP_BUF
881typedef RUBY_JMP_BUF rb_jmpbuf_t;
882#else
883typedef void *rb_jmpbuf_t[5];
884#endif
885
886/*
887 `rb_vm_tag_jmpbuf_t` type represents a buffer used to
888 long jump to a C frame associated with `rb_vm_tag`.
889
890 Use-site of `rb_vm_tag_jmpbuf_t` is responsible for calling the
891 following functions:
892 - `rb_vm_tag_jmpbuf_init` once `rb_vm_tag_jmpbuf_t` is allocated.
893 - `rb_vm_tag_jmpbuf_deinit` once `rb_vm_tag_jmpbuf_t` is no longer necessary.
894
895 `RB_VM_TAG_JMPBUF_GET` transforms a `rb_vm_tag_jmpbuf_t` into a
896 `rb_jmpbuf_t` to be passed to `rb_setjmp/rb_longjmp`.
897*/
898#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
899/*
900 WebAssembly target with Asyncify-based SJLJ needs
901 to capture the execution context by unwind/rewind-ing
902 call frames into a jump buffer. The buffer space tends
903 to be considerably large unlike other architectures'
904 register-based buffers.
905 Therefore, we allocates the buffer on the heap on such
906 environments.
907*/
908typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
909
910#define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
911
912static inline void
913rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
914{
915 *jmpbuf = ruby_xmalloc(sizeof(rb_jmpbuf_t));
916}
917
918static inline void
919rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
920{
921 ruby_xfree(*jmpbuf);
922}
923#else
924typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
925
926#define RB_VM_TAG_JMPBUF_GET(buf) (buf)
927
928static inline void
929rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
930{
931 // no-op
932}
933
934static inline void
935rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
936{
937 // no-op
938}
939#endif
940
941/*
942 the members which are written in EC_PUSH_TAG() should be placed at
943 the beginning and the end, so that entire region is accessible.
944*/
945struct rb_vm_tag {
946 VALUE tag;
947 VALUE retval;
948 rb_vm_tag_jmpbuf_t buf;
949 struct rb_vm_tag *prev;
950 enum ruby_tag_type state;
951 unsigned int lock_rec;
952};
953
954STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
955STATIC_ASSERT(rb_vm_tag_buf_end,
956 offsetof(struct rb_vm_tag, buf) + sizeof(rb_vm_tag_jmpbuf_t) <
957 sizeof(struct rb_vm_tag));
958
961 void *arg;
962};
963
964struct rb_mutex_struct;
965
966typedef struct rb_ensure_entry {
967 VALUE marker;
968 VALUE (*e_proc)(VALUE);
969 VALUE data2;
971
972typedef struct rb_ensure_list {
973 struct rb_ensure_list *next;
974 struct rb_ensure_entry entry;
976
977typedef struct rb_fiber_struct rb_fiber_t;
978
980 struct rb_waiting_list *next;
981 struct rb_thread_struct *thread;
982 struct rb_fiber_struct *fiber;
983};
984
986 /* execution information */
987 VALUE *vm_stack; /* must free, must mark */
988 size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
990
991 struct rb_vm_tag *tag;
992
993 /* interrupt flags */
994 rb_atomic_t interrupt_flag;
995 rb_atomic_t interrupt_mask; /* size should match flag */
996#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
997 uint32_t checked_clock;
998#endif
999
1000 rb_fiber_t *fiber_ptr;
1001 struct rb_thread_struct *thread_ptr;
1002
1003 /* storage (ec (fiber) local) */
1004 struct rb_id_table *local_storage;
1005 VALUE local_storage_recursive_hash;
1006 VALUE local_storage_recursive_hash_for_trace;
1007
1008 /* Inheritable fiber storage. */
1009 VALUE storage;
1010
1011 /* eval env */
1012 const VALUE *root_lep;
1013 VALUE root_svar;
1014
1015 /* ensure & callcc */
1016 rb_ensure_list_t *ensure_list;
1017
1018 /* trace information */
1019 struct rb_trace_arg_struct *trace_arg;
1020
1021 /* temporary places */
1022 VALUE errinfo;
1023 VALUE passed_block_handler; /* for rb_iterate */
1024
1025 uint8_t raised_flag; /* only 3 bits needed */
1026
1027 /* n.b. only 7 bits needed, really: */
1028 BITFIELD(enum method_missing_reason, method_missing_reason, 8);
1029
1030 VALUE private_const_reference;
1031
1032 /* for GC */
1033 struct {
1034 VALUE *stack_start;
1035 VALUE *stack_end;
1036 size_t stack_maxsize;
1038 } machine;
1039};
1040
1041#ifndef rb_execution_context_t
1043#define rb_execution_context_t rb_execution_context_t
1044#endif
1045
1046// for builtin.h
1047#define VM_CORE_H_EC_DEFINED 1
1048
1049// Set the vm_stack pointer in the execution context.
1050void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1051
1052// Initialize the vm_stack pointer in the execution context and push the initial stack frame.
1053// @param ec the execution context to update.
1054// @param stack a pointer to the stack to use.
1055// @param size the size of the stack, as in `VALUE stack[size]`.
1056void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1057
1058// Clear (set to `NULL`) the vm_stack pointer.
1059// @param ec the execution context to update.
1060void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
1061
1063 bool ractor_safe;
1064};
1065
1066typedef struct rb_ractor_struct rb_ractor_t;
1067
1068struct rb_native_thread;
1069
1070typedef struct rb_thread_struct {
1071 struct ccan_list_node lt_node; // managed by a ractor
1072 VALUE self;
1073 rb_ractor_t *ractor;
1074 rb_vm_t *vm;
1075 struct rb_native_thread *nt;
1077
1078 struct rb_thread_sched_item sched;
1079 rb_atomic_t serial; // only for RUBY_DEBUG_LOG()
1080
1081 VALUE last_status; /* $? */
1082
1083 /* for cfunc */
1084 struct rb_calling_info *calling;
1085
1086 /* for load(true) */
1087 VALUE top_self;
1088 VALUE top_wrapper;
1089
1090 /* thread control */
1091
1092 BITFIELD(enum rb_thread_status, status, 2);
1093 /* bit flags */
1094 unsigned int has_dedicated_nt : 1;
1095 unsigned int to_kill : 1;
1096 unsigned int abort_on_exception: 1;
1097 unsigned int report_on_exception: 1;
1098 unsigned int pending_interrupt_queue_checked: 1;
1099 int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1100 uint32_t running_time_us; /* 12500..800000 */
1101
1102 void *blocking_region_buffer;
1103
1104 VALUE thgroup;
1105 VALUE value;
1106
1107 /* temporary place of retval on OPT_CALL_THREADED_CODE */
1108#if OPT_CALL_THREADED_CODE
1109 VALUE retval;
1110#endif
1111
1112 /* async errinfo queue */
1113 VALUE pending_interrupt_queue;
1114 VALUE pending_interrupt_mask_stack;
1115
1116 /* interrupt management */
1117 rb_nativethread_lock_t interrupt_lock;
1118 struct rb_unblock_callback unblock;
1119 VALUE locking_mutex;
1120 struct rb_mutex_struct *keeping_mutexes;
1121
1122 struct rb_waiting_list *join_list;
1123
1124 union {
1125 struct {
1126 VALUE proc;
1127 VALUE args;
1128 int kw_splat;
1129 } proc;
1130 struct {
1131 VALUE (*func)(void *);
1132 void *arg;
1133 } func;
1134 } invoke_arg;
1135
1136 enum thread_invoke_type {
1137 thread_invoke_type_none = 0,
1138 thread_invoke_type_proc,
1139 thread_invoke_type_ractor_proc,
1140 thread_invoke_type_func
1141 } invoke_type;
1142
1143 /* statistics data for profiler */
1144 VALUE stat_insn_usage;
1145
1146 /* fiber */
1147 rb_fiber_t *root_fiber;
1148
1149 VALUE scheduler;
1150 unsigned int blocking;
1151
1152 /* misc */
1153 VALUE name;
1154 void **specific_storage;
1155
1156 struct rb_ext_config ext_config;
1157} rb_thread_t;
1158
1159static inline unsigned int
1160rb_th_serial(const rb_thread_t *th)
1161{
1162 return th ? (unsigned int)th->serial : 0;
1163}
1164
1165typedef enum {
1166 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1167 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1168 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1169 /* 0x03..0x06 is reserved */
1170 VM_DEFINECLASS_TYPE_MASK = 0x07
1171} rb_vm_defineclass_type_t;
1172
1173#define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1174#define VM_DEFINECLASS_FLAG_SCOPED 0x08
1175#define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1176#define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1177#define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1178 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1179
1180/* iseq.c */
1181RUBY_SYMBOL_EXPORT_BEGIN
1182
1183/* node -> iseq */
1184rb_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);
1185rb_iseq_t *rb_iseq_new_top (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1186rb_iseq_t *rb_iseq_new_main (const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1187rb_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);
1188rb_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,
1189 enum rb_iseq_type, const rb_compile_option_t*);
1190
1191struct iseq_link_anchor;
1193 VALUE flags;
1194 VALUE reserved;
1195 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1196 const void *data;
1197};
1198static inline struct rb_iseq_new_with_callback_callback_func *
1199rb_iseq_new_with_callback_new_callback(
1200 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1201{
1202 VALUE memo = rb_imemo_new(imemo_ifunc, (VALUE)func, (VALUE)ptr, Qundef, Qfalse);
1203 return (struct rb_iseq_new_with_callback_callback_func *)memo;
1204}
1205rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1206 VALUE name, VALUE path, VALUE realpath, int first_lineno,
1207 const rb_iseq_t *parent, enum rb_iseq_type, const rb_compile_option_t*);
1208
1209VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1210int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1211
1212VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1213
1214RUBY_EXTERN VALUE rb_cISeq;
1215RUBY_EXTERN VALUE rb_cRubyVM;
1216RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1217RUBY_EXTERN VALUE rb_block_param_proxy;
1218RUBY_SYMBOL_EXPORT_END
1219
1220#define GetProcPtr(obj, ptr) \
1221 GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1222
1223typedef struct {
1224 const struct rb_block block;
1225 unsigned int is_from_method: 1; /* bool */
1226 unsigned int is_lambda: 1; /* bool */
1227 unsigned int is_isolated: 1; /* bool */
1228} rb_proc_t;
1229
1230RUBY_SYMBOL_EXPORT_BEGIN
1231VALUE rb_proc_isolate(VALUE self);
1232VALUE rb_proc_isolate_bang(VALUE self);
1233VALUE rb_proc_ractor_make_shareable(VALUE self);
1234RUBY_SYMBOL_EXPORT_END
1235
1236typedef struct {
1237 VALUE flags; /* imemo header */
1238 rb_iseq_t *iseq;
1239 const VALUE *ep;
1240 const VALUE *env;
1241 unsigned int env_size;
1242} rb_env_t;
1243
1244extern const rb_data_type_t ruby_binding_data_type;
1245
1246#define GetBindingPtr(obj, ptr) \
1247 GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1248
1249typedef struct {
1250 const struct rb_block block;
1251 const VALUE pathobj;
1252 int first_lineno;
1253} rb_binding_t;
1254
1255/* used by compile time and send insn */
1256
1257enum vm_check_match_type {
1258 VM_CHECKMATCH_TYPE_WHEN = 1,
1259 VM_CHECKMATCH_TYPE_CASE = 2,
1260 VM_CHECKMATCH_TYPE_RESCUE = 3
1261};
1262
1263#define VM_CHECKMATCH_TYPE_MASK 0x03
1264#define VM_CHECKMATCH_ARRAY 0x04
1265
1266enum vm_special_object_type {
1267 VM_SPECIAL_OBJECT_VMCORE = 1,
1268 VM_SPECIAL_OBJECT_CBASE,
1269 VM_SPECIAL_OBJECT_CONST_BASE
1270};
1271
1272enum vm_svar_index {
1273 VM_SVAR_LASTLINE = 0, /* $_ */
1274 VM_SVAR_BACKREF = 1, /* $~ */
1275
1276 VM_SVAR_EXTRA_START = 2,
1277 VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1278};
1279
1280/* inline cache */
1281typedef struct iseq_inline_constant_cache *IC;
1282typedef struct iseq_inline_iv_cache_entry *IVC;
1283typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1284typedef union iseq_inline_storage_entry *ISE;
1285typedef const struct rb_callinfo *CALL_INFO;
1286typedef const struct rb_callcache *CALL_CACHE;
1287typedef struct rb_call_data *CALL_DATA;
1288
1289typedef VALUE CDHASH;
1290
1291#ifndef FUNC_FASTCALL
1292#define FUNC_FASTCALL(x) x
1293#endif
1294
1295typedef rb_control_frame_t *
1296 (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1297
1298#define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1299#define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1300
1301#define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1302#define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1303#define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1304
1305enum vm_frame_env_flags {
1306 /* Frame/Environment flag bits:
1307 * MMMM MMMM MMMM MMMM ____ FFFF FFFE EEEX (LSB)
1308 *
1309 * X : tag for GC marking (It seems as Fixnum)
1310 * EEE : 4 bits Env flags
1311 * FF..: 7 bits Frame flags
1312 * MM..: 15 bits frame magic (to check frame corruption)
1313 */
1314
1315 /* frame types */
1316 VM_FRAME_MAGIC_METHOD = 0x11110001,
1317 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1318 VM_FRAME_MAGIC_CLASS = 0x33330001,
1319 VM_FRAME_MAGIC_TOP = 0x44440001,
1320 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1321 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1322 VM_FRAME_MAGIC_EVAL = 0x77770001,
1323 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1324 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1325
1326 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1327
1328 /* frame flag */
1329 VM_FRAME_FLAG_FINISH = 0x0020,
1330 VM_FRAME_FLAG_BMETHOD = 0x0040,
1331 VM_FRAME_FLAG_CFRAME = 0x0080,
1332 VM_FRAME_FLAG_LAMBDA = 0x0100,
1333 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1334 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1335 VM_FRAME_FLAG_PASSED = 0x0800,
1336
1337 /* env flag */
1338 VM_ENV_FLAG_LOCAL = 0x0002,
1339 VM_ENV_FLAG_ESCAPED = 0x0004,
1340 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1341 VM_ENV_FLAG_ISOLATED = 0x0010,
1342};
1343
1344#define VM_ENV_DATA_SIZE ( 3)
1345
1346#define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1347#define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1348#define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1349#define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1350
1351#define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1352
1353static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1354
1355static inline void
1356VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1357{
1358 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1359 VM_ASSERT(FIXNUM_P(flags));
1360 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1361}
1362
1363static inline void
1364VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1365{
1366 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1367 VM_ASSERT(FIXNUM_P(flags));
1368 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1369}
1370
1371static inline unsigned long
1372VM_ENV_FLAGS(const VALUE *ep, long flag)
1373{
1374 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1375 VM_ASSERT(FIXNUM_P(flags));
1376 return flags & flag;
1377}
1378
1379static inline unsigned long
1380VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1381{
1382 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1383}
1384
1385static inline int
1386VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1387{
1388 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1389}
1390
1391static inline int
1392VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1393{
1394 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1395}
1396
1397static inline int
1398VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1399{
1400 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1401}
1402
1403static inline int
1404VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1405{
1406 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1407}
1408
1409static inline int
1410rb_obj_is_iseq(VALUE iseq)
1411{
1412 return imemo_type_p(iseq, imemo_iseq);
1413}
1414
1415#if VM_CHECK_MODE > 0
1416#define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1417#endif
1418
1419static inline int
1420VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1421{
1422 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1423 VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1424 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1425 return cframe_p;
1426}
1427
1428static inline int
1429VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1430{
1431 return !VM_FRAME_CFRAME_P(cfp);
1432}
1433
1434#define RUBYVM_CFUNC_FRAME_P(cfp) \
1435 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1436
1437#define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1438#define VM_BLOCK_HANDLER_NONE 0
1439
1440static inline int
1441VM_ENV_LOCAL_P(const VALUE *ep)
1442{
1443 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1444}
1445
1446static inline const VALUE *
1447VM_ENV_PREV_EP(const VALUE *ep)
1448{
1449 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1450 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1451}
1452
1453static inline VALUE
1454VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1455{
1456 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1457 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1458}
1459
1460#if VM_CHECK_MODE > 0
1461int rb_vm_ep_in_heap_p(const VALUE *ep);
1462#endif
1463
1464static inline int
1465VM_ENV_ESCAPED_P(const VALUE *ep)
1466{
1467 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1468 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1469}
1470
1471#if VM_CHECK_MODE > 0
1472static inline int
1473vm_assert_env(VALUE obj)
1474{
1475 VM_ASSERT(imemo_type_p(obj, imemo_env));
1476 return 1;
1477}
1478#endif
1479
1481static inline VALUE
1482VM_ENV_ENVVAL(const VALUE *ep)
1483{
1484 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1485 VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1486 VM_ASSERT(vm_assert_env(envval));
1487 return envval;
1488}
1489
1491static inline const rb_env_t *
1492VM_ENV_ENVVAL_PTR(const VALUE *ep)
1493{
1494 return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1495}
1496
1497static inline const rb_env_t *
1498vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1499{
1500 rb_env_t *env = (rb_env_t *)rb_imemo_new(imemo_env, (VALUE)env_ep, (VALUE)env_body, 0, (VALUE)iseq);
1501 env->env_size = env_size;
1502 env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1503 return env;
1504}
1505
1506static inline void
1507VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1508{
1509 *((VALUE *)ptr) = v;
1510}
1511
1512static inline void
1513VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1514{
1515 VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1516 VM_FORCE_WRITE(ptr, special_const_value);
1517}
1518
1519static inline void
1520VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1521{
1522 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1523 VM_FORCE_WRITE(&ep[index], v);
1524}
1525
1526const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1527const VALUE *rb_vm_proc_local_ep(VALUE proc);
1528void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep);
1529void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src);
1530
1531VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1532
1533#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1534#define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1535
1536#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1537 ((void *)(ecfp) > (void *)(cfp))
1538
1539static inline const rb_control_frame_t *
1540RUBY_VM_END_CONTROL_FRAME(const rb_execution_context_t *ec)
1541{
1542 return (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size);
1543}
1544
1545static inline int
1546RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1547{
1548 return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1549}
1550
1551static inline int
1552VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1553{
1554 if ((block_handler & 0x03) == 0x01) {
1555#if VM_CHECK_MODE > 0
1556 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1557 VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1558#endif
1559 return 1;
1560 }
1561 else {
1562 return 0;
1563 }
1564}
1565
1566static inline VALUE
1567VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1568{
1569 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1570 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1571 return block_handler;
1572}
1573
1574static inline const struct rb_captured_block *
1575VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1576{
1577 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1578 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1579 return captured;
1580}
1581
1582static inline int
1583VM_BH_IFUNC_P(VALUE block_handler)
1584{
1585 if ((block_handler & 0x03) == 0x03) {
1586#if VM_CHECK_MODE > 0
1587 struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1588 VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1589#endif
1590 return 1;
1591 }
1592 else {
1593 return 0;
1594 }
1595}
1596
1597static inline VALUE
1598VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1599{
1600 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1601 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1602 return block_handler;
1603}
1604
1605static inline const struct rb_captured_block *
1606VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1607{
1608 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1609 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1610 return captured;
1611}
1612
1613static inline const struct rb_captured_block *
1614VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1615{
1616 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1617 VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1618 return captured;
1619}
1620
1621static inline enum rb_block_handler_type
1622vm_block_handler_type(VALUE block_handler)
1623{
1624 if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1625 return block_handler_type_iseq;
1626 }
1627 else if (VM_BH_IFUNC_P(block_handler)) {
1628 return block_handler_type_ifunc;
1629 }
1630 else if (SYMBOL_P(block_handler)) {
1631 return block_handler_type_symbol;
1632 }
1633 else {
1634 VM_ASSERT(rb_obj_is_proc(block_handler));
1635 return block_handler_type_proc;
1636 }
1637}
1638
1639static inline void
1640vm_block_handler_verify(MAYBE_UNUSED(VALUE block_handler))
1641{
1642 VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1643 (vm_block_handler_type(block_handler), 1));
1644}
1645
1646static inline enum rb_block_type
1647vm_block_type(const struct rb_block *block)
1648{
1649#if VM_CHECK_MODE > 0
1650 switch (block->type) {
1651 case block_type_iseq:
1652 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1653 break;
1654 case block_type_ifunc:
1655 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1656 break;
1657 case block_type_symbol:
1658 VM_ASSERT(SYMBOL_P(block->as.symbol));
1659 break;
1660 case block_type_proc:
1661 VM_ASSERT(rb_obj_is_proc(block->as.proc));
1662 break;
1663 }
1664#endif
1665 return block->type;
1666}
1667
1668static inline void
1669vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1670{
1671 struct rb_block *mb = (struct rb_block *)block;
1672 mb->type = type;
1673}
1674
1675static inline const struct rb_block *
1676vm_proc_block(VALUE procval)
1677{
1678 VM_ASSERT(rb_obj_is_proc(procval));
1679 return &((rb_proc_t *)RTYPEDDATA_DATA(procval))->block;
1680}
1681
1682static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1683static inline const VALUE *vm_block_ep(const struct rb_block *block);
1684
1685static inline const rb_iseq_t *
1686vm_proc_iseq(VALUE procval)
1687{
1688 return vm_block_iseq(vm_proc_block(procval));
1689}
1690
1691static inline const VALUE *
1692vm_proc_ep(VALUE procval)
1693{
1694 return vm_block_ep(vm_proc_block(procval));
1695}
1696
1697static inline const rb_iseq_t *
1698vm_block_iseq(const struct rb_block *block)
1699{
1700 switch (vm_block_type(block)) {
1701 case block_type_iseq: return rb_iseq_check(block->as.captured.code.iseq);
1702 case block_type_proc: return vm_proc_iseq(block->as.proc);
1703 case block_type_ifunc:
1704 case block_type_symbol: return NULL;
1705 }
1706 VM_UNREACHABLE(vm_block_iseq);
1707 return NULL;
1708}
1709
1710static inline const VALUE *
1711vm_block_ep(const struct rb_block *block)
1712{
1713 switch (vm_block_type(block)) {
1714 case block_type_iseq:
1715 case block_type_ifunc: return block->as.captured.ep;
1716 case block_type_proc: return vm_proc_ep(block->as.proc);
1717 case block_type_symbol: return NULL;
1718 }
1719 VM_UNREACHABLE(vm_block_ep);
1720 return NULL;
1721}
1722
1723static inline VALUE
1724vm_block_self(const struct rb_block *block)
1725{
1726 switch (vm_block_type(block)) {
1727 case block_type_iseq:
1728 case block_type_ifunc:
1729 return block->as.captured.self;
1730 case block_type_proc:
1731 return vm_block_self(vm_proc_block(block->as.proc));
1732 case block_type_symbol:
1733 return Qundef;
1734 }
1735 VM_UNREACHABLE(vm_block_self);
1736 return Qundef;
1737}
1738
1739static inline VALUE
1740VM_BH_TO_SYMBOL(VALUE block_handler)
1741{
1742 VM_ASSERT(SYMBOL_P(block_handler));
1743 return block_handler;
1744}
1745
1746static inline VALUE
1747VM_BH_FROM_SYMBOL(VALUE symbol)
1748{
1749 VM_ASSERT(SYMBOL_P(symbol));
1750 return symbol;
1751}
1752
1753static inline VALUE
1754VM_BH_TO_PROC(VALUE block_handler)
1755{
1756 VM_ASSERT(rb_obj_is_proc(block_handler));
1757 return block_handler;
1758}
1759
1760static inline VALUE
1761VM_BH_FROM_PROC(VALUE procval)
1762{
1763 VM_ASSERT(rb_obj_is_proc(procval));
1764 return procval;
1765}
1766
1767/* VM related object allocate functions */
1768VALUE rb_thread_alloc(VALUE klass);
1769VALUE rb_binding_alloc(VALUE klass);
1770VALUE rb_proc_alloc(VALUE klass);
1771VALUE rb_proc_dup(VALUE self);
1772
1773/* for debug */
1774extern bool rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *);
1775extern bool rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc, FILE *);
1776extern bool rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *);
1777
1778#define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp, stderr)
1779#define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp), stderr)
1780bool rb_vm_bugreport(const void *, FILE *);
1781typedef void (*ruby_sighandler_t)(int);
1782RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 5)
1783NORETURN(void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *, const char *fmt, ...));
1784
1785/* functions about thread/vm execution */
1786RUBY_SYMBOL_EXPORT_BEGIN
1787VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1788VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1789VALUE rb_iseq_path(const rb_iseq_t *iseq);
1790VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
1791RUBY_SYMBOL_EXPORT_END
1792
1793VALUE rb_iseq_pathobj_new(VALUE path, VALUE realpath);
1794void rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath);
1795
1796int rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp);
1797void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause);
1798
1799VALUE rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, int argc, const VALUE *argv, int kw_splat, VALUE block_handler);
1800
1801VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
1802static inline VALUE
1803rb_vm_make_proc(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1804{
1805 return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1806}
1807
1808static inline VALUE
1809rb_vm_make_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1810{
1811 return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1812}
1813
1814VALUE rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp);
1815VALUE rb_vm_env_local_variables(const rb_env_t *env);
1816const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
1817const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars);
1818void rb_vm_inc_const_missing_count(void);
1819VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc,
1820 const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat);
1821void rb_vm_pop_frame_no_int(rb_execution_context_t *ec);
1822void rb_vm_pop_frame(rb_execution_context_t *ec);
1823
1824void rb_thread_start_timer_thread(void);
1825void rb_thread_stop_timer_thread(void);
1826void rb_thread_reset_timer_thread(void);
1827void rb_thread_wakeup_timer_thread(int);
1828
1829static inline void
1830rb_vm_living_threads_init(rb_vm_t *vm)
1831{
1832 ccan_list_head_init(&vm->waiting_fds);
1833 ccan_list_head_init(&vm->workqueue);
1834 ccan_list_head_init(&vm->ractor.set);
1835 ccan_list_head_init(&vm->ractor.sched.zombie_threads);
1836}
1837
1838typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
1839rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1840rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1841VALUE *rb_vm_svar_lep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1842int rb_vm_get_sourceline(const rb_control_frame_t *);
1843void rb_vm_stack_to_heap(rb_execution_context_t *ec);
1844void ruby_thread_init_stack(rb_thread_t *th);
1845rb_thread_t * ruby_thread_from_native(void);
1846int ruby_thread_set_native(rb_thread_t *th);
1847int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
1848void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp);
1849void rb_vm_env_write(const VALUE *ep, int index, VALUE v);
1850VALUE rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler);
1851
1852void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE exception_class, VALUE mesg);
1853
1854#define rb_vm_register_special_exception(sp, e, m) \
1855 rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1856
1857void rb_gc_mark_machine_stack(const rb_execution_context_t *ec);
1858
1859void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);
1860
1861const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
1862
1863#define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1864
1865#define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1866 STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1867 STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1868 const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1869 if (UNLIKELY((cfp) <= &bound[1])) { \
1870 vm_stackoverflow(); \
1871 } \
1872} while (0)
1873
1874#define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1875 CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1876
1877VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr);
1878
1879rb_execution_context_t *rb_vm_main_ractor_ec(rb_vm_t *vm); // ractor.c
1880
1881/* for thread */
1882
1883#if RUBY_VM_THREAD_MODEL == 2
1884
1885RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
1886RUBY_EXTERN rb_vm_t *ruby_current_vm_ptr;
1887RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
1888RUBY_EXTERN rb_event_flag_t ruby_vm_event_enabled_global_flags;
1889RUBY_EXTERN unsigned int ruby_vm_event_local_num;
1890
1891#define GET_VM() rb_current_vm()
1892#define GET_RACTOR() rb_current_ractor()
1893#define GET_THREAD() rb_current_thread()
1894#define GET_EC() rb_current_execution_context(true)
1895
1896static inline rb_thread_t *
1897rb_ec_thread_ptr(const rb_execution_context_t *ec)
1898{
1899 return ec->thread_ptr;
1900}
1901
1902static inline rb_ractor_t *
1903rb_ec_ractor_ptr(const rb_execution_context_t *ec)
1904{
1905 const rb_thread_t *th = rb_ec_thread_ptr(ec);
1906 if (th) {
1907 VM_ASSERT(th->ractor != NULL);
1908 return th->ractor;
1909 }
1910 else {
1911 return NULL;
1912 }
1913}
1914
1915static inline rb_vm_t *
1916rb_ec_vm_ptr(const rb_execution_context_t *ec)
1917{
1918 const rb_thread_t *th = rb_ec_thread_ptr(ec);
1919 if (th) {
1920 return th->vm;
1921 }
1922 else {
1923 return NULL;
1924 }
1925}
1926
1927static inline rb_execution_context_t *
1928rb_current_execution_context(bool expect_ec)
1929{
1930#ifdef RB_THREAD_LOCAL_SPECIFIER
1931 #ifdef __APPLE__
1932 rb_execution_context_t *ec = rb_current_ec();
1933 #else
1934 rb_execution_context_t *ec = ruby_current_ec;
1935 #endif
1936
1937 /* On the shared objects, `__tls_get_addr()` is used to access the TLS
1938 * and the address of the `ruby_current_ec` can be stored on a function
1939 * frame. However, this address can be mis-used after native thread
1940 * migration of a coroutine.
1941 * 1) Get `ptr =&ruby_current_ec` op NT1 and store it on the frame.
1942 * 2) Context switch and resume it on the NT2.
1943 * 3) `ptr` is used on NT2 but it accesses to the TLS on NT1.
1944 * This assertion checks such misusage.
1945 *
1946 * To avoid accidents, `GET_EC()` should be called once on the frame.
1947 * Note that inlining can produce the problem.
1948 */
1949 VM_ASSERT(ec == rb_current_ec_noinline());
1950#else
1951 rb_execution_context_t *ec = native_tls_get(ruby_current_ec_key);
1952#endif
1953 VM_ASSERT(!expect_ec || ec != NULL);
1954 return ec;
1955}
1956
1957static inline rb_thread_t *
1958rb_current_thread(void)
1959{
1960 const rb_execution_context_t *ec = GET_EC();
1961 return rb_ec_thread_ptr(ec);
1962}
1963
1964static inline rb_ractor_t *
1965rb_current_ractor_raw(bool expect)
1966{
1967 if (ruby_single_main_ractor) {
1968 return ruby_single_main_ractor;
1969 }
1970 else {
1971 const rb_execution_context_t *ec = rb_current_execution_context(expect);
1972 return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
1973 }
1974}
1975
1976static inline rb_ractor_t *
1977rb_current_ractor(void)
1978{
1979 return rb_current_ractor_raw(true);
1980}
1981
1982static inline rb_vm_t *
1983rb_current_vm(void)
1984{
1985#if 0 // TODO: reconsider the assertions
1986 VM_ASSERT(ruby_current_vm_ptr == NULL ||
1987 ruby_current_execution_context_ptr == NULL ||
1988 rb_ec_thread_ptr(GET_EC()) == NULL ||
1989 rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
1990 rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
1991#endif
1992
1993 return ruby_current_vm_ptr;
1994}
1995
1996void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
1997 unsigned int recorded_lock_rec,
1998 unsigned int current_lock_rec);
1999
2000static inline unsigned int
2001rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
2002{
2003 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2004
2005 if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
2006 return 0;
2007 }
2008 else {
2009 return vm->ractor.sync.lock_rec;
2010 }
2011}
2012
2013#else
2014#error "unsupported thread model"
2015#endif
2016
2017enum {
2018 TIMER_INTERRUPT_MASK = 0x01,
2019 PENDING_INTERRUPT_MASK = 0x02,
2020 POSTPONED_JOB_INTERRUPT_MASK = 0x04,
2021 TRAP_INTERRUPT_MASK = 0x08,
2022 TERMINATE_INTERRUPT_MASK = 0x10,
2023 VM_BARRIER_INTERRUPT_MASK = 0x20,
2024};
2025
2026#define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
2027#define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
2028#define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
2029#define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
2030#define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
2031#define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
2032#define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
2033 (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
2034
2035static inline bool
2036RUBY_VM_INTERRUPTED_ANY(rb_execution_context_t *ec)
2037{
2038#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
2039 uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
2040
2041 if (current_clock != ec->checked_clock) {
2042 ec->checked_clock = current_clock;
2043 RUBY_VM_SET_TIMER_INTERRUPT(ec);
2044 }
2045#endif
2046 return ec->interrupt_flag & ~(ec)->interrupt_mask;
2047}
2048
2049VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
2050int rb_signal_buff_size(void);
2051int rb_signal_exec(rb_thread_t *th, int sig);
2052void rb_threadptr_check_signal(rb_thread_t *mth);
2053void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
2054void rb_threadptr_signal_exit(rb_thread_t *th);
2055int rb_threadptr_execute_interrupts(rb_thread_t *, int);
2056void rb_threadptr_interrupt(rb_thread_t *th);
2057void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
2058void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
2059void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
2060VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec);
2061void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo);
2062void rb_execution_context_update(rb_execution_context_t *ec);
2063void rb_execution_context_mark(const rb_execution_context_t *ec);
2064void rb_fiber_close(rb_fiber_t *fib);
2065void Init_native_thread(rb_thread_t *th);
2066int rb_vm_check_ints_blocking(rb_execution_context_t *ec);
2067
2068// vm_sync.h
2069void rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond);
2070void rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec);
2071
2072#define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
2073static inline void
2074rb_vm_check_ints(rb_execution_context_t *ec)
2075{
2076#ifdef RUBY_ASSERT_CRITICAL_SECTION
2077 VM_ASSERT(ruby_assert_critical_section_entered == 0);
2078#endif
2079
2080 VM_ASSERT(ec == GET_EC());
2081
2082 if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2083 rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2084 }
2085}
2086
2087/* tracer */
2088
2090 rb_event_flag_t event;
2092 const rb_control_frame_t *cfp;
2093 VALUE self;
2094 ID id;
2095 ID called_id;
2096 VALUE klass;
2097 VALUE data;
2098
2099 int klass_solved;
2100
2101 /* calc from cfp */
2102 int lineno;
2103 VALUE path;
2104};
2105
2106void rb_hook_list_mark(rb_hook_list_t *hooks);
2107void rb_hook_list_mark_and_update(rb_hook_list_t *hooks);
2108void rb_hook_list_free(rb_hook_list_t *hooks);
2109void rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line);
2110void rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval);
2111
2112void rb_exec_event_hooks(struct rb_trace_arg_struct *trace_arg, rb_hook_list_t *hooks, int pop_p);
2113
2114#define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2115 const rb_event_flag_t flag_arg_ = (flag_); \
2116 rb_hook_list_t *hooks_arg_ = (hooks_); \
2117 if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2118 /* defer evaluating the other arguments */ \
2119 rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2120 } \
2121} while (0)
2122
2123static inline void
2124rb_exec_event_hook_orig(rb_execution_context_t *ec, rb_hook_list_t *hooks, rb_event_flag_t flag,
2125 VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
2126{
2127 struct rb_trace_arg_struct trace_arg;
2128
2129 VM_ASSERT((hooks->events & flag) != 0);
2130
2131 trace_arg.event = flag;
2132 trace_arg.ec = ec;
2133 trace_arg.cfp = ec->cfp;
2134 trace_arg.self = self;
2135 trace_arg.id = id;
2136 trace_arg.called_id = called_id;
2137 trace_arg.klass = klass;
2138 trace_arg.data = data;
2139 trace_arg.path = Qundef;
2140 trace_arg.klass_solved = 0;
2141
2142 rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2143}
2144
2146 VALUE self;
2147 uint32_t id;
2148 rb_hook_list_t hooks;
2149};
2150
2151static inline rb_hook_list_t *
2152rb_ec_ractor_hooks(const rb_execution_context_t *ec)
2153{
2154 struct rb_ractor_pub *cr_pub = (struct rb_ractor_pub *)rb_ec_ractor_ptr(ec);
2155 return &cr_pub->hooks;
2156}
2157
2158#define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2159 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2160
2161#define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2162 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2163
2164static inline void
2165rb_exec_event_hook_script_compiled(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE eval_script)
2166{
2167 EXEC_EVENT_HOOK(ec, RUBY_EVENT_SCRIPT_COMPILED, ec->cfp->self, 0, 0, 0,
2168 NIL_P(eval_script) ? (VALUE)iseq :
2169 rb_ary_new_from_args(2, eval_script, (VALUE)iseq));
2170}
2171
2172void rb_vm_trap_exit(rb_vm_t *vm);
2173void rb_vm_postponed_job_atfork(void); /* vm_trace.c */
2174void rb_vm_postponed_job_free(void); /* vm_trace.c */
2175size_t rb_vm_memsize_postponed_job_queue(void); /* vm_trace.c */
2176void rb_vm_postponed_job_queue_init(rb_vm_t *vm); /* vm_trace.c */
2177
2178RUBY_SYMBOL_EXPORT_BEGIN
2179
2180int rb_thread_check_trap_pending(void);
2181
2182/* #define RUBY_EVENT_RESERVED_FOR_INTERNAL_USE 0x030000 */ /* from vm_core.h */
2183#define RUBY_EVENT_COVERAGE_LINE 0x010000
2184#define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2185
2186extern VALUE rb_get_coverages(void);
2187extern void rb_set_coverages(VALUE, int, VALUE);
2188extern void rb_clear_coverages(void);
2189extern void rb_reset_coverages(void);
2190extern void rb_resume_coverages(void);
2191extern void rb_suspend_coverages(void);
2192
2193void rb_postponed_job_flush(rb_vm_t *vm);
2194
2195// ractor.c
2196RUBY_EXTERN VALUE rb_eRactorUnsafeError;
2197RUBY_EXTERN VALUE rb_eRactorIsolationError;
2198
2199RUBY_SYMBOL_EXPORT_END
2200
2201#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:135
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:966
struct rb_iseq_constant_body::@152 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