v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
serializer-deserializer.h
Go to the documentation of this file.
1// Copyright 2020 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_SNAPSHOT_SERIALIZER_DESERIALIZER_H_
6#define V8_SNAPSHOT_SERIALIZER_DESERIALIZER_H_
7
10
11namespace v8 {
12namespace internal {
13
14class Isolate;
15
16// The Serializer/Deserializer class is a common superclass for Serializer and
17// Deserializer which is used to store common constants and methods used by
18// both.
20 public:
21 static void IterateStartupObjectCache(Isolate* isolate, RootVisitor* visitor);
22
23 static void IterateSharedHeapObjectCache(Isolate* isolate,
24 RootVisitor* visitor);
25
26 protected:
27 enum class SlotType {
30 };
31 static bool CanBeDeferred(Tagged<HeapObject> o, SlotType slot_type);
32
34 Tagged<AccessorInfo> accessor_info);
36 Isolate* isolate, Tagged<FunctionTemplateInfo> function_template_info);
37
38 // clang-format off
39#define UNUSED_SERIALIZER_BYTE_CODES(V) \
40 /* Free range 0x22..0x2f */ \
41 V(0x22) V(0x23) V(0x24) V(0x25) V(0x26) V(0x27) \
42 V(0x28) V(0x29) V(0x2a) V(0x2b) V(0x2c) V(0x2d) V(0x2e) V(0x2f) \
43 /* Free range 0x30..0x3f */ \
44 V(0x30) V(0x31) V(0x32) V(0x33) V(0x34) V(0x35) V(0x36) V(0x37) \
45 V(0x38) V(0x39) V(0x3a) V(0x3b) V(0x3c) V(0x3d) V(0x3e) V(0x3f) \
46 /* Free range 0x97..0x9f */ \
47 V(0x98) V(0x99) V(0x9a) V(0x9b) V(0x9c) V(0x9d) V(0x9e) V(0x9f) \
48 /* Free range 0xa0..0xaf */ \
49 V(0xa0) V(0xa1) V(0xa2) V(0xa3) V(0xa4) V(0xa5) V(0xa6) V(0xa7) \
50 V(0xa8) V(0xa9) V(0xaa) V(0xab) V(0xac) V(0xad) V(0xae) V(0xaf) \
51 /* Free range 0xb0..0xbf */ \
52 V(0xb0) V(0xb1) V(0xb2) V(0xb3) V(0xb4) V(0xb5) V(0xb6) V(0xb7) \
53 V(0xb8) V(0xb9) V(0xba) V(0xbb) V(0xbc) V(0xbd) V(0xbe) V(0xbf) \
54 /* Free range 0xc0..0xcf */ \
55 V(0xc0) V(0xc1) V(0xc2) V(0xc3) V(0xc4) V(0xc5) V(0xc6) V(0xc7) \
56 V(0xc8) V(0xc9) V(0xca) V(0xcb) V(0xcc) V(0xcd) V(0xce) V(0xcf) \
57 /* Free range 0xd0..0xdf */ \
58 V(0xd0) V(0xd1) V(0xd2) V(0xd3) V(0xd4) V(0xd5) V(0xd6) V(0xd7) \
59 V(0xd8) V(0xd9) V(0xda) V(0xdb) V(0xdc) V(0xdd) V(0xde) V(0xdf) \
60 /* Free range 0xe0..0xef */ \
61 V(0xe0) V(0xe1) V(0xe2) V(0xe3) V(0xe4) V(0xe5) V(0xe6) V(0xe7) \
62 V(0xe8) V(0xe9) V(0xea) V(0xeb) V(0xec) V(0xed) V(0xee) V(0xef) \
63 /* Free range 0xf0..0xff */ \
64 V(0xf0) V(0xf1) V(0xf2) V(0xf3) V(0xf4) V(0xf5) V(0xf6) V(0xf7) \
65 V(0xf8) V(0xf9) V(0xfa) V(0xfb) V(0xfc) V(0xfd) V(0xfe) V(0xff)
66 // clang-format on
67
68 // The static assert below will trigger when the number of preallocated spaces
69 // changed. If that happens, update the kNewObject and kBackref bytecode
70 // ranges in the comments below.
71 static_assert(4 == kNumberOfSnapshotSpaces);
72
73 // First 32 root array items.
74 static const int kRootArrayConstantsCount = 0x20;
75
76 // 32 common raw data lengths.
77 static const int kFixedRawDataCount = 0x20;
78 // 16 repeats lengths.
79 static const int kFixedRepeatRootCount = 0x10;
80
81 // 8 hot (recently seen or back-referenced) objects with optional skip.
82 static const int kHotObjectCount = 8;
83
84 enum Bytecode : uint8_t {
85 //
86 // ---------- byte code range 0x00..0x1f ----------
87 //
88
89 // 0x00..0x03 Allocate new object, in specified space.
90 kNewObject = 0x00,
91 // Reference to previously allocated object.
92 kBackref = 0x04,
93 // Reference to an object in the read only heap.
95 // Object in the startup object cache.
97 // Root array item.
99 // Object provided in the attached list.
101 // Object in the shared heap object cache.
103 // Do nothing, used for padding.
105 // A tag emitted at strategic points in the snapshot to delineate sections.
106 // If the deserializer does not find these at the expected moments then it
107 // is an indication that the snapshot and the VM do not fit together.
108 // Examine the build process for architecture, version or configuration
109 // mismatches.
111 // Repeats of variable length of a root.
113 // Used for embedder-allocated backing stores for TypedArrays.
116 // Used for embedder-provided serialization data for embedder fields.
118 // Used for embedder-provided serialziation data for API wrappers.
120 // Raw data of variable length.
122 // Used to encode external references provided through the API.
124 // External reference referenced by id.
126 // Same as three bytecodes above but for serializing sandboxed external
127 // pointer values.
128 // TODO(v8:10391): Remove them once all ExternalPointer usages are
129 // sandbox-ready.
133 // In-place weak references.
136 // Registers the current slot as a "pending" forward reference, to be later
137 // filled by a corresponding resolution bytecode.
139 // Resolves an existing "pending" forward reference to point to the current
140 // object.
142 // Special construction bytecodes for the metamaps. In theory we could
143 // reuse forward-references for this, but then the forward reference would
144 // be registered during object map deserialization, before the object is
145 // allocated, so there wouldn't be a allocated object whose map field we can
146 // register as the pending field. We could either hack around this, or
147 // simply introduce this new bytecode.
150 // When the sandbox is enabled, a prefix indicating that the following
151 // object is referenced through an indirect pointer, i.e. through an entry
152 // in a pointer table.
154 // When the sandbox is enabled, this bytecode instructs the deserializer to
155 // initialize the "self" indirect pointer of trusted objects, which
156 // references the object's pointer table entry. As the "self" indirect
157 // pointer is always the first field after the map word, it is guaranteed
158 // that it will be deserialized before any inner objects, which may require
159 // the pointer table entry for back reference to the trusted object.
161 // This bytecode instructs the deserializer to allocate an entry in the
162 // JSDispatchTable for the host object and store the corresponding dispatch
163 // handle into the current slot.
165 // A back-reference to the already allocated n-th dispatch entry.
167 // A prefix indicating that the following object is referenced through a
168 // protected pointer, i.e. a pointer from one trusted object to another.
170
171 //
172 // ---------- byte code range 0x40..0x7f ----------
173 //
174
175 // 0x40..0x5f
177
178 // 0x60..0x7f
180
181 //
182 // ---------- byte code range 0x80..0x9f ----------
183 //
184
185 // 0x80..0x8f
187
188 // 0x90..0x97
190 };
191
192 // Helper class for encoding and decoding a value into and from a bytecode.
193 //
194 // The value is encoded by allocating an entire bytecode range, and encoding
195 // the value as an index in that range, starting at kMinValue; thus the range
196 // of values
197 // [kMinValue, kMinValue + 1, ... , kMaxValue]
198 // is encoded as
199 // [kBytecode, kBytecode + 1, ... , kBytecode + (N - 1)]
200 // where N is the number of values, i.e. kMaxValue - kMinValue + 1.
201 template <Bytecode kBytecode, int kMinValue, int kMaxValue,
202 typename TValue = int>
204 static_assert((kBytecode + kMaxValue - kMinValue) <= kMaxUInt8);
205
206 static constexpr bool IsEncodable(TValue value) {
207 return base::IsInRange(static_cast<int>(value), kMinValue, kMaxValue);
208 }
209
210 static constexpr uint8_t Encode(TValue value) {
211 DCHECK(IsEncodable(value));
212 return static_cast<uint8_t>(kBytecode + static_cast<int>(value) -
213 kMinValue);
214 }
215
216 static constexpr TValue Decode(uint8_t bytecode) {
217 DCHECK(base::IsInRange(bytecode, Encode(static_cast<TValue>(kMinValue)),
218 Encode(static_cast<TValue>(kMaxValue))));
219 return static_cast<TValue>(bytecode - kBytecode + kMinValue);
220 }
221 };
222
223 template <Bytecode bytecode>
227
229
230 //
231 // Some other constants.
232 //
233
234 // Sentinel after a new object to indicate that double alignment is needed.
235 static const int kDoubleAlignmentSentinel = 0;
236
237 // Raw data size encoding helpers.
241
245
246 // Repeat count encoding helpers.
247 static const int kFirstEncodableRepeatRootCount = 2;
252
256
257 // Encodes/decodes repeat count into a serialized variable repeat count
258 // value.
260 static constexpr bool IsEncodable(int repeat_count) {
261 return repeat_count >= kFirstEncodableVariableRepeatRootCount;
262 }
263
264 static constexpr int Encode(int repeat_count) {
265 DCHECK(IsEncodable(repeat_count));
266 return repeat_count - kFirstEncodableVariableRepeatRootCount;
267 }
268
269 static constexpr int Decode(int value) {
271 }
272 };
273
276 RootIndex>;
278
279 // This backing store reference value represents empty backing stores during
280 // serialization/deserialization.
281 static const uint32_t kEmptyBackingStoreRefSentinel = 0;
282};
283
299
315} // namespace internal
316} // namespace v8
317
318#endif // V8_SNAPSHOT_SERIALIZER_DESERIALIZER_H_
interpreter::Bytecode bytecode
Definition builtins.cc:43
static void IterateStartupObjectCache(Isolate *isolate, RootVisitor *visitor)
void RestoreExternalReferenceRedirector(Isolate *isolate, Tagged< AccessorInfo > accessor_info)
static void IterateSharedHeapObjectCache(Isolate *isolate, RootVisitor *visitor)
static bool CanBeDeferred(Tagged< HeapObject > o, SlotType slot_type)
constexpr bool IsInRange(T value, U lower_limit, U higher_limit)
Definition bounds.h:20
constexpr int kMaxUInt8
Definition globals.h:378
static constexpr int kNumberOfSnapshotSpaces
Definition references.h:24
return value
Definition map-inl.h:893
#define DCHECK(condition)
Definition logging.h:482
DeserializeEmbedderFieldsCallback(v8::DeserializeInternalFieldsCallback js_cb=v8::DeserializeInternalFieldsCallback(), v8::DeserializeContextDataCallback context_cb=v8::DeserializeContextDataCallback(), v8::DeserializeAPIWrapperCallback api_wrapper_cb=v8::DeserializeAPIWrapperCallback())
v8::DeserializeInternalFieldsCallback js_object_callback
SerializeEmbedderFieldsCallback(v8::SerializeInternalFieldsCallback js_cb=v8::SerializeInternalFieldsCallback(), v8::SerializeContextDataCallback context_cb=v8::SerializeContextDataCallback(), v8::SerializeAPIWrapperCallback api_wrapper_cb=v8::SerializeAPIWrapperCallback())
v8::SerializeInternalFieldsCallback js_object_callback