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
20enum class CppHeapPointerTag : uint64_t {
22};
23
24namespace internal {
25
26#ifdef V8_COMPRESS_POINTERS
27V8_INLINE static Address* GetCppHeapPointerTableBase(v8::Isolate* isolate) {
28 Address addr = reinterpret_cast<Address>(isolate) +
29 Internals::kIsolateCppHeapPointerTableOffset +
31 return *reinterpret_cast<Address**>(addr);
32}
33#endif // V8_COMPRESS_POINTERS
34
35template <CppHeapPointerTag tag, typename T>
36V8_INLINE static T* ReadCppHeapPointerField(v8::Isolate* isolate,
37 Address heap_object_ptr,
38 int offset) {
39#ifdef V8_COMPRESS_POINTERS
40 static_assert(tag != static_cast<CppHeapPointerTag>(kExternalPointerNullTag));
41 // See src/sandbox/external-pointer-table-inl.h. Logic duplicated here so
42 // it can be inlined and doesn't require an additional call.
43 const CppHeapPointerHandle handle =
44 Internals::ReadRawField<CppHeapPointerHandle>(heap_object_ptr, offset);
45 if (handle == 0) {
46 return reinterpret_cast<T*>(kNullAddress);
47 }
48 const uint32_t index = handle >> kExternalPointerIndexShift;
49 const Address* table = GetCppHeapPointerTableBase(isolate);
50 const std::atomic<Address>* ptr =
51 reinterpret_cast<const std::atomic<Address>*>(&table[index]);
52 Address entry = std::atomic_load_explicit(ptr, std::memory_order_relaxed);
53 return reinterpret_cast<T*>(entry & ~static_cast<uint64_t>(tag));
54#else // !V8_COMPRESS_POINTERS
55 return reinterpret_cast<T*>(
56 Internals::ReadRawField<Address>(heap_object_ptr, offset));
57#endif // !V8_COMPRESS_POINTERS
58}
59
60} // namespace internal
61} // namespace v8
62
63#endif // INCLUDE_V8_SANDBOX_H_
Definition: v8-isolate.h:210
static const int kExternalPointerTableBasePointerOffset
Definition: v8-internal.h:781
uint32_t CppHeapPointerHandle
Definition: v8-internal.h:309
uintptr_t Address
Definition: v8-internal.h:31
@ kExternalObjectValueTag
Definition: v8-internal.h:551
@ kExternalPointerNullTag
Definition: v8-internal.h:535
Definition: libplatform.h:15
CppHeapPointerTag
Definition: v8-sandbox.h:20
#define V8_INLINE
Definition: v8config.h:477