value-serializer.h 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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 <cstdint>
#include <vector>

#include "include/v8.h"
#include "src/base/compiler-specific.h"
#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 20 21

namespace v8 {
namespace internal {

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 32
class Object;
class Oddball;
33
class Smi;
34 35 36 37 38 39 40 41 42 43 44

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:
45
  ValueSerializer(Isolate* isolate, v8::ValueSerializer::Delegate* delegate);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  ~ValueSerializer();

  /*
   * Writes out a header, which includes the format version.
   */
  void WriteHeader();

  /*
   * Serializes a V8 object into the buffer.
   */
  Maybe<bool> WriteObject(Handle<Object> 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.
   */
62 63 64 65 66 67 68
  std::vector<uint8_t> ReleaseBuffer();

  /*
   * Returns the buffer, allocated via the delegate, and its size.
   * Caller assumes ownership of the buffer.
   */
  std::pair<uint8_t*, size_t> Release();
69

70 71 72 73 74 75 76 77
  /*
   * 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<JSArrayBuffer> array_buffer);

78 79 80 81 82 83 84
  /*
   * Publicly exposed wire format writing methods.
   * These are intended for use within the delegate's WriteHostObject method.
   */
  void WriteUint32(uint32_t value);
  void WriteUint64(uint64_t value);
  void WriteRawBytes(const void* source, size_t length);
85
  void WriteDouble(double value);
86

87 88 89 90 91 92 93 94 95
  /*
   * Indicate whether to treat ArrayBufferView objects as host objects,
   * i.e. pass them to Delegate::WriteHostObject. This should not be
   * called when no Delegate was passed.
   *
   * The default is not to treat ArrayBufferViews as host objects.
   */
  void SetTreatArrayBufferViewsAsHostObjects(bool mode);

96
 private:
97
  // Managing allocations of the internal buffer.
98
  Maybe<bool> ExpandBuffer(size_t required_capacity);
99

100 101 102 103
  // Writing the wire format.
  void WriteTag(SerializationTag tag);
  template <typename T>
  void WriteVarint(T value);
104 105
  template <typename T>
  void WriteZigZag(T value);
106 107
  void WriteOneByteString(Vector<const uint8_t> chars);
  void WriteTwoByteString(Vector<const uc16> chars);
108
  Maybe<uint8_t*> ReserveRawBytes(size_t bytes);
109 110 111

  // Writing V8 objects of various kinds.
  void WriteOddball(Oddball* oddball);
112 113
  void WriteSmi(Smi* smi);
  void WriteHeapNumber(HeapNumber* number);
114
  void WriteString(Handle<String> string);
115 116
  Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver) WARN_UNUSED_RESULT;
  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 123
  Maybe<bool> WriteJSMap(Handle<JSMap> map) WARN_UNUSED_RESULT;
  Maybe<bool> WriteJSSet(Handle<JSSet> map) WARN_UNUSED_RESULT;
124 125
  Maybe<bool> WriteJSArrayBuffer(Handle<JSArrayBuffer> array_buffer)
      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 132 133 134
  /*
   * 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.
   */
135
  Maybe<uint32_t> WriteJSObjectPropertiesSlow(
136 137
      Handle<JSObject> object, Handle<FixedArray> keys) WARN_UNUSED_RESULT;

138 139 140 141 142 143 144 145
  /*
   * 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<Object> arg0);

146 147
  Maybe<bool> ThrowIfOutOfMemory();

148
  Isolate* const isolate_;
149
  v8::ValueSerializer::Delegate* const delegate_;
150
  bool treat_array_buffer_views_as_host_objects_ = false;
151 152 153
  uint8_t* buffer_ = nullptr;
  size_t buffer_size_ = 0;
  size_t buffer_capacity_ = 0;
154
  bool out_of_memory_ = false;
155 156 157 158 159
  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).
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 167 168 169 170 171 172 173 174
  DISALLOW_COPY_AND_ASSIGN(ValueSerializer);
};

/*
 * Deserializes values from data written with ValueSerializer, or a compatible
 * implementation.
 */
class ValueDeserializer {
 public:
175 176
  ValueDeserializer(Isolate* isolate, Vector<const uint8_t> data,
                    v8::ValueDeserializer::Delegate* delegate);
177 178 179 180 181 182 183
  ~ValueDeserializer();

