Ruby 4.1.0dev (2026-09-29 revision 5fe7d61b2050ab8e130316fee458320cba1c0cfe)
vm_core.h (5fe7d61b2050ab8e130316fee458320cba1c0cfe)
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(NSIG) /* 99% of everything else */
150# /* take it */
151#else /* Last resort */
152# define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
153#endif
154
155#define RUBY_NSIG NSIG
156
157#if defined(SIGCLD)
158# define RUBY_SIGCHLD (SIGCLD)
159#elif defined(SIGCHLD)
160# define RUBY_SIGCHLD (SIGCHLD)
161#endif
162
163#if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
164# define USE_SIGALTSTACK
165void *rb_allocate_sigaltstack(void);
166void *rb_register_sigaltstack(void *);
167# define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
168# define RB_ALTSTACK_FREE(var) free(var)
169# define RB_ALTSTACK(var) var
170#else /* noop */
171# define RB_ALTSTACK_INIT(var, altstack)
172# define RB_ALTSTACK_FREE(var)
173# define RB_ALTSTACK(var) (0)
174#endif
175
176#include THREAD_IMPL_H
177#define RUBY_VM_THREAD_MODEL 2
178
179/*****************/
180/* configuration */
181/*****************/
182
183/* gcc ver. check */
184#if defined(__GNUC__) && __GNUC__ >= 2
185
186#if OPT_TOKEN_THREADED_CODE
187#if OPT_DIRECT_THREADED_CODE
188#undef OPT_DIRECT_THREADED_CODE
189#endif
190#endif
191
192#else /* defined(__GNUC__) && __GNUC__ >= 2 */
193
194/* disable threaded code options */
195#if OPT_DIRECT_THREADED_CODE
196#undef OPT_DIRECT_THREADED_CODE
197#endif
198#if OPT_TOKEN_THREADED_CODE
199#undef OPT_TOKEN_THREADED_CODE
200#endif
201#endif
202
203/* call threaded code */
204#if OPT_CALL_THREADED_CODE
205#if OPT_DIRECT_THREADED_CODE
206#undef OPT_DIRECT_THREADED_CODE
207#endif /* OPT_DIRECT_THREADED_CODE */
208#endif /* OPT_CALL_THREADED_CODE */
209
210void rb_vm_encoded_insn_data_table_init(void);
211typedef unsigned long rb_num_t;
212typedef signed long rb_snum_t;
213
214enum ruby_tag_type {
215 RUBY_TAG_NONE = 0x0,
216 RUBY_TAG_RETURN = 0x1,
217 RUBY_TAG_BREAK = 0x2,
218 RUBY_TAG_NEXT = 0x3,
219 RUBY_TAG_RETRY = 0x4,
220 RUBY_TAG_REDO = 0x5,
221 RUBY_TAG_RAISE = 0x6,
222 RUBY_TAG_THROW = 0x7,
223 RUBY_TAG_FATAL = 0x8,
224 RUBY_TAG_MASK = 0xf
225};
226
227#define TAG_NONE RUBY_TAG_NONE
228#define TAG_RETURN RUBY_TAG_RETURN
229#define TAG_BREAK RUBY_TAG_BREAK
230#define TAG_NEXT RUBY_TAG_NEXT
231#define TAG_RETRY RUBY_TAG_RETRY
232#define TAG_REDO RUBY_TAG_REDO
233#define TAG_RAISE RUBY_TAG_RAISE
234#define TAG_THROW RUBY_TAG_THROW
235#define TAG_FATAL RUBY_TAG_FATAL
236#define TAG_MASK RUBY_TAG_MASK
237
238enum ruby_vm_throw_flags {
239 VM_THROW_NO_ESCAPE_FLAG = 0x8000,
240 VM_THROW_STATE_MASK = 0xff
241};
242
243/* forward declarations */
244struct rb_thread_struct;
246
247/* iseq data type */
249
251 rb_serial_t raw;
252 VALUE data[2];
253};
254
255#define IMEMO_CONST_CACHE_SHAREABLE IMEMO_FL_USER0
256
257// imemo_constcache
259 VALUE flags;
260
261 VALUE value;
262 const rb_cref_t *ic_cref;
263 /* Ractor that filled this entry. An unshareable value may be handed out again
264 * only to that Ractor: it is the one that passed the owner check. */
265 rb_serial_t ractor_id;
266};
267STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
268 (offsetof(struct iseq_inline_constant_cache_entry, ractor_id) +
269 sizeof(rb_serial_t)) <= RVALUE_SIZE);
270
287
289 uint64_t value; // Either rb_setivar_cache or rb_getivar_cache packed in a uint64_t.
290 ID iv_set_name;
291};
292
296
298 struct {
299 struct rb_thread_struct *running_thread;
300 VALUE value;
301 } once;
302 struct iseq_inline_constant_cache ic_cache;
303 struct iseq_inline_iv_cache_entry iv_cache;
304};
305
307 const struct rb_call_data *cd;
308 const struct rb_callcache *cc;
309 VALUE block_handler;
310 VALUE recv;
311 int argc;
312 bool kw_splat;
313 VALUE heap_argv;
314};
315
316#ifndef VM_ARGC_STACK_MAX
317#define VM_ARGC_STACK_MAX 128
318#endif
319
320#define VM_KW_SPECIFIED_BITS_MAX (32-1) /* TODO: 32 -> Fixnum's max bits */
321
322# define CALLING_ARGC(calling) ((calling)->heap_argv ? RARRAY_LENINT((calling)->heap_argv) : (calling)->argc)
323
325
326#ifndef RUBY_CORE_DATA_TYPE_CHECK
327# if RUBY_DEBUG
328# define RUBY_CORE_DATA_TYPE_CHECK 1
329# else
330# define RUBY_CORE_DATA_TYPE_CHECK 0
331# endif
332#endif
333#if !RUBY_CORE_DATA_TYPE_CHECK
334#define GetCoreDataFromValue(obj, type, data_type, ptr) ((ptr) = (type*)RTYPEDDATA_GET_DATA(obj))
335#else
336#define GetCoreDataFromValue(obj, type, data_type, ptr) TypedData_Get_Struct(obj, type, data_type, ptr)
337#endif
338
340 VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
341 VALUE label; /* String */
342 int first_lineno;
343 int node_id;
344 rb_code_location_t code_location;
346
347#define PATHOBJ_PATH 0
348#define PATHOBJ_REALPATH 1
349
350static inline VALUE
351pathobj_path(VALUE pathobj)
352{
353 if (RB_TYPE_P(pathobj, T_STRING)) {
354 return pathobj;
355 }
356 else {
357 VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
358 return RARRAY_AREF(pathobj, PATHOBJ_PATH);
359 }
360}
361
362static inline VALUE
363pathobj_realpath(VALUE pathobj)
364{
365 if (RB_TYPE_P(pathobj, T_STRING)) {
366 return pathobj;
367 }
368 else {
369 VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
370 return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
371 }
372}
373
374/* Forward declarations */
375typedef uintptr_t iseq_bits_t;
376
377#define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
378
379/* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
380#define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
381
382/* instruction sequence type */
383enum rb_iseq_type {
384 ISEQ_TYPE_TOP,
385 ISEQ_TYPE_METHOD,
386 ISEQ_TYPE_BLOCK,
387 ISEQ_TYPE_CLASS,
388 ISEQ_TYPE_RESCUE,
389 ISEQ_TYPE_ENSURE,
390 ISEQ_TYPE_EVAL,
391 ISEQ_TYPE_MAIN,
392 ISEQ_TYPE_PLAIN
393};
394
395// Attributes specified by Primitive.attr!
396enum rb_builtin_attr {
397 // The iseq does not call methods.
398 BUILTIN_ATTR_LEAF = 0x01,
399 // This iseq only contains single `opt_invokebuiltin_delegate_leave` instruction with 0 arguments.
400 BUILTIN_ATTR_SINGLE_NOARG_LEAF = 0x02,
401 // This attribute signals JIT to duplicate the iseq for each block iseq so that its `yield` will be monomorphic.
402 BUILTIN_ATTR_INLINE_BLOCK = 0x04,
403 // The iseq acts like a C method in backtraces.
404 BUILTIN_ATTR_C_TRACE = 0x08,
405 // The iseq uses noint branch/jump opcodes that skip interrupt checking.
406 BUILTIN_ATTR_WITHOUT_INTERRUPTS = 0x10,
407 // The iseq operates on the nearest user box in its caller frames.
408 BUILTIN_ATTR_CALLER_USER_BOX = 0x20,
409};
410
411typedef VALUE (*rb_jit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *);
412typedef VALUE (*rb_zjit_func_t)(struct rb_execution_context_struct *, struct rb_control_frame_struct *, rb_jit_func_t);
413
414enum lvar_state {
415 lvar_uninitialized,
416 lvar_initialized,
417 lvar_reassigned,
418};
419
420/* Lazily-allocated per-iseq variable data. NULL when unused (the common case:
421 * no coverage, no script_lines, no flip-flops, no disassembly). */
423 rb_snum_t flip_count;
424 VALUE script_lines;
425 VALUE coverage;
426 VALUE pc2branchindex;
427 VALUE *original_iseq;
428};
429
431 enum rb_iseq_type type;
432
433 unsigned int iseq_size;
434 VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
435
460 struct {
461 unsigned int has_lead : 1;
462 unsigned int has_opt : 1;
463 unsigned int has_rest : 1;
464 unsigned int has_post : 1;
465 unsigned int has_kw : 1;
466 unsigned int has_kwrest : 1;
467 unsigned int has_block : 1;
468
469 unsigned int ambiguous_param0 : 1; /* {|a|} */
470 unsigned int accepts_no_kwarg : 1;
471 unsigned int ruby2_keywords: 1;
472 unsigned int anon_rest: 1;
473 unsigned int anon_kwrest: 1;
474 unsigned int use_block: 1;
475 unsigned int forwardable: 1;
476 unsigned int accepts_no_block: 1;
477 } flags;
478
479 unsigned int size;
480
481 int lead_num;
482 int opt_num;
483 int rest_start;
484 int post_start;
485 int post_num;
486 int block_start;
487
488 const VALUE *opt_table; /* (opt_num + 1) entries. */
489 /* opt_num and opt_table:
490 *
491 * def foo o1=e1, o2=e2, ..., oN=eN
492 * #=>
493 * # prologue code
494 * A1: e1
495 * A2: e2
496 * ...
497 * AN: eN
498 * AL: body
499 * opt_num = N
500 * opt_table = [A1, A2, ..., AN, AL]
501 */
502
504 int num;
505 int required_num;
506 int bits_start;
507 int rest_start;
508 const ID *table;
509 VALUE *default_values;
510 } *keyword;
511 } param;
512
513 rb_iseq_location_t location;
514
515 /* insn info, must be freed */
517 const struct iseq_insn_info_entry *body;
518 union {
519 unsigned int *positions;
520#if VM_INSN_INFO_TABLE_IMPL == 2
521 struct succ_index_table *succ_index_table;
522#endif
523 } positions_or_succ_index_table;
524 unsigned int size;
525 } insns_info;
526
527 const ID *local_table; /* must free */
528
529 union {
530 uint8_t *list;
531 uint8_t single[sizeof(uint8_t *)];
532 } lvar_states;
533
534 /* catch table */
535 struct iseq_catch_table *catch_table;
536
537 /* for child iseq */
538 const struct rb_iseq_struct *parent_iseq;
539 struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
540
541 union iseq_inline_storage_entry *is_entries; /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
542 struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
543
544 struct rb_iseq_variable *variable;
545
546 unsigned int local_table_size;
547 unsigned int ic_size; // Number of IC caches
548 unsigned int ise_size; // Number of ISE caches
549 unsigned int ivc_size; // Number of IVC caches
550 unsigned int icvarc_size; // Number of ICVARC caches
551 unsigned int ci_size;
552 unsigned int stack_max; /* for stack overflow check */
553
554 unsigned int builtin_attrs; // Union of rb_builtin_attr
555
556 bool prism; // ISEQ was generated from prism compiler
557
558 // Set once an EP escape of this iseq has been reported to the enabled JIT.
559 rb_atomic_t jit_ep_escape_recorded;
560
561 union {
562 iseq_bits_t * list; /* Find references for GC */
563 iseq_bits_t single;
564 } mark_bits;
565
566 struct rb_id_table *outer_variables;
567
568 const rb_iseq_t *mandatory_only_iseq;
569
570#if USE_YJIT || USE_ZJIT
571 // Number of calls on jit_exec()
572 unsigned int jit_entry_calls;
573 // Number of calls on jit_exec_exception()
574 unsigned int jit_exception_calls;
575 // Function pointer for JIT code on jit_exec()
576 rb_jit_func_t jit_entry;
577 // Function pointer for JIT code on jit_exec_exception()
578 rb_jit_func_t jit_exception;
579 void *jit_payload;
580#endif
581
582#if USE_YJIT
583 // Used to estimate how frequently this ISEQ gets called
584 unsigned int yjit_calls_at_interv;
585#endif
586
587 // Hash of the source this iseq was compiled from, or 0 if it is
588 // unavailable. A computed hash of 0 is remapped to another value, so
589 // 0 never denotes a real hash.
590 uint64_t source_hash;
591};
592
593/* T_IMEMO/iseq */
594/* typedef rb_iseq_t is in method.h */
596 VALUE flags; /* 1 */
597
598 struct rb_iseq_constant_body *body; /* 2 */
599
600 union { /* 3, 4 words */
601 struct iseq_compile_data *compile_data; /* used at compile time */
602
603 struct {
604 VALUE obj;
605 int index;
606 } loader;
607
608 struct {
609 unsigned int local_hooks_cnt;
610 rb_event_flag_t global_trace_events;
611 } exec;
612 } aux;
613};
614
615#define ISEQ_BODY(iseq) ((iseq)->body)
616
617#if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
618#define USE_LAZY_LOAD 0
619#endif
620
621#if !USE_LAZY_LOAD
622static inline const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq) {return 0;}
623#endif
624const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
625
626static inline const rb_iseq_t *
627rb_iseq_check(const rb_iseq_t *iseq)
628{
629 if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
630 rb_iseq_complete((rb_iseq_t *)iseq);
631 }
632 return iseq;
633}
634
635static inline bool
636rb_iseq_attr_p(const rb_iseq_t *iseq, enum rb_builtin_attr attr)
637{
638 return (ISEQ_BODY(iseq)->builtin_attrs & attr) == attr;
639}
640
641static inline const rb_iseq_t *
642def_iseq_ptr(rb_method_definition_t *def)
643{
644//TODO: re-visit. to check the bug, enable this assertion.
645#if VM_CHECK_MODE > 0
646 if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
647#endif
648 return rb_iseq_check(def->body.iseq.iseqptr);
649}
650
651enum ruby_special_exceptions {
652 ruby_error_reenter,
653 ruby_error_nomemory,
654 ruby_error_sysstack,
655 ruby_error_stackfatal,
656 ruby_error_stream_closed,
657 ruby_special_error_count
658};
659
660extern const rb_data_type_t ruby_vm_data_type;
661
662#define GetVMPtr(obj, ptr) \
663 GetCoreDataFromValue((obj), rb_vm_t, &ruby_vm_data_type, (ptr))
664
665struct rb_vm_struct;
666typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
667
668typedef struct rb_at_exit_list {
669 rb_vm_at_exit_func *func;
670 struct rb_at_exit_list *next;
672
673void rb_gc_init_objspaces(void);
674void rb_objspace_free(void *objspace);
675void rb_objspace_call_finalizer(void);
676
677enum rb_hook_list_type {
678 hook_list_type_ractor_local,
679 hook_list_type_targeted_iseq,
680 hook_list_type_targeted_def, // C function
681 hook_list_type_global
682};
683
684typedef struct rb_hook_list_struct {
685 struct rb_event_hook_struct *hooks;
686 rb_event_flag_t events;
687 unsigned int running;
688 enum rb_hook_list_type type;
689 bool need_clean;
691
692// see builtin.h for definition
693typedef const struct rb_builtin_function *RB_BUILTIN;
694
695/* The mark redirect used by the object-traversal APIs
696 * (rb_objspace_reachable_objects_from etc.). It is installed while a traversal
697 * runs and is NULL during a real GC. Storage is per-Ractor
698 * (rb_ractor_t.mark_func_data); on a modular GC, threads without a current
699 * Ractor fall back to rb_vm_struct's gc sub-struct (see gc.c). */
701 void *data;
702 void (*mark_func)(VALUE v, void *data);
703 /* Marker set while a shareable-verification walk runs (read by
704 * rb_gc_checking_shareable). The slot is per-Ractor, so it only affects the
705 * walk of the Ractor doing the verification. */
706 bool checking_shareable;
707};
708
709typedef struct rb_vm_struct {
710 VALUE self;
711
712 struct {
713 struct ccan_list_head set;
714 /* For a single-objspace impl (mmtk): Ractors between termination and
715 * ractor_free. The global root scan keeps marking their
716 * registered_marks. */
717 struct ccan_list_head terminated_set;
718 unsigned int cnt;
719 unsigned int blocking_cnt;
720
721 struct rb_ractor_struct *main_ractor;
722 struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
723
724 struct {
725 // monitor
726 rb_nativethread_lock_t lock;
727 struct rb_ractor_struct *lock_owner;
728 unsigned int lock_rec;
729
730 // join at exit
731 rb_nativethread_cond_t terminate_cond;
732 bool terminate_waiting;
733 } sync;
734
735 /* VM-wide locks for the Ractor transfer/inheritance machinery. All of them
736 * are leaf locks: no safepoint inside a critical section. */
737 rb_nativethread_lock_t generic_fields_lock; /* the shared generic-fields table in variable.c */
738
739 // ractor scheduling; see thread_sched.h
740 struct rb_ractor_sched sched;
741 } ractor;
742
743#ifdef USE_SIGALTSTACK
744 void *main_altstack;
745#endif
746
747 rb_serial_t fork_gen;
748
749 /* set in single-threaded processes only: */
750 volatile int ubf_async_safe;
751
752 unsigned int running: 1;
753 unsigned int thread_abort_on_exception: 1;
754 unsigned int thread_report_on_exception: 1;
755 unsigned int thread_ignore_deadlock: 1;
756
757 /* object management */
758 const VALUE special_exceptions[ruby_special_error_count];
759
760 /* Ruby Box */
761 rb_box_t *master_box;
762 rb_box_t *root_box;
763 rb_box_t *main_box;
764
765 /* load */
766 // For running the init function of statically linked
767 // extensions when they are loaded
768 struct st_table static_ext_inits;
769
770 /* signal */
771 struct {
772 VALUE cmd[RUBY_NSIG];
773 } trap_list;
774
775 /* hook (for internal events: NEWOBJ, FREEOBJ, GC events, etc.) */
776 rb_hook_list_t global_hooks;
777
778 int src_encoding_index;
779
780
781 /* `once` completion event (see vm_once_dispatch) */
782 rb_nativethread_lock_t once_lock;
783 rb_nativethread_cond_t once_cond;
784
785 VALUE orig_progname, progname;
786 VALUE coverages, cme2counter, me_set;
787 int coverage_mode;
788
789 struct {
790 /* The VM only points at rb_global_objspace, the process-wide GC data such as
791 * the page pool. Each Ractor owns its own rb_objspace through r->objspace,
792 * and the boot objspace belongs to the main Ractor. */
793 struct rb_global_objspace *global_objspace;
794 /* Objspaces of terminated, not-yet-inherited Ractors. No mutator runs in
795 * them; a global GC sweeps them under the barrier (missing one leaves stale
796 * mark bits = UAF), inheritance merges them under the VM lock. owner_slot is
797 * the dead Ractor's r->objspace, cleared when inherited. */
798 struct rb_objspace_zombie {
799 void *objspace;
800 void **owner_slot;
801 /* The terminated Ractor owning this zombie; a root scan reaches its
802 * rb_gc_register_mark_object pins and join value through it. NULL for an
803 * orphan, whose Ractor struct is gone and has neither any more. */
804 struct rb_ractor_struct *owner;
805 /* Heap pages this zombie holds: measured when it retires and refreshed
806 * under the barrier of each global cycle. The total below stays exactly
807 * in sync, entry by entry. */
808 size_t pages;
809 } *zombie_objspaces;
810 size_t zombie_objspaces_count;
811 size_t zombie_objspaces_capa;
812 /* Sum of .pages over zombie_objspaces. Between global cycles it is an upper
813 * bound: a zombie's heap never grows and only shrinks at a global cycle. */
814 size_t zombie_total_pages;
815
816#if USE_MODULAR_GC
817 struct gc_mark_func_data_struct *mark_func_data;
818#endif
819 struct {
820 rb_nativethread_lock_t lock;
821 struct rb_ractor_struct **registry;
822 size_t registry_cnt, registry_capa;
823 } registered_addrs;
824
825 /* Holders keeping GC disabled (atomic): Ractors that called GC.disable (at
826 * most one hold each) plus short internal critical sections. One holder stops
827 * GC everywhere; GC.enable releases only the caller's own hold, never
828 * overriding another Ractor's disable. */
829 rb_atomic_t disable_holders;
830 /* Handle of the postponed job that merges an orphan objspace into the main
831 * one (rb_postponed_job_handle_t; POSTPONED_JOB_HANDLE_INVALID when not
832 * registered). */
833 unsigned int orphan_merge_pjob;
834 /* Used to resolve the objspace during VM teardown (the cleanup path of
835 * rb_gc_get_objspace). */
836 void *cleanup_objspace;
837 } gc;
838
839 rb_at_exit_list *at_exit;
840
841 const struct rb_builtin_function *builtin_function_table;
842
843 st_table ci_table;
844 struct rb_id_table negative_cme_table;
845 st_table overloaded_cme_table; // cme -> overloaded_cme
846 set_table unused_block_warning_table;
847 VALUE cc_refinement_set;
848
849 // This id table contains a mapping from ID to ICs. It does this with ID
850 // keys and nested st_tables as values. The nested tables have ICs as keys
851 // and Qtrue as values. It is used when inline constant caches need to be
852 // invalidated or ISEQs are being freed.
853 struct rb_id_table constant_cache;
854 ID inserting_constant_cache_id;
855
856#ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
857#define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
858#endif
859 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
860 bool global_cc_cache_table_used; // vm_eval.c
861
862#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
863 uint32_t clock;
864#endif
865
866 /* params */
867 struct { /* size in byte */
868 size_t thread_vm_stack_size;
869 size_t thread_machine_stack_size;
870 size_t fiber_vm_stack_size;
871 size_t fiber_machine_stack_size;
872 } default_params;
873} rb_vm_t;
874
875extern bool ruby_vm_during_cleanup;
876
877/* default values */
878
879#define RUBY_VM_SIZE_ALIGN 4096
880
881#define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
882#define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
883#define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
884#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
885
886#define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
887#define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
888#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
889#if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
890#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
891#else
892#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
893#endif
894
895#if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer) || __has_feature(leak_sanitizer)
896/* It seems sanitizers consume A LOT of machine stacks */
897#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
898#define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
899#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
900#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
901#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
902#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
903#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
904#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
905#endif
906
907#ifndef VM_DEBUG_BP_CHECK
908#define VM_DEBUG_BP_CHECK 0
909#endif
910
911#ifndef VM_DEBUG_VERIFY_METHOD_CACHE
912#define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
913#endif
914
916 VALUE self;
917 const VALUE *ep;
918 union {
919 const rb_iseq_t *iseq;
920 const struct vm_ifunc *ifunc;
921 VALUE val;
922 } code;
923};
924
925enum rb_block_handler_type {
926 block_handler_type_iseq,
927 block_handler_type_ifunc,
928 block_handler_type_symbol,
929 block_handler_type_proc
930};
931
932enum rb_block_type {
933 block_type_iseq,
934 block_type_ifunc,
935 block_type_symbol,
936 block_type_proc
937};
938
939struct rb_block {
940 enum rb_block_type type : 8;
941 union {
942 struct rb_captured_block captured;
943 VALUE symbol;
944 VALUE proc;
945 } as;
946};
947
949 const VALUE *pc; // cfp[0]
950 VALUE *sp; // cfp[1]
951 const rb_iseq_t *_iseq; // cfp[2] -- use CFP_ISEQ(cfp) to read
952 VALUE self; // cfp[3] / block[0]
953 const VALUE *ep; // cfp[4] / block[1]
954 const void *block_code; // cfp[5] / block[2] -- iseq, ifunc, or forwarded block handler
955 void *jit_return; // cfp[6] -- return address for JIT code
956#if VM_DEBUG_BP_CHECK
957 VALUE *bp_check; // cfp[7]
958#endif
960
961extern const rb_data_type_t ruby_threadptr_data_type;
962
963static inline struct rb_thread_struct *
964rb_thread_ptr(VALUE thval)
965{
966 return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
967}
968
969enum rb_thread_status {
970 THREAD_RUNNABLE,
971 THREAD_STOPPED,
972 THREAD_STOPPED_FOREVER,
973 THREAD_KILLED
974};
975
976#ifdef RUBY_JMP_BUF
977typedef RUBY_JMP_BUF rb_jmpbuf_t;
978#else
979typedef void *rb_jmpbuf_t[5];
980#endif
981
982/*
983 `rb_vm_tag_jmpbuf_t` type represents a buffer used to
984 long jump to a C frame associated with `rb_vm_tag`.
985
986 Use-site of `rb_vm_tag_jmpbuf_t` is responsible for calling the
987 following functions:
988 - `rb_vm_tag_jmpbuf_init` once `rb_vm_tag_jmpbuf_t` is allocated.
989 - `rb_vm_tag_jmpbuf_deinit` once `rb_vm_tag_jmpbuf_t` is no longer necessary.
990
991 `RB_VM_TAG_JMPBUF_GET` transforms a `rb_vm_tag_jmpbuf_t` into a
992 `rb_jmpbuf_t` to be passed to `rb_setjmp/rb_longjmp`.
993*/
994#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
995/*
996 WebAssembly target with Asyncify-based SJLJ needs
997 to capture the execution context by unwind/rewind-ing
998 call frames into a jump buffer. The buffer space tends
999 to be considerably large unlike other architectures'
1000 register-based buffers.
1001 Therefore, we allocates the buffer on the heap on such
1002 environments.
1003*/
1004typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
1005
1006#define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
1007
1008static inline void
1009rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
1010{
1011 *jmpbuf = ruby_xmalloc(sizeof(rb_jmpbuf_t));
1012}
1013
1014static inline void
1015rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
1016{
1017 ruby_xfree(*jmpbuf);
1018}
1019#else
1020typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
1021
1022#define RB_VM_TAG_JMPBUF_GET(buf) (buf)
1023
1024static inline void
1025rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
1026{
1027 // no-op
1028}
1029
1030static inline void
1031rb_vm_tag_jmpbuf_deinit(const rb_vm_tag_jmpbuf_t *jmpbuf)
1032{
1033 // no-op
1034}
1035#endif
1036
1037/*
1038 the members which are written in EC_PUSH_TAG() should be placed at
1039 the beginning and the end, so that entire region is accessible.
1040*/
1042 VALUE tag;
1043 VALUE retval;
1044 rb_vm_tag_jmpbuf_t buf;
1045 struct rb_vm_tag *prev;
1046 enum ruby_tag_type state;
1047 unsigned int lock_rec;
1048#if USE_ZJIT
1049 // ec->cfp as of EC_PUSH_TAG, which is saved for materializing JITFrame.
1050 rb_control_frame_t *cfp;
1051 // Whether cfp had a ZJIT frame before this tag's setjmp was established.
1052 // It's used for checking if zjit_materialize_frames should materialize
1053 // the frame or not when the tag is popped. If zjit_frame_active is true,
1054 // we don't want to materialize cfp->jit_return, which will still be used
1055 // by JIT code.
1056 bool zjit_frame_active;
1057#endif
1058};
1059
1060STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
1061STATIC_ASSERT(rb_vm_tag_buf_end,
1062 offsetof(struct rb_vm_tag, buf) + sizeof(rb_vm_tag_jmpbuf_t) <
1063 sizeof(struct rb_vm_tag));
1064
1067 void *arg;
1068 rb_atomic_t event_serial;
1069};
1070
1071struct rb_mutex_struct;
1072
1073typedef struct rb_fiber_struct rb_fiber_t;
1074
1076 struct rb_waiting_list *next;
1077 struct rb_thread_struct *thread;
1078 struct rb_fiber_struct *fiber;
1079};
1080
1081
1083 /* execution information */
1084 VALUE *vm_stack; /* must free, must mark */
1085 size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
1086 rb_control_frame_t *cfp;
1087
1088 struct rb_vm_tag *tag;
1089
1090 /* interrupt flags */
1091 rb_atomic_t interrupt_flag;
1092 rb_atomic_t interrupt_mask; /* size should match flag */
1093#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1094 uint32_t checked_clock;
1095#endif
1096
1097 rb_fiber_t *fiber_ptr;
1098 struct rb_thread_struct *thread_ptr;
1099 rb_serial_t serial;
1100 rb_serial_t ractor_id;
1101
1102 /* storage (ec (fiber) local) */
1103 struct rb_id_table *local_storage;
1104 VALUE local_storage_recursive_hash;
1105 VALUE local_storage_recursive_hash_for_trace;
1106
1107 /* Inheritable fiber storage. */
1108 VALUE storage;
1109
1110 /* eval env */
1111 const VALUE *root_lep;
1112 VALUE root_svar;
1113
1114 /* trace information */
1115 struct rb_trace_arg_struct *trace_arg;
1116
1117 /* temporary places */
1118 VALUE errinfo;
1119 VALUE passed_block_handler; /* for rb_iterate */
1120
1121 uint8_t raised_flag; /* only 3 bits needed */
1122
1123 /* n.b. only 7 bits needed, really: */
1124 BITFIELD(enum method_missing_reason, method_missing_reason, 8);
1125
1126 VALUE private_const_reference;
1127
1128 struct {
1129 VALUE obj;
1130 VALUE fields_obj;
1131 } gen_fields_cache;
1132
1133 /* for GC */
1134 struct {
1135 VALUE *stack_start;
1136 VALUE *stack_end;
1137 size_t stack_maxsize;
1139
1140#ifdef RUBY_ASAN_ENABLED
1141 void *asan_fake_stack_handle;
1142#endif
1143 } machine;
1144
1145#ifdef RUBY_ASSERT_CRITICAL_SECTION
1146 int assert_critical_section_entered;
1147#endif
1148};
1149
1150#ifndef rb_execution_context_t
1152#define rb_execution_context_t rb_execution_context_t
1153#endif
1154
1155// for builtin.h
1156#define VM_CORE_H_EC_DEFINED 1
1157
1158// Set the vm_stack pointer in the execution context.
1159void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1160
1161// Initialize the vm_stack pointer in the execution context and push the initial stack frame.
1162// @param ec the execution context to update.
1163// @param stack a pointer to the stack to use.
1164// @param size the size of the stack, as in `VALUE stack[size]`.
1165void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
1166
1167// Clear (set to `NULL`) the vm_stack pointer.
1168// @param ec the execution context to update.
1169void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
1170
1171// Close an execution context and free related resources that are no longer needed.
1172// @param ec the execution context to close.
1173void rb_ec_close(rb_execution_context_t *ec);
1174
1176 bool ractor_safe;
1177};
1178
1179typedef struct rb_ractor_struct rb_ractor_t;
1180
1181struct rb_native_thread;
1182
1183typedef struct rb_thread_struct {
1184 struct ccan_list_node lt_node; // managed by a ractor (r->threads.set)
1185 VALUE self;
1186 rb_ractor_t *ractor;
1187 rb_vm_t *vm;
1188 struct rb_native_thread *nt;
1190
1191 struct rb_thread_sched_item sched;
1192 bool mn_schedulable;
1193 rb_atomic_t serial; // only for RUBY_DEBUG_LOG()
1194
1195 VALUE last_status; /* $? */
1196
1197 /* for cfunc */
1198 struct rb_calling_info *calling;
1199
1200 /* for load(true) */
1201 VALUE top_self;
1202 VALUE top_wrapper;
1203
1204 /* thread control */
1205
1206 BITFIELD(enum rb_thread_status, status, 2);
1207 /* bit flags */
1208 unsigned int main_thread : 1;
1209 unsigned int has_dedicated_nt : 1;
1210 unsigned int to_kill : 1;
1211 unsigned int abort_on_exception: 1;
1212 unsigned int report_on_exception: 1;
1213 unsigned int pending_interrupt_queue_checked: 1;
1214 int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1215 uint32_t running_time_us; /* 12500..800000 */
1216
1217 void *blocking_region_buffer;
1218
1219 VALUE thgroup;
1220 VALUE value;
1221
1222 /* temporary place of retval on OPT_CALL_THREADED_CODE */
1223#if OPT_CALL_THREADED_CODE
1224 VALUE retval;
1225#endif
1226
1227 /* async errinfo queue */
1228 VALUE pending_interrupt_queue;
1229 VALUE pending_interrupt_mask_stack;
1230
1231 /* interrupt management */
1232 rb_nativethread_lock_t interrupt_lock;
1233 struct rb_unblock_callback unblock;
1234 VALUE locking_mutex;
1235 struct rb_mutex_struct *keeping_mutexes;
1236 struct ccan_list_head interrupt_exec_tasks;
1237
1238 struct rb_waiting_list *join_list;
1239
1240 union {
1241 struct {
1242 VALUE proc;
1243 VALUE args;
1244 int kw_splat;
1245 } proc;
1246 struct {
1247 VALUE (*func)(void *);
1248 void *arg;
1249 } func;
1250 } invoke_arg;
1251
1252 enum thread_invoke_type {
1253 thread_invoke_type_none = 0,
1254 thread_invoke_type_proc,
1255 thread_invoke_type_ractor_proc,
1256 thread_invoke_type_func
1257 } invoke_type;
1258
1259 /* fiber */
1260 rb_fiber_t *root_fiber;
1261
1262 VALUE scheduler;
1263 unsigned int blocking;
1264
1265 /* misc */
1266 VALUE name;
1267 void **specific_storage;
1268
1269 struct rb_ext_config ext_config;
1270} rb_thread_t;
1271
1272static inline unsigned int
1273rb_th_serial(const rb_thread_t *th)
1274{
1275 return th ? (unsigned int)th->serial : 0;
1276}
1277
1278typedef enum {
1279 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1280 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1281 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1282 /* 0x03..0x06 is reserved */
1283 VM_DEFINECLASS_TYPE_MASK = 0x07
1284} rb_vm_defineclass_type_t;
1285
1286#define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1287#define VM_DEFINECLASS_FLAG_SCOPED 0x08
1288#define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1289#define VM_DEFINECLASS_FLAG_DYNAMIC_CREF 0x20
1290#define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1291#define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1292 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1293#define VM_DEFINECLASS_DYNAMIC_CREF_P(x) \
1294 ((x) & VM_DEFINECLASS_FLAG_DYNAMIC_CREF)
1295
1296/* iseq.c */
1297RUBY_SYMBOL_EXPORT_BEGIN
1298
1299/* node -> iseq */
1300rb_iseq_t *rb_iseq_new (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type);
1301rb_iseq_t *rb_iseq_new_top (const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1302rb_iseq_t *rb_iseq_new_main (const VALUE ast_value, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1303rb_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);
1304rb_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,
1305 enum rb_iseq_type, const rb_compile_option_t*,
1306 VALUE script_lines);
1307
1308struct iseq_link_anchor;
1310 VALUE flags;
1311 VALUE reserved;
1312 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1313 const void *data;
1314};
1315static inline struct rb_iseq_new_with_callback_callback_func *
1316rb_iseq_new_with_callback_new_callback(
1317 void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1318{
1320 IMEMO_NEW(struct rb_iseq_new_with_callback_callback_func, imemo_ifunc, Qfalse);
1321 memo->func = func;
1322 memo->data = ptr;
1323
1324 return memo;
1325}
1326rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1327 VALUE name, VALUE path, VALUE realpath, int first_lineno,
1328 const rb_iseq_t *parent, enum rb_iseq_type, const rb_compile_option_t*);
1329
1330VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1331int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1332
1333VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1334
1335RUBY_EXTERN VALUE rb_cISeq;
1336RUBY_EXTERN VALUE rb_cRubyVM;
1337RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1338RUBY_EXTERN VALUE rb_block_param_proxy;
1339RUBY_SYMBOL_EXPORT_END
1340
1341extern const rb_data_type_t ruby_proc_data_type;
1342
1343#define GetProcPtr(obj, ptr) \
1344 GetCoreDataFromValue((obj), rb_proc_t, &ruby_proc_data_type, (ptr))
1345
1346typedef struct {
1347 enum rb_block_type type : 8;
1348 unsigned int is_from_method: 1; /* bool */
1349 unsigned int is_lambda: 1; /* bool */
1350 unsigned int is_isolated: 1; /* bool */
1351 unsigned int is_refined: 1; /* bool: Proc#refined */
1353
1354typedef struct {
1355 rb_proc_header_t header;
1356 struct rb_captured_block captured;
1358
1359typedef struct {
1360 rb_proc_header_t header;
1361 VALUE symbol;
1363
1364typedef struct {
1365 rb_proc_header_t header;
1366 VALUE proc;
1368
1369/* A Proc of any block type. */
1370typedef union {
1371 const struct rb_block block;
1372 rb_proc_header_t header;
1373 rb_proc_captured_t captured;
1374 rb_proc_symbol_t symbol;
1375 rb_proc_proc_t proc;
1376} rb_proc_t;
1377
1378STATIC_ASSERT(rb_proc_captured_offset,
1379 offsetof(rb_proc_captured_t, captured) == offsetof(rb_proc_t, block.as.captured));
1380STATIC_ASSERT(rb_proc_symbol_offset,
1381 offsetof(rb_proc_symbol_t, symbol) == offsetof(rb_proc_t, block.as.symbol));
1382STATIC_ASSERT(rb_proc_proc_offset,
1383 offsetof(rb_proc_proc_t, proc) == offsetof(rb_proc_t, block.as.proc));
1384
1385/* A refined proc's refinements recipe (see Proc#refined) lives in a hidden
1386 * ivar on the proc object; the accessors return nil/NULL unless is_refined is
1387 * set. rb_proc_refinements_cref_for_call also makes the copy of the block
1388 * that Proc#refined defers until the first call, so it can raise and must not
1389 * be called outside a tag. */
1390VALUE rb_proc_refinements_recipe(VALUE procval);
1391void rb_proc_set_refinements_recipe(VALUE procval, VALUE recipe);
1392const rb_cref_t *rb_proc_refinements_cref_for_call(VALUE procval);
1393
1394RUBY_SYMBOL_EXPORT_BEGIN
1395VALUE rb_proc_isolate(VALUE self);
1396VALUE rb_proc_isolate_bang(VALUE self, VALUE replace_self);
1397VALUE rb_proc_ractor_make_shareable(VALUE proc, VALUE replace_self);
1398RUBY_SYMBOL_EXPORT_END
1399
1400typedef struct {
1401 VALUE flags; /* imemo header */
1402 rb_iseq_t *iseq;
1403 const VALUE *ep;
1404 const VALUE *env;
1405 unsigned int env_size;
1406} rb_env_t;
1407
1408extern const rb_data_type_t ruby_binding_data_type;
1409
1410#define GetBindingPtr(obj, ptr) \
1411 GetCoreDataFromValue((obj), rb_binding_t, &ruby_binding_data_type, (ptr))
1412
1413typedef struct {
1414 const struct rb_block block;
1415 const VALUE pathobj;
1416 int first_lineno;
1417} rb_binding_t;
1418
1419/* used by compile time and send insn */
1420
1421enum vm_check_match_type {
1422 VM_CHECKMATCH_TYPE_WHEN = 1,
1423 VM_CHECKMATCH_TYPE_CASE = 2,
1424 VM_CHECKMATCH_TYPE_RESCUE = 3
1425};
1426
1427#define VM_CHECKMATCH_TYPE_MASK 0x03
1428#define VM_CHECKMATCH_ARRAY 0x04
1429
1430enum vm_opt_newarray_send_type {
1431 VM_OPT_NEWARRAY_SEND_MAX = 1,
1432 VM_OPT_NEWARRAY_SEND_MIN = 2,
1433 VM_OPT_NEWARRAY_SEND_HASH = 3,
1434 VM_OPT_NEWARRAY_SEND_PACK = 4,
1435 VM_OPT_NEWARRAY_SEND_PACK_BUFFER = 5,
1436 VM_OPT_NEWARRAY_SEND_INCLUDE_P = 6,
1437};
1438
1439enum vm_special_object_type {
1440 VM_SPECIAL_OBJECT_VMCORE = 1,
1441 VM_SPECIAL_OBJECT_CBASE,
1442 VM_SPECIAL_OBJECT_CONST_BASE
1443};
1444
1445enum vm_svar_index {
1446 VM_SVAR_LASTLINE = 0, /* $_ */
1447 VM_SVAR_BACKREF = 1, /* $~ */
1448
1449 VM_SVAR_EXTRA_START = 2,
1450 VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1451};
1452
1453/* inline cache */
1454typedef struct iseq_inline_constant_cache *IC;
1455typedef struct iseq_inline_iv_cache_entry *IVC;
1456typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1457typedef union iseq_inline_storage_entry *ISE;
1458typedef const struct rb_callinfo *CALL_INFO;
1459typedef const struct rb_callcache *CALL_CACHE;
1460typedef struct rb_call_data *CALL_DATA;
1461
1462typedef VALUE CDHASH;
1463
1464#ifndef FUNC_FASTCALL
1465#define FUNC_FASTCALL(x) x
1466#endif
1467
1468typedef rb_control_frame_t *
1469 (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1470
1471#define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1472#define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1473
1474#define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1475#define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1476#define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1477
1478enum vm_frame_env_flags {
1479 /* Frame/Environment flag bits:
1480 * MMMM MMMM MMMM MMMM ___F FFFF FFFE EEEX (LSB)
1481 *
1482 * X : tag for GC marking (It seems as Fixnum)
1483 * EEE : 4 bits Env flags
1484 * FF..: 8 bits Frame flags
1485 * MM..: 15 bits frame magic (to check frame corruption)
1486 */
1487
1488 /* frame types */
1489 VM_FRAME_MAGIC_METHOD = 0x11110001,
1490 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1491 VM_FRAME_MAGIC_CLASS = 0x33330001,
1492 VM_FRAME_MAGIC_TOP = 0x44440001,
1493 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1494 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1495 VM_FRAME_MAGIC_EVAL = 0x77770001,
1496 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1497 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1498
1499 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1500
1501 /* frame flag */
1502 VM_FRAME_FLAG_FINISH = 0x0020,
1503 VM_FRAME_FLAG_BMETHOD = 0x0040,
1504 VM_FRAME_FLAG_CFRAME = 0x0080,
1505 VM_FRAME_FLAG_LAMBDA = 0x0100,
1506 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1507 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1508 VM_FRAME_FLAG_PASSED = 0x0800,
1509 VM_FRAME_FLAG_BOX_REQUIRE = 0x1000,
1510
1511 /* env flag */
1512 VM_ENV_FLAG_LOCAL = 0x0002,
1513 VM_ENV_FLAG_ESCAPED = 0x0004,
1514 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1515 VM_ENV_FLAG_ISOLATED = 0x0010,
1516};
1517
1518#define VM_ENV_DATA_SIZE ( 3)
1519
1520#define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1521#define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1522#define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1523#define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1524
1525#define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1526
1527static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1528
1529static inline void
1530VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1531{
1532 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1533 VM_ASSERT(FIXNUM_P(flags));
1534 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1535}
1536
1537static inline void
1538VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1539{
1540 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1541 VM_ASSERT(FIXNUM_P(flags));
1542 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1543}
1544
1545static inline unsigned long
1546VM_ENV_FLAGS(const VALUE *ep, long flag)
1547{
1548 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1549 VM_ASSERT(FIXNUM_P(flags));
1550 return flags & flag;
1551}
1552
1553static inline unsigned long
1554VM_ENV_FLAGS_UNCHECKED(const VALUE *ep, long flag)
1555{
1556 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1557 return flags & flag;
1558}
1559
1560static inline unsigned long
1561VM_ENV_FRAME_TYPE_P(const VALUE *ep, unsigned long frame_type)
1562{
1563 return VM_ENV_FLAGS(ep, VM_FRAME_MAGIC_MASK) == frame_type;
1564}
1565
1566static inline unsigned long
1567VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1568{
1569 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1570}
1571
1572static inline unsigned long
1573VM_FRAME_TYPE_UNCHECKED(const rb_control_frame_t *cfp)
1574{
1575 return VM_ENV_FLAGS_UNCHECKED(cfp->ep, VM_FRAME_MAGIC_MASK);
1576}
1577
1578static inline int
1579VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1580{
1581 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1582}
1583
1584static inline int
1585VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1586{
1587 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1588}
1589
1590static inline int
1591VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1592{
1593 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1594}
1595
1596static inline int
1597VM_FRAME_FINISHED_P_UNCHECKED(const rb_control_frame_t *cfp)
1598{
1599 return VM_ENV_FLAGS_UNCHECKED(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1600}
1601
1602static inline int
1603VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1604{
1605 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1606}
1607
1608static inline int
1609rb_obj_is_iseq(VALUE iseq)
1610{
1611 return imemo_type_p(iseq, imemo_iseq);
1612}
1613
1614#if VM_CHECK_MODE > 0
1615#define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1616#endif
1617
1618static inline int
1619VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1620{
1621 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1622 // With zjit_jit_frame, cfp->_iseq may be stale (not yet materialized),
1623 // so skip this assertion when jit_return is set (zjit.h is not available here).
1624 VM_ASSERT(cfp->jit_return ||
1625 RUBY_VM_NORMAL_ISEQ_P(cfp->_iseq) != cframe_p ||
1626 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1627 return cframe_p;
1628}
1629
1630static inline int
1631VM_FRAME_CFRAME_P_UNCHECKED(const rb_control_frame_t *cfp)
1632{
1633 return VM_ENV_FLAGS_UNCHECKED(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1634}
1635
1636static inline int
1637VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1638{
1639 return !VM_FRAME_CFRAME_P(cfp);
1640}
1641
1642static inline int
1643VM_FRAME_RUBYFRAME_P_UNCHECKED(const rb_control_frame_t *cfp)
1644{
1645 return !VM_FRAME_CFRAME_P_UNCHECKED(cfp);
1646}
1647
1648static inline int
1649VM_FRAME_NS_REQUIRE_P(const rb_control_frame_t *cfp)
1650{
1651 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BOX_REQUIRE) != 0;
1652}
1653
1654#define RUBYVM_CFUNC_FRAME_P(cfp) \
1655 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1656
1657#define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1658#define VM_BLOCK_HANDLER_NONE 0
1659
1660static inline int
1661VM_ENV_LOCAL_P(const VALUE *ep)
1662{
1663 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1664}
1665
1666static inline int
1667VM_ENV_LOCAL_P_UNCHECKED(const VALUE *ep)
1668{
1669 return VM_ENV_FLAGS_UNCHECKED(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1670}
1671
1672static inline const VALUE *
1673VM_ENV_PREV_EP_UNCHECKED(const VALUE *ep)
1674{
1675 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1676}
1677
1678static inline const VALUE *
1679VM_ENV_PREV_EP(const VALUE *ep)
1680{
1681 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1682 return VM_ENV_PREV_EP_UNCHECKED(ep);
1683}
1684
1685static inline bool
1686VM_ENV_BOXED_P(const VALUE *ep)
1687{
1688 return VM_ENV_FRAME_TYPE_P(ep, VM_FRAME_MAGIC_CLASS) || VM_ENV_FRAME_TYPE_P(ep, VM_FRAME_MAGIC_TOP);
1689}
1690
1691static inline VALUE
1692VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1693{
1694 if (VM_ENV_BOXED_P(ep)) {
1695 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1696 return VM_BLOCK_HANDLER_NONE;
1697 }
1698
1699 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1700 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1701}
1702
1703static inline const rb_box_t *
1704VM_ENV_BOX(const VALUE *ep)
1705{
1706 VM_ASSERT(VM_ENV_BOXED_P(ep));
1707 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1708 return (const rb_box_t *)GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1709}
1710
1711static inline const rb_box_t *
1712VM_ENV_BOX_UNCHECKED(const VALUE *ep)
1713{
1714 return (const rb_box_t *)GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1715}
1716
1717#if VM_CHECK_MODE > 0
1718int rb_vm_ep_in_heap_p(const VALUE *ep);
1719#endif
1720static inline rb_execution_context_t * rb_current_execution_context(bool expect_ec);
1721
1722static inline int
1723VM_ENV_ESCAPED_P(const VALUE *ep)
1724{
1725#if VM_CHECK_MODE > 0
1726 if (rb_current_execution_context(false)) {
1727 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1728 }
1729#endif
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:1459
#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:258
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:285
Definition vm_core.h:293
Definition vm_core.h:288
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:250
Definition vm_core.h:297
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