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;
809 #ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
810 #define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
812 const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE];
814 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
820 size_t thread_vm_stack_size;
821 size_t thread_machine_stack_size;
822 size_t fiber_vm_stack_size;
823 size_t fiber_machine_stack_size;
830 #define RUBY_VM_SIZE_ALIGN 4096
832 #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE))
833 #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE))
834 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE))
835 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE))
837 #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE))
838 #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE))
839 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE))
840 #if defined(__powerpc64__) || defined(__ppc64__)
841 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE))
843 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE))
846 #if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
848 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
849 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
850 #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
851 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
852 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
853 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
854 #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
855 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
858 #ifndef VM_DEBUG_BP_CHECK
859 #define VM_DEBUG_BP_CHECK 0
862 #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
863 #define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
876 enum rb_block_handler_type {
877 block_handler_type_iseq,
878 block_handler_type_ifunc,
879 block_handler_type_symbol,
880 block_handler_type_proc
896 enum rb_block_type
type;
905 const void *block_code;
907 #if VM_DEBUG_BP_CHECK
915 rb_thread_ptr(
VALUE thval)
920 enum rb_thread_status {
923 THREAD_STOPPED_FOREVER,
928 typedef RUBY_JMP_BUF rb_jmpbuf_t;
930 typedef void *rb_jmpbuf_t[5];
945 #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
955 typedef rb_jmpbuf_t *rb_vm_tag_jmpbuf_t;
957 #define RB_VM_TAG_JMPBUF_GET(buf) (*buf)
960 rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
966 rb_vm_tag_jmpbuf_deinit(
const rb_vm_tag_jmpbuf_t *jmpbuf)
971 typedef rb_jmpbuf_t rb_vm_tag_jmpbuf_t;
973 #define RB_VM_TAG_JMPBUF_GET(buf) (buf)
976 rb_vm_tag_jmpbuf_init(rb_vm_tag_jmpbuf_t *jmpbuf)
982 rb_vm_tag_jmpbuf_deinit(
const rb_vm_tag_jmpbuf_t *jmpbuf)
995 rb_vm_tag_jmpbuf_t buf;
997 enum ruby_tag_type state;
998 unsigned int lock_rec;
1001 STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(
struct rb_vm_tag, buf) > 0);
1002 STATIC_ASSERT(rb_vm_tag_buf_end,
1003 offsetof(
struct rb_vm_tag, buf) +
sizeof(rb_vm_tag_jmpbuf_t) <
1024 size_t vm_stack_size;
1032 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1033 uint32_t checked_clock;
1041 VALUE local_storage_recursive_hash;
1042 VALUE local_storage_recursive_hash_for_trace;
1048 const VALUE *root_lep;
1056 VALUE passed_block_handler;
1058 uint8_t raised_flag;
1061 BITFIELD(
enum method_missing_reason, method_missing_reason, 8);
1063 VALUE private_const_reference;
1069 size_t stack_maxsize;
1072 #ifdef RUBY_ASAN_ENABLED
1073 void *asan_fake_stack_handle;
1078 #ifndef rb_execution_context_t
1080 #define rb_execution_context_t rb_execution_context_t
1084 #define VM_CORE_H_EC_DEFINED 1
1108 struct ccan_list_node lt_node;
1116 bool mn_schedulable;
1130 BITFIELD(
enum rb_thread_status, status, 2);
1132 unsigned int has_dedicated_nt : 1;
1133 unsigned int to_kill : 1;
1134 unsigned int abort_on_exception: 1;
1135 unsigned int report_on_exception: 1;
1136 unsigned int pending_interrupt_queue_checked: 1;
1138 uint32_t running_time_us;
1140 void *blocking_region_buffer;
1146 #if OPT_CALL_THREADED_CODE
1151 VALUE pending_interrupt_queue;
1152 VALUE pending_interrupt_mask_stack;
1155 rb_nativethread_lock_t interrupt_lock;
1157 VALUE locking_mutex;
1159 struct ccan_list_head interrupt_exec_tasks;
1170 VALUE (*func)(
void *);
1175 enum thread_invoke_type {
1176 thread_invoke_type_none = 0,
1177 thread_invoke_type_proc,
1178 thread_invoke_type_ractor_proc,
1179 thread_invoke_type_func
1183 VALUE stat_insn_usage;
1189 unsigned int blocking;
1193 void **specific_storage;
1198 static inline unsigned int
1201 return th ? (
unsigned int)th->serial : 0;
1205 VM_DEFINECLASS_TYPE_CLASS = 0x00,
1206 VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1207 VM_DEFINECLASS_TYPE_MODULE = 0x02,
1209 VM_DEFINECLASS_TYPE_MASK = 0x07
1210 } rb_vm_defineclass_type_t;
1212 #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1213 #define VM_DEFINECLASS_FLAG_SCOPED 0x08
1214 #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1215 #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1216 #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1217 ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1220 RUBY_SYMBOL_EXPORT_BEGIN
1229 VALUE script_lines);
1239 rb_iseq_new_with_callback_new_callback(
1262 RUBY_SYMBOL_EXPORT_END
1264 #define GetProcPtr(obj, ptr) \
1265 GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1269 unsigned int is_from_method: 1;
1270 unsigned int is_lambda: 1;
1271 unsigned int is_isolated: 1;
1274 RUBY_SYMBOL_EXPORT_BEGIN
1277 VALUE rb_proc_ractor_make_shareable(
VALUE self);
1278 RUBY_SYMBOL_EXPORT_END
1285 unsigned int env_size;
1290 #define GetBindingPtr(obj, ptr) \
1291 GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1295 const VALUE pathobj;
1301 enum vm_check_match_type {
1302 VM_CHECKMATCH_TYPE_WHEN = 1,
1303 VM_CHECKMATCH_TYPE_CASE = 2,
1304 VM_CHECKMATCH_TYPE_RESCUE = 3
1307 #define VM_CHECKMATCH_TYPE_MASK 0x03
1308 #define VM_CHECKMATCH_ARRAY 0x04
1310 enum vm_opt_newarray_send_type {
1311 VM_OPT_NEWARRAY_SEND_MAX = 1,
1312 VM_OPT_NEWARRAY_SEND_MIN = 2,
1313 VM_OPT_NEWARRAY_SEND_HASH = 3,
1314 VM_OPT_NEWARRAY_SEND_PACK = 4,
1315 VM_OPT_NEWARRAY_SEND_PACK_BUFFER = 5,
1318 enum vm_special_object_type {
1319 VM_SPECIAL_OBJECT_VMCORE = 1,
1320 VM_SPECIAL_OBJECT_CBASE,
1321 VM_SPECIAL_OBJECT_CONST_BASE
1324 enum vm_svar_index {
1325 VM_SVAR_LASTLINE = 0,
1326 VM_SVAR_BACKREF = 1,
1328 VM_SVAR_EXTRA_START = 2,
1329 VM_SVAR_FLIPFLOP_START = 2
1341 typedef VALUE CDHASH;
1343 #ifndef FUNC_FASTCALL
1344 #define FUNC_FASTCALL(x) x
1350 #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1351 #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1353 #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1354 #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1355 #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1357 enum vm_frame_env_flags {
1368 VM_FRAME_MAGIC_METHOD = 0x11110001,
1369 VM_FRAME_MAGIC_BLOCK = 0x22220001,
1370 VM_FRAME_MAGIC_CLASS = 0x33330001,
1371 VM_FRAME_MAGIC_TOP = 0x44440001,
1372 VM_FRAME_MAGIC_CFUNC = 0x55550001,
1373 VM_FRAME_MAGIC_IFUNC = 0x66660001,
1374 VM_FRAME_MAGIC_EVAL = 0x77770001,
1375 VM_FRAME_MAGIC_RESCUE = 0x78880001,
1376 VM_FRAME_MAGIC_DUMMY = 0x79990001,
1378 VM_FRAME_MAGIC_MASK = 0x7fff0001,
1381 VM_FRAME_FLAG_FINISH = 0x0020,
1382 VM_FRAME_FLAG_BMETHOD = 0x0040,
1383 VM_FRAME_FLAG_CFRAME = 0x0080,
1384 VM_FRAME_FLAG_LAMBDA = 0x0100,
1385 VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1386 VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1387 VM_FRAME_FLAG_PASSED = 0x0800,
1390 VM_ENV_FLAG_LOCAL = 0x0002,
1391 VM_ENV_FLAG_ESCAPED = 0x0004,
1392 VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1393 VM_ENV_FLAG_ISOLATED = 0x0010,
1396 #define VM_ENV_DATA_SIZE ( 3)
1398 #define VM_ENV_DATA_INDEX_ME_CREF (-2)
1399 #define VM_ENV_DATA_INDEX_SPECVAL (-1)
1400 #define VM_ENV_DATA_INDEX_FLAGS ( 0)
1401 #define VM_ENV_DATA_INDEX_ENV ( 1)
1403 #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1405 static inline void VM_FORCE_WRITE_SPECIAL_CONST(
const VALUE *
ptr,
VALUE special_const_value);
1408 VM_ENV_FLAGS_SET(
const VALUE *ep,
VALUE flag)
1410 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1412 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1416 VM_ENV_FLAGS_UNSET(
const VALUE *ep,
VALUE flag)
1418 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1420 VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1423 static inline unsigned long
1424 VM_ENV_FLAGS(
const VALUE *ep,
long flag)
1426 VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1428 return flags & flag;
1431 static inline unsigned long
1434 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1440 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1446 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1452 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1458 return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1462 rb_obj_is_iseq(
VALUE iseq)
1464 return imemo_type_p(iseq, imemo_iseq);
1467 #if VM_CHECK_MODE > 0
1468 #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1474 int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1475 VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1476 (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1483 return !VM_FRAME_CFRAME_P(cfp);
1486 #define RUBYVM_CFUNC_FRAME_P(cfp) \
1487 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1489 #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1490 #define VM_BLOCK_HANDLER_NONE 0
1493 VM_ENV_LOCAL_P(
const VALUE *ep)
1495 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1498 static inline const VALUE *
1499 VM_ENV_PREV_EP(
const VALUE *ep)
1501 VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1502 return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1506 VM_ENV_BLOCK_HANDLER(
const VALUE *ep)
1508 VM_ASSERT(VM_ENV_LOCAL_P(ep));
1509 return ep[VM_ENV_DATA_INDEX_SPECVAL];
1512 #if VM_CHECK_MODE > 0
1513 int rb_vm_ep_in_heap_p(
const VALUE *ep);
1517 VM_ENV_ESCAPED_P(
const VALUE *ep)
1519 VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1520 return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1525 VM_ENV_ENVVAL(const
VALUE *ep)
1527 VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1528 VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1529 VM_ASSERT(envval ==
Qundef || imemo_type_p(envval, imemo_env));
1535 VM_ENV_ENVVAL_PTR(const
VALUE *ep)
1537 return (
const rb_env_t *)VM_ENV_ENVVAL(ep);
1545 env->env = env_body;
1546 env->env_size = env_size;
1547 env_ep[VM_ENV_DATA_INDEX_ENV] = (
VALUE)env;
1558 VM_FORCE_WRITE_SPECIAL_CONST(
const VALUE *
ptr,
VALUE special_const_value)
1561 VM_FORCE_WRITE(
ptr, special_const_value);
1565 VM_STACK_ENV_WRITE(
const VALUE *ep,
int index,
VALUE v)
1567 VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1568 VM_FORCE_WRITE(&ep[index], v);
1571 const VALUE *rb_vm_ep_local_ep(
const VALUE *ep);
1572 const VALUE *rb_vm_proc_local_ep(
VALUE proc);
1578 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1579 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1581 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1582 ((void *)(ecfp) > (void *)(cfp))
1593 return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1597 VM_BH_ISEQ_BLOCK_P(
VALUE block_handler)
1599 if ((block_handler & 0x03) == 0x01) {
1600 #if VM_CHECK_MODE > 0
1602 VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1614 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1615 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1616 return block_handler;
1620 VM_BH_TO_ISEQ_BLOCK(
VALUE block_handler)
1623 VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1628 VM_BH_IFUNC_P(
VALUE block_handler)
1630 if ((block_handler & 0x03) == 0x03) {
1631 #if VM_CHECK_MODE > 0
1633 VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1645 VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1646 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1647 return block_handler;
1651 VM_BH_TO_IFUNC_BLOCK(
VALUE block_handler)
1654 VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1659 VM_BH_TO_CAPT_BLOCK(
VALUE block_handler)
1662 VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1666 static inline enum rb_block_handler_type
1667 vm_block_handler_type(
VALUE block_handler)
1669 if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1670 return block_handler_type_iseq;
1672 else if (VM_BH_IFUNC_P(block_handler)) {
1673 return block_handler_type_ifunc;
1675 else if (
SYMBOL_P(block_handler)) {
1676 return block_handler_type_symbol;
1680 return block_handler_type_proc;
1685 vm_block_handler_verify(MAYBE_UNUSED(
VALUE block_handler))
1687 VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1688 (vm_block_handler_type(block_handler), 1));
1691 static inline enum rb_block_type
1692 vm_block_type(
const struct rb_block *block)
1694 #if VM_CHECK_MODE > 0
1695 switch (block->type) {
1696 case block_type_iseq:
1697 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1699 case block_type_ifunc:
1700 VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1702 case block_type_symbol:
1703 VM_ASSERT(
SYMBOL_P(block->as.symbol));
1705 case block_type_proc:
1714 vm_block_type_set(
const struct rb_block *block,
enum rb_block_type
type)
1720 static inline const struct rb_block *
1721 vm_proc_block(
VALUE procval)
1728 static inline const VALUE *vm_block_ep(
const struct rb_block *block);
1731 vm_proc_iseq(
VALUE procval)
1733 return vm_block_iseq(vm_proc_block(procval));
1736 static inline const VALUE *
1737 vm_proc_ep(
VALUE procval)
1739 return vm_block_ep(vm_proc_block(procval));
1743 vm_block_iseq(
const struct rb_block *block)
1745 switch (vm_block_type(block)) {
1746 case block_type_iseq:
return rb_iseq_check(block->as.captured.code.iseq);
1747 case block_type_proc:
return vm_proc_iseq(block->as.proc);
1748 case block_type_ifunc:
1749 case block_type_symbol:
return NULL;
1751 VM_UNREACHABLE(vm_block_iseq);
1755 static inline const VALUE *
1756 vm_block_ep(
const struct rb_block *block)
1758 switch (vm_block_type(block)) {
1759 case block_type_iseq:
1760 case block_type_ifunc:
return block->as.captured.ep;
1761 case block_type_proc:
return vm_proc_ep(block->as.proc);
1762 case block_type_symbol:
return NULL;
1764 VM_UNREACHABLE(vm_block_ep);
1769 vm_block_self(
const struct rb_block *block)
1771 switch (vm_block_type(block)) {
1772 case block_type_iseq:
1773 case block_type_ifunc:
1774 return block->as.captured.self;
1775 case block_type_proc:
1776 return vm_block_self(vm_proc_block(block->as.proc));
1777 case block_type_symbol:
1780 VM_UNREACHABLE(vm_block_self);
1785 VM_BH_TO_SYMBOL(
VALUE block_handler)
1787 VM_ASSERT(
SYMBOL_P(block_handler));
1788 return block_handler;
1792 VM_BH_FROM_SYMBOL(
VALUE symbol)
1799 VM_BH_TO_PROC(
VALUE block_handler)
1802 return block_handler;
1806 VM_BH_FROM_PROC(
VALUE procval)
1823 #define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp, stderr)
1824 #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp), stderr)
1825 bool rb_vm_bugreport(
const void *,
FILE *);
1826 typedef void (*ruby_sighandler_t)(int);
1828 NORETURN(
void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler,
int sig, const
void *, const
char *fmt, ...));
1831 RUBY_SYMBOL_EXPORT_BEGIN
1836 RUBY_SYMBOL_EXPORT_END
1850 return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1856 return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1863 void rb_vm_inc_const_missing_count(
void);
1869 void rb_thread_start_timer_thread(
void);
1870 void rb_thread_stop_timer_thread(
void);
1871 void rb_thread_reset_timer_thread(
void);
1872 void rb_thread_wakeup_timer_thread(
int);
1875 rb_vm_living_threads_init(
rb_vm_t *vm)
1877 ccan_list_head_init(&vm->waiting_fds);
1878 ccan_list_head_init(&vm->workqueue);
1879 ccan_list_head_init(&vm->ractor.set);
1880 ccan_list_head_init(&vm->ractor.sched.zombie_threads);
1883 typedef int rb_backtrace_iter_func(
void *,
VALUE,
int,
VALUE);
1889 void ruby_thread_init_stack(
rb_thread_t *th,
void *local_in_parent_frame);
1894 void rb_vm_env_write(
const VALUE *ep,
int index,
VALUE v);
1897 void rb_vm_register_special_exception_str(
enum ruby_special_exceptions sp,
VALUE exception_class,
VALUE mesg);
1899 #define rb_vm_register_special_exception(sp, e, m) \
1900 rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1908 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1910 #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1911 STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1912 STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1913 const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1914 if (UNLIKELY((cfp) <= &bound[1])) { \
1915 vm_stackoverflow(); \
1919 #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1920 CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1928 #if RUBY_VM_THREAD_MODEL == 2
1936 #define GET_VM() rb_current_vm()
1937 #define GET_RACTOR() rb_current_ractor()
1938 #define GET_THREAD() rb_current_thread()
1939 #define GET_EC() rb_current_execution_context(true)
1944 return ec->thread_ptr;
1952 VM_ASSERT(th->ractor != NULL);
1975 rb_current_execution_context(
bool expect_ec)
1977 #ifdef RB_THREAD_LOCAL_SPECIFIER
1996 VM_ASSERT(ec == rb_current_ec_noinline());
2000 VM_ASSERT(!expect_ec || ec != NULL);
2005 rb_current_thread(
void)
2008 return rb_ec_thread_ptr(ec);
2012 rb_current_ractor_raw(
bool expect)
2014 if (ruby_single_main_ractor) {
2015 return ruby_single_main_ractor;
2019 return (expect || ec) ? rb_ec_ractor_ptr(ec) : NULL;
2024 rb_current_ractor(
void)
2026 return rb_current_ractor_raw(
true);
2033 VM_ASSERT(ruby_current_vm_ptr == NULL ||
2034 ruby_current_execution_context_ptr == NULL ||
2035 rb_ec_thread_ptr(GET_EC()) == NULL ||
2036 rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
2037 rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
2040 return ruby_current_vm_ptr;
2044 unsigned int recorded_lock_rec,
2045 unsigned int current_lock_rec);
2047 static inline unsigned int
2050 rb_vm_t *vm = rb_ec_vm_ptr(ec);
2052 if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
2056 return vm->ractor.sync.lock_rec;
2061 #error "unsupported thread model"
2065 TIMER_INTERRUPT_MASK = 0x01,
2066 PENDING_INTERRUPT_MASK = 0x02,
2067 POSTPONED_JOB_INTERRUPT_MASK = 0x04,
2068 TRAP_INTERRUPT_MASK = 0x08,
2069 TERMINATE_INTERRUPT_MASK = 0x10,
2070 VM_BARRIER_INTERRUPT_MASK = 0x20,
2073 #define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
2074 #define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
2075 #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
2076 #define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
2077 #define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
2078 #define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
2079 #define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
2080 (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
2085 #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
2086 uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
2088 if (current_clock != ec->checked_clock) {
2089 ec->checked_clock = current_clock;
2090 RUBY_VM_SET_TIMER_INTERRUPT(ec);
2093 return ec->interrupt_flag & ~(ec)->interrupt_mask;
2097 int rb_signal_buff_size(
void);
2100 void rb_threadptr_signal_raise(
rb_thread_t *th,
int sig);
2102 int rb_threadptr_execute_interrupts(
rb_thread_t *,
int);
2104 void rb_threadptr_unlock_all_locking_mutexes(
rb_thread_t *th);
2105 void rb_threadptr_pending_interrupt_clear(
rb_thread_t *th);
2116 void rb_vm_cond_wait(
rb_vm_t *vm, rb_nativethread_cond_t *cond);
2117 void rb_vm_cond_timedwait(
rb_vm_t *vm, rb_nativethread_cond_t *cond,
unsigned long msec);
2119 #define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
2123 #ifdef RUBY_ASSERT_CRITICAL_SECTION
2124 VM_ASSERT(ruby_assert_critical_section_entered == 0);
2127 VM_ASSERT(ec == GET_EC());
2129 if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
2130 rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
2161 #define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2162 const rb_event_flag_t flag_arg_ = (flag_); \
2163 rb_hook_list_t *hooks_arg_ = (hooks_); \
2164 if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2166 rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2176 VM_ASSERT((hooks->events & flag) != 0);
2178 trace_arg.event = flag;
2180 trace_arg.cfp = ec->cfp;
2181 trace_arg.self =
self;
2183 trace_arg.called_id = called_id;
2184 trace_arg.klass = klass;
2185 trace_arg.data = data;
2187 trace_arg.klass_solved = 0;
2189 rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2202 return &cr_pub->hooks;
2205 #define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2206 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2208 #define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2209 EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2219 void rb_vm_trap_exit(
rb_vm_t *vm);
2220 void rb_vm_postponed_job_atfork(
void);
2221 void rb_vm_postponed_job_free(
void);
2222 size_t rb_vm_memsize_postponed_job_queue(
void);
2223 void rb_vm_postponed_job_queue_init(
rb_vm_t *vm);
2225 RUBY_SYMBOL_EXPORT_BEGIN
2227 int rb_thread_check_trap_pending(
void);
2230 #define RUBY_EVENT_COVERAGE_LINE 0x010000
2231 #define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2233 extern VALUE rb_get_coverages(
void);
2234 extern void rb_set_coverages(
VALUE,
int,
VALUE);
2235 extern void rb_clear_coverages(
void);
2236 extern void rb_reset_coverages(
void);
2237 extern void rb_resume_coverages(
void);
2238 extern void rb_suspend_coverages(
void);
2240 void rb_postponed_job_flush(
rb_vm_t *vm);
2246 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::@150 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.