V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
deserializer.h
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_SNAPSHOT_DESERIALIZER_H_
6 #define V8_SNAPSHOT_DESERIALIZER_H_
7 
8 #include <vector>
9 
10 #include "src/objects/code.h"
11 #include "src/objects/js-array.h"
12 #include "src/objects/map.h"
13 #include "src/objects/string.h"
14 #include "src/snapshot/deserializer-allocator.h"
15 #include "src/snapshot/serializer-common.h"
16 #include "src/snapshot/snapshot-source-sink.h"
17 
18 namespace v8 {
19 namespace internal {
20 
21 class AllocationSite;
22 class HeapObject;
23 class Object;
24 class UnalignedSlot;
25 
26 // Used for platforms with embedded constant pools to trigger deserialization
27 // of objects found in code.
28 #if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64) || \
29  defined(V8_TARGET_ARCH_PPC) || defined(V8_TARGET_ARCH_S390) || \
30  V8_EMBEDDED_CONSTANT_POOL
31 #define V8_CODE_EMBEDS_OBJECT_POINTER 1
32 #else
33 #define V8_CODE_EMBEDS_OBJECT_POINTER 0
34 #endif
35 
36 // A Deserializer reads a snapshot and reconstructs the Object graph it defines.
38  public:
39  ~Deserializer() override;
40 
41  void SetRehashability(bool v) { can_rehash_ = v; }
42 
43  protected:
44  // Create a deserializer from a snapshot byte source.
45  template <class Data>
46  Deserializer(Data* data, bool deserializing_user_code)
47  : isolate_(nullptr),
48  source_(data->Payload()),
49  magic_number_(data->GetMagicNumber()),
50  external_reference_table_(nullptr),
51  allocator_(this),
52  deserializing_user_code_(deserializing_user_code),
53  can_rehash_(false) {
54  allocator()->DecodeReservation(data->Reservations());
55  // We start the indices here at 1, so that we can distinguish between an
56  // actual index and a nullptr in a deserialized object requiring fix-up.
57  off_heap_backing_stores_.push_back(nullptr);
58  }
59 
60  void Initialize(Isolate* isolate);
61  void DeserializeDeferredObjects();
62 
63  // Create Log events for newly deserialized objects.
64  void LogNewObjectEvents();
65  void LogScriptEvents(Script* script);
66  void LogNewMapEvents();
67 
68  // This returns the address of an object that has been described in the
69  // snapshot by chunk index and offset.
70  HeapObject* GetBackReferencedObject(int space);
71 
72  // Add an object to back an attached reference. The order to add objects must
73  // mirror the order they are added in the serializer.
74  void AddAttachedObject(Handle<HeapObject> attached_object) {
75  attached_objects_.push_back(attached_object);
76  }
77 
78  Isolate* isolate() const { return isolate_; }
79  SnapshotByteSource* source() { return &source_; }
80  const std::vector<AllocationSite*>& new_allocation_sites() const {
81  return new_allocation_sites_;
82  }
83  const std::vector<Code>& new_code_objects() const {
84  return new_code_objects_;
85  }
86  const std::vector<Map>& new_maps() const { return new_maps_; }
87  const std::vector<AccessorInfo*>& accessor_infos() const {
88  return accessor_infos_;
89  }
90  const std::vector<CallHandlerInfo*>& call_handler_infos() const {
91  return call_handler_infos_;
92  }
93  const std::vector<Handle<String>>& new_internalized_strings() const {
94  return new_internalized_strings_;
95  }
96  const std::vector<Handle<Script>>& new_scripts() const {
97  return new_scripts_;
98  }
99 
100  DeserializerAllocator* allocator() { return &allocator_; }
101  bool deserializing_user_code() const { return deserializing_user_code_; }
102  bool can_rehash() const { return can_rehash_; }
103 
104  void Rehash();
105 
106  // Cached current isolate.
107  Isolate* isolate_;
108 
109  private:
110  void VisitRootPointers(Root root, const char* description, ObjectSlot start,
111  ObjectSlot end) override;
112 
113  void Synchronize(VisitorSynchronization::SyncTag tag) override;
114 
115  void UnalignedCopy(UnalignedSlot dest, MaybeObject value);
116  void UnalignedCopy(UnalignedSlot dest, Address value);
117 
118  // Fills in some heap data in an area from start to end (non-inclusive). The
119  // space id is used for the write barrier. The object_address is the address
120  // of the object we are writing into, or nullptr if we are not writing into an
121  // object, i.e. if we are writing a series of tagged values that are not on
122  // the heap. Return false if the object content has been deferred.
123  bool ReadData(UnalignedSlot start, UnalignedSlot end, int space,
124  Address object_address);
125 
126  // A helper function for ReadData, templatized on the bytecode for efficiency.
127  // Returns the new value of {current}.
128  template <int where, int how, int within, int space_number_if_any>
129  inline UnalignedSlot ReadDataCase(Isolate* isolate, UnalignedSlot current,
130  Address current_object_address, byte data,
131  bool write_barrier_needed);
132 
133  // A helper function for ReadData for reading external references.
134  // Returns the new value of {current}.
135  inline UnalignedSlot ReadExternalReferenceCase(
136  HowToCode how, UnalignedSlot current, Address current_object_address);
137 
138  void ReadObject(int space_number, UnalignedSlot write_back,
139  HeapObjectReferenceType reference_type);
140 
141  // Special handling for serialized code like hooking up internalized strings.
142  HeapObject* PostProcessNewObject(HeapObject* obj, int space);
143 
144  // Objects from the attached object descriptions in the serialized user code.
145  std::vector<Handle<HeapObject>> attached_objects_;
146 
147  SnapshotByteSource source_;
148  uint32_t magic_number_;
149 
150  ExternalReferenceTable* external_reference_table_;
151 
152  std::vector<Map> new_maps_;
153  std::vector<AllocationSite*> new_allocation_sites_;
154  std::vector<Code> new_code_objects_;
155  std::vector<AccessorInfo*> accessor_infos_;
156  std::vector<CallHandlerInfo*> call_handler_infos_;
157  std::vector<Handle<String>> new_internalized_strings_;
158  std::vector<Handle<Script>> new_scripts_;
159  std::vector<byte*> off_heap_backing_stores_;
160 
161  DeserializerAllocator allocator_;
162  const bool deserializing_user_code_;
163 
164  // TODO(6593): generalize rehashing, and remove this flag.
165  bool can_rehash_;
166  std::vector<HeapObject*> to_rehash_;
167 
168 #ifdef DEBUG
169  uint32_t num_api_references_;
170 #endif // DEBUG
171 
172  // For source(), isolate(), and allocator().
173  friend class DeserializerAllocator;
174 
175  DISALLOW_COPY_AND_ASSIGN(Deserializer);
176 };
177 
178 // Used to insert a deserialized internalized string into the string table.
180  public:
181  explicit StringTableInsertionKey(String string);
182 
183  bool IsMatch(Object* string) override;
184 
185  V8_WARN_UNUSED_RESULT Handle<String> AsHandle(Isolate* isolate) override;
186 
187  private:
188  uint32_t ComputeHashField(String string);
189 
190  String string_;
191  DISALLOW_HEAP_ALLOCATION(no_gc);
192 };
193 
194 } // namespace internal
195 } // namespace v8
196 
197 #endif // V8_SNAPSHOT_DESERIALIZER_H_
Definition: libplatform.h:13
Definition: v8.h:963