1 // Copyright 2016 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_VALUE_SERIALIZER_H_ 6 #define V8_VALUE_SERIALIZER_H_ 7 8 #include <cstdint> 9 #include <vector> 10 11 #include "include/v8.h" 12 #include "src/base/compiler-specific.h" 13 #include "src/base/macros.h" 14 #include "src/identity-map.h" 15 #include "src/messages.h" 16 #include "src/vector.h" 17 #include "src/zone/zone.h" 18 19 namespace v8 { 20 namespace internal { 21 22 class HeapNumber; 23 class Isolate; 24 class JSArrayBuffer; 25 class JSArrayBufferView; 26 class JSDate; 27 class JSMap; 28 class JSRegExp; 29 class JSSet; 30 class JSValue; 31 class Object; 32 class Oddball; 33 class Smi; 34 35 enum class SerializationTag : uint8_t; 36 37 /** 38 * Writes V8 objects in a binary format that allows the objects to be cloned 39 * according to the HTML structured clone algorithm. 40 * 41 * Format is based on Blink's previous serialization logic. 42 */ 43 class ValueSerializer { 44 public: 45 ValueSerializer(Isolate* isolate, v8::ValueSerializer::Delegate* delegate); 46 ~ValueSerializer(); 47 48 /* 49 * Writes out a header, which includes the format version. 50 */ 51 void WriteHeader(); 52 53 /* 54 * Serializes a V8 object into the buffer. 55 */ 56 Maybe<bool> WriteObject(Handle<Object> object) WARN_UNUSED_RESULT; 57 58 /* 59 * Returns the stored data. This serializer should not be used once the buffer 60 * is released. The contents are undefined if a previous write has failed. 61 */ 62 std::vector<uint8_t> ReleaseBuffer(); 63 64 /* 65 * Returns the buffer, allocated via the delegate, and its size. 66 * Caller assumes ownership of the buffer. 67 */ 68 std::pair<uint8_t*, size_t> Release(); 69 70 /* 71 * Marks an ArrayBuffer as havings its contents transferred out of band. 72 * Pass the corresponding JSArrayBuffer in the deserializing context to 73 * ValueDeserializer::TransferArrayBuffer. 74 */ 75 void TransferArrayBuffer(uint32_t transfer_id, 76 Handle<JSArrayBuffer> array_buffer); 77 78 /* 79 * Publicly exposed wire format writing methods. 80 * These are intended for use within the delegate's WriteHostObject method. 81 */ 82 void WriteUint32(uint32_t value); 83 void WriteUint64(uint64_t value); 84 void WriteRawBytes(const void* source, size_t length); 85 void WriteDouble(double value); 86 87 /* 88 * Indicate whether to treat ArrayBufferView objects as host objects, 89 * i.e. pass them to Delegate::WriteHostObject. This should not be 90 * called when no Delegate was passed. 91 * 92 * The default is not to treat ArrayBufferViews as host objects. 93 */ 94 void SetTreatArrayBufferViewsAsHostObjects(bool mode); 95 96 private: 97 // Managing allocations of the internal buffer. 98 Maybe<bool> ExpandBuffer(size_t required_capacity); 99 100 // Writing the wire format. 101 void WriteTag(SerializationTag tag); 102 template <typename T> 103 void WriteVarint(T value); 104 template <typename T> 105 void WriteZigZag(T value); 106 void WriteOneByteString(Vector<const uint8_t> chars); 107 void WriteTwoByteString(Vector<const uc16> chars); 108 Maybe<uint8_t*> ReserveRawBytes(size_t bytes); 109 110 // Writing V8 objects of various kinds. 111 void WriteOddball(Oddball* oddball); 112 void WriteSmi(Smi* smi); 113 void WriteHeapNumber(HeapNumber* number); 114 void WriteString(Handle<String> string); 115 Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT; 116 Maybe<bool> WriteJSObject(Handle<JSObject> object) WARN_UNUSED_RESULT; 117 Maybe<bool> WriteJSObjectSlow(Handle<JSObject> object) WARN_UNUSED_RESULT; 118 Maybe<bool> WriteJSArray(Handle<JSArray> array) WARN_UNUSED_RESULT; 119 void WriteJSDate(JSDate* date); 120 Maybe<bool> WriteJSValue(Handle<JSValue> value) WARN_UNUSED_RESULT; 121 void WriteJSRegExp(JSRegExp* regexp); 122 Maybe<bool> WriteJSMap(Handle<JSMap> map) WARN_UNUSED_RESULT; 123 Maybe<bool> WriteJSSet(Handle<JSSet> map) WARN_UNUSED_RESULT; 124 Maybe<bool> WriteJSArrayBuffer(Handle<JSArrayBuffer> array_buffer) 125 WARN_UNUSED_RESULT; 126 Maybe<bool> WriteJSArrayBufferView(JSArrayBufferView* array_buffer); 127 Maybe<bool> WriteWasmModule(Handle<JSObject> object) WARN_UNUSED_RESULT; 128 Maybe<bool> WriteHostObject(Handle<JSObject> object) WARN_UNUSED_RESULT; 129 130 /* 131 * Reads the specified keys from the object and writes key-value pairs to the 132 * buffer. Returns the number of keys actually written, which may be smaller 133 * if some keys are not own properties when accessed. 134 */ 135 Maybe<uint32_t> WriteJSObjectPropertiesSlow( 136 Handle<JSObject> object, Handle<FixedArray> keys) WARN_UNUSED_RESULT; 137 138 /* 139 * Asks the delegate to handle an error that occurred during data cloning, by 140 * throwing an exception appropriate for the host. 141 */ 142 void ThrowDataCloneError(MessageTemplate::Template template_index); 143 V8_NOINLINE void ThrowDataCloneError(MessageTemplate::Template template_index, 144 Handle<Object> arg0); 145 146 Maybe<bool> ThrowIfOutOfMemory(); 147 148 Isolate* const isolate_; 149 v8::ValueSerializer::Delegate* const delegate_; 150 bool treat_array_buffer_views_as_host_objects_ = false; 151 uint8_t* buffer_ = nullptr; 152 size_t buffer_size_ = 0; 153 size_t buffer_capacity_ = 0; 154 bool out_of_memory_ = false; 155 Zone zone_; 156 157 // To avoid extra lookups in the identity map, ID+1 is actually stored in the 158 // map (checking if the used identity is zero is the fast way of checking if 159 // the entry is new). 160 IdentityMap<uint32_t, ZoneAllocationPolicy> id_map_; 161 uint32_t next_id_ = 0; 162 163 // A similar map, for transferred array buffers. 164 IdentityMap<uint32_t, ZoneAllocationPolicy> array_buffer_transfer_map_; 165 166 DISALLOW_COPY_AND_ASSIGN(ValueSerializer); 167 }; 168 169 /* 170 * Deserializes values from data written with ValueSerializer, or a compatible 171 * implementation. 172 */ 173 class ValueDeserializer { 174 public: 175 ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data, 176 v8::ValueDeserializer::Delegate* delegate); 177 ~ValueDeserializer(); 178 179 /* 180 * Runs version detection logic, which may fail if the format is invalid. 181 */ 182 Maybe<bool> ReadHeader() WARN_UNUSED_RESULT; 183 184 /* 185 * Reads the underlying wire format version. Likely mostly to be useful to 186 * legacy code reading old wire format versions. Must be called after 187 * ReadHeader. 188 */ GetWireFormatVersion()189 uint32_t GetWireFormatVersion() const { return version_; } 190 191 /* 192 * Deserializes a V8 object from the buffer. 193 */ 194 MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT; 195 196 /* 197 * Reads an object, consuming the entire buffer. 198 * 199 * This is required for the legacy "version 0" format, which did not allow 200 * reference deduplication, and instead relied on a "stack" model for 201 * deserializing, with the contents of objects and arrays provided first. 202 */ 203 MaybeHandle<Object> ReadObjectUsingEntireBufferForLegacyFormat() 204 WARN_UNUSED_RESULT; 205 206 /* 207 * Accepts the array buffer corresponding to the one passed previously to 208 * ValueSerializer::TransferArrayBuffer. 209 */ 210 void TransferArrayBuffer(uint32_t transfer_id, 211 Handle<JSArrayBuffer> array_buffer); 212 213 /* 214 * Publicly exposed wire format writing methods. 215 * These are intended for use within the delegate's WriteHostObject method. 216 */ 217 bool ReadUint32(uint32_t* value) WARN_UNUSED_RESULT; 218 bool ReadUint64(uint64_t* value) WARN_UNUSED_RESULT; 219 bool ReadDouble(double* value) WARN_UNUSED_RESULT; 220 bool ReadRawBytes(size_t length, const void** data) WARN_UNUSED_RESULT; 221 222 private: 223 // Reading the wire format. 224 Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT; 225 void ConsumeTag(SerializationTag peeked_tag); 226 Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT; 227 template <typename T> 228 Maybe<T> ReadVarint() WARN_UNUSED_RESULT; 229 template <typename T> 230 Maybe<T> ReadZigZag() WARN_UNUSED_RESULT; 231 Maybe<double> ReadDouble() WARN_UNUSED_RESULT; 232 Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT; 233 234 // Reads a string if it matches the one provided. 235 // Returns true if this was the case. Otherwise, nothing is consumed. 236 bool ReadExpectedString(Handle<String> expected) WARN_UNUSED_RESULT; 237 238 // Like ReadObject, but skips logic for special cases in simulating the 239 // "stack machine". 240 MaybeHandle<Object> ReadObjectInternal() WARN_UNUSED_RESULT; 241 242 // Reads a string intended to be part of a more complicated object. 243 // Before v12, these are UTF-8 strings. After, they can be any encoding 244 // permissible for a string (with the relevant tag). 245 MaybeHandle<String> ReadString() WARN_UNUSED_RESULT; 246 247 // Reading V8 objects of specific kinds. 248 // The tag is assumed to have already been read. 249 MaybeHandle<String> ReadUtf8String() WARN_UNUSED_RESULT; 250 MaybeHandle<String> ReadOneByteString() WARN_UNUSED_RESULT; 251 MaybeHandle<String> ReadTwoByteString() WARN_UNUSED_RESULT; 252 MaybeHandle<JSObject> ReadJSObject() WARN_UNUSED_RESULT; 253 MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT; 254 MaybeHandle<JSArray> ReadDenseJSArray() WARN_UNUSED_RESULT; 255 MaybeHandle<JSDate> ReadJSDate() WARN_UNUSED_RESULT; 256 MaybeHandle<JSValue> ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; 257 MaybeHandle<JSRegExp> ReadJSRegExp() WARN_UNUSED_RESULT; 258 MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT; 259 MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT; 260 MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT; 261 MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer(bool is_shared) 262 WARN_UNUSED_RESULT; 263 MaybeHandle<JSArrayBufferView> ReadJSArrayBufferView( 264 Handle<JSArrayBuffer> buffer) WARN_UNUSED_RESULT; 265 MaybeHandle<JSObject> ReadWasmModule() WARN_UNUSED_RESULT; 266 MaybeHandle<JSObject> ReadHostObject() WARN_UNUSED_RESULT; 267 268 /* 269 * Reads key-value pairs into the object until the specified end tag is 270 * encountered. If successful, returns the number of properties read. 271 */ 272 Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object, 273 SerializationTag end_tag, 274 bool can_use_transitions); 275 276 // Manipulating the map from IDs to reified objects. 277 bool HasObjectWithID(uint32_t id); 278 MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id); 279 void AddObjectWithID(uint32_t id, Handle<JSReceiver> object); 280 281 Isolate* const isolate_; 282 v8::ValueDeserializer::Delegate* const delegate_; 283 const uint8_t* position_; 284 const uint8_t* const end_; 285 PretenureFlag pretenure_; 286 uint32_t version_ = 0; 287 uint32_t next_id_ = 0; 288 289 // Always global handles. 290 Handle<FixedArray> id_map_; 291 MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_; 292 293 DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); 294 }; 295 296 } // namespace internal 297 } // namespace v8 298 299 #endif // V8_VALUE_SERIALIZER_H_ 300