v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
embedder-data-slot.h
Go to the documentation of this file.
1// Copyright 2018 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 V8_OBJECTS_EMBEDDER_DATA_SLOT_H_
6#define V8_OBJECTS_EMBEDDER_DATA_SLOT_H_
7
8#include <utility>
9
11#include "src/common/globals.h"
12#include "src/objects/slots.h"
13#include "src/sandbox/isolate.h"
14
15// Has to be the last include (doesn't have include guards):
17
18namespace v8 {
19namespace internal {
20
21class EmbedderDataArray;
22class JSObject;
23class Object;
24
25// An EmbedderDataSlot instance describes a kEmbedderDataSlotSize field ("slot")
26// holding an embedder data which may contain raw aligned pointer or a tagged
27// pointer (smi or heap object).
28// Its address() is the address of the slot.
29// The slot's contents can be read and written using respective load_XX() and
30// store_XX() methods.
31// Storing heap object through this slot may require triggering write barriers
32// so this operation must be done via static store_tagged() methods.
34 : public SlotBase<EmbedderDataSlot, Address, kTaggedSize> {
35 public:
36#ifdef V8_ENABLE_SANDBOX
37 // When the sandbox is enabled, an EmbedderDataSlot always contains a valid
38 // external pointer table index (initially, zero) in it's "raw" part and a
39 // valid tagged value in its 32-bit "tagged" part.
40 //
41 // Layout (sandbox):
42 // +-----------------------------------+-----------------------------------+
43 // | Tagged (Smi/CompressedPointer) | External Pointer Table Index |
44 // +-----------------------------------+-----------------------------------+
45 // ^ ^
46 // kTaggedPayloadOffset kRawPayloadOffset
47 // kExternalPointerOffset
48 static constexpr int kTaggedPayloadOffset = 0;
49 static constexpr int kRawPayloadOffset = kTaggedSize;
50 static constexpr int kExternalPointerOffset = kRawPayloadOffset;
51#elif defined(V8_COMPRESS_POINTERS) && defined(V8_TARGET_BIG_ENDIAN)
52 // The raw payload is located in the other "tagged" part of the full pointer
53 // and cotains the upper part of an aligned address. The raw part is not
54 // expected to look like a tagged value.
55 //
56 // Layout (big endian pointer compression):
57 // +-----------------------------------+-----------------------------------+
58 // | External Pointer (high word) | Tagged (Smi/CompressedPointer) |
59 // | | OR External Pointer (low word) |
60 // +-----------------------------------+-----------------------------------+
61 // ^ ^
62 // kRawPayloadOffset kTaggedayloadOffset
63 // kExternalPointerOffset
64 static constexpr int kExternalPointerOffset = 0;
65 static constexpr int kRawPayloadOffset = 0;
66 static constexpr int kTaggedPayloadOffset = kTaggedSize;
67#elif defined(V8_COMPRESS_POINTERS) && defined(V8_TARGET_LITTLE_ENDIAN)
68 // Layout (little endian pointer compression):
69 // +-----------------------------------+-----------------------------------+
70 // | Tagged (Smi/CompressedPointer) | External Pointer (high word) |
71 // | OR External Pointer (low word) | |
72 // +-----------------------------------+-----------------------------------+
73 // ^ ^
74 // kTaggedPayloadOffset kRawPayloadOffset
75 // kExternalPointerOffset
76 static constexpr int kExternalPointerOffset = 0;
77 static constexpr int kTaggedPayloadOffset = 0;
78 static constexpr int kRawPayloadOffset = kTaggedSize;
79#else
80 // Layout (no pointer compression):
81 // +-----------------------------------------------------------------------+
82 // | Tagged (Smi/Pointer) OR External Pointer |
83 // +-----------------------------------------------------------------------+
84 // ^
85 // kTaggedPayloadOffset
86 // kExternalPointerOffset
87 static constexpr int kTaggedPayloadOffset = 0;
88 static constexpr int kExternalPointerOffset = 0;
89#endif // V8_ENABLE_SANDBOX
90
91 static constexpr int kRequiredPtrAlignment = kSmiTagSize;
92
95 V8_INLINE EmbedderDataSlot(Tagged<JSObject> object, int embedder_field_index);
96
97 // Opaque type used for storing raw embedder data.
99
100 V8_INLINE void Initialize(Tagged<Object> initial_value);
101
103 V8_INLINE void store_smi(Tagged<Smi> value);
104
105 // Setting an arbitrary tagged value requires triggering a write barrier
106 // which requires separate object and offset values, therefore these static
107 // functions also has the target object parameter.
109 int entry_index, Tagged<Object> value);
110 static V8_INLINE void store_tagged(Tagged<JSObject> object,
111 int embedder_field_index,
112 Tagged<Object> value);
113
114 // Tries reinterpret the value as an aligned pointer and sets *out_result to
115 // the pointer-like value. Note, that some Smis could still look like an
116 // aligned pointers.
117 // Returns true on success.
118 // When the sandbox is enabled, calling this method when the raw part of the
119 // slot does not contain valid external pointer table index is undefined
120 // behaviour and most likely result in crashes.
122 void** out_result) const;
123
124 // Returns true if the pointer was successfully stored or false it the pointer
125 // was improperly aligned.
127 IsolateForSandbox isolate, Tagged<HeapObject> host, void* ptr);
128
130 const DisallowGarbageCollection& no_gc);
132 const DisallowGarbageCollection& no_gc) const;
133 V8_INLINE void store_raw(IsolateForSandbox isolate, RawData data,
134 const DisallowGarbageCollection& no_gc);
135
136 private:
137 // Stores given value to the embedder data slot in a concurrent-marker
138 // friendly manner (tagged part of the slot is written atomically).
140};
141
142} // namespace internal
143} // namespace v8
144
146
147#endif // V8_OBJECTS_EMBEDDER_DATA_SLOT_H_
V8_INLINE void store_raw(IsolateForSandbox isolate, RawData data, const DisallowGarbageCollection &no_gc)
V8_INLINE void store_smi(Tagged< Smi > value)
V8_INLINE Tagged< Object > load_tagged() const
V8_INLINE V8_WARN_UNUSED_RESULT bool store_aligned_pointer(IsolateForSandbox isolate, Tagged< HeapObject > host, void *ptr)
static constexpr int kTaggedPayloadOffset
static constexpr int kRequiredPtrAlignment
V8_INLINE bool ToAlignedPointer(IsolateForSandbox isolate, void **out_result) const
V8_INLINE void gc_safe_store(IsolateForSandbox isolate, Address value)
static constexpr int kExternalPointerOffset
V8_INLINE bool MustClearDuringSerialization(const DisallowGarbageCollection &no_gc)
static V8_INLINE void store_tagged(Tagged< EmbedderDataArray > array, int entry_index, Tagged< Object > value)
V8_INLINE void Initialize(Tagged< Object > initial_value)
V8_INLINE RawData load_raw(IsolateForSandbox isolate, const DisallowGarbageCollection &no_gc) const
constexpr int kTaggedSize
Definition globals.h:542
const int kSmiTagSize
Definition v8-internal.h:87
static constexpr Address kNullAddress
Definition v8-internal.h:53
#define V8_INLINE
Definition v8config.h:500
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671