datadog-ruby_core_source 3.5.4 → 3.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/datadog-ruby_core_source.gemspec +1 -1
  3. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ccan/build_assert/build_assert.h +40 -0
  4. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ccan/check_type/check_type.h +63 -0
  5. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ccan/container_of/container_of.h +142 -0
  6. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ccan/list/list.h +788 -0
  7. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ccan/str/str.h +16 -0
  8. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/id.h +252 -0
  9. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/internal.h +2465 -0
  10. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/iseq.h +304 -0
  11. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/method.h +229 -0
  12. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/node.h +449 -0
  13. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ruby_assert.h +60 -0
  14. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/ruby_atomic.h +244 -0
  15. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/thread_pthread.h +75 -0
  16. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/thread_win32.h +36 -0
  17. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/vm_core.h +1902 -0
  18. data/lib/datadog/ruby_core_source/ruby-2.6.0-p0/vm_opts.h +70 -0
  19. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ccan/build_assert/build_assert.h +40 -0
  20. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ccan/check_type/check_type.h +63 -0
  21. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ccan/container_of/container_of.h +142 -0
  22. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ccan/list/list.h +788 -0
  23. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ccan/str/str.h +16 -0
  24. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/id.h +290 -0
  25. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/internal.h +2697 -0
  26. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/iseq.h +312 -0
  27. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/method.h +229 -0
  28. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/node.h +476 -0
  29. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ruby_assert.h +15 -0
  30. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/ruby_atomic.h +244 -0
  31. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/thread_pthread.h +75 -0
  32. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/thread_win32.h +36 -0
  33. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/vm_core.h +1968 -0
  34. data/lib/datadog/ruby_core_source/ruby-2.7.0-p0/vm_opts.h +72 -0
  35. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ccan/build_assert/build_assert.h +40 -0
  36. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ccan/check_type/check_type.h +63 -0
  37. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ccan/container_of/container_of.h +142 -0
  38. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ccan/list/list.h +788 -0
  39. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ccan/str/str.h +16 -0
  40. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/id.h +290 -0
  41. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/id_table.h +36 -0
  42. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/array.h +119 -0
  43. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/class.h +174 -0
  44. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/compilers.h +108 -0
  45. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/gc.h +161 -0
  46. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/imemo.h +243 -0
  47. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/serial.h +24 -0
  48. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/static_assert.h +17 -0
  49. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/vm.h +136 -0
  50. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal/warnings.h +17 -0
  51. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/internal.h +107 -0
  52. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/iseq.h +305 -0
  53. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/method.h +245 -0
  54. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/node.h +484 -0
  55. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ractor_core.h +343 -0
  56. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ruby_assert.h +15 -0
  57. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/ruby_atomic.h +23 -0
  58. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/thread_pthread.h +115 -0
  59. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/thread_win32.h +61 -0
  60. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/vm_core.h +2023 -0
  61. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/vm_debug.h +116 -0
  62. data/lib/datadog/ruby_core_source/ruby-3.0.0-p0/vm_opts.h +73 -0
  63. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ccan/build_assert/build_assert.h +40 -0
  64. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ccan/check_type/check_type.h +63 -0
  65. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ccan/container_of/container_of.h +142 -0
  66. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ccan/list/list.h +788 -0
  67. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ccan/str/str.h +16 -0
  68. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/darray.h +198 -0
  69. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/id.h +293 -0
  70. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/id_table.h +36 -0
  71. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/array.h +113 -0
  72. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/class.h +196 -0
  73. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/compilers.h +107 -0
  74. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/gc.h +186 -0
  75. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/imemo.h +243 -0
  76. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/serial.h +23 -0
  77. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/static_assert.h +16 -0
  78. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/vm.h +133 -0
  79. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal/warnings.h +16 -0
  80. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/internal.h +109 -0
  81. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/iseq.h +322 -0
  82. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/method.h +253 -0
  83. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/node.h +510 -0
  84. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ractor_core.h +346 -0
  85. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ruby_assert.h +14 -0
  86. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/ruby_atomic.h +23 -0
  87. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/thread_pthread.h +115 -0
  88. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/vm_core.h +2107 -0
  89. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/vm_debug.h +124 -0
  90. data/lib/datadog/ruby_core_source/ruby-3.1.0-p0/vm_opts.h +73 -0
  91. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ccan/build_assert/build_assert.h +40 -0
  92. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ccan/check_type/check_type.h +63 -0
  93. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ccan/container_of/container_of.h +142 -0
  94. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ccan/list/list.h +789 -0
  95. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ccan/str/str.h +17 -0
  96. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/id.h +299 -0
  97. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/id_table.h +39 -0
  98. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/array.h +163 -0
  99. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/basic_operators.h +64 -0
  100. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/class.h +182 -0
  101. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/compilers.h +107 -0
  102. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/gc.h +192 -0
  103. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/imemo.h +242 -0
  104. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/serial.h +23 -0
  105. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/static_assert.h +16 -0
  106. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/vm.h +134 -0
  107. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal/warnings.h +16 -0
  108. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/internal.h +113 -0
  109. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/iseq.h +329 -0
  110. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/method.h +254 -0
  111. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/node.h +514 -0
  112. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ractor_core.h +342 -0
  113. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ruby_assert.h +14 -0
  114. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/ruby_atomic.h +23 -0
  115. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/shape.h +232 -0
  116. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/thread_none.h +20 -0
  117. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/thread_pthread.h +133 -0
  118. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/vm_core.h +2106 -0
  119. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/vm_debug.h +122 -0
  120. data/lib/datadog/ruby_core_source/ruby-3.2.0-p0/vm_opts.h +73 -0
  121. data/lib/datadog/ruby_core_source/version.rb +1 -1
  122. metadata +122 -4
