Loading...
Searching...
No Matches
v8-sandbox.h
Go to the documentation of this file.
1// Copyright 2024 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_SANDBOX_H_
6#define INCLUDE_V8_SANDBOX_H_
7
8#include <cstdint>
9
10#include "v8-internal.h" // NOLINT(build/include_directory)
11#include "v8config.h" // NOLINT(build/include_directory)
12
13namespace v8 {
14
28enum class CppHeapPointerTag : uint16_t {
29 kFirstTag = 0,
30 kNullTag = 0,
31
56 kDefaultTag = 0x7000,
57
58 kZappedEntryTag = 0x7ffd,
59 kEvacuationEntryTag = 0x7ffe,
60 kFreeEntryTag = 0x7fff,
61 // The tags are limited to 15 bits, so the last tag is 0x7fff.
62 kLastTag = 0x7fff,
63};
64
65// Convenience struct to represent tag ranges. This is used for type checks
66// against supertypes, which cover a range of types (their subtypes).
67// Both the lower- and the upper bound are inclusive. In other words, this
68// struct represents the range [lower_bound, upper_bound].
72 : lower_bound(lower), upper_bound(upper) {}
75
76 // Check whether the tag of the given CppHeapPointerTable entry is within
77 // this range. This method encodes implementation details of the
78 // CppHeapPointerTable, which is necessary as it is used by
79 // ReadCppHeapPointerField below.
80 // Returns true if the check is successful and the tag of the given entry is
81 // within this range, false otherwise.
82 bool CheckTagOf(uint64_t entry) {
83 // Note: the cast to uint32_t is important here. Otherwise, the uint16_t's
84 // would be promoted to int in the range check below, which would result in
85 // undefined behavior (signed integer undeflow) if the actual value is less
86 // than the lower bound. Then, the compiler would take advantage of the
87 // undefined behavior and turn the range check into a simple
88 // `actual_tag <= last_tag` comparison, which is incorrect.
89 uint32_t actual_tag = static_cast<uint16_t>(entry);
90 // The actual_tag is shifted to the left by one and contains the marking
91 // bit in the LSB. To ignore that during the type check, simply add one to
92 // the (shifted) range.
93 constexpr int kTagShift = internal::kCppHeapPointerTagShift;
94 uint32_t first_tag = static_cast<uint32_t>(lower_bound) << kTagShift;
95 uint32_t last_tag = (static_cast<uint32_t>(upper_bound) << kTagShift) + 1;
96 return actual_tag >= first_tag && actual_tag <= last_tag;
97 }
98};
99
102
104 public:
111};
112
113namespace internal {
114
115#ifdef V8_COMPRESS_POINTERS
116V8_INLINE static Address* GetCppHeapPointerTableBase(v8::Isolate* isolate) {
117 Address addr = reinterpret_cast<Address>(isolate) +
118 Internals::kIsolateCppHeapPointerTableOffset +
120 return *reinterpret_cast<Address**>(addr);
121}
122#endif // V8_COMPRESS_POINTERS
123
124template <typename T>
125V8_INLINE static T* ReadCppHeapPointerField(v8::Isolate* isolate,
126 Address heap_object_ptr, int offset,
127 CppHeapPointerTagRange tag_range) {
128#ifdef V8_COMPRESS_POINTERS
129 // See src/sandbox/cppheap-pointer-table-inl.h. Logic duplicated here so
130 // it can be inlined and doesn't require an additional call.
131 const CppHeapPointerHandle handle =
132 Internals::ReadRawField<CppHeapPointerHandle>(heap_object_ptr, offset);
133 const uint32_t index = handle >> kExternalPointerIndexShift;
134 const Address* table = GetCppHeapPointerTableBase(isolate);
135 const std::atomic<Address>* ptr =
136 reinterpret_cast<const std::atomic<Address>*>(&table[index]);
137 Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
138
139 Address pointer = entry;
140 if (V8_LIKELY(tag_range.CheckTagOf(entry))) {
141 pointer = entry >> kCppHeapPointerPayloadShift;
142 } else {
143 // If the type check failed, we simply return nullptr here. That way:
144 // 1. The null handle always results in nullptr being returned here, which
145 // is a desired property. Otherwise, we would need an explicit check for
146 // the null handle above, and therefore an additional branch. This
147 // works because the 0th entry of the table always contains nullptr
148 // tagged with the null tag (i.e. an all-zeros entry). As such,
149 // regardless of whether the type check succeeds, the result will
150 // always be nullptr.
151 // 2. The returned pointer is guaranteed to crash even on platforms with
152 // top byte ignore (TBI), such as Arm64. The alternative would be to
153 // simply return the original entry with the left-shifted payload.
154 // However, due to TBI, an access to that may not always result in a
155 // crash (specifically, if the second most significant byte happens to
156 // be zero). In addition, there shouldn't be a difference on Arm64
157 // between returning nullptr or the original entry, since it will
158 // simply compile to a `csel x0, x8, xzr, lo` instead of a
159 // `csel x0, x10, x8, lo` instruction.
160 pointer = 0;
161 }
162 return reinterpret_cast<T*>(pointer);
163#else // !V8_COMPRESS_POINTERS
164 return reinterpret_cast<T*>(
165 Internals::ReadRawField<Address>(heap_object_ptr, offset));
166#endif // !V8_COMPRESS_POINTERS
167}
168
169} // namespace internal
170} // namespace v8
171
172#endif // INCLUDE_V8_SANDBOX_H_
Definition: v8-isolate.h:261
Definition: v8-sandbox.h:103
static void InitializeBeforeThreadCreation()
static const int kExternalPointerTableBasePointerOffset
Definition: v8-internal.h:847
uint32_t CppHeapPointerHandle
Definition: v8-internal.h:366
constexpr uint64_t kCppHeapPointerPayloadShift
Definition: v8-internal.h:383
constexpr uint64_t kCppHeapPointerTagShift
Definition: v8-internal.h:382
uintptr_t Address
Definition: v8-internal.h:52
Definition: libplatform.h:15
CppHeapPointerTag
Definition: v8-sandbox.h:28
constexpr CppHeapPointerTagRange kAnyCppHeapPointer(CppHeapPointerTag::kFirstTag, CppHeapPointerTag::kLastTag)
Definition: v8-sandbox.h:69
constexpr CppHeapPointerTagRange(CppHeapPointerTag lower, CppHeapPointerTag upper)
Definition: v8-sandbox.h:70
bool CheckTagOf(uint64_t entry)
Definition: v8-sandbox.h:82
CppHeapPointerTag lower_bound
Definition: v8-sandbox.h:73
CppHeapPointerTag upper_bound
Definition: v8-sandbox.h:74
#define V8_EXPORT
Definition: v8config.h:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_LIKELY(condition)
Definition: v8config.h:650