snapshot.h 5 KB
Newer Older
1
// Copyright 2020 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_SNAPSHOT_SNAPSHOT_H_
#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 13
namespace v8 {
namespace internal {
14

15
class Context;
16
class Isolate;
17 18
class SnapshotData;
class JSGlobalProxy;
19

20
class Snapshot : public AllStatic {
21
 public:
22 23
  // ---------------- Serialization -------------------------------------------

24 25 26 27 28 29
  enum SerializerFlag {
    // If set, serializes unknown external references as verbatim data. This
    // usually leads to invalid state if the snapshot is deserialized in a
    // different isolate or a different process.
    // If unset, all external references must be known to the encoder.
    kAllowUnknownExternalReferencesForTesting = 1 << 0,
30 31 32 33 34 35 36 37 38
    // If set, the serializer enters a more permissive mode which allows
    // serialization of a currently active, running isolate. This has multiple
    // effects; for example, open handles are allowed, microtasks may exist,
    // etc. Note that in this mode, the serializer is allowed to skip
    // visitation of certain problematic areas even if they are non-empty. The
    // resulting snapshot is not guaranteed to result in a runnable context
    // after deserialization.
    // If unset, we assert that these previously mentioned areas are empty.
    kAllowActiveIsolateForTesting = 1 << 1,
39 40 41 42 43
  };
  using SerializerFlags = base::Flags<SerializerFlag>;
  V8_EXPORT_PRIVATE static constexpr SerializerFlags kDefaultSerializerFlags =
      {};

44 45 46 47 48 49 50
  // In preparation for serialization, clear data from the given isolate's heap
  // that 1. can be reconstructed and 2. is not suitable for serialization. The
  // `clear_recompilable_data` flag controls whether compiled objects are
  // cleared from shared function infos and regexp objects.
  V8_EXPORT_PRIVATE static void ClearReconstructableDataForSerialization(
      Isolate* isolate, bool clear_recompilable_data);

51 52 53 54 55 56 57
  // Serializes the given isolate and contexts. Each context may have an
  // associated callback to serialize internal fields. The default context must
  // be passed at index 0.
  static v8::StartupData Create(
      Isolate* isolate, std::vector<Context>* contexts,
      const std::vector<SerializeInternalFieldsCallback>&
          embedder_fields_serializers,
58
      const DisallowHeapAllocation& no_gc,
59
      SerializerFlags flags = kDefaultSerializerFlags);
60 61

  // Convenience helper for the above when only serializing a single context.
62 63
  static v8::StartupData Create(
      Isolate* isolate, Context default_context,
64
      const DisallowHeapAllocation& no_gc,
65
      SerializerFlags flags = kDefaultSerializerFlags);
66 67

  // ---------------- Deserialization -----------------------------------------
68

69 70 71
  // Initialize the Isolate from the internal snapshot. Returns false if no
  // snapshot could be found.
  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 80 81 82 83 84
  // ---------------- Testing -------------------------------------------------

  // This function is used to stress the snapshot component. It serializes the
  // current isolate and context into a snapshot, deserializes the snapshot into
  // a new isolate and context, and finally runs VerifyHeap on the fresh
  // isolate.
85
  V8_EXPORT_PRIVATE static void SerializeDeserializeAndVerifyForTesting(
86 87
      Isolate* isolate, Handle<Context> default_context);

88
  // ---------------- Helper methods ------------------------------------------
89 90

  static bool HasContextSnapshot(Isolate* isolate, size_t index);
91
  static bool EmbedsScript(Isolate* isolate);
92 93
  V8_EXPORT_PRIVATE static bool VerifyChecksum(const v8::StartupData* data);
  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 105 106 107
// Convenience wrapper around snapshot data blob creation used e.g. by tests and
// mksnapshot.
V8_EXPORT_PRIVATE v8::StartupData CreateSnapshotDataBlobInternal(
    v8::SnapshotCreator::FunctionCodeHandling function_code_handling,
108 109 110 111 112 113
    const char* embedded_source, v8::Isolate* isolate = nullptr);

// Convenience wrapper around snapshot data blob warmup used e.g. by tests and
// mksnapshot.
V8_EXPORT_PRIVATE v8::StartupData WarmUpSnapshotDataBlobInternal(
    v8::StartupData cold_snapshot_blob, const char* warmup_source);
114

115 116 117 118
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
void SetSnapshotFromFile(StartupData* snapshot_blob);
#endif

119 120
}  // namespace internal
}  // namespace v8
121

122
#endif  // V8_SNAPSHOT_SNAPSHOT_H_