@downdraft/native-mobile-android-arm64 0.0.0 → 0.1.1

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 (138) hide show
  1. package/README.md +7 -0
  2. package/include/node/cppgc/allocation.h +308 -0
  3. package/include/node/cppgc/common.h +28 -0
  4. package/include/node/cppgc/cross-thread-persistent.h +462 -0
  5. package/include/node/cppgc/custom-space.h +97 -0
  6. package/include/node/cppgc/default-platform.h +68 -0
  7. package/include/node/cppgc/explicit-management.h +100 -0
  8. package/include/node/cppgc/garbage-collected.h +114 -0
  9. package/include/node/cppgc/heap-consistency.h +309 -0
  10. package/include/node/cppgc/heap-handle.h +48 -0
  11. package/include/node/cppgc/heap-state.h +82 -0
  12. package/include/node/cppgc/heap-statistics.h +126 -0
  13. package/include/node/cppgc/heap.h +202 -0
  14. package/include/node/cppgc/internal/api-constants.h +75 -0
  15. package/include/node/cppgc/internal/atomic-entry-flag.h +48 -0
  16. package/include/node/cppgc/internal/base-page-handle.h +43 -0
  17. package/include/node/cppgc/internal/caged-heap-local-data.h +117 -0
  18. package/include/node/cppgc/internal/caged-heap.h +64 -0
  19. package/include/node/cppgc/internal/compiler-specific.h +46 -0
  20. package/include/node/cppgc/internal/conditional-stack-allocated.h +41 -0
  21. package/include/node/cppgc/internal/finalizer-trait.h +93 -0
  22. package/include/node/cppgc/internal/gc-info.h +150 -0
  23. package/include/node/cppgc/internal/logging.h +50 -0
  24. package/include/node/cppgc/internal/member-storage.h +305 -0
  25. package/include/node/cppgc/internal/name-trait.h +141 -0
  26. package/include/node/cppgc/internal/persistent-node.h +219 -0
  27. package/include/node/cppgc/internal/pointer-policies.h +280 -0
  28. package/include/node/cppgc/internal/write-barrier.h +487 -0
  29. package/include/node/cppgc/liveness-broker.h +78 -0
  30. package/include/node/cppgc/macros.h +56 -0
  31. package/include/node/cppgc/member.h +669 -0
  32. package/include/node/cppgc/name-provider.h +75 -0
  33. package/include/node/cppgc/object-size-trait.h +58 -0
  34. package/include/node/cppgc/persistent.h +376 -0
  35. package/include/node/cppgc/platform.h +174 -0
  36. package/include/node/cppgc/prefinalizer.h +75 -0
  37. package/include/node/cppgc/process-heap-statistics.h +36 -0
  38. package/include/node/cppgc/sentinel-pointer.h +39 -0
  39. package/include/node/cppgc/source-location.h +16 -0
  40. package/include/node/cppgc/tagged-member.h +114 -0
  41. package/include/node/cppgc/testing.h +106 -0
  42. package/include/node/cppgc/trace-trait.h +127 -0
  43. package/include/node/cppgc/type-traits.h +272 -0
  44. package/include/node/cppgc/visitor.h +534 -0
  45. package/include/node/js_native_api.h +634 -0
  46. package/include/node/js_native_api_types.h +244 -0
  47. package/include/node/libplatform/libplatform-export.h +29 -0
  48. package/include/node/libplatform/libplatform.h +112 -0
  49. package/include/node/libplatform/v8-tracing.h +343 -0
  50. package/include/node/node.h +1744 -0
  51. package/include/node/node_api.h +265 -0
  52. package/include/node/node_api_types.h +58 -0
  53. package/include/node/node_buffer.h +94 -0
  54. package/include/node/node_object_wrap.h +145 -0
  55. package/include/node/node_version.h +116 -0
  56. package/include/node/uv/aix.h +32 -0
  57. package/include/node/uv/bsd.h +34 -0
  58. package/include/node/uv/darwin.h +61 -0
  59. package/include/node/uv/errno.h +483 -0
  60. package/include/node/uv/linux.h +34 -0
  61. package/include/node/uv/os390.h +33 -0
  62. package/include/node/uv/posix.h +31 -0
  63. package/include/node/uv/sunos.h +44 -0
  64. package/include/node/uv/threadpool.h +37 -0
  65. package/include/node/uv/tree.h +521 -0
  66. package/include/node/uv/unix.h +509 -0
  67. package/include/node/uv/version.h +43 -0
  68. package/include/node/uv/win.h +698 -0
  69. package/include/node/uv.h +1994 -0
  70. package/include/node/v8-array-buffer.h +646 -0
  71. package/include/node/v8-callbacks.h +533 -0
  72. package/include/node/v8-container.h +183 -0
  73. package/include/node/v8-context.h +580 -0
  74. package/include/node/v8-cpp-heap-external.h +56 -0
  75. package/include/node/v8-cppgc.h +194 -0
  76. package/include/node/v8-data.h +96 -0
  77. package/include/node/v8-date.h +57 -0
  78. package/include/node/v8-debug.h +210 -0
  79. package/include/node/v8-embedder-heap.h +54 -0
  80. package/include/node/v8-embedder-state-scope.h +52 -0
  81. package/include/node/v8-exception.h +298 -0
  82. package/include/node/v8-extension.h +60 -0
  83. package/include/node/v8-external-memory-accounter.h +60 -0
  84. package/include/node/v8-external.h +73 -0
  85. package/include/node/v8-fast-api-calls.h +784 -0
  86. package/include/node/v8-forward.h +82 -0
  87. package/include/node/v8-function-callback.h +708 -0
  88. package/include/node/v8-function.h +148 -0
  89. package/include/node/v8-handle-base.h +155 -0
  90. package/include/node/v8-initialization.h +343 -0
  91. package/include/node/v8-inspector-protocol.h +13 -0
  92. package/include/node/v8-inspector.h +458 -0
  93. package/include/node/v8-internal.h +1827 -0
  94. package/include/node/v8-isolate.h +1925 -0
  95. package/include/node/v8-json.h +54 -0
  96. package/include/node/v8-local-handle.h +884 -0
  97. package/include/node/v8-locker.h +138 -0
  98. package/include/node/v8-maybe.h +185 -0
  99. package/include/node/v8-memory-span.h +273 -0
  100. package/include/node/v8-message.h +215 -0
  101. package/include/node/v8-metrics.h +273 -0
  102. package/include/node/v8-microtask-queue.h +152 -0
  103. package/include/node/v8-microtask.h +28 -0
  104. package/include/node/v8-object.h +1134 -0
  105. package/include/node/v8-persistent-handle.h +556 -0
  106. package/include/node/v8-platform.h +1552 -0
  107. package/include/node/v8-primitive-object.h +118 -0
  108. package/include/node/v8-primitive.h +1056 -0
  109. package/include/node/v8-profiler.h +1401 -0
  110. package/include/node/v8-promise.h +185 -0
  111. package/include/node/v8-proxy.h +49 -0
  112. package/include/node/v8-regexp.h +105 -0
  113. package/include/node/v8-sandbox.h +170 -0
  114. package/include/node/v8-script.h +996 -0
  115. package/include/node/v8-snapshot.h +302 -0
  116. package/include/node/v8-source-location.h +91 -0
  117. package/include/node/v8-statistics.h +246 -0
  118. package/include/node/v8-template.h +1096 -0
  119. package/include/node/v8-trace-categories.h +28 -0
  120. package/include/node/v8-traced-handle.h +420 -0
  121. package/include/node/v8-typed-array.h +386 -0
  122. package/include/node/v8-unwinder-state.h +30 -0
  123. package/include/node/v8-unwinder.h +146 -0
  124. package/include/node/v8-util.h +549 -0
  125. package/include/node/v8-value-serializer-version.h +24 -0
  126. package/include/node/v8-value-serializer.h +316 -0
  127. package/include/node/v8-value.h +674 -0
  128. package/include/node/v8-version-string.h +38 -0
  129. package/include/node/v8-version.h +20 -0
  130. package/include/node/v8-wasm-trap-handler-posix.h +31 -0
  131. package/include/node/v8-wasm-trap-handler-win.h +28 -0
  132. package/include/node/v8-wasm.h +325 -0
  133. package/include/node/v8-weak-callback-info.h +88 -0
  134. package/include/node/v8.h +88 -0
  135. package/include/node/v8config.h +1077 -0
  136. package/lib/libc++_shared.so +0 -0
  137. package/lib/libnode.so +4 -0
  138. package/package.json +23 -3
