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 :
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.
37 : class V8_EXPORT_PRIVATE Deserializer : public SerializerDeserializer {
38 : public:
39 : ~Deserializer() override;
40 :
41 216336 : void SetRehashability(bool v) { can_rehash_ = v; }
42 :
43 : protected:
44 : // Create a deserializer from a snapshot byte source.
45 : template <class Data>
46 216646 : Deserializer(Data* data, bool deserializing_user_code)
47 : : isolate_(nullptr),
48 : source_(data->Payload()),
49 : magic_number_(data->GetMagicNumber()),
50 : deserializing_user_code_(deserializing_user_code),
51 866592 : can_rehash_(false) {
52 649957 : allocator()->DecodeReservation(data->Reservations());
53 : // We start the indices here at 1, so that we can distinguish between an
54 : // actual index and a nullptr in a deserialized object requiring fix-up.
55 433308 : off_heap_backing_stores_.push_back(nullptr);
56 216652 : }
57 :
58 : void Initialize(Isolate* isolate);
59 : void DeserializeDeferredObjects();
60 :
61 : // Create Log events for newly deserialized objects.
62 : void LogNewObjectEvents();
63 : void LogScriptEvents(Script script);
64 : void LogNewMapEvents();
65 :
66 : // This returns the address of an object that has been described in the
67 : // snapshot by chunk index and offset.
68 : HeapObject GetBackReferencedObject(int space);
69 :
70 : // Add an object to back an attached reference. The order to add objects must
71 : // mirror the order they are added in the serializer.
72 : void AddAttachedObject(Handle<HeapObject> attached_object) {
73 91885 : attached_objects_.push_back(attached_object);
74 : }
75 :
76 : Isolate* isolate() const { return isolate_; }
77 5 : SnapshotByteSource* source() { return &source_; }
78 : const std::vector<AllocationSite>& new_allocation_sites() const {
79 : return new_allocation_sites_;
80 : }
81 : const std::vector<Code>& new_code_objects() const {
82 : return new_code_objects_;
83 : }
84 : const std::vector<Map>& new_maps() const { return new_maps_; }
85 : const std::vector<AccessorInfo>& accessor_infos() const {
86 62386 : return accessor_infos_;
87 : }
88 : const std::vector<CallHandlerInfo>& call_handler_infos() const {
89 62386 : return call_handler_infos_;
90 : }
91 : const std::vector<Handle<String>>& new_internalized_strings() const {
92 : return new_internalized_strings_;
93 : }
94 : const std::vector<Handle<Script>>& new_scripts() const {
95 : return new_scripts_;
96 : }
97 :
98 632115216 : DeserializerAllocator* allocator() { return &allocator_; }
99 : bool deserializing_user_code() const { return deserializing_user_code_; }
100 : bool can_rehash() const { return can_rehash_; }
101 :
102 : void Rehash();
103 :
104 : // Cached current isolate.
105 : Isolate* isolate_;
106 :
107 : private:
108 : void VisitRootPointers(Root root, const char* description,
109 : FullObjectSlot start, FullObjectSlot end) override;
110 :
111 : void Synchronize(VisitorSynchronization::SyncTag tag) override;
112 :
113 : template <typename TSlot>
114 : inline TSlot Write(TSlot dest, MaybeObject value);
115 :
116 : template <typename TSlot>
117 : inline TSlot WriteAddress(TSlot 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 : template <typename TSlot>
125 : bool ReadData(TSlot start, TSlot end, int space, 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 <typename TSlot, Bytecode bytecode, int space_number_if_any>
130 : inline TSlot ReadDataCase(Isolate* isolate, TSlot 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 : inline Address ReadExternalReferenceCase();
136 :
137 : HeapObject ReadObject();
138 : HeapObject ReadObject(int space_number);
139 : void ReadCodeObjectBody(int space_number, Address code_object_address);
140 :
141 : public:
142 : void VisitCodeTarget(Code host, RelocInfo* rinfo);
143 : void VisitEmbeddedPointer(Code host, RelocInfo* rinfo);
144 : void VisitRuntimeEntry(Code host, RelocInfo* rinfo);
145 : void VisitExternalReference(Code host, RelocInfo* rinfo);
146 : void VisitInternalReference(Code host, RelocInfo* rinfo);
147 : void VisitOffHeapTarget(Code host, RelocInfo* rinfo);
148 :
149 : private:
150 : template <typename TSlot>
151 : TSlot ReadRepeatedObject(TSlot current, int repeat_count);
152 :
153 : // Special handling for serialized code like hooking up internalized strings.
154 : HeapObject PostProcessNewObject(HeapObject obj, int space);
155 :
156 : // Objects from the attached object descriptions in the serialized user code.
157 : std::vector<Handle<HeapObject>> attached_objects_;
158 :
159 : SnapshotByteSource source_;
160 : uint32_t magic_number_;
161 :
162 : std::vector<Map> new_maps_;
163 : std::vector<AllocationSite> new_allocation_sites_;
164 : std::vector<Code> new_code_objects_;
165 : std::vector<AccessorInfo> accessor_infos_;
166 : std::vector<CallHandlerInfo> call_handler_infos_;
167 : std::vector<Handle<String>> new_internalized_strings_;
168 : std::vector<Handle<Script>> new_scripts_;
169 : std::vector<byte*> off_heap_backing_stores_;
170 :
171 : DeserializerAllocator allocator_;
172 : const bool deserializing_user_code_;
173 :
174 : // TODO(6593): generalize rehashing, and remove this flag.
175 : bool can_rehash_;
176 : std::vector<HeapObject> to_rehash_;
177 :
178 : #ifdef DEBUG
179 : uint32_t num_api_references_;
180 : #endif // DEBUG
181 :
182 : // For source(), isolate(), and allocator().
183 : friend class DeserializerAllocator;
184 :
185 : DISALLOW_COPY_AND_ASSIGN(Deserializer);
186 : };
187 :
188 : // Used to insert a deserialized internalized string into the string table.
189 14479 : class StringTableInsertionKey : public StringTableKey {
190 : public:
191 : explicit StringTableInsertionKey(String string);
192 :
193 : bool IsMatch(Object string) override;
194 :
195 : V8_WARN_UNUSED_RESULT Handle<String> AsHandle(Isolate* isolate) override;
196 :
197 : private:
198 : uint32_t ComputeHashField(String string);
199 :
200 : String string_;
201 : DISALLOW_HEAP_ALLOCATION(no_gc)
202 : };
203 :
204 : } // namespace internal
205 : } // namespace v8
206 :
207 : #endif // V8_SNAPSHOT_DESERIALIZER_H_
|