@@ -0,0 +1,2106 @@
1
+ #ifndef RUBY_VM_CORE_H
2
+ #define RUBY_VM_CORE_H
3
+ /**********************************************************************
4
+
5
+ vm_core.h -
6
+
7
+ $Author$
8
+ created at: 04/01/01 19:41:38 JST
9
+
10
+ Copyright (C) 2004-2007 Koichi Sasada
11
+
12
+ **********************************************************************/
13
+
14
+ /*
15
+ * Enable check mode.
16
+ * 1: enable local assertions.
17
+ */
18
+ #ifndef VM_CHECK_MODE
19
+
20
+ // respect RUBY_DUBUG: if given n is 0, then use RUBY_DEBUG
21
+ #define N_OR_RUBY_DEBUG(n) (((n) > 0) ? (n) : RUBY_DEBUG)
22
+
23
+ #define VM_CHECK_MODE N_OR_RUBY_DEBUG(0)
24
+ #endif
25
+
26
+ /**
27
+ * VM Debug Level
28
+ *
29
+ * debug level:
30
+ * 0: no debug output
31
+ * 1: show instruction name
32
+ * 2: show stack frame when control stack frame is changed
33
+ * 3: show stack status
34
+ * 4: show register
35
+ * 5:
36
+ * 10: gc check
37
+ */
38
+
39
+ #ifndef VMDEBUG
40
+ #define VMDEBUG 0
41
+ #endif
42
+
43
+ #if 0
44
+ #undef VMDEBUG
45
+ #define VMDEBUG 3
46
+ #endif
47
+
48
+ #include "ruby/internal/config.h"
49
+
50
+ #include <stddef.h>
51
+ #include <signal.h>
52
+ #include <stdarg.h>
53
+
54
+ #include "ruby_assert.h"
55
+
56
+ #if VM_CHECK_MODE > 0
57
+ #define VM_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr)
58
+ #define VM_UNREACHABLE(func) rb_bug(#func ": unreachable")
59
+ #define RUBY_ASSERT_CRITICAL_SECTION
60
+ #define RUBY_DEBUG_THREAD_SCHEDULE() rb_thread_schedule()
61
+ #else
62
+ #define VM_ASSERT(expr) ((void)0)
63
+ #define VM_UNREACHABLE(func) UNREACHABLE
64
+ #define RUBY_DEBUG_THREAD_SCHEDULE()
65
+ #endif
66
+
67
+ #define RUBY_ASSERT_MUTEX_OWNED(mutex) VM_ASSERT(rb_mutex_owned_p(mutex))
68
+
69
+ #if defined(RUBY_ASSERT_CRITICAL_SECTION)
70
+ // TODO add documentation
71
+ extern int ruby_assert_critical_section_entered;
72
+ #define RUBY_ASSERT_CRITICAL_SECTION_ENTER() do{ruby_assert_critical_section_entered += 1;}while(false)
73
+ #define RUBY_ASSERT_CRITICAL_SECTION_LEAVE() do{VM_ASSERT(ruby_assert_critical_section_entered > 0);ruby_assert_critical_section_entered -= 1;}while(false)
74
+ #else
75
+ #define RUBY_ASSERT_CRITICAL_SECTION_ENTER()
76
+ #define RUBY_ASSERT_CRITICAL_SECTION_LEAVE()
77
+ #endif
78
+
79
+ #if defined(__wasm__) && !defined(__EMSCRIPTEN__)
80
+ # include "wasm/setjmp.h"
81
+ #else
82
+ # include <setjmp.h>
83
+ #endif
84
+
85
+ #if defined(__linux__) || defined(__FreeBSD__)
86
+ # define RB_THREAD_T_HAS_NATIVE_ID
87
+ #endif
88
+
89
+ #include "ruby/internal/stdbool.h"
90
+ #include "ccan/list/list.h"
91
+ #include "id.h"
92
+ #include "internal.h"
93
+ #include "internal/array.h"
94
+ #include "internal/basic_operators.h"
95
+ #include "internal/serial.h"
96
+ #include "internal/vm.h"
97
+ #include "method.h"
98
+ #include "node.h"
99
+ #include "ruby/ruby.h"
100
+ #include "ruby/st.h"
101
+ #include "ruby_atomic.h"
102
+ #include "vm_opts.h"
103
+ #include "shape.h"
104
+
105
+ #include "ruby/thread_native.h"
106
+
107
+ /*
108
+ * implementation selector of get_insn_info algorithm
109
+ * 0: linear search
110
+ * 1: binary search
111
+ * 2: succinct bitvector
112
+ */
113
+ #ifndef VM_INSN_INFO_TABLE_IMPL
114
+ # define VM_INSN_INFO_TABLE_IMPL 2
115
+ #endif
116
+
117
+ #if defined(NSIG_MAX) /* POSIX issue 8 */
118
+ # undef NSIG
119
+ # define NSIG NSIG_MAX
120
+ #elif defined(_SIG_MAXSIG) /* FreeBSD */
121
+ # undef NSIG
122
+ # define NSIG _SIG_MAXSIG
123
+ #elif defined(_SIGMAX) /* QNX */
124
+ # define NSIG (_SIGMAX + 1)
125
+ #elif defined(NSIG) /* 99% of everything else */
126
+ # /* take it */
127
+ #else /* Last resort */
128
+ # define NSIG (sizeof(sigset_t) * CHAR_BIT + 1)
129
+ #endif
130
+
131
+ #define RUBY_NSIG NSIG
132
+
133
+ #if defined(SIGCLD)
134
+ # define RUBY_SIGCHLD (SIGCLD)
135
+ #elif defined(SIGCHLD)
136
+ # define RUBY_SIGCHLD (SIGCHLD)
137
+ #else
138
+ # define RUBY_SIGCHLD (0)
139
+ #endif
140
+
141
+ /* platforms with broken or non-existent SIGCHLD work by polling */
142
+ #if defined(__APPLE__)
143
+ # define SIGCHLD_LOSSY (1)
144
+ #else
145
+ # define SIGCHLD_LOSSY (0)
146
+ #endif
147
+
148
+ /* define to 0 to test old code path */
149
+ #define WAITPID_USE_SIGCHLD (RUBY_SIGCHLD || SIGCHLD_LOSSY)
150
+
151
+ #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
152
+ # define USE_SIGALTSTACK
153
+ void *rb_allocate_sigaltstack(void);
154
+ void *rb_register_sigaltstack(void *);
155
+ # define RB_ALTSTACK_INIT(var, altstack) var = rb_register_sigaltstack(altstack)
156
+ # define RB_ALTSTACK_FREE(var) free(var)
157
+ # define RB_ALTSTACK(var) var
158
+ #else /* noop */
159
+ # define RB_ALTSTACK_INIT(var, altstack)
160
+ # define RB_ALTSTACK_FREE(var)
161
+ # define RB_ALTSTACK(var) (0)
162
+ #endif
163
+
164
+ #include THREAD_IMPL_H
165
+ #define RUBY_VM_THREAD_MODEL 2
166
+
167
+ /*****************/
168
+ /* configuration */
169
+ /*****************/
170
+
171
+ /* gcc ver. check */
172
+ #if defined(__GNUC__) && __GNUC__ >= 2
173
+
174
+ #if OPT_TOKEN_THREADED_CODE
175
+ #if OPT_DIRECT_THREADED_CODE
176
+ #undef OPT_DIRECT_THREADED_CODE
177
+ #endif
178
+ #endif
179
+
180
+ #else /* defined(__GNUC__) && __GNUC__ >= 2 */
181
+
182
+ /* disable threaded code options */
183
+ #if OPT_DIRECT_THREADED_CODE
184
+ #undef OPT_DIRECT_THREADED_CODE
185
+ #endif
186
+ #if OPT_TOKEN_THREADED_CODE
187
+ #undef OPT_TOKEN_THREADED_CODE
188
+ #endif
189
+ #endif
190
+
191
+ /* call threaded code */
192
+ #if OPT_CALL_THREADED_CODE
193
+ #if OPT_DIRECT_THREADED_CODE
194
+ #undef OPT_DIRECT_THREADED_CODE
195
+ #endif /* OPT_DIRECT_THREADED_CODE */
196
+ #if OPT_STACK_CACHING
197
+ #undef OPT_STACK_CACHING
198
+ #endif /* OPT_STACK_CACHING */
199
+ #endif /* OPT_CALL_THREADED_CODE */
200
+
201
+ void rb_vm_encoded_insn_data_table_init(void);
202
+ typedef unsigned long rb_num_t;
203
+ typedef signed long rb_snum_t;
204
+
205
+ enum ruby_tag_type {
206
+ RUBY_TAG_NONE = 0x0,
207
+ RUBY_TAG_RETURN = 0x1,
208
+ RUBY_TAG_BREAK = 0x2,
209
+ RUBY_TAG_NEXT = 0x3,
210
+ RUBY_TAG_RETRY = 0x4,
211
+ RUBY_TAG_REDO = 0x5,
212
+ RUBY_TAG_RAISE = 0x6,
213
+ RUBY_TAG_THROW = 0x7,
214
+ RUBY_TAG_FATAL = 0x8,
215
+ RUBY_TAG_MASK = 0xf
216
+ };
217
+
218
+ #define TAG_NONE RUBY_TAG_NONE
219
+ #define TAG_RETURN RUBY_TAG_RETURN
220
+ #define TAG_BREAK RUBY_TAG_BREAK
221
+ #define TAG_NEXT RUBY_TAG_NEXT
222
+ #define TAG_RETRY RUBY_TAG_RETRY
223
+ #define TAG_REDO RUBY_TAG_REDO
224
+ #define TAG_RAISE RUBY_TAG_RAISE
225
+ #define TAG_THROW RUBY_TAG_THROW
226
+ #define TAG_FATAL RUBY_TAG_FATAL
227
+ #define TAG_MASK RUBY_TAG_MASK
228
+
229
+ enum ruby_vm_throw_flags {
230
+ VM_THROW_NO_ESCAPE_FLAG = 0x8000,
231
+ VM_THROW_STATE_MASK = 0xff
232
+ };
233
+
234
+ /* forward declarations */
235
+ struct rb_thread_struct;
236
+ struct rb_control_frame_struct;
237
+
238
+ /* iseq data type */
239
+ typedef struct rb_compile_option_struct rb_compile_option_t;
240
+
241
+ union ic_serial_entry {
242
+ rb_serial_t raw;
243
+ VALUE data[2];
244
+ };
245
+
246
+ // imemo_constcache
247
+ struct iseq_inline_constant_cache_entry {
248
+ VALUE flags;
249
+
250
+ VALUE value; // v0
251
+ VALUE _unused1; // v1
252
+ VALUE _unused2; // v2
253
+ const rb_cref_t *ic_cref; // v3
254
+ };
255
+ STATIC_ASSERT(sizeof_iseq_inline_constant_cache_entry,
256
+ (offsetof(struct iseq_inline_constant_cache_entry, ic_cref) +
257
+ sizeof(const rb_cref_t *)) <= RVALUE_SIZE);
258
+
259
+ struct iseq_inline_constant_cache {
260
+ struct iseq_inline_constant_cache_entry *entry;
261
+
262
+ /**
263
+ * A null-terminated list of ids, used to represent a constant's path
264
+ * idNULL is used to represent the :: prefix, and 0 is used to donate the end
265
+ * of the list.
266
+ *
267
+ * For example
268
+ * FOO {rb_intern("FOO"), 0}
269
+ * FOO::BAR {rb_intern("FOO"), rb_intern("BAR"), 0}
270
+ * ::FOO {idNULL, rb_intern("FOO"), 0}
271
+ * ::FOO::BAR {idNULL, rb_intern("FOO"), rb_intern("BAR"), 0}
272
+ */
273
+ const ID *segments;
274
+ };
275
+
276
+ struct iseq_inline_iv_cache_entry {
277
+ uintptr_t value; // attr_index in lower bits, dest_shape_id in upper bits
278
+ ID iv_set_name;
279
+ };
280
+
281
+ struct iseq_inline_cvar_cache_entry {
282
+ struct rb_cvar_class_tbl_entry *entry;
283
+ };
284
+
285
+ union iseq_inline_storage_entry {
286
+ struct {
287
+ struct rb_thread_struct *running_thread;
288
+ VALUE value;
289
+ } once;
290
+ struct iseq_inline_constant_cache ic_cache;
291
+ struct iseq_inline_iv_cache_entry iv_cache;
292
+ };
293
+
294
+ struct rb_calling_info {
295
+ const struct rb_callinfo *ci;
296
+ const struct rb_callcache *cc;
297
+ VALUE block_handler;
298
+ VALUE recv;
299
+ int argc;
300
+ int kw_splat;
301
+ };
302
+
303
+ struct rb_execution_context_struct;
304
+
305
+ #if 1
306
+ #define CoreDataFromValue(obj, type) (type*)DATA_PTR(obj)
307
+ #else
308
+ #define CoreDataFromValue(obj, type) (type*)rb_data_object_get(obj)
309
+ #endif
310
+ #define GetCoreDataFromValue(obj, type, ptr) ((ptr) = CoreDataFromValue((obj), type))
311
+
312
+ typedef struct rb_iseq_location_struct {
313
+ VALUE pathobj; /* String (path) or Array [path, realpath]. Frozen. */
314
+ VALUE base_label; /* String */
315
+ VALUE label; /* String */
316
+ int first_lineno;
317
+ int node_id;
318
+ rb_code_location_t code_location;
319
+ } rb_iseq_location_t;
320
+
321
+ #define PATHOBJ_PATH 0
322
+ #define PATHOBJ_REALPATH 1
323
+
324
+ static inline VALUE
325
+ pathobj_path(VALUE pathobj)
326
+ {
327
+ if (RB_TYPE_P(pathobj, T_STRING)) {
328
+ return pathobj;
329
+ }
330
+ else {
331
+ VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
332
+ return RARRAY_AREF(pathobj, PATHOBJ_PATH);
333
+ }
334
+ }
335
+
336
+ static inline VALUE
337
+ pathobj_realpath(VALUE pathobj)
338
+ {
339
+ if (RB_TYPE_P(pathobj, T_STRING)) {
340
+ return pathobj;
341
+ }
342
+ else {
343
+ VM_ASSERT(RB_TYPE_P(pathobj, T_ARRAY));
344
+ return RARRAY_AREF(pathobj, PATHOBJ_REALPATH);
345
+ }
346
+ }
347
+
348
+ /* Forward declarations */
349
+ struct rb_mjit_unit;
350
+
351
+ typedef uintptr_t iseq_bits_t;
352
+
353
+ #define ISEQ_IS_SIZE(body) (body->ic_size + body->ivc_size + body->ise_size + body->icvarc_size)
354
+
355
+ /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
356
+ #define ISEQ_IS_IC_ENTRY(body, idx) (body->is_entries[(idx) + body->ise_size + body->icvarc_size + body->ivc_size].ic_cache);
357
+
358
+ /* instruction sequence type */
359
+ enum rb_iseq_type {
360
+ ISEQ_TYPE_TOP,
361
+ ISEQ_TYPE_METHOD,
362
+ ISEQ_TYPE_BLOCK,
363
+ ISEQ_TYPE_CLASS,
364
+ ISEQ_TYPE_RESCUE,
365
+ ISEQ_TYPE_ENSURE,
366
+ ISEQ_TYPE_EVAL,
367
+ ISEQ_TYPE_MAIN,
368
+ ISEQ_TYPE_PLAIN
369
+ };
370
+
371
+ struct rb_iseq_constant_body {
372
+ enum rb_iseq_type type;
373
+
374
+ unsigned int iseq_size;
375
+ VALUE *iseq_encoded; /* encoded iseq (insn addr and operands) */
376
+
377
+ /**
378
+ * parameter information
379
+ *
380
+ * def m(a1, a2, ..., aM, # mandatory
381
+ * b1=(...), b2=(...), ..., bN=(...), # optional
382
+ * *c, # rest
383
+ * d1, d2, ..., dO, # post
384
+ * e1:(...), e2:(...), ..., eK:(...), # keyword
385
+ * **f, # keyword_rest
386
+ * &g) # block
387
+ * =>
388
+ *
389
+ * lead_num = M
390
+ * opt_num = N
391
+ * rest_start = M+N
392
+ * post_start = M+N+(*1)
393
+ * post_num = O
394
+ * keyword_num = K
395
+ * block_start = M+N+(*1)+O+K
396
+ * keyword_bits = M+N+(*1)+O+K+(&1)
397
+ * size = M+N+O+(*1)+K+(&1)+(**1) // parameter size.
398
+ */
399
+
400
+ struct {
401
+ struct {
402
+ unsigned int has_lead : 1;
403
+ unsigned int has_opt : 1;
404
+ unsigned int has_rest : 1;
405
+ unsigned int has_post : 1;
406
+ unsigned int has_kw : 1;
407
+ unsigned int has_kwrest : 1;
408
+ unsigned int has_block : 1;
409
+
410
+ unsigned int ambiguous_param0 : 1; /* {|a|} */
411
+ unsigned int accepts_no_kwarg : 1;
412
+ unsigned int ruby2_keywords: 1;
413
+ } flags;
414
+
415
+ unsigned int size;
416
+
417
+ int lead_num;
418
+ int opt_num;
419
+ int rest_start;
420
+ int post_start;
421
+ int post_num;
422
+ int block_start;
423
+
424
+ const VALUE *opt_table; /* (opt_num + 1) entries. */
425
+ /* opt_num and opt_table:
426
+ *
427
+ * def foo o1=e1, o2=e2, ..., oN=eN
428
+ * #=>
429
+ * # prologue code
430
+ * A1: e1
431
+ * A2: e2
432
+ * ...
433
+ * AN: eN
434
+ * AL: body
435
+ * opt_num = N
436
+ * opt_table = [A1, A2, ..., AN, AL]
437
+ */
438
+
439
+ const struct rb_iseq_param_keyword {
440
+ int num;
441
+ int required_num;
442
+ int bits_start;
443
+ int rest_start;
444
+ const ID *table;
445
+ VALUE *default_values;
446
+ } *keyword;
447
+ } param;
448
+
449
+ rb_iseq_location_t location;
450
+
451
+ /* insn info, must be freed */
452
+ struct iseq_insn_info {
453
+ const struct iseq_insn_info_entry *body;
454
+ unsigned int *positions;
455
+ unsigned int size;
456
+ #if VM_INSN_INFO_TABLE_IMPL == 2
457
+ struct succ_index_table *succ_index_table;
458
+ #endif
459
+ } insns_info;
460
+
461
+ const ID *local_table; /* must free */
462
+
463
+ /* catch table */
464
+ struct iseq_catch_table *catch_table;
465
+
466
+ /* for child iseq */
467
+ const struct rb_iseq_struct *parent_iseq;
468
+ struct rb_iseq_struct *local_iseq; /* local_iseq->flip_cnt can be modified */
469
+
470
+ union iseq_inline_storage_entry *is_entries; /* [ TS_IVC | TS_ICVARC | TS_ISE | TS_IC ] */
471
+ struct rb_call_data *call_data; //struct rb_call_data calls[ci_size];
472
+
473
+ struct {
474
+ rb_snum_t flip_count;
475
+ VALUE script_lines;
476
+ VALUE coverage;
477
+ VALUE pc2branchindex;
478
+ VALUE *original_iseq;
479
+ } variable;
480
+
481
+ unsigned int local_table_size;
482
+ unsigned int ic_size; // Number of IC caches
483
+ unsigned int ise_size; // Number of ISE caches
484
+ unsigned int ivc_size; // Number of IVC caches
485
+ unsigned int icvarc_size; // Number of ICVARC caches
486
+ unsigned int ci_size;
487
+ unsigned int stack_max; /* for stack overflow check */
488
+
489
+ bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
490
+ // If true, this ISeq is leaf *and* backtraces are not used, for example,
491
+ // by rb_profile_frames. We verify only leafness on VM_CHECK_MODE though.
492
+ // Note that GC allocations might use backtraces due to
493
+ // ObjectSpace#trace_object_allocations.
494
+ // For more details, see: https://bugs.ruby-lang.org/issues/16956
495
+ bool builtin_inline_p;
496
+
497
+ union {
498
+ iseq_bits_t * list; /* Find references for GC */
499
+ iseq_bits_t single;
500
+ } mark_bits;
501
+
502
+ struct rb_id_table *outer_variables;
503
+
504
+ const rb_iseq_t *mandatory_only_iseq;
505
+
506
+ #if USE_MJIT || USE_YJIT
507
+ // Function pointer for JIT code
508
+ VALUE (*jit_func)(struct rb_execution_context_struct *, struct rb_control_frame_struct *);
509
+ // Number of total calls with jit_exec()
510
+ long unsigned total_calls;
511
+ #endif
512
+
513
+ #if USE_MJIT
514
+ // MJIT stores some data on each iseq.
515
+ struct rb_mjit_unit *mjit_unit;
516
+ #endif
517
+
518
+ #if USE_YJIT
519
+ // YJIT stores some data on each iseq.
520
+ void *yjit_payload;
521
+ #endif
522
+ };
523
+
524
+ /* T_IMEMO/iseq */
525
+ /* typedef rb_iseq_t is in method.h */
526
+ struct rb_iseq_struct {
527
+ VALUE flags; /* 1 */
528
+ VALUE wrapper; /* 2 */
529
+
530
+ struct rb_iseq_constant_body *body; /* 3 */
531
+
532
+ union { /* 4, 5 words */
533
+ struct iseq_compile_data *compile_data; /* used at compile time */
534
+
535
+ struct {
536
+ VALUE obj;
537
+ int index;
538
+ } loader;
539
+
540
+ struct {
541
+ struct rb_hook_list_struct *local_hooks;
542
+ rb_event_flag_t global_trace_events;
543
+ } exec;
544
+ } aux;
545
+ };
546
+
547
+ #define ISEQ_BODY(iseq) ((iseq)->body)
548
+
549
+ #ifndef EXTSTATIC
550
+ #define EXTSTATIC 0
551
+ #endif
552
+
553
+ #ifndef USE_LAZY_LOAD
554
+ #define USE_LAZY_LOAD 0
555
+ #endif
556
+
557
+ #if USE_LAZY_LOAD
558
+ const rb_iseq_t *rb_iseq_complete(const rb_iseq_t *iseq);
559
+ #endif
560
+
561
+ static inline const rb_iseq_t *
562
+ rb_iseq_check(const rb_iseq_t *iseq)
563
+ {
564
+ #if USE_LAZY_LOAD
565
+ if (ISEQ_BODY(iseq) == NULL) {
566
+ rb_iseq_complete((rb_iseq_t *)iseq);
567
+ }
568
+ #endif
569
+ return iseq;
570
+ }
571
+
572
+ static inline const rb_iseq_t *
573
+ def_iseq_ptr(rb_method_definition_t *def)
574
+ {
575
+ //TODO: re-visit. to check the bug, enable this assertion.
576
+ #if VM_CHECK_MODE > 0
577
+ if (def->type != VM_METHOD_TYPE_ISEQ) rb_bug("def_iseq_ptr: not iseq (%d)", def->type);
578
+ #endif
579
+ return rb_iseq_check(def->body.iseq.iseqptr);
580
+ }
581
+
582
+ enum ruby_special_exceptions {
583
+ ruby_error_reenter,
584
+ ruby_error_nomemory,
585
+ ruby_error_sysstack,
586
+ ruby_error_stackfatal,
587
+ ruby_error_stream_closed,
588
+ ruby_special_error_count
589
+ };
590
+
591
+ #define GetVMPtr(obj, ptr) \
592
+ GetCoreDataFromValue((obj), rb_vm_t, (ptr))
593
+
594
+ struct rb_vm_struct;
595
+ typedef void rb_vm_at_exit_func(struct rb_vm_struct*);
596
+
597
+ typedef struct rb_at_exit_list {
598
+ rb_vm_at_exit_func *func;
599
+ struct rb_at_exit_list *next;
600
+ } rb_at_exit_list;
601
+
602
+ struct rb_objspace;
603
+ struct rb_objspace *rb_objspace_alloc(void);
604
+ void rb_objspace_free(struct rb_objspace *);
605
+ void rb_objspace_call_finalizer(struct rb_objspace *);
606
+
607
+ typedef struct rb_hook_list_struct {
608
+ struct rb_event_hook_struct *hooks;
609
+ rb_event_flag_t events;
610
+ unsigned int running;
611
+ bool need_clean;
612
+ bool is_local;
613
+ } rb_hook_list_t;
614
+
615
+
616
+ // see builtin.h for definition
617
+ typedef const struct rb_builtin_function *RB_BUILTIN;
618
+
619
+ typedef struct rb_vm_struct {
620
+ VALUE self;
621
+
622
+ struct {
623
+ struct ccan_list_head set;
624
+ unsigned int cnt;
625
+ unsigned int blocking_cnt;
626
+
627
+ struct rb_ractor_struct *main_ractor;
628
+ struct rb_thread_struct *main_thread; // == vm->ractor.main_ractor->threads.main
629
+
630
+ struct {
631
+ // monitor
632
+ rb_nativethread_lock_t lock;
633
+ struct rb_ractor_struct *lock_owner;
634
+ unsigned int lock_rec;
635
+
636
+ // barrier
637
+ bool barrier_waiting;
638
+ unsigned int barrier_cnt;
639
+ rb_nativethread_cond_t barrier_cond;
640
+
641
+ // join at exit
642
+ rb_nativethread_cond_t terminate_cond;
643
+ bool terminate_waiting;
644
+ } sync;
645
+ } ractor;
646
+
647
+ #ifdef USE_SIGALTSTACK
648
+ void *main_altstack;
649
+ #endif
650
+
651
+ rb_serial_t fork_gen;
652
+ rb_nativethread_lock_t waitpid_lock;
653
+ struct ccan_list_head waiting_pids; /* PID > 0: <=> struct waitpid_state */
654
+ struct ccan_list_head waiting_grps; /* PID <= 0: <=> struct waitpid_state */
655
+ struct ccan_list_head waiting_fds; /* <=> struct waiting_fd */
656
+
657
+ /* set in single-threaded processes only: */
658
+ volatile int ubf_async_safe;
659
+
660
+ unsigned int running: 1;
661
+ unsigned int thread_abort_on_exception: 1;
662
+ unsigned int thread_report_on_exception: 1;
663
+ unsigned int thread_ignore_deadlock: 1;
664
+
665
+ /* object management */
666
+ VALUE mark_object_ary;
667
+ const VALUE special_exceptions[ruby_special_error_count];
668
+
669
+ /* object shapes */
670
+ rb_shape_t *shape_list;
671
+ rb_shape_t *root_shape;
672
+ shape_id_t next_shape_id;
673
+
674
+ /* load */
675
+ VALUE top_self;
676
+ VALUE load_path;
677
+ VALUE load_path_snapshot;
678
+ VALUE load_path_check_cache;
679
+ VALUE expanded_load_path;
680
+ VALUE loaded_features;
681
+ VALUE loaded_features_snapshot;
682
+ VALUE loaded_features_realpaths;
683
+ struct st_table *loaded_features_index;
684
+ struct st_table *loading_table;
685
+ #if EXTSTATIC
686
+ // For running the init function of statically linked
687
+ // extensions when they are loaded
688
+ struct st_table *static_ext_inits;
689
+ #endif
690
+
691
+ /* signal */
692
+ struct {
693
+ VALUE cmd[RUBY_NSIG];
694
+ } trap_list;
695
+
696
+ /* relation table of ensure - rollback for callcc */
697
+ struct st_table *ensure_rollback_table;
698
+
699
+ /* postponed_job (async-signal-safe, NOT thread-safe) */
700
+ struct rb_postponed_job_struct *postponed_job_buffer;
701
+ rb_atomic_t postponed_job_index;
702
+
703
+ int src_encoding_index;
704
+
705
+ /* workqueue (thread-safe, NOT async-signal-safe) */
706
+ struct ccan_list_head workqueue; /* <=> rb_workqueue_job.jnode */
707
+ rb_nativethread_lock_t workqueue_lock;
708
+
709
+ VALUE orig_progname, progname;
710
+ VALUE coverages, me2counter;
711
+ int coverage_mode;
712
+
713
+ st_table * defined_module_hash;
714
+
715
+ struct rb_objspace *objspace;
716
+
717
+ rb_at_exit_list *at_exit;
718
+
719
+ st_table *frozen_strings;
720
+
721
+ const struct rb_builtin_function *builtin_function_table;
722
+ int builtin_inline_index;
723
+
724
+ struct rb_id_table *negative_cme_table;
725
+ st_table *overloaded_cme_table; // cme -> overloaded_cme
726
+
727
+ // This id table contains a mapping from ID to ICs. It does this with ID
728
+ // keys and nested st_tables as values. The nested tables have ICs as keys
729
+ // and Qtrue as values. It is used when inline constant caches need to be
730
+ // invalidated or ISEQs are being freed.
731
+ struct rb_id_table *constant_cache;
732
+
733
+ #ifndef VM_GLOBAL_CC_CACHE_TABLE_SIZE
734
+ #define VM_GLOBAL_CC_CACHE_TABLE_SIZE 1023
735
+ #endif
736
+ const struct rb_callcache *global_cc_cache_table[VM_GLOBAL_CC_CACHE_TABLE_SIZE]; // vm_eval.c
737
+
738
+ #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
739
+ uint32_t clock;
740
+ #endif
741
+
742
+ /* params */
743
+ struct { /* size in byte */
744
+ size_t thread_vm_stack_size;
745
+ size_t thread_machine_stack_size;
746
+ size_t fiber_vm_stack_size;
747
+ size_t fiber_machine_stack_size;
748
+ } default_params;
749
+
750
+ } rb_vm_t;
751
+
752
+ /* default values */
753
+
754
+ #define RUBY_VM_SIZE_ALIGN 4096
755
+
756
+ #define RUBY_VM_THREAD_VM_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
757
+ #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
758
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE ( 128 * 1024 * sizeof(VALUE)) /* 512 KB or 1024 KB */
759
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
760
+
761
+ #define RUBY_VM_FIBER_VM_STACK_SIZE ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
762
+ #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN ( 2 * 1024 * sizeof(VALUE)) /* 8 KB or 16 KB */
763
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 64 * 1024 * sizeof(VALUE)) /* 256 KB or 512 KB */
764
+ #if defined(__powerpc64__) || defined(__ppc64__) // macOS has __ppc64__
765
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 32 * 1024 * sizeof(VALUE)) /* 128 KB or 256 KB */
766
+ #else
767
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 16 * 1024 * sizeof(VALUE)) /* 64 KB or 128 KB */
768
+ #endif
769
+
770
+ #if __has_feature(memory_sanitizer) || __has_feature(address_sanitizer)
771
+ /* It seems sanitizers consume A LOT of machine stacks */
772
+ #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE
773
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE (1024 * 1024 * sizeof(VALUE))
774
+ #undef RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN
775
+ #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN ( 512 * 1024 * sizeof(VALUE))
776
+ #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE
777
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE ( 256 * 1024 * sizeof(VALUE))
778
+ #undef RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN
779
+ #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN ( 128 * 1024 * sizeof(VALUE))
780
+ #endif
781
+
782
+ #ifndef VM_DEBUG_BP_CHECK
783
+ #define VM_DEBUG_BP_CHECK 0
784
+ #endif
785
+
786
+ #ifndef VM_DEBUG_VERIFY_METHOD_CACHE
787
+ #define VM_DEBUG_VERIFY_METHOD_CACHE (VMDEBUG != 0)
788
+ #endif
789
+
790
+ struct rb_captured_block {
791
+ VALUE self;
792
+ const VALUE *ep;
793
+ union {
794
+ const rb_iseq_t *iseq;
795
+ const struct vm_ifunc *ifunc;
796
+ VALUE val;
797
+ } code;
798
+ };
799
+
800
+ enum rb_block_handler_type {
801
+ block_handler_type_iseq,
802
+ block_handler_type_ifunc,
803
+ block_handler_type_symbol,
804
+ block_handler_type_proc
805
+ };
806
+
807
+ enum rb_block_type {
808
+ block_type_iseq,
809
+ block_type_ifunc,
810
+ block_type_symbol,
811
+ block_type_proc
812
+ };
813
+
814
+ struct rb_block {
815
+ union {
816
+ struct rb_captured_block captured;
817
+ VALUE symbol;
818
+ VALUE proc;
819
+ } as;
820
+ enum rb_block_type type;
821
+ };
822
+
823
+ typedef struct rb_control_frame_struct {
824
+ const VALUE *pc; /* cfp[0] */
825
+ VALUE *sp; /* cfp[1] */
826
+ const rb_iseq_t *iseq; /* cfp[2] */
827
+ VALUE self; /* cfp[3] / block[0] */
828
+ const VALUE *ep; /* cfp[4] / block[1] */
829
+ const void *block_code; /* cfp[5] / block[2] */ /* iseq or ifunc or forwarded block handler */
830
+ VALUE *__bp__; /* cfp[6] */ /* outside vm_push_frame, use vm_base_ptr instead. */
831
+
832
+ #if VM_DEBUG_BP_CHECK
833
+ VALUE *bp_check; /* cfp[7] */
834
+ #endif
835
+ // Return address for YJIT code
836
+ void *jit_return;
837
+ } rb_control_frame_t;
838
+
839
+ extern const rb_data_type_t ruby_threadptr_data_type;
840
+
841
+ static inline struct rb_thread_struct *
842
+ rb_thread_ptr(VALUE thval)
843
+ {
844
+ return (struct rb_thread_struct *)rb_check_typeddata(thval, &ruby_threadptr_data_type);
845
+ }
846
+
847
+ enum rb_thread_status {
848
+ THREAD_RUNNABLE,
849
+ THREAD_STOPPED,
850
+ THREAD_STOPPED_FOREVER,
851
+ THREAD_KILLED
852
+ };
853
+
854
+ #ifdef RUBY_JMP_BUF
855
+ typedef RUBY_JMP_BUF rb_jmpbuf_t;
856
+ #else
857
+ typedef void *rb_jmpbuf_t[5];
858
+ #endif
859
+
860
+ /*
861
+ the members which are written in EC_PUSH_TAG() should be placed at
862
+ the beginning and the end, so that entire region is accessible.
863
+ */
864
+ struct rb_vm_tag {
865
+ VALUE tag;
866
+ VALUE retval;
867
+ rb_jmpbuf_t buf;
868
+ struct rb_vm_tag *prev;
869
+ enum ruby_tag_type state;
870
+ unsigned int lock_rec;
871
+ };
872
+
873
+ STATIC_ASSERT(rb_vm_tag_buf_offset, offsetof(struct rb_vm_tag, buf) > 0);
874
+ STATIC_ASSERT(rb_vm_tag_buf_end,
875
+ offsetof(struct rb_vm_tag, buf) + sizeof(rb_jmpbuf_t) <
876
+ sizeof(struct rb_vm_tag));
877
+
878
+ struct rb_unblock_callback {
879
+ rb_unblock_function_t *func;
880
+ void *arg;
881
+ };
882
+
883
+ struct rb_mutex_struct;
884
+
885
+ typedef struct rb_ensure_entry {
886
+ VALUE marker;
887
+ VALUE (*e_proc)(VALUE);
888
+ VALUE data2;
889
+ } rb_ensure_entry_t;
890
+
891
+ typedef struct rb_ensure_list {
892
+ struct rb_ensure_list *next;
893
+ struct rb_ensure_entry entry;
894
+ } rb_ensure_list_t;
895
+
896
+ typedef struct rb_fiber_struct rb_fiber_t;
897
+
898
+ struct rb_waiting_list {
899
+ struct rb_waiting_list *next;
900
+ struct rb_thread_struct *thread;
901
+ struct rb_fiber_struct *fiber;
902
+ };
903
+
904
+ struct rb_execution_context_struct {
905
+ /* execution information */
906
+ VALUE *vm_stack; /* must free, must mark */
907
+ size_t vm_stack_size; /* size in word (byte size / sizeof(VALUE)) */
908
+ rb_control_frame_t *cfp;
909
+
910
+ struct rb_vm_tag *tag;
911
+
912
+ /* interrupt flags */
913
+ rb_atomic_t interrupt_flag;
914
+ rb_atomic_t interrupt_mask; /* size should match flag */
915
+ #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
916
+ uint32_t checked_clock;
917
+ #endif
918
+
919
+ rb_fiber_t *fiber_ptr;
920
+ struct rb_thread_struct *thread_ptr;
921
+
922
+ /* storage (ec (fiber) local) */
923
+ struct rb_id_table *local_storage;
924
+ VALUE local_storage_recursive_hash;
925
+ VALUE local_storage_recursive_hash_for_trace;
926
+
927
+ /* Inheritable fiber storage. */
928
+ VALUE storage;
929
+
930
+ /* eval env */
931
+ const VALUE *root_lep;
932
+ VALUE root_svar;
933
+
934
+ /* ensure & callcc */
935
+ rb_ensure_list_t *ensure_list;
936
+
937
+ /* trace information */
938
+ struct rb_trace_arg_struct *trace_arg;
939
+
940
+ /* temporary places */
941
+ VALUE errinfo;
942
+ VALUE passed_block_handler; /* for rb_iterate */
943
+
944
+ uint8_t raised_flag; /* only 3 bits needed */
945
+
946
+ /* n.b. only 7 bits needed, really: */
947
+ BITFIELD(enum method_missing_reason, method_missing_reason, 8);
948
+
949
+ VALUE private_const_reference;
950
+
951
+ /* for GC */
952
+ struct {
953
+ VALUE *stack_start;
954
+ VALUE *stack_end;
955
+ size_t stack_maxsize;
956
+ RUBY_ALIGNAS(SIZEOF_VALUE) jmp_buf regs;
957
+ } machine;
958
+ };
959
+
960
+ #ifndef rb_execution_context_t
961
+ typedef struct rb_execution_context_struct rb_execution_context_t;
962
+ #define rb_execution_context_t rb_execution_context_t
963
+ #endif
964
+
965
+ // for builtin.h
966
+ #define VM_CORE_H_EC_DEFINED 1
967
+
968
+ // Set the vm_stack pointer in the execution context.
969
+ void rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
970
+
971
+ // Initialize the vm_stack pointer in the execution context and push the initial stack frame.
972
+ // @param ec the execution context to update.
973
+ // @param stack a pointer to the stack to use.
974
+ // @param size the size of the stack, as in `VALUE stack[size]`.
975
+ void rb_ec_initialize_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size);
976
+
977
+ // Clear (set to `NULL`) the vm_stack pointer.
978
+ // @param ec the execution context to update.
979
+ void rb_ec_clear_vm_stack(rb_execution_context_t *ec);
980
+
981
+ struct rb_ext_config {
982
+ bool ractor_safe;
983
+ };
984
+
985
+ typedef struct rb_ractor_struct rb_ractor_t;
986
+
987
+ struct rb_native_thread;
988
+
989
+ typedef struct rb_thread_struct {
990
+ struct ccan_list_node lt_node; // managed by a ractor
991
+ VALUE self;
992
+ rb_ractor_t *ractor;
993
+ rb_vm_t *vm;
994
+ struct rb_native_thread *nt;
995
+ rb_execution_context_t *ec;
996
+
997
+ struct rb_thread_sched_item sched;
998
+ rb_atomic_t serial; // only for RUBY_DEBUG_LOG()
999
+
1000
+ VALUE last_status; /* $? */
1001
+
1002
+ /* for cfunc */
1003
+ struct rb_calling_info *calling;
1004
+
1005
+ /* for load(true) */
1006
+ VALUE top_self;
1007
+ VALUE top_wrapper;
1008
+
1009
+ /* thread control */
1010
+
1011
+ BITFIELD(enum rb_thread_status, status, 2);
1012
+ /* bit flags */
1013
+ unsigned int locking_native_thread : 1;
1014
+ unsigned int to_kill : 1;
1015
+ unsigned int abort_on_exception: 1;
1016
+ unsigned int report_on_exception: 1;
1017
+ unsigned int pending_interrupt_queue_checked: 1;
1018
+ int8_t priority; /* -3 .. 3 (RUBY_THREAD_PRIORITY_{MIN,MAX}) */
1019
+ uint32_t running_time_us; /* 12500..800000 */
1020
+
1021
+ void *blocking_region_buffer;
1022
+
1023
+ VALUE thgroup;
1024
+ VALUE value;
1025
+
1026
+ /* temporary place of retval on OPT_CALL_THREADED_CODE */
1027
+ #if OPT_CALL_THREADED_CODE
1028
+ VALUE retval;
1029
+ #endif
1030
+
1031
+ /* async errinfo queue */
1032
+ VALUE pending_interrupt_queue;
1033
+ VALUE pending_interrupt_mask_stack;
1034
+
1035
+ /* interrupt management */
1036
+ rb_nativethread_lock_t interrupt_lock;
1037
+ struct rb_unblock_callback unblock;
1038
+ VALUE locking_mutex;
1039
+ struct rb_mutex_struct *keeping_mutexes;
1040
+
1041
+ struct rb_waiting_list *join_list;
1042
+
1043
+ union {
1044
+ struct {
1045
+ VALUE proc;
1046
+ VALUE args;
1047
+ int kw_splat;
1048
+ } proc;
1049
+ struct {
1050
+ VALUE (*func)(void *);
1051
+ void *arg;
1052
+ } func;
1053
+ } invoke_arg;
1054
+
1055
+ enum thread_invoke_type {
1056
+ thread_invoke_type_none = 0,
1057
+ thread_invoke_type_proc,
1058
+ thread_invoke_type_ractor_proc,
1059
+ thread_invoke_type_func
1060
+ } invoke_type;
1061
+
1062
+ /* statistics data for profiler */
1063
+ VALUE stat_insn_usage;
1064
+
1065
+ /* fiber */
1066
+ rb_fiber_t *root_fiber;
1067
+
1068
+ VALUE scheduler;
1069
+ unsigned int blocking;
1070
+
1071
+ /* misc */
1072
+ VALUE name;
1073
+
1074
+ struct rb_ext_config ext_config;
1075
+ } rb_thread_t;
1076
+
1077
+ static inline unsigned int
1078
+ rb_th_serial(const rb_thread_t *th)
1079
+ {
1080
+ return (unsigned int)th->serial;
1081
+ }
1082
+
1083
+ typedef enum {
1084
+ VM_DEFINECLASS_TYPE_CLASS = 0x00,
1085
+ VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
1086
+ VM_DEFINECLASS_TYPE_MODULE = 0x02,
1087
+ /* 0x03..0x06 is reserved */
1088
+ VM_DEFINECLASS_TYPE_MASK = 0x07
1089
+ } rb_vm_defineclass_type_t;
1090
+
1091
+ #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
1092
+ #define VM_DEFINECLASS_FLAG_SCOPED 0x08
1093
+ #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
1094
+ #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
1095
+ #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
1096
+ ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
1097
+
1098
+ /* iseq.c */
1099
+ RUBY_SYMBOL_EXPORT_BEGIN
1100
+
1101
+ /* node -> iseq */
1102
+ rb_iseq_t *rb_iseq_new (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, enum rb_iseq_type);
1103
+ rb_iseq_t *rb_iseq_new_top (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent);
1104
+ rb_iseq_t *rb_iseq_new_main (const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt);
1105
+ rb_iseq_t *rb_iseq_new_eval (const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth);
1106
+ rb_iseq_t *rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1107
+ enum rb_iseq_type, const rb_compile_option_t*);
1108
+
1109
+ struct iseq_link_anchor;
1110
+ struct rb_iseq_new_with_callback_callback_func {
1111
+ VALUE flags;
1112
+ VALUE reserved;
1113
+ void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *);
1114
+ const void *data;
1115
+ };
1116
+ static inline struct rb_iseq_new_with_callback_callback_func *
1117
+ rb_iseq_new_with_callback_new_callback(
1118
+ void (*func)(rb_iseq_t *, struct iseq_link_anchor *, const void *), const void *ptr)
1119
+ {
1120
+ VALUE memo = rb_imemo_new(imemo_ifunc, (VALUE)func, (VALUE)ptr, Qundef, Qfalse);
1121
+ return (struct rb_iseq_new_with_callback_callback_func *)memo;
1122
+ }
1123
+ rb_iseq_t *rb_iseq_new_with_callback(const struct rb_iseq_new_with_callback_callback_func * ifunc,
1124
+ VALUE name, VALUE path, VALUE realpath, int first_lineno,
1125
+ const rb_iseq_t *parent, enum rb_iseq_type, const rb_compile_option_t*);
1126
+
1127
+ VALUE rb_iseq_disasm(const rb_iseq_t *iseq);
1128
+ int rb_iseq_disasm_insn(VALUE str, const VALUE *iseqval, size_t pos, const rb_iseq_t *iseq, VALUE child);
1129
+ attr_index_t rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq);
1130
+
1131
+ VALUE rb_iseq_coverage(const rb_iseq_t *iseq);
1132
+
1133
+ RUBY_EXTERN VALUE rb_cISeq;
1134
+ RUBY_EXTERN VALUE rb_cRubyVM;
1135
+ RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
1136
+ RUBY_EXTERN VALUE rb_block_param_proxy;
1137
+ RUBY_SYMBOL_EXPORT_END
1138
+
1139
+ #define GetProcPtr(obj, ptr) \
1140
+ GetCoreDataFromValue((obj), rb_proc_t, (ptr))
1141
+
1142
+ typedef struct {
1143
+ const struct rb_block block;
1144
+ unsigned int is_from_method: 1; /* bool */
1145
+ unsigned int is_lambda: 1; /* bool */
1146
+ unsigned int is_isolated: 1; /* bool */
1147
+ } rb_proc_t;
1148
+
1149
+ RUBY_SYMBOL_EXPORT_BEGIN
1150
+ VALUE rb_proc_isolate(VALUE self);
1151
+ VALUE rb_proc_isolate_bang(VALUE self);
1152
+ VALUE rb_proc_ractor_make_shareable(VALUE self);
1153
+ RUBY_SYMBOL_EXPORT_END
1154
+
1155
+ typedef struct {
1156
+ VALUE flags; /* imemo header */
1157
+ rb_iseq_t *iseq;
1158
+ const VALUE *ep;
1159
+ const VALUE *env;
1160
+ unsigned int env_size;
1161
+ } rb_env_t;
1162
+
1163
+ extern const rb_data_type_t ruby_binding_data_type;
1164
+
1165
+ #define GetBindingPtr(obj, ptr) \
1166
+ GetCoreDataFromValue((obj), rb_binding_t, (ptr))
1167
+
1168
+ typedef struct {
1169
+ const struct rb_block block;
1170
+ const VALUE pathobj;
1171
+ int first_lineno;
1172
+ } rb_binding_t;
1173
+
1174
+ /* used by compile time and send insn */
1175
+
1176
+ enum vm_check_match_type {
1177
+ VM_CHECKMATCH_TYPE_WHEN = 1,
1178
+ VM_CHECKMATCH_TYPE_CASE = 2,
1179
+ VM_CHECKMATCH_TYPE_RESCUE = 3
1180
+ };
1181
+
1182
+ #define VM_CHECKMATCH_TYPE_MASK 0x03
1183
+ #define VM_CHECKMATCH_ARRAY 0x04
1184
+
1185
+ enum vm_special_object_type {
1186
+ VM_SPECIAL_OBJECT_VMCORE = 1,
1187
+ VM_SPECIAL_OBJECT_CBASE,
1188
+ VM_SPECIAL_OBJECT_CONST_BASE
1189
+ };
1190
+
1191
+ enum vm_svar_index {
1192
+ VM_SVAR_LASTLINE = 0, /* $_ */
1193
+ VM_SVAR_BACKREF = 1, /* $~ */
1194
+
1195
+ VM_SVAR_EXTRA_START = 2,
1196
+ VM_SVAR_FLIPFLOP_START = 2 /* flipflop */
1197
+ };
1198
+
1199
+ /* inline cache */
1200
+ typedef struct iseq_inline_constant_cache *IC;
1201
+ typedef struct iseq_inline_iv_cache_entry *IVC;
1202
+ typedef struct iseq_inline_cvar_cache_entry *ICVARC;
1203
+ typedef union iseq_inline_storage_entry *ISE;
1204
+ typedef const struct rb_callinfo *CALL_INFO;
1205
+ typedef const struct rb_callcache *CALL_CACHE;
1206
+ typedef struct rb_call_data *CALL_DATA;
1207
+
1208
+ typedef VALUE CDHASH;
1209
+
1210
+ #ifndef FUNC_FASTCALL
1211
+ #define FUNC_FASTCALL(x) x
1212
+ #endif
1213
+
1214
+ typedef rb_control_frame_t *
1215
+ (FUNC_FASTCALL(*rb_insn_func_t))(rb_execution_context_t *, rb_control_frame_t *);
1216
+
1217
+ #define VM_TAGGED_PTR_SET(p, tag) ((VALUE)(p) | (tag))
1218
+ #define VM_TAGGED_PTR_REF(v, mask) ((void *)((v) & ~mask))
1219
+
1220
+ #define GC_GUARDED_PTR(p) VM_TAGGED_PTR_SET((p), 0x01)
1221
+ #define GC_GUARDED_PTR_REF(p) VM_TAGGED_PTR_REF((p), 0x03)
1222
+ #define GC_GUARDED_PTR_P(p) (((VALUE)(p)) & 0x01)
1223
+
1224
+ enum vm_frame_env_flags {
1225
+ /* Frame/Environment flag bits:
1226
+ * MMMM MMMM MMMM MMMM ____ FFFF FFFE EEEX (LSB)
1227
+ *
1228
+ * X : tag for GC marking (It seems as Fixnum)
1229
+ * EEE : 4 bits Env flags
1230
+ * FF..: 7 bits Frame flags
1231
+ * MM..: 15 bits frame magic (to check frame corruption)
1232
+ */
1233
+
1234
+ /* frame types */
1235
+ VM_FRAME_MAGIC_METHOD = 0x11110001,
1236
+ VM_FRAME_MAGIC_BLOCK = 0x22220001,
1237
+ VM_FRAME_MAGIC_CLASS = 0x33330001,
1238
+ VM_FRAME_MAGIC_TOP = 0x44440001,
1239
+ VM_FRAME_MAGIC_CFUNC = 0x55550001,
1240
+ VM_FRAME_MAGIC_IFUNC = 0x66660001,
1241
+ VM_FRAME_MAGIC_EVAL = 0x77770001,
1242
+ VM_FRAME_MAGIC_RESCUE = 0x78880001,
1243
+ VM_FRAME_MAGIC_DUMMY = 0x79990001,
1244
+
1245
+ VM_FRAME_MAGIC_MASK = 0x7fff0001,
1246
+
1247
+ /* frame flag */
1248
+ VM_FRAME_FLAG_FINISH = 0x0020,
1249
+ VM_FRAME_FLAG_BMETHOD = 0x0040,
1250
+ VM_FRAME_FLAG_CFRAME = 0x0080,
1251
+ VM_FRAME_FLAG_LAMBDA = 0x0100,
1252
+ VM_FRAME_FLAG_MODIFIED_BLOCK_PARAM = 0x0200,
1253
+ VM_FRAME_FLAG_CFRAME_KW = 0x0400,
1254
+ VM_FRAME_FLAG_PASSED = 0x0800,
1255
+
1256
+ /* env flag */
1257
+ VM_ENV_FLAG_LOCAL = 0x0002,
1258
+ VM_ENV_FLAG_ESCAPED = 0x0004,
1259
+ VM_ENV_FLAG_WB_REQUIRED = 0x0008,
1260
+ VM_ENV_FLAG_ISOLATED = 0x0010,
1261
+ };
1262
+
1263
+ #define VM_ENV_DATA_SIZE ( 3)
1264
+
1265
+ #define VM_ENV_DATA_INDEX_ME_CREF (-2) /* ep[-2] */
1266
+ #define VM_ENV_DATA_INDEX_SPECVAL (-1) /* ep[-1] */
1267
+ #define VM_ENV_DATA_INDEX_FLAGS ( 0) /* ep[ 0] */
1268
+ #define VM_ENV_DATA_INDEX_ENV ( 1) /* ep[ 1] */
1269
+
1270
+ #define VM_ENV_INDEX_LAST_LVAR (-VM_ENV_DATA_SIZE)
1271
+
1272
+ static inline void VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value);
1273
+
1274
+ static inline void
1275
+ VM_ENV_FLAGS_SET(const VALUE *ep, VALUE flag)
1276
+ {
1277
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1278
+ VM_ASSERT(FIXNUM_P(flags));
1279
+ VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags | flag);
1280
+ }
1281
+
1282
+ static inline void
1283
+ VM_ENV_FLAGS_UNSET(const VALUE *ep, VALUE flag)
1284
+ {
1285
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1286
+ VM_ASSERT(FIXNUM_P(flags));
1287
+ VM_FORCE_WRITE_SPECIAL_CONST(&ep[VM_ENV_DATA_INDEX_FLAGS], flags & ~flag);
1288
+ }
1289
+
1290
+ static inline unsigned long
1291
+ VM_ENV_FLAGS(const VALUE *ep, long flag)
1292
+ {
1293
+ VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
1294
+ VM_ASSERT(FIXNUM_P(flags));
1295
+ return flags & flag;
1296
+ }
1297
+
1298
+ static inline unsigned long
1299
+ VM_FRAME_TYPE(const rb_control_frame_t *cfp)
1300
+ {
1301
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_MAGIC_MASK);
1302
+ }
1303
+
1304
+ static inline int
1305
+ VM_FRAME_LAMBDA_P(const rb_control_frame_t *cfp)
1306
+ {
1307
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_LAMBDA) != 0;
1308
+ }
1309
+
1310
+ static inline int
1311
+ VM_FRAME_CFRAME_KW_P(const rb_control_frame_t *cfp)
1312
+ {
1313
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME_KW) != 0;
1314
+ }
1315
+
1316
+ static inline int
1317
+ VM_FRAME_FINISHED_P(const rb_control_frame_t *cfp)
1318
+ {
1319
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_FINISH) != 0;
1320
+ }
1321
+
1322
+ static inline int
1323
+ VM_FRAME_BMETHOD_P(const rb_control_frame_t *cfp)
1324
+ {
1325
+ return VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_BMETHOD) != 0;
1326
+ }
1327
+
1328
+ static inline int
1329
+ rb_obj_is_iseq(VALUE iseq)
1330
+ {
1331
+ return imemo_type_p(iseq, imemo_iseq);
1332
+ }
1333
+
1334
+ #if VM_CHECK_MODE > 0
1335
+ #define RUBY_VM_NORMAL_ISEQ_P(iseq) rb_obj_is_iseq((VALUE)iseq)
1336
+ #endif
1337
+
1338
+ static inline int
1339
+ VM_FRAME_CFRAME_P(const rb_control_frame_t *cfp)
1340
+ {
1341
+ int cframe_p = VM_ENV_FLAGS(cfp->ep, VM_FRAME_FLAG_CFRAME) != 0;
1342
+ VM_ASSERT(RUBY_VM_NORMAL_ISEQ_P(cfp->iseq) != cframe_p ||
1343
+ (VM_FRAME_TYPE(cfp) & VM_FRAME_MAGIC_MASK) == VM_FRAME_MAGIC_DUMMY);
1344
+ return cframe_p;
1345
+ }
1346
+
1347
+ static inline int
1348
+ VM_FRAME_RUBYFRAME_P(const rb_control_frame_t *cfp)
1349
+ {
1350
+ return !VM_FRAME_CFRAME_P(cfp);
1351
+ }
1352
+
1353
+ #define RUBYVM_CFUNC_FRAME_P(cfp) \
1354
+ (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
1355
+
1356
+ #define VM_GUARDED_PREV_EP(ep) GC_GUARDED_PTR(ep)
1357
+ #define VM_BLOCK_HANDLER_NONE 0
1358
+
1359
+ static inline int
1360
+ VM_ENV_LOCAL_P(const VALUE *ep)
1361
+ {
1362
+ return VM_ENV_FLAGS(ep, VM_ENV_FLAG_LOCAL) ? 1 : 0;
1363
+ }
1364
+
1365
+ static inline const VALUE *
1366
+ VM_ENV_PREV_EP(const VALUE *ep)
1367
+ {
1368
+ VM_ASSERT(VM_ENV_LOCAL_P(ep) == 0);
1369
+ return GC_GUARDED_PTR_REF(ep[VM_ENV_DATA_INDEX_SPECVAL]);
1370
+ }
1371
+
1372
+ static inline VALUE
1373
+ VM_ENV_BLOCK_HANDLER(const VALUE *ep)
1374
+ {
1375
+ VM_ASSERT(VM_ENV_LOCAL_P(ep));
1376
+ return ep[VM_ENV_DATA_INDEX_SPECVAL];
1377
+ }
1378
+
1379
+ #if VM_CHECK_MODE > 0
1380
+ int rb_vm_ep_in_heap_p(const VALUE *ep);
1381
+ #endif
1382
+
1383
+ static inline int
1384
+ VM_ENV_ESCAPED_P(const VALUE *ep)
1385
+ {
1386
+ VM_ASSERT(rb_vm_ep_in_heap_p(ep) == !!VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED));
1387
+ return VM_ENV_FLAGS(ep, VM_ENV_FLAG_ESCAPED) ? 1 : 0;
1388
+ }
1389
+
1390
+ #if VM_CHECK_MODE > 0
1391
+ static inline int
1392
+ vm_assert_env(VALUE obj)
1393
+ {
1394
+ VM_ASSERT(imemo_type_p(obj, imemo_env));
1395
+ return 1;
1396
+ }
1397
+ #endif
1398
+
1399
+ RBIMPL_ATTR_NONNULL((1))
1400
+ static inline VALUE
1401
+ VM_ENV_ENVVAL(const VALUE *ep)
1402
+ {
1403
+ VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1404
+ VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1405
+ VM_ASSERT(vm_assert_env(envval));
1406
+ return envval;
1407
+ }
1408
+
1409
+ RBIMPL_ATTR_NONNULL((1))
1410
+ static inline const rb_env_t *
1411
+ VM_ENV_ENVVAL_PTR(const VALUE *ep)
1412
+ {
1413
+ return (const rb_env_t *)VM_ENV_ENVVAL(ep);
1414
+ }
1415
+
1416
+ static inline const rb_env_t *
1417
+ vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
1418
+ {
1419
+ rb_env_t *env = (rb_env_t *)rb_imemo_new(imemo_env, (VALUE)env_ep, (VALUE)env_body, 0, (VALUE)iseq);
1420
+ env->env_size = env_size;
1421
+ env_ep[VM_ENV_DATA_INDEX_ENV] = (VALUE)env;
1422
+ return env;
1423
+ }
1424
+
1425
+ static inline void
1426
+ VM_FORCE_WRITE(const VALUE *ptr, VALUE v)
1427
+ {
1428
+ *((VALUE *)ptr) = v;
1429
+ }
1430
+
1431
+ static inline void
1432
+ VM_FORCE_WRITE_SPECIAL_CONST(const VALUE *ptr, VALUE special_const_value)
1433
+ {
1434
+ VM_ASSERT(RB_SPECIAL_CONST_P(special_const_value));
1435
+ VM_FORCE_WRITE(ptr, special_const_value);
1436
+ }
1437
+
1438
+ static inline void
1439
+ VM_STACK_ENV_WRITE(const VALUE *ep, int index, VALUE v)
1440
+ {
1441
+ VM_ASSERT(VM_ENV_FLAGS(ep, VM_ENV_FLAG_WB_REQUIRED) == 0);
1442
+ VM_FORCE_WRITE(&ep[index], v);
1443
+ }
1444
+
1445
+ const VALUE *rb_vm_ep_local_ep(const VALUE *ep);
1446
+ const VALUE *rb_vm_proc_local_ep(VALUE proc);
1447
+ void rb_vm_block_ep_update(VALUE obj, const struct rb_block *dst, const VALUE *ep);
1448
+ void rb_vm_block_copy(VALUE obj, const struct rb_block *dst, const struct rb_block *src);
1449
+
1450
+ VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp);
1451
+
1452
+ #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
1453
+ #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
1454
+
1455
+ #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
1456
+ ((void *)(ecfp) > (void *)(cfp))
1457
+
1458
+ static inline const rb_control_frame_t *
1459
+ RUBY_VM_END_CONTROL_FRAME(const rb_execution_context_t *ec)
1460
+ {
1461
+ return (rb_control_frame_t *)(ec->vm_stack + ec->vm_stack_size);
1462
+ }
1463
+
1464
+ static inline int
1465
+ RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
1466
+ {
1467
+ return !RUBY_VM_VALID_CONTROL_FRAME_P(cfp, RUBY_VM_END_CONTROL_FRAME(ec));
1468
+ }
1469
+
1470
+ static inline int
1471
+ VM_BH_ISEQ_BLOCK_P(VALUE block_handler)
1472
+ {
1473
+ if ((block_handler & 0x03) == 0x01) {
1474
+ #if VM_CHECK_MODE > 0
1475
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1476
+ VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq));
1477
+ #endif
1478
+ return 1;
1479
+ }
1480
+ else {
1481
+ return 0;
1482
+ }
1483
+ }
1484
+
1485
+ static inline VALUE
1486
+ VM_BH_FROM_ISEQ_BLOCK(const struct rb_captured_block *captured)
1487
+ {
1488
+ VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x01);
1489
+ VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1490
+ return block_handler;
1491
+ }
1492
+
1493
+ static inline const struct rb_captured_block *
1494
+ VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
1495
+ {
1496
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1497
+ VM_ASSERT(VM_BH_ISEQ_BLOCK_P(block_handler));
1498
+ return captured;
1499
+ }
1500
+
1501
+ static inline int
1502
+ VM_BH_IFUNC_P(VALUE block_handler)
1503
+ {
1504
+ if ((block_handler & 0x03) == 0x03) {
1505
+ #if VM_CHECK_MODE > 0
1506
+ struct rb_captured_block *captured = (void *)(block_handler & ~0x03);
1507
+ VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc));
1508
+ #endif
1509
+ return 1;
1510
+ }
1511
+ else {
1512
+ return 0;
1513
+ }
1514
+ }
1515
+
1516
+ static inline VALUE
1517
+ VM_BH_FROM_IFUNC_BLOCK(const struct rb_captured_block *captured)
1518
+ {
1519
+ VALUE block_handler = VM_TAGGED_PTR_SET(captured, 0x03);
1520
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1521
+ return block_handler;
1522
+ }
1523
+
1524
+ static inline const struct rb_captured_block *
1525
+ VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
1526
+ {
1527
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1528
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler));
1529
+ return captured;
1530
+ }
1531
+
1532
+ static inline const struct rb_captured_block *
1533
+ VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
1534
+ {
1535
+ struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03);
1536
+ VM_ASSERT(VM_BH_IFUNC_P(block_handler) || VM_BH_ISEQ_BLOCK_P(block_handler));
1537
+ return captured;
1538
+ }
1539
+
1540
+ static inline enum rb_block_handler_type
1541
+ vm_block_handler_type(VALUE block_handler)
1542
+ {
1543
+ if (VM_BH_ISEQ_BLOCK_P(block_handler)) {
1544
+ return block_handler_type_iseq;
1545
+ }
1546
+ else if (VM_BH_IFUNC_P(block_handler)) {
1547
+ return block_handler_type_ifunc;
1548
+ }
1549
+ else if (SYMBOL_P(block_handler)) {
1550
+ return block_handler_type_symbol;
1551
+ }
1552
+ else {
1553
+ VM_ASSERT(rb_obj_is_proc(block_handler));
1554
+ return block_handler_type_proc;
1555
+ }
1556
+ }
1557
+
1558
+ static inline void
1559
+ vm_block_handler_verify(MAYBE_UNUSED(VALUE block_handler))
1560
+ {
1561
+ VM_ASSERT(block_handler == VM_BLOCK_HANDLER_NONE ||
1562
+ (vm_block_handler_type(block_handler), 1));
1563
+ }
1564
+
1565
+ static inline int
1566
+ vm_cfp_forwarded_bh_p(const rb_control_frame_t *cfp, VALUE block_handler)
1567
+ {
1568
+ return ((VALUE) cfp->block_code) == block_handler;
1569
+ }
1570
+
1571
+ static inline enum rb_block_type
1572
+ vm_block_type(const struct rb_block *block)
1573
+ {
1574
+ #if VM_CHECK_MODE > 0
1575
+ switch (block->type) {
1576
+ case block_type_iseq:
1577
+ VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq));
1578
+ break;
1579
+ case block_type_ifunc:
1580
+ VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc));
1581
+ break;
1582
+ case block_type_symbol:
1583
+ VM_ASSERT(SYMBOL_P(block->as.symbol));
1584
+ break;
1585
+ case block_type_proc:
1586
+ VM_ASSERT(rb_obj_is_proc(block->as.proc));
1587
+ break;
1588
+ }
1589
+ #endif
1590
+ return block->type;
1591
+ }
1592
+
1593
+ static inline void
1594
+ vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
1595
+ {
1596
+ struct rb_block *mb = (struct rb_block *)block;
1597
+ mb->type = type;
1598
+ }
1599
+
1600
+ static inline const struct rb_block *
1601
+ vm_proc_block(VALUE procval)
1602
+ {
1603
+ VM_ASSERT(rb_obj_is_proc(procval));
1604
+ return &((rb_proc_t *)RTYPEDDATA_DATA(procval))->block;
1605
+ }
1606
+
1607
+ static inline const rb_iseq_t *vm_block_iseq(const struct rb_block *block);
1608
+ static inline const VALUE *vm_block_ep(const struct rb_block *block);
1609
+
1610
+ static inline const rb_iseq_t *
1611
+ vm_proc_iseq(VALUE procval)
1612
+ {
1613
+ return vm_block_iseq(vm_proc_block(procval));
1614
+ }
1615
+
1616
+ static inline const VALUE *
1617
+ vm_proc_ep(VALUE procval)
1618
+ {
1619
+ return vm_block_ep(vm_proc_block(procval));
1620
+ }
1621
+
1622
+ static inline const rb_iseq_t *
1623
+ vm_block_iseq(const struct rb_block *block)
1624
+ {
1625
+ switch (vm_block_type(block)) {
1626
+ case block_type_iseq: return rb_iseq_check(block->as.captured.code.iseq);
1627
+ case block_type_proc: return vm_proc_iseq(block->as.proc);
1628
+ case block_type_ifunc:
1629
+ case block_type_symbol: return NULL;
1630
+ }
1631
+ VM_UNREACHABLE(vm_block_iseq);
1632
+ return NULL;
1633
+ }
1634
+
1635
+ static inline const VALUE *
1636
+ vm_block_ep(const struct rb_block *block)
1637
+ {
1638
+ switch (vm_block_type(block)) {
1639
+ case block_type_iseq:
1640
+ case block_type_ifunc: return block->as.captured.ep;
1641
+ case block_type_proc: return vm_proc_ep(block->as.proc);
1642
+ case block_type_symbol: return NULL;
1643
+ }
1644
+ VM_UNREACHABLE(vm_block_ep);
1645
+ return NULL;
1646
+ }
1647
+
1648
+ static inline VALUE
1649
+ vm_block_self(const struct rb_block *block)
1650
+ {
1651
+ switch (vm_block_type(block)) {
1652
+ case block_type_iseq:
1653
+ case block_type_ifunc:
1654
+ return block->as.captured.self;
1655
+ case block_type_proc:
1656
+ return vm_block_self(vm_proc_block(block->as.proc));
1657
+ case block_type_symbol:
1658
+ return Qundef;
1659
+ }
1660
+ VM_UNREACHABLE(vm_block_self);
1661
+ return Qundef;
1662
+ }
1663
+
1664
+ static inline VALUE
1665
+ VM_BH_TO_SYMBOL(VALUE block_handler)
1666
+ {
1667
+ VM_ASSERT(SYMBOL_P(block_handler));
1668
+ return block_handler;
1669
+ }
1670
+
1671
+ static inline VALUE
1672
+ VM_BH_FROM_SYMBOL(VALUE symbol)
1673
+ {
1674
+ VM_ASSERT(SYMBOL_P(symbol));
1675
+ return symbol;
1676
+ }
1677
+
1678
+ static inline VALUE
1679
+ VM_BH_TO_PROC(VALUE block_handler)
1680
+ {
1681
+ VM_ASSERT(rb_obj_is_proc(block_handler));
1682
+ return block_handler;
1683
+ }
1684
+
1685
+ static inline VALUE
1686
+ VM_BH_FROM_PROC(VALUE procval)
1687
+ {
1688
+ VM_ASSERT(rb_obj_is_proc(procval));
1689
+ return procval;
1690
+ }
1691
+
1692
+ /* VM related object allocate functions */
1693
+ VALUE rb_thread_alloc(VALUE klass);
1694
+ VALUE rb_binding_alloc(VALUE klass);
1695
+ VALUE rb_proc_alloc(VALUE klass);
1696
+ VALUE rb_proc_dup(VALUE self);
1697
+
1698
+ /* for debug */
1699
+ extern void rb_vmdebug_stack_dump_raw(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1700
+ extern void rb_vmdebug_debug_print_pre(const rb_execution_context_t *ec, const rb_control_frame_t *cfp, const VALUE *_pc);
1701
+ extern void rb_vmdebug_debug_print_post(const rb_execution_context_t *ec, const rb_control_frame_t *cfp
1702
+ #if OPT_STACK_CACHING
1703
+ , VALUE reg_a, VALUE reg_b
1704
+ #endif
1705
+ );
1706
+
1707
+ #define SDR() rb_vmdebug_stack_dump_raw(GET_EC(), GET_EC()->cfp)
1708
+ #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_EC(), (cfp))
1709
+ void rb_vm_bugreport(const void *);
1710
+ typedef void (*ruby_sighandler_t)(int);
1711
+ RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 5)
1712
+ NORETURN(void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *, const char *fmt, ...));
1713
+
1714
+ /* functions about thread/vm execution */
1715
+ RUBY_SYMBOL_EXPORT_BEGIN
1716
+ VALUE rb_iseq_eval(const rb_iseq_t *iseq);
1717
+ VALUE rb_iseq_eval_main(const rb_iseq_t *iseq);
1718
+ VALUE rb_iseq_path(const rb_iseq_t *iseq);
1719
+ VALUE rb_iseq_realpath(const rb_iseq_t *iseq);
1720
+ RUBY_SYMBOL_EXPORT_END
1721
+
1722
+ VALUE rb_iseq_pathobj_new(VALUE path, VALUE realpath);
1723
+ void rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath);
1724
+
1725
+ int rb_ec_frame_method_id_and_class(const rb_execution_context_t *ec, ID *idp, ID *called_idp, VALUE *klassp);
1726
+ void rb_ec_setup_exception(const rb_execution_context_t *ec, VALUE mesg, VALUE cause);
1727
+
1728
+ VALUE rb_vm_invoke_proc(rb_execution_context_t *ec, rb_proc_t *proc, int argc, const VALUE *argv, int kw_splat, VALUE block_handler);
1729
+
1730
+ VALUE rb_vm_make_proc_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda);
1731
+ static inline VALUE
1732
+ rb_vm_make_proc(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1733
+ {
1734
+ return rb_vm_make_proc_lambda(ec, captured, klass, 0);
1735
+ }
1736
+
1737
+ static inline VALUE
1738
+ rb_vm_make_lambda(const rb_execution_context_t *ec, const struct rb_captured_block *captured, VALUE klass)
1739
+ {
1740
+ return rb_vm_make_proc_lambda(ec, captured, klass, 1);
1741
+ }
1742
+
1743
+ VALUE rb_vm_make_binding(const rb_execution_context_t *ec, const rb_control_frame_t *src_cfp);
1744
+ VALUE rb_vm_env_local_variables(const rb_env_t *env);
1745
+ const rb_env_t *rb_vm_env_prev_env(const rb_env_t *env);
1746
+ const VALUE *rb_binding_add_dynavars(VALUE bindval, rb_binding_t *bind, int dyncount, const ID *dynvars);
1747
+ void rb_vm_inc_const_missing_count(void);
1748
+ VALUE rb_vm_call_kw(rb_execution_context_t *ec, VALUE recv, VALUE id, int argc,
1749
+ const VALUE *argv, const rb_callable_method_entry_t *me, int kw_splat);
1750
+ MJIT_STATIC void rb_vm_pop_frame(rb_execution_context_t *ec);
1751
+
1752
+ void rb_thread_start_timer_thread(void);
1753
+ void rb_thread_stop_timer_thread(void);
1754
+ void rb_thread_reset_timer_thread(void);
1755
+ void rb_thread_wakeup_timer_thread(int);
1756
+
1757
+ static inline void
1758
+ rb_vm_living_threads_init(rb_vm_t *vm)
1759
+ {
1760
+ ccan_list_head_init(&vm->waiting_fds);
1761
+ ccan_list_head_init(&vm->waiting_pids);
1762
+ ccan_list_head_init(&vm->workqueue);
1763
+ ccan_list_head_init(&vm->waiting_grps);
1764
+ ccan_list_head_init(&vm->ractor.set);
1765
+ }
1766
+
1767
+ typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
1768
+ rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1769
+ rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp);
1770
+ int rb_vm_get_sourceline(const rb_control_frame_t *);
1771
+ void rb_vm_stack_to_heap(rb_execution_context_t *ec);
1772
+ void ruby_thread_init_stack(rb_thread_t *th);
1773
+ rb_thread_t * ruby_thread_from_native(void);
1774
+ int ruby_thread_set_native(rb_thread_t *th);
1775
+ int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, ID *called_idp, VALUE *klassp);
1776
+ void rb_vm_rewind_cfp(rb_execution_context_t *ec, rb_control_frame_t *cfp);
1777
+ MJIT_STATIC VALUE rb_vm_bh_to_procval(const rb_execution_context_t *ec, VALUE block_handler);
1778
+
1779
+ void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE exception_class, VALUE mesg);
1780
+
1781
+ #define rb_vm_register_special_exception(sp, e, m) \
1782
+ rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
1783
+
1784
+ void rb_gc_mark_machine_stack(const rb_execution_context_t *ec);
1785
+
1786
+ void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);
1787
+
1788
+ MJIT_STATIC const rb_callable_method_entry_t *rb_vm_frame_method_entry(const rb_control_frame_t *cfp);
1789
+
1790
+ #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
1791
+
1792
+ #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) do { \
1793
+ STATIC_ASSERT(sizeof_sp, sizeof(*(sp)) == sizeof(VALUE)); \
1794
+ STATIC_ASSERT(sizeof_cfp, sizeof(*(cfp)) == sizeof(rb_control_frame_t)); \
1795
+ const struct rb_control_frame_struct *bound = (void *)&(sp)[(margin)]; \
1796
+ if (UNLIKELY((cfp) <= &bound[1])) { \
1797
+ vm_stackoverflow(); \
1798
+ } \
1799
+ } while (0)
1800
+
1801
+ #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
1802
+ CHECK_VM_STACK_OVERFLOW0((cfp), (cfp)->sp, (margin))
1803
+
1804
+ VALUE rb_catch_protect(VALUE t, rb_block_call_func *func, VALUE data, enum ruby_tag_type *stateptr);
1805
+
1806
+ rb_execution_context_t *rb_vm_main_ractor_ec(rb_vm_t *vm); // ractor.c
1807
+
1808
+ /* for thread */
1809
+
1810
+ #if RUBY_VM_THREAD_MODEL == 2
1811
+ MJIT_SYMBOL_EXPORT_BEGIN
1812
+
1813
+ RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
1814
+ RUBY_EXTERN rb_vm_t *ruby_current_vm_ptr;
1815
+ RUBY_EXTERN rb_event_flag_t ruby_vm_event_flags;
1816
+ RUBY_EXTERN rb_event_flag_t ruby_vm_event_enabled_global_flags;
1817
+ RUBY_EXTERN unsigned int ruby_vm_event_local_num;
1818
+
1819
+ MJIT_SYMBOL_EXPORT_END
1820
+
1821
+ #define GET_VM() rb_current_vm()
1822
+ #define GET_RACTOR() rb_current_ractor()
1823
+ #define GET_THREAD() rb_current_thread()
1824
+ #define GET_EC() rb_current_execution_context(true)
1825
+
1826
+ static inline rb_thread_t *
1827
+ rb_ec_thread_ptr(const rb_execution_context_t *ec)
1828
+ {
1829
+ return ec->thread_ptr;
1830
+ }
1831
+
1832
+ static inline rb_ractor_t *
1833
+ rb_ec_ractor_ptr(const rb_execution_context_t *ec)
1834
+ {
1835
+ const rb_thread_t *th = rb_ec_thread_ptr(ec);
1836
+ if (th) {
1837
+ VM_ASSERT(th->ractor != NULL);
1838
+ return th->ractor;
1839
+ }
1840
+ else {
1841
+ return NULL;
1842
+ }
1843
+ }
1844
+
1845
+ static inline rb_vm_t *
1846
+ rb_ec_vm_ptr(const rb_execution_context_t *ec)
1847
+ {
1848
+ const rb_thread_t *th = rb_ec_thread_ptr(ec);
1849
+ if (th) {
1850
+ return th->vm;
1851
+ }
1852
+ else {
1853
+ return NULL;
1854
+ }
1855
+ }
1856
+
1857
+ static inline rb_execution_context_t *
1858
+ rb_current_execution_context(bool expect_ec)
1859
+ {
1860
+ #ifdef RB_THREAD_LOCAL_SPECIFIER
1861
+ #ifdef __APPLE__
1862
+ rb_execution_context_t *ec = rb_current_ec();
1863
+ #else
1864
+ rb_execution_context_t *ec = ruby_current_ec;
1865
+ #endif
1866
+ #else
1867
+ rb_execution_context_t *ec = native_tls_get(ruby_current_ec_key);
1868
+ #endif
1869
+ VM_ASSERT(!expect_ec || ec != NULL);
1870
+ return ec;
1871
+ }
1872
+
1873
+ static inline rb_thread_t *
1874
+ rb_current_thread(void)
1875
+ {
1876
+ const rb_execution_context_t *ec = GET_EC();
1877
+ return rb_ec_thread_ptr(ec);
1878
+ }
1879
+
1880
+ static inline rb_ractor_t *
1881
+ rb_current_ractor(void)
1882
+ {
1883
+ if (ruby_single_main_ractor) {
1884
+ return ruby_single_main_ractor;
1885
+ }
1886
+ else {
1887
+ const rb_execution_context_t *ec = GET_EC();
1888
+ return rb_ec_ractor_ptr(ec);
1889
+ }
1890
+ }
1891
+
1892
+ static inline rb_vm_t *
1893
+ rb_current_vm(void)
1894
+ {
1895
+ #if 0 // TODO: reconsider the assertions
1896
+ VM_ASSERT(ruby_current_vm_ptr == NULL ||
1897
+ ruby_current_execution_context_ptr == NULL ||
1898
+ rb_ec_thread_ptr(GET_EC()) == NULL ||
1899
+ rb_ec_thread_ptr(GET_EC())->status == THREAD_KILLED ||
1900
+ rb_ec_vm_ptr(GET_EC()) == ruby_current_vm_ptr);
1901
+ #endif
1902
+
1903
+ return ruby_current_vm_ptr;
1904
+ }
1905
+
1906
+ void rb_ec_vm_lock_rec_release(const rb_execution_context_t *ec,
1907
+ unsigned int recorded_lock_rec,
1908
+ unsigned int current_lock_rec);
1909
+
1910
+ static inline unsigned int
1911
+ rb_ec_vm_lock_rec(const rb_execution_context_t *ec)
1912
+ {
1913
+ rb_vm_t *vm = rb_ec_vm_ptr(ec);
1914
+
1915
+ if (vm->ractor.sync.lock_owner != rb_ec_ractor_ptr(ec)) {
1916
+ return 0;
1917
+ }
1918
+ else {
1919
+ return vm->ractor.sync.lock_rec;
1920
+ }
1921
+ }
1922
+
1923
+ #else
1924
+ #error "unsupported thread model"
1925
+ #endif
1926
+
1927
+ enum {
1928
+ TIMER_INTERRUPT_MASK = 0x01,
1929
+ PENDING_INTERRUPT_MASK = 0x02,
1930
+ POSTPONED_JOB_INTERRUPT_MASK = 0x04,
1931
+ TRAP_INTERRUPT_MASK = 0x08,
1932
+ TERMINATE_INTERRUPT_MASK = 0x10,
1933
+ VM_BARRIER_INTERRUPT_MASK = 0x20,
1934
+ };
1935
+
1936
+ #define RUBY_VM_SET_TIMER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TIMER_INTERRUPT_MASK)
1937
+ #define RUBY_VM_SET_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, PENDING_INTERRUPT_MASK)
1938
+ #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
1939
+ #define RUBY_VM_SET_TRAP_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TRAP_INTERRUPT_MASK)
1940
+ #define RUBY_VM_SET_TERMINATE_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, TERMINATE_INTERRUPT_MASK)
1941
+ #define RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec) ATOMIC_OR((ec)->interrupt_flag, VM_BARRIER_INTERRUPT_MASK)
1942
+ #define RUBY_VM_INTERRUPTED(ec) ((ec)->interrupt_flag & ~(ec)->interrupt_mask & \
1943
+ (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
1944
+
1945
+ static inline bool
1946
+ RUBY_VM_INTERRUPTED_ANY(rb_execution_context_t *ec)
1947
+ {
1948
+ #if defined(USE_VM_CLOCK) && USE_VM_CLOCK
1949
+ uint32_t current_clock = rb_ec_vm_ptr(ec)->clock;
1950
+
1951
+ if (current_clock != ec->checked_clock) {
1952
+ ec->checked_clock = current_clock;
1953
+ RUBY_VM_SET_TIMER_INTERRUPT(ec);
1954
+ }
1955
+ #endif
1956
+ return ec->interrupt_flag & ~(ec)->interrupt_mask;
1957
+ }
1958
+
1959
+ VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt);
1960
+ int rb_signal_buff_size(void);
1961
+ int rb_signal_exec(rb_thread_t *th, int sig);
1962
+ void rb_threadptr_check_signal(rb_thread_t *mth);
1963
+ void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
1964
+ void rb_threadptr_signal_exit(rb_thread_t *th);
1965
+ int rb_threadptr_execute_interrupts(rb_thread_t *, int);
1966
+ void rb_threadptr_interrupt(rb_thread_t *th);
1967
+ void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
1968
+ void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
1969
+ void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
1970
+ VALUE rb_ec_get_errinfo(const rb_execution_context_t *ec);
1971
+ void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo);
1972
+ void rb_execution_context_update(rb_execution_context_t *ec);
1973
+ void rb_execution_context_mark(const rb_execution_context_t *ec);
1974
+ void rb_fiber_close(rb_fiber_t *fib);
1975
+ void Init_native_thread(rb_thread_t *th);
1976
+ int rb_vm_check_ints_blocking(rb_execution_context_t *ec);
1977
+
1978
+ // vm_sync.h
1979
+ void rb_vm_cond_wait(rb_vm_t *vm, rb_nativethread_cond_t *cond);
1980
+ void rb_vm_cond_timedwait(rb_vm_t *vm, rb_nativethread_cond_t *cond, unsigned long msec);
1981
+
1982
+ #define RUBY_VM_CHECK_INTS(ec) rb_vm_check_ints(ec)
1983
+ static inline void
1984
+ rb_vm_check_ints(rb_execution_context_t *ec)
1985
+ {
1986
+ #ifdef RUBY_ASSERT_CRITICAL_SECTION
1987
+ VM_ASSERT(ruby_assert_critical_section_entered == 0);
1988
+ #endif
1989
+
1990
+ VM_ASSERT(ec == GET_EC());
1991
+
1992
+ if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(ec))) {
1993
+ rb_threadptr_execute_interrupts(rb_ec_thread_ptr(ec), 0);
1994
+ }
1995
+ }
1996
+
1997
+ /* tracer */
1998
+
1999
+ struct rb_trace_arg_struct {
2000
+ rb_event_flag_t event;
2001
+ rb_execution_context_t *ec;
2002
+ const rb_control_frame_t *cfp;
2003
+ VALUE self;
2004
+ ID id;
2005
+ ID called_id;
2006
+ VALUE klass;
2007
+ VALUE data;
2008
+
2009
+ int klass_solved;
2010
+
2011
+ /* calc from cfp */
2012
+ int lineno;
2013
+ VALUE path;
2014
+ };
2015
+
2016
+ void rb_hook_list_mark(rb_hook_list_t *hooks);
2017
+ void rb_hook_list_free(rb_hook_list_t *hooks);
2018
+ void rb_hook_list_connect_tracepoint(VALUE target, rb_hook_list_t *list, VALUE tpval, unsigned int target_line);
2019
+ void rb_hook_list_remove_tracepoint(rb_hook_list_t *list, VALUE tpval);
2020
+
2021
+ void rb_exec_event_hooks(struct rb_trace_arg_struct *trace_arg, rb_hook_list_t *hooks, int pop_p);
2022
+
2023
+ #define EXEC_EVENT_HOOK_ORIG(ec_, hooks_, flag_, self_, id_, called_id_, klass_, data_, pop_p_) do { \
2024
+ const rb_event_flag_t flag_arg_ = (flag_); \
2025
+ rb_hook_list_t *hooks_arg_ = (hooks_); \
2026
+ if (UNLIKELY((hooks_arg_)->events & (flag_arg_))) { \
2027
+ /* defer evaluating the other arguments */ \
2028
+ rb_exec_event_hook_orig(ec_, hooks_arg_, flag_arg_, self_, id_, called_id_, klass_, data_, pop_p_); \
2029
+ } \
2030
+ } while (0)
2031
+
2032
+ static inline void
2033
+ rb_exec_event_hook_orig(rb_execution_context_t *ec, rb_hook_list_t *hooks, rb_event_flag_t flag,
2034
+ VALUE self, ID id, ID called_id, VALUE klass, VALUE data, int pop_p)
2035
+ {
2036
+ struct rb_trace_arg_struct trace_arg;
2037
+
2038
+ VM_ASSERT((hooks->events & flag) != 0);
2039
+
2040
+ trace_arg.event = flag;
2041
+ trace_arg.ec = ec;
2042
+ trace_arg.cfp = ec->cfp;
2043
+ trace_arg.self = self;
2044
+ trace_arg.id = id;
2045
+ trace_arg.called_id = called_id;
2046
+ trace_arg.klass = klass;
2047
+ trace_arg.data = data;
2048
+ trace_arg.path = Qundef;
2049
+ trace_arg.klass_solved = 0;
2050
+
2051
+ rb_exec_event_hooks(&trace_arg, hooks, pop_p);
2052
+ }
2053
+
2054
+ struct rb_ractor_pub {
2055
+ VALUE self;
2056
+ uint32_t id;
2057
+ rb_hook_list_t hooks;
2058
+ };
2059
+
2060
+ static inline rb_hook_list_t *
2061
+ rb_ec_ractor_hooks(const rb_execution_context_t *ec)
2062
+ {
2063
+ struct rb_ractor_pub *cr_pub = (struct rb_ractor_pub *)rb_ec_ractor_ptr(ec);
2064
+ return &cr_pub->hooks;
2065
+ }
2066
+
2067
+ #define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2068
+ EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 0)
2069
+
2070
+ #define EXEC_EVENT_HOOK_AND_POP_FRAME(ec_, flag_, self_, id_, called_id_, klass_, data_) \
2071
+ EXEC_EVENT_HOOK_ORIG(ec_, rb_ec_ractor_hooks(ec_), flag_, self_, id_, called_id_, klass_, data_, 1)
2072
+
2073
+ static inline void
2074
+ rb_exec_event_hook_script_compiled(rb_execution_context_t *ec, const rb_iseq_t *iseq, VALUE eval_script)
2075
+ {
2076
+ EXEC_EVENT_HOOK(ec, RUBY_EVENT_SCRIPT_COMPILED, ec->cfp->self, 0, 0, 0,
2077
+ NIL_P(eval_script) ? (VALUE)iseq :
2078
+ rb_ary_new_from_args(2, eval_script, (VALUE)iseq));
2079
+ }
2080
+
2081
+ void rb_vm_trap_exit(rb_vm_t *vm);
2082
+
2083
+ RUBY_SYMBOL_EXPORT_BEGIN
2084
+
2085
+ int rb_thread_check_trap_pending(void);
2086
+
2087
+ /* #define RUBY_EVENT_RESERVED_FOR_INTERNAL_USE 0x030000 */ /* from vm_core.h */
2088
+ #define RUBY_EVENT_COVERAGE_LINE 0x010000
2089
+ #define RUBY_EVENT_COVERAGE_BRANCH 0x020000
2090
+
2091
+ extern VALUE rb_get_coverages(void);
2092
+ extern void rb_set_coverages(VALUE, int, VALUE);
2093
+ extern void rb_clear_coverages(void);
2094
+ extern void rb_reset_coverages(void);
2095
+ extern void rb_resume_coverages(void);
2096
+ extern void rb_suspend_coverages(void);
2097
+
2098
+ void rb_postponed_job_flush(rb_vm_t *vm);
2099
+
2100
+ // ractor.c
2101
+ RUBY_EXTERN VALUE rb_eRactorUnsafeError;
2102
+ RUBY_EXTERN VALUE rb_eRactorIsolationError;
2103
+
2104
+ RUBY_SYMBOL_EXPORT_END
2105
+
2106
+ #endif /* RUBY_VM_CORE_H */