// Copyright 2016 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_VALUE_SERIALIZER_H_ #define V8_VALUE_SERIALIZER_H_ #include #include #include "include/v8.h" #include "src/base/compiler-specific.h" #include "src/base/macros.h" #include "src/identity-map.h" #include "src/messages.h" #include "src/vector.h" #include "src/zone.h" namespace v8 { namespace internal { class HeapNumber; class Isolate; class JSArrayBuffer; class JSArrayBufferView; class JSDate; class JSMap; class JSRegExp; class JSSet; class JSValue; class Object; class Oddball; class Smi; enum class SerializationTag : uint8_t; /** * Writes V8 objects in a binary format that allows the objects to be cloned * according to the HTML structured clone algorithm. * * Format is based on Blink's previous serialization logic. */ class ValueSerializer { public: ValueSerializer(Isolate* isolate, v8::ValueSerializer::Delegate* delegate); ~ValueSerializer(); /* * Writes out a header, which includes the format version. */ void WriteHeader(); /* * Serializes a V8 object into the buffer. */ Maybe WriteObject(Handle object) WARN_UNUSED_RESULT; /* * Returns the stored data. This serializer should not be used once the buffer * is released. The contents are undefined if a previous write has failed. */ std::vector ReleaseBuffer() { return std::move(buffer_); } /* * Marks an ArrayBuffer as havings its contents transferred out of band. * Pass the corresponding JSArrayBuffer in the deserializing context to * ValueDeserializer::TransferArrayBuffer. */ void TransferArrayBuffer(uint32_t transfer_id, Handle array_buffer); private: // Writing the wire format. void WriteTag(SerializationTag tag); template void WriteVarint(T value); template void WriteZigZag(T value); void WriteDouble(double value); void WriteOneByteString(Vector chars); void WriteTwoByteString(Vector chars); void WriteRawBytes(const void* source, size_t length); uint8_t* ReserveRawBytes(size_t bytes); // Writing V8 objects of various kinds. void WriteOddball(Oddball* oddball); void WriteSmi(Smi* smi); void WriteHeapNumber(HeapNumber* number); void WriteString(Handle string); Maybe WriteJSReceiver(Handle receiver) WARN_UNUSED_RESULT; Maybe WriteJSObject(Handle object) WARN_UNUSED_RESULT; Maybe WriteJSObjectSlow(Handle object) WARN_UNUSED_RESULT; Maybe WriteJSArray(Handle array) WARN_UNUSED_RESULT; void WriteJSDate(JSDate* date); Maybe WriteJSValue(Handle value) WARN_UNUSED_RESULT; void WriteJSRegExp(JSRegExp* regexp); Maybe WriteJSMap(Handle map) WARN_UNUSED_RESULT; Maybe WriteJSSet(Handle map) WARN_UNUSED_RESULT; Maybe WriteJSArrayBuffer(JSArrayBuffer* array_buffer); Maybe WriteJSArrayBufferView(JSArrayBufferView* array_buffer); /* * Reads the specified keys from the object and writes key-value pairs to the * buffer. Returns the number of keys actually written, which may be smaller * if some keys are not own properties when accessed. */ Maybe WriteJSObjectPropertiesSlow( Handle object, Handle keys) WARN_UNUSED_RESULT; /* * Asks the delegate to handle an error that occurred during data cloning, by * throwing an exception appropriate for the host. */ void ThrowDataCloneError(MessageTemplate::Template template_index); V8_NOINLINE void ThrowDataCloneError(MessageTemplate::Template template_index, Handle arg0); Isolate* const isolate_; v8::ValueSerializer::Delegate* const delegate_; std::vector buffer_; Zone zone_; // To avoid extra lookups in the identity map, ID+1 is actually stored in the // map (checking if the used identity is zero is the fast way of checking if // the entry is new). IdentityMap id_map_; uint32_t next_id_ = 0; // A similar map, for transferred array buffers. IdentityMap array_buffer_transfer_map_; DISALLOW_COPY_AND_ASSIGN(ValueSerializer); }; /* * Deserializes values from data written with ValueSerializer, or a compatible * implementation. */ class ValueDeserializer { public: ValueDeserializer(Isolate* isolate, Vector data); ~ValueDeserializer(); /* * Runs version detection logic, which may fail if the format is invalid. */ Maybe ReadHeader() WARN_UNUSED_RESULT; /* * Reads the underlying wire format version. Likely mostly to be useful to * legacy code reading old wire format versions. Must be called after * ReadHeader. */ uint32_t GetWireFormatVersion() const { return version_; } /* * Deserializes a V8 object from the buffer. */ MaybeHandle ReadObject() WARN_UNUSED_RESULT; /* * Reads an object, consuming the entire buffer. * * This is required for the legacy "version 0" format, which did not allow * reference deduplication, and instead relied on a "stack" model for * deserializing, with the contents of objects and arrays provided first. */ MaybeHandle ReadObjectUsingEntireBufferForLegacyFormat() WARN_UNUSED_RESULT; /* * Accepts the array buffer corresponding to the one passed previously to * ValueSerializer::TransferArrayBuffer. */ void TransferArrayBuffer(uint32_t transfer_id, Handle array_buffer); private: // Reading the wire format. Maybe PeekTag() const WARN_UNUSED_RESULT; void ConsumeTag(SerializationTag peeked_tag); Maybe ReadTag() WARN_UNUSED_RESULT; template Maybe ReadVarint() WARN_UNUSED_RESULT; template Maybe ReadZigZag() WARN_UNUSED_RESULT; Maybe ReadDouble() WARN_UNUSED_RESULT; Maybe> ReadRawBytes(int size) WARN_UNUSED_RESULT; // Like ReadObject, but skips logic for special cases in simulating the // "stack machine". MaybeHandle ReadObjectInternal() WARN_UNUSED_RESULT; // Reading V8 objects of specific kinds. // The tag is assumed to have already been read. MaybeHandle ReadUtf8String() WARN_UNUSED_RESULT; MaybeHandle ReadTwoByteString() WARN_UNUSED_RESULT; MaybeHandle ReadJSObject() WARN_UNUSED_RESULT; MaybeHandle ReadSparseJSArray() WARN_UNUSED_RESULT; MaybeHandle ReadDenseJSArray() WARN_UNUSED_RESULT; MaybeHandle ReadJSDate() WARN_UNUSED_RESULT; MaybeHandle ReadJSValue(SerializationTag tag) WARN_UNUSED_RESULT; MaybeHandle ReadJSRegExp() WARN_UNUSED_RESULT; MaybeHandle ReadJSMap() WARN_UNUSED_RESULT; MaybeHandle ReadJSSet() WARN_UNUSED_RESULT; MaybeHandle ReadJSArrayBuffer() WARN_UNUSED_RESULT; MaybeHandle ReadTransferredJSArrayBuffer(bool is_shared) WARN_UNUSED_RESULT; MaybeHandle ReadJSArrayBufferView( Handle buffer) WARN_UNUSED_RESULT; /* * Reads key-value pairs into the object until the specified end tag is * encountered. If successful, returns the number of properties read. */ Maybe ReadJSObjectProperties(Handle object, SerializationTag end_tag); // Manipulating the map from IDs to reified objects. bool HasObjectWithID(uint32_t id); MaybeHandle GetObjectWithID(uint32_t id); void AddObjectWithID(uint32_t id, Handle object); Isolate* const isolate_; const uint8_t* position_; const uint8_t* const end_; uint32_t version_ = 0; uint32_t next_id_ = 0; // Always global handles. Handle id_map_; MaybeHandle array_buffer_transfer_map_; DISALLOW_COPY_AND_ASSIGN(ValueDeserializer); }; } // namespace internal } // namespace v8 #endif // V8_VALUE_SERIALIZER_H_