value-serializer.h 11.5 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_OBJECTS_VALUE_SERIALIZER_H_
#define V8_OBJECTS_VALUE_SERIALIZER_H_
7 8 9 10

#include <cstdint>
#include <vector>

11
#include "include/v8-value-serializer.h"
12 13
#include "src/base/compiler-specific.h"
#include "src/base/macros.h"
14
#include "src/base/strings.h"
15
#include "src/base/vector.h"
16
#include "src/common/message-template.h"
17
#include "src/handles/maybe-handles.h"
18
#include "src/utils/identity-map.h"
19
#include "src/zone/zone.h"
20 21 22 23

namespace v8 {
namespace internal {

24
class BigInt;
25
class HeapNumber;
26
class Isolate;
27
class JSArrayBuffer;
28
class JSArrayBufferView;
29
class JSDate;
30
class JSMap;
31
class JSPrimitiveWrapper;
32
class JSRegExp;
33
class JSSet;
34 35
class Object;
class Oddball;
36
class Smi;
37
class WasmMemoryObject;
38
class WasmModuleObject;
39 40 41 42 43 44 45 46 47 48 49

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:
50
  ValueSerializer(Isolate* isolate, v8::ValueSerializer::Delegate* delegate);
51
  ~ValueSerializer();
52 53
  ValueSerializer(const ValueSerializer&) = delete;
  ValueSerializer& operator=(const ValueSerializer&) = delete;
54 55 56 57 58 59 60 61 62

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

  /*
   * Serializes a V8 object into the buffer.
   */
63
  Maybe<bool> WriteObject(Handle<Object> object) V8_WARN_UNUSED_RESULT;
64

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

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

79 80 81 82 83 84 85
  /*
   * 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);
86
  void WriteDouble(double value);
87

88 89 90 91 92 93 94 95 96
  /*
   * 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);

97
 private:
98 99
  friend class WebSnapshotSerializer;

100
  // Managing allocations of the internal buffer.
101
  Maybe<bool> ExpandBuffer(size_t required_capacity);
102

103 104 105 106
  // Writing the wire format.
  void WriteTag(SerializationTag tag);
  template <typename T>
  void WriteVarint(T value);
107 108
  template <typename T>
  void WriteZigZag(T value);
109
  void WriteOneByteString(base::Vector<const uint8_t> chars);
110
  void WriteTwoByteString(base::Vector<const base::uc16> chars);
111
  void WriteBigIntContents(BigInt bigint);
112
  Maybe<uint8_t*> ReserveRawBytes(size_t bytes);
113 114

  // Writing V8 objects of various kinds.
115
  void WriteOddball(Oddball oddball);
116
  void WriteSmi(Smi smi);
117
  void WriteHeapNumber(HeapNumber number);
118
  void WriteBigInt(BigInt bigint);
119
  void WriteString(Handle<String> string);
120 121 122 123 124
  Maybe<bool> WriteJSReceiver(Handle<JSReceiver> receiver)
      V8_WARN_UNUSED_RESULT;
  Maybe<bool> WriteJSObject(Handle<JSObject> object) V8_WARN_UNUSED_RESULT;
  Maybe<bool> WriteJSObjectSlow(Handle<JSObject> object) V8_WARN_UNUSED_RESULT;
  Maybe<bool> WriteJSArray(Handle<JSArray> array) V8_WARN_UNUSED_RESULT;
125
  void WriteJSDate(JSDate date);
126 127
  Maybe<bool> WriteJSPrimitiveWrapper(Handle<JSPrimitiveWrapper> value)
      V8_WARN_UNUSED_RESULT;
128
  void WriteJSRegExp(Handle<JSRegExp> regexp);
129 130
  Maybe<bool> WriteJSMap(Handle<JSMap> map) V8_WARN_UNUSED_RESULT;
  Maybe<bool> WriteJSSet(Handle<JSSet> map) V8_WARN_UNUSED_RESULT;
131
  Maybe<bool> WriteJSArrayBuffer(Handle<JSArrayBuffer> array_buffer)
132
      V8_WARN_UNUSED_RESULT;
133
  Maybe<bool> WriteJSArrayBufferView(JSArrayBufferView array_buffer);
134
  Maybe<bool> WriteJSError(Handle<JSObject> error) V8_WARN_UNUSED_RESULT;
135
#if V8_ENABLE_WEBASSEMBLY
136
  Maybe<bool> WriteWasmModule(Handle<WasmModuleObject> object)
137
      V8_WARN_UNUSED_RESULT;
138
  Maybe<bool> WriteWasmMemory(Handle<WasmMemoryObject> object)
139
      V8_WARN_UNUSED_RESULT;
140
#endif  // V8_ENABLE_WEBASSEMBLY
141
  Maybe<bool> WriteHostObject(Handle<JSObject> object) V8_WARN_UNUSED_RESULT;
142

143 144 145 146 147
  /*
   * 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.
   */
148
  Maybe<uint32_t> WriteJSObjectPropertiesSlow(
149
      Handle<JSObject> object, Handle<FixedArray> keys) V8_WARN_UNUSED_RESULT;
150

151 152 153 154
  /*
   * Asks the delegate to handle an error that occurred during data cloning, by
   * throwing an exception appropriate for the host.
   */
155 156
  void ThrowDataCloneError(MessageTemplate template_index);
  V8_NOINLINE void ThrowDataCloneError(MessageTemplate template_index,
157 158
                                       Handle<Object> arg0);

159 160
  Maybe<bool> ThrowIfOutOfMemory();

161
  Isolate* const isolate_;
162
  v8::ValueSerializer::Delegate* const delegate_;
163 164 165
  uint8_t* buffer_ = nullptr;
  size_t buffer_size_ = 0;
  size_t buffer_capacity_ = 0;
166
  bool treat_array_buffer_views_as_host_objects_ = false;
167
  bool out_of_memory_ = false;
168 169 170 171 172
  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).
173
  IdentityMap<uint32_t, ZoneAllocationPolicy> id_map_;
174
  uint32_t next_id_ = 0;
175

176
  // A similar map, for transferred array buffers.
177
  IdentityMap<uint32_t, ZoneAllocationPolicy> array_buffer_transfer_map_;
178 179 180 181 182 183 184 185
};

