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
66
69
77 public:
84
94};
95
96namespace internal {
97
98#ifdef V8_COMPRESS_POINTERS
99V8_INLINE static Address* GetCppHeapPointerTableBase(v8::Isolate* isolate) {
100 Address addr = reinterpret_cast<Address>(isolate) +
101 Internals::kIsolateCppHeapPointerTableOffset +
103 return *reinterpret_cast<Address**>(addr);
104}
105#endif // V8_COMPRESS_POINTERS
106
107template <typename T>
108V8_INLINE static T* ReadCppHeapPointerField(v8::Isolate* isolate,
109 Address heap_object_ptr, int offset,
110 CppHeapPointerTagRange tag_range) {
111 // This is a specialized version of the the CppHeapPointerTable accessors
112 // which (1) allows the code to be inlined into the callers for performance
113 // and (2) is optimized for code size as there are a huge number of callers
114 // from auto-generated bindings code.
115
116#ifdef V8_COMPRESS_POINTERS
117 const CppHeapPointerHandle handle =
118 Internals::ReadRawField<CppHeapPointerHandle>(heap_object_ptr, offset);
119 const uint32_t index = handle >> kExternalPointerIndexShift;
120 const Address* table = GetCppHeapPointerTableBase(isolate);
121 const std::atomic<Address>* ptr =
122 reinterpret_cast<const std::atomic<Address>*>(&table[index]);
123 Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
124
125 // Note: the cast to uint32_t is important here. Otherwise, the uint16_t's
126 // would be promoted to int in the range check below, which would result in
127 // undefined behavior (signed integer underflow) if the actual value is less
128 // than the lower bound. Then, the compiler would take advantage of the
129 // undefined behavior and turn the range check into a simple
130 // `actual_tag <= last_tag` comparison, which is incorrect.
131 uint32_t actual_tag = static_cast<uint16_t>(entry);
132 // The actual_tag is shifted to the left by one and contains the marking
133 // bit in the LSB. To ignore that during the type check, simply add one to
134 // the (shifted) range.
135 constexpr int kTagShift = internal::kCppHeapPointerTagShift;
136 uint32_t first_tag = static_cast<uint32_t>(tag_range.first) << kTagShift;
137 uint32_t last_tag = (static_cast<uint32_t>(tag_range.last) << kTagShift) + 1;
138 if (V8_LIKELY(actual_tag >= first_tag && actual_tag <= last_tag)) {
139 entry = entry >> kCppHeapPointerPayloadShift;
140 } else {
141 // If the type check failed, we simply return nullptr here. That way:
142 // 1. The null handle always results in nullptr being returned here, which
143 // is a desired property. Otherwise, we would need an explicit check for
144 // the null handle above, and therefore an additional branch. This
145 // works because the 0th entry of the table always contains nullptr
146 // tagged with the null tag (i.e. an all-zeros entry). As such,
147 // regardless of whether the type check succeeds, the result will
148 // always be nullptr.
149 // 2. The returned pointer is guaranteed to crash even on platforms with
150 // top byte ignore (TBI), such as Arm64. The alternative would be to
151 // simply return the original entry with the left-shifted payload.
152 // However, due to TBI, an access to that may not always result in a
153 // crash (specifically, if the second most significant byte happens to
154 // be zero). In addition, there shouldn't be a difference on Arm64
155 // between returning nullptr or the original entry, since it will
156 // simply compile to a `csel x0, x8, xzr, lo` instead of a
157 // `csel x0, x10, x8, lo` instruction.
158 // 3. The machine code sequence ends up being pretty short, which is
159 // important here as this code will be inlined into a lot of functions.
160 entry = 0;
161 }
162 return reinterpret_cast<T*>(entry);
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// TODO(saelo): temporary workaround needed to introduce range-based type
170// checks for the external pointer table. See comment above
171// ExternalPointerCanBeEmpty(ExternalPointerTagRange) function for details.
172V8_INLINE static constexpr bool ExternalPointerCanBeEmpty(
173 CppHeapPointerTagRange tag_range) {
174 return true;
175}
176
177} // namespace internal
178} // namespace v8
179
180#endif // INCLUDE_V8_SANDBOX_H_
Definition: v8-isolate.h:291
Definition: v8-sandbox.h:76
static void InitializeBeforeThreadCreation()
static void PrepareCurrentThreadForHardwareSandboxing()
static const int kExternalPointerTableBasePointerOffset
Definition: v8-internal.h:941
uint32_t CppHeapPointerHandle
Definition: v8-internal.h:375
constexpr uint64_t kCppHeapPointerPayloadShift
Definition: v8-internal.h:392
constexpr uint64_t kCppHeapPointerTagShift
Definition: v8-internal.h:391
uintptr_t Address
Definition: v8-internal.h:38
Definition: libplatform.h:15
internal::TagRange< CppHeapPointerTag > CppHeapPointerTagRange
Definition: v8-sandbox.h:65
CppHeapPointerTag
Definition: v8-sandbox.h:28
constexpr CppHeapPointerTagRange kAnyCppHeapPointer(CppHeapPointerTag::kFirstTag, CppHeapPointerTag::kLastTag)
Definition: v8-internal.h:455
#define V8_EXPORT
Definition: v8config.h:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_LIKELY(condition)
Definition: v8config.h:669