1 // Copyright 2016 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 #ifndef V8_SNAPSHOT_STARTUP_SERIALIZER_H_
6 #define V8_SNAPSHOT_STARTUP_SERIALIZER_H_
7 
8 #include <unordered_set>
9 
10 #include "src/handles/global-handles.h"
11 #include "src/snapshot/roots-serializer.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class HeapObject;
17 class SnapshotByteSink;
18 class ReadOnlySerializer;
19 
20 class V8_EXPORT_PRIVATE StartupSerializer : public RootsSerializer {
21  public:
22   StartupSerializer(Isolate* isolate, Snapshot::SerializerFlags flags,
23                     ReadOnlySerializer* read_only_serializer);
24   ~StartupSerializer() override;
25   StartupSerializer(const StartupSerializer&) = delete;
26   StartupSerializer& operator=(const StartupSerializer&) = delete;
27 
28   // Serialize the current state of the heap.  The order is:
29   // 1) Strong roots
30   // 2) Builtins and bytecode handlers
31   // 3) Startup object cache
32   // 4) Weak references (e.g. the string table)
33   void SerializeStrongReferences(const DisallowGarbageCollection& no_gc);
34   void SerializeWeakReferencesAndDeferred();
35 
36   // If |obj| can be serialized in the read-only snapshot then add it to the
37   // read-only object cache if not already present and emits a
38   // ReadOnlyObjectCache bytecode into |sink|. Returns whether this was
39   // successful.
40   bool SerializeUsingReadOnlyObjectCache(SnapshotByteSink* sink,
41                                          Handle<HeapObject> obj);
42 
43   // Adds |obj| to the startup object object cache if not already present and
44   // emits a StartupObjectCache bytecode into |sink|.
45   void SerializeUsingStartupObjectCache(SnapshotByteSink* sink,
46                                         Handle<HeapObject> obj);
47 
48   // The per-heap dirty FinalizationRegistry list is weak and not serialized. No
49   // JSFinalizationRegistries should be used during startup.
50   void CheckNoDirtyFinalizationRegistries();
51 
52  private:
53   void SerializeObjectImpl(Handle<HeapObject> o) override;
54   void SerializeStringTable(StringTable* string_table);
55 
56   ReadOnlySerializer* read_only_serializer_;
57   GlobalHandleVector<AccessorInfo> accessor_infos_;
58   GlobalHandleVector<CallHandlerInfo> call_handler_infos_;
59 };
60 
61 class SerializedHandleChecker : public RootVisitor {
62  public:
63   SerializedHandleChecker(Isolate* isolate, std::vector<Context>* contexts);
64   void VisitRootPointers(Root root, const char* description,
65                          FullObjectSlot start, FullObjectSlot end) override;
66   bool CheckGlobalAndEternalHandles();
67 
68  private:
69   void AddToSet(FixedArray serialized);
70 
71   Isolate* isolate_;
72   std::unordered_set<Object, Object::Hasher> serialized_;
73   bool ok_ = true;
74 };
75 
76 }  // namespace internal
77 }  // namespace v8
78 
79 #endif  // V8_SNAPSHOT_STARTUP_SERIALIZER_H_
80