/*
 * Deserializes values from data written with ValueSerializer, or a compatible
 * implementation.
 */
class ValueDeserializer {
 public:
186
  ValueDeserializer(Isolate* isolate, base::Vector<const uint8_t> data,
187
                    v8::ValueDeserializer::Delegate* delegate);
188
  ValueDeserializer(Isolate* isolate, const uint8_t* data, size_t size);
189
  ~ValueDeserializer();
190 191
  ValueDeserializer(const ValueDeserializer&) = delete;
  ValueDeserializer& operator=(const ValueDeserializer&) = delete;
192 193 194 195

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

198 199 200 201 202 203 204
  /*
   * 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_; }

205 206 207
  /*
   * Deserializes a V8 object from the buffer.
   */
208
  MaybeHandle<Object> ReadObject() V8_WARN_UNUSED_RESULT;
209

210 211 212 213 214 215 216 217
  /*
   * 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()
218
      V8_WARN_UNUSED_RESULT;
219

220 221 222 223 224 225 226
  /*
   * Accepts the array buffer corresponding to the one passed previously to
   * ValueSerializer::TransferArrayBuffer.
   */
  void TransferArrayBuffer(uint32_t transfer_id,
                           Handle<JSArrayBuffer> array_buffer);

227 228 229 230
  /*
   * Publicly exposed wire format writing methods.
   * These are intended for use within the delegate's WriteHostObject method.
   */
231 232 233 234
  bool ReadUint32(uint32_t* value) V8_WARN_UNUSED_RESULT;
  bool ReadUint64(uint64_t* value) V8_WARN_UNUSED_RESULT;
  bool ReadDouble(double* value) V8_WARN_UNUSED_RESULT;
  bool ReadRawBytes(size_t length, const void** data) V8_WARN_UNUSED_RESULT;
235

236
 private:
237 238
  friend class WebSnapshotDeserializer;

239
  // Reading the wire format.
240
  Maybe<SerializationTag> PeekTag() const V8_WARN_UNUSED_RESULT;
241
  void ConsumeTag(SerializationTag peeked_tag);
242
  Maybe<SerializationTag> ReadTag() V8_WARN_UNUSED_RESULT;
243
  template <typename T>
244
  Maybe<T> ReadVarint() V8_WARN_UNUSED_RESULT;
245
  template <typename T>
246 247
  Maybe<T> ReadZigZag() V8_WARN_UNUSED_RESULT;
  Maybe<double> ReadDouble() V8_WARN_UNUSED_RESULT;
248 249
  Maybe<base::Vector<const uint8_t>> ReadRawBytes(int size)
      V8_WARN_UNUSED_RESULT;
250

251 252
  // Reads a string if it matches the one provided.
  // Returns true if this was the case. Otherwise, nothing is consumed.
253
  bool ReadExpectedString(Handle<String> expected) V8_WARN_UNUSED_RESULT;
254

255 256
  // Like ReadObject, but skips logic for special cases in simulating the
  // "stack machine".
257
  MaybeHandle<Object> ReadObjectInternal() V8_WARN_UNUSED_RESULT;
258

259 260 261
  // 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).
262
  MaybeHandle<String> ReadString() V8_WARN_UNUSED_RESULT;
263

264 265
  // Reading V8 objects of specific kinds.
  // The tag is assumed to have already been read.
266 267 268 269 270 271 272 273
  MaybeHandle<BigInt> ReadBigInt() V8_WARN_UNUSED_RESULT;
  MaybeHandle<String> ReadUtf8String() V8_WARN_UNUSED_RESULT;
  MaybeHandle<String> ReadOneByteString() V8_WARN_UNUSED_RESULT;
  MaybeHandle<String> ReadTwoByteString() V8_WARN_UNUSED_RESULT;
  MaybeHandle<JSObject> ReadJSObject() V8_WARN_UNUSED_RESULT;
  MaybeHandle<JSArray> ReadSparseJSArray() V8_WARN_UNUSED_RESULT;
  MaybeHandle<JSArray> ReadDenseJSArray() V8_WARN_UNUSED_RESULT;
  MaybeHandle<JSDate> ReadJSDate() V8_WARN_UNUSED_RESULT;
274 275
  MaybeHandle<JSPrimitiveWrapper> ReadJSPrimitiveWrapper(SerializationTag tag)
      V8_WARN_UNUSED_RESULT;
276 277 278
  MaybeHandle<JSRegExp> ReadJSRegExp() V8_WARN_UNUSED_RESULT;
  MaybeHandle<JSMap> ReadJSMap() V8_WARN_UNUSED_RESULT;
  MaybeHandle<JSSet> ReadJSSet() V8_WARN_UNUSED_RESULT;
279
  MaybeHandle<JSArrayBuffer> ReadJSArrayBuffer(bool is_shared)
280
      V8_WARN_UNUSED_RESULT;
281
  MaybeHandle<JSArrayBuffer> ReadTransferredJSArrayBuffer()
282
      V8_WARN_UNUSED_RESULT;
283
  MaybeHandle<JSArrayBufferView> ReadJSArrayBufferView(
284
      Handle<JSArrayBuffer> buffer) V8_WARN_UNUSED_RESULT;
285
  MaybeHandle<Object> ReadJSError() V8_WARN_UNUSED_RESULT;
286
#if V8_ENABLE_WEBASSEMBLY
287 288
  MaybeHandle<JSObject> ReadWasmModuleTransfer() V8_WARN_UNUSED_RESULT;
  MaybeHandle<WasmMemoryObject> ReadWasmMemory() V8_WARN_UNUSED_RESULT;
289
#endif  // V8_ENABLE_WEBASSEMBLY
290
  MaybeHandle<JSObject> ReadHostObject() V8_WARN_UNUSED_RESULT;
291 292 293 294 295 296

  /*
   * 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,
297 298
                                         SerializationTag end_tag,
                                         bool can_use_transitions);
299 300 301 302 303

  // 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);
304 305

  Isolate* const isolate_;
306
  v8::ValueDeserializer::Delegate* const delegate_;
307 308 309
  const uint8_t* position_;
  const uint8_t* const end_;
  uint32_t version_ = 0;
310
  uint32_t next_id_ = 0;
311

312
  // Always global handles.
313
  Handle<FixedArray> id_map_;
314
  MaybeHandle<SimpleNumberDictionary> array_buffer_transfer_map_;
315 316 317 318 319
};

}  // namespace internal
}  // namespace v8

320
#endif  // V8_OBJECTS_VALUE_SERIALIZER_H_