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 8 #include "src/common/assert-scope.h" 9 #include "src/objects/visitors.h" 10 #include "src/snapshot/references.h" 11 12 namespace v8 { 13 namespace internal { 14 15 class CallHandlerInfo; 16 class Isolate; 17 18 // The Serializer/Deserializer class is a common superclass for Serializer and 19 // Deserializer which is used to store common constants and methods used by 20 // both. 21 class SerializerDeserializer : public RootVisitor { 22 public: 23 static void Iterate(Isolate* isolate, RootVisitor* visitor); 24 25 protected: 26 static bool CanBeDeferred(HeapObject o); 27 28 void RestoreExternalReferenceRedirector(Isolate* isolate, 29 Handle<AccessorInfo> accessor_info); 30 void RestoreExternalReferenceRedirector( 31 Isolate* isolate, Handle<CallHandlerInfo> call_handler_info); 32 33 // clang-format off 34 #define UNUSED_SERIALIZER_BYTE_CODES(V) \ 35 /* Free range 0x1c..0x1f */ \ 36 V(0x1c) V(0x1d) V(0x1e) V(0x1f) \ 37 /* Free range 0x20..0x2f */ \ 38 V(0x20) V(0x21) V(0x22) V(0x23) V(0x24) V(0x25) V(0x26) V(0x27) \ 39 V(0x28) V(0x29) V(0x2a) V(0x2b) V(0x2c) V(0x2d) V(0x2e) V(0x2f) \ 40 /* Free range 0x30..0x3f */ \ 41 V(0x30) V(0x31) V(0x32) V(0x33) V(0x34) V(0x35) V(0x36) V(0x37) \ 42 V(0x38) V(0x39) V(0x3a) V(0x3b) V(0x3c) V(0x3d) V(0x3e) V(0x3f) \ 43 /* Free range 0x97..0x9f */ \ 44 V(0x98) V(0x99) V(0x9a) V(0x9b) V(0x9c) V(0x9d) V(0x9e) V(0x9f) \ 45 /* Free range 0xa0..0xaf */ \ 46 V(0xa0) V(0xa1) V(0xa2) V(0xa3) V(0xa4) V(0xa5) V(0xa6) V(0xa7) \ 47 V(0xa8) V(0xa9) V(0xaa) V(0xab) V(0xac) V(0xad) V(0xae) V(0xaf) \ 48 /* Free range 0xb0..0xbf */ \ 49 V(0xb0) V(0xb1) V(0xb2) V(0xb3) V(0xb4) V(0xb5) V(0xb6) V(0xb7) \ 50 V(0xb8) V(0xb9) V(0xba) V(0xbb) V(0xbc) V(0xbd) V(0xbe) V(0xbf) \ 51 /* Free range 0xc0..0xcf */ \ 52 V(0xc0) V(0xc1) V(0xc2) V(0xc3) V(0xc4) V(0xc5) V(0xc6) V(0xc7) \ 53 V(0xc8) V(0xc9) V(0xca) V(0xcb) V(0xcc) V(0xcd) V(0xce) V(0xcf) \ 54 /* Free range 0xd0..0xdf */ \ 55 V(0xd0) V(0xd1) V(0xd2) V(0xd3) V(0xd4) V(0xd5) V(0xd6) V(0xd7) \ 56 V(0xd8) V(0xd9) V(0xda) V(0xdb) V(0xdc) V(0xdd) V(0xde) V(0xdf) \ 57 /* Free range 0xe0..0xef */ \ 58 V(0xe0) V(0xe1) V(0xe2) V(0xe3) V(0xe4) V(0xe5) V(0xe6) V(0xe7) \ 59 V(0xe8) V(0xe9) V(0xea) V(0xeb) V(0xec) V(0xed) V(0xee) V(0xef) \ 60 /* Free range 0xf0..0xff */ \ 61 V(0xf0) V(0xf1) V(0xf2) V(0xf3) V(0xf4) V(0xf5) V(0xf6) V(0xf7) \ 62 V(0xf8) V(0xf9) V(0xfa) V(0xfb) V(0xfc) V(0xfd) V(0xfe) V(0xff) 63 // clang-format on 64 65 // The static assert below will trigger when the number of preallocated spaces 66 // changed. If that happens, update the kNewObject and kBackref bytecode 67 // ranges in the comments below. 68 STATIC_ASSERT(4 == kNumberOfSnapshotSpaces); 69 70 // First 32 root array items. 71 static const int kRootArrayConstantsCount = 0x20; 72 73 // 32 common raw data lengths. 74 static const int kFixedRawDataCount = 0x20; 75 // 16 repeats lengths. 76 static const int kFixedRepeatCount = 0x10; 77 78 // 8 hot (recently seen or back-referenced) objects with optional skip. 79 static const int kHotObjectCount = 8; 80 81 enum Bytecode : byte { 82 // 83 // ---------- byte code range 0x00..0x1b ---------- 84 // 85 86 // 0x00..0x03 Allocate new object, in specified space. 87 kNewObject = 0x00, 88 // Reference to previously allocated object. 89 kBackref = 0x04, 90 // Reference to an object in the read only heap. 91 kReadOnlyHeapRef, 92 // Object in the startup object cache. 93 kStartupObjectCache, 94 // Root array item. 95 kRootArray, 96 // Object provided in the attached list. 97 kAttachedReference, 98 // Object in the read-only object cache. 99 kReadOnlyObjectCache, 100 // Do nothing, used for padding. 101 kNop, 102 // A tag emitted at strategic points in the snapshot to delineate sections. 103 // If the deserializer does not find these at the expected moments then it 104 // is an indication that the snapshot and the VM do not fit together. 105 // Examine the build process for architecture, version or configuration 106 // mismatches. 107 kSynchronize, 108 // Repeats of variable length. 109 kVariableRepeat, 110 // Used for embedder-allocated backing stores for TypedArrays. 111 kOffHeapBackingStore, 112 // Used for embedder-provided serialization data for embedder fields. 113 kEmbedderFieldsData, 114 // Raw data of variable length. 115 kVariableRawData, 116 // Used to encode external references provided through the API. 117 kApiReference, 118 // External reference referenced by id. 119 kExternalReference, 120 // Same as two bytecodes above but for serializing sandboxed external 121 // pointer values. 122 // TODO(v8:10391): Remove them once all ExternalPointer usages are 123 // sandbox-ready. 124 kSandboxedApiReference, 125 kSandboxedExternalReference, 126 // Internal reference of a code objects in code stream. 127 kInternalReference, 128 // In-place weak references. 129 kClearedWeakReference, 130 kWeakPrefix, 131 // Encodes an off-heap instruction stream target. 132 kOffHeapTarget, 133 // Registers the current slot as a "pending" forward reference, to be later 134 // filled by a corresponding resolution bytecode. 135 kRegisterPendingForwardRef, 136 // Resolves an existing "pending" forward reference to point to the current 137 // object. 138 kResolvePendingForwardRef, 139 // Special construction bytecode for the metamap. In theory we could re-use 140 // forward-references for this, but then the forward reference would be 141 // registered during object map deserialization, before the object is 142 // allocated, so there wouldn't be a allocated object whose map field we can 143 // register as the pending field. We could either hack around this, or 144 // simply introduce this new bytecode. 145 kNewMetaMap, 146 // Special construction bytecode for Code object bodies, which have a more 147 // complex deserialization ordering and RelocInfo processing. 148 kCodeBody, 149 150 // 151 // ---------- byte code range 0x40..0x7f ---------- 152 // 153 154 // 0x40..0x5f 155 kRootArrayConstants = 0x40, 156 157 // 0x60..0x7f 158 kFixedRawData = 0x60, 159 160 // 161 // ---------- byte code range 0x80..0x9f ---------- 162 // 163 164 // 0x80..0x8f 165 kFixedRepeat = 0x80, 166 167 // 0x90..0x97 168 kHotObject = 0x90, 169 }; 170 171 // Helper class for encoding and decoding a value into and from a bytecode. 172 // 173 // The value is encoded by allocating an entire bytecode range, and encoding 174 // the value as an index in that range, starting at kMinValue; thus the range 175 // of values 176 // [kMinValue, kMinValue + 1, ... , kMaxValue] 177 // is encoded as 178 // [kBytecode, kBytecode + 1, ... , kBytecode + (N - 1)] 179 // where N is the number of values, i.e. kMaxValue - kMinValue + 1. 180 template <Bytecode kBytecode, int kMinValue, int kMaxValue, 181 typename TValue = int> 182 struct BytecodeValueEncoder { 183 STATIC_ASSERT((kBytecode + kMaxValue - kMinValue) <= kMaxUInt8); 184 IsEncodableBytecodeValueEncoder185 static constexpr bool IsEncodable(TValue value) { 186 return base::IsInRange(static_cast<int>(value), kMinValue, kMaxValue); 187 } 188 EncodeBytecodeValueEncoder189 static constexpr byte Encode(TValue value) { 190 CONSTEXPR_DCHECK(IsEncodable(value)); 191 return static_cast<byte>(kBytecode + static_cast<int>(value) - kMinValue); 192 } 193 DecodeBytecodeValueEncoder194 static constexpr TValue Decode(byte bytecode) { 195 CONSTEXPR_DCHECK(base::IsInRange(bytecode, 196 Encode(static_cast<TValue>(kMinValue)), 197 Encode(static_cast<TValue>(kMaxValue)))); 198 return static_cast<TValue>(bytecode - kBytecode + kMinValue); 199 } 200 }; 201 202 template <Bytecode bytecode> 203 using SpaceEncoder = 204 BytecodeValueEncoder<bytecode, 0, kNumberOfSnapshotSpaces - 1, 205 SnapshotSpace>; 206 207 using NewObject = SpaceEncoder<kNewObject>; 208 209 // 210 // Some other constants. 211 // 212 213 // Sentinel after a new object to indicate that double alignment is needed. 214 static const int kDoubleAlignmentSentinel = 0; 215 216 // Raw data size encoding helpers. 217 static const int kFirstEncodableFixedRawDataSize = 1; 218 static const int kLastEncodableFixedRawDataSize = 219 kFirstEncodableFixedRawDataSize + kFixedRawDataCount - 1; 220 221 using FixedRawDataWithSize = 222 BytecodeValueEncoder<kFixedRawData, kFirstEncodableFixedRawDataSize, 223 kLastEncodableFixedRawDataSize>; 224 225 // Repeat count encoding helpers. 226 static const int kFirstEncodableRepeatCount = 2; 227 static const int kLastEncodableFixedRepeatCount = 228 kFirstEncodableRepeatCount + kFixedRepeatCount - 1; 229 static const int kFirstEncodableVariableRepeatCount = 230 kLastEncodableFixedRepeatCount + 1; 231 232 using FixedRepeatWithCount = 233 BytecodeValueEncoder<kFixedRepeat, kFirstEncodableRepeatCount, 234 kLastEncodableFixedRepeatCount>; 235 236 // Encodes/decodes repeat count into a serialized variable repeat count 237 // value. 238 struct VariableRepeatCount { IsEncodableVariableRepeatCount239 static constexpr bool IsEncodable(int repeat_count) { 240 return repeat_count >= kFirstEncodableVariableRepeatCount; 241 } 242 EncodeVariableRepeatCount243 static constexpr int Encode(int repeat_count) { 244 CONSTEXPR_DCHECK(IsEncodable(repeat_count)); 245 return repeat_count - kFirstEncodableVariableRepeatCount; 246 } 247 DecodeVariableRepeatCount248 static constexpr int Decode(int value) { 249 return value + kFirstEncodableVariableRepeatCount; 250 } 251 }; 252 253 using RootArrayConstant = 254 BytecodeValueEncoder<kRootArrayConstants, 0, kRootArrayConstantsCount - 1, 255 RootIndex>; 256 using HotObject = BytecodeValueEncoder<kHotObject, 0, kHotObjectCount - 1>; 257 258 // This backing store reference value represents nullptr values during 259 // serialization/deserialization. 260 static const uint32_t kNullRefSentinel = 0; 261 }; 262 263 } // namespace internal 264 } // namespace v8 265 266 #endif // V8_SNAPSHOT_SERIALIZER_DESERIALIZER_H_ 267