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)
97 extern 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"
118 #include "internal.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
142 #if defined(NSIG_MAX)
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
166 void *rb_allocate_sigaltstack(
void);
167 void *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
211 void rb_vm_encoded_insn_data_table_init(
void);
212 typedef unsigned long rb_num_t;
213 typedef 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
239 enum 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
267 STATIC_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
344 pathobj_path(
VALUE pathobj)
356 pathobj_realpath(
VALUE pathobj)
370 typedef uintptr_t iseq_bits_t;
372 #define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
375 #define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
391 enum rb_builtin_attr {
393 BUILTIN_ATTR_LEAF = 0x01,
395 BUILTIN_ATTR_SINGLE_NOARG_LEAF = 0x02,
397 BUILTIN_ATTR_INLINE_BLOCK = 0x04,
399 BUILTIN_ATTR_C_TRACE = 0x08,
405 enum rb_iseq_type type;
407 unsigned int iseq_size;
435 unsigned int has_lead : 1;
436 unsigned int has_opt : 1;
437 unsigned int has_rest : 1;
438 unsigned int has_post : 1;
439 unsigned int has_kw : 1;
440 unsigned int has_kwrest : 1;
441 unsigned int has_block : 1;
443 unsigned int ambiguous_param0 : 1;
444 unsigned int accepts_no_kwarg : 1;
445 unsigned int ruby2_keywords: 1;
446 unsigned int anon_rest: 1;
447 unsigned int anon_kwrest: 1;
448 unsigned int use_block: 1;
449 unsigned int forwardable: 1;
461 const VALUE *opt_table;
476 const struct rb_iseq_param_keyword {
482 VALUE *default_values;
491 unsigned int *positions;
493 #if VM_INSN_INFO_TABLE_IMPL == 2
498 const ID *local_table;
511 rb_snum_t flip_count;
514 VALUE pc2branchindex;
515 VALUE *original_iseq;
518 unsigned int local_table_size;
519 unsigned int ic_size;
520 unsigned int ise_size;
521 unsigned int ivc_size;
522 unsigned int icvarc_size;
523 unsigned int ci_size;
524 unsigned int stack_max;
526 unsigned int builtin_attrs;
539 #if USE_RJIT || USE_YJIT
541 rb_jit_func_t jit_entry;
543 long unsigned jit_entry_calls;
548 rb_jit_func_t jit_exception;
550 long unsigned jit_exception_calls;
562 uint64_t yjit_calls_at_interv;
589 #define ISEQ_BODY(iseq) ((iseq)->body)
591 #if !defined(USE_LAZY_LOAD) || !(USE_LAZY_LOAD+0)
592 #define USE_LAZY_LOAD 0
603 if (USE_LAZY_LOAD && ISEQ_BODY(iseq) == NULL) {
610 rb_iseq_attr_p(
const rb_iseq_t *iseq,
enum rb_builtin_attr attr)
612 return (ISEQ_BODY(iseq)->builtin_attrs & attr) == attr;
619 #if VM_CHECK_MODE > 0
620 if (def->type != VM_METHOD_TYPE_ISEQ)
rb_bug(
"def_iseq_ptr: not iseq (%d)", def->type);
622 return rb_iseq_check(def->body.iseq.
iseqptr);
625 enum ruby_special_exceptions {
629 ruby_error_stackfatal,
630 ruby_error_stream_closed,
631 ruby_special_error_count
634 #define GetVMPtr(obj, ptr) \
635 GetCoreDataFromValue((obj), rb_vm_t, (ptr))
641 rb_vm_at_exit_func *func;
645 void *rb_objspace_alloc(
void);
646 void rb_objspace_free(
void *
objspace);
647 void rb_objspace_call_finalizer(
void);
652 unsigned int running;
670 struct ccan_list_head set;
672 unsigned int blocking_cnt;
679 rb_nativethread_lock_t lock;
681 unsigned int lock_rec;
684 rb_nativethread_cond_t terminate_cond;
685 bool terminate_waiting;
687 #ifndef RUBY_THREAD_PTHREAD_H
688 bool barrier_waiting;
689 unsigned int barrier_cnt;
690 rb_nativethread_cond_t barrier_cond;
696 rb_nativethread_lock_t lock;
700 rb_nativethread_cond_t cond;
701 unsigned int snt_cnt;
702 unsigned int dnt_cnt;
704 unsigned int running_cnt;
706 unsigned int max_cpu;
707 struct ccan_list_head grq;
708 unsigned int grq_cnt;
711 struct ccan_list_head running_threads;
714 struct ccan_list_head timeslice_threads;
716 struct ccan_list_head zombie_threads;
719 bool timeslice_wait_inf;
722 rb_nativethread_cond_t barrier_complete_cond;
723 rb_nativethread_cond_t barrier_release_cond;
724 bool barrier_waiting;
725 unsigned int barrier_waiting_cnt;
726 unsigned int barrier_serial;
730 #ifdef USE_SIGALTSTACK
734 rb_serial_t fork_gen;
735 struct ccan_list_head waiting_fds;
738 volatile int ubf_async_safe;
740 unsigned int running: 1;
741 unsigned int thread_abort_on_exception: 1;
742 unsigned int thread_report_on_exception: 1;
743 unsigned int thread_ignore_deadlock: 1;
746 VALUE mark_object_ary;
748 const VALUE special_exceptions[ruby_special_error_count];
753 VALUE load_path_snapshot;
754 VALUE load_path_check_cache;
755 VALUE expanded_load_path;
756 VALUE loaded_features;
757 VALUE loaded_features_snapshot;
758 VALUE loaded_features_realpaths;
759 VALUE loaded_features_realpath_map;
760 struct st_table *loaded_features_index;
768 VALUE cmd[RUBY_NSIG];
774 int src_encoding_index;
777 struct ccan_list_head workqueue;
778 rb_nativethread_lock_t workqueue_lock;
780 VALUE orig_progname, progname;
781 VALUE coverages, me2counter;
786 struct gc_mark_func_data_struct {
788 void (*mark_func)(
VALUE v,
void *data);
801 st_table *unused_block_warning_table;
808 ID inserting_constant_cache_id;
810 #ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
811 #define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
813 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE];
815 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
821 size_t thread_vm_stack_size;
822 size_t thread_machine_stack_size;
823 size_t fiber_vm_stack_size;
824 size_t fiber_machine_stack_size;
831 #define RUBY_VM_SIZE_ALIGN 4096
833 #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE))
834 #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE))
835 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE))
836 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE))
838 #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE))
839 #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE))
840 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE))
841 #if defined(__powerpc64__) || defined(__ppc64__)
842 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE))
844 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE))
847 #if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
849 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
850 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
851 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
852 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
853 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
854 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
855 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
856 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
859 #ifndef VM_DEBUG_BP_CHECK
860 #define VM_DEBUG_BP_CHECK 0
863 #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
864 #define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
877 enum rb_block_handler_type {
878 block_handler_type_iseq,
879 block_handler_type_ifunc,
880 block_handler_type_symbol,
881 block_handler_type_proc
897 enum rb_block_type
type;
906 const void *block_code;
908 #if VM_DEBUG_BP_CHECK
916 rb_thread_ptr(
VALUE thval)
921 enum rb_thread_status {
924 THREAD_STOPPED_FOREVER,
929 typedef RUBY_JMP_BUF rb_jmpbuf_t;
931 typedef void *rb_jmpbuf_t[5];
946 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
956 typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
958 #define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
961 rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
967 rb_vm_tag_jmpbuf_deinit(
const rb_vm_tag_jmpbuf_t *jmpbuf)
972 typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
974 #define RB_VM_TAG_JMPBUF_GET(buf) (buf)
977 rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
983 rb_vm_tag_jmpbuf_deinit(
const rb_vm_tag_jmpbuf_t *jmpbuf)
996 rb_vm_tag_jmpbuf_t buf;
998 enum ruby_tag_type state;
999 unsigned int lock_rec;
1002 STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(
struct rb_vm_tag, buf) > 0);
1003 STATIC_ASSERT(rb_vm_tag_buf_end,
1004 offsetof(
struct rb_vm_tag, buf) +
sizeof(rb_vm_tag_jmpbuf_t) <
1025 size_t vm_stack_size;
1033 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1034 uint32_t checked_clock;
1042 VALUE local_storage_recursive_hash;
1043 VALUE local_storage_recursive_hash_for_trace;
1049 const VALUE *root_lep;
1057 VALUE passed_block_handler;
1059 uint8_t raised_flag;
1062 BITFIELD(
enum method_missing_reason, method_missing_reason, 8);
1064 VALUE private_const_reference;
1070 size_t stack_maxsize;
1073 #ifdef RUBY_ASAN_ENABLED
1074 void *asan_fake_stack_handle;
1079 #ifndef rb_execution_context_t
1081 #define rb_execution_context_t rb_execution_context_t
1085 #define VM_CORE_H_EC_DEFINED 1
1109 struct ccan_list_node lt_node;
1117 bool mn_schedulable;
1131 BITFIELD(
enum rb_thread_status, status, 2);
1133 unsigned int has_dedicated_nt : 1;
1134 unsigned int to_kill : 1;
1135 unsigned int abort_on_exception: 1;
1136 unsigned int report_on_exception: 1;
1137 unsigned int pending_interrupt_queue_checked: 1;
1139 uint32_t running_time_us;
1141 void *blocking_region_buffer;
1147 #if OPT_CALL_THREADED_CODE
1152 VALUE pending_interrupt_queue;
1153 VALUE pending_interrupt_mask_stack;
1156 rb_nativethread_lock_t interrupt_lock;
1158 VALUE locking_mutex;
1160 struct ccan_list_head interrupt_exec_tasks;
1171 VALUE (*func)(
void *);
1176 enum thread_invoke_type {
1177 thread_invoke_type_none = 0,
1178 thread_invoke_type_proc,
1179 thread_invoke_type_ractor_proc,
1180 thread_invoke_type_func
1184 VALUE stat_insn_usage;
1190 unsigned int blocking;
1194 void **specific_storage;
1199 static inline unsigned int
1202 return th ? (
unsigned int)th->serial : 0;
1206 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1207 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1208 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1210 VM_DEFINECLASS_TYPE_MASK = 0x07
1211 } rb_vm_defineclass_type_t;
1213 #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1214 #define VM_DEFINECLASS_FLAG_SCOPED 0x08
1215 #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1216 #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1217 #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1218 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1221 RUBY_SYMBOL_EXPORT_BEGIN
1230 VALUE script_lines);
1240 rb_iseq_new_with_callback_new_callback(
1263 RUBY_SYMBOL_EXPORT_END
1265 #define GetProcPtr(obj, ptr) \
1266 GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1270 unsigned int is_from_method: 1;
1271 unsigned int is_lambda: 1;
1272 unsigned int is_isolated: 1;
1275 RUBY_SYMBOL_EXPORT_BEGIN
1278 VALUE rb_proc_ractor_make_shareable(
VALUE self);
1279 RUBY_SYMBOL_EXPORT_END
1286 unsigned int env_size;
1291 #define GetBindingPtr(obj, ptr) \
1292 GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1296 const VALUE pathobj;
1302 enum vm_check_match_type {
1303 VM_CHECKMATCH_TYPE_WHEN = 1,
1304 VM_CHECKMATCH_TYPE_CASE = 2,
1305 VM_CHECKMATCH_TYPE_RESCUE = 3
1308 #define VM_CHECKMATCH_TYPE_MASK 0x03
1309 #define VM_CHECKMATCH_ARRAY 0x04
1311 enum vm_opt_newarray_send_type {
1312 VM_OPT_NEWARRAY_SEND_MAX = 1,
1313 VM_OPT_NEWARRAY_SEND_MIN = 2,
1314 VM_OPT_NEWARRAY_SEND_HASH = 3,
1315 VM_OPT_NEWARRAY_SEND_PACK = 4,
1316 VM_OPT_NEWARRAY_SEND_PACK_BUFFER = 5,
1317 VM_OPT_NEWARRAY_SEND_INCLUDE_P = 6,
1320 enum vm_special_object_type {
1321 VM_SPECIAL_OBJECT_VMCORE = 1,
1322 VM_SPECIAL_OBJECT_CBASE,
1323 VM_SPECIAL_OBJECT_CONST_BASE
1326 enum vm_svar_index {
1327 VM_SVAR_LASTLINE = 0,
1328 VM_SVAR_BACKREF = 1,
1330 VM_SVAR_EXTRA_START = 2,
1331 VM_SVAR_FLIPFLOP_START = 2
1343 typedef VALUE CDHASH;
1345 #ifndef FUNC_FASTCALL
1346 #define FUNC_FASTCALL(x) x
1352 #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1353 #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1355 #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1356 #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1357 #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1359 enum vm_frame_env_flags {
1370 VM_FRAME_MAGIC_METHOD = 0x11110001,
1371 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1372 VM_FRAME_MAGIC_CLASS = 0x33330001,
1373 VM_FRAME_MAGIC_TOP = 0x44440001,
1374 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1375 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1376 VM_FRAME_MAGIC_EVAL = 0x77770001,
1377 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1378 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1380 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1383 VM_FRAME_FLAG_FINISH = 0x0020,
1384 VM_FRAME_FLAG_BMETHOD = 0x0040,
1385 VM_FRAME_FLAG_CFRAME = 0x0080,
1386 VM_FRAME_FLAG_LAMBDA = 0x0100,
1387 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1388 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1389 VM_FRAME_FLAG_PASSED = 0x0800,
1392 VM_ENV_FLAG_LOCAL = 0x0002,
1393 VM_ENV_FLAG_ESCAPED = 0x0004,
1394 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1395 VM_ENV_FLAG_ISOLATED = 0x0010,
1398 #define VM_ENV_DATA_SIZE ( 3)
1400 #define VM_ENV_DATA_INDEX_ME_CREF (-2)
1401 #define VM_ENV_DATA_INDEX_SPECVAL (-1)
1402 #define VM_ENV_DATA_INDEX_FLAGS ( 0)
1403 #define VM_ENV_DATA_INDEX_ENV ( 1)
1405 #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1407 static inline void VM_FORCE_WRITE_SPECIAL_CONST(
const VALUE *
ptr,
VALUE special_const_value);
1410 VM_ENV_FLAGS_SET(
const VALUE *ep,
VALUE flag)
1412 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1414 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1418 VM_ENV_FLAGS_UNSET(
const VALUE *ep,
VALUE flag)
1420 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1422 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1425 static inline unsigned long
1426 VM_ENV_FLAGS(
const VALUE *ep,
long flag)
1428 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1430 return flags & flag;
1433 static inline unsigned long
1436 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1442 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1448 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1454 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1460 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1464 rb_obj_is_iseq(
VALUE iseq)
1466 return imemo_type_p(iseq, imemo_iseq);
1469 #if VM_CHECK_MODE > 0
1470 #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1476 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1477 VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1478 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1485 return !VM_FRAME_CFRAME_P(cfp);
1488 #define RUBYVM_CFUNC_FRAME_P(cfp) \
1489 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1491 #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1492 #define VM_BLOCK_HANDLER_NONE 0
1495 VM_ENV_LOCAL_P(
const VALUE *ep)
1497 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1500 static inline const VALUE *
1501 VM_ENV_PREV_EP(
const VALUE *ep)
1503 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1504 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1508 VM_ENV_BLOCK_HANDLER(
const VALUE *ep)
1510 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1511 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1514 #if VM_CHECK_MODE > 0
1515 int rb_vm_ep_in_heap_p(
const VALUE *ep);
1519 VM_ENV_ESCAPED_P(
const VALUE *ep)
1521 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1522 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1527 VM_ENV_ENVVAL(const
VALUE *ep)
1529 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1530 VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1531 VM_ASSERT(envval ==
Qundef || imemo_type_p(envval, imemo_env));
1537 VM_ENV_ENVVAL_PTR(const
VALUE *ep)
1539 return (
const rb_env_t *)VM_ENV_ENVVAL(ep);
1547 env->env = env_body;
1548 env->env_size = env_size;
1549 env_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)env;
1560 VM_FORCE_WRITE_SPECIAL_CONST(
const VALUE *
ptr,
VALUE special_const_value)
1563 VM_FORCE_WRITE(
ptr, special_const_value);
1567 VM_STACK_ENV_WRITE(
const VALUE *ep,
int index,
VALUE v)
1569 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1570 VM_FORCE_WRITE(&ep[index], v);
1573 const VALUE *rb_vm_ep_local_ep(
const VALUE *ep);
1574 const VALUE *rb_vm_proc_local_ep(
VALUE proc);
1580 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1581 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1583 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1584 ((void *)(ecfp) > (void *)(cfp))
1595 return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1599 VM_BH_ISEQ_BLOCK_P(
VALUE block_handler)
1601 if ((block_handler & 0x03) == 0x01) {
1602 #if VM_CHECK_MODE > 0
1604 VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1616 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1617 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1618 return block_handler;
1622 VM_BH_TO_ISEQ_BLOCK(
VALUE block_handler)
1625 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1630 VM_BH_IFUNC_P(
VALUE block_handler)
1632 if ((block_handler & 0x03) == 0x03) {
1633 #if VM_CHECK_MODE > 0
1635 VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1647 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1648 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1649 return block_handler;
1653 VM_BH_TO_IFUNC_BLOCK(
VALUE block_handler)
1656 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1661 VM_BH_TO_CAPT_BLOCK(
VALUE block_handler)
1664 VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1668 static inline enum rb_block_handler_type
1669 vm_block_handler_type(
VALUE block_handler)
1671 if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1672 return block_handler_type_iseq;
1674 else if (VM_BH_IFUNC_P(block_handler)) {
1675 return block_handler_type_ifunc;
1677 else if (
SYMBOL_P(block_handler)) {
1678 return block_handler_type_symbol;
1682 return block_handler_type_proc;
1687 vm_block_handler_verify(MAYBE_UNUSED(
VALUE block_handler))
1689 VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1690 (vm_block_handler_type(block_handler), 1));
1693 static inline enum rb_block_type
1694 vm_block_type(
const struct rb_block *block)
1696 #if VM_CHECK_MODE > 0
1697 switch (block->type) {
1698 case block_type_iseq:
1699 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1701 case block_type_ifunc:
1702 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1704 case block_type_symbol:
1705 VM_ASSERT(
SYMBOL_P(block->as.symbol));
1707 case block_type_proc:
1716 vm_block_type_set(
const struct rb_block *block,
enum rb_block_type
type)
1722 static inline const struct rb_block *
1723 vm_proc_block(
VALUE procval)
1730 static inline const VALUE *vm_block_ep(
const struct rb_block *block);
1733 vm_proc_iseq(
VALUE procval)
1735 return vm_block_iseq(vm_proc_block(procval));
1738 static inline const VALUE *
1739 vm_proc_ep(
VALUE procval)
1741 return vm_block_ep(vm_proc_block(procval));
1745 vm_block_iseq(
const struct rb_block *block)
1747 switch (vm_block_type(block)) {
1748 case block_type_iseq:
return rb_iseq_check(block->as.captured.code.iseq);
1749 case block_type_proc:
return vm_proc_iseq(block->as.proc);
1750 case block_type_ifunc:
1751 case block_type_symbol:
return NULL;
1753 VM_UNREACHABLE(vm_block_iseq);
1757 static inline const VALUE *
1758 vm_block_ep(
const struct rb_block *block)
1760 switch (vm_block_type(block)) {
1761 case block_type_iseq:
1762 case block_type_ifunc:
return block->as.captured.ep;
1763 case block_type_proc:
return vm_proc_ep(block->as.proc);
1764 case block_type_symbol:
return NULL;
1766 VM_UNREACHABLE(vm_block_ep);
1771 vm_block_self(
const struct rb_block *block)
1773 switch (vm_block_type(block)) {
1774 case block_type_iseq:
1775 case block_type_ifunc:
1776 return block->as.captured.self;
1777 case block_type_proc:
1778 return vm_block_self(vm_proc_block(block->as.proc));
1779 case block_type_symbol:
1782 VM_UNREACHABLE(vm_block_self);
1787 VM_BH_TO_SYMBOL(
VALUE block_handler)
1789 VM_ASSERT(
SYMBOL_P(block_handler));
1790 return block_handler;
1794 VM_BH_FROM_SYMBOL(
VALUE symbol)
1801 VM_BH_TO_PROC(
VALUE block_handler)
1804 return block_handler;
1808 VM_BH_FROM_PROC(
VALUE procval)
1825 #define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp, stderr)
1826 #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp), stderr)
1827 bool rb_vm_bugreport(
const void *,
FILE *);
1828 typedef void (*ruby_sighandler_t)(int);
1830 NORETURN(
void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler,
int sig, const
void *, const
char *fmt, ...));
1833 RUBY_SYMBOL_EXPORT_BEGIN
1838 RUBY_SYMBOL_EXPORT_END
1852 return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1858 return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1865 void rb_vm_inc_const_missing_count(
void);
1871 void rb_thread_start_timer_thread(
void);
1872 void rb_thread_stop_timer_thread(
void);
1873 void rb_thread_reset_timer_thread(
void);
1874 void rb_thread_wakeup_timer_thread(
int);
1877 rb_vm_living_threads_init(
rb_vm_t *vm)
1879 ccan_list_head_init(&vm->waiting_fds);
1880 ccan_list_head_init(&vm->workqueue);
1881 ccan_list_head_init(&vm->ractor.set);
1882 ccan_list_head_init(&vm->ractor.sched.zombie_threads);
1885 typedef int rb_backtrace_iter_func(
void *,
VALUE,
int,
VALUE);
1891 void ruby_thread_init_stack(
rb_thread_t *th,
void *local_in_parent_frame);
1896 void rb_vm_env_write(
const VALUE *ep,
int index,
VALUE v);
1899 void rb_vm_register_special_exception_str(
enum ruby_special_exceptions sp,
VALUE exception_class,
VALUE mesg);
1901 #define rb_vm_register_special_exception(sp, e, m) \
1902 rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1910 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1912 #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1913 STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1914 STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1915 const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1916 if (UNLIKELY((cfp) <= &bound[1])) { \
1917 vm_stackoverflow(); \
1921 #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1922 CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1930 #if RUBY_VM_THREAD_MODEL == 2
1938 #define GET_VM() rb_current_vm()
1939 #define GET_RACTOR() rb_current_ractor()
1940 #define GET_THREAD() rb_current_thread()
1941 #define GET_EC() rb_current_execution_context(true)
1946 return ec->thread_ptr;
1954 VM_ASSERT(th->ractor != NULL);
1977 rb_current_execution_context(
bool expect_ec)
1979 #ifdef RB_THREAD_LOCAL_SPECIFIER
1998 VM_ASSERT(ec == rb_current_ec_noinline());
2002 VM_ASSERT(!expect_ec || ec != NULL);
2007 rb_current_thread(
void)
2010 return rb_ec_thread_ptr(ec);
2014 rb_current_ractor_raw(
bool expect)
2016 if (ruby_single_main_ractor) {
2017 return ruby_single_main_ractor;
2021 return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
2026 rb_current_ractor(
void)
2028 return rb_current_ractor_raw(
true);
2035 VM_ASSERT(ruby_current_vm_ptr == NULL ||
2036 ruby_current_execution_context_ptr == NULL ||
2037 rb_ec_thread_ptr(GET_EC()) == NULL ||
2038 rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
2039 rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
2042 return ruby_current_vm_ptr;
2046 unsigned int recorded_lock_rec,
2047 unsigned int current_lock_rec);
2049 static inline unsigned int
2052 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2054 if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
2058 return vm->ractor.sync.lock_rec;
2063 #error "unsupported thread model"
2067 TIMER_INTERRUPT_MASK = 0x01,
2068 PENDING_INTERRUPT_MASK = 0x02,
2069 POSTPONED_JOB_INTERRUPT_MASK = 0x04,
2070 TRAP_INTERRUPT_MASK = 0x08,
2071 TERMINATE_INTERRUPT_MASK = 0x10,
2072 VM_BARRIER_INTERRUPT_MASK = 0x20,
2075 #define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
2076 #define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
2077 #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
2078 #define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
2079 #define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
2080 #define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
2081 #define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
2082 (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
2087 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
2088 uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
2090 if (current_clock != ec->checked_clock) {
2091 ec->checked_clock = current_clock;
2092 RUBY_VM_SET_TIMER_INTERRUPT(ec);
2095 return ec->interrupt_flag & ~(ec)->interrupt_mask;
2099 int rb_signal_buff_size(
void);
2102 void rb_threadptr_signal_raise(
rb_thread_t *th,
int sig);
2104 int rb_threadptr_execute_interrupts(
rb_thread_t *,
int);
2106 void rb_threadptr_unlock_all_locking_mutexes(
rb_thread_t *th);
2107 void rb_threadptr_pending_interrupt_clear(
rb_thread_t *th);
2118 void rb_vm_cond_wait(
rb_vm_t *vm, rb_nativethread_cond_t *cond);
2119 void rb_vm_cond_timedwait(
rb_vm_t *vm, rb_nativethread_cond_t *cond,
unsigned long msec);
2121 #define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
2125 #ifdef RUBY_ASSERT_CRITICAL_SECTION
2126 VM_ASSERT(ruby_assert_critical_section_entered == 0);
2129 VM_ASSERT(ec == GET_EC());
2131 if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2132 rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2163 #define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2164 const rb_event_flag_t flag_arg_ = (flag_); \
2165 rb_hook_list_t *hooks_arg_ = (hooks_); \
2166 if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2168 rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2178 VM_ASSERT((hooks->events & flag) != 0);
2180 trace_arg.event = flag;
2182 trace_arg.cfp = ec->cfp;
2183 trace_arg.self =
self;
2185 trace_arg.called_id = called_id;
2186 trace_arg.klass = klass;
2187 trace_arg.data = data;
2189 trace_arg.klass_solved = 0;
2191 rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2204 return &cr_pub->hooks;
2207 #define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2208 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2210 #define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2211 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2221 void rb_vm_trap_exit(
rb_vm_t *vm);
2222 void rb_vm_postponed_job_atfork(
void);
2223 void rb_vm_postponed_job_free(
void);
2224 size_t rb_vm_memsize_postponed_job_queue(
void);
2225 void rb_vm_postponed_job_queue_init(
rb_vm_t *vm);
2227 RUBY_SYMBOL_EXPORT_BEGIN
2229 int rb_thread_check_trap_pending(
void);
2232 #define RUBY_EVENT_COVERAGE_LINE 0x010000
2233 #define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2235 extern VALUE rb_get_coverages(
void);
2236 extern void rb_set_coverages(
VALUE,
int,
VALUE);
2237 extern void rb_clear_coverages(
void);
2238 extern void rb_reset_coverages(
void);
2239 extern void rb_resume_coverages(
void);
2240 extern void rb_suspend_coverages(
void);
2242 void rb_postponed_job_flush(
rb_vm_t *vm);
2248 RUBY_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.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
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.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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::@154 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.
void * ruby_xmalloc(size_t size)
Allocates a storage instance.
void ruby_xfree(void *ptr)
Deallocates a storage instance.