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