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