v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
snapshot.h
Go to the documentation of this file.
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-array-buffer.h" // For ArrayBuffer::Allocator.
11#include "include/v8-snapshot.h" // For StartupData.
13#include "src/common/globals.h"
15
16namespace v8 {
17namespace internal {
18
19class Context;
20class Isolate;
21class JSGlobalProxy;
22class SafepointScope;
23class SnapshotData;
24
25class Snapshot : public AllStatic {
26 public:
27 // ---------------- Serialization -------------------------------------------
28
30 // If set, serializes unknown external references as verbatim data. This
31 // usually leads to invalid state if the snapshot is deserialized in a
32 // different isolate or a different process.
33 // If unset, all external references must be known to the encoder.
35 // If set, the serializer enters a more permissive mode which allows
36 // serialization of a currently active, running isolate. This has multiple
37 // effects; for example, open handles are allowed, microtasks may exist,
38 // etc. Note that in this mode, the serializer is allowed to skip
39 // visitation of certain problematic areas even if they are non-empty. The
40 // resulting snapshot is not guaranteed to result in a runnable context
41 // after deserialization.
42 // If unset, we assert that these previously mentioned areas are empty.
44 // If set, the ReadOnlySerializer and the SharedHeapSerializer reconstructs
45 // their respective object caches from the existing ReadOnlyHeap's read-only
46 // object cache or the existing shared heap's object cache so the same
47 // mapping is used. This mode is used for testing deserialization of a
48 // snapshot from a live isolate that's using a shared ReadOnlyHeap or is
49 // attached to a shared isolate. Otherwise during deserialization the
50 // indices will mismatch, causing deserialization crashes when e.g. types
51 // mismatch. If unset, the read-only object cache is populated as read-only
52 // objects are serialized, and the shared heap object cache is populated as
53 // shared heap objects are serialized.
55 };
59
60 // In preparation for serialization, clear data from the given isolate's heap
61 // that 1. can be reconstructed and 2. is not suitable for serialization. The
62 // `clear_recompilable_data` flag controls whether compiled objects are
63 // cleared from shared function infos and regexp objects.
65 Isolate* isolate, bool clear_recompilable_data);
66
67 // Serializes the given isolate and contexts. Each context may have an
68 // associated callback to serialize internal fields. The default context must
69 // be passed at index 0.
71 Isolate* isolate, std::vector<Tagged<Context>>* contexts,
72 const std::vector<SerializeEmbedderFieldsCallback>&
73 embedder_fields_serializers,
74 const SafepointScope& safepoint_scope,
75 const DisallowGarbageCollection& no_gc,
77
78 // ---------------- Deserialization -----------------------------------------
79
80 // Initialize the Isolate from the internal snapshot. Returns false if no
81 // snapshot could be found.
82 static bool Initialize(Isolate* isolate);
83
84 // Create a new context using the internal context snapshot.
86 Isolate* isolate, DirectHandle<JSGlobalProxy> global_proxy,
87 size_t context_index,
88 DeserializeEmbedderFieldsCallback embedder_fields_deserializer);
89
90 // ---------------- Testing -------------------------------------------------
91
92 // This function is used to stress the snapshot component. It serializes the
93 // current isolate and context into a snapshot, deserializes the snapshot into
94 // a new isolate and context, and finally runs VerifyHeap on the fresh
95 // isolate.
97 Isolate* isolate, DirectHandle<Context> default_context);
98
99 // ---------------- Helper methods ------------------------------------------
100
101 static bool HasContextSnapshot(Isolate* isolate, size_t index);
102 static bool EmbedsScript(Isolate* isolate);
104 const v8::StartupData* data);
105 V8_EXPORT_PRIVATE static uint32_t CalculateChecksum(
106 const v8::StartupData* data);
107 V8_EXPORT_PRIVATE static bool VerifyChecksum(const v8::StartupData* data);
108 static bool ExtractRehashability(const v8::StartupData* data);
110 const v8::StartupData* data);
111 static bool VersionIsValid(const v8::StartupData* data);
112
113 // To be implemented by the snapshot source.
114 static const v8::StartupData* DefaultSnapshotBlob();
115 static bool ShouldVerifyChecksum(const v8::StartupData* data);
116
117#ifdef DEBUG
118 static bool SnapshotIsValid(const v8::StartupData* snapshot_blob);
119#endif // DEBUG
120};
121
122// Convenience wrapper around snapshot data blob creation used e.g. by tests.
124 v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
125 const char* embedded_source = nullptr,
126 Snapshot::SerializerFlags serializer_flags =
128// Convenience wrapper around snapshot data blob creation used e.g. by
129// mksnapshot.
131 v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
132 const char* embedded_source, v8::SnapshotCreator& snapshot_creator,
133 Snapshot::SerializerFlags serializer_flags =
135// .. and for inspector-test.cc which needs an extern declaration due to
136// restrictive include rules:
139 v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
140 const char* embedded_source);
141
142// Convenience wrapper around snapshot data blob warmup used e.g. by tests and
143// mksnapshot.
145 v8::StartupData cold_snapshot_blob, const char* warmup_source);
146
147#ifdef V8_USE_EXTERNAL_STARTUP_DATA
148void SetSnapshotFromFile(StartupData* snapshot_blob);
149#endif
150
151// The implementation of the API-exposed class SnapshotCreator.
153 public:
154 // This ctor is used for internal usages:
155 // 1. %ProfileCreateSnapshotDataBlob(): Needs to hook into an existing
156 // Isolate.
157 //
158 // TODO(v8:14490): Refactor 1. to go through the public API and simplify this
159 // part of the internal snapshot creator.
160 SnapshotCreatorImpl(Isolate* isolate, const intptr_t* api_external_references,
161 const StartupData* existing_blob, bool owns_isolate);
162 explicit SnapshotCreatorImpl(const v8::Isolate::CreateParams& params);
163
165 const v8::Isolate::CreateParams& params);
166
168
169 Isolate* isolate() const { return isolate_; }
170
175
176 size_t AddData(DirectHandle<NativeContext> context, Address object);
177 size_t AddData(Address object);
178
180 SnapshotCreator::FunctionCodeHandling function_code_handling,
181 Snapshot::SerializerFlags serializer_flags =
183
185 v8::SnapshotCreator* snapshot_creator);
186
187 static constexpr size_t kDefaultContextIndex = 0;
188 static constexpr size_t kFirstAddtlContextIndex = kDefaultContextIndex + 1;
189
190 private:
199
200 void InitInternal(const StartupData*);
201
203 bool created() const { return contexts_.size() == 0; }
204
205 const bool owns_isolate_;
207 std::unique_ptr<v8::ArrayBuffer::Allocator> array_buffer_allocator_;
208 std::vector<SerializableContext> contexts_;
209};
210
211} // namespace internal
212} // namespace v8
213
214#endif // V8_SNAPSHOT_SNAPSHOT_H_
static constexpr size_t kFirstAddtlContextIndex
Definition snapshot.h:188
void SetDefaultContext(DirectHandle< NativeContext > context, SerializeEmbedderFieldsCallback callback)
Definition snapshot.cc:948
size_t AddContext(DirectHandle< NativeContext > context, SerializeEmbedderFieldsCallback callback)
Definition snapshot.cc:960
static constexpr size_t kDefaultContextIndex
Definition snapshot.h:187
std::vector< SerializableContext > contexts_
Definition snapshot.h:208
SnapshotCreatorImpl(Isolate *isolate, const intptr_t *api_external_references, const StartupData *existing_blob, bool owns_isolate)
Definition snapshot.cc:881
size_t AddData(DirectHandle< NativeContext > context, Address object)
Definition snapshot.cc:972
static SnapshotCreatorImpl * FromSnapshotCreator(v8::SnapshotCreator *snapshot_creator)
Definition snapshot.cc:1162
DirectHandle< NativeContext > context_at(size_t i) const
Definition snapshot.cc:1010
void InitInternal(const StartupData *)
Definition snapshot.cc:860
StartupData CreateBlob(SnapshotCreator::FunctionCodeHandling function_code_handling, Snapshot::SerializerFlags serializer_flags=Snapshot::kDefaultSerializerFlags)
Definition snapshot.cc:1043
std::unique_ptr< v8::ArrayBuffer::Allocator > array_buffer_allocator_
Definition snapshot.h:207
static bool VersionIsValid(const v8::StartupData *data)
Definition snapshot.cc:164
static V8_EXPORT_PRIVATE void ClearReconstructableDataForSerialization(Isolate *isolate, bool clear_recompilable_data)
Definition snapshot.cc:221
static V8_EXPORT_PRIVATE void SerializeDeserializeAndVerifyForTesting(Isolate *isolate, DirectHandle< Context > default_context)
Definition snapshot.cc:346
static v8::StartupData Create(Isolate *isolate, std::vector< Tagged< Context > > *contexts, const std::vector< SerializeEmbedderFieldsCallback > &embedder_fields_serializers, const SafepointScope &safepoint_scope, const DisallowGarbageCollection &no_gc, SerializerFlags flags=kDefaultSerializerFlags)
Definition snapshot.cc:409
base::Flags< SerializerFlag > SerializerFlags
Definition snapshot.h:56
static bool EmbedsScript(Isolate *isolate)
static bool ExtractRehashability(const v8::StartupData *data)
Definition snapshot.cc:669
static bool Initialize(Isolate *isolate)
Definition snapshot.cc:176
static const v8::StartupData * DefaultSnapshotBlob()
static V8_EXPORT_PRIVATE uint32_t GetExpectedChecksum(const v8::StartupData *data)
Definition snapshot.cc:640
@ kReconstructReadOnlyAndSharedObjectCachesForTesting
Definition snapshot.h:54
@ kAllowUnknownExternalReferencesForTesting
Definition snapshot.h:34
static V8_EXPORT_PRIVATE constexpr SerializerFlags kDefaultSerializerFlags
Definition snapshot.h:57
static bool HasContextSnapshot(Isolate *isolate, size_t index)
Definition snapshot.cc:154
static V8_EXPORT_PRIVATE uint32_t CalculateChecksum(const v8::StartupData *data)
Definition snapshot.cc:643
static MaybeDirectHandle< Context > NewContextFromSnapshot(Isolate *isolate, DirectHandle< JSGlobalProxy > global_proxy, size_t context_index, DeserializeEmbedderFieldsCallback embedder_fields_deserializer)
Definition snapshot.cc:203
static bool ShouldVerifyChecksum(const v8::StartupData *data)
static V8_EXPORT_PRIVATE bool VerifyChecksum(const v8::StartupData *data)
Definition snapshot.cc:647
static V8_EXPORT_PRIVATE uint32_t ExtractReadOnlySnapshotChecksum(const v8::StartupData *data)
Definition snapshot.cc:677
TNode< Object > callback
v8::StartupData CreateSnapshotDataBlobInternalForInspectorTest(v8::SnapshotCreator::FunctionCodeHandling function_code_handling, const char *embedded_source)
Definition snapshot.cc:816
v8::StartupData WarmUpSnapshotDataBlobInternal(v8::StartupData cold_snapshot_blob, const char *warmup_source)
Definition snapshot.cc:823
void SetSnapshotFromFile(StartupData *snapshot_blob)
v8::StartupData CreateSnapshotDataBlobInternal(v8::SnapshotCreator::FunctionCodeHandling function_code_handling, const char *embedded_source, SnapshotCreator &snapshot_creator, Snapshot::SerializerFlags serializer_flags)
Definition snapshot.cc:784
#define V8_EXPORT_PRIVATE
Definition macros.h:460
SerializableContext(Address *handle_location, SerializeEmbedderFieldsCallback callback)
Definition snapshot.h:193