Line data Source code
1 : // Copyright 2006-2008 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 "src/snapshot/partial-serializer.h"
9 : #include "src/snapshot/startup-serializer.h"
10 :
11 : #include "src/utils.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 : // Forward declarations.
17 : class Isolate;
18 : class BuiltinSerializer;
19 : class PartialSerializer;
20 : class StartupSerializer;
21 :
22 : // Wrapper around reservation sizes and the serialization payload.
23 239593 : class SnapshotData : public SerializedData {
24 : public:
25 : // Used when producing.
26 : template <class AllocatorT>
27 : explicit SnapshotData(const Serializer<AllocatorT>* serializer);
28 :
29 : // Used when consuming.
30 : explicit SnapshotData(const Vector<const byte> snapshot)
31 294249 : : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) {
32 : }
33 :
34 : Vector<const Reservation> Reservations() const;
35 : virtual Vector<const byte> Payload() const;
36 :
37 : Vector<const byte> RawData() const {
38 473 : return Vector<const byte>(data_, size_);
39 : }
40 :
41 : protected:
42 : // The data header consists of uint32_t-sized entries:
43 : // [0] magic number and (internal) external reference count
44 : // [1] number of reservation size entries
45 : // [2] payload length
46 : // ... reservations
47 : // ... serialized payload
48 : static const uint32_t kNumReservationsOffset =
49 : kMagicNumberOffset + kUInt32Size;
50 : static const uint32_t kPayloadLengthOffset =
51 : kNumReservationsOffset + kUInt32Size;
52 : static const uint32_t kHeaderSize = kPayloadLengthOffset + kUInt32Size;
53 : };
54 :
55 155891 : class BuiltinSnapshotData final : public SnapshotData {
56 : public:
57 : // Used when producing.
58 : // This simply forwards to the SnapshotData constructor.
59 : // The BuiltinSerializer appends the builtin offset table to the payload.
60 : explicit BuiltinSnapshotData(const BuiltinSerializer* serializer);
61 :
62 : // Used when consuming.
63 : explicit BuiltinSnapshotData(const Vector<const byte> snapshot)
64 155730 : : SnapshotData(snapshot) {
65 : }
66 :
67 : // Returns the serialized payload without the builtin offsets table.
68 : Vector<const byte> Payload() const override;
69 :
70 : // Returns only the builtin offsets table.
71 : Vector<const uint32_t> BuiltinOffsets() const;
72 :
73 : private:
74 : // In addition to the format specified in SnapshotData, BuiltinsSnapshotData
75 : // includes a list of builtin at the end of the serialized payload:
76 : //
77 : // ...
78 : // ... serialized payload
79 : // ... list of builtins offsets
80 : };
81 :
82 : class Snapshot : public AllStatic {
83 : public:
84 : // ---------------- Deserialization ----------------
85 :
86 : // Initialize the Isolate from the internal snapshot. Returns false if no
87 : // snapshot could be found.
88 : static bool Initialize(Isolate* isolate);
89 :
90 : // Create a new context using the internal partial snapshot.
91 : static MaybeHandle<Context> NewContextFromSnapshot(
92 : Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
93 : size_t context_index,
94 : v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer);
95 :
96 : // Deserializes a single given builtin code object. Intended to be called at
97 : // runtime after the isolate (and the builtins table) has been fully
98 : // initialized.
99 : static Code* DeserializeBuiltin(Isolate* isolate, int builtin_id);
100 :
101 : // ---------------- Helper methods ----------------
102 :
103 : static bool HasContextSnapshot(Isolate* isolate, size_t index);
104 : static bool EmbedsScript(Isolate* isolate);
105 :
106 : // To be implemented by the snapshot source.
107 : static const v8::StartupData* DefaultSnapshotBlob();
108 :
109 : // ---------------- Serialization ----------------
110 :
111 : static v8::StartupData CreateSnapshotBlob(
112 : const SnapshotData* startup_snapshot,
113 : const BuiltinSnapshotData* builtin_snapshot,
114 : const std::vector<SnapshotData*>& context_snapshots,
115 : bool can_be_rehashed);
116 :
117 : #ifdef DEBUG
118 : static bool SnapshotIsValid(const v8::StartupData* snapshot_blob);
119 : #endif // DEBUG
120 :
121 : private:
122 : static uint32_t ExtractNumContexts(const v8::StartupData* data);
123 : static uint32_t ExtractContextOffset(const v8::StartupData* data,
124 : uint32_t index);
125 : static bool ExtractRehashability(const v8::StartupData* data);
126 : static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
127 : static Vector<const byte> ExtractBuiltinData(const v8::StartupData* data);
128 : static Vector<const byte> ExtractContextData(const v8::StartupData* data,
129 : uint32_t index);
130 :
131 : static uint32_t GetHeaderValue(const v8::StartupData* data, uint32_t offset) {
132 83551 : return ReadLittleEndianValue<uint32_t>(data->data + offset);
133 : }
134 : static void SetHeaderValue(char* data, uint32_t offset, uint32_t value) {
135 141 : WriteLittleEndianValue(data + offset, value);
136 : }
137 :
138 : static void CheckVersion(const v8::StartupData* data);
139 :
140 : // Snapshot blob layout:
141 : // [0] number of contexts N
142 : // [1] rehashability
143 : // [2] (128 bytes) version string
144 : // [3] offset to builtins
145 : // [4] offset to context 0
146 : // [5] offset to context 1
147 : // ...
148 : // ... offset to context N - 1
149 : // ... startup snapshot data
150 : // ... builtin snapshot data
151 : // ... context 0 snapshot data
152 : // ... context 1 snapshot data
153 :
154 : static const uint32_t kNumberOfContextsOffset = 0;
155 : // TODO(yangguo): generalize rehashing, and remove this flag.
156 : static const uint32_t kRehashabilityOffset =
157 : kNumberOfContextsOffset + kUInt32Size;
158 : static const uint32_t kVersionStringOffset =
159 : kRehashabilityOffset + kUInt32Size;
160 : static const uint32_t kVersionStringLength = 64;
161 : static const uint32_t kBuiltinOffsetOffset =
162 : kVersionStringOffset + kVersionStringLength;
163 : static const uint32_t kFirstContextOffsetOffset =
164 : kBuiltinOffsetOffset + kUInt32Size;
165 :
166 : static uint32_t StartupSnapshotOffset(int num_contexts) {
167 55059 : return kFirstContextOffsetOffset + num_contexts * kInt32Size;
168 : }
169 :
170 : static uint32_t ContextSnapshotOffsetOffset(int index) {
171 83692 : return kFirstContextOffsetOffset + index * kInt32Size;
172 : }
173 :
174 : DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
175 : };
176 :
177 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
178 : void SetSnapshotFromFile(StartupData* snapshot_blob);
179 : #endif
180 :
181 : } // namespace internal
182 : } // namespace v8
183 :
184 : #endif // V8_SNAPSHOT_SNAPSHOT_H_
|