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 "src/heap/heap.h" 9 #include "src/objects.h" 10 #include "src/snapshot/serializer-common.h" 11 #include "src/snapshot/snapshot-source-sink.h" 12 13 namespace v8 { 14 namespace internal { 15 16 // Used for platforms with embedded constant pools to trigger deserialization 17 // of objects found in code. 18 #if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64) || \ 19 defined(V8_TARGET_ARCH_PPC) || defined(V8_TARGET_ARCH_S390) || \ 20 V8_EMBEDDED_CONSTANT_POOL 21 #define V8_CODE_EMBEDS_OBJECT_POINTER 1 22 #else 23 #define V8_CODE_EMBEDS_OBJECT_POINTER 0 24 #endif 25 26 class Heap; 27 28 // A Deserializer reads a snapshot and reconstructs the Object graph it defines. 29 class Deserializer : public SerializerDeserializer { 30 public: 31 // Create a deserializer from a snapshot byte source. 32 template <class Data> 33 explicit Deserializer(Data* data, bool deserializing_user_code = false) 34 : isolate_(NULL), 35 source_(data->Payload()), 36 magic_number_(data->GetMagicNumber()), 37 next_map_index_(0), 38 external_reference_table_(NULL), 39 deserialized_large_objects_(0), 40 deserializing_user_code_(deserializing_user_code), 41 next_alignment_(kWordAligned) { 42 DecodeReservation(data->Reservations()); 43 } 44 45 ~Deserializer() override; 46 47 // Deserialize the snapshot into an empty heap. 48 void Deserialize(Isolate* isolate); 49 50 // Deserialize a single object and the objects reachable from it. 51 MaybeHandle<Object> DeserializePartial( 52 Isolate* isolate, Handle<JSGlobalProxy> global_proxy, 53 v8::DeserializeInternalFieldsCallback internal_fields_deserializer); 54 55 // Deserialize an object graph. Fail gracefully. 56 MaybeHandle<HeapObject> DeserializeObject(Isolate* isolate); 57 58 // Add an object to back an attached reference. The order to add objects must 59 // mirror the order they are added in the serializer. 60 void AddAttachedObject(Handle<HeapObject> attached_object) { 61 attached_objects_.Add(attached_object); 62 } 63 64 private: 65 void VisitPointers(Object** start, Object** end) override; 66 67 void Synchronize(VisitorSynchronization::SyncTag tag) override; 68 69 void VisitRuntimeEntry(RelocInfo* rinfo) override { UNREACHABLE(); } 70 71 void Initialize(Isolate* isolate); 72 73 bool deserializing_user_code() { return deserializing_user_code_; } 74 75 void DecodeReservation(Vector<const SerializedData::Reservation> res); 76 77 bool ReserveSpace(); 78 79 void UnalignedCopy(Object** dest, Object** src) { 80 memcpy(dest, src, sizeof(*src)); 81 } 82 83 void SetAlignment(byte data) { 84 DCHECK_EQ(kWordAligned, next_alignment_); 85 int alignment = data - (kAlignmentPrefix - 1); 86 DCHECK_LE(kWordAligned, alignment); 87 DCHECK_LE(alignment, kDoubleUnaligned); 88 next_alignment_ = static_cast<AllocationAlignment>(alignment); 89 } 90 91 void DeserializeDeferredObjects(); 92 void DeserializeInternalFields( 93 v8::DeserializeInternalFieldsCallback internal_fields_deserializer); 94 95 void FlushICacheForNewIsolate(); 96 void FlushICacheForNewCodeObjectsAndRecordEmbeddedObjects(); 97 98 void CommitPostProcessedObjects(Isolate* isolate); 99 100 // Fills in some heap data in an area from start to end (non-inclusive). The 101 // space id is used for the write barrier. The object_address is the address 102 // of the object we are writing into, or NULL if we are not writing into an 103 // object, i.e. if we are writing a series of tagged values that are not on 104 // the heap. Return false if the object content has been deferred. 105 bool ReadData(Object** start, Object** end, int space, 106 Address object_address); 107 void ReadObject(int space_number, Object** write_back); 108 Address Allocate(int space_index, int size); 109 110 // Special handling for serialized code like hooking up internalized strings. 111 HeapObject* PostProcessNewObject(HeapObject* obj, int space); 112 113 // This returns the address of an object that has been described in the 114 // snapshot by chunk index and offset. 115 HeapObject* GetBackReferencedObject(int space); 116 117 Object** CopyInNativesSource(Vector<const char> source_vector, 118 Object** current); 119 120 // Cached current isolate. 121 Isolate* isolate_; 122 123 // Objects from the attached object descriptions in the serialized user code. 124 List<Handle<HeapObject> > attached_objects_; 125 126 SnapshotByteSource source_; 127 uint32_t magic_number_; 128 129 // The address of the next object that will be allocated in each space. 130 // Each space has a number of chunks reserved by the GC, with each chunk 131 // fitting into a page. Deserialized objects are allocated into the 132 // current chunk of the target space by bumping up high water mark. 133 Heap::Reservation reservations_[kNumberOfSpaces]; 134 uint32_t current_chunk_[kNumberOfPreallocatedSpaces]; 135 Address high_water_[kNumberOfPreallocatedSpaces]; 136 int next_map_index_; 137 List<Address> allocated_maps_; 138 139 ExternalReferenceTable* external_reference_table_; 140 141 List<HeapObject*> deserialized_large_objects_; 142 List<Code*> new_code_objects_; 143 List<AccessorInfo*> accessor_infos_; 144 List<Handle<String> > new_internalized_strings_; 145 List<Handle<Script> > new_scripts_; 146 147 bool deserializing_user_code_; 148 149 AllocationAlignment next_alignment_; 150 151 DISALLOW_COPY_AND_ASSIGN(Deserializer); 152 }; 153 154 } // namespace internal 155 } // namespace v8 156 157 #endif // V8_SNAPSHOT_DESERIALIZER_H_ 158