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