@@ -0,0 +1,1827 @@
1
+ // Copyright 2018 the V8 project authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ #ifndef INCLUDE_V8_INTERNAL_H_
6
+ #define INCLUDE_V8_INTERNAL_H_
7
+
8
+ #include <stddef.h>
9
+ #include <stdint.h>
10
+ #include <string.h>
11
+
12
+ #include <atomic>
13
+ #include <compare>
14
+ #include <concepts>
15
+ #include <iterator>
16
+ #include <limits>
17
+ #include <memory>
18
+ #include <optional>
19
+ #include <type_traits>
20
+
21
+ #include "v8config.h" // NOLINT(build/include_directory)
22
+
23
+ namespace v8 {
24
+
25
+ class Array;
26
+ class Context;
27
+ class Data;
28
+ class Isolate;
29
+
30
+ namespace internal {
31
+
32
+ class Heap;
33
+ class LocalHeap;
34
+ class Isolate;
35
+ class IsolateGroup;
36
+ class LocalIsolate;
37
+
38
+ typedef uintptr_t Address;
39
+ static constexpr Address kNullAddress = 0;
40
+
41
+ constexpr int KB = 1024;
42
+ constexpr int MB = KB * 1024;
43
+ constexpr int GB = MB * 1024;
44
+ #ifdef V8_TARGET_ARCH_X64
45
+ constexpr size_t TB = size_t{GB} * 1024;
46
+ #endif
47
+
48
+ /**
49
+ * Configuration of tagging scheme.
50
+ */
51
+ const int kApiSystemPointerSize = sizeof(void*);
52
+ const int kApiDoubleSize = sizeof(double);
53
+ const int kApiInt32Size = sizeof(int32_t);
54
+ const int kApiInt64Size = sizeof(int64_t);
55
+ const int kApiSizetSize = sizeof(size_t);
56
+
57
+ // Tag information for HeapObject.
58
+ const int kHeapObjectTag = 1;
59
+ const int kWeakHeapObjectTag = 3;
60
+ const int kHeapObjectTagSize = 2;
61
+ const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
62
+ const intptr_t kHeapObjectReferenceTagMask = 1 << (kHeapObjectTagSize - 1);
63
+
64
+ // Tag information for fowarding pointers stored in object headers.
65
+ // 0b00 at the lowest 2 bits in the header indicates that the map word is a
66
+ // forwarding pointer.
67
+ const int kForwardingTag = 0;
68
+ const int kForwardingTagSize = 2;
69
+ const intptr_t kForwardingTagMask = (1 << kForwardingTagSize) - 1;
70
+
71
+ // Tag information for Smi.
72
+ const int kSmiTag = 0;
73
+ const int kSmiTagSize = 1;
74
+ const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
75
+
76
+ template <size_t tagged_ptr_size>
77
+ struct SmiTagging;
78
+
79
+ constexpr intptr_t kIntptrAllBitsSet = intptr_t{-1};
80
+ constexpr uintptr_t kUintptrAllBitsSet =
81
+ static_cast<uintptr_t>(kIntptrAllBitsSet);
82
+
83
+ // Smi constants for systems where tagged pointer is a 32-bit value.
84
+ template <>
85
+ struct SmiTagging<4> {
86
+ enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
87
+
88
+ static constexpr intptr_t kSmiMinValue =
89
+ static_cast<intptr_t>(kUintptrAllBitsSet << (kSmiValueSize - 1));
90
+ static constexpr intptr_t kSmiMaxValue = -(kSmiMinValue + 1);
91
+
92
+ V8_INLINE static constexpr int SmiToInt(Address value) {
93
+ int shift_bits = kSmiTagSize + kSmiShiftSize;
94
+ // Truncate and shift down (requires >> to be sign extending).
95
+ return static_cast<int32_t>(static_cast<uint32_t>(value)) >> shift_bits;
96
+ }
97
+
98
+ template <class T, typename std::enable_if_t<std::is_integral_v<T> &&
99
+ std::is_signed_v<T>>* = nullptr>
100
+ V8_INLINE static constexpr bool IsValidSmi(T value) {
101
+ // Is value in range [kSmiMinValue, kSmiMaxValue].
102
+ // Use unsigned operations in order to avoid undefined behaviour in case of
103
+ // signed integer overflow.
104
+ return (static_cast<uintptr_t>(value) -
105
+ static_cast<uintptr_t>(kSmiMinValue)) <=
106
+ (static_cast<uintptr_t>(kSmiMaxValue) -
107
+ static_cast<uintptr_t>(kSmiMinValue));
108
+ }
109
+
110
+ template <class T,
111
+ typename std::enable_if_t<std::is_integral_v<T> &&
112
+ std::is_unsigned_v<T>>* = nullptr>
113
+ V8_INLINE static constexpr bool IsValidSmi(T value) {
114
+ static_assert(kSmiMaxValue <= std::numeric_limits<uintptr_t>::max());
115
+ return value <= static_cast<uintptr_t>(kSmiMaxValue);
116
+ }
117
+
118
+ // Same as the `intptr_t` version but works with int64_t on 32-bit builds
119
+ // without slowing down anything else.
120
+ V8_INLINE static constexpr bool IsValidSmi(int64_t value) {
121
+ return (static_cast<uint64_t>(value) -
122
+ static_cast<uint64_t>(kSmiMinValue)) <=
123
+ (static_cast<uint64_t>(kSmiMaxValue) -
124
+ static_cast<uint64_t>(kSmiMinValue));
125
+ }
126
+
127
+ V8_INLINE static constexpr bool IsValidSmi(uint64_t value) {
128
+ static_assert(kSmiMaxValue <= std::numeric_limits<uint64_t>::max());
129
+ return value <= static_cast<uint64_t>(kSmiMaxValue);
130
+ }
131
+ };
132
+
133
+ // Smi constants for systems where tagged pointer is a 64-bit value.
134
+ template <>
135
+ struct SmiTagging<8> {
136
+ enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
137
+
138
+ static constexpr intptr_t kSmiMinValue =
139
+ static_cast<intptr_t>(kUintptrAllBitsSet << (kSmiValueSize - 1));
140
+ static constexpr intptr_t kSmiMaxValue = -(kSmiMinValue + 1);
141
+
142
+ V8_INLINE static constexpr int SmiToInt(Address value) {
143
+ int shift_bits = kSmiTagSize + kSmiShiftSize;
144
+ // Shift down and throw away top 32 bits.
145
+ return static_cast<int>(static_cast<intptr_t>(value) >> shift_bits);
146
+ }
147
+
148
+ template <class T, typename std::enable_if_t<std::is_integral_v<T> &&
149
+ std::is_signed_v<T>>* = nullptr>
150
+ V8_INLINE static constexpr bool IsValidSmi(T value) {
151
+ // To be representable as a long smi, the value must be a 32-bit integer.
152
+ return std::numeric_limits<int32_t>::min() <= value &&
153
+ value <= std::numeric_limits<int32_t>::max();
154
+ }
155
+
156
+ template <class T,
157
+ typename std::enable_if_t<std::is_integral_v<T> &&
158
+ std::is_unsigned_v<T>>* = nullptr>
159
+ V8_INLINE static constexpr bool IsValidSmi(T value) {
160
+ return value <= std::numeric_limits<int32_t>::max();
161
+ }
162
+ };
163
+
164
+ #ifdef V8_COMPRESS_POINTERS
165
+ // See v8:7703 or src/common/ptr-compr-inl.h for details about pointer
166
+ // compression.
167
+ constexpr size_t kPtrComprCageReservationSize = size_t{1} << 32;
168
+ constexpr size_t kPtrComprCageBaseAlignment = size_t{1} << 32;
169
+
170
+ static_assert(
171
+ kApiSystemPointerSize == kApiInt64Size,
172
+ "Pointer compression can be enabled only for 64-bit architectures");
173
+ const int kApiTaggedSize = kApiInt32Size;
174
+ #else
175
+ const int kApiTaggedSize = kApiSystemPointerSize;
176
+ #endif
177
+
178
+ constexpr bool PointerCompressionIsEnabled() {
179
+ return kApiTaggedSize != kApiSystemPointerSize;
180
+ }
181
+
182
+ #ifdef V8_31BIT_SMIS_ON_64BIT_ARCH
183
+ using PlatformSmiTagging = SmiTagging<kApiInt32Size>;
184
+ #else
185
+ using PlatformSmiTagging = SmiTagging<kApiTaggedSize>;
186
+ #endif
187
+
188
+ // TODO(ishell): Consinder adding kSmiShiftBits = kSmiShiftSize + kSmiTagSize
189
+ // since it's used much more often than the inividual constants.
190
+ const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
191
+ const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
192
+ const int kSmiMinValue = static_cast<int>(PlatformSmiTagging::kSmiMinValue);
193
+ const int kSmiMaxValue = static_cast<int>(PlatformSmiTagging::kSmiMaxValue);
194
+ constexpr bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
195
+ constexpr bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
196
+ constexpr bool Is64() { return kApiSystemPointerSize == sizeof(int64_t); }
197
+
198
+ V8_INLINE static constexpr Address IntToSmi(int value) {
199
+ return (static_cast<Address>(value) << (kSmiTagSize + kSmiShiftSize)) |
200
+ kSmiTag;
201
+ }
202
+
203
+ /*
204
+ * Sandbox related types, constants, and functions.
205
+ */
206
+ constexpr bool SandboxIsEnabled() {
207
+ #ifdef V8_ENABLE_SANDBOX
208
+ return true;
209
+ #else
210
+ return false;
211
+ #endif
212
+ }
213
+
214
+ // SandboxedPointers are guaranteed to point into the sandbox. This is achieved
215
+ // for example by storing them as offset rather than as raw pointers.
216
+ using SandboxedPointer_t = Address;
217
+
218
+ #ifdef V8_ENABLE_SANDBOX
219
+
220
+ // Size of the sandbox, excluding the guard regions surrounding it.
221
+ #if defined(V8_TARGET_OS_ANDROID)
222
+ // On Android, most 64-bit devices seem to be configured with only 39 bits of
223
+ // virtual address space for userspace. As such, limit the sandbox to 128GB (a
224
+ // quarter of the total available address space).
225
+ constexpr size_t kSandboxSizeLog2 = 37; // 128 GB
226
+ #elif defined(V8_TARGET_OS_IOS)
227
+ // On iOS, we only get 64 GB of usable virtual address space even with the
228
+ // "jumbo" extended virtual addressing entitlement. Limit the sandbox size to
229
+ // 16 GB so that the base address + size for the emulated virtual address space
230
+ // lies within the 64 GB total virtual address space.
231
+ constexpr size_t kSandboxSizeLog2 = 34; // 16 GB
232
+ #else
233
+ // Everywhere else use a 1TB sandbox.
234
+ constexpr size_t kSandboxSizeLog2 = 40; // 1 TB
235
+ #endif // V8_TARGET_OS_ANDROID
236
+ constexpr size_t kSandboxSize = 1ULL << kSandboxSizeLog2;
237
+
238
+ // Required alignment of the sandbox. For simplicity, we require the
239
+ // size of the guard regions to be a multiple of this, so that this specifies
240
+ // the alignment of the sandbox including and excluding surrounding guard
241
+ // regions. The alignment requirement is due to the pointer compression cage
242
+ // being located at the start of the sandbox.
243
+ constexpr size_t kSandboxAlignment = kPtrComprCageBaseAlignment;
244
+
245
+ // Sandboxed pointers are stored inside the heap as offset from the sandbox
246
+ // base shifted to the left. This way, it is guaranteed that the offset is
247
+ // smaller than the sandbox size after shifting it to the right again. This
248
+ // constant specifies the shift amount.
249
+ constexpr uint64_t kSandboxedPointerShift = 64 - kSandboxSizeLog2;
250
+
251
+ // Size of the guard regions surrounding the sandbox. This assumes a worst-case
252
+ // scenario of a 32-bit unsigned index used to access an array of 64-bit values
253
+ // with an additional 4GB (compressed pointer) offset. In particular, accesses
254
+ // to TypedArrays are effectively computed as
255
+ // `entry_pointer = array->base + array->offset + index * array->element_size`.
256
+ // See also https://crbug.com/40070746 for more details.
257
+ constexpr size_t kSandboxGuardRegionSize = 32ULL * GB + 4ULL * GB;
258
+
259
+ static_assert((kSandboxGuardRegionSize % kSandboxAlignment) == 0,
260
+ "The size of the guard regions around the sandbox must be a "
261
+ "multiple of its required alignment.");
262
+
263
+ // On OSes where reserving virtual memory is too expensive to reserve the
264
+ // entire address space backing the sandbox, notably Windows pre 8.1, we create
265
+ // a partially reserved sandbox that doesn't actually reserve most of the
266
+ // memory, and so doesn't have the desired security properties as unrelated
267
+ // memory allocations could end up inside of it, but which still ensures that
268
+ // objects that should be located inside the sandbox are allocated within
269
+ // kSandboxSize bytes from the start of the sandbox. The minimum size of the
270
+ // region that is actually reserved for such a sandbox is specified by this
271
+ // constant and should be big enough to contain the pointer compression cage as
272
+ // well as the ArrayBuffer partition.
273
+ constexpr size_t kSandboxMinimumReservationSize = 8ULL * GB;
274
+
275
+ static_assert(kSandboxMinimumReservationSize > kPtrComprCageReservationSize,
276
+ "The minimum reservation size for a sandbox must be larger than "
277
+ "the pointer compression cage contained within it.");
278
+
279
+ // The maximum buffer size allowed inside the sandbox. This is mostly dependent
280
+ // on the size of the guard regions around the sandbox: an attacker must not be
281
+ // able to construct a buffer that appears larger than the guard regions and
282
+ // thereby "reach out of" the sandbox.
283
+ constexpr size_t kMaxSafeBufferSizeForSandbox = 32ULL * GB - 1;
284
+ static_assert(kMaxSafeBufferSizeForSandbox <= kSandboxGuardRegionSize,
285
+ "The maximum allowed buffer size must not be larger than the "
286
+ "sandbox's guard regions");
287
+
288
+ constexpr size_t kBoundedSizeShift = 29;
289
+ static_assert(1ULL << (64 - kBoundedSizeShift) ==
290
+ kMaxSafeBufferSizeForSandbox + 1,
291
+ "The maximum size of a BoundedSize must be synchronized with the "
292
+ "kMaxSafeBufferSizeForSandbox");
293
+
294
+ #endif // V8_ENABLE_SANDBOX
295
+
296
+ #ifdef V8_COMPRESS_POINTERS
297
+
298
+ #ifdef V8_TARGET_OS_ANDROID
299
+ // The size of the virtual memory reservation for an external pointer table.
300
+ // This determines the maximum number of entries in a table. Using a maximum
301
+ // size allows omitting bounds checks on table accesses if the indices are
302
+ // guaranteed (e.g. through shifting) to be below the maximum index. This
303
+ // value must be a power of two.
304
+ constexpr size_t kExternalPointerTableReservationSize = 256 * MB;
305
+
306
+ // The external pointer table indices stored in HeapObjects as external
307
+ // pointers are shifted to the left by this amount to guarantee that they are
308
+ // smaller than the maximum table size even after the C++ compiler multiplies
309
+ // them by 8 to be used as indexes into a table of 64 bit pointers.
310
+ constexpr uint32_t kExternalPointerIndexShift = 7;
311
+ #elif defined(V8_TARGET_OS_IOS)
312
+ // iOS restricts large memory allocations, with 128 MB being the maximum size we
313
+ // can configure. If we exceed this, SegmentedTable::Initialize will throw a V8
314
+ // out-of-memory error when running the JetStream benchmark
315
+ // (https://browserbench.org/JetStream/).
316
+ constexpr size_t kExternalPointerTableReservationSize = 128 * MB;
317
+ constexpr uint32_t kExternalPointerIndexShift = 8;
318
+ #else
319
+ constexpr size_t kExternalPointerTableReservationSize = 512 * MB;
320
+ constexpr uint32_t kExternalPointerIndexShift = 6;
321
+ #endif // V8_TARGET_OS_ANDROID
322
+
323
+ // The maximum number of entries in an external pointer table.
324
+ constexpr int kExternalPointerTableEntrySize = 8;
325
+ constexpr int kExternalPointerTableEntrySizeLog2 = 3;
326
+ constexpr size_t kMaxExternalPointers =
327
+ kExternalPointerTableReservationSize / kExternalPointerTableEntrySize;
328
+ static_assert((1 << (32 - kExternalPointerIndexShift)) == kMaxExternalPointers,
329
+ "kExternalPointerTableReservationSize and "
330
+ "kExternalPointerIndexShift don't match");
331
+
332
+ #else // !V8_COMPRESS_POINTERS
333
+
334
+ // Needed for the V8.SandboxedExternalPointersCount histogram.
335
+ constexpr size_t kMaxExternalPointers = 0;
336
+
337
+ #endif // V8_COMPRESS_POINTERS
338
+
339
+ constexpr uint64_t kExternalPointerMarkBit = 1ULL << 48;
340
+ constexpr uint64_t kExternalPointerTagShift = 49;
341
+ constexpr uint64_t kExternalPointerTagMask = 0x00fe000000000000ULL;
342
+ constexpr uint64_t kExternalPointerShiftedTagMask =
343
+ kExternalPointerTagMask >> kExternalPointerTagShift;
344
+ static_assert(kExternalPointerShiftedTagMask << kExternalPointerTagShift ==
345
+ kExternalPointerTagMask);
346
+ constexpr uint64_t kExternalPointerTagAndMarkbitMask = 0x00ff000000000000ULL;
347
+ constexpr uint64_t kExternalPointerPayloadMask = 0xff00ffffffffffffULL;
348
+
349
+ // A ExternalPointerHandle represents a (opaque) reference to an external
350
+ // pointer that can be stored inside the sandbox. A ExternalPointerHandle has
351
+ // meaning only in combination with an (active) Isolate as it references an
352
+ // external pointer stored in the currently active Isolate's
353
+ // ExternalPointerTable. Internally, an ExternalPointerHandles is simply an
354
+ // index into an ExternalPointerTable that is shifted to the left to guarantee
355
+ // that it is smaller than the size of the table.
356
+ using ExternalPointerHandle = uint32_t;
357
+
358
+ // ExternalPointers point to objects located outside the sandbox. When the V8
359
+ // sandbox is enabled, these are stored on heap as ExternalPointerHandles,
360
+ // otherwise they are simply raw pointers.
361
+ #ifdef V8_ENABLE_SANDBOX
362
+ using ExternalPointer_t = ExternalPointerHandle;
363
+ #else
364
+ using ExternalPointer_t = Address;
365
+ #endif
366
+
367
+ constexpr ExternalPointer_t kNullExternalPointer = 0;
368
+ constexpr ExternalPointerHandle kNullExternalPointerHandle = 0;
369
+
370
+ // See `ExternalPointerHandle` for the main documentation. The difference to
371
+ // `ExternalPointerHandle` is that the handle does not represent an arbitrary
372
+ // external pointer but always refers to an object managed by `CppHeap`. The
373
+ // handles are using in combination with a dedicated table for `CppHeap`
374
+ // references.
375
+ using CppHeapPointerHandle = uint32_t;
376
+
377
+ // The actual pointer to objects located on the `CppHeap`. When pointer
378
+ // compression is enabled these pointers are stored as `CppHeapPointerHandle`.
379
+ // In non-compressed configurations the pointers are simply stored as raw
380
+ // pointers.
381
+ #ifdef V8_COMPRESS_POINTERS
382
+ using CppHeapPointer_t = CppHeapPointerHandle;
383
+ #else
384
+ using CppHeapPointer_t = Address;
385
+ #endif
386
+
387
+ constexpr CppHeapPointer_t kNullCppHeapPointer = 0;
388
+ constexpr CppHeapPointerHandle kNullCppHeapPointerHandle = 0;
389
+
390
+ constexpr uint64_t kCppHeapPointerMarkBit = 1ULL;
391
+ constexpr uint64_t kCppHeapPointerTagShift = 1;
392
+ constexpr uint64_t kCppHeapPointerPayloadShift = 16;
393
+
394
+ #ifdef V8_COMPRESS_POINTERS
395
+ // CppHeapPointers use a dedicated pointer table. These constants control the
396
+ // size and layout of the table. See the corresponding constants for the
397
+ // external pointer table for further details.
398
+ constexpr size_t kCppHeapPointerTableReservationSize =
399
+ kExternalPointerTableReservationSize;
400
+ constexpr uint32_t kCppHeapPointerIndexShift = kExternalPointerIndexShift;
401
+
402
+ constexpr int kCppHeapPointerTableEntrySize = 8;
403
+ constexpr int kCppHeapPointerTableEntrySizeLog2 = 3;
404
+ constexpr size_t kMaxCppHeapPointers =
405
+ kCppHeapPointerTableReservationSize / kCppHeapPointerTableEntrySize;
406
+ static_assert((1 << (32 - kCppHeapPointerIndexShift)) == kMaxCppHeapPointers,
407
+ "kCppHeapPointerTableReservationSize and "
408
+ "kCppHeapPointerIndexShift don't match");
409
+
410
+ #else // !V8_COMPRESS_POINTERS
411
+
412
+ // Needed for the V8.SandboxedCppHeapPointersCount histogram.
413
+ constexpr size_t kMaxCppHeapPointers = 0;
414
+
415
+ #endif // V8_COMPRESS_POINTERS
416
+
417
+ // The number of tags reserved for embedder data stored in internal fields. The
418
+ // value is picked arbitrarily, and is slightly larger than the number of tags
419
+ // currently used in Chrome.
420
+ #define V8_EMBEDDER_DATA_TAG_COUNT 15
421
+
422
+ // The number of tags reserved for pointers stored in v8::External. The value is
423
+ // picked arbitrarily, and is slightly larger than the number of tags currently
424
+ // used in Chrome.
425
+ #define V8_EXTERNAL_POINTER_TAG_COUNT 40
426
+
427
+ // Generic tag range struct to represent ranges of type tags.
428
+ //
429
+ // When referencing external objects via pointer tables, type tags are
430
+ // frequently necessary to guarantee type safety for the external objects. When
431
+ // support for subtyping is necessary, range-based type checks are used in
432
+ // which all subtypes of a given supertype use contiguous tags. This struct can
433
+ // then be used to represent such a type range.
434
+ //
435
+ // In addition, there is an option for performance tweaks: if the size of the
436
+ // type range corresponding to a supertype is a power of two and starts at a
437
+ // power of two (e.g. [0x100, 0x13f]), then the compiler can often optimize
438
+ // the type check to use even fewer instructions (essentially replace a AND +
439
+ // SUB with a single AND).
440
+ //
441
+ // Tag ranges can also to a limited degree be used for union types. For
442
+ // example, with the type graph as above, it would be possible to specify a
443
+ // Union(D, E, F) as the tag range [D, F]. However, this only works as long as
444
+ // the (otherwise independent) types that form the union have adjacent tags.
445
+ //
446
+ //
447
+ // There are broadly speaking two options for performing the type check when
448
+ // given the expected type range and the actual tag of the entry.
449
+ //
450
+ // The first option is to simply have the equivalent of
451
+ //
452
+ // CHECK(expected_tag_range.Contains(actual_tag))
453
+ //
454
+ // This is nice and simple, and friendly to both the branch-predictor and the
455
+ // user/developer as it produces clear error messages. However, this approach
456
+ // may result in quite a bit of code being generated, for example for calling
457
+ // RuntimeAbort from generated code or similar.
458
+ //
459
+ // The second option is to generate code such as
460
+ //
461
+ // if (!expected_tag_range.Contains(actual_tag)) return nullptr;
462
+ //
463
+ // With this, we are also guaranteed to crash safely when the returned pointer
464
+ // is used, but this may result in significantly less code being generated, for
465
+ // example because the compiler can implement this with a single conditional
466
+ // select in combination with the zero register (e.g. on Arm).
467
+ //
468
+ // The choice of which approach to use therefore depends on the use case, the
469
+ // performance and code size constraints, and the importance of debuggability.
470
+ template <typename Tag>
471
+ struct TagRange {
472
+ static_assert(std::is_enum_v<Tag> &&
473
+ std::is_same_v<std::underlying_type_t<Tag>, uint16_t>,
474
+ "Tag parameter must be an enum with base type uint16_t");
475
+
476
+ // Construct the inclusive tag range [first, last].
477
+ constexpr TagRange(Tag first, Tag last) : first(first), last(last) {
478
+ #ifdef V8_ENABLE_CHECKS
479
+ // This would typically be a DCHECK, but that's not available here.
480
+ #if V8_HAS_BUILTIN_UNREACHABLE
481
+ if (first > last) __builtin_unreachable(); // Invalid tag range.
482
+ #elif defined(_MSC_VER)
483
+ if (first > last) __assume(0); // Invalid tag range.
484
+ #endif
485
+ #endif
486
+ }
487
+
488
+ // Construct a tag range consisting of a single tag.
489
+ //
490
+ // A single tag is always implicitly convertible to a tag range. This greatly
491
+ // increases readability as most of the time, the exact tag of a field is
492
+ // known and so no tag range needs to explicitly be created for it.
493
+ constexpr TagRange(Tag tag) // NOLINT(runtime/explicit)
494
+ : first(tag), last(tag) {}
495
+
496
+ // Construct an empty tag range.
497
+ constexpr TagRange() : TagRange(static_cast<Tag>(0)) {}
498
+
499
+ // A tag range is considered empty if it only contains the null tag.
500
+ constexpr bool IsEmpty() const { return first == 0 && last == 0; }
501
+
502
+ constexpr size_t Size() const {
503
+ if (IsEmpty()) {
504
+ return 0;
505
+ } else {
506
+ return last - first + 1;
507
+ }
508
+ }
509
+
510
+ constexpr bool Contains(Tag tag) const {
511
+ // Need to perform the math with uint32_t. Otherwise, the uint16_ts would
512
+ // be promoted to (signed) int, allowing the compiler to (wrongly) assume
513
+ // that an underflow cannot happen as that would be undefined behavior.
514
+ return static_cast<uint32_t>(tag) - static_cast<uint32_t>(first) <=
515
+ static_cast<uint32_t>(last) - static_cast<uint32_t>(first);
516
+ }
517
+
518
+ constexpr bool Contains(TagRange tag_range) const {
519
+ return tag_range.first >= first && tag_range.last <= last;
520
+ }
521
+
522
+ constexpr bool operator==(const TagRange other) const {
523
+ return first == other.first && last == other.last;
524
+ }
525
+
526
+ constexpr size_t hash_value() const {
527
+ static_assert(std::is_same_v<std::underlying_type_t<Tag>, uint16_t>);
528
+ return (static_cast<size_t>(first) << 16) | last;
529
+ }
530
+
531
+ // Internally we represent tag ranges as closed ranges [first, last].
532
+ Tag first;
533
+ Tag last;
534
+ };
535
+
536
+ //
537
+ // External Pointers.
538
+ //
539
+ // When the sandbox is enabled, external pointers are stored in an external
540
+ // pointer table and are referenced from HeapObjects through an index (a
541
+ // "handle"). When stored in the table, the pointers are tagged with per-type
542
+ // tags to prevent type confusion attacks between different external objects.
543
+ //
544
+ // When loading an external pointer, a range of allowed tags can be specified.
545
+ // This way, type hierarchies can be supported. The main requirement for that
546
+ // is that all (transitive) child classes of a given parent class have type ids
547
+ // in the same range, and that there are no unrelated types in that range. For
548
+ // more details about how to assign type tags to types, see the TagRange class.
549
+ //
550
+ // The external pointer sandboxing mechanism ensures that every access to an
551
+ // external pointer field will result in a valid pointer of the expected type
552
+ // even in the presence of an attacker able to corrupt memory inside the
553
+ // sandbox. However, if any data related to the external object is stored
554
+ // inside the sandbox it may still be corrupted and so must be validated before
555
+ // use or moved into the external object. Further, an attacker will always be
556
+ // able to substitute different external pointers of the same type for each
557
+ // other. Therefore, code using external pointers must be written in a
558
+ // "substitution-safe" way, i.e. it must always be possible to substitute
559
+ // external pointers of the same type without causing memory corruption outside
560
+ // of the sandbox. Generally this is achieved by referencing any group of
561
+ // related external objects through a single external pointer.
562
+ //
563
+ // Currently we use bit 62 for the marking bit which should always be unused as
564
+ // it's part of the non-canonical address range. When Arm's top-byte ignore
565
+ // (TBI) is enabled, this bit will be part of the ignored byte, and we assume
566
+ // that the Embedder is not using this byte (really only this one bit) for any
567
+ // other purpose. This bit also does not collide with the memory tagging
568
+ // extension (MTE) which would use bits [56, 60).
569
+ //
570
+ // External pointer tables are also available even when the sandbox is off but
571
+ // pointer compression is on. In that case, the mechanism can be used to ease
572
+ // alignment requirements as it turns unaligned 64-bit raw pointers into
573
+ // aligned 32-bit indices. To "opt-in" to the external pointer table mechanism
574
+ // for this purpose, instead of using the ExternalPointer accessors one needs to
575
+ // use ExternalPointerHandles directly and use them to access the pointers in an
576
+ // ExternalPointerTable.
577
+ //
578
+ // The tag is currently in practice limited to 15 bits since it needs to fit
579
+ // together with a marking bit into the unused parts of a pointer.
580
+ enum ExternalPointerTag : uint16_t {
581
+ kFirstExternalPointerTag = 0,
582
+ kExternalPointerNullTag = 0,
583
+
584
+ // When adding new tags, please ensure that the code using these tags is
585
+ // "substitution-safe", i.e. still operate safely if external pointers of the
586
+ // same type are swapped by an attacker. See comment above for more details.
587
+
588
+ // Shared external pointers are owned by the shared Isolate and stored in the
589
+ // shared external pointer table associated with that Isolate, where they can
590
+ // be accessed from multiple threads at the same time. The objects referenced
591
+ // in this way must therefore always be thread-safe.
592
+ kFirstSharedExternalPointerTag,
593
+ kWaiterQueueNodeTag = kFirstSharedExternalPointerTag,
594
+ kExternalStringResourceTag,
595
+ kExternalStringResourceDataTag,
596
+ kLastSharedExternalPointerTag = kExternalStringResourceDataTag,
597
+
598
+ // External pointers using these tags are kept in a per-Isolate external
599
+ // pointer table and can only be accessed when this Isolate is active.
600
+ kNativeContextMicrotaskQueueTag,
601
+
602
+ // Placeholders for embedder data.
603
+ kFirstEmbedderDataTag,
604
+ kLastEmbedderDataTag = kFirstEmbedderDataTag + V8_EMBEDDER_DATA_TAG_COUNT - 1,
605
+
606
+ // Placeholders for pointers store in v8::External.
607
+ kFirstExternalTypeTag,
608
+ kLastExternalTypeTag =
609
+ kFirstExternalTypeTag + V8_EXTERNAL_POINTER_TAG_COUNT - 1,
610
+ // This tag is used when a fast-api callback as a parameter of type
611
+ // `kPointer`. The V8 fast API is only able to use this generic tag, and is
612
+ // therefore not supposed to be used in Chrome.
613
+ kFastApiExternalTypeTag = kLastExternalTypeTag,
614
+ kFirstMaybeReadOnlyExternalPointerTag,
615
+ kFunctionTemplateInfoCallbackTag = kFirstMaybeReadOnlyExternalPointerTag,
616
+ kAccessorInfoGetterTag,
617
+ kAccessorInfoSetterTag,
618
+
619
+ // InterceptorInfo external pointers.
620
+ kFirstInterceptorInfoExternalPointerTag,
621
+ kApiNamedPropertyQueryCallbackTag = kFirstInterceptorInfoExternalPointerTag,
622
+ kApiNamedPropertyGetterCallbackTag,
623
+ kApiNamedPropertySetterCallbackTag,
624
+ kApiNamedPropertyDescriptorCallbackTag,
625
+ kApiNamedPropertyDefinerCallbackTag,
626
+ kApiNamedPropertyDeleterCallbackTag,
627
+ kApiNamedPropertyEnumeratorCallbackTag,
628
+ kApiIndexedPropertyQueryCallbackTag,
629
+ kApiIndexedPropertyGetterCallbackTag,
630
+ kApiIndexedPropertySetterCallbackTag,
631
+ kApiIndexedPropertyDescriptorCallbackTag,
632
+ kApiIndexedPropertyDefinerCallbackTag,
633
+ kApiIndexedPropertyDeleterCallbackTag,
634
+ kApiIndexedPropertyEnumeratorCallbackTag,
635
+ kLastInterceptorInfoExternalPointerTag =
636
+ kApiIndexedPropertyEnumeratorCallbackTag,
637
+
638
+ kLastMaybeReadOnlyExternalPointerTag = kLastInterceptorInfoExternalPointerTag,
639
+
640
+ kWasmStackMemoryTag,
641
+
642
+ // Foreigns
643
+ kFirstForeignExternalPointerTag,
644
+ kGenericForeignTag = kFirstForeignExternalPointerTag,
645
+
646
+ kApiAccessCheckCallbackTag,
647
+ kApiAbortScriptExecutionCallbackTag,
648
+ kSyntheticModuleTag,
649
+ kMicrotaskCallbackTag,
650
+ kMicrotaskCallbackDataTag,
651
+ kCFunctionTag,
652
+ kCFunctionInfoTag,
653
+ kMessageListenerTag,
654
+ kWaiterQueueForeignTag,
655
+
656
+ // Managed
657
+ kFirstManagedResourceTag,
658
+ kFirstManagedExternalPointerTag = kFirstManagedResourceTag,
659
+ kGenericManagedTag = kFirstManagedExternalPointerTag,
660
+ kWasmWasmStreamingTag,
661
+ kWasmFuncDataTag,
662
+ kWasmManagedDataTag,
663
+ kWasmNativeModuleTag,
664
+ kBackingStoreTag,
665
+ kIcuBreakIteratorTag,
666
+ kIcuUnicodeStringTag,
667
+ kIcuListFormatterTag,
668
+ kIcuLocaleTag,
669
+ kIcuSimpleDateFormatTag,
670
+ kIcuDateIntervalFormatTag,
671
+ kIcuRelativeDateTimeFormatterTag,
672
+ kIcuLocalizedNumberFormatterTag,
673
+ kIcuPluralRulesTag,
674
+ kIcuCollatorTag,
675
+ kTemporalDurationTag,
676
+ kTemporalInstantTag,
677
+ kTemporalPlainDateTag,
678
+ kTemporalPlainTimeTag,
679
+ kTemporalPlainDateTimeTag,
680
+ kTemporalPlainYearMonthTag,
681
+ kTemporalPlainMonthDayTag,
682
+ kTemporalZonedDateTimeTag,
683
+ kDisplayNamesInternalTag,
684
+ kD8WorkerTag,
685
+ kD8ModuleEmbedderDataTag,
686
+ kLastForeignExternalPointerTag = kD8ModuleEmbedderDataTag,
687
+ kLastManagedExternalPointerTag = kLastForeignExternalPointerTag,
688
+ // External resources whose lifetime is tied to their entry in the external
689
+ // pointer table but which are not referenced via a Managed
690
+ kArrayBufferExtensionTag,
691
+ kLastManagedResourceTag = kArrayBufferExtensionTag,
692
+
693
+ kExternalPointerZappedEntryTag = 0x7d,
694
+ kExternalPointerEvacuationEntryTag = 0x7e,
695
+ kExternalPointerFreeEntryTag = 0x7f,
696
+ // The tags are limited to 7 bits, so the last tag is 0x7f.
697
+ kLastExternalPointerTag = 0x7f,
698
+ };
699
+
700
+ using ExternalPointerTagRange = TagRange<ExternalPointerTag>;
701
+
702
+ constexpr ExternalPointerTagRange kAnyExternalPointerTagRange(
703
+ kFirstExternalPointerTag, kLastExternalPointerTag);
704
+ constexpr ExternalPointerTagRange kAnySharedExternalPointerTagRange(
705
+ kFirstSharedExternalPointerTag, kLastSharedExternalPointerTag);
706
+ constexpr ExternalPointerTagRange kAnyForeignExternalPointerTagRange(
707
+ kFirstForeignExternalPointerTag, kLastForeignExternalPointerTag);
708
+ constexpr ExternalPointerTagRange kAnyInterceptorInfoExternalPointerTagRange(
709
+ kFirstInterceptorInfoExternalPointerTag,
710
+ kLastInterceptorInfoExternalPointerTag);
711
+ constexpr ExternalPointerTagRange kAnyManagedExternalPointerTagRange(
712
+ kFirstManagedExternalPointerTag, kLastManagedExternalPointerTag);
713
+ constexpr ExternalPointerTagRange kAnyMaybeReadOnlyExternalPointerTagRange(
714
+ kFirstMaybeReadOnlyExternalPointerTag,
715
+ kLastMaybeReadOnlyExternalPointerTag);
716
+ constexpr ExternalPointerTagRange kAnyManagedResourceExternalPointerTag(
717
+ kFirstManagedResourceTag, kLastManagedResourceTag);
718
+
719
+ // True if the external pointer must be accessed from the shared isolate's
720
+ // external pointer table.
721
+ V8_INLINE static constexpr bool IsSharedExternalPointerType(
722
+ ExternalPointerTagRange tag_range) {
723
+ return kAnySharedExternalPointerTagRange.Contains(tag_range);
724
+ }
725
+
726
+ // True if the external pointer may live in a read-only object, in which case
727
+ // the table entry will be in the shared read-only segment of the external
728
+ // pointer table.
729
+ V8_INLINE static constexpr bool IsMaybeReadOnlyExternalPointerType(
730
+ ExternalPointerTagRange tag_range) {
731
+ return kAnyMaybeReadOnlyExternalPointerTagRange.Contains(tag_range);
732
+ }
733
+
734
+ // True if the external pointer references an external object whose lifetime is
735
+ // tied to the entry in the external pointer table.
736
+ // In this case, the entry in the ExternalPointerTable always points to an
737
+ // object derived from ExternalPointerTable::ManagedResource.
738
+ V8_INLINE static constexpr bool IsManagedExternalPointerType(
739
+ ExternalPointerTagRange tag_range) {
740
+ return kAnyManagedResourceExternalPointerTag.Contains(tag_range);
741
+ }
742
+
743
+ // When an external poiner field can contain the null external pointer handle,
744
+ // the type checking mechanism needs to also check for null.
745
+ // TODO(saelo): this is mostly a temporary workaround to introduce range-based
746
+ // type checks. In the future, we should either (a) change the type tagging
747
+ // scheme so that null always passes or (b) (more likely) introduce dedicated
748
+ // null entries for those tags that need them (similar to other well-known
749
+ // empty value constants such as the empty fixed array).
750
+ V8_INLINE static constexpr bool ExternalPointerCanBeEmpty(
751
+ ExternalPointerTagRange tag_range) {
752
+ return tag_range.Contains(kArrayBufferExtensionTag) ||
753
+ (tag_range.first <= kLastEmbedderDataTag &&
754
+ kFirstEmbedderDataTag <= tag_range.last) ||
755
+ kAnyInterceptorInfoExternalPointerTagRange.Contains(tag_range);
756
+ }
757
+
758
+ // Indirect Pointers.
759
+ //
760
+ // When the sandbox is enabled, indirect pointers are used to reference
761
+ // HeapObjects that live outside of the sandbox (but are still managed by V8's
762
+ // garbage collector). When object A references an object B through an indirect
763
+ // pointer, object A will contain a IndirectPointerHandle, i.e. a shifted
764
+ // 32-bit index, which identifies an entry in a pointer table (either the
765
+ // trusted pointer table for TrustedObjects, or the code pointer table if it is
766
+ // a Code object). This table entry then contains the actual pointer to object
767
+ // B. Further, object B owns this pointer table entry, and it is responsible
768
+ // for updating the "self-pointer" in the entry when it is relocated in memory.
769
+ // This way, in contrast to "normal" pointers, indirect pointers never need to
770
+ // be tracked by the GC (i.e. there is no remembered set for them).
771
+ // These pointers do not exist when the sandbox is disabled.
772
+
773
+ // An IndirectPointerHandle represents a 32-bit index into a pointer table.
774
+ using IndirectPointerHandle = uint32_t;
775
+
776
+ // A null handle always references an entry that contains nullptr.
777
+ constexpr IndirectPointerHandle kNullIndirectPointerHandle = 0;
778
+
779
+ // When the sandbox is enabled, indirect pointers are used to implement:
780
+ // - TrustedPointers: an indirect pointer using the trusted pointer table (TPT)
781
+ // and referencing a TrustedObject in one of the trusted heap spaces.
782
+ // - CodePointers, an indirect pointer using the code pointer table (CPT) and
783
+ // referencing a Code object together with its instruction stream.
784
+
785
+ //
786
+ // Trusted Pointers.
787
+ //
788
+ // A pointer to a TrustedObject.
789
+ // When the sandbox is enabled, these are indirect pointers using the trusted
790
+ // pointer table (TPT). They are used to reference trusted objects (located in
791
+ // one of V8's trusted heap spaces, outside of the sandbox) from inside the
792
+ // sandbox in a memory-safe way. When the sandbox is disabled, these are
793
+ // regular tagged pointers.
794
+ using TrustedPointerHandle = IndirectPointerHandle;
795
+
796
+ // The size of the virtual memory reservation for the trusted pointer table.
797
+ // As with the external pointer table, a maximum table size in combination with
798
+ // shifted indices allows omitting bounds checks.
799
+ constexpr size_t kTrustedPointerTableReservationSize = 64 * MB;
800
+
801
+ // The trusted pointer handles are stored shifted to the left by this amount
802
+ // to guarantee that they are smaller than the maximum table size.
803
+ constexpr uint32_t kTrustedPointerHandleShift = 9;
804
+
805
+ // A null handle always references an entry that contains nullptr.
806
+ constexpr TrustedPointerHandle kNullTrustedPointerHandle =
807
+ kNullIndirectPointerHandle;
808
+
809
+ // The maximum number of entries in an trusted pointer table.
810
+ constexpr int kTrustedPointerTableEntrySize = 8;
811
+ constexpr int kTrustedPointerTableEntrySizeLog2 = 3;
812
+ constexpr size_t kMaxTrustedPointers =
813
+ kTrustedPointerTableReservationSize / kTrustedPointerTableEntrySize;
814
+ static_assert((1 << (32 - kTrustedPointerHandleShift)) == kMaxTrustedPointers,
815
+ "kTrustedPointerTableReservationSize and "
816
+ "kTrustedPointerHandleShift don't match");
817
+
818
+ //
819
+ // Code Pointers.
820
+ //
821
+ // A pointer to a Code object.
822
+ // Essentially a specialized version of a trusted pointer that (when the
823
+ // sandbox is enabled) uses the code pointer table (CPT) instead of the TPT.
824
+ // Each entry in the CPT contains both a pointer to a Code object as well as a
825
+ // pointer to the Code's entrypoint. This allows calling/jumping into Code with
826
+ // one fewer memory access (compared to the case where the entrypoint pointer
827
+ // first needs to be loaded from the Code object). As such, a CodePointerHandle
828
+ // can be used both to obtain the referenced Code object and to directly load
829
+ // its entrypoint.
830
+ //
831
+ // When the sandbox is disabled, these are regular tagged pointers.
832
+ using CodePointerHandle = IndirectPointerHandle;
833
+
834
+ // The size of the virtual memory reservation for the code pointer table.
835
+ // As with the other tables, a maximum table size in combination with shifted
836
+ // indices allows omitting bounds checks.
837
+ constexpr size_t kCodePointerTableReservationSize = 128 * MB;
838
+
839
+ // Code pointer handles are shifted by a different amount than indirect pointer
840
+ // handles as the tables have a different maximum size.
841
+ constexpr uint32_t kCodePointerHandleShift = 9;
842
+
843
+ // A null handle always references an entry that contains nullptr.
844
+ constexpr CodePointerHandle kNullCodePointerHandle = kNullIndirectPointerHandle;
845
+
846
+ // It can sometimes be necessary to distinguish a code pointer handle from a
847
+ // trusted pointer handle. A typical example would be a union trusted pointer
848
+ // field that can refer to both Code objects and other trusted objects. To
849
+ // support these use-cases, we use a simple marking scheme where some of the
850
+ // low bits of a code pointer handle are set, while they will be unset on a
851
+ // trusted pointer handle. This way, the correct table to resolve the handle
852
+ // can be determined even in the absence of a type tag.
853
+ constexpr uint32_t kCodePointerHandleMarker = 0x1;
854
+ static_assert(kCodePointerHandleShift > 0);
855
+ static_assert(kTrustedPointerHandleShift > 0);
856
+
857
+ // The maximum number of entries in a code pointer table.
858
+ constexpr int kCodePointerTableEntrySize = 16;
859
+ constexpr int kCodePointerTableEntrySizeLog2 = 4;
860
+ constexpr size_t kMaxCodePointers =
861
+ kCodePointerTableReservationSize / kCodePointerTableEntrySize;
862
+ static_assert(
863
+ (1 << (32 - kCodePointerHandleShift)) == kMaxCodePointers,
864
+ "kCodePointerTableReservationSize and kCodePointerHandleShift don't match");
865
+
866
+ constexpr int kCodePointerTableEntryEntrypointOffset = 0;
867
+ constexpr int kCodePointerTableEntryCodeObjectOffset = 8;
868
+
869
+ // Constants that can be used to mark places that should be modified once
870
+ // certain types of objects are moved out of the sandbox and into trusted space.
871
+ constexpr bool kRuntimeGeneratedCodeObjectsLiveInTrustedSpace = true;
872
+ constexpr bool kBuiltinCodeObjectsLiveInTrustedSpace = false;
873
+ constexpr bool kAllCodeObjectsLiveInTrustedSpace =
874
+ kRuntimeGeneratedCodeObjectsLiveInTrustedSpace &&
875
+ kBuiltinCodeObjectsLiveInTrustedSpace;
876
+
877
+ // {obj} must be the raw tagged pointer representation of a HeapObject
878
+ // that's guaranteed to never be in ReadOnlySpace.
879
+ V8_DEPRECATE_SOON(
880
+ "Use GetCurrentIsolate() instead, which is guaranteed to return the same "
881
+ "isolate since https://crrev.com/c/6458560.")
882
+ V8_EXPORT internal::Isolate* IsolateFromNeverReadOnlySpaceObject(Address obj);
883
+
884
+ // Returns if we need to throw when an error occurs. This infers the language
885
+ // mode based on the current context and the closure. This returns true if the
886
+ // language mode is strict.
887
+ V8_EXPORT bool ShouldThrowOnError(internal::Isolate* isolate);
888
+
889
+ struct HandleScopeData final {
890
+ static constexpr uint32_t kSizeInBytes =
891
+ 2 * kApiSystemPointerSize + 2 * kApiInt32Size;
892
+
893
+ Address* next;
894
+ Address* limit;
895
+ int level;
896
+ int sealed_level;
897
+
898
+ void Initialize() {
899
+ next = limit = nullptr;
900
+ sealed_level = level = 0;
901
+ }
902
+ };
903
+
904
+ static_assert(HandleScopeData::kSizeInBytes == sizeof(HandleScopeData));
905
+
906
+ /**
907
+ * This class exports constants and functionality from within v8 that
908
+ * is necessary to implement inline functions in the v8 api. Don't
909
+ * depend on functions and constants defined here.
910
+ */
911
+ class Internals {
912
+ #ifdef V8_MAP_PACKING
913
+ V8_INLINE static constexpr Address UnpackMapWord(Address mapword) {
914
+ // TODO(wenyuzhao): Clear header metadata.
915
+ return mapword ^ kMapWordXorMask;
916
+ }
917
+ #endif
918
+
919
+ public:
920
+ // These values match non-compiler-dependent values defined within
921
+ // the implementation of v8.
922
+ static const int kHeapObjectMapOffset = 0;
923
+ static const int kMapInstanceTypeOffset = 1 * kApiTaggedSize + kApiInt32Size;
924
+ static const int kStringResourceOffset =
925
+ 1 * kApiTaggedSize + 2 * kApiInt32Size;
926
+
927
+ static const int kOddballKindOffset = 4 * kApiTaggedSize + kApiDoubleSize;
928
+ static const int kJSObjectHeaderSize = 3 * kApiTaggedSize;
929
+ #ifdef V8_COMPRESS_POINTERS
930
+ static const int kJSAPIObjectWithEmbedderSlotsHeaderSize =
931
+ kJSObjectHeaderSize + kApiInt32Size;
932
+ #else // !V8_COMPRESS_POINTERS
933
+ static const int kJSAPIObjectWithEmbedderSlotsHeaderSize =
934
+ kJSObjectHeaderSize + kApiTaggedSize;
935
+ #endif // !V8_COMPRESS_POINTERS
936
+ static const int kFixedArrayHeaderSize = 2 * kApiTaggedSize;
937
+ static const int kEmbedderDataArrayHeaderSize = 2 * kApiTaggedSize;
938
+ static const int kEmbedderDataSlotSize = kApiSystemPointerSize;
939
+ #ifdef V8_ENABLE_SANDBOX
940
+ static const int kEmbedderDataSlotExternalPointerOffset = kApiTaggedSize;
941
+ #else
942
+ static const int kEmbedderDataSlotExternalPointerOffset = 0;
943
+ #endif
944
+ static const int kNativeContextEmbedderDataOffset = 6 * kApiTaggedSize;
945
+ static const int kStringRepresentationAndEncodingMask = 0x0f;
946
+ static const int kStringEncodingMask = 0x8;
947
+ static const int kExternalTwoByteRepresentationTag = 0x02;
948
+ static const int kExternalOneByteRepresentationTag = 0x0a;
949
+
950
+ // AccessorInfo::data and InterceptorInfo::data field.
951
+ static const int kCallbackInfoDataOffset = 1 * kApiTaggedSize;
952
+
953
+ static const uint32_t kNumIsolateDataSlots = 4;
954
+ static const int kStackGuardSize = 8 * kApiSystemPointerSize;
955
+ static const int kNumberOfBooleanFlags = 6;
956
+ static const int kErrorMessageParamSize = 1;
957
+ static const int kTablesAlignmentPaddingSize = 1;
958
+ static const int kRegExpStaticResultOffsetsVectorSize = kApiSystemPointerSize;
959
+ static const int kBuiltinTier0EntryTableSize = 7 * kApiSystemPointerSize;
960
+ static const int kBuiltinTier0TableSize = 7 * kApiSystemPointerSize;
961
+ static const int kLinearAllocationAreaSize = 3 * kApiSystemPointerSize;
962
+ static const int kThreadLocalTopSize = 29 * kApiSystemPointerSize;
963
+ static const int kHandleScopeDataSize =
964
+ 2 * kApiSystemPointerSize + 2 * kApiInt32Size;
965
+
966
+ // ExternalPointerTable and TrustedPointerTable layout guarantees.
967
+ static const int kExternalEntityTableBasePointerOffset = 0;
968
+ static const int kSegmentedTableSegmentPoolSize = 4;
969
+ static const int kExternalEntityTableSize =
970
+ 4 * kApiSystemPointerSize +
971
+ kSegmentedTableSegmentPoolSize * sizeof(uint32_t);
972
+
973
+ // IsolateData layout guarantees.
974
+ static const int kIsolateCageBaseOffset = 0;
975
+ static const int kIsolateStackGuardOffset =
976
+ kIsolateCageBaseOffset + kApiSystemPointerSize;
977
+ static const int kVariousBooleanFlagsOffset =
978
+ kIsolateStackGuardOffset + kStackGuardSize;
979
+ static const int kErrorMessageParamOffset =
980
+ kVariousBooleanFlagsOffset + kNumberOfBooleanFlags;
981
+ static const int kBuiltinTier0EntryTableOffset =
982
+ kErrorMessageParamOffset + kErrorMessageParamSize +
983
+ kTablesAlignmentPaddingSize + kRegExpStaticResultOffsetsVectorSize;
984
+ static const int kBuiltinTier0TableOffset =
985
+ kBuiltinTier0EntryTableOffset + kBuiltinTier0EntryTableSize;
986
+ static const int kNewAllocationInfoOffset =
987
+ kBuiltinTier0TableOffset + kBuiltinTier0TableSize;
988
+ static const int kOldAllocationInfoOffset =
989
+ kNewAllocationInfoOffset + kLinearAllocationAreaSize;
990
+ static const int kLastYoungAllocationOffset =
991
+ kOldAllocationInfoOffset + kApiSystemPointerSize;
992
+
993
+ static const int kFastCCallAlignmentPaddingSize =
994
+ kApiSystemPointerSize == 8 ? 5 * kApiSystemPointerSize
995
+ : 1 * kApiSystemPointerSize;
996
+ static const int kIsolateFastCCallCallerPcOffset =
997
+ kLastYoungAllocationOffset + kLinearAllocationAreaSize +
998
+ kFastCCallAlignmentPaddingSize;
999
+ static const int kIsolateFastCCallCallerFpOffset =
1000
+ kIsolateFastCCallCallerPcOffset + kApiSystemPointerSize;
1001
+ static const int kIsolateFastApiCallTargetOffset =
1002
+ kIsolateFastCCallCallerFpOffset + kApiSystemPointerSize;
1003
+ static const int kIsolateLongTaskStatsCounterOffset =
1004
+ kIsolateFastApiCallTargetOffset + kApiSystemPointerSize;
1005
+ static const int kIsolateThreadLocalTopOffset =
1006
+ kIsolateLongTaskStatsCounterOffset + kApiSizetSize;
1007
+ static const int kIsolateHandleScopeDataOffset =
1008
+ kIsolateThreadLocalTopOffset + kThreadLocalTopSize;
1009
+ static const int kIsolateEmbedderDataOffset =
1010
+ kIsolateHandleScopeDataOffset + kHandleScopeDataSize;
1011
+ #ifdef V8_COMPRESS_POINTERS
1012
+ static const int kIsolateExternalPointerTableOffset =
1013
+ kIsolateEmbedderDataOffset + kNumIsolateDataSlots * kApiSystemPointerSize;
1014
+ static const int kIsolateSharedExternalPointerTableAddressOffset =
1015
+ kIsolateExternalPointerTableOffset + kExternalEntityTableSize;
1016
+ static const int kIsolateCppHeapPointerTableOffset =
1017
+ kIsolateSharedExternalPointerTableAddressOffset + kApiSystemPointerSize;
1018
+ #ifdef V8_ENABLE_SANDBOX
1019
+ static const int kIsolateTrustedCageBaseOffset =
1020
+ kIsolateCppHeapPointerTableOffset + kExternalEntityTableSize;
1021
+ static const int kIsolateTrustedPointerTableOffset =
1022
+ kIsolateTrustedCageBaseOffset + kApiSystemPointerSize;
1023
+ static const int kIsolateSharedTrustedPointerTableAddressOffset =
1024
+ kIsolateTrustedPointerTableOffset + kExternalEntityTableSize;
1025
+ static const int kIsolateTrustedPointerPublishingScopeOffset =
1026
+ kIsolateSharedTrustedPointerTableAddressOffset + kApiSystemPointerSize;
1027
+ static const int kIsolateCodePointerTableBaseAddressOffset =
1028
+ kIsolateTrustedPointerPublishingScopeOffset + kApiSystemPointerSize;
1029
+ static const int kIsolateJSDispatchTableOffset =
1030
+ kIsolateCodePointerTableBaseAddressOffset + kApiSystemPointerSize;
1031
+ #else
1032
+ static const int kIsolateJSDispatchTableOffset =
1033
+ kIsolateCppHeapPointerTableOffset + kExternalEntityTableSize;
1034
+ #endif // V8_ENABLE_SANDBOX
1035
+ #else
1036
+ static const int kIsolateJSDispatchTableOffset =
1037
+ kIsolateEmbedderDataOffset + kNumIsolateDataSlots * kApiSystemPointerSize;
1038
+ #endif // V8_COMPRESS_POINTERS
1039
+ static const int kIsolateApiCallbackThunkArgumentOffset =
1040
+ kIsolateJSDispatchTableOffset + kExternalEntityTableSize;
1041
+ static const int kIsolateRegexpExecVectorArgumentOffset =
1042
+ kIsolateApiCallbackThunkArgumentOffset + kApiSystemPointerSize;
1043
+ static const int kContinuationPreservedEmbedderDataOffset =
1044
+ kIsolateRegexpExecVectorArgumentOffset + kApiSystemPointerSize;
1045
+ static const int kIsolateRootsOffset =
1046
+ kContinuationPreservedEmbedderDataOffset + kApiSystemPointerSize;
1047
+
1048
+ #if V8_TARGET_ARCH_PPC64
1049
+ static constexpr int kFrameCPSlotCount = 1;
1050
+ #else
1051
+ static constexpr int kFrameCPSlotCount = 0;
1052
+ #endif
1053
+
1054
+ #if V8_TARGET_ARCH_ARM64
1055
+ // The padding required to keep SP 16-byte aligned.
1056
+ static constexpr int kSPAlignmentSlotCount = 1;
1057
+ #else
1058
+ static constexpr int kSPAlignmentSlotCount = 0;
1059
+ #endif
1060
+
1061
+ static const int kFrameTypeApiCallExit = 18;
1062
+ static const int kFrameTypeApiConstructExit = 19;
1063
+ static const int kFrameTypeApiNamedAccessorExit = 20;
1064
+ static const int kFrameTypeApiIndexedAccessorExit = 21;
1065
+
1066
+ // Assert scopes
1067
+ static const int kDisallowGarbageCollectionAlign = alignof(uint32_t);
1068
+ static const int kDisallowGarbageCollectionSize = sizeof(uint32_t);
1069
+
1070
+ #if V8_STATIC_ROOTS_BOOL
1071
+
1072
+ // These constants are copied from static-roots.h and guarded by static asserts.
1073
+ #define EXPORTED_STATIC_ROOTS_PTR_LIST(V) \
1074
+ V(UndefinedValue, 0x11) \
1075
+ V(NullValue, 0x2d) \
1076
+ V(TrueValue, 0x71) \
1077
+ V(FalseValue, 0x55) \
1078
+ V(EmptyString, 0x49) \
1079
+ /* The Hole moves around depending on build flags, so define it */ \
1080
+ /* separately inside StaticReadOnlyRoot using build macros */ \
1081
+ V(TheHoleValue, kBuildDependentTheHoleValue)
1082
+
1083
+ using Tagged_t = uint32_t;
1084
+ struct StaticReadOnlyRoot {
1085
+ #ifdef V8_ENABLE_WEBASSEMBLY
1086
+ static constexpr Tagged_t kBuildDependentTheHoleValue = 0x2fffd;
1087
+ #else
1088
+ static constexpr Tagged_t kBuildDependentTheHoleValue = 0xfffd;
1089
+ #endif
1090
+
1091
+ #define DEF_ROOT(name, value) static constexpr Tagged_t k##name = value;
1092
+ EXPORTED_STATIC_ROOTS_PTR_LIST(DEF_ROOT)
1093
+ #undef DEF_ROOT
1094
+
1095
+ // Use 0 for kStringMapLowerBound since string maps are the first maps.
1096
+ static constexpr Tagged_t kStringMapLowerBound = 0;
1097
+ static constexpr Tagged_t kStringMapUpperBound = 0x425;
1098
+
1099
+ #define PLUSONE(...) +1
1100
+ static constexpr size_t kNumberOfExportedStaticRoots =
1101
+ 2 + EXPORTED_STATIC_ROOTS_PTR_LIST(PLUSONE);
1102
+ #undef PLUSONE
1103
+ };
1104
+
1105
+ #endif // V8_STATIC_ROOTS_BOOL
1106
+
1107
+ static const int kUndefinedValueRootIndex = 0;
1108
+ static const int kTheHoleValueRootIndex = 1;
1109
+ static const int kNullValueRootIndex = 2;
1110
+ static const int kTrueValueRootIndex = 3;
1111
+ static const int kFalseValueRootIndex = 4;
1112
+ static const int kEmptyStringRootIndex = 5;
1113
+
1114
+ static const int kNodeClassIdOffset = 1 * kApiSystemPointerSize;
1115
+ static const int kNodeFlagsOffset = 1 * kApiSystemPointerSize + 3;
1116
+ static const int kNodeStateMask = 0x3;
1117
+ static const int kNodeStateIsWeakValue = 2;
1118
+
1119
+ static const int kFirstNonstringType = 0x80;
1120
+ static const int kOddballType = 0x83;
1121
+ static const int kForeignType = 0xcc;
1122
+ static const int kJSSpecialApiObjectType = 0x410;
1123
+ static const int kJSObjectType = 0x421;
1124
+ static const int kFirstJSApiObjectType = 0x422;
1125
+ static const int kLastJSApiObjectType = 0x80A;
1126
+ // Defines a range [kFirstEmbedderJSApiObjectType, kJSApiObjectTypesCount]
1127
+ // of JSApiObject instance type values that an embedder can use.
1128
+ static const int kFirstEmbedderJSApiObjectType = 0;
1129
+ static const int kLastEmbedderJSApiObjectType =
1130
+ kLastJSApiObjectType - kFirstJSApiObjectType;
1131
+
1132
+ static const int kUndefinedOddballKind = 4;
1133
+ static const int kNullOddballKind = 3;
1134
+
1135
+ // Constants used by PropertyCallbackInfo to check if we should throw when an
1136
+ // error occurs.
1137
+ static const int kDontThrow = 0;
1138
+ static const int kThrowOnError = 1;
1139
+ static const int kInferShouldThrowMode = 2;
1140
+
1141
+ // Soft limit for AdjustAmountofExternalAllocatedMemory. Trigger an
1142
+ // incremental GC once the external memory reaches this limit.
1143
+ static constexpr size_t kExternalAllocationSoftLimit = 64 * 1024 * 1024;
1144
+
1145
+ #ifdef V8_MAP_PACKING
1146
+ static const uintptr_t kMapWordMetadataMask = 0xffffULL << 48;
1147
+ // The lowest two bits of mapwords are always `0b10`
1148
+ static const uintptr_t kMapWordSignature = 0b10;
1149
+ // XORing a (non-compressed) map with this mask ensures that the two
1150
+ // low-order bits are 0b10. The 0 at the end makes this look like a Smi,
1151
+ // although real Smis have all lower 32 bits unset. We only rely on these
1152
+ // values passing as Smis in very few places.
1153
+ static const int kMapWordXorMask = 0b11;
1154
+ #endif
1155
+
1156
+ V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
1157
+ V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
1158
+ #ifdef V8_ENABLE_CHECKS
1159
+ CheckInitializedImpl(isolate);
1160
+ #endif
1161
+ }
1162
+
1163
+ V8_INLINE static constexpr bool HasHeapObjectTag(Address value) {
1164
+ return (value & kHeapObjectTagMask) == static_cast<Address>(kHeapObjectTag);
1165
+ }
1166
+
1167
+ V8_INLINE static constexpr int SmiValue(Address value) {
1168
+ return PlatformSmiTagging::SmiToInt(value);
1169
+ }
1170
+
1171
+ V8_INLINE static constexpr Address AddressToSmi(Address value) {
1172
+ return (value << (kSmiTagSize + PlatformSmiTagging::kSmiShiftSize)) |
1173
+ kSmiTag;
1174
+ }
1175
+
1176
+ V8_INLINE static constexpr Address IntToSmi(int value) {
1177
+ return AddressToSmi(static_cast<Address>(value));
1178
+ }
1179
+
1180
+ template <typename T,
1181
+ typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
1182
+ V8_INLINE static constexpr Address IntegralToSmi(T value) {
1183
+ return AddressToSmi(static_cast<Address>(value));
1184
+ }
1185
+
1186
+ template <typename T,
1187
+ typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
1188
+ V8_INLINE static constexpr bool IsValidSmi(T value) {
1189
+ return PlatformSmiTagging::IsValidSmi(value);
1190
+ }
1191
+
1192
+ template <typename T,
1193
+ typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
1194
+ static constexpr std::optional<Address> TryIntegralToSmi(T value) {
1195
+ if (V8_LIKELY(PlatformSmiTagging::IsValidSmi(value))) {
1196
+ return {AddressToSmi(static_cast<Address>(value))};
1197
+ }
1198
+ return {};
1199
+ }
1200
+
1201
+ #if V8_STATIC_ROOTS_BOOL
1202
+ V8_INLINE static bool is_identical(Address obj, Tagged_t constant) {
1203
+ return static_cast<Tagged_t>(obj) == constant;
1204
+ }
1205
+
1206
+ V8_INLINE static bool CheckInstanceMapRange(Address obj, Tagged_t first_map,
1207
+ Tagged_t last_map) {
1208
+ auto map = ReadRawField<Tagged_t>(obj, kHeapObjectMapOffset);
1209
+ #ifdef V8_MAP_PACKING
1210
+ map = UnpackMapWord(map);
1211
+ #endif
1212
+ return map >= first_map && map <= last_map;
1213
+ }
1214
+ #endif
1215
+
1216
+ V8_INLINE static int GetInstanceType(Address obj) {
1217
+ Address map = ReadTaggedPointerField(obj, kHeapObjectMapOffset);
1218
+ #ifdef V8_MAP_PACKING
1219
+ map = UnpackMapWord(map);
1220
+ #endif
1221
+ return ReadRawField<uint16_t>(map, kMapInstanceTypeOffset);
1222
+ }
1223
+
1224
+ V8_INLINE static Address LoadMap(Address obj) {
1225
+ if (!HasHeapObjectTag(obj)) return kNullAddress;
1226
+ Address map = ReadTaggedPointerField(obj, kHeapObjectMapOffset);
1227
+ #ifdef V8_MAP_PACKING
1228
+ map = UnpackMapWord(map);
1229
+ #endif
1230
+ return map;
1231
+ }
1232
+
1233
+ V8_INLINE static int GetOddballKind(Address obj) {
1234
+ return SmiValue(ReadTaggedSignedField(obj, kOddballKindOffset));
1235
+ }
1236
+
1237
+ V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
1238
+ int representation = (instance_type & kStringRepresentationAndEncodingMask);
1239
+ return representation == kExternalTwoByteRepresentationTag;
1240
+ }
1241
+
1242
+ V8_INLINE static constexpr bool CanHaveInternalField(int instance_type) {
1243
+ static_assert(kJSObjectType + 1 == kFirstJSApiObjectType);
1244
+ static_assert(kJSObjectType < kLastJSApiObjectType);
1245
+ static_assert(kFirstJSApiObjectType < kLastJSApiObjectType);
1246
+ // Check for IsJSObject() || IsJSSpecialApiObject() || IsJSApiObject()
1247
+ return instance_type == kJSSpecialApiObjectType ||
1248
+ // inlined version of base::IsInRange
1249
+ (static_cast<unsigned>(static_cast<unsigned>(instance_type) -
1250
+ static_cast<unsigned>(kJSObjectType)) <=
1251
+ static_cast<unsigned>(kLastJSApiObjectType - kJSObjectType));
1252
+ }
1253
+
1254
+ V8_INLINE static uint8_t GetNodeFlag(Address* obj, int shift) {
1255
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
1256
+ return *addr & static_cast<uint8_t>(1U << shift);
1257
+ }
1258
+
1259
+ V8_INLINE static void UpdateNodeFlag(Address* obj, bool value, int shift) {
1260
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
1261
+ uint8_t mask = static_cast<uint8_t>(1U << shift);
1262
+ *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
1263
+ }
1264
+
1265
+ V8_INLINE static uint8_t GetNodeState(Address* obj) {
1266
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
1267
+ return *addr & kNodeStateMask;
1268
+ }
1269
+
1270
+ V8_INLINE static void UpdateNodeState(Address* obj, uint8_t value) {
1271
+ uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
1272
+ *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
1273
+ }
1274
+
1275
+ V8_INLINE static void SetEmbedderData(v8::Isolate* isolate, uint32_t slot,
1276
+ void* data) {
1277
+ Address addr = reinterpret_cast<Address>(isolate) +
1278
+ kIsolateEmbedderDataOffset + slot * kApiSystemPointerSize;
1279
+ *reinterpret_cast<void**>(addr) = data;
1280
+ }
1281
+
1282
+ V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
1283
+ uint32_t slot) {
1284
+ Address addr = reinterpret_cast<Address>(isolate) +
1285
+ kIsolateEmbedderDataOffset + slot * kApiSystemPointerSize;
1286
+ return *reinterpret_cast<void* const*>(addr);
1287
+ }
1288
+
1289
+ V8_INLINE static HandleScopeData* GetHandleScopeData(v8::Isolate* isolate) {
1290
+ Address addr =
1291
+ reinterpret_cast<Address>(isolate) + kIsolateHandleScopeDataOffset;
1292
+ return reinterpret_cast<HandleScopeData*>(addr);
1293
+ }
1294
+
1295
+ V8_INLINE static void IncrementLongTasksStatsCounter(v8::Isolate* isolate) {
1296
+ Address addr =
1297
+ reinterpret_cast<Address>(isolate) + kIsolateLongTaskStatsCounterOffset;
1298
+ ++(*reinterpret_cast<size_t*>(addr));
1299
+ }
1300
+
1301
+ V8_INLINE static Address* GetRootSlot(v8::Isolate* isolate, int index) {
1302
+ Address addr = reinterpret_cast<Address>(isolate) + kIsolateRootsOffset +
1303
+ index * kApiSystemPointerSize;
1304
+ return reinterpret_cast<Address*>(addr);
1305
+ }
1306
+
1307
+ V8_INLINE static Address GetRoot(v8::Isolate* isolate, int index) {
1308
+ #if V8_STATIC_ROOTS_BOOL
1309
+ Address base = *reinterpret_cast<Address*>(
1310
+ reinterpret_cast<uintptr_t>(isolate) + kIsolateCageBaseOffset);
1311
+ switch (index) {
1312
+ #define DECOMPRESS_ROOT(name, ...) \
1313
+ case k##name##RootIndex: \
1314
+ return base + StaticReadOnlyRoot::k##name;
1315
+ EXPORTED_STATIC_ROOTS_PTR_LIST(DECOMPRESS_ROOT)
1316
+ #undef DECOMPRESS_ROOT
1317
+ #undef EXPORTED_STATIC_ROOTS_PTR_LIST
1318
+ default:
1319
+ break;
1320
+ }
1321
+ #endif // V8_STATIC_ROOTS_BOOL
1322
+ return *GetRootSlot(isolate, index);
1323
+ }
1324
+
1325
+ #ifdef V8_ENABLE_SANDBOX
1326
+ V8_INLINE static Address* GetExternalPointerTableBase(v8::Isolate* isolate) {
1327
+ Address addr = reinterpret_cast<Address>(isolate) +
1328
+ kIsolateExternalPointerTableOffset +
1329
+ kExternalEntityTableBasePointerOffset;
1330
+ return *reinterpret_cast<Address**>(addr);
1331
+ }
1332
+
1333
+ V8_INLINE static Address* GetSharedExternalPointerTableBase(
1334
+ v8::Isolate* isolate) {
1335
+ Address addr = reinterpret_cast<Address>(isolate) +
1336
+ kIsolateSharedExternalPointerTableAddressOffset;
1337
+ addr = *reinterpret_cast<Address*>(addr);
1338
+ addr += kExternalEntityTableBasePointerOffset;
1339
+ return *reinterpret_cast<Address**>(addr);
1340
+ }
1341
+ #endif
1342
+
1343
+ template <typename T>
1344
+ V8_INLINE static T ReadRawField(Address heap_object_ptr, int offset) {
1345
+ Address addr = heap_object_ptr + offset - kHeapObjectTag;
1346
+ #ifdef V8_COMPRESS_POINTERS
1347
+ if constexpr (sizeof(T) > kApiTaggedSize) {
1348
+ // TODO(ishell, v8:8875): When pointer compression is enabled 8-byte size
1349
+ // fields (external pointers, doubles and BigInt data) are only
1350
+ // kTaggedSize aligned so we have to use unaligned pointer friendly way of
1351
+ // accessing them in order to avoid undefined behavior in C++ code.
1352
+ T r;
1353
+ memcpy(&r, reinterpret_cast<void*>(addr), sizeof(T));
1354
+ return r;
1355
+ }
1356
+ #endif
1357
+ return *reinterpret_cast<const T*>(addr);
1358
+ }
1359
+
1360
+ V8_INLINE static Address ReadTaggedPointerField(Address heap_object_ptr,
1361
+ int offset) {
1362
+ #ifdef V8_COMPRESS_POINTERS
1363
+ uint32_t value = ReadRawField<uint32_t>(heap_object_ptr, offset);
1364
+ Address base = GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr);
1365
+ return base + static_cast<Address>(static_cast<uintptr_t>(value));
1366
+ #else
1367
+ return ReadRawField<Address>(heap_object_ptr, offset);
1368
+ #endif
1369
+ }
1370
+
1371
+ V8_INLINE static Address ReadTaggedSignedField(Address heap_object_ptr,
1372
+ int offset) {
1373
+ #ifdef V8_COMPRESS_POINTERS
1374
+ uint32_t value = ReadRawField<uint32_t>(heap_object_ptr, offset);
1375
+ return static_cast<Address>(static_cast<uintptr_t>(value));
1376
+ #else
1377
+ return ReadRawField<Address>(heap_object_ptr, offset);
1378
+ #endif
1379
+ }
1380
+
1381
+ // Returns v8::Isolate::Current(), but without needing to include the
1382
+ // v8-isolate.h header.
1383
+ V8_EXPORT static v8::Isolate* GetCurrentIsolate();
1384
+
1385
+ V8_INLINE static v8::Isolate* GetCurrentIsolateForSandbox() {
1386
+ #ifdef V8_ENABLE_SANDBOX
1387
+ return GetCurrentIsolate();
1388
+ #else
1389
+ // Not used in non-sandbox mode.
1390
+ return nullptr;
1391
+ #endif
1392
+ }
1393
+
1394
+ template <ExternalPointerTagRange tag_range>
1395
+ V8_INLINE static Address ReadExternalPointerField(v8::Isolate* isolate,
1396
+ Address heap_object_ptr,
1397
+ int offset) {
1398
+ #ifdef V8_ENABLE_SANDBOX
1399
+ static_assert(!tag_range.IsEmpty());
1400
+ // See src/sandbox/external-pointer-table.h. Logic duplicated here so
1401
+ // it can be inlined and doesn't require an additional call.
1402
+ Address* table = IsSharedExternalPointerType(tag_range)
1403
+ ? GetSharedExternalPointerTableBase(isolate)
1404
+ : GetExternalPointerTableBase(isolate);
1405
+ internal::ExternalPointerHandle handle =
1406
+ ReadRawField<ExternalPointerHandle>(heap_object_ptr, offset);
1407
+ uint32_t index = handle >> kExternalPointerIndexShift;
1408
+ std::atomic<Address>* ptr =
1409
+ reinterpret_cast<std::atomic<Address>*>(&table[index]);
1410
+ Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
1411
+ ExternalPointerTag actual_tag = static_cast<ExternalPointerTag>(
1412
+ (entry & kExternalPointerTagMask) >> kExternalPointerTagShift);
1413
+ if (V8_LIKELY(tag_range.Contains(actual_tag))) {
1414
+ return entry & kExternalPointerPayloadMask;
1415
+ } else {
1416
+ return 0;
1417
+ }
1418
+ return entry;
1419
+ #else
1420
+ return ReadRawField<Address>(heap_object_ptr, offset);
1421
+ #endif // V8_ENABLE_SANDBOX
1422
+ }
1423
+
1424
+ V8_INLINE static Address ReadExternalPointerField(
1425
+ v8::Isolate* isolate, Address heap_object_ptr, int offset,
1426
+ ExternalPointerTagRange tag_range) {
1427
+ #ifdef V8_ENABLE_SANDBOX
1428
+ // See src/sandbox/external-pointer-table.h. Logic duplicated here so
1429
+ // it can be inlined and doesn't require an additional call.
1430
+ Address* table = IsSharedExternalPointerType(tag_range)
1431
+ ? GetSharedExternalPointerTableBase(isolate)
1432
+ : GetExternalPointerTableBase(isolate);
1433
+ internal::ExternalPointerHandle handle =
1434
+ ReadRawField<ExternalPointerHandle>(heap_object_ptr, offset);
1435
+ uint32_t index = handle >> kExternalPointerIndexShift;
1436
+ std::atomic<Address>* ptr =
1437
+ reinterpret_cast<std::atomic<Address>*>(&table[index]);
1438
+ Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
1439
+ ExternalPointerTag actual_tag = static_cast<ExternalPointerTag>(
1440
+ (entry & kExternalPointerTagMask) >> kExternalPointerTagShift);
1441
+ if (V8_LIKELY(tag_range.Contains(actual_tag))) {
1442
+ return entry & kExternalPointerPayloadMask;
1443
+ } else {
1444
+ return 0;
1445
+ }
1446
+ return entry;
1447
+ #else
1448
+ return ReadRawField<Address>(heap_object_ptr, offset);
1449
+ #endif // V8_ENABLE_SANDBOX
1450
+ }
1451
+
1452
+ #ifdef V8_COMPRESS_POINTERS
1453
+ V8_INLINE static Address GetPtrComprCageBaseFromOnHeapAddress(Address addr) {
1454
+ return addr & -static_cast<intptr_t>(kPtrComprCageBaseAlignment);
1455
+ }
1456
+
1457
+ V8_INLINE static uint32_t CompressTagged(Address value) {
1458
+ return static_cast<uint32_t>(value);
1459
+ }
1460
+
1461
+ V8_INLINE static Address DecompressTaggedField(Address heap_object_ptr,
1462
+ uint32_t value) {
1463
+ Address base = GetPtrComprCageBaseFromOnHeapAddress(heap_object_ptr);
1464
+ return base + static_cast<Address>(static_cast<uintptr_t>(value));
1465
+ }
1466
+
1467
+ #endif // V8_COMPRESS_POINTERS
1468
+ };
1469
+
1470
+ // Only perform cast check for types derived from v8::Data since
1471
+ // other types do not implement the Cast method.
1472
+ template <bool PerformCheck>
1473
+ struct CastCheck {
1474
+ template <class T>
1475
+ static void Perform(T* data);
1476
+ };
1477
+
1478
+ template <>
1479
+ template <class T>
1480
+ void CastCheck<true>::Perform(T* data) {
1481
+ T::Cast(data);
1482
+ }
1483
+
1484
+ template <>
1485
+ template <class T>
1486
+ void CastCheck<false>::Perform(T* data) {}
1487
+
1488
+ template <class T>
1489
+ V8_INLINE void PerformCastCheck(T* data) {
1490
+ CastCheck<std::is_base_of_v<Data, T> &&
1491
+ !std::is_same_v<Data, std::remove_cv_t<T>>>::Perform(data);
1492
+ }
1493
+
1494
+ // A base class for backing stores, which is needed due to vagaries of
1495
+ // how static casts work with std::shared_ptr.
1496
+ class BackingStoreBase {};
1497
+
1498
+ // The maximum value in enum GarbageCollectionReason, defined in heap.h.
1499
+ // This is needed for histograms sampling garbage collection reasons.
1500
+ constexpr int kGarbageCollectionReasonMaxValue = 30;
1501
+
1502
+ // Base class for the address block allocator compatible with standard
1503
+ // containers, which registers its allocated range as strong roots.
1504
+ class V8_EXPORT StrongRootAllocatorBase {
1505
+ public:
1506
+ Heap* heap() const { return heap_; }
1507
+
1508
+ constexpr bool operator==(const StrongRootAllocatorBase&) const = default;
1509
+
1510
+ protected:
1511
+ explicit StrongRootAllocatorBase(Heap* heap) : heap_(heap) {}
1512
+ explicit StrongRootAllocatorBase(LocalHeap* heap);
1513
+ explicit StrongRootAllocatorBase(Isolate* isolate);
1514
+ explicit StrongRootAllocatorBase(v8::Isolate* isolate);
1515
+ explicit StrongRootAllocatorBase(LocalIsolate* isolate);
1516
+
1517
+ // Allocate/deallocate a range of n elements of type internal::Address.
1518
+ Address* allocate_impl(size_t n);
1519
+ void deallocate_impl(Address* p, size_t n) noexcept;
1520
+
1521
+ private:
1522
+ Heap* heap_;
1523
+ };
1524
+
1525
+ // The general version of this template behaves just as std::allocator, with
1526
+ // the exception that the constructor takes the isolate as parameter. Only
1527
+ // specialized versions, e.g., internal::StrongRootAllocator<internal::Address>
1528
+ // and internal::StrongRootAllocator<v8::Local<T>> register the allocated range
1529
+ // as strong roots.
1530
+ template <typename T>
1531
+ class StrongRootAllocator : private std::allocator<T> {
1532
+ public:
1533
+ using value_type = T;
1534
+
1535
+ template <typename HeapOrIsolateT>
1536
+ explicit StrongRootAllocator(HeapOrIsolateT*) {}
1537
+ template <typename U>
1538
+ StrongRootAllocator(const StrongRootAllocator<U>& other) noexcept {}
1539
+
1540
+ using std::allocator<T>::allocate;
1541
+ using std::allocator<T>::deallocate;
1542
+ };
1543
+
1544
+ template <typename Iterator>
1545
+ concept HasIteratorConcept = requires { typename Iterator::iterator_concept; };
1546
+
1547
+ template <typename Iterator>
1548
+ concept HasIteratorCategory =
1549
+ requires { typename Iterator::iterator_category; };
1550
+
1551
+ // Helper struct that contains an `iterator_concept` type alias only when either
1552
+ // `Iterator` or `std::iterator_traits<Iterator>` do.
1553
+ // Default: no alias.
1554
+ template <typename Iterator>
1555
+ struct MaybeDefineIteratorConcept {};
1556
+ // Use `Iterator::iterator_concept` if available.
1557
+ template <HasIteratorConcept Iterator>
1558
+ struct MaybeDefineIteratorConcept<Iterator> {
1559
+ using iterator_concept = typename Iterator::iterator_concept;
1560
+ };
1561
+ // Otherwise fall back to `std::iterator_traits<Iterator>` if possible.
1562
+ template <typename Iterator>
1563
+ requires(HasIteratorCategory<Iterator> && !HasIteratorConcept<Iterator>)
1564
+ struct MaybeDefineIteratorConcept<Iterator> {
1565
+ using iterator_concept =
1566
+ typename std::iterator_traits<Iterator>::iterator_concept;
1567
+ };
1568
+
1569
+ // A class of iterators that wrap some different iterator type.
1570
+ // If specified, ElementType is the type of element accessed by the wrapper
1571
+ // iterator; in this case, the actual reference and pointer types of Iterator
1572
+ // must be convertible to ElementType& and ElementType*, respectively.
1573
+ template <typename Iterator, typename ElementType = void>
1574
+ class WrappedIterator : public MaybeDefineIteratorConcept<Iterator> {
1575
+ public:
1576
+ static_assert(
1577
+ std::is_void_v<ElementType> ||
1578
+ (std::is_convertible_v<typename std::iterator_traits<Iterator>::pointer,
1579
+ std::add_pointer_t<ElementType>> &&
1580
+ std::is_convertible_v<typename std::iterator_traits<Iterator>::reference,
1581
+ std::add_lvalue_reference_t<ElementType>>));
1582
+
1583
+ using difference_type =
1584
+ typename std::iterator_traits<Iterator>::difference_type;
1585
+ using value_type =
1586
+ std::conditional_t<std::is_void_v<ElementType>,
1587
+ typename std::iterator_traits<Iterator>::value_type,
1588
+ ElementType>;
1589
+ using pointer =
1590
+ std::conditional_t<std::is_void_v<ElementType>,
1591
+ typename std::iterator_traits<Iterator>::pointer,
1592
+ std::add_pointer_t<ElementType>>;
1593
+ using reference =
1594
+ std::conditional_t<std::is_void_v<ElementType>,
1595
+ typename std::iterator_traits<Iterator>::reference,
1596
+ std::add_lvalue_reference_t<ElementType>>;
1597
+ using iterator_category =
1598
+ typename std::iterator_traits<Iterator>::iterator_category;
1599
+
1600
+ constexpr WrappedIterator() noexcept = default;
1601
+ constexpr explicit WrappedIterator(Iterator it) noexcept : it_(it) {}
1602
+
1603
+ template <typename OtherIterator, typename OtherElementType>
1604
+ requires std::is_convertible_v<OtherIterator, Iterator>
1605
+ constexpr WrappedIterator(
1606
+ const WrappedIterator<OtherIterator, OtherElementType>& other) noexcept
1607
+ : it_(other.base()) {}
1608
+
1609
+ [[nodiscard]] constexpr reference operator*() const noexcept { return *it_; }
1610
+ [[nodiscard]] constexpr pointer operator->() const noexcept {
1611
+ if constexpr (std::is_pointer_v<Iterator>) {
1612
+ return it_;
1613
+ } else {
1614
+ return it_.operator->();
1615
+ }
1616
+ }
1617
+
1618
+ template <typename OtherIterator, typename OtherElementType>
1619
+ [[nodiscard]] constexpr bool operator==(
1620
+ const WrappedIterator<OtherIterator, OtherElementType>& other)
1621
+ const noexcept {
1622
+ return it_ == other.base();
1623
+ }
1624
+
1625
+ template <typename OtherIterator, typename OtherElementType>
1626
+ [[nodiscard]] constexpr auto operator<=>(
1627
+ const WrappedIterator<OtherIterator, OtherElementType>& other)
1628
+ const noexcept {
1629
+ if constexpr (std::three_way_comparable_with<Iterator, OtherIterator>) {
1630
+ return it_ <=> other.base();
1631
+ } else if constexpr (std::totally_ordered_with<Iterator, OtherIterator>) {
1632
+ if (it_ < other.base()) {
1633
+ return std::strong_ordering::less;
1634
+ }
1635
+ return (it_ > other.base()) ? std::strong_ordering::greater
1636
+ : std::strong_ordering::equal;
1637
+ } else {
1638
+ if (it_ < other.base()) {
1639
+ return std::partial_ordering::less;
1640
+ }
1641
+ if (other.base() < it_) {
1642
+ return std::partial_ordering::greater;
1643
+ }
1644
+ return (it_ == other.base()) ? std::partial_ordering::equivalent
1645
+ : std::partial_ordering::unordered;
1646
+ }
1647
+ }
1648
+
1649
+ constexpr WrappedIterator& operator++() noexcept {
1650
+ ++it_;
1651
+ return *this;
1652
+ }
1653
+ constexpr WrappedIterator operator++(int) noexcept {
1654
+ WrappedIterator result(*this);
1655
+ ++(*this);
1656
+ return result;
1657
+ }
1658
+
1659
+ constexpr WrappedIterator& operator--() noexcept {
1660
+ --it_;
1661
+ return *this;
1662
+ }
1663
+ constexpr WrappedIterator operator--(int) noexcept {
1664
+ WrappedIterator result(*this);
1665
+ --(*this);
1666
+ return result;
1667
+ }
1668
+ [[nodiscard]] constexpr WrappedIterator operator+(
1669
+ difference_type n) const noexcept {
1670
+ WrappedIterator result(*this);
1671
+ result += n;
1672
+ return result;
1673
+ }
1674
+ [[nodiscard]] friend constexpr WrappedIterator operator+(
1675
+ difference_type n, const WrappedIterator& x) noexcept {
1676
+ return x + n;
1677
+ }
1678
+ constexpr WrappedIterator& operator+=(difference_type n) noexcept {
1679
+ it_ += n;
1680
+ return *this;
1681
+ }
1682
+ [[nodiscard]] constexpr WrappedIterator operator-(
1683
+ difference_type n) const noexcept {
1684
+ return *this + -n;
1685
+ }
1686
+ constexpr WrappedIterator& operator-=(difference_type n) noexcept {
1687
+ return *this += -n;
1688
+ }
1689
+ template <typename OtherIterator, typename OtherElementType>
1690
+ [[nodiscard]] constexpr auto operator-(
1691
+ const WrappedIterator<OtherIterator, OtherElementType>& other)
1692
+ const noexcept {
1693
+ return it_ - other.base();
1694
+ }
1695
+ [[nodiscard]] constexpr reference operator[](
1696
+ difference_type n) const noexcept {
1697
+ return it_[n];
1698
+ }
1699
+
1700
+ [[nodiscard]] constexpr const Iterator& base() const noexcept { return it_; }
1701
+
1702
+ private:
1703
+ Iterator it_;
1704
+ };
1705
+
1706
+ // Helper functions about values contained in handles.
1707
+ // A value is either an indirect pointer or a direct pointer, depending on
1708
+ // whether direct local support is enabled.
1709
+ class ValueHelper final {
1710
+ public:
1711
+ // ValueHelper::InternalRepresentationType is an abstract type that
1712
+ // corresponds to the internal representation of v8::Local and essentially
1713
+ // to what T* really is (these two are always in sync). This type is used in
1714
+ // methods like GetDataFromSnapshotOnce that need access to a handle's
1715
+ // internal representation. In particular, if `x` is a `v8::Local<T>`, then
1716
+ // `v8::Local<T>::FromRepr(x.repr())` gives exactly the same handle as `x`.
1717
+ #ifdef V8_ENABLE_DIRECT_HANDLE
1718
+ static constexpr Address kTaggedNullAddress = 1;
1719
+
1720
+ using InternalRepresentationType = internal::Address;
1721
+ static constexpr InternalRepresentationType kEmpty = kTaggedNullAddress;
1722
+ #else
1723
+ using InternalRepresentationType = internal::Address*;
1724
+ static constexpr InternalRepresentationType kEmpty = nullptr;
1725
+ #endif // V8_ENABLE_DIRECT_HANDLE
1726
+
1727
+ template <typename T>
1728
+ V8_INLINE static bool IsEmpty(T* value) {
1729
+ return ValueAsRepr(value) == kEmpty;
1730
+ }
1731
+
1732
+ // Returns a handle's "value" for all kinds of abstract handles. For Local,
1733
+ // it is equivalent to `*handle`. The variadic parameters support handle
1734
+ // types with extra type parameters, like `Persistent<T, M>`.
1735
+ template <template <typename T, typename... Ms> typename H, typename T,
1736
+ typename... Ms>
1737
+ V8_INLINE static T* HandleAsValue(const H<T, Ms...>& handle) {
1738
+ return handle.template value<T>();
1739
+ }
1740
+
1741
+ #ifdef V8_ENABLE_DIRECT_HANDLE
1742
+
1743
+ template <typename T>
1744
+ V8_INLINE static Address ValueAsAddress(const T* value) {
1745
+ return reinterpret_cast<Address>(value);
1746
+ }
1747
+
1748
+ template <typename T, bool check_null = true, typename S>
1749
+ V8_INLINE static T* SlotAsValue(S* slot) {
1750
+ if (check_null && slot == nullptr) {
1751
+ return reinterpret_cast<T*>(kTaggedNullAddress);
1752
+ }
1753
+ return *reinterpret_cast<T**>(slot);
1754
+ }
1755
+
1756
+ template <typename T>
1757
+ V8_INLINE static InternalRepresentationType ValueAsRepr(const T* value) {
1758
+ return reinterpret_cast<InternalRepresentationType>(value);
1759
+ }
1760
+
1761
+ template <typename T>
1762
+ V8_INLINE static T* ReprAsValue(InternalRepresentationType repr) {
1763
+ return reinterpret_cast<T*>(repr);
1764
+ }
1765
+
1766
+ #else // !V8_ENABLE_DIRECT_HANDLE
1767
+
1768
+ template <typename T>
1769
+ V8_INLINE static Address ValueAsAddress(const T* value) {
1770
+ return *reinterpret_cast<const Address*>(value);
1771
+ }
1772
+
1773
+ template <typename T, bool check_null = true, typename S>
1774
+ V8_INLINE static T* SlotAsValue(S* slot) {
1775
+ return reinterpret_cast<T*>(slot);
1776
+ }
1777
+
1778
+ template <typename T>
1779
+ V8_INLINE static InternalRepresentationType ValueAsRepr(const T* value) {
1780
+ return const_cast<InternalRepresentationType>(
1781
+ reinterpret_cast<const Address*>(value));
1782
+ }
1783
+
1784
+ template <typename T>
1785
+ V8_INLINE static T* ReprAsValue(InternalRepresentationType repr) {
1786
+ return reinterpret_cast<T*>(repr);
1787
+ }
1788
+
1789
+ #endif // V8_ENABLE_DIRECT_HANDLE
1790
+ };
1791
+
1792
+ /**
1793
+ * Helper functions about handles.
1794
+ */
1795
+ class HandleHelper final {
1796
+ public:
1797
+ /**
1798
+ * Checks whether two handles are equal.
1799
+ * They are equal iff they are both empty or they are both non-empty and the
1800
+ * objects to which they refer are physically equal.
1801
+ *
1802
+ * If both handles refer to JS objects, this is the same as strict equality.
1803
+ * For primitives, such as numbers or strings, a `false` return value does not
1804
+ * indicate that the values aren't equal in the JavaScript sense.
1805
+ * Use `Value::StrictEquals()` to check primitives for equality.
1806
+ */
1807
+ template <typename T1, typename T2>
1808
+ V8_INLINE static bool EqualHandles(const T1& lhs, const T2& rhs) {
1809
+ if (lhs.IsEmpty()) return rhs.IsEmpty();
1810
+ if (rhs.IsEmpty()) return false;
1811
+ return lhs.ptr() == rhs.ptr();
1812
+ }
1813
+ };
1814
+
1815
+ V8_EXPORT void VerifyHandleIsNonEmpty(bool is_empty);
1816
+
1817
+ // These functions are here just to match friend declarations in
1818
+ // XxxCallbackInfo classes allowing these functions to access the internals
1819
+ // of the info objects. These functions are supposed to be called by debugger
1820
+ // macros.
1821
+ void PrintFunctionCallbackInfo(void* function_callback_info);
1822
+ void PrintPropertyCallbackInfo(void* property_callback_info);
1823
+
1824
+ } // namespace internal
1825
+ } // namespace v8
1826
+
1827
+ #endif // INCLUDE_V8_INTERNAL_H_