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/context-deserializer.h"
6
7 #include "src/api/api-inl.h"
8 #include "src/common/assert-scope.h"
9 #include "src/heap/heap-inl.h"
10 #include "src/objects/slots.h"
11 #include "src/snapshot/snapshot.h"
12
13 namespace v8 {
14 namespace internal {
15
DeserializeContext(Isolate * isolate,const SnapshotData * data,bool can_rehash,Handle<JSGlobalProxy> global_proxy,v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer)16 MaybeHandle<Context> ContextDeserializer::DeserializeContext(
17 Isolate* isolate, const SnapshotData* data, bool can_rehash,
18 Handle<JSGlobalProxy> global_proxy,
19 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
20 ContextDeserializer d(isolate, data, can_rehash);
21
22 MaybeHandle<Object> maybe_result =
23 d.Deserialize(isolate, global_proxy, embedder_fields_deserializer);
24
25 Handle<Object> result;
26 return maybe_result.ToHandle(&result) ? Handle<Context>::cast(result)
27 : MaybeHandle<Context>();
28 }
29
Deserialize(Isolate * isolate,Handle<JSGlobalProxy> global_proxy,v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer)30 MaybeHandle<Object> ContextDeserializer::Deserialize(
31 Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
32 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
33 // Replace serialized references to the global proxy and its map with the
34 // given global proxy and its map.
35 AddAttachedObject(global_proxy);
36 AddAttachedObject(handle(global_proxy->map(), isolate));
37
38 Handle<Object> result;
39 {
40 // There's no code deserialized here. If this assert fires then that's
41 // changed and logging should be added to notify the profiler et al. of
42 // the new code, which also has to be flushed from instruction cache.
43 DisallowCodeAllocation no_code_allocation;
44
45 result = ReadObject();
46 DeserializeDeferredObjects();
47 DeserializeEmbedderFields(embedder_fields_deserializer);
48
49 LogNewMapEvents();
50 WeakenDescriptorArrays();
51 }
52
53 if (FLAG_rehash_snapshot && can_rehash()) Rehash();
54 SetupOffHeapArrayBufferBackingStores();
55
56 return result;
57 }
58
SetupOffHeapArrayBufferBackingStores()59 void ContextDeserializer::SetupOffHeapArrayBufferBackingStores() {
60 for (Handle<JSArrayBuffer> buffer : new_off_heap_array_buffers()) {
61 uint32_t store_index = buffer->GetBackingStoreRefForDeserialization();
62 auto bs = backing_store(store_index);
63 buffer->AllocateExternalPointerEntries(isolate());
64 SharedFlag shared =
65 bs && bs->is_shared() ? SharedFlag::kShared : SharedFlag::kNotShared;
66 buffer->Setup(shared, bs);
67 }
68 }
69
DeserializeEmbedderFields(v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer)70 void ContextDeserializer::DeserializeEmbedderFields(
71 v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
72 if (!source()->HasMore() || source()->Get() != kEmbedderFieldsData) return;
73 DisallowGarbageCollection no_gc;
74 DisallowJavascriptExecution no_js(isolate());
75 DisallowCompilation no_compile(isolate());
76 DCHECK_NOT_NULL(embedder_fields_deserializer.callback);
77 for (int code = source()->Get(); code != kSynchronize;
78 code = source()->Get()) {
79 HandleScope scope(isolate());
80 Handle<JSObject> obj = Handle<JSObject>::cast(GetBackReferencedObject());
81 int index = source()->GetInt();
82 int size = source()->GetInt();
83 // TODO(yangguo,jgruber): Turn this into a reusable shared buffer.
84 byte* data = new byte[size];
85 source()->CopyRaw(data, size);
86 embedder_fields_deserializer.callback(v8::Utils::ToLocal(obj), index,
87 {reinterpret_cast<char*>(data), size},
88 embedder_fields_deserializer.data);
89 delete[] data;
90 }
91 }
92 } // namespace internal
93 } // namespace v8
94