  /*
   * Runs version detection logic, which may fail if the format is invalid.
   */
  Maybe<bool> ReadHeader() WARN_UNUSED_RESULT;

184 185 186 187 188 189 190
  /*
   * 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_; }

191 192 193 194 195
  /*
   * Deserializes a V8 object from the buffer.
   */
  MaybeHandle<Object> ReadObject() WARN_UNUSED_RESULT;

196 197 198 199 200 201 202 203 204 205
  /*
   * 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<Object> ReadObjectUsingEntireBufferForLegacyFormat()
      WARN_UNUSED_RESULT;

206 207 208 209 210 211 212
  /*
   * Accepts the array buffer corresponding to the one passed previously to
   * ValueSerializer::TransferArrayBuffer.
   */
  void TransferArrayBuffer(uint32_t transfer_id,
                           Handle<JSArrayBuffer> array_buffer);

213 214 215 216 217 218
  /*
   * Publicly exposed wire format writing methods.
   * These are intended for use within the delegate's WriteHostObject method.
   */
  bool ReadUint32(uint32_t* value) WARN_UNUSED_RESULT;
  bool ReadUint64(uint64_t* value) WARN_UNUSED_RESULT;
219
  bool ReadDouble(double* value) WARN_UNUSED_RESULT;
220 221
  bool ReadRawBytes(size_t length, const void** data) WARN_UNUSED_RESULT;

222
 private:
223
  // Reading the wire format.
224
  Maybe<SerializationTag> PeekTag() const WARN_UNUSED_RESULT;
225
  void ConsumeTag(SerializationTag peeked_tag);
226 227 228
  Maybe<SerializationTag> ReadTag() WARN_UNUSED_RESULT;
  template <typename T>
  Maybe<T> ReadVarint() WARN_UNUSED_RESULT;
229 230 231
  template <typename T>
  Maybe<T> ReadZigZag() WARN_UNUSED_RESULT;
  Maybe<double> ReadDouble() WARN_UNUSED_RESULT;
232 233
  Maybe<Vector<const uint8_t>> ReadRawBytes(int size) WARN_UNUSED_RESULT;

234 235 236 237
  // Reads a string if it matches the one provided.
  // Returns true if this was the case. Otherwise, nothing is consumed.
  bool ReadExpectedString(Handle<String> expected) WARN_UNUSED_RESULT;

238 239 240 241
  // Like ReadObject, but skips logic for special cases in simulating the
  // "stack machine".
  MaybeHandle<Object> ReadObjectInternal() WARN_UNUSED_RESULT;

242 243 244 245 246
  // Reads a string intended to be part of a more complicated object.
  // Before v12, these are UTF-8 strings. After, they can be any encoding
  // permissible for a string (with the relevant tag).
  MaybeHandle<String> ReadString() WARN_UNUSED_RESULT;

247 248 249
  // Reading V8 objects of specific kinds.
  // The tag is assumed to have already been read.
  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 254
  MaybeHandle<JSArray> ReadSparseJSArray() WARN_UNUSED_RESULT;
  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 259
  MaybeHandle<JSMap> ReadJSMap() WARN_UNUSED_RESULT;
  MaybeHandle<JSSet> ReadJSSet() WARN_UNUSED_RESULT;
260
  MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer() WARN_UNUSED_RESULT;
261 262
  MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer(bool is_shared)
      WARN_UNUSED_RESULT;
263 264
  MaybeHandle<JSArrayBufferView> ReadJSArrayBufferView(
      Handle<JSArrayBuffer> buffer) WARN_UNUSED_RESULT;
265
  MaybeHandle<JSObject> ReadWasmModule() WARN_UNUSED_RESULT;
266
  MaybeHandle<JSObject> ReadHostObject() WARN_UNUSED_RESULT;
267 268 269 270 271 272

  /*
   * Reads key-value pairs into the object until the specified end tag is
   * encountered. If successful, returns the number of properties read.
   */
  Maybe<uint32_t> ReadJSObjectProperties(Handle<JSObject> object,
273 274
                                         SerializationTag end_tag,
                                         bool can_use_transitions);
275 276 277 278 279

  // Manipulating the map from IDs to reified objects.
  bool HasObjectWithID(uint32_t id);
  MaybeHandle<JSReceiver> GetObjectWithID(uint32_t id);
  void AddObjectWithID(uint32_t id, Handle<JSReceiver> object);
280 281

  Isolate* const isolate_;
282
  v8::ValueDeserializer::Delegate* const delegate_;
283 284
  const uint8_t* position_;
  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 292
  MaybeHandle<SeededNumberDictionary> array_buffer_transfer_map_;

293 294 295 296 297 298 299
  DISALLOW_COPY_AND_ASSIGN(ValueDeserializer);
};

}  // namespace internal
}  // namespace v8

#endif  // V8_VALUE_SERIALIZER_H_