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
33
58 kFirstV8InternalTag = 0x6000,
59 // V8-internal Oilpan objects that use v8::Object::Wrap() should go here.
65
66#if !V8_ENABLE_SANDBOX
67 // Embedders that use the sandbox should use specific tags for each type.
69#endif // !V8_ENABLE_SANDBOX
70
72 kZappedEntryTag = 0x7ffd,
73 kEvacuationEntryTag = 0x7ffe,
74 kFreeEntryTag = 0x7fff,
75 // The tags are limited to 15 bits, so the last tag is 0x7fff.
76 kLastTag = 0x7fff,
77};
78
79static_assert(static_cast<uint16_t>(CppHeapPointerTag::kLastV8InternalTag) <
80 static_cast<uint16_t>(CppHeapPointerTag::kZappedEntryTag));
81
83
86
87// All tags that are used with v8::Object::Wrappable have to be within this
88// tag range. The reason is that in some cases, an APIWrapper object has to be
89// unwrapped to access the v8::Object::Wrappable base class, e.g. to get type
90// information.
94
98
100 "V8Internal tag range must be within kObjectWrappableTagRange");
101
109 public:
116};
117
118namespace internal {
119
120#ifdef V8_COMPRESS_POINTERS
121V8_INLINE static Address* GetCppHeapPointerTableBase(v8::Isolate* isolate) {
122 Address addr = reinterpret_cast<Address>(isolate) +
123 Internals::kIsolateCppHeapPointerTableOffset +
125 return *reinterpret_cast<Address**>(addr);
126}
127#endif // V8_COMPRESS_POINTERS
128
129template <typename T>
130V8_INLINE static T* ReadCppHeapPointerField(v8::Isolate* isolate,
131 Address heap_object_ptr, int offset,
132 CppHeapPointerTagRange tag_range) {
133 // This is a specialized version of the CppHeapPointerTable accessors
134 // which (1) allows the code to be inlined into the callers for performance
135 // and (2) is optimized for code size as there are a huge number of callers
136 // from auto-generated bindings code.
137
138#ifdef V8_COMPRESS_POINTERS
139 const CppHeapPointerHandle handle =
140 Internals::ReadRawField<CppHeapPointerHandle>(heap_object_ptr, offset);
141 const uint32_t index = handle >> kExternalPointerIndexShift;
142 const Address* table = GetCppHeapPointerTableBase(isolate);
143 const std::atomic<Address>* ptr =
144 reinterpret_cast<const std::atomic<Address>*>(&table[index]);
145 Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
146
147 // Note: the cast to uint32_t is important here. Otherwise, the uint16_t's
148 // would be promoted to int in the range check below, which would result in
149 // undefined behavior (signed integer underflow) if the actual value is less
150 // than the lower bound. Then, the compiler would take advantage of the
151 // undefined behavior and turn the range check into a simple
152 // `actual_tag <= last_tag` comparison, which is incorrect.
153 uint32_t actual_tag = static_cast<uint16_t>(entry);
154 // The actual_tag is shifted to the left by one and contains the marking
155 // bit in the LSB. To ignore that during the type check, simply add one to
156 // the (shifted) range.
157 constexpr int kTagShift = internal::kCppHeapPointerTagShift;
158 uint32_t first_tag = static_cast<uint32_t>(tag_range.first) << kTagShift;
159 uint32_t last_tag = (static_cast<uint32_t>(tag_range.last) << kTagShift) + 1;
160 // Avoid DCE of the entry logic using volatile.
161 volatile Address safe_entry;
162 if (actual_tag >= first_tag && actual_tag <= last_tag) [[likely]] {
163 safe_entry = entry >> kCppHeapPointerPayloadShift;
164 } else {
165 // If the type check failed, we simply return nullptr here. That way:
166 // 1. The null handle always results in nullptr being returned here, which
167 // is a desired property. Otherwise, we would need an explicit check for
168 // the null handle above, and therefore an additional branch. This
169 // works because the 0th entry of the table always contains nullptr
170 // tagged with the null tag (i.e. an all-zeros entry). As such,
171 // regardless of whether the type check succeeds, the result will
172 // always be nullptr.
173 // 2. The returned pointer is guaranteed to crash even on platforms with
174 // top byte ignore (TBI), such as Arm64. The alternative would be to
175 // simply return the original entry with the left-shifted payload.
176 // However, due to TBI, an access to that may not always result in a
177 // crash (specifically, if the second most significant byte happens to
178 // be zero). In addition, there shouldn't be a difference on Arm64
179 // between returning nullptr or the original entry, since it will
180 // simply compile to a `csel x0, x8, xzr, lo` instead of a
181 // `csel x0, x10, x8, lo` instruction.
182 // 3. The machine code sequence ends up being pretty short, which is
183 // important here as this code will be inlined into a lot of functions.
184 safe_entry = 0;
185 }
186 return reinterpret_cast<T*>(safe_entry);
187#else // !V8_COMPRESS_POINTERS
188 return reinterpret_cast<T*>(
189 Internals::ReadRawField<Address>(heap_object_ptr, offset));
190#endif // !V8_COMPRESS_POINTERS
191}
192
193// TODO(saelo): temporary workaround needed to introduce range-based type
194// checks for the external pointer table. See comment above
195// ExternalPointerCanBeEmpty(ExternalPointerTagRange) function for details.
196V8_INLINE static constexpr bool ExternalPointerCanBeEmpty(
197 CppHeapPointerTagRange tag_range) {
198 return true;
199}
200
201} // namespace internal
202} // namespace v8
203
204#endif // INCLUDE_V8_SANDBOX_H_
Definition: v8-isolate.h:291
Definition: v8-sandbox.h:108
static void InitializeBeforeThreadCreation()
static const int kExternalEntityTableBasePointerOffset
Definition: v8-internal.h:1021
uint32_t CppHeapPointerHandle
Definition: v8-internal.h:401
constexpr uint64_t kCppHeapPointerPayloadShift
Definition: v8-internal.h:418
constexpr uint64_t kCppHeapPointerTagShift
Definition: v8-internal.h:417
uintptr_t Address
Definition: v8-internal.h:38
Definition: libplatform.h:15
constexpr CppHeapPointerTagRange kAnyCppHeapPointer(CppHeapPointerTag::kFirstTag, CppHeapPointerTag::kZappedEntryTag)
internal::TagRange< CppHeapPointerTag > CppHeapPointerTagRange
Definition: v8-sandbox.h:82
CppHeapPointerTag
Definition: v8-sandbox.h:28
constexpr CppHeapPointerTagRange kObjectWrappableTagRange(CppHeapPointerTag::kFirstObjectWrappableTag, CppHeapPointerTag::kLastObjectWrappableTag)
constexpr CppHeapPointerTagRange kV8InternalTagRange(CppHeapPointerTag::kFirstV8InternalTag, CppHeapPointerTag::kLastV8InternalTag)
Definition: v8-internal.h:510
constexpr bool Contains(Tag tag) const
Definition: v8-internal.h:545
#define V8_EXPORT
Definition: v8config.h:867
#define V8_INLINE
Definition: v8config.h:511