• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // TODO(leszeks): Could we defer string serialization if forward references
55   // were resolved after object post processing?
56   return !o.IsMap() && !o.IsInternalizedString() &&
57          !(o.IsJSObject() && JSObject::cast(o).GetEmbedderFieldCount() > 0);
58 }
59 
RestoreExternalReferenceRedirector(Isolate * isolate,AccessorInfo accessor_info)60 void SerializerDeserializer::RestoreExternalReferenceRedirector(
61     Isolate* isolate, AccessorInfo accessor_info) {
62   DisallowGarbageCollection no_gc;
63   // Restore wiped accessor infos.
64   Foreign::cast(accessor_info.js_getter())
65       .set_foreign_address(isolate, accessor_info.redirected_getter());
66 }
67 
RestoreExternalReferenceRedirector(Isolate * isolate,CallHandlerInfo call_handler_info)68 void SerializerDeserializer::RestoreExternalReferenceRedirector(
69     Isolate* isolate, CallHandlerInfo call_handler_info) {
70   DisallowGarbageCollection no_gc;
71   Foreign::cast(call_handler_info.js_callback())
72       .set_foreign_address(isolate, call_handler_info.redirected_callback());
73 }
74 
75 }  // namespace internal
76 }  // namespace v8
77