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