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