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

Generated by: LCOV version 1.10