21#define N_OR_RUBY_DEBUG(n) (((n) > 0) ? (n) : RUBY_DEBUG)
23#define VM_CHECK_MODE N_OR_RUBY_DEBUG(0)
48#include "ruby/internal/config.h"
54#include "ruby_assert.h"
56#define RVALUE_SIZE (sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]))
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()
65#define VM_ASSERT(...) ((void)0)
66#define VM_UNREACHABLE(func) UNREACHABLE
67#define RUBY_DEBUG_THREAD_SCHEDULE()
70#define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
72#if defined(RUBY_ASSERT_CRITICAL_SECTION)
97extern int ruby_assert_critical_section_entered;
98#define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{ruby_assert_critical_section_entered += 1;}while(false)
99#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{VM_ASSERT(ruby_assert_critical_section_entered > 0);ruby_assert_critical_section_entered -= 1;}while(false)
101#define RUBY_ASSERT_CRITICAL_SECTION_ENTER()
102#define RUBY_ASSERT_CRITICAL_SECTION_LEAVE()
105#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
106# include "wasm/setjmp.h"
111#if defined(__linux__) || defined(__FreeBSD__)
112# define RB_THREAD_T_HAS_NATIVE_ID
116#include "ccan/list/list.h"
119#include "internal/array.h"
120#include "internal/basic_operators.h"
121#include "internal/sanitizers.h"
122#include "internal/serial.h"
123#include "internal/vm.h"
128#include "ruby_atomic.h"
138#ifndef VM_INSN_INFO_TABLE_IMPL
139# define VM_INSN_INFO_TABLE_IMPL 2
144# define NSIG NSIG_MAX
145#elif defined(_SIG_MAXSIG)
147# define NSIG _SIG_MAXSIG
148#elif defined(_SIGMAX)
149# define NSIG (_SIGMAX + 1)
153# define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
156#define RUBY_NSIG NSIG
159# define RUBY_SIGCHLD (SIGCLD)
160#elif defined(SIGCHLD)
161# define RUBY_SIGCHLD (SIGCHLD)
164#if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
165# define USE_SIGALTSTACK
166void *rb_allocate_sigaltstack(
void);
167void *rb_register_sigaltstack(
void *);
168# define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
169# define RB_ALTSTACK_FREE(var) free(var)
170# define RB_ALTSTACK(var) var
172# define RB_ALTSTACK_INIT(var, altstack)
173# define RB_ALTSTACK_FREE(var)
174# define RB_ALTSTACK(var) (0)
177#include THREAD_IMPL_H
178#define RUBY_VM_THREAD_MODEL 2
185#if defined(__GNUC__) && __GNUC__ >= 2
187#if OPT_TOKEN_THREADED_CODE
188#if OPT_DIRECT_THREADED_CODE
189#undef OPT_DIRECT_THREADED_CODE
196#if OPT_DIRECT_THREADED_CODE
197#undef OPT_DIRECT_THREADED_CODE
199#if OPT_TOKEN_THREADED_CODE
200#undef OPT_TOKEN_THREADED_CODE
205#if OPT_CALL_THREADED_CODE
206#if OPT_DIRECT_THREADED_CODE
207#undef OPT_DIRECT_THREADED_CODE
211void rb_vm_encoded_insn_data_table_init(
void);
212typedef unsigned long rb_num_t;
213typedef signed long rb_snum_t;
217 RUBY_TAG_RETURN = 0x1,
218 RUBY_TAG_BREAK = 0x2,
220 RUBY_TAG_RETRY = 0x4,
222 RUBY_TAG_RAISE = 0x6,
223 RUBY_TAG_THROW = 0x7,
224 RUBY_TAG_FATAL = 0x8,
228#define TAG_NONE RUBY_TAG_NONE
229#define TAG_RETURN RUBY_TAG_RETURN
230#define TAG_BREAK RUBY_TAG_BREAK
231#define TAG_NEXT RUBY_TAG_NEXT
232#define TAG_RETRY RUBY_TAG_RETRY
233#define TAG_REDO RUBY_TAG_REDO
234#define TAG_RAISE RUBY_TAG_RAISE
235#define TAG_THROW RUBY_TAG_THROW
236#define TAG_FATAL RUBY_TAG_FATAL
237#define TAG_MASK RUBY_TAG_MASK
239enum ruby_vm_throw_flags {
240 VM_THROW_NO_ESCAPE_FLAG = 0x8000,
241 VM_THROW_STATE_MASK = 0xff
256#define IMEMO_CONST_CACHE_SHAREABLE IMEMO_FL_USER0
267STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
269 sizeof(
const rb_cref_t *)) <= RVALUE_SIZE);
316#ifndef VM_ARGC_STACK_MAX
317#define VM_ARGC_STACK_MAX 128
320# define CALLING_ARGC(calling) ((calling)->heap_argv ? RARRAY_LENINT((calling)->heap_argv) : (calling)->argc)
325#define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
327#define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
329#define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
340#define PATHOBJ_PATH 0
341#define PATHOBJ_REALPATH 1
344pathobj_path(
VALUE pathobj)
356pathobj_realpath(
VALUE pathobj)
368typedef uintptr_t iseq_bits_t;
370#define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
373#define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
389enum rb_builtin_attr {
391 BUILTIN_ATTR_LEAF = 0x01,
393 BUILTIN_ATTR_SINGLE_NOARG_LEAF = 0x02,
395 BUILTIN_ATTR_INLINE_BLOCK = 0x04,
397 BUILTIN_ATTR_C_TRACE = 0x08,
403 enum rb_iseq_type type;
405 unsigned int iseq_size;
433 unsigned int has_lead : 1;
434 unsigned int has_opt : 1;
435 unsigned int has_rest : 1;
436 unsigned int has_post : 1;
437 unsigned int has_kw : 1;
438 unsigned int has_kwrest : 1;
439 unsigned int has_block : 1;
441 unsigned int ambiguous_param0 : 1;
442 unsigned int accepts_no_kwarg : 1;
443 unsigned int ruby2_keywords: 1;
444 unsigned int anon_rest: 1;
445 unsigned int anon_kwrest: 1;
446 unsigned int use_block: 1;
447 unsigned int forwardable: 1;
459 const VALUE *opt_table;
474 const struct rb_iseq_param_keyword {
480 VALUE *default_values;
489 unsigned int *positions;
491#if VM_INSN_INFO_TABLE_IMPL == 2
492 struct succ_index_table *succ_index_table;
496 const ID *local_table;
509 rb_snum_t flip_count;
512 VALUE pc2branchindex;
513 VALUE *original_iseq;
516 unsigned int local_table_size;
517 unsigned int ic_size;
518 unsigned int ise_size;
519 unsigned int ivc_size;
520 unsigned int icvarc_size;
521 unsigned int ci_size;
522 unsigned int stack_max;
524 unsigned int builtin_attrs;
539 rb_jit_func_t jit_entry;
541 long unsigned jit_entry_calls;
543 rb_jit_func_t jit_exception;
545 long unsigned jit_exception_calls;
549 uint64_t yjit_calls_at_interv;
576#define ISEQ_BODY(iseq) ((iseq)->body)
578#if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
579#define USE_LAZY_LOAD 0
590 if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
597rb_iseq_attr_p(
const rb_iseq_t *iseq,
enum rb_builtin_attr attr)
599 return (ISEQ_BODY(iseq)->builtin_attrs & attr) == attr;
607 if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug(
"def_iseq_ptr: not iseq (%d)", def->type);
609 return rb_iseq_check(def->body.iseq.
iseqptr);
612enum ruby_special_exceptions {
616 ruby_error_stackfatal,
617 ruby_error_stream_closed,
618 ruby_special_error_count
621#define GetVMPtr(obj, ptr) \
622 GetCoreDataFromValue((obj), rb_vm_t, (ptr))
628 rb_vm_at_exit_func *func;
632void *rb_objspace_alloc(
void);
633void rb_objspace_free(
void *
objspace);
634void rb_objspace_call_finalizer(
void);
639 unsigned int running;
657 struct ccan_list_head set;
659 unsigned int blocking_cnt;
666 rb_nativethread_lock_t lock;
668 unsigned int lock_rec;
671 rb_nativethread_cond_t terminate_cond;
672 bool terminate_waiting;
674#ifndef RUBY_THREAD_PTHREAD_H
675 bool barrier_waiting;
676 unsigned int barrier_cnt;
677 rb_nativethread_cond_t barrier_cond;
683 rb_nativethread_lock_t lock;
687 rb_nativethread_cond_t cond;
688 unsigned int snt_cnt;
689 unsigned int dnt_cnt;
691 unsigned int running_cnt;
693 unsigned int max_cpu;
694 struct ccan_list_head grq;
695 unsigned int grq_cnt;
698 struct ccan_list_head running_threads;
701 struct ccan_list_head timeslice_threads;
703 struct ccan_list_head zombie_threads;
706 bool timeslice_wait_inf;
709 rb_nativethread_cond_t barrier_complete_cond;
710 rb_nativethread_cond_t barrier_release_cond;
711 bool barrier_waiting;
712 unsigned int barrier_waiting_cnt;
713 unsigned int barrier_serial;
717#ifdef USE_SIGALTSTACK
721 rb_serial_t fork_gen;
722 struct ccan_list_head waiting_fds;
725 volatile int ubf_async_safe;
727 unsigned int running: 1;
728 unsigned int thread_abort_on_exception: 1;
729 unsigned int thread_report_on_exception: 1;
730 unsigned int thread_ignore_deadlock: 1;
733 VALUE mark_object_ary;
735 const VALUE special_exceptions[ruby_special_error_count];
740 VALUE load_path_snapshot;
741 VALUE load_path_check_cache;
742 VALUE expanded_load_path;
743 VALUE loaded_features;
744 VALUE loaded_features_snapshot;
745 VALUE loaded_features_realpaths;
746 VALUE loaded_features_realpath_map;
747 struct st_table *loaded_features_index;
755 VALUE cmd[RUBY_NSIG];
761 int src_encoding_index;
764 struct ccan_list_head workqueue;
765 rb_nativethread_lock_t workqueue_lock;
767 VALUE orig_progname, progname;
768 VALUE coverages, me2counter;
773 struct gc_mark_func_data_struct {
775 void (*mark_func)(
VALUE v,
void *data);
788 st_table *unused_block_warning_table;
795 ID inserting_constant_cache_id;
797#ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
798#define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
800 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE];
802#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
808 size_t thread_vm_stack_size;
809 size_t thread_machine_stack_size;
810 size_t fiber_vm_stack_size;
811 size_t fiber_machine_stack_size;
818#define RUBY_VM_SIZE_ALIGN 4096
820#define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE))
821#define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE))
822#define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE))
823#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE))
825#define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE))
826#define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE))
827#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE))
828#if defined(__powerpc64__) || defined(__ppc64__)
829#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE))
831#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE))
834#if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
836#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
837#define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
838#undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
839#define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
840#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
841#define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
842#undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
843#define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
846#ifndef VM_DEBUG_BP_CHECK
847#define VM_DEBUG_BP_CHECK 0
850#ifndef VM_DEBUG_VERIFY_METHOD_CACHE
851#define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
864enum rb_block_handler_type {
865 block_handler_type_iseq,
866 block_handler_type_ifunc,
867 block_handler_type_symbol,
868 block_handler_type_proc
884 enum rb_block_type
type;
893 const void *block_code;
903rb_thread_ptr(
VALUE thval)
908enum rb_thread_status {
911 THREAD_STOPPED_FOREVER,
916typedef RUBY_JMP_BUF rb_jmpbuf_t;
918typedef void *rb_jmpbuf_t[5];
933#if defined(__wasm__) && !defined(__EMSCRIPTEN__)
943typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
945#define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
948rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
950 *jmpbuf = ruby_xmalloc(
sizeof(rb_jmpbuf_t));
954rb_vm_tag_jmpbuf_deinit(
const rb_vm_tag_jmpbuf_t *jmpbuf)
959typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
961#define RB_VM_TAG_JMPBUF_GET(buf) (buf)
964rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
970rb_vm_tag_jmpbuf_deinit(
const rb_vm_tag_jmpbuf_t *jmpbuf)
983 rb_vm_tag_jmpbuf_t buf;
985 enum ruby_tag_type state;
986 unsigned int lock_rec;
989STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(
struct rb_vm_tag, buf) > 0);
990STATIC_ASSERT(rb_vm_tag_buf_end,
991 offsetof(
struct rb_vm_tag, buf) +
sizeof(rb_vm_tag_jmpbuf_t) <
1012 size_t vm_stack_size;
1020#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1021 uint32_t checked_clock;
1029 VALUE local_storage_recursive_hash;
1030 VALUE local_storage_recursive_hash_for_trace;
1036 const VALUE *root_lep;
1044 VALUE passed_block_handler;
1046 uint8_t raised_flag;
1049 BITFIELD(
enum method_missing_reason, method_missing_reason, 8);
1051 VALUE private_const_reference;
1057 size_t stack_maxsize;
1060#ifdef RUBY_ASAN_ENABLED
1061 void *asan_fake_stack_handle;
1066#ifndef rb_execution_context_t
1068#define rb_execution_context_t rb_execution_context_t
1072#define VM_CORE_H_EC_DEFINED 1
1096 struct ccan_list_node lt_node;
1104 bool mn_schedulable;
1118 BITFIELD(
enum rb_thread_status, status, 2);
1120 unsigned int has_dedicated_nt : 1;
1121 unsigned int to_kill : 1;
1122 unsigned int abort_on_exception: 1;
1123 unsigned int report_on_exception: 1;
1124 unsigned int pending_interrupt_queue_checked: 1;
1126 uint32_t running_time_us;
1128 void *blocking_region_buffer;
1134#if OPT_CALL_THREADED_CODE
1139 VALUE pending_interrupt_queue;
1140 VALUE pending_interrupt_mask_stack;
1143 rb_nativethread_lock_t interrupt_lock;
1145 VALUE locking_mutex;
1147 struct ccan_list_head interrupt_exec_tasks;
1158 VALUE (*func)(
void *);
1163 enum thread_invoke_type {
1164 thread_invoke_type_none = 0,
1165 thread_invoke_type_proc,
1166 thread_invoke_type_ractor_proc,
1167 thread_invoke_type_func
1171 VALUE stat_insn_usage;
1177 unsigned int blocking;
1181 void **specific_storage;
1186static inline unsigned int
1189 return th ? (
unsigned int)th->serial : 0;
1193 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1194 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1195 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1197 VM_DEFINECLASS_TYPE_MASK = 0x07
1198} rb_vm_defineclass_type_t;
1200#define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1201#define VM_DEFINECLASS_FLAG_SCOPED 0x08
1202#define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1203#define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1204#define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1205 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1208RUBY_SYMBOL_EXPORT_BEGIN
1217 VALUE script_lines);
1227rb_iseq_new_with_callback_new_callback(
1250RUBY_SYMBOL_EXPORT_END
1252#define GetProcPtr(obj, ptr) \
1253 GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1257 unsigned int is_from_method: 1;
1258 unsigned int is_lambda: 1;
1259 unsigned int is_isolated: 1;
1262RUBY_SYMBOL_EXPORT_BEGIN
1265VALUE rb_proc_ractor_make_shareable(
VALUE self);
1266RUBY_SYMBOL_EXPORT_END
1273 unsigned int env_size;
1278#define GetBindingPtr(obj, ptr) \
1279 GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1283 const VALUE pathobj;
1289enum vm_check_match_type {
1290 VM_CHECKMATCH_TYPE_WHEN = 1,
1291 VM_CHECKMATCH_TYPE_CASE = 2,
1292 VM_CHECKMATCH_TYPE_RESCUE = 3
1295#define VM_CHECKMATCH_TYPE_MASK 0x03
1296#define VM_CHECKMATCH_ARRAY 0x04
1298enum vm_opt_newarray_send_type {
1299 VM_OPT_NEWARRAY_SEND_MAX = 1,
1300 VM_OPT_NEWARRAY_SEND_MIN = 2,
1301 VM_OPT_NEWARRAY_SEND_HASH = 3,
1302 VM_OPT_NEWARRAY_SEND_PACK = 4,
1303 VM_OPT_NEWARRAY_SEND_PACK_BUFFER = 5,
1304 VM_OPT_NEWARRAY_SEND_INCLUDE_P = 6,
1307enum vm_special_object_type {
1308 VM_SPECIAL_OBJECT_VMCORE = 1,
1309 VM_SPECIAL_OBJECT_CBASE,
1310 VM_SPECIAL_OBJECT_CONST_BASE
1314 VM_SVAR_LASTLINE = 0,
1315 VM_SVAR_BACKREF = 1,
1317 VM_SVAR_EXTRA_START = 2,
1318 VM_SVAR_FLIPFLOP_START = 2
1330typedef VALUE CDHASH;
1332#ifndef FUNC_FASTCALL
1333#define FUNC_FASTCALL(x) x
1339#define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1340#define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1342#define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1343#define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1344#define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1346enum vm_frame_env_flags {
1357 VM_FRAME_MAGIC_METHOD = 0x11110001,
1358 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1359 VM_FRAME_MAGIC_CLASS = 0x33330001,
1360 VM_FRAME_MAGIC_TOP = 0x44440001,
1361 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1362 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1363 VM_FRAME_MAGIC_EVAL = 0x77770001,
1364 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1365 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1367 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1370 VM_FRAME_FLAG_FINISH = 0x0020,
1371 VM_FRAME_FLAG_BMETHOD = 0x0040,
1372 VM_FRAME_FLAG_CFRAME = 0x0080,
1373 VM_FRAME_FLAG_LAMBDA = 0x0100,
1374 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1375 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1376 VM_FRAME_FLAG_PASSED = 0x0800,
1379 VM_ENV_FLAG_LOCAL = 0x0002,
1380 VM_ENV_FLAG_ESCAPED = 0x0004,
1381 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1382 VM_ENV_FLAG_ISOLATED = 0x0010,
1385#define VM_ENV_DATA_SIZE ( 3)
1387#define VM_ENV_DATA_INDEX_ME_CREF (-2)
1388#define VM_ENV_DATA_INDEX_SPECVAL (-1)
1389#define VM_ENV_DATA_INDEX_FLAGS ( 0)
1390#define VM_ENV_DATA_INDEX_ENV ( 1)
1392#define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1394static inline void VM_FORCE_WRITE_SPECIAL_CONST(
const VALUE *ptr,
VALUE special_const_value);
1399 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1401 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1405VM_ENV_FLAGS_UNSET(
const VALUE *ep,
VALUE flag)
1407 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1409 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1412static inline unsigned long
1413VM_ENV_FLAGS(
const VALUE *ep,
long flag)
1415 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1417 return flags & flag;
1420static inline unsigned long
1423 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1429 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1435 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1441 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1447 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1451rb_obj_is_iseq(
VALUE iseq)
1453 return imemo_type_p(iseq, imemo_iseq);
1456#if VM_CHECK_MODE > 0
1457#define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1463 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1464 VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1465 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1472 return !VM_FRAME_CFRAME_P(cfp);
1475#define RUBYVM_CFUNC_FRAME_P(cfp) \
1476 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1478#define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1479#define VM_BLOCK_HANDLER_NONE 0
1482VM_ENV_LOCAL_P(
const VALUE *ep)
1484 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1487static inline const VALUE *
1488VM_ENV_PREV_EP(
const VALUE *ep)
1490 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1491 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1495VM_ENV_BLOCK_HANDLER(
const VALUE *ep)
1497 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1498 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1501#if VM_CHECK_MODE > 0
1502int rb_vm_ep_in_heap_p(
const VALUE *ep);
1506VM_ENV_ESCAPED_P(
const VALUE *ep)
1508 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1509 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1514VM_ENV_ENVVAL(const
VALUE *ep)
1516 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1517 VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1518 VM_ASSERT(envval ==
Qundef || imemo_type_p(envval, imemo_env));
1524VM_ENV_ENVVAL_PTR(const
VALUE *ep)
1526 return (
const rb_env_t *)VM_ENV_ENVVAL(ep);
1534 env->env = env_body;
1535 env->env_size = env_size;
1536 env_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)env;
1543 *((
VALUE *)ptr) = v;
1547VM_FORCE_WRITE_SPECIAL_CONST(
const VALUE *ptr,
VALUE special_const_value)
1550 VM_FORCE_WRITE(ptr, special_const_value);
1554VM_STACK_ENV_WRITE(
const VALUE *ep,
int index,
VALUE v)
1556 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1557 VM_FORCE_WRITE(&ep[index], v);
1560const VALUE *rb_vm_ep_local_ep(
const VALUE *ep);
1567#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1568#define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1570#define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1571 ((void *)(ecfp) > (void *)(cfp))
1582 return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1586VM_BH_ISEQ_BLOCK_P(
VALUE block_handler)
1588 if ((block_handler & 0x03) == 0x01) {
1589#if VM_CHECK_MODE > 0
1591 VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1603 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1604 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1605 return block_handler;
1609VM_BH_TO_ISEQ_BLOCK(
VALUE block_handler)
1612 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1617VM_BH_IFUNC_P(
VALUE block_handler)
1619 if ((block_handler & 0x03) == 0x03) {
1620#if VM_CHECK_MODE > 0
1622 VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1634 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1635 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1636 return block_handler;
1640VM_BH_TO_IFUNC_BLOCK(
VALUE block_handler)
1643 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1648VM_BH_TO_CAPT_BLOCK(
VALUE block_handler)
1651 VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1655static inline enum rb_block_handler_type
1656vm_block_handler_type(
VALUE block_handler)
1658 if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1659 return block_handler_type_iseq;
1661 else if (VM_BH_IFUNC_P(block_handler)) {
1662 return block_handler_type_ifunc;
1664 else if (
SYMBOL_P(block_handler)) {
1665 return block_handler_type_symbol;
1669 return block_handler_type_proc;
1674vm_block_handler_verify(MAYBE_UNUSED(
VALUE block_handler))
1676 VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1677 (vm_block_handler_type(block_handler), 1));
1680static inline enum rb_block_type
1681vm_block_type(
const struct rb_block *block)
1683#if VM_CHECK_MODE > 0
1684 switch (block->type) {
1685 case block_type_iseq:
1686 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1688 case block_type_ifunc:
1689 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1691 case block_type_symbol:
1692 VM_ASSERT(
SYMBOL_P(block->as.symbol));
1694 case block_type_proc:
1703vm_block_type_set(
const struct rb_block *block,
enum rb_block_type
type)
1709static inline const struct rb_block *
1710vm_proc_block(
VALUE procval)
1717static inline const VALUE *vm_block_ep(
const struct rb_block *block);
1720vm_proc_iseq(
VALUE procval)
1722 return vm_block_iseq(vm_proc_block(procval));
1725static inline const VALUE *
1726vm_proc_ep(
VALUE procval)
1728 return vm_block_ep(vm_proc_block(procval));
1732vm_block_iseq(
const struct rb_block *block)
1734 switch (vm_block_type(block)) {
1735 case block_type_iseq:
return rb_iseq_check(block->as.captured.code.iseq);
1736 case block_type_proc:
return vm_proc_iseq(block->as.proc);
1737 case block_type_ifunc:
1738 case block_type_symbol:
return NULL;
1740 VM_UNREACHABLE(vm_block_iseq);
1744static inline const VALUE *
1745vm_block_ep(
const struct rb_block *block)
1747 switch (vm_block_type(block)) {
1748 case block_type_iseq:
1749 case block_type_ifunc:
return block->as.captured.ep;
1750 case block_type_proc:
return vm_proc_ep(block->as.proc);
1751 case block_type_symbol:
return NULL;
1753 VM_UNREACHABLE(vm_block_ep);
1758vm_block_self(
const struct rb_block *block)
1760 switch (vm_block_type(block)) {
1761 case block_type_iseq:
1762 case block_type_ifunc:
1763 return block->as.captured.self;
1764 case block_type_proc:
1765 return vm_block_self(vm_proc_block(block->as.proc));
1766 case block_type_symbol:
1769 VM_UNREACHABLE(vm_block_self);
1774VM_BH_TO_SYMBOL(
VALUE block_handler)
1776 VM_ASSERT(
SYMBOL_P(block_handler));
1777 return block_handler;
1781VM_BH_FROM_SYMBOL(
VALUE symbol)
1788VM_BH_TO_PROC(
VALUE block_handler)
1791 return block_handler;
1795VM_BH_FROM_PROC(
VALUE procval)
1812#define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp, stderr)
1813#define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp), stderr)
1814bool rb_vm_bugreport(
const void *,
FILE *);
1815typedef void (*ruby_sighandler_t)(int);
1817NORETURN(
void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler,
int sig, const
void *, const
char *fmt, ...));
1820RUBY_SYMBOL_EXPORT_BEGIN
1825RUBY_SYMBOL_EXPORT_END
1839 return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1845 return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1853void rb_vm_inc_const_missing_count(
void);
1859void rb_thread_start_timer_thread(
void);
1860void rb_thread_stop_timer_thread(
void);
1861void rb_thread_reset_timer_thread(
void);
1862void rb_thread_wakeup_timer_thread(
int);
1865rb_vm_living_threads_init(
rb_vm_t *vm)
1867 ccan_list_head_init(&vm->waiting_fds);
1868 ccan_list_head_init(&vm->workqueue);
1869 ccan_list_head_init(&vm->ractor.set);
1870 ccan_list_head_init(&vm->ractor.sched.zombie_threads);
1873typedef int rb_backtrace_iter_func(
void *,
VALUE,
int,
VALUE);
1879void ruby_thread_init_stack(
rb_thread_t *th,
void *local_in_parent_frame);
1884void rb_vm_env_write(
const VALUE *ep,
int index,
VALUE v);
1887void rb_vm_register_special_exception_str(
enum ruby_special_exceptions sp,
VALUE exception_class,
VALUE mesg);
1889#define rb_vm_register_special_exception(sp, e, m) \
1890 rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1898#define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1900#define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1901 STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1902 STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1903 const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1904 if (UNLIKELY((cfp) <= &bound[1])) { \
1905 vm_stackoverflow(); \
1909#define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1910 CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1918#if RUBY_VM_THREAD_MODEL == 2
1926#define GET_VM() rb_current_vm()
1927#define GET_RACTOR() rb_current_ractor()
1928#define GET_THREAD() rb_current_thread()
1929#define GET_EC() rb_current_execution_context(true)
1934 return ec->thread_ptr;
1942 VM_ASSERT(th->ractor != NULL);
1965rb_current_execution_context(
bool expect_ec)
1967#ifdef RB_THREAD_LOCAL_SPECIFIER
1968 #if defined(__arm64__) || defined(__aarch64__)
1986 VM_ASSERT(ec == rb_current_ec_noinline());
1990 VM_ASSERT(!expect_ec || ec != NULL);
1995rb_current_thread(
void)
1998 return rb_ec_thread_ptr(ec);
2002rb_current_ractor_raw(
bool expect)
2004 if (ruby_single_main_ractor) {
2005 return ruby_single_main_ractor;
2009 return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
2014rb_current_ractor(
void)
2016 return rb_current_ractor_raw(
true);
2023 VM_ASSERT(ruby_current_vm_ptr == NULL ||
2024 ruby_current_execution_context_ptr == NULL ||
2025 rb_ec_thread_ptr(GET_EC()) == NULL ||
2026 rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
2027 rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
2030 return ruby_current_vm_ptr;
2034 unsigned int recorded_lock_rec,
2035 unsigned int current_lock_rec);
2037static inline unsigned int
2040 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2042 if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
2046 return vm->ractor.sync.lock_rec;
2051#error "unsupported thread model"
2055 TIMER_INTERRUPT_MASK = 0x01,
2056 PENDING_INTERRUPT_MASK = 0x02,
2057 POSTPONED_JOB_INTERRUPT_MASK = 0x04,
2058 TRAP_INTERRUPT_MASK = 0x08,
2059 TERMINATE_INTERRUPT_MASK = 0x10,
2060 VM_BARRIER_INTERRUPT_MASK = 0x20,
2063#define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
2064#define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
2065#define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
2066#define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
2067#define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
2068#define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
2069#define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
2070 (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
2075#if defined(USE_VM_CLOCK) && USE_VM_CLOCK
2076 uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
2078 if (current_clock != ec->checked_clock) {
2079 ec->checked_clock = current_clock;
2080 RUBY_VM_SET_TIMER_INTERRUPT(ec);
2083 return ec->interrupt_flag & ~(ec)->interrupt_mask;
2087int rb_signal_buff_size(
void);
2090void rb_threadptr_signal_raise(
rb_thread_t *th,
int sig);
2092int rb_threadptr_execute_interrupts(
rb_thread_t *,
int);
2094void rb_threadptr_unlock_all_locking_mutexes(
rb_thread_t *th);
2095void rb_threadptr_pending_interrupt_clear(
rb_thread_t *th);
2106void rb_vm_cond_wait(
rb_vm_t *vm, rb_nativethread_cond_t *cond);
2107void rb_vm_cond_timedwait(
rb_vm_t *vm, rb_nativethread_cond_t *cond,
unsigned long msec);
2109#define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
2113#ifdef RUBY_ASSERT_CRITICAL_SECTION
2114 VM_ASSERT(ruby_assert_critical_section_entered == 0);
2117 VM_ASSERT(ec == GET_EC());
2119 if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2120 rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2151#define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2152 const rb_event_flag_t flag_arg_ = (flag_); \
2153 rb_hook_list_t *hooks_arg_ = (hooks_); \
2154 if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2156 rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2166 VM_ASSERT((hooks->events & flag) != 0);
2168 trace_arg.event = flag;
2170 trace_arg.cfp = ec->cfp;
2171 trace_arg.self = self;
2173 trace_arg.called_id = called_id;
2174 trace_arg.klass = klass;
2175 trace_arg.data = data;
2177 trace_arg.klass_solved = 0;
2179 rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2192 return &cr_pub->hooks;
2195#define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2196 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2198#define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2199 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2206 rb_ary_new_from_args(2, eval_script, (
VALUE)iseq));
2209void rb_vm_trap_exit(
rb_vm_t *vm);
2210void rb_vm_postponed_job_atfork(
void);
2211void rb_vm_postponed_job_free(
void);
2212size_t rb_vm_memsize_postponed_job_queue(
void);
2213void rb_vm_postponed_job_queue_init(
rb_vm_t *vm);
2215RUBY_SYMBOL_EXPORT_BEGIN
2217int rb_thread_check_trap_pending(
void);
2220#define RUBY_EVENT_COVERAGE_LINE 0x010000
2221#define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2223extern VALUE rb_get_coverages(
void);
2224extern void rb_set_coverages(
VALUE,
int,
VALUE);
2225extern void rb_clear_coverages(
void);
2226extern void rb_reset_coverages(
void);
2227extern void rb_resume_coverages(
void);
2228extern void rb_suspend_coverages(
void);
2230void rb_postponed_job_flush(
rb_vm_t *vm);
2236RUBY_SYMBOL_EXPORT_END
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ALIGNAS
Wraps (or simulates) alignas.
#define RUBY_EXTERN
Declaration of externally visible global variables.
#define RUBY_EVENT_SCRIPT_COMPILED
Encountered an eval.
uint32_t rb_event_flag_t
Represents event(s).
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#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.
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.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
void rb_unblock_function_t(void *)
This is the type of UBFs.
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.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Functions related to nodes in the AST.
#define RARRAY_AREF(a, i)
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
const ID * segments
A null-terminated list of ids, used to represent a constant's path idNULL is used to represent the ::...
This is the struct that holds necessary info for a struct.
struct rb_iseq_constant_body::@155 param
parameter information
const rb_iseq_t * iseqptr
iseq pointer, should be separated from iseqval
IFUNC (Internal FUNCtion)
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.