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