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/partial-deserializer.h"
6 :
7 : #include "src/api.h"
8 : #include "src/heap/heap-inl.h"
9 : #include "src/snapshot/snapshot.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 83551 : 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 83551 : d.Deserialize(isolate, global_proxy, embedder_fields_deserializer);
23 :
24 : Handle<Object> result;
25 : return maybe_result.ToHandle(&result) ? Handle<Context>::cast(result)
26 167102 : : MaybeHandle<Context>();
27 : }
28 :
29 83551 : MaybeHandle<Object> PartialDeserializer::Deserialize(
30 : Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
31 : v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
32 167102 : Initialize(isolate);
33 83551 : if (!allocator()->ReserveSpace()) {
34 0 : V8::FatalProcessOutOfMemory("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 83551 : OldSpace* code_space = isolate->heap()->code_space();
43 : Address start_address = code_space->top();
44 : Object* root;
45 83551 : VisitRootPointer(Root::kPartialSnapshotCache, &root);
46 83551 : DeserializeDeferredObjects();
47 83551 : DeserializeEmbedderFields(embedder_fields_deserializer);
48 :
49 83551 : 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 83551 : CHECK_EQ(start_address, code_space->top());
55 :
56 167102 : if (FLAG_rehash_snapshot && can_rehash()) RehashContext(Context::cast(root));
57 :
58 167102 : return Handle<Object>(root, isolate);
59 : }
60 :
61 83551 : void PartialDeserializer::DeserializeEmbedderFields(
62 : v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
63 251043 : if (!source()->HasMore() || source()->Get() != kEmbedderFieldsData) return;
64 : DisallowHeapAllocation no_gc;
65 40 : DisallowJavascriptExecution no_js(isolate());
66 : DisallowCompilation no_compile(isolate());
67 : DCHECK_NOT_NULL(embedder_fields_deserializer.callback);
68 255 : for (int code = source()->Get(); code != kSynchronize;
69 : code = source()->Get()) {
70 : HandleScope scope(isolate());
71 215 : int space = code & kSpaceMask;
72 : DCHECK_LE(space, kNumberOfSpaces);
73 : DCHECK_EQ(code - space, kNewObject);
74 215 : Handle<JSObject> obj(JSObject::cast(GetBackReferencedObject(space)),
75 : isolate());
76 215 : int index = source()->GetInt();
77 215 : int size = source()->GetInt();
78 : // TODO(yangguo,jgruber): Turn this into a reusable shared buffer.
79 215 : 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 215 : embedder_fields_deserializer.data);
84 215 : delete[] data;
85 40 : }
86 : }
87 :
88 83496 : void PartialDeserializer::RehashContext(Context* context) {
89 : DCHECK(can_rehash());
90 166992 : for (const auto& array : transition_arrays()) array->Sort();
91 166992 : context->global_object()->global_dictionary()->Rehash();
92 83496 : SortMapDescriptors();
93 83496 : }
94 :
95 : } // namespace internal
96 : } // namespace v8
|