• 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 #ifndef V8_SNAPSHOT_SNAPSHOT_H_
6 #define V8_SNAPSHOT_SNAPSHOT_H_
7 
8 #include <vector>
9 
10 #include "include/v8-snapshot.h"  // For StartupData.
11 #include "src/common/assert-scope.h"
12 #include "src/common/globals.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class Context;
18 class GlobalSafepointScope;
19 class Isolate;
20 class SnapshotData;
21 class JSGlobalProxy;
22 
23 class Snapshot : public AllStatic {
24  public:
25   // ---------------- Serialization -------------------------------------------
26 
27   enum SerializerFlag {
28     // If set, serializes unknown external references as verbatim data. This
29     // usually leads to invalid state if the snapshot is deserialized in a
30     // different isolate or a different process.
31     // If unset, all external references must be known to the encoder.
32     kAllowUnknownExternalReferencesForTesting = 1 << 0,
33     // If set, the serializer enters a more permissive mode which allows
34     // serialization of a currently active, running isolate. This has multiple
35     // effects; for example, open handles are allowed, microtasks may exist,
36     // etc. Note that in this mode, the serializer is allowed to skip
37     // visitation of certain problematic areas even if they are non-empty. The
38     // resulting snapshot is not guaranteed to result in a runnable context
39     // after deserialization.
40     // If unset, we assert that these previously mentioned areas are empty.
41     kAllowActiveIsolateForTesting = 1 << 1,
42     // If set, the ReadOnlySerializer and the SharedHeapSerializer reconstructs
43     // their respective object caches from the existing ReadOnlyHeap's read-only
44     // object cache or the existing shared heap's object cache so the same
45     // mapping is used.  This mode is used for testing deserialization of a
46     // snapshot from a live isolate that's using a shared ReadOnlyHeap or is
47     // attached to a shared isolate. Otherwise during deserialization the
48     // indices will mismatch, causing deserialization crashes when e.g. types
49     // mismatch.  If unset, the read-only object cache is populated as read-only
50     // objects are serialized, and the shared heap object cache is populated as
51     // shared heap objects are serialized.
52     kReconstructReadOnlyAndSharedObjectCachesForTesting = 1 << 2,
53   };
54   using SerializerFlags = base::Flags<SerializerFlag>;
55   V8_EXPORT_PRIVATE static constexpr SerializerFlags kDefaultSerializerFlags =
56       {};
57 
58   // In preparation for serialization, clear data from the given isolate's heap
59   // that 1. can be reconstructed and 2. is not suitable for serialization. The
60   // `clear_recompilable_data` flag controls whether compiled objects are
61   // cleared from shared function infos and regexp objects.
62   V8_EXPORT_PRIVATE static void ClearReconstructableDataForSerialization(
63       Isolate* isolate, bool clear_recompilable_data);
64 
65   // Serializes the given isolate and contexts. Each context may have an
66   // associated callback to serialize internal fields. The default context must
67   // be passed at index 0.
68   static v8::StartupData Create(
69       Isolate* isolate, std::vector<Context>* contexts,
70       const std::vector<SerializeInternalFieldsCallback>&
71           embedder_fields_serializers,
72       const GlobalSafepointScope& global_safepoint,
73       const DisallowGarbageCollection& no_gc,
74       SerializerFlags flags = kDefaultSerializerFlags);
75 
76   // Convenience helper for the above when only serializing a single context.
77   static v8::StartupData Create(
78       Isolate* isolate, Context default_context,
79       const GlobalSafepointScope& global_safepoint,
80       const DisallowGarbageCollection& no_gc,
81       SerializerFlags flags = kDefaultSerializerFlags);
82 
83   // ---------------- Deserialization -----------------------------------------
84 
85   // Initialize the Isolate from the internal snapshot. Returns false if no
86   // snapshot could be found.
87   static bool Initialize(Isolate* isolate);
88 
89   // Create a new context using the internal context snapshot.
90   static MaybeHandle<Context> NewContextFromSnapshot(
91       Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
92       size_t context_index,
93       v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer);
94 
95   // ---------------- Testing -------------------------------------------------
96 
97   // This function is used to stress the snapshot component. It serializes the
98   // current isolate and context into a snapshot, deserializes the snapshot into
99   // a new isolate and context, and finally runs VerifyHeap on the fresh
100   // isolate.
101   V8_EXPORT_PRIVATE static void SerializeDeserializeAndVerifyForTesting(
102       Isolate* isolate, Handle<Context> default_context);
103 
104   // ---------------- Helper methods ------------------------------------------
105 
106   static bool HasContextSnapshot(Isolate* isolate, size_t index);
107   static bool EmbedsScript(Isolate* isolate);
108   V8_EXPORT_PRIVATE static bool VerifyChecksum(const v8::StartupData* data);
109   static bool ExtractRehashability(const v8::StartupData* data);
110   static bool VersionIsValid(const v8::StartupData* data);
111 
112   // To be implemented by the snapshot source.
113   static const v8::StartupData* DefaultSnapshotBlob();
114 
115 #ifdef DEBUG
116   static bool SnapshotIsValid(const v8::StartupData* snapshot_blob);
117 #endif  // DEBUG
118 };
119 
120 // Convenience wrapper around snapshot data blob creation used e.g. by tests and
121 // mksnapshot.
122 V8_EXPORT_PRIVATE v8::StartupData CreateSnapshotDataBlobInternal(
123     v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
124     const char* embedded_source, v8::Isolate* isolate = nullptr);
125 
126 // Convenience wrapper around snapshot data blob warmup used e.g. by tests and
127 // mksnapshot.
128 V8_EXPORT_PRIVATE v8::StartupData WarmUpSnapshotDataBlobInternal(
129     v8::StartupData cold_snapshot_blob, const char* warmup_source);
130 
131 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
132 void SetSnapshotFromFile(StartupData* snapshot_blob);
133 #endif
134 
135 }  // namespace internal
136 }  // namespace v8
137 
138 #endif  // V8_SNAPSHOT_SNAPSHOT_H_
139