LCOV - code coverage report
Current view: top level - src - value-serializer.h (source / functions) Hit Total Coverage
Test: app.info Lines: 1 1 100.0 %
Date: 2017-10-20 Functions: 0 0 -

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

Generated by: LCOV version 1.10