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 Deserializer : public SerializerDeserializer {
38 : public:
39 : ~Deserializer() override;
40 :
41 211632 : void SetRehashability(bool v) { can_rehash_ = v; }
42 :
43 : protected:
44 : // Create a deserializer from a snapshot byte source.
45 : template <class Data>
46 211895 : 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 847580 : can_rehash_(false) {
54 423790 : 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 423792 : off_heap_backing_stores_.push_back(nullptr);
58 211896 : }
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 89910 : 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,
111 : FullObjectSlot start, FullObjectSlot end) override;
112 :
113 : void Synchronize(VisitorSynchronization::SyncTag tag) override;
114 :
115 : template <typename TSlot>
116 : inline TSlot Write(TSlot dest, MaybeObject value);
117 :
118 : template <typename TSlot>
119 : inline TSlot WriteAddress(TSlot dest, Address value);
120 :
121 : // Fills in some heap data in an area from start to end (non-inclusive). The
122 : // space id is used for the write barrier. The object_address is the address
123 : // of the object we are writing into, or nullptr if we are not writing into an
124 : // object, i.e. if we are writing a series of tagged values that are not on
125 : // the heap. Return false if the object content has been deferred.
126 : template <typename TSlot>
127 : bool ReadData(TSlot start, TSlot end, int space, Address object_address);
128 :
129 : // A helper function for ReadData, templatized on the bytecode for efficiency.
130 : // Returns the new value of {current}.
131 : template <typename TSlot, Bytecode bytecode, int space_number_if_any>
132 : inline TSlot ReadDataCase(Isolate* isolate, TSlot current,
133 : Address current_object_address, byte data,
134 : bool write_barrier_needed);
135 :
136 : // A helper function for ReadData for reading external references.
137 : inline Address ReadExternalReferenceCase();
138 :
139 : HeapObject ReadObject();
140 : HeapObject ReadObject(int space_number);
141 : void ReadCodeObjectBody(int space_number, Address code_object_address);
142 :
143 : public:
144 : void VisitCodeTarget(Code host, RelocInfo* rinfo);
145 : void VisitEmbeddedPointer(Code host, RelocInfo* rinfo);
146 : void VisitRuntimeEntry(Code host, RelocInfo* rinfo);
147 : void VisitExternalReference(Code host, RelocInfo* rinfo);
148 : void VisitInternalReference(Code host, RelocInfo* rinfo);
149 : void VisitOffHeapTarget(Code host, RelocInfo* rinfo);
150 :
151 : private:
152 : template <typename TSlot>
153 : TSlot ReadRepeatedObject(TSlot current, int repeat_count);
154 :
155 : // Special handling for serialized code like hooking up internalized strings.
156 : HeapObject PostProcessNewObject(HeapObject obj, int space);
157 :
158 : // Objects from the attached object descriptions in the serialized user code.
159 : std::vector<Handle<HeapObject>> attached_objects_;
160 :
161 : SnapshotByteSource source_;
162 : uint32_t magic_number_;
163 :
164 : ExternalReferenceTable* external_reference_table_;
165 :
166 : std::vector<Map> new_maps_;
167 : std::vector<AllocationSite> new_allocation_sites_;
168 : std::vector<Code> new_code_objects_;
169 : std::vector<AccessorInfo> accessor_infos_;
170 : std::vector<CallHandlerInfo> call_handler_infos_;
171 : std::vector<Handle<String>> new_internalized_strings_;
172 : std::vector<Handle<Script>> new_scripts_;
173 : std::vector<byte*> off_heap_backing_stores_;
174 :
175 : DeserializerAllocator allocator_;
176 : const bool deserializing_user_code_;
177 :
178 : // TODO(6593): generalize rehashing, and remove this flag.
179 : bool can_rehash_;
180 : std::vector<HeapObject> to_rehash_;
181 :
182 : #ifdef DEBUG
183 : uint32_t num_api_references_;
184 : #endif // DEBUG
185 :
186 : // For source(), isolate(), and allocator().
187 : friend class DeserializerAllocator;
188 :
189 : DISALLOW_COPY_AND_ASSIGN(Deserializer);
190 : };
191 :
192 : // Used to insert a deserialized internalized string into the string table.
193 4682 : class StringTableInsertionKey : public StringTableKey {
194 : public:
195 : explicit StringTableInsertionKey(String string);
196 :
197 : bool IsMatch(Object string) override;
198 :
199 : V8_WARN_UNUSED_RESULT Handle<String> AsHandle(Isolate* isolate) override;
200 :
201 : private:
202 : uint32_t ComputeHashField(String string);
203 :
204 : String string_;
205 : DISALLOW_HEAP_ALLOCATION(no_gc)
206 : };
207 :
208 : } // namespace internal
209 : } // namespace v8
210 :
211 : #endif // V8_SNAPSHOT_DESERIALIZER_H_
|