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