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/partial-deserializer.h"
6
7 #include "src/api-inl.h"
8 #include "src/heap/heap-inl.h"
9 #include "src/snapshot/snapshot.h"
10
11 namespace v8 {
12 namespace internal {
13
DeserializeContext(Isolate * isolate,const SnapshotData * data,bool can_rehash,Handle<JSGlobalProxy> global_proxy,v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer)14 MaybeHandle<Context> PartialDeserializer::DeserializeContext(
15 Isolate* isolate, const SnapshotData* data, bool can_rehash,
16 Handle<JSGlobalProxy> global_proxy,
17 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
18 PartialDeserializer d(data);
19 d.SetRehashability(can_rehash);
20
21 MaybeHandle<Object> maybe_result =
22 d.Deserialize(isolate, global_proxy, embedder_fields_deserializer);
23
24 Handle<Object> result;
25 return maybe_result.ToHandle(&result) ? Handle<Context>::cast(result)
26 : MaybeHandle<Context>();
27 }
28
Deserialize(Isolate * isolate,Handle<JSGlobalProxy> global_proxy,v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer)29 MaybeHandle<Object> PartialDeserializer::Deserialize(
30 Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
31 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
32 Initialize(isolate);
33 if (!allocator()->ReserveSpace()) {
34 V8::FatalProcessOutOfMemory(isolate, "PartialDeserializer");
35 }
36
37 AddAttachedObject(global_proxy);
38
39 DisallowHeapAllocation no_gc;
40 // Keep track of the code space start and end pointers in case new
41 // code objects were unserialized
42 CodeSpace* code_space = isolate->heap()->code_space();
43 Address start_address = code_space->top();
44 Object* root;
45 VisitRootPointer(Root::kPartialSnapshotCache, nullptr, &root);
46 DeserializeDeferredObjects();
47 DeserializeEmbedderFields(embedder_fields_deserializer);
48
49 allocator()->RegisterDeserializedObjectsForBlackAllocation();
50
51 // There's no code deserialized here. If this assert fires then that's
52 // changed and logging should be added to notify the profiler et al of the
53 // new code, which also has to be flushed from instruction cache.
54 CHECK_EQ(start_address, code_space->top());
55
56 if (FLAG_rehash_snapshot && can_rehash()) Rehash();
57
58 return Handle<Object>(root, isolate);
59 }
60
DeserializeEmbedderFields(v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer)61 void PartialDeserializer::DeserializeEmbedderFields(
62 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
63 if (!source()->HasMore() || source()->Get() != kEmbedderFieldsData) return;
64 DisallowHeapAllocation no_gc;
65 DisallowJavascriptExecution no_js(isolate());
66 DisallowCompilation no_compile(isolate());
67 DCHECK_NOT_NULL(embedder_fields_deserializer.callback);
68 for (int code = source()->Get(); code != kSynchronize;
69 code = source()->Get()) {
70 HandleScope scope(isolate());
71 int space = code & kSpaceMask;
72 DCHECK_LE(space, kNumberOfSpaces);
73 DCHECK_EQ(code - space, kNewObject);
74 Handle<JSObject> obj(JSObject::cast(GetBackReferencedObject(space)),
75 isolate());
76 int index = source()->GetInt();
77 int size = source()->GetInt();
78 // TODO(yangguo,jgruber): Turn this into a reusable shared buffer.
79 byte* data = new byte[size];
80 source()->CopyRaw(data, size);
81 embedder_fields_deserializer.callback(v8::Utils::ToLocal(obj), index,
82 {reinterpret_cast<char*>(data), size},
83 embedder_fields_deserializer.data);
84 delete[] data;
85 }
86 }
87 } // namespace internal
88 } // namespace v8
89