1 // Copyright 2020 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/serializer-deserializer.h"
6 
7 #include "src/objects/foreign-inl.h"
8 #include "src/objects/objects-inl.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 namespace {
14 DISABLE_CFI_PERF
IterateObjectCache(Isolate* isolate, std::vector<Object>* cache, Root root_id, RootVisitor* visitor)15 void IterateObjectCache(Isolate* isolate, std::vector<Object>* cache,
16                         Root root_id, RootVisitor* visitor) {
17   for (size_t i = 0;; ++i) {
18     // Extend the array ready to get a value when deserializing.
19     if (cache->size() <= i) cache->push_back(Smi::zero());
20     // During deserialization, the visitor populates the object cache and
21     // eventually terminates the cache with undefined.
22     visitor->VisitRootPointer(root_id, nullptr, FullObjectSlot(&cache->at(i)));
23     if (cache->at(i).IsUndefined(isolate)) break;
24   }
25 }
26 }  // namespace
27 
28 // The startup and shared heap object caches are terminated by undefined. We
29 // visit these caches...
30 //  - during deserialization to populate it.
31 //  - during normal GC to keep its content alive.
32 //  - not during serialization. The context serializer adds to it explicitly.
IterateStartupObjectCache(Isolate* isolate, RootVisitor* visitor)33 void SerializerDeserializer::IterateStartupObjectCache(Isolate* isolate,
34                                                        RootVisitor* visitor) {
35   IterateObjectCache(isolate, isolate->startup_object_cache(),
36                      Root::kStartupObjectCache, visitor);
37 }
38 
IterateSharedHeapObjectCache( Isolate* isolate, RootVisitor* visitor)39 void SerializerDeserializer::IterateSharedHeapObjectCache(
40     Isolate* isolate, RootVisitor* visitor) {
41   IterateObjectCache(isolate, isolate->shared_heap_object_cache(),
42                      Root::kSharedHeapObjectCache, visitor);
43 }
44 
CanBeDeferred(HeapObject o)45 bool SerializerDeserializer::CanBeDeferred(HeapObject o) {
46   // 1. Maps cannot be deferred as objects are expected to have a valid map
47   // immediately.
48   // 2. Internalized strings cannot be deferred as they might be
49   // converted to thin strings during post processing, at which point forward
50   // references to the now-thin string will already have been written.
51   // 3. JS objects with embedder fields cannot be deferred because the
52   // serialize/deserialize callbacks need the back reference immediately to
53   // identify the object.
54   // 4. ByteArray cannot be deferred as JSTypedArray needs the base_pointer
55   // ByteArray immediately if it's on heap.
56   // TODO(leszeks): Could we defer string serialization if forward references
57   // were resolved after object post processing?
58   return !o.IsMap() && !o.IsInternalizedString() &&
59          !(o.IsJSObject() && JSObject::cast(o).GetEmbedderFieldCount() > 0) &&
60          !o.IsByteArray();
61 }
62 
RestoreExternalReferenceRedirector( Isolate* isolate, AccessorInfo accessor_info)63 void SerializerDeserializer::RestoreExternalReferenceRedirector(
64     Isolate* isolate, AccessorInfo accessor_info) {
65   DisallowGarbageCollection no_gc;
66   // Restore wiped accessor infos.
67   Foreign::cast(accessor_info.js_getter())
68       .set_foreign_address(isolate, accessor_info.redirected_getter());
69 }
70 
RestoreExternalReferenceRedirector( Isolate* isolate, CallHandlerInfo call_handler_info)71 void SerializerDeserializer::RestoreExternalReferenceRedirector(
72     Isolate* isolate, CallHandlerInfo call_handler_info) {
73   DisallowGarbageCollection no_gc;
74   Foreign::cast(call_handler_info.js_callback())
75       .set_foreign_address(isolate, call_handler_info.redirected_callback());
76 }
77 
78 }  // namespace internal
79 }  // namespace v8
80