Line data Source code
1 : // Copyright 2017 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 : #include "src/snapshot/object-deserializer.h"
6 :
7 : #include "src/assembler-inl.h"
8 : #include "src/isolate.h"
9 : #include "src/objects.h"
10 : #include "src/objects/slots.h"
11 : #include "src/snapshot/code-serializer.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 0 : ObjectDeserializer::ObjectDeserializer(const SerializedCodeData* data)
17 214 : : Deserializer(data, true) {}
18 :
19 : MaybeHandle<SharedFunctionInfo>
20 214 : ObjectDeserializer::DeserializeSharedFunctionInfo(
21 : Isolate* isolate, const SerializedCodeData* data, Handle<String> source) {
22 : ObjectDeserializer d(data);
23 :
24 : d.AddAttachedObject(source);
25 :
26 : Handle<HeapObject> result;
27 428 : return d.Deserialize(isolate).ToHandle(&result)
28 : ? Handle<SharedFunctionInfo>::cast(result)
29 428 : : MaybeHandle<SharedFunctionInfo>();
30 : }
31 :
32 214 : MaybeHandle<HeapObject> ObjectDeserializer::Deserialize(Isolate* isolate) {
33 214 : Initialize(isolate);
34 :
35 214 : if (!allocator()->ReserveSpace()) return MaybeHandle<HeapObject>();
36 :
37 : DCHECK(deserializing_user_code());
38 : HandleScope scope(isolate);
39 : Handle<HeapObject> result;
40 : {
41 : DisallowHeapAllocation no_gc;
42 214 : Object root;
43 : VisitRootPointer(Root::kPartialSnapshotCache, nullptr,
44 428 : FullObjectSlot(&root));
45 214 : DeserializeDeferredObjects();
46 214 : FlushICache();
47 214 : LinkAllocationSites();
48 214 : LogNewMapEvents();
49 214 : result = handle(HeapObject::cast(root), isolate);
50 214 : Rehash();
51 214 : allocator()->RegisterDeserializedObjectsForBlackAllocation();
52 : }
53 214 : CommitPostProcessedObjects();
54 214 : return scope.CloseAndEscape(result);
55 : }
56 :
57 214 : void ObjectDeserializer::FlushICache() {
58 : DCHECK(deserializing_user_code());
59 428 : for (Code code : new_code_objects()) {
60 : // Record all references to embedded objects in the new code object.
61 : WriteBarrierForCode(code);
62 : Assembler::FlushICache(code->raw_instruction_start(),
63 0 : code->raw_instruction_size());
64 : }
65 214 : }
66 :
67 214 : void ObjectDeserializer::CommitPostProcessedObjects() {
68 6036 : CHECK_LE(new_internalized_strings().size(), kMaxInt);
69 : StringTable::EnsureCapacityForDeserialization(
70 428 : isolate(), static_cast<int>(new_internalized_strings().size()));
71 5180 : for (Handle<String> string : new_internalized_strings()) {
72 : DisallowHeapAllocation no_gc;
73 4752 : StringTableInsertionKey key(*string);
74 : DCHECK(
75 : StringTable::ForwardStringIfExists(isolate(), &key, *string).is_null());
76 4752 : StringTable::AddKeyNoResize(isolate(), &key);
77 : }
78 :
79 214 : Heap* heap = isolate()->heap();
80 : Factory* factory = isolate()->factory();
81 642 : for (Handle<Script> script : new_scripts()) {
82 : // Assign a new script id to avoid collision.
83 214 : script->set_id(isolate()->heap()->NextScriptId());
84 214 : LogScriptEvents(*script);
85 : // Add script to list.
86 : Handle<WeakArrayList> list = factory->script_list();
87 : list = WeakArrayList::AddToEnd(isolate(), list,
88 428 : MaybeObjectHandle::Weak(script));
89 : heap->SetRootScriptList(*list);
90 : }
91 214 : }
92 :
93 214 : void ObjectDeserializer::LinkAllocationSites() {
94 : DisallowHeapAllocation no_gc;
95 214 : Heap* heap = isolate()->heap();
96 : // Allocation sites are present in the snapshot, and must be linked into
97 : // a list at deserialization time.
98 428 : for (AllocationSite site : new_allocation_sites()) {
99 0 : if (!site->HasWeakNext()) continue;
100 : // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
101 : // as a (weak) root. If this root is relocated correctly, this becomes
102 : // unnecessary.
103 0 : if (heap->allocation_sites_list() == Smi::kZero) {
104 0 : site->set_weak_next(ReadOnlyRoots(heap).undefined_value());
105 : } else {
106 0 : site->set_weak_next(heap->allocation_sites_list());
107 : }
108 : heap->set_allocation_sites_list(site);
109 : }
110 214 : }
111 :
112 : } // namespace internal
113 183867 : } // namespace v8
|