Ruby 4.1.0dev (2026-09-22 revision d41370aec5370c96a7a1a62b1be84d21347901fc)
vm_core.h (d41370aec5370c96a7a1a62b1be84d21347901fc)
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, ...) \
60 RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr RBIMPL_VA_OPT_ARGS(__VA_ARGS__))
61#define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
62#define RUBY_ASSERT_CRITICAL_SECTION
63#define RUBY_DEBUG_THREAD_SCHEDULE() rb_thread_schedule()
64#else
65#define VM_ASSERT(/*expr, */...) ((void)0)
66#define VM_UNREACHABLE(func) UNREACHABLE
67#define RUBY_DEBUG_THREAD_SCHEDULE()
68#endif
69
70#define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
71
72#if defined(RUBY_ASSERT_CRITICAL_SECTION)
73/*
74# Critical Section Assertions
75
76These assertions are used to ensure that context switching does not occur between two points in the code. In theory,
77such code should already be protected by a mutex, but these assertions are used to ensure that the mutex is held.
78
79The specific case where it can be useful is where a mutex is held further up the call stack, and the code in question
80may not directly hold the mutex. In this case, the critical section assertions can be used to ensure that the mutex is
81held by someone else.
82
83These assertions are only enabled when RUBY_ASSERT_CRITICAL_SECTION is defined, which is only defined if VM_CHECK_MODE
84is set.
85
86## Example Usage
87
88```c
89RUBY_ASSERT_CRITICAL_SECTION_ENTER();
90// ... some code which does not invoke rb_vm_check_ints() ...
91RUBY_ASSERT_CRITICAL_SECTION_LEAVE();
92```
93
94If `rb_vm_check_ints()` is called between the `RUBY_ASSERT_CRITICAL_SECTION_ENTER()` and
95`RUBY_ASSERT_CRITICAL_SECTION_LEAVE()`, a failed assertion will result.
96*/
97#define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{GET_EC()->assert_critical_section_entered += 1;}while(false)
98#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{rb_execution_context_t *ec__ = GET_EC();VM_ASSERT(ec__->assert_critical_section_entered > 0);ec__->assert_critical_section_entered -= 1;}while(false)
99#else
100#define RUBY_ASSERT_CRITICAL_SECTION_ENTER()
101#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE()
102#endif
103
104#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
105# include "wasm/setjmp.h"
106#else
107# include <setjmp.h>
108#endif
109
110#if defined(__linux__) || defined(__FreeBSD__)
111# define RB_THREAD_T_HAS_NATIVE_ID
112#endif
113
115#include "ccan/list/list.h"
116#include "id.h"
117#include "internal.h"
118#include "internal/array.h"
119#include "internal/basic_operators.h"
120#include "internal/box.h"
121#include "internal/sanitizers.h"
122#include "internal/serial.h"
123#include "internal/set_table.h"
124#include "internal/vm.h"
125#include "method.h"
126#include "node.h"
127#include "ruby/ruby.h"
128#include "ruby/st.h"
129#include "ruby_atomic.h"
130#include "vm_opts.h"
131
132#include "ruby/thread_native.h"
133/*
134 * implementation selector of get_insn_info algorithm
135 * 0: linear search
136 * 1: binary search
137 * 2: succinct bitvector
138 */
139#ifndef VM_INSN_INFO_TABLE_IMPL
140# define VM_INSN_INFO_TABLE_IMPL 2
141#endif
142
143#if defined(NSIG_MAX) /* POSIX issue 8 */
144# undef NSIG
145# define NSIG NSIG_MAX
146#elif defined(_SIG_MAXSIG) /* FreeBSD */
147# undef NSIG
148# define NSIG _SIG_MAXSIG
149#elif defined(_SIGMAX) /* QNX */
150# define NSIG (_SIGMAX + 1)
151#elif defined(NSIG) /* 99% of everything else */
152# /* take it */
153#else /* Last resort */
154# define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
155#endif
156
157#define RUBY_NSIG NSIG
158
159#if defined(SIGCLD)
160# define RUBY_SIGCHLD (SIGCLD)
161#elif defined(SIGCHLD)
162# define RUBY_SIGCHLD (SIGCHLD)
163#endif
164
165#if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
166# define USE_SIGALTSTACK
167void *rb_allocate_sigaltstack(void);
168void *rb_register_sigaltstack(void *);
169# define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
170# define RB_ALTSTACK_FREE(var) free(var)
171# define RB_ALTSTACK(var) var
172#else /* noop */
173# define RB_ALTSTACK_INIT(var, altstack)
174# define RB_ALTSTACK_FREE(var)
175# define RB_ALTSTACK(var) (0)
176#endif
177
178#include THREAD_IMPL_H
179#define RUBY_VM_THREAD_MODEL 2
180
181/*****************/
182/* configuration */
183/*****************/
184
185/* gcc ver. check */
186#if defined(__GNUC__) && __GNUC__ >= 2
187
188#if OPT_TOKEN_THREADED_CODE
189#if OPT_DIRECT_THREADED_CODE
190#undef OPT_DIRECT_THREADED_CODE
191#endif
192#endif
193
194#else /* defined(__GNUC__) && __GNUC__ >= 2 */
195
196/* disable threaded code options */
197#if OPT_DIRECT_THREADED_CODE
198#undef OPT_DIRECT_THREADED_CODE
199#endif
200#if OPT_TOKEN_THREADED_CODE
201#undef OPT_TOKEN_THREADED_CODE
202#endif
203#endif
204
205/* call threaded code */
206#if OPT_CALL_THREADED_CODE
207#if OPT_DIRECT_THREADED_CODE
208#undef OPT_DIRECT_THREADED_CODE
209#endif /* OPT_DIRECT_THREADED_CODE */
210#endif /* OPT_CALL_THREADED_CODE */
211
212void rb_vm_encoded_insn_data_table_init(void);
213typedef unsigned long rb_num_t;
214typedef signed long rb_snum_t;
215
216enum ruby_tag_type {
217 RUBY_TAG_NONE = 0x0,
218 RUBY_TAG_RETURN = 0x1,
219 RUBY_TAG_BREAK = 0x2,
220 RUBY_TAG_NEXT = 0x3,
221 RUBY_TAG_RETRY = 0x4,
222 RUBY_TAG_REDO = 0x5,
223 RUBY_TAG_RAISE = 0x6,
224 RUBY_TAG_THROW = 0x7,
225 RUBY_TAG_FATAL = 0x8,
226 RUBY_TAG_MASK = 0xf
227};
228
229#define TAG_NONE RUBY_TAG_NONE
230#define TAG_RETURN RUBY_TAG_RETURN
231#define TAG_BREAK RUBY_TAG_BREAK
232#define TAG_NEXT RUBY_TAG_NEXT
233#define TAG_RETRY RUBY_TAG_RETRY
234#define TAG_REDO RUBY_TAG_REDO
235#define TAG_RAISE RUBY_TAG_RAISE
236#define TAG_THROW RUBY_TAG_THROW
237#define TAG_FATAL RUBY_TAG_FATAL
238#define TAG_MASK RUBY_TAG_MASK
239
240enum ruby_vm_throw_flags {
241 VM_THROW_NO_ESCAPE_FLAG = 0x8000,
242 VM_THROW_STATE_MASK = 0xff
243};
244
245/* forward declarations */
246struct rb_thread_struct;
248
249/* iseq data type */
251
253 rb_serial_t raw;
254 VALUE data[2];
255};
256
257#define IMEMO_CONST_CACHE_SHAREABLE IMEMO_FL_USER0
258
259// imemo_constcache
261 VALUE flags;
262
263 VALUE value;
264 const rb_cref_t *ic_cref;
265 /* Ractor that filled this entry. An unshareable value may be handed out again
266 * only to that Ractor: it is the one that passed the owner check. */
267 rb_serial_t ractor_id;
268};
269STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
270 (offsetof(struct iseq_inline_constant_cache_entry, ractor_id) +
271 sizeof(rb_serial_t)) <= RVALUE_SIZE);
272
289
291 uint64_t value; // Either rb_setivar_cache or rb_getivar_cache packed in a uint64_t.
292 ID iv_set_name;
293};
294
298
300 struct {
301 struct rb_thread_struct *running_thread;
302 VALUE value;
303 } once;
304 struct iseq_inline_constant_cache ic_cache;
305 struct iseq_inline_iv_cache_entry iv_cache;
306};
307
309 const struct rb_call_data *cd;
310 const struct rb_callcache *cc;
311 VALUE block_handler;
312 VALUE recv;
313 int argc;
314 bool kw_splat;
315 VALUE heap_argv;
316};
317
318#ifndef VM_ARGC_STACK_MAX
319#define VM_ARGC_STACK_MAX 128
320#endif
321
322#define VM_KW_SPECIFIED_BITS_MAX (32-1) /* TODO: 32 -> Fixnum's max bits */
323
324# define CALLING_ARGC(calling) ((calling)->heap_argv ? RARRAY_LENINT((calling)->heap_argv) : (calling)->argc)
325
327
328#ifndef RUBY_CORE_DATA_TYPE_CHECK
329# if RUBY_DEBUG
330# define RUBY_CORE_DATA_TYPE_CHECK 1
331# else
332# define RUBY_CORE_DATA_TYPE_CHECK 0
333# endif
334#endif
335#if !RUBY_CORE_DATA_TYPE_CHECK
336#define GetCoreDataFromValue(obj, type, data_type, ptr) ((ptr) = (type*)RTYPEDDATA_GET_DATA(obj))
337#else
338#define GetCoreDataFromValue(obj, type, data_type, ptr) TypedData_Get_Struct(obj, type, data_type, ptr)
339#endif
340
342 VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
343 VALUE label; /* String */
344 int first_lineno;
345 int node_id;
346 rb_code_location_t code_location;
348
349#define PATHOBJ_PATH 0
350#define PATHOBJ_REALPATH 1
351
352static inline VALUE
353pathobj_path(VALUE pathobj)
354{
355 if (RB_TYPE_P(pathobj, T_STRING)) {
356 return pathobj;
357 }
358 else {
359 VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
360 return RARRAY_AREF(pathobj, PATHOBJ_PATH);
361 }
362}
363
364static inline VALUE
365pathobj_realpath(VALUE pathobj)
366{
367 if (RB_TYPE_P(pathobj, T_STRING)) {
368 return pathobj;
369 }
370 else {
371 VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
372 return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
373 }
374}
375
376/* Forward declarations */
377typedef uintptr_t iseq_bits_t;
378
379#define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
380
381/* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
382#define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
383
384/* instruction sequence type */
385enum rb_iseq_type {
386 ISEQ_TYPE_TOP,
387 ISEQ_TYPE_METHOD,
388 ISEQ_TYPE_BLOCK,
389 ISEQ_TYPE_CLASS,
390 ISEQ_TYPE_RESCUE,
391 ISEQ_TYPE_ENSURE,
392 ISEQ_TYPE_EVAL,
393 ISEQ_TYPE_MAIN,
394 ISEQ_TYPE_PLAIN
395};
396
397// Attributes specified by Primitive.attr!
398enum rb_builtin_attr {
399 // The iseq does not call methods.
400 BUILTIN_ATTR_LEAF = 0x01,
401 // This iseq only contains single `opt_invokebuiltin_delegate_leave` instruction with 0 arguments.
402 BUILTIN_ATTR_SINGLE_NOARG_LEAF = 0x02,
403 // This attribute signals JIT to duplicate the iseq for each block iseq so that its `yield` will be monomorphic.
404 BUILTIN_ATTR_INLINE_BLOCK = 0x04,
405 // The iseq acts like a C method in backtraces.
406 BUILTIN_ATTR_C_TRACE = 0x08,
407 // The iseq uses noint branch/jump opcodes that skip interrupt checking.
408 BUILTIN_ATTR_WITHOUT_INTERRUPTS = 0x10,
409 // The iseq operates on the nearest user box in its caller frames.
410 BUILTIN_ATTR_CALLER_USER_BOX = 0x20,
411};
412
413typedef VALUE (*rb_jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *);
414typedef VALUE (*rb_zjit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *, rb_jit_func_t);
415
416enum lvar_state {
417 lvar_uninitialized,
418 lvar_initialized,
419 lvar_reassigned,
420};
421
422/* Lazily-allocated per-iseq variable data. NULL when unused (the common case:
423 * no coverage, no script_lines, no flip-flops, no disassembly). */
425 rb_snum_t flip_count;
426 VALUE script_lines;
427 VALUE coverage;
428 VALUE pc2branchindex;
429 VALUE *original_iseq;
430};
431
433 enum rb_iseq_type type;
434
435 unsigned int iseq_size;
436 VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
437
462 struct {
463 unsigned int has_lead : 1;
464 unsigned int has_opt : 1;
465 unsigned int has_rest : 1;
466 unsigned int has_post : 1;
467 unsigned int has_kw : 1;
468 unsigned int has_kwrest : 1;
469 unsigned int has_block : 1;
470
471 unsigned int ambiguous_param0 : 1; /* {|a|} */
472 unsigned int accepts_no_kwarg : 1;
473 unsigned int ruby2_keywords: 1;
474 unsigned int anon_rest: 1;
475 unsigned int anon_kwrest: 1;
476 unsigned int use_block: 1;
477 unsigned int forwardable: 1;
478 unsigned int accepts_no_block: 1;
479 } flags;
480
481 unsigned int size;
482
483 int lead_num;
484 int opt_num;
485 int rest_start;
486 int post_start;
487 int post_num;
488 int block_start;
489
490 const VALUE *opt_table; /* (opt_num + 1) entries. */
491 /* opt_num and opt_table:
492 *
493 * def foo o1=e1, o2=e2, ..., oN=eN
494 * #=>
495 * # prologue code
496 * A1: e1
497 * A2: e2
498 * ...
499 * AN: eN
500 * AL: body
501 * opt_num = N
502 * opt_table = [A1, A2, ..., AN, AL]
503 */
504
506 int num;
507 int required_num;
508 int bits_start;
509 int rest_start;
510 const ID *table;
511 VALUE *default_values;
512 } *keyword;
513 } param;
514
515 rb_iseq_location_t location;
516
517 /* insn info, must be freed */
519 const struct iseq_insn_info_entry *body;
520 union {
521 unsigned int *positions;
522#if VM_INSN_INFO_TABLE_IMPL == 2
523 struct succ_index_table *succ_index_table;
524#endif
525 } positions_or_succ_index_table;
526 unsigned int size;
527 } insns_info;
528
529 const ID *local_table; /* must free */
530
531 union {
532 uint8_t *list;
533 uint8_t single[sizeof(uint8_t *)];
534 } lvar_states;
535
536 /* catch table */
537 struct iseq_catch_table *catch_table;
538
539 /* for child iseq */
540 const struct rb_iseq_struct *parent_iseq;
541 struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
542
543 union iseq_inline_storage_entry *is_entries; /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
544 struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
545
546 struct rb_iseq_variable *variable;
547
548 unsigned int local_table_size;
549 unsigned int ic_size; // Number of IC caches
550 unsigned int ise_size; // Number of ISE caches
551 unsigned int ivc_size; // Number of IVC caches
552 unsigned int icvarc_size; // Number of ICVARC caches
553 unsigned int ci_size;
554 unsigned int stack_max; /* for stack overflow check */
555
556 unsigned int builtin_attrs; // Union of rb_builtin_attr
557
558 bool prism; // ISEQ was generated from prism compiler
559
560 // Set once an EP escape of this iseq has been reported to the enabled JIT.
561 rb_atomic_t jit_ep_escape_recorded;
562
563 union {
564 iseq_bits_t * list; /* Find references for GC */
565 iseq_bits_t single;
566 } mark_bits;
567
568 struct rb_id_table *outer_variables;
569
570 const rb_iseq_t *mandatory_only_iseq;
571
572#if USE_YJIT || USE_ZJIT
573 // Number of calls on jit_exec()
574 unsigned int jit_entry_calls;
575 // Number of calls on jit_exec_exception()
576 unsigned int jit_exception_calls;
577 // Function pointer for JIT code on jit_exec()
578 rb_jit_func_t jit_entry;
579 // Function pointer for JIT code on jit_exec_exception()
580 rb_jit_func_t jit_exception;
581 void *jit_payload;
582#endif
583
584#if USE_YJIT
585 // Used to estimate how frequently this ISEQ gets called
586 unsigned int yjit_calls_at_interv;
587#endif
588
589 // Hash of the source this iseq was compiled from, or 0 if it is
590 // unavailable. A computed hash of 0 is remapped to another value, so
591 // 0 never denotes a real hash.
592 uint64_t source_hash;
593};
594
595/* T_IMEMO/iseq */
596/* typedef rb_iseq_t is in method.h */
598 VALUE flags; /* 1 */
599
600 struct rb_iseq_constant_body *body; /* 2 */
601
602 union { /* 3, 4 words */
603 struct iseq_compile_data *compile_data; /* used at compile time */
604
605 struct {
606 VALUE obj;
607 int index;
608 } loader;
609
610 struct {
611 unsigned int local_hooks_cnt;
612 rb_event_flag_t global_trace_events;
613 } exec;
614 } aux;
615};
616
617#define ISEQ_BODY(iseq) ((iseq)->body)
618
619#if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
620#define USE_LAZY_LOAD 0
621#endif
622
623#if !USE_LAZY_LOAD
624static inline const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq) {return 0;}
625#endif
626const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
627
628static inline const rb_iseq_t *
629rb_iseq_check(const rb_iseq_t *iseq)
630{
631 if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
632 rb_iseq_complete((rb_iseq_t *)iseq);
633 }
634 return iseq;
635}
636
637static inline bool
638rb_iseq_attr_p(const rb_iseq_t *iseq, enum rb_builtin_attr attr)
639{
640 return (ISEQ_BODY(iseq)->builtin_attrs & attr) == attr;
641}
642
643static inline const rb_iseq_t *
644def_iseq_ptr(rb_method_definition_t *def)
645{
646//TODO: re-visit. to check the bug, enable this assertion.
647#if VM_CHECK_MODE > 0
648 if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
649#endif
650 return rb_iseq_check(def->body.iseq.iseqptr);
651}
652
653enum ruby_special_exceptions {
654 ruby_error_reenter,
655 ruby_error_nomemory,
656 ruby_error_sysstack,
657 ruby_error_stackfatal,
658 ruby_error_stream_closed,
659 ruby_special_error_count
660};
661
662extern const rb_data_type_t ruby_vm_data_type;
663
664#define GetVMPtr(obj, ptr) \
665 GetCoreDataFromValue((obj), rb_vm_t, &ruby_vm_data_type, (ptr))
666
667struct rb_vm_struct;
668typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
669
670typedef struct rb_at_exit_list {
671 rb_vm_at_exit_func *func;
672 struct rb_at_exit_list *next;
674
675void rb_gc_init_objspaces(void);
676void rb_objspace_free(void *objspace);
677void rb_objspace_call_finalizer(void);
678
679enum rb_hook_list_type {
680 hook_list_type_ractor_local,
681 hook_list_type_targeted_iseq,
682 hook_list_type_targeted_def, // C function
683 hook_list_type_global
684};
685
686typedef struct rb_hook_list_struct {
687 struct rb_event_hook_struct *hooks;
688 rb_event_flag_t events;
689 unsigned int running;
690 enum rb_hook_list_type type;
691 bool need_clean;
693
694// see builtin.h for definition
695typedef const struct rb_builtin_function *RB_BUILTIN;
696
697/* The mark redirect used by the object-traversal APIs
698 * (rb_objspace_reachable_objects_from etc.). It is installed while a traversal
699 * runs and is NULL during a real GC. Storage is per-Ractor
700 * (rb_ractor_t.mark_func_data); on a modular GC, threads without a current
701 * Ractor fall back to rb_vm_struct's gc sub-struct (see gc.c). */
703 void *data;
704 void (*mark_func)(VALUE v, void *data);
705 /* Marker set while a shareable-verification walk runs (read by
706 * rb_gc_checking_shareable). The slot is per-Ractor, so it only affects the
707 * walk of the Ractor doing the verification. */
708 bool checking_shareable;
709};
710
711typedef struct rb_vm_struct {
712 VALUE self;
713
714 struct {
715 struct ccan_list_head set;
716 /* For a single-objspace impl (mmtk): Ractors between termination and
717 * ractor_free. The global root scan keeps marking their
718 * registered_marks. */
719 struct ccan_list_head terminated_set;
720 unsigned int cnt;
721 unsigned int blocking_cnt;
722
723 struct rb_ractor_struct *main_ractor;
724 struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
725
726 struct {
727 // monitor
728 rb_nativethread_lock_t lock;
729 struct rb_ractor_struct *lock_owner;
730 unsigned int lock_rec;
731
732 // join at exit
733 rb_nativethread_cond_t terminate_cond;
734 bool terminate_waiting;
735 } sync;
736
737 /* VM-wide locks for the Ractor transfer/inheritance machinery. All of them
738 * are leaf locks: no safepoint inside a critical section. */
739 rb_nativethread_lock_t generic_fields_lock; /* the shared generic-fields table in variable.c */
740
741 // ractor scheduling; see thread_sched.h
742 struct rb_ractor_sched sched;
743 } ractor;
744
745#ifdef USE_SIGALTSTACK
746 void *main_altstack;
747#endif
748
749 rb_serial_t fork_gen;
750
751 /* set in single-threaded processes only: */
752 volatile int ubf_async_safe;
753
754 unsigned int running: 1;
755 unsigned int thread_abort_on_exception: 1;
756 unsigned int thread_report_on_exception: 1;
757 unsigned int thread_ignore_deadlock: 1;
758
759 /* object management */
760 const VALUE special_exceptions[ruby_special_error_count];
761
762 /* Ruby Box */
763 rb_box_t *master_box;
764 rb_box_t *root_box;
765 rb_box_t *main_box;
766
767 /* load */
768 // For running the init function of statically linked
769 // extensions when they are loaded
770 struct st_table static_ext_inits;
771
772 /* signal */
773 struct {
774 VALUE cmd[RUBY_NSIG];
775 } trap_list;
776
777 /* hook (for internal events: NEWOBJ, FREEOBJ, GC events, etc.) */
778 rb_hook_list_t global_hooks;
779
780 int src_encoding_index;
781
782
783 /* `once` completion event (see vm_once_dispatch) */
784 rb_nativethread_lock_t once_lock;
785 rb_nativethread_cond_t once_cond;
786
787 VALUE orig_progname, progname;
788 VALUE coverages, cme2counter, me_set;
789 int coverage_mode;
790
791 struct {
792 /* The VM only points at rb_global_objspace, the process-wide GC data such as
793 * the page pool. Each Ractor owns its own rb_objspace through r->objspace,
794 * and the boot objspace belongs to the main Ractor. */
795 struct rb_global_objspace *global_objspace;
796 /* Objspaces of terminated, not-yet-inherited Ractors. No mutator runs in
797 * them; a global GC sweeps them under the barrier (missing one leaves stale
798 * mark bits = UAF), inheritance merges them under the VM lock. owner_slot is
799 * the dead Ractor's r->objspace, cleared when inherited. */
800 struct rb_objspace_zombie {
801 void *objspace;
802 void **owner_slot;
803 /* The terminated Ractor owning this zombie; a root scan reaches its
804 * rb_gc_register_mark_object pins and join value through it. NULL for an
805 * orphan, whose Ractor struct is gone and has neither any more. */
806 struct rb_ractor_struct *owner;
807 /* Heap pages this zombie holds: measured when it retires and refreshed
808 * under the barrier of each global cycle. The total below stays exactly
809 * in sync, entry by entry. */
810 size_t pages;
811 } *zombie_objspaces;
812 size_t zombie_objspaces_count;
813 size_t zombie_objspaces_capa;
814 /* Sum of .pages over zombie_objspaces. Between global cycles it is an upper
815 * bound: a zombie's heap never grows and only shrinks at a global cycle. */
816 size_t zombie_total_pages;
817
818#if USE_MODULAR_GC
819 struct gc_mark_func_data_struct *mark_func_data;
820#endif
821 /* One VM-wide list for rb_gc_register_address: a slot can later hold another
822 * objspace's value, so it is not split per Ractor and every Ractor's GC scans it
823 * conservatively. Leaf lock; register/unregister are cold paths. */
824 struct {
825 rb_nativethread_lock_t lock;
826 VALUE **addrs; /* rb_gc_register_address: mark_maybe on *addr */
827 size_t addrs_cnt, addrs_capa;
828 } registered_globals;
829
830 /* Holders keeping GC disabled (atomic): Ractors that called GC.disable (at
831 * most one hold each) plus short internal critical sections. One holder stops
832 * GC everywhere; GC.enable releases only the caller's own hold, never
833 * overriding another Ractor's disable. */
834 rb_atomic_t disable_holders;
835 /* Handle of the postponed job that merges an orphan objspace into the main
836 * one (rb_postponed_job_handle_t; POSTPONED_JOB_HANDLE_INVALID when not
837 * registered). */
838 unsigned int orphan_merge_pjob;
839 /* Used to resolve the objspace during VM teardown (the cleanup path of
840 * rb_gc_get_objspace). */
841 void *cleanup_objspace;
842 } gc;
843
844 rb_at_exit_list *at_exit;
845
846 const struct rb_builtin_function *builtin_function_table;
847
848 st_table ci_table;
849 struct rb_id_table negative_cme_table;
850 st_table overloaded_cme_table; // cme -> overloaded_cme
851 set_table unused_block_warning_table;
852 VALUE cc_refinement_set;
853
854 // This id table contains a mapping from ID to ICs. It does this with ID
855 // keys and nested st_tables as values. The nested tables have ICs as keys
856 // and Qtrue as values. It is used when inline constant caches need to be
857 // invalidated or ISEQs are being freed.
858 struct rb_id_table constant_cache;
859 ID inserting_constant_cache_id;
860
861#ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
862#define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
863#endif
864 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
865 bool global_cc_cache_table_used; // vm_eval.c
866
867#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
868 uint32_t clock;
869#endif
870
871 /* params */
872 struct { /* size in byte */
873 size_t thread_vm_stack_size;
874 size_t thread_machine_stack_size;
875 size_t fiber_vm_stack_size;
876 size_t fiber_machine_stack_size;
877 } default_params;
878} rb_vm_t;
879
880extern bool ruby_vm_during_cleanup;
881
882/* default values */
883
884#define RUBY_VM_SIZE_ALIGN 4096
885
886#define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
887#define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
888#define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
889#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
890
891#define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
892#define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
893#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
894#if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
895#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
896#else
897#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
898#endif
899
900#if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer) || __has_feature(leak_sanitizer)
901/* It seems sanitizers consume A LOT of machine stacks */
902#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
903#define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
904#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
905#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
906#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
907#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
908#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
909#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
910#endif
911
912#ifndef VM_DEBUG_BP_CHECK
913#define VM_DEBUG_BP_CHECK 0
914#endif
915
916#ifndef VM_DEBUG_VERIFY_METHOD_CACHE
917#define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
918#endif
919
921 VALUE self;
922 const VALUE *ep;
923 union {
924 const rb_iseq_t *iseq;
925 const struct vm_ifunc *ifunc;
926 VALUE val;
927 } code;
928};
929
930enum rb_block_handler_type {
931 block_handler_type_iseq,
932 block_handler_type_ifunc,
933 block_handler_type_symbol,
934 block_handler_type_proc
935};
936
937enum rb_block_type {
938 block_type_iseq,
939 block_type_ifunc,
940 block_type_symbol,
941 block_type_proc
942};
943
944struct rb_block {
945 enum rb_block_type type : 8;
946 union {
947 struct rb_captured_block captured;
948 VALUE symbol;
949 VALUE proc;
950 } as;
951};
952
954 const VALUE *pc; // cfp[0]
955 VALUE *sp; // cfp[1]
956 const rb_iseq_t *_iseq; // cfp[2] -- use CFP_ISEQ(cfp) to read
957 VALUE self; // cfp[3] / block[0]
958 const VALUE *ep; // cfp[4] / block[1]
959 const void *block_code; // cfp[5] / block[2] -- iseq, ifunc, or forwarded block handler
960 void *jit_return; // cfp[6] -- return address for JIT code
961#if VM_DEBUG_BP_CHECK
962 VALUE *bp_check; // cfp[7]
963#endif
965
966extern const rb_data_type_t ruby_threadptr_data_type;
967
968static inline struct rb_thread_struct *
969rb_thread_ptr(VALUE thval)
970{
971 return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
972}
973
974enum rb_thread_status {
975 THREAD_RUNNABLE,
976 THREAD_STOPPED,
977 THREAD_STOPPED_FOREVER,
978 THREAD_KILLED
979};
980
981#ifdef RUBY_JMP_BUF
982typedef RUBY_JMP_BUF rb_jmpbuf_t;
983#else
984typedef void *rb_jmpbuf_t[5];
985#endif
986
987/*
988 `rb_vm_tag_jmpbuf_t` type represents a buffer used to
989 long jump to a C frame associated with `rb_vm_tag`.
990
991 Use-site of `rb_vm_tag_jmpbuf_t` is responsible for calling the
992 following functions:
993 - `rb_vm_tag_jmpbuf_init` once `rb_vm_tag_jmpbuf_t` is allocated.
994 - `rb_vm_tag_jmpbuf_deinit` once `rb_vm_tag_jmpbuf_t` is no longer necessary.
995
996 `RB_VM_TAG_JMPBUF_GET` transforms a `rb_vm_tag_jmpbuf_t` into a
997 `rb_jmpbuf_t` to be passed to `rb_setjmp/rb_longjmp`.
998*/
999#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
1000/*
1001 WebAssembly target with Asyncify-based SJLJ needs
1002 to capture the execution context by unwind/rewind-ing
1003 call frames into a jump buffer. The buffer space tends
1004 to be considerably large unlike other architectures'
1005 register-based buffers.
1006 Therefore, we allocates the buffer on the heap on such
1007 environments.
1008*/
1009typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
1010
1011#define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
1012
1013static inline void
1014rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
1015{
1016 *jmpbuf = ruby_xmalloc(sizeof(rb_jmpbuf_t));
1017}
1018
1019static inline void
1020rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
1021{
1022 ruby_xfree(*jmpbuf);
1023}
1024#else
1025typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
1026
1027#define RB_VM_TAG_JMPBUF_GET(buf) (buf)
1028
1029static inline void
1030rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
1031{
1032 // no-op
1033}
1034
1035static inline void
1036rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
1037{
1038 // no-op
1039}
1040#endif
1041
1042/*
1043 the members which are written in EC_PUSH_TAG() should be placed at
1044 the beginning and the end, so that entire region is accessible.
1045*/
1047 VALUE tag;
1048 VALUE retval;
1049 rb_vm_tag_jmpbuf_t buf;
1050 struct rb_vm_tag *prev;
1051 enum ruby_tag_type state;
1052 unsigned int lock_rec;
1053#if USE_ZJIT
1054 // ec->cfp as of EC_PUSH_TAG, which is saved for materializing JITFrame.
1055 rb_control_frame_t *cfp;
1056 // Whether cfp had a ZJIT frame before this tag's setjmp was established.
1057 // It's used for checking if zjit_materialize_frames should materialize
1058 // the frame or not when the tag is popped. If zjit_frame_active is true,
1059 // we don't want to materialize cfp->jit_return, which will still be used
1060 // by JIT code.
1061 bool zjit_frame_active;
1062#endif
1063};
1064
1065STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
1066STATIC_ASSERT(rb_vm_tag_buf_end,
1067 offsetof(struct rb_vm_tag, buf) + sizeof(rb_vm_tag_jmpbuf_t) <
1068 sizeof(struct rb_vm_tag));
1069
1072 void *arg;
1073 rb_atomic_t event_serial;
1074};
1075
1076struct rb_mutex_struct;
1077
1078typedef struct rb_fiber_struct rb_fiber_t;
1079
1081 struct rb_waiting_list *next;
1082 struct rb_thread_struct *thread;
1083 struct rb_fiber_struct *fiber;
1084};
1085
1086
1088 /* execution information */
1089 VALUE *vm_stack; /* must free, must mark */
1090 size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
1091 rb_control_frame_t *cfp;
1092
1093 struct rb_vm_tag *tag;
1094
1095 /* interrupt flags */
1096 rb_atomic_t interrupt_flag;
1097 rb_atomic_t interrupt_mask; /* size should match flag */
1098#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1099 uint32_t checked_clock;
1100#endif
1101
1102 rb_fiber_t *fiber_ptr;
1103 struct rb_thread_struct *thread_ptr;
1104 rb_serial_t serial;
1105 rb_serial_t ractor_id;
1106
1107 /* storage (ec (fiber) local) */
1108 struct rb_id_table *local_storage;
1109 VALUE local_storage_recursive_hash;
1110 VALUE local_storage_recursive_hash_for_trace;
1111
1112 /* Inheritable fiber storage. */
1113 VALUE storage;
1114
1115 /* eval env */
1116 const VALUE *root_lep;
1117 VALUE root_svar;
1118
1119 /* trace information */
1120 struct rb_trace_arg_struct *trace_arg;
1121
1122 /* temporary places */
1123 VALUE errinfo;
1124 VALUE passed_block_handler; /* for rb_iterate */
1125
1126 uint8_t raised_flag; /* only 3 bits needed */
1127
1128 /* n.b. only 7 bits needed, really: */
1129 BITFIELD(enum method_missing_reason, method_missing_reason, 8);
1130
1131 VALUE private_const_reference;
1132
1133 struct {
1134 VALUE obj;
1135 VALUE fields_obj;
1136 } gen_fields_cache;
1137
1138 /* for GC */
1139 struct {
1140 VALUE *stack_start;
1141 VALUE *stack_end;
1142 size_t stack_maxsize;
1144
1145#ifdef RUBY_ASAN_ENABLED
1146 void *asan_fake_stack_handle;
1147#endif
1148 } machine;
1149
1150#ifdef RUBY_ASSERT_CRITICAL_SECTION
1151 int assert_critical_section_entered;
1152#endif
1153};
1154
1155#ifndef rb_execution_context_t
1157#define rb_execution_context_t rb_execution_context_t
1158#endif
1159
1160// for builtin.h
1161#define VM_CORE_H_EC_DEFINED 1
1162
1163// Set the vm_stack pointer in the execution context.
1164void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1165
1166// Initialize the vm_stack pointer in the execution context and push the initial stack frame.
1167// @param ec the execution context to update.
1168// @param stack a pointer to the stack to use.
1169// @param size the size of the stack, as in `VALUE stack[size]`.
1170void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1171
1172// Clear (set to `NULL`) the vm_stack pointer.
1173// @param ec the execution context to update.
1174void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
1175
1176// Close an execution context and free related resources that are no longer needed.
1177// @param ec the execution context to close.
1178void rb_ec_close(rb_execution_context_t *ec);
1179
1181 bool ractor_safe;
1182};
1183
1184typedef struct rb_ractor_struct rb_ractor_t;
1185
1186struct rb_native_thread;
1187
1188typedef struct rb_thread_struct {
1189 struct ccan_list_node lt_node; // managed by a ractor (r->threads.set)
1190 VALUE self;
1191 rb_ractor_t *ractor;
1192 rb_vm_t *vm;
1193 struct rb_native_thread *nt;
1195
1196 struct rb_thread_sched_item sched;
1197 bool mn_schedulable;
1198 rb_atomic_t serial; // only for RUBY_DEBUG_LOG()
1199
1200 VALUE last_status; /* $? */
1201
1202 /* for cfunc */
1203 struct rb_calling_info *calling;
1204
1205 /* for load(true) */
1206 VALUE top_self;
1207 VALUE top_wrapper;
1208
1209 /* thread control */
1210
1211 BITFIELD(enum rb_thread_status, status, 2);
1212 /* bit flags */
1213 unsigned int main_thread : 1;
1214 unsigned int has_dedicated_nt : 1;
1215 unsigned int to_kill : 1;
1216 unsigned int abort_on_exception: 1;
1217 unsigned int report_on_exception: 1;
1218 unsigned int pending_interrupt_queue_checked: 1;
1219 int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1220 uint32_t running_time_us; /* 12500..800000 */
1221
1222 void *blocking_region_buffer;
1223
1224 VALUE thgroup;
1225 VALUE value;
1226
1227 /* temporary place of retval on OPT_CALL_THREADED_CODE */
1228#if OPT_CALL_THREADED_CODE
1229 VALUE retval;
1230#endif
1231
1232 /* async errinfo queue */
1233 VALUE pending_interrupt_queue;
1234 VALUE pending_interrupt_mask_stack;
1235
1236 /* interrupt management */
1237 rb_nativethread_lock_t interrupt_lock;
1238 struct rb_unblock_callback unblock;
1239 VALUE locking_mutex;
1240 struct rb_mutex_struct *keeping_mutexes;
1241 struct ccan_list_head interrupt_exec_tasks;
1242
1243 struct rb_waiting_list *join_list;
1244
1245 union {
1246 struct {
1247 VALUE proc;
1248 VALUE args;
1249 int kw_splat;
1250 } proc;
1251 struct {
1252 VALUE (*func)(void *);
1253 void *arg;
1254 } func;
1255 } invoke_arg;
1256
1257 enum thread_invoke_type {
1258 thread_invoke_type_none = 0,
1259 thread_invoke_type_proc,
1260 thread_invoke_type_ractor_proc,
1261 thread_invoke_type_func
1262 } invoke_type;
1263
1264 /* fiber */
1265 rb_fiber_t *root_fiber;
1266
1267 VALUE scheduler;
1268 unsigned int blocking;
1269
1270 /* misc */
1271 VALUE name;
1272 void **specific_storage;
1273
1274 struct rb_ext_config ext_config;
1275} rb_thread_t;
1276
1277static inline unsigned int
1278rb_th_serial(const rb_thread_t *th)
1279{
1280 return th ? (unsigned int)th->serial : 0;
1281}
1282
1283typedef enum {
1284 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1285 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1286 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1287 /* 0x03..0x06 is reserved */
1288 VM_DEFINECLASS_TYPE_MASK = 0x07
1289} rb_vm_defineclass_type_t;
1290
1291#define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1292#define VM_DEFINECLASS_FLAG_SCOPED 0x08
1293#define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1294#define VM_DEFINECLASS_FLAG_DYNAMIC_CREF 0x20
1295#define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1296#define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1297 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1298#define VM_DEFINECLASS_DYNAMIC_CREF_P(x) \
1299 ((x) & VM_DEFINECLASS_FLAG_DYNAMIC_CREF)
1300
1301/* iseq.c */
1302RUBY_SYMBOL_EXPORT_BEGIN
1303
1304/* node -> iseq */
1305rb_iseq_t *rb_iseq_new (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type);
1306rb_iseq_t *rb_iseq_new_top (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1307rb_iseq_t *rb_iseq_new_main (const VALUE ast_value, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1308rb_iseq_t *rb_iseq_new_eval (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth);
1309rb_iseq_t *rb_iseq_new_with_opt( VALUE ast_value, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1310 enum rb_iseq_type, const rb_compile_option_t*,
1311 VALUE script_lines);
1312
1313struct iseq_link_anchor;
1315 VALUE flags;
1316 VALUE reserved;
1317 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1318 const void *data;
1319};
1320static inline struct rb_iseq_new_with_callback_callback_func *
1321rb_iseq_new_with_callback_new_callback(
1322 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1323{
1325 IMEMO_NEW(struct rb_iseq_new_with_callback_callback_func, imemo_ifunc, Qfalse);
1326 memo->func = func;
1327 memo->data = ptr;
1328
1329 return memo;
1330}
1331rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1332 VALUE name, VALUE path, VALUE realpath, int first_lineno,
1333 const rb_iseq_t *parent, enum rb_iseq_type, const rb_compile_option_t*);
1334
1335VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1336int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1337
1338VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1339
1340RUBY_EXTERN VALUE rb_cISeq;
1341RUBY_EXTERN VALUE rb_cRubyVM;
1342RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1343RUBY_EXTERN VALUE rb_block_param_proxy;
1344RUBY_SYMBOL_EXPORT_END
1345
1346extern const rb_data_type_t ruby_proc_data_type;
1347
1348#define GetProcPtr(obj, ptr) \
1349 GetCoreDataFromValue((obj), rb_proc_t, &ruby_proc_data_type, (ptr))
1350
1351typedef struct {
1352 enum rb_block_type type : 8;
1353 unsigned int is_from_method: 1; /* bool */
1354 unsigned int is_lambda: 1; /* bool */
1355 unsigned int is_isolated: 1; /* bool */
1356 unsigned int is_refined: 1; /* bool: Proc#refined */
1358
1359typedef struct {
1360 rb_proc_header_t header;
1361 struct rb_captured_block captured;
1363
1364typedef struct {
1365 rb_proc_header_t header;
1366 VALUE symbol;
1368
1369typedef struct {
1370 rb_proc_header_t header;
1371 VALUE proc;
1373
1374/* A Proc of any block type. */
1375typedef union {
1376 const struct rb_block block;
1377 rb_proc_header_t header;
1378 rb_proc_captured_t captured;
1379 rb_proc_symbol_t symbol;
1380 rb_proc_proc_t proc;
1381} rb_proc_t;
1382
1383STATIC_ASSERT(rb_proc_captured_offset,
1384 offsetof(rb_proc_captured_t, captured) == offsetof(rb_proc_t, block.as.captured));
1385STATIC_ASSERT(rb_proc_symbol_offset,
1386 offsetof(rb_proc_symbol_t, symbol) == offsetof(rb_proc_t, block.as.symbol));
1387STATIC_ASSERT(rb_proc_proc_offset,
1388 offsetof(rb_proc_proc_t, proc) == offsetof(rb_proc_t, block.as.proc));
1389
1390/* A refined proc's refinements recipe (see Proc#refined) lives in a hidden
1391 * ivar on the proc object; the accessors return nil/NULL unless is_refined is
1392 * set. rb_proc_refinements_cref_for_call also makes the copy of the block
1393 * that Proc#refined defers until the first call, so it can raise and must not
1394 * be called outside a tag. */
1395VALUE rb_proc_refinements_recipe(VALUE procval);
1396void rb_proc_set_refinements_recipe(VALUE procval, VALUE recipe);
1397const rb_cref_t *rb_proc_refinements_cref_for_call(VALUE procval);
1398
1399RUBY_SYMBOL_EXPORT_BEGIN
1400VALUE rb_proc_isolate(VALUE self);
1401VALUE rb_proc_isolate_bang(VALUE self, VALUE replace_self);
1402VALUE rb_proc_ractor_make_shareable(VALUE proc, VALUE replace_self);
1403RUBY_SYMBOL_EXPORT_END
1404
1405typedef struct {
1406 VALUE flags; /* imemo header */
1407 rb_iseq_t *iseq;
1408 const VALUE *ep;
1409 const VALUE *env;
1410 unsigned int env_size;
1411} rb_env_t;
1412
1413extern const rb_data_type_t ruby_binding_data_type;
1414
1415#define GetBindingPtr(obj, ptr) \
1416 GetCoreDataFromValue((obj), rb_binding_t, &ruby_binding_data_type, (ptr))
1417
1418typedef struct {
1419 const struct rb_block block;
1420 const VALUE pathobj;
1421 int first_lineno;
1422} rb_binding_t;
1423
1424/* used by compile time and send insn */
1425
1426enum vm_check_match_type {
1427 VM_CHECKMATCH_TYPE_WHEN = 1,
1428 VM_CHECKMATCH_TYPE_CASE = 2,
1429 VM_CHECKMATCH_TYPE_RESCUE = 3
1430};
1431
1432#define VM_CHECKMATCH_TYPE_MASK 0x03
1433#define VM_CHECKMATCH_ARRAY 0x04
1434
1435enum vm_opt_newarray_send_type {
1436 VM_OPT_NEWARRAY_SEND_MAX = 1,
1437 VM_OPT_NEWARRAY_SEND_MIN = 2,
1438 VM_OPT_NEWARRAY_SEND_HASH = 3,
1439 VM_OPT_NEWARRAY_SEND_PACK = 4,
1440 VM_OPT_NEWARRAY_SEND_PACK_BUFFER = 5,
1441 VM_OPT_NEWARRAY_SEND_INCLUDE_P = 6,
1442};
1443
1444enum vm_special_object_type {
1445 VM_SPECIAL_OBJECT_VMCORE = 1,
1446 VM_SPECIAL_OBJECT_CBASE,
1447 VM_SPECIAL_OBJECT_CONST_BASE
1448};
1449
1450enum vm_svar_index {
1451 VM_SVAR_LASTLINE = 0, /* $_ */
1452 VM_SVAR_BACKREF = 1, /* $~ */
1453
1454 VM_SVAR_EXTRA_START = 2,
1455 VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1456};
1457
1458/* inline cache */
1459typedef struct iseq_inline_constant_cache *IC;
1460typedef struct iseq_inline_iv_cache_entry *IVC;
1461typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1462typedef union iseq_inline_storage_entry *ISE;
1463typedef const struct rb_callinfo *CALL_INFO;
1464typedef const struct rb_callcache *CALL_CACHE;
1465typedef struct rb_call_data *CALL_DATA;
1466
1467typedef VALUE CDHASH;
1468
1469#ifndef FUNC_FASTCALL
1470#define FUNC_FASTCALL(x) x
1471#endif
1472
1473typedef rb_control_frame_t *
1474 (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1475
1476#define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1477#define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1478
1479#define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1480#define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1481#define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1482
1483enum vm_frame_env_flags {
1484 /* Frame/Environment flag bits:
1485 * MMMM MMMM MMMM MMMM ___F FFFF FFFE EEEX (LSB)
1486 *
1487 * X : tag for GC marking (It seems as Fixnum)
1488 * EEE : 4 bits Env flags
1489 * FF..: 8 bits Frame flags
1490 * MM..: 15 bits frame magic (to check frame corruption)
1491 */
1492
1493 /* frame types */
1494 VM_FRAME_MAGIC_METHOD = 0x11110001,
1495 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1496 VM_FRAME_MAGIC_CLASS = 0x33330001,
1497 VM_FRAME_MAGIC_TOP = 0x44440001,
1498 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1499 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1500 VM_FRAME_MAGIC_EVAL = 0x77770001,
1501 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1502 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1503
1504 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1505
1506 /* frame flag */
1507 VM_FRAME_FLAG_FINISH = 0x0020,
1508 VM_FRAME_FLAG_BMETHOD = 0x0040,
1509 VM_FRAME_FLAG_CFRAME = 0x0080,
1510 VM_FRAME_FLAG_LAMBDA = 0x0100,
1511 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1512 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1513 VM_FRAME_FLAG_PASSED = 0x0800,
1514 VM_FRAME_FLAG_BOX_REQUIRE = 0x1000,
1515
1516 /* env flag */
1517 VM_ENV_FLAG_LOCAL = 0x0002,
1518 VM_ENV_FLAG_ESCAPED = 0x0004,
1519 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1520 VM_ENV_FLAG_ISOLATED = 0x0010,
1521};
1522
1523#define VM_ENV_DATA_SIZE ( 3)
1524
1525#define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1526#define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1527#define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1528#define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1529
1530#define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1531
1532static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1533
1534static inline void
1535VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1536{
1537 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1538 VM_ASSERT(FIXNUM_P(flags));
1539 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1540}
1541
1542static inline void
1543VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1544{
1545 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1546 VM_ASSERT(FIXNUM_P(flags));
1547 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1548}
1549
1550static inline unsigned long
1551VM_ENV_FLAGS(const VALUE *ep, long flag)
1552{
1553 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1554 VM_ASSERT(FIXNUM_P(flags));
1555 return flags & flag;
1556}
1557
1558static inline unsigned long
1559VM_ENV_FLAGS_UNCHECKED(const VALUE *ep, long flag)
1560{
1561 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1562 return flags & flag;
1563}
1564
1565static inline unsigned long
1566VM_ENV_FRAME_TYPE_P(const VALUE *ep, unsigned long frame_type)
1567{
1568 return VM_ENV_FLAGS(ep, VM_FRAME_MAGIC_MASK) == frame_type;
1569}
1570
1571static inline unsigned long
1572VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1573{
1574 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1575}
1576
1577static inline unsigned long
1578VM_FRAME_TYPE_UNCHECKED(const rb_control_frame_t *cfp)
1579{
1580 return VM_ENV_FLAGS_UNCHECKED(cfp->ep, VM_FRAME_MAGIC_MASK);
1581}
1582
1583static inline int
1584VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1585{
1586 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1587}
1588
1589static inline int
1590VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1591{
1592 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1593}
1594
1595static inline int
1596VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1597{
1598 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1599}
1600
1601static inline int
1602VM_FRAME_FINISHED_P_UNCHECKED(const rb_control_frame_t *cfp)
1603{
1604 return VM_ENV_FLAGS_UNCHECKED(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1605}
1606
1607static inline int
1608VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1609{
1610 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1611}
1612
1613static inline int
1614rb_obj_is_iseq(VALUE iseq)
1615{
1616 return imemo_type_p(iseq, imemo_iseq);
1617}
1618
1619#if VM_CHECK_MODE > 0
1620#define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1621#endif
1622
1623static inline int
1624VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1625{
1626 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1627 // With zjit_jit_frame, cfp->_iseq may be stale (not yet materialized),
1628 // so skip this assertion when jit_return is set (zjit.h is not available here).
1629 VM_ASSERT(cfp->jit_return ||
1630 RUBY_VM_NORMAL_ISEQ_P(cfp->_iseq) != cframe_p ||
1631 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1632 return cframe_p;
1633}
1634
1635static inline int
1636VM_FRAME_CFRAME_P_UNCHECKED(const rb_control_frame_t *cfp)
1637{
1638 return VM_ENV_FLAGS_UNCHECKED(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1639}
1640
1641static inline int
1642VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1643{
1644 return !VM_FRAME_CFRAME_P(cfp);
1645}
1646
1647static inline int
1648VM_FRAME_RUBYFRAME_P_UNCHECKED(const rb_control_frame_t *cfp)
1649{
1650 return !VM_FRAME_CFRAME_P_UNCHECKED(cfp);
1651}
1652
1653static inline int
1654VM_FRAME_NS_REQUIRE_P(const rb_control_frame_t *cfp)
1655{
1656 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BOX_REQUIRE) != 0;
1657}
1658
1659#define RUBYVM_CFUNC_FRAME_P(cfp) \
1660 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1661
1662#define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1663#define VM_BLOCK_HANDLER_NONE 0
1664
1665static inline int
1666VM_ENV_LOCAL_P(const VALUE *ep)
1667{
1668 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1669}
1670
1671static inline int
1672VM_ENV_LOCAL_P_UNCHECKED(const VALUE *ep)
1673{
1674 return VM_ENV_FLAGS_UNCHECKED(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1675}
1676
1677static inline const VALUE *
1678VM_ENV_PREV_EP_UNCHECKED(const VALUE *ep)
1679{
1680 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1681}
1682
1683static inline const VALUE *
1684VM_ENV_PREV_EP(const VALUE *ep)
1685{
1686 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1687 return VM_ENV_PREV_EP_UNCHECKED(ep);
1688}
1689
1690static inline bool
1691VM_ENV_BOXED_P(const VALUE *ep)
1692{
1693 return VM_ENV_FRAME_TYPE_P(ep, VM_FRAME_MAGIC_CLASS) || VM_ENV_FRAME_TYPE_P(ep, VM_FRAME_MAGIC_TOP);
1694}
1695
1696static inline VALUE
1697VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1698{
1699 if (VM_ENV_BOXED_P(ep)) {
1700 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1701 return VM_BLOCK_HANDLER_NONE;
1702 }
1703
1704 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1705 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1706}
1707
1708static inline const rb_box_t *
1709VM_ENV_BOX(const VALUE *ep)
1710{
1711 VM_ASSERT(VM_ENV_BOXED_P(ep));
1712 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1713 return (const rb_box_t *)GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1714}
1715
1716static inline const rb_box_t *
1717VM_ENV_BOX_UNCHECKED(const VALUE *ep)
1718{
1719 return (const rb_box_t *)GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1720}
1721
1722#if VM_CHECK_MODE > 0
1723int rb_vm_ep_in_heap_p(const VALUE *ep);
1724#endif
1725
1726static inline int
1727VM_ENV_ESCAPED_P(const VALUE *ep)
1728{
1729 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1730 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1731}
1732
1734static inline VALUE
1735VM_ENV_ENVVAL(const VALUE *ep)
1736{
1737 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1738 VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1739 VM_ASSERT(envval == Qundef || imemo_type_p(envval, imemo_env));
1740 return envval;
1741}
1742
1744static inline const rb_env_t *
1745VM_ENV_ENVVAL_PTR(const VALUE *ep)
1746{
1747 return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1748}
1749
1750static inline const rb_env_t *
1751vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1752{
1753 rb_env_t *env = IMEMO_NEW(rb_env_t, imemo_env, (VALUE)iseq);
1754 env->ep = env_ep;
1755 env->env = env_body;
1756 env->env_size = env_size;
1757 env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1758 return env;
1759}
1760
1761static inline void
1762VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1763{
1764 *((VALUE *)ptr) = v;
1765}
1766
1767static inline void
1768VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1769{
1770 VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1771 VM_FORCE_WRITE(ptr, special_const_value);
1772}
1773
1774static inline void
1775VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1776{
1777 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1778 VM_FORCE_WRITE(&ep[index], v);
1779}
1780
1781const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1782const VALUE *rb_vm_proc_local_ep(VALUE proc);
1783void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep);
1784void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src);
1785
1786VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1787
1788#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1789#define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1790
1791#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1792 ((void *)(ecfp) > (void *)(cfp))
1793
1794static inline const rb_control_frame_t *
1795RUBY_VM_END_CONTROL_FRAME(const rb_execution_context_t *ec)
1796{
1797 return (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size);
1798}
1799
1800static inline int
1801RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1802{
1803 return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1804}
1805
1806static inline int
1807VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1808{
1809 if ((block_handler & 0x03) == 0x01) {
1810#if VM_CHECK_MODE > 0
1811 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1812 if (!imemo_type_p(captured->code.val, imemo_iseq)) {
1813 rb_bug("not imemo_iseq. captured:%p IMEMO_P(captured->code.val):%d, "
1814 "flags:%.*" PRIxVALUE,
1815 (void *)captured,
1816 RB_TYPE_P(captured->code.val, T_IMEMO),
1817 (int)(sizeof(VALUE) * CHAR_BIT / 4), RBASIC(captured->code.val)->flags);
1818 }
1819#endif
1820 return 1;
1821 }
1822 else {
1823 return 0;
1824 }
1825}
1826
1827static inline VALUE
1828VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1829{
1830 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1831 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1832 return block_handler;
1833}
1834
1835static inline const struct rb_captured_block *
1836VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1837{
1838 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1839 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1840 return captured;
1841}
1842
1843static inline int
1844VM_BH_IFUNC_P(VALUE block_handler)
1845{
1846 if ((block_handler & 0x03) == 0x03) {
1847#if VM_CHECK_MODE > 0
1848 struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1849 VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1850#endif
1851 return 1;
1852 }
1853 else {
1854 return 0;
1855 }
1856}
1857
1858static inline VALUE
1859VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1860{
1861 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1862 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1863 return block_handler;
1864}
1865
1866static inline const struct rb_captured_block *
1867VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1868{
1869 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1870 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1871 return captured;
1872}
1873
1874static inline const struct rb_captured_block *
1875VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1876{
1877 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1878 VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1879 return captured;
1880}
1881
1882static inline enum rb_block_handler_type
1883vm_block_handler_type(VALUE block_handler)
1884{
1885 if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1886 return block_handler_type_iseq;
1887 }
1888 else if (VM_BH_IFUNC_P(block_handler)) {
1889 return block_handler_type_ifunc;
1890 }
1891 else if (SYMBOL_P(block_handler)) {
1892 return block_handler_type_symbol;
1893 }
1894 else {
1895 VM_ASSERT(rb_obj_is_proc(block_handler));
1896 return block_handler_type_proc;
1897 }
1898}
1899
1900static inline void
1901vm_block_handler_verify(MAYBE_UNUSED(VALUE block_handler))
1902{
1903 VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1904 (vm_block_handler_type(block_handler), 1));
1905}
1906
1907static inline enum rb_block_type
1908vm_block_type(const struct rb_block *block)
1909{
1910#if VM_CHECK_MODE > 0
1911 switch (block->type) {
1912 case block_type_iseq:
1913 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1914 break;
1915 case block_type_ifunc:
1916 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1917 break;
1918 case block_type_symbol:
1919 VM_ASSERT(SYMBOL_P(block->as.symbol));
1920 break;
1921 case block_type_proc:
1922 VM_ASSERT(rb_obj_is_proc(block->as.proc));
1923 break;
1924 }
1925#endif
1926 return block->type;
1927}
1928
1929static inline void
1930vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1931{
1932 struct rb_block *mb = (struct rb_block *)block;
1933 mb->type = type;
1934}
1935
1936static inline const struct rb_block *
1937vm_proc_block(VALUE procval)
1938{
1939 VM_ASSERT(rb_obj_is_proc(procval));
1940 return &((rb_proc_t *)RTYPEDDATA_DATA(procval))->block;
1941}
1942
1943static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1944static inline const VALUE *vm_block_ep(const struct rb_block *block);
1945
1946static inline const rb_iseq_t *
1947vm_proc_iseq(VALUE procval)
1948{
1949 return vm_block_iseq(vm_proc_block(procval));
1950}
1951
1952static inline const VALUE *
1953vm_proc_ep(VALUE procval)
1954{
1955 return vm_block_ep(vm_proc_block(procval));
1956}
1957
1958static inline const rb_iseq_t *
1959vm_block_iseq(const struct rb_block *block)
1960{
1961 switch (vm_block_type(block)) {
1962 case block_type_iseq: return rb_iseq_check(block->as.captured.code.iseq);
1963 case block_type_proc: return vm_proc_iseq(block->as.proc);
1964 case block_type_ifunc:
1965 case block_type_symbol: return NULL;
1966 }
1967 VM_UNREACHABLE(vm_block_iseq);
1968 return NULL;
1969}
1970
1971static inline const VALUE *
1972vm_block_ep(const struct rb_block *block)
1973{
1974 switch (vm_block_type(block)) {
1975 case block_type_iseq:
1976 case block_type_ifunc: return block->as.captured.ep;
1977 case block_type_proc: return vm_proc_ep(block->as.proc);
1978 case block_type_symbol: return NULL;
1979 }
1980 VM_UNREACHABLE(vm_block_ep);
1981 return NULL;
1982}
1983
1984static inline VALUE
1985vm_block_self(const struct rb_block *block)
1986{
1987 switch (vm_block_type(block)) {
1988 case block_type_iseq:
1989 case block_type_ifunc:
1990 return block->as.captured.self;
1991 case block_type_proc:
1992 return vm_block_self(vm_proc_block(block->as.proc));
1993 case block_type_symbol:
1994 return Qundef;
1995 }
1996 VM_UNREACHABLE(vm_block_self);
1997 return Qundef;
1998}
1999
2000static inline VALUE
2001VM_BH_TO_SYMBOL(VALUE block_handler)
2002{
2003 VM_ASSERT(SYMBOL_P(block_handler));
2004 return block_handler;
2005}
2006
2007static inline VALUE
2008VM_BH_FROM_SYMBOL(VALUE symbol)
2009{
2010 VM_ASSERT(SYMBOL_P(symbol));
2011 return symbol;
2012}
2013
2014static inline VALUE
2015VM_BH_TO_PROC(VALUE block_handler)
2016{
2017 VM_ASSERT(rb_obj_is_proc(block_handler));
2018 return block_handler;
2019}
2020
2021static inline VALUE
2022VM_BH_FROM_PROC(VALUE procval)
2023{
2024 VM_ASSERT(rb_obj_is_proc(procval));
2025 return procval;
2026}
2027
2028/* VM related object allocate functions */
2029VALUE rb_thread_alloc(VALUE klass);
2030/* Build the Thread out of objects a named objspace owns; only a Ractor building its
2031 * child needs this (create_ractor_alloc_thread). */
2032VALUE rb_thread_alloc_in_objspace(VALUE klass, void *objspace);
2033VALUE rb_binding_alloc(VALUE klass);
2034VALUE rb_proc_alloc(VALUE klass, enum rb_block_type block_type);
2035VALUE rb_proc_dup(VALUE self);
2036VALUE rb_proc_dup_0(VALUE self);
2037
2038/* for debug */
2039extern bool rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *);
2040extern bool rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc, FILE *);
2041extern bool rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, FILE *);
2042
2043#define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp, stderr)
2044#define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp), stderr)
2045bool rb_vm_bugreport(const void *, FILE *);
2046typedef void (*ruby_sighandler_t)(int);
2047RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 5)
2048NORETURN(void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *, const char *fmt, ...));
2049
2050/* functions about thread/vm execution */
2051RUBY_SYMBOL_EXPORT_BEGIN
2052VALUE rb_iseq_eval(const rb_iseq_t *iseq, const rb_box_t *box);
2053VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
2054VALUE rb_iseq_path(const rb_iseq_t *iseq);
2055VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
2056RUBY_SYMBOL_EXPORT_END
2057
2058VALUE rb_iseq_pathobj_new(VALUE path, VALUE realpath);
2059void rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath);
2060
2061int rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp);
2062void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause);
2063
2064VALUE rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, int argc, const VALUE *argv, int kw_splat, VALUE block_handler, const rb_cref_t *cref);
2065
2066VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
2067static inline VALUE
2068rb_vm_make_proc(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
2069{
2070 return rb_vm_make_proc_lambda(ec, captured, klass, 0);
2071}
2072
2073static inline VALUE
2074rb_vm_make_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
2075{
2076 return rb_vm_make_proc_lambda(ec, captured, klass, 1);
2077}
2078
2079VALUE rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp);
2080VALUE rb_vm_env_local_variables(const rb_env_t *env);
2081VALUE rb_vm_env_numbered_parameters(const rb_env_t *env);
2082const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
2083const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars);
2084void rb_vm_inc_const_missing_count(void);
2085VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc,
2086 const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat);
2087void rb_vm_pop_frame_no_int(rb_execution_context_t *ec);
2088void rb_vm_pop_frame(rb_execution_context_t *ec);
2089
2090void rb_thread_start_timer_thread(void);
2091void rb_thread_stop_timer_thread(void);
2092void rb_thread_reset_timer_thread(void);
2093void rb_thread_wakeup_timer_thread(int);
2094
2095static inline void
2096rb_vm_living_threads_init(rb_vm_t *vm)
2097{
2098 ccan_list_head_init(&vm->ractor.set);
2099 ccan_list_head_init(&vm->ractor.terminated_set);
2100}
2101
2102typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
2103rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
2104rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
2105VALUE *rb_vm_svar_lep(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
2106int rb_vm_get_sourceline(const rb_control_frame_t *);
2107void rb_vm_stack_to_heap(rb_execution_context_t *ec);
2108void ruby_thread_init_stack(rb_thread_t *th, void *local_in_parent_frame);
2109void rb_thread_malloc_stack_set(rb_thread_t *th, void *stack, size_t stack_size);
2110rb_thread_t * ruby_thread_from_native(void);
2111int ruby_thread_set_native(rb_thread_t *th);
2112int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
2113void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp);
2114void rb_vm_env_write(const VALUE *ep, int index, VALUE v);
2115VALUE rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler);
2116
2117void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE exception_class, VALUE mesg);
2118
2119#define rb_vm_register_special_exception(sp, e, m) \
2120 rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
2121
2122void rb_gc_mark_machine_context(const rb_execution_context_t *ec);
2123
2124const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
2125const rb_callable_method_entry_t *rb_vm_frame_method_entry_unchecked(const rb_control_frame_t *cfp);
2126
2127#define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
2128
2129#define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
2130 STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
2131 STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
2132 const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
2133 if (UNLIKELY((cfp) <= &bound[1])) { \
2134 vm_stackoverflow(); \
2135 } \
2136} while (0)
2137
2138#define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
2139 CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
2140
2141VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr);
2142
2143rb_execution_context_t *rb_vm_main_ractor_ec(rb_vm_t *vm); // ractor.c
2144
2145/* for thread */
2146
2147#if RUBY_VM_THREAD_MODEL == 2
2148
2149RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
2150RUBY_EXTERN rb_vm_t *ruby_current_vm_ptr;
2151RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
2152RUBY_EXTERN rb_event_flag_t ruby_vm_event_enabled_global_flags; // only ever added to
2153RUBY_EXTERN unsigned int ruby_vm_iseq_events_enabled;
2154RUBY_EXTERN unsigned int ruby_vm_c_events_enabled;
2155
2156#define GET_VM() rb_current_vm()
2157#define GET_RACTOR() rb_current_ractor()
2158#define GET_THREAD() rb_current_thread()
2159#define GET_EC() rb_current_execution_context(true)
2160
2161static inline rb_serial_t
2162rb_ec_serial(struct rb_execution_context_struct *ec)
2163{
2164 VM_ASSERT(ec->serial >= 1);
2165 return ec->serial;
2166}
2167
2168static inline rb_thread_t *
2169rb_ec_thread_ptr(const rb_execution_context_t *ec)
2170{
2171 return ec->thread_ptr;
2172}
2173
2174static inline rb_ractor_t *
2175rb_ec_ractor_ptr(const rb_execution_context_t *ec)
2176{
2177 const rb_thread_t *th = rb_ec_thread_ptr(ec);
2178 if (th) {
2179 VM_ASSERT(th->ractor != NULL);
2180 return th->ractor;
2181 }
2182 else {
2183 return NULL;
2184 }
2185}
2186
2187static inline rb_serial_t
2188rb_ec_ractor_id(const rb_execution_context_t *ec)
2189{
2190 rb_serial_t ractor_id = ec->ractor_id;
2191 RUBY_ASSERT(ractor_id);
2192 return ractor_id;
2193}
2194
2195static inline rb_vm_t *
2196rb_ec_vm_ptr(const rb_execution_context_t *ec)
2197{
2198 const rb_thread_t *th = rb_ec_thread_ptr(ec);
2199 if (th) {
2200 return th->vm;
2201 }
2202 else {
2203 return NULL;
2204 }
2205}
2206
2207NOINLINE(struct rb_execution_context_struct *rb_current_ec_noinline(void));
2208
2209static inline rb_execution_context_t *
2210rb_current_execution_context(bool expect_ec)
2211{
2212#ifdef RB_THREAD_LOCAL_SPECIFIER
2213 #ifdef RB_THREAD_CURRENT_EC_NOINLINE
2214 rb_execution_context_t * volatile ec = rb_current_ec();
2215 #else
2216 rb_execution_context_t * volatile ec = ruby_current_ec;
2217 #endif
2218
2219 /* On the shared objects, `__tls_get_addr()` is used to access the TLS
2220 * and the address of the `ruby_current_ec` can be stored on a function
2221 * frame. However, this address can be mis-used after native thread
2222 * migration of a coroutine.
2223 * 1) Get `ptr = &ruby_current_ec` on NT1 and store it on the frame.
2224 * 2) Context switch and resume it on the NT2.
2225 * 3) `ptr` is used on NT2 but it accesses the TLS of NT1.
2226 * This assertion checks such misusage.
2227 *
2228 * To avoid accidents, `GET_EC()` should be called once on the frame.
2229 * Note that inlining can produce the problem.
2230 */
2231 VM_ASSERT(ec == rb_current_ec_noinline());
2232#else
2233 rb_execution_context_t * volatile ec = native_tls_get(ruby_current_ec_key);
2234#endif
2235 VM_ASSERT(!expect_ec || ec != NULL);
2236 return ec;
2237}
2238
2239static inline rb_thread_t *
2240rb_current_thread(void)
2241{
2242 const rb_execution_context_t *ec = GET_EC();
2243 return rb_ec_thread_ptr(ec);
2244}
2245
2246static inline rb_ractor_t *
2247rb_current_ractor_raw(bool expect)
2248{
2249 if (ruby_single_main_ractor) {
2250 return ruby_single_main_ractor;
2251 }
2252 else {
2253 const rb_execution_context_t *ec = rb_current_execution_context(expect);
2254 return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
2255 }
2256}
2257
2258static inline rb_ractor_t *
2259rb_current_ractor(void)
2260{
2261 return rb_current_ractor_raw(true);
2262}
2263
2264static inline rb_vm_t *
2265rb_current_vm(void)
2266{
2267#if 0 // TODO: reconsider the assertions
2268 VM_ASSERT(ruby_current_vm_ptr == NULL ||
2269 ruby_current_execution_context_ptr == NULL ||
2270 rb_ec_thread_ptr(GET_EC()) == NULL ||
2271 rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
2272 rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
2273#endif
2274
2275 return ruby_current_vm_ptr;
2276}
2277
2278void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
2279 unsigned int recorded_lock_rec,
2280 unsigned int current_lock_rec);
2281
2282/* This technically is a data race, as it's checked without the lock, however we
2283 * check against a value only our own thread will write. */
2284NO_SANITIZE("thread", static inline bool
2285vm_locked_by_ractor_p(rb_vm_t *vm, rb_ractor_t *cr))
2286{
2287 VM_ASSERT(cr == GET_RACTOR());
2288 return vm->ractor.sync.lock_owner == cr;
2289}
2290
2291static inline unsigned int
2292rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
2293{
2294 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2295
2296 if (!vm_locked_by_ractor_p(vm, rb_ec_ractor_ptr(ec))) {
2297 return 0;
2298 }
2299 else {
2300 return vm->ractor.sync.lock_rec;
2301 }
2302}
2303
2304#else
2305#error "unsupported thread model"
2306#endif
2307
2308enum {
2309 TIMER_INTERRUPT_MASK = 0x01,
2310 PENDING_INTERRUPT_MASK = 0x02,
2311 POSTPONED_JOB_INTERRUPT_MASK = 0x04,
2312 TRAP_INTERRUPT_MASK = 0x08,
2313 TERMINATE_INTERRUPT_MASK = 0x10,
2314 VM_BARRIER_INTERRUPT_MASK = 0x20,
2315};
2316
2317#define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
2318#define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
2319#define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
2320#define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
2321#define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
2322#define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
2323
2324static inline bool
2325RUBY_VM_INTERRUPTED(rb_execution_context_t *ec)
2326{
2327 return (ATOMIC_LOAD_RELAXED(ec->interrupt_flag) & ~(ec->interrupt_mask) & (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK));
2328}
2329
2330static inline bool
2331RUBY_VM_INTERRUPTED_ANY(rb_execution_context_t *ec)
2332{
2333#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
2334 uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
2335
2336 if (current_clock != ec->checked_clock) {
2337 ec->checked_clock = current_clock;
2338 RUBY_VM_SET_TIMER_INTERRUPT(ec);
2339 }
2340#endif
2341 return ATOMIC_LOAD_RELAXED(ec->interrupt_flag) & ~(ec)->interrupt_mask;
2342}
2343
2344VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
2345int rb_signal_buff_size(void);
2346int rb_signal_exec(rb_thread_t *th, int sig);
2347void rb_threadptr_check_signal(rb_thread_t *mth);
2348void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
2349void rb_threadptr_interrupt_raise(rb_thread_t *th);
2350void rb_threadptr_signal_exit(rb_thread_t *th);
2351int rb_threadptr_execute_interrupts(rb_thread_t *, int);
2352void rb_threadptr_interrupt(rb_thread_t *th);
2353void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
2354void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
2355void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
2356VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec);
2357void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo);
2358void rb_execution_context_update(rb_execution_context_t *ec);
2359void rb_execution_context_mark(const rb_execution_context_t *ec);
2360void rb_fiber_close(rb_fiber_t *fib);
2361void Init_native_thread(rb_thread_t *th);
2362int rb_vm_check_ints_blocking(rb_execution_context_t *ec);
2363
2364#define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
2365static inline void
2366rb_vm_check_ints(rb_execution_context_t *ec)
2367{
2368#ifdef RUBY_ASSERT_CRITICAL_SECTION
2369 VM_ASSERT(ec->assert_critical_section_entered == 0);
2370#endif
2371
2372 VM_ASSERT(ec == rb_current_ec_noinline());
2373
2374 if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2375 rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2376 }
2377}
2378
2379/* tracer */
2380
2382 rb_event_flag_t event;
2384 const rb_control_frame_t *cfp;
2385 VALUE self;
2386 ID id;
2387 ID called_id;
2388 VALUE klass;
2389 VALUE data;
2390
2391 int klass_solved;
2392
2393 /* calc from cfp */
2394 int lineno;
2395 VALUE path;
2396};
2397
2398void rb_hook_list_mark(rb_hook_list_t *hooks);
2399void rb_hook_list_mark_and_move(rb_hook_list_t *hooks);
2400void rb_hook_list_free(rb_hook_list_t *hooks);
2401void rb_hook_list_connect_local_tracepoint(rb_hook_list_t *list, VALUE tpval, unsigned int target_line);
2402bool rb_hook_list_remove_local_tracepoint(rb_hook_list_t *list, VALUE tpval);
2403unsigned int rb_hook_list_count(rb_hook_list_t *list);
2404
2405void rb_exec_event_hooks(struct rb_trace_arg_struct *trace_arg, rb_hook_list_t *hooks, int pop_p);
2406
2407#define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2408 const rb_event_flag_t flag_arg_ = (flag_); \
2409 rb_hook_list_t *hooks_arg_ = (hooks_); \
2410 if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2411 /* defer evaluating the other arguments */ \
2412 rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2413 } \
2414} while (0)
2415
2416static inline void
2417rb_exec_event_hook_orig(rb_execution_context_t *ec, rb_hook_list_t *hooks, rb_event_flag_t flag,
2418 VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
2419{
2420 struct rb_trace_arg_struct trace_arg;
2421
2422 VM_ASSERT((hooks->events & flag) != 0);
2423
2424 trace_arg.event = flag;
2425 trace_arg.ec = ec;
2426 trace_arg.cfp = ec->cfp;
2427 trace_arg.self = self;
2428 trace_arg.id = id;
2429 trace_arg.called_id = called_id;
2430 trace_arg.klass = klass;
2431 trace_arg.data = data;
2432 trace_arg.path = Qundef;
2433 trace_arg.klass_solved = 0;
2434
2435 rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2436}
2437
2439 VALUE self;
2440 rb_serial_t id;
2441 rb_hook_list_t hooks;
2442 st_table targeted_hooks; // also called "local hooks". {ISEQ => hook_list, def => hook_list...}
2443 unsigned int targeted_hooks_cnt; // ex: tp.enabled(target: method(:puts))
2444};
2445
2446static inline rb_hook_list_t *
2447rb_ec_ractor_hooks(const rb_execution_context_t *ec)
2448{
2449 struct rb_ractor_pub *cr_pub = (struct rb_ractor_pub *)rb_ec_ractor_ptr(ec);
2450 return &cr_pub->hooks;
2451}
2452
2453static inline rb_hook_list_t *
2454rb_vm_global_hooks(const rb_execution_context_t *ec)
2455{
2456 return &rb_ec_vm_ptr(ec)->global_hooks;
2457}
2458
2459static inline rb_hook_list_t *
2460rb_ec_hooks(const rb_execution_context_t *ec, rb_event_flag_t event)
2461{
2462 // Should be a single bit set
2463 VM_ASSERT(event != 0 && ((event - 1) & event) == 0);
2464
2466 return rb_vm_global_hooks(ec);
2467 }
2468 else {
2469 return rb_ec_ractor_hooks(ec);
2470 }
2471}
2472
2473#define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2474 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_hooks(ec_, flag_), flag_, self_, id_, called_id_, klass_, data_, 0)
2475
2476#define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2477 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_hooks(ec_, flag_), flag_, self_, id_, called_id_, klass_, data_, 1)
2478
2479static inline void
2480rb_exec_event_hook_script_compiled(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE eval_script)
2481{
2482 EXEC_EVENT_HOOK(ec, RUBY_EVENT_SCRIPT_COMPILED, ec->cfp->self, 0, 0, 0,
2483 NIL_P(eval_script) ? (VALUE)iseq :
2484 rb_ary_new_from_args(2, eval_script, (VALUE)iseq));
2485}
2486
2487void rb_vm_trap_exit(rb_vm_t *vm);
2488void rb_vm_postponed_job_atfork(void); /* vm_trace.c */
2489size_t rb_vm_memsize_postponed_job_queue(void); /* vm_trace.c */
2490
2491RUBY_SYMBOL_EXPORT_BEGIN
2492
2493int rb_thread_check_trap_pending(void);
2494
2495/* #define RUBY_EVENT_RESERVED_FOR_INTERNAL_USE 0x030000 */ /* from vm_core.h */
2496#define RUBY_EVENT_COVERAGE_LINE 0x010000
2497#define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2498
2499void rb_postponed_job_flush(void);
2500void rb_postponed_job_trigger_for_ractor(unsigned int h, VALUE running_ractor);
2501
2502// ractor.c
2503RUBY_EXTERN VALUE rb_eRactorUnsafeError;
2504RUBY_EXTERN VALUE rb_eRactorIsolationError;
2505
2506RUBY_SYMBOL_EXPORT_END
2507
2508#endif /* RUBY_VM_CORE_H */
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_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
#define RUBY_INTERNAL_EVENT_OBJSPACE_MASK
Bitmask of GC events.
Definition event.h:100
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#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:1449
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition format.h:33
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:386
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.
Functions related to nodes in the AST.
#define RARRAY_AREF(a, i)
Definition rarray.h:402
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition rtypeddata.h:106
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:260
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:287
Definition vm_core.h:295
Definition vm_core.h:290
Definition iseq.h:338
Internal header for Ruby Box.
Definition box.h:14
Definition method.h:63
CREF (Class REFerence)
Definition method.h:45
Internal header for Class.
Definition class.h:31
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:242
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
Definition method.h:143
Definition st.h:79
IFUNC (Internal FUNCtion)
Definition imemo.h:87
Definition vm_core.h:252
Definition vm_core.h:299
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition value.h:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376