GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
|||
2 |
#include "node_snapshotable.h" |
||
3 |
#include <iostream> |
||
4 |
#include <sstream> |
||
5 |
#include "base_object-inl.h" |
||
6 |
#include "debug_utils-inl.h" |
||
7 |
#include "env-inl.h" |
||
8 |
#include "node_blob.h" |
||
9 |
#include "node_builtins.h" |
||
10 |
#include "node_contextify.h" |
||
11 |
#include "node_errors.h" |
||
12 |
#include "node_external_reference.h" |
||
13 |
#include "node_file.h" |
||
14 |
#include "node_internals.h" |
||
15 |
#include "node_main_instance.h" |
||
16 |
#include "node_metadata.h" |
||
17 |
#include "node_process.h" |
||
18 |
#include "node_snapshot_builder.h" |
||
19 |
#include "node_util.h" |
||
20 |
#include "node_v8.h" |
||
21 |
#include "node_v8_platform-inl.h" |
||
22 |
|||
23 |
#if HAVE_INSPECTOR |
||
24 |
#include "inspector/worker_inspector.h" // ParentInspectorHandle |
||
25 |
#endif |
||
26 |
|||
27 |
namespace node { |
||
28 |
|||
29 |
using v8::Context; |
||
30 |
using v8::Function; |
||
31 |
using v8::FunctionCallbackInfo; |
||
32 |
using v8::HandleScope; |
||
33 |
using v8::Isolate; |
||
34 |
using v8::Local; |
||
35 |
using v8::Object; |
||
36 |
using v8::ObjectTemplate; |
||
37 |
using v8::ScriptCompiler; |
||
38 |
using v8::ScriptOrigin; |
||
39 |
using v8::SnapshotCreator; |
||
40 |
using v8::StartupData; |
||
41 |
using v8::String; |
||
42 |
using v8::TryCatch; |
||
43 |
using v8::Value; |
||
44 |
|||
45 |
const uint32_t SnapshotData::kMagic; |
||
46 |
|||
47 |
std::ostream& operator<<(std::ostream& output, |
||
48 |
const builtins::CodeCacheInfo& info) { |
||
49 |
output << "<builtins::CodeCacheInfo id=" << info.id |
||
50 |
<< ", size=" << info.data.size() << ">\n"; |
||
51 |
return output; |
||
52 |
} |
||
53 |
|||
54 |
std::ostream& operator<<(std::ostream& output, |
||
55 |
const std::vector<builtins::CodeCacheInfo>& vec) { |
||
56 |
output << "{\n"; |
||
57 |
for (const auto& info : vec) { |
||
58 |
output << info; |
||
59 |
} |
||
60 |
output << "}\n"; |
||
61 |
return output; |
||
62 |
} |
||
63 |
|||
64 |
std::ostream& operator<<(std::ostream& output, |
||
65 |
const std::vector<uint8_t>& vec) { |
||
66 |
output << "{\n"; |
||
67 |
for (const auto& i : vec) { |
||
68 |
output << i << ","; |
||
69 |
} |
||
70 |
output << "}"; |
||
71 |
return output; |
||
72 |
} |
||
73 |
|||
74 |
18 |
std::ostream& operator<<(std::ostream& output, |
|
75 |
const std::vector<PropInfo>& vec) { |
||
76 |
18 |
output << "{\n"; |
|
77 |
✓✓ | 366 |
for (const auto& info : vec) { |
78 |
348 |
output << " " << info << ",\n"; |
|
79 |
} |
||
80 |
18 |
output << "}"; |
|
81 |
18 |
return output; |
|
82 |
} |
||
83 |
|||
84 |
348 |
std::ostream& operator<<(std::ostream& output, const PropInfo& info) { |
|
85 |
696 |
output << "{ \"" << info.name << "\", " << std::to_string(info.id) << ", " |
|
86 |
696 |
<< std::to_string(info.index) << " }"; |
|
87 |
348 |
return output; |
|
88 |
} |
||
89 |
|||
90 |
6 |
std::ostream& operator<<(std::ostream& output, |
|
91 |
const std::vector<std::string>& vec) { |
||
92 |
6 |
output << "{\n"; |
|
93 |
✓✓ | 708 |
for (const auto& info : vec) { |
94 |
702 |
output << " \"" << info << "\",\n"; |
|
95 |
} |
||
96 |
6 |
output << "}"; |
|
97 |
6 |
return output; |
|
98 |
} |
||
99 |
|||
100 |
6 |
std::ostream& operator<<(std::ostream& output, const RealmSerializeInfo& i) { |
|
101 |
output << "{\n" |
||
102 |
6 |
<< "// -- persistent_values begins --\n" |
|
103 |
6 |
<< i.persistent_values << ",\n" |
|
104 |
<< "// -- persistent_values ends --\n" |
||
105 |
6 |
<< "// -- native_objects begins --\n" |
|
106 |
6 |
<< i.native_objects << ",\n" |
|
107 |
6 |
<< "// -- native_objects ends --\n" |
|
108 |
6 |
<< i.context << ", // context\n" |
|
109 |
6 |
<< "}"; |
|
110 |
6 |
return output; |
|
111 |
} |
||
112 |
|||
113 |
6 |
std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) { |
|
114 |
output << "{\n" |
||
115 |
6 |
<< "// -- builtins begins --\n" |
|
116 |
6 |
<< i.builtins << ",\n" |
|
117 |
<< "// -- builtins ends --\n" |
||
118 |
6 |
<< "// -- async_hooks begins --\n" |
|
119 |
6 |
<< i.async_hooks << ",\n" |
|
120 |
6 |
<< "// -- async_hooks ends --\n" |
|
121 |
6 |
<< i.tick_info << ", // tick_info\n" |
|
122 |
6 |
<< i.immediate_info << ", // immediate_info\n" |
|
123 |
6 |
<< "// -- performance_state begins --\n" |
|
124 |
6 |
<< i.performance_state << ",\n" |
|
125 |
6 |
<< "// -- performance_state ends --\n" |
|
126 |
6 |
<< i.exiting << ", // exiting\n" |
|
127 |
6 |
<< i.stream_base_state << ", // stream_base_state\n" |
|
128 |
6 |
<< i.should_abort_on_uncaught_toggle |
|
129 |
<< ", // should_abort_on_uncaught_toggle\n" |
||
130 |
6 |
<< "// -- principal_realm begins --\n" |
|
131 |
6 |
<< i.principal_realm << ",\n" |
|
132 |
<< "// -- principal_realm ends --\n" |
||
133 |
6 |
<< "}"; |
|
134 |
6 |
return output; |
|
135 |
} |
||
136 |
|||
137 |
class FileIO { |
||
138 |
public: |
||
139 |
4 |
explicit FileIO(FILE* file) |
|
140 |
4 |
: f(file), |
|
141 |
4 |
is_debug(per_process::enabled_debug_list.enabled( |
|
142 |
8 |
DebugCategory::MKSNAPSHOT)) {} |
|
143 |
|||
144 |
template <typename... Args> |
||
145 |
2074 |
void Debug(const char* format, Args&&... args) const { |
|
146 |
1342 |
per_process::Debug( |
|
147 |
DebugCategory::MKSNAPSHOT, format, std::forward<Args>(args)...); |
||
148 |
2074 |
} |
|
149 |
|||
150 |
template <typename T> |
||
151 |
std::string ToStr(const T& arg) const { |
||
152 |
std::stringstream ss; |
||
153 |
ss << arg; |
||
154 |
return ss.str(); |
||
155 |
} |
||
156 |
|||
157 |
template <typename T> |
||
158 |
std::string GetName() const { |
||
159 |
#define TYPE_LIST(V) \ |
||
160 |
V(builtins::CodeCacheInfo) \ |
||
161 |
V(PropInfo) \ |
||
162 |
V(std::string) |
||
163 |
|||
164 |
#define V(TypeName) \ |
||
165 |
if (std::is_same_v<T, TypeName>) { \ |
||
166 |
return #TypeName; \ |
||
167 |
} |
||
168 |
TYPE_LIST(V) |
||
169 |
#undef V |
||
170 |
|||
171 |
std::string name; |
||
172 |
if (std::is_arithmetic_v<T>) { |
||
173 |
if (!std::is_signed_v<T>) { |
||
174 |
name += "u"; |
||
175 |
} |
||
176 |
name += std::is_integral_v<T> ? "int" : "float"; |
||
177 |
name += std::to_string(sizeof(T) * 8); |
||
178 |
name += "_t"; |
||
179 |
} |
||
180 |
return name; |
||
181 |
} |
||
182 |
|||
183 |
FILE* f = nullptr; |
||
184 |
bool is_debug = false; |
||
185 |
}; |
||
186 |
|||
187 |
class FileReader : public FileIO { |
||
188 |
public: |
||
189 |
2 |
explicit FileReader(FILE* file) : FileIO(file) {} |
|
190 |
2 |
~FileReader() {} |
|
191 |
|||
192 |
// Helper for reading numeric types. |
||
193 |
template <typename T> |
||
194 |
1824 |
T Read() { |
|
195 |
static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type"); |
||
196 |
T result; |
||
197 |
1824 |
Read(&result, 1); |
|
198 |
1824 |
return result; |
|
199 |
} |
||
200 |
|||
201 |
// Layout of vectors: |
||
202 |
// [ 4/8 bytes ] count |
||
203 |
// [ ... ] contents (count * size of individual elements) |
||
204 |
template <typename T> |
||
205 |
608 |
std::vector<T> ReadVector() { |
|
206 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
608 |
if (is_debug) { |
207 |
std::string name = GetName<T>(); |
||
208 |
Debug("\nReadVector<%s>()(%d-byte)\n", name.c_str(), sizeof(T)); |
||
209 |
} |
||
210 |
608 |
size_t count = static_cast<size_t>(Read<size_t>()); |
|
211 |
✗✓✗✓ ✗✓✓✓ ✗✓ |
608 |
if (count == 0) { |
212 |
2 |
return std::vector<T>(); |
|
213 |
} |
||
214 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
606 |
if (is_debug) { |
215 |
Debug("Reading %d vector elements...\n", count); |
||
216 |
} |
||
217 |
1212 |
std::vector<T> result = ReadVector<T>(count, std::is_arithmetic<T>{}); |
|
218 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
606 |
if (is_debug) { |
219 |
std::string str = std::is_arithmetic_v<T> ? "" : ToStr(result); |
||
220 |
std::string name = GetName<T>(); |
||
221 |
Debug("ReadVector<%s>() read %s\n", name.c_str(), str.c_str()); |
||
222 |
} |
||
223 |
606 |
return result; |
|
224 |
} |
||
225 |
|||
226 |
950 |
std::string ReadString() { |
|
227 |
950 |
size_t length = Read<size_t>(); |
|
228 |
|||
229 |
✗✓ | 950 |
if (is_debug) { |
230 |
Debug("ReadString(), length=%d: ", length); |
||
231 |
} |
||
232 |
|||
233 |
✗✓ | 950 |
CHECK_GT(length, 0); // There should be no empty strings. |
234 |
1900 |
MallocedBuffer<char> buf(length + 1); |
|
235 |
950 |
size_t r = fread(buf.data, 1, length + 1, f); |
|
236 |
✗✓ | 950 |
CHECK_EQ(r, length + 1); |
237 |
950 |
std::string result(buf.data, length); // This creates a copy of buf.data. |
|
238 |
|||
239 |
✗✓ | 950 |
if (is_debug) { |
240 |
Debug("\"%s\", read %d bytes\n", result.c_str(), r); |
||
241 |
} |
||
242 |
|||
243 |
950 |
read_total += r; |
|
244 |
1900 |
return result; |
|
245 |
} |
||
246 |
|||
247 |
size_t read_total = 0; |
||
248 |
|||
249 |
private: |
||
250 |
// Helper for reading an array of numeric types. |
||
251 |
template <typename T> |
||
252 |
2422 |
void Read(T* out, size_t count) { |
|
253 |
static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type"); |
||
254 |
DCHECK_GT(count, 0); // Should not read contents for vectors of size 0. |
||
255 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
2422 |
if (is_debug) { |
256 |
std::string name = GetName<T>(); |
||
257 |
Debug("Read<%s>()(%d-byte), count=%d: ", name.c_str(), sizeof(T), count); |
||
258 |
} |
||
259 |
|||
260 |
2422 |
size_t r = fread(out, sizeof(T), count, f); |
|
261 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
2422 |
CHECK_EQ(r, count); |
262 |
|||
263 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
2422 |
if (is_debug) { |
264 |
std::string str = |
||
265 |
"{ " + std::to_string(out[0]) + (count > 1 ? ", ... }" : " }"); |
||
266 |
Debug("%s, read %d bytes\n", str.c_str(), r); |
||
267 |
} |
||
268 |
2422 |
read_total += r; |
|
269 |
2422 |
} |
|
270 |
|||
271 |
// Helper for reading numeric vectors. |
||
272 |
template <typename Number> |
||
273 |
596 |
std::vector<Number> ReadVector(size_t count, std::true_type) { |
|
274 |
static_assert(std::is_arithmetic_v<Number>, "Not an arithmetic type"); |
||
275 |
DCHECK_GT(count, 0); // Should not read contents for vectors of size 0. |
||
276 |
596 |
std::vector<Number> result(count); |
|
277 |
596 |
Read(result.data(), count); |
|
278 |
596 |
return result; |
|
279 |
} |
||
280 |
|||
281 |
// Helper for reading non-numeric vectors. |
||
282 |
template <typename T> |
||
283 |
10 |
std::vector<T> ReadVector(size_t count, std::false_type) { |
|
284 |
static_assert(!std::is_arithmetic_v<T>, "Arithmetic type"); |
||
285 |
DCHECK_GT(count, 0); // Should not read contents for vectors of size 0. |
||
286 |
10 |
std::vector<T> result; |
|
287 |
10 |
result.reserve(count); |
|
288 |
10 |
bool original_is_debug = is_debug; |
|
289 |
10 |
is_debug = original_is_debug && !std::is_same_v<T, std::string>; |
|
290 |
✓✓✓✓ ✓✓ |
954 |
for (size_t i = 0; i < count; ++i) { |
291 |
✗✓✗✓ ✗✓ |
944 |
if (is_debug) { |
292 |
Debug("\n[%d] ", i); |
||
293 |
} |
||
294 |
944 |
result.push_back(Read<T>()); |
|
295 |
} |
||
296 |
10 |
is_debug = original_is_debug; |
|
297 |
|||
298 |
10 |
return result; |
|
299 |
} |
||
300 |
}; |
||
301 |
|||
302 |
class FileWriter : public FileIO { |
||
303 |
public: |
||
304 |
2 |
explicit FileWriter(FILE* file) : FileIO(file) {} |
|
305 |
2 |
~FileWriter() {} |
|
306 |
|||
307 |
// Helper for writing numeric types. |
||
308 |
template <typename T> |
||
309 |
1824 |
size_t Write(const T& data) { |
|
310 |
static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type"); |
||
311 |
1824 |
return Write(&data, 1); |
|
312 |
} |
||
313 |
|||
314 |
// Layout of vectors: |
||
315 |
// [ 4/8 bytes ] count |
||
316 |
// [ ... ] contents (count * size of individual elements) |
||
317 |
template <typename T> |
||
318 |
608 |
size_t WriteVector(const std::vector<T>& data) { |
|
319 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
608 |
if (is_debug) { |
320 |
std::string str = std::is_arithmetic_v<T> ? "" : ToStr(data); |
||
321 |
std::string name = GetName<T>(); |
||
322 |
Debug("\nWriteVector<%s>() (%d-byte), count=%d: %s\n", |
||
323 |
name.c_str(), |
||
324 |
sizeof(T), |
||
325 |
data.size(), |
||
326 |
str.c_str()); |
||
327 |
} |
||
328 |
|||
329 |
608 |
size_t written_total = Write<size_t>(data.size()); |
|
330 |
✗✓✗✓ ✗✓✓✓ ✗✓ |
608 |
if (data.size() == 0) { |
331 |
2 |
return written_total; |
|
332 |
} |
||
333 |
606 |
written_total += WriteVector<T>(data, std::is_arithmetic<T>{}); |
|
334 |
|||
335 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
606 |
if (is_debug) { |
336 |
std::string name = GetName<T>(); |
||
337 |
Debug("WriteVector<%s>() wrote %d bytes\n", name.c_str(), written_total); |
||
338 |
} |
||
339 |
|||
340 |
606 |
return written_total; |
|
341 |
} |
||
342 |
|||
343 |
// The layout of a written string: |
||
344 |
// [ 4/8 bytes ] length |
||
345 |
// [ |length| bytes ] contents |
||
346 |
950 |
size_t WriteString(const std::string& data) { |
|
347 |
✗✓ | 950 |
CHECK_GT(data.size(), 0); // No empty strings should be written. |
348 |
950 |
size_t written_total = Write<size_t>(data.size()); |
|
349 |
✗✓ | 950 |
if (is_debug) { |
350 |
std::string str = ToStr(data); |
||
351 |
Debug("WriteString(), length=%d: \"%s\"\n", data.size(), data.c_str()); |
||
352 |
} |
||
353 |
|||
354 |
950 |
size_t r = fwrite(data.c_str(), 1, data.size() + 1, f); |
|
355 |
✗✓ | 950 |
CHECK_EQ(r, data.size() + 1); |
356 |
950 |
written_total += r; |
|
357 |
|||
358 |
✗✓ | 950 |
if (is_debug) { |
359 |
Debug("WriteString() wrote %d bytes\n", written_total); |
||
360 |
} |
||
361 |
|||
362 |
950 |
return written_total; |
|
363 |
} |
||
364 |
|||
365 |
private: |
||
366 |
// Helper for writing an array of numeric types. |
||
367 |
template <typename T> |
||
368 |
2422 |
size_t Write(const T* data, size_t count) { |
|
369 |
DCHECK_GT(count, 0); // Should not write contents for vectors of size 0. |
||
370 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
2422 |
if (is_debug) { |
371 |
std::string str = |
||
372 |
"{ " + std::to_string(data[0]) + (count > 1 ? ", ... }" : " }"); |
||
373 |
std::string name = GetName<T>(); |
||
374 |
Debug("Write<%s>() (%d-byte), count=%d: %s", |
||
375 |
name.c_str(), |
||
376 |
sizeof(T), |
||
377 |
count, |
||
378 |
str.c_str()); |
||
379 |
} |
||
380 |
|||
381 |
2422 |
size_t r = fwrite(data, sizeof(T), count, f); |
|
382 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
2422 |
CHECK_EQ(r, count); |
383 |
|||
384 |
✗✓✗✓ ✗✓✗✓ ✗✓ |
2422 |
if (is_debug) { |
385 |
Debug(", wrote %d bytes\n", r); |
||
386 |
} |
||
387 |
2422 |
return r; |
|
388 |
} |
||
389 |
|||
390 |
// Helper for writing numeric vectors. |
||
391 |
template <typename Number> |
||
392 |
596 |
size_t WriteVector(const std::vector<Number>& data, std::true_type) { |
|
393 |
596 |
return Write(data.data(), data.size()); |
|
394 |
} |
||
395 |
|||
396 |
// Helper for writing non-numeric vectors. |
||
397 |
template <typename T> |
||
398 |
10 |
size_t WriteVector(const std::vector<T>& data, std::false_type) { |
|
399 |
DCHECK_GT(data.size(), |
||
400 |
0); // Should not write contents for vectors of size 0. |
||
401 |
10 |
size_t written_total = 0; |
|
402 |
10 |
bool original_is_debug = is_debug; |
|
403 |
10 |
is_debug = original_is_debug && !std::is_same_v<T, std::string>; |
|
404 |
✓✓✓✓ ✓✓ |
954 |
for (size_t i = 0; i < data.size(); ++i) { |
405 |
✗✓✗✓ ✗✓ |
944 |
if (is_debug) { |
406 |
Debug("\n[%d] ", i); |
||
407 |
} |
||
408 |
944 |
written_total += Write<T>(data[i]); |
|
409 |
} |
||
410 |
10 |
is_debug = original_is_debug; |
|
411 |
|||
412 |
10 |
return written_total; |
|
413 |
} |
||
414 |
}; |
||
415 |
|||
416 |
// Layout of serialized std::string: |
||
417 |
// [ 4/8 bytes ] length |
||
418 |
// [ |length| bytes ] contents |
||
419 |
template <> |
||
420 |
234 |
std::string FileReader::Read() { |
|
421 |
234 |
return ReadString(); |
|
422 |
} |
||
423 |
template <> |
||
424 |
234 |
size_t FileWriter::Write(const std::string& data) { |
|
425 |
234 |
return WriteString(data); |
|
426 |
} |
||
427 |
|||
428 |
// Layout of v8::StartupData |
||
429 |
// [ 4/8 bytes ] raw_size |
||
430 |
// [ |raw_size| bytes ] contents |
||
431 |
template <> |
||
432 |
2 |
v8::StartupData FileReader::Read() { |
|
433 |
2 |
Debug("Read<v8::StartupData>()\n"); |
|
434 |
|||
435 |
2 |
int raw_size = Read<int>(); |
|
436 |
2 |
Debug("size=%d\n", raw_size); |
|
437 |
|||
438 |
✗✓ | 2 |
CHECK_GT(raw_size, 0); // There should be no startup data of size 0. |
439 |
// The data pointer of v8::StartupData would be deleted so it must be new'ed. |
||
440 |
4 |
std::unique_ptr<char> buf = std::unique_ptr<char>(new char[raw_size]); |
|
441 |
2 |
Read<char>(buf.get(), raw_size); |
|
442 |
|||
443 |
4 |
return v8::StartupData{buf.release(), raw_size}; |
|
444 |
} |
||
445 |
|||
446 |
template <> |
||
447 |
2 |
size_t FileWriter::Write(const v8::StartupData& data) { |
|
448 |
2 |
Debug("\nWrite<v8::StartupData>() size=%d\n", data.raw_size); |
|
449 |
|||
450 |
✗✓ | 2 |
CHECK_GT(data.raw_size, 0); // There should be no startup data of size 0. |
451 |
2 |
size_t written_total = Write<int>(data.raw_size); |
|
452 |
2 |
written_total += Write<char>(data.data, static_cast<size_t>(data.raw_size)); |
|
453 |
|||
454 |
2 |
Debug("Write<v8::StartupData>() wrote %d bytes\n\n", written_total); |
|
455 |
2 |
return written_total; |
|
456 |
} |
||
457 |
|||
458 |
// Layout of builtins::CodeCacheInfo |
||
459 |
// [ 4/8 bytes ] length of the module id string |
||
460 |
// [ ... ] |length| bytes of module id |
||
461 |
// [ 4/8 bytes ] length of module code cache |
||
462 |
// [ ... ] |length| bytes of module code cache |
||
463 |
template <> |
||
464 |
594 |
builtins::CodeCacheInfo FileReader::Read() { |
|
465 |
594 |
Debug("Read<builtins::CodeCacheInfo>()\n"); |
|
466 |
|||
467 |
594 |
builtins::CodeCacheInfo result{ReadString(), ReadVector<uint8_t>()}; |
|
468 |
|||
469 |
✗✓ | 594 |
if (is_debug) { |
470 |
std::string str = ToStr(result); |
||
471 |
Debug("Read<builtins::CodeCacheInfo>() %s\n", str.c_str()); |
||
472 |
} |
||
473 |
594 |
return result; |
|
474 |
} |
||
475 |
|||
476 |
template <> |
||
477 |
594 |
size_t FileWriter::Write(const builtins::CodeCacheInfo& data) { |
|
478 |
1188 |
Debug("\nWrite<builtins::CodeCacheInfo>() id = %s" |
|
479 |
", size=%d\n", |
||
480 |
1188 |
data.id.c_str(), |
|
481 |
1188 |
data.data.size()); |
|
482 |
|||
483 |
594 |
size_t written_total = WriteString(data.id); |
|
484 |
594 |
written_total += WriteVector<uint8_t>(data.data); |
|
485 |
|||
486 |
594 |
Debug("Write<builtins::CodeCacheInfo>() wrote %d bytes\n", written_total); |
|
487 |
594 |
return written_total; |
|
488 |
} |
||
489 |
|||
490 |
// Layout of PropInfo |
||
491 |
// [ 4/8 bytes ] length of the data name string |
||
492 |
// [ ... ] |length| bytes of data name |
||
493 |
// [ 4 bytes ] index in the PropInfo vector |
||
494 |
// [ 4/8 bytes ] index in the snapshot blob, can be used with |
||
495 |
// GetDataFromSnapshotOnce(). |
||
496 |
template <> |
||
497 |
116 |
PropInfo FileReader::Read() { |
|
498 |
116 |
Debug("Read<PropInfo>()\n"); |
|
499 |
|||
500 |
116 |
PropInfo result; |
|
501 |
116 |
result.name = ReadString(); |
|
502 |
116 |
result.id = Read<uint32_t>(); |
|
503 |
116 |
result.index = Read<SnapshotIndex>(); |
|
504 |
|||
505 |
✗✓ | 116 |
if (is_debug) { |
506 |
std::string str = ToStr(result); |
||
507 |
Debug("Read<PropInfo>() %s\n", str.c_str()); |
||
508 |
} |
||
509 |
|||
510 |
116 |
return result; |
|
511 |
} |
||
512 |
|||
513 |
template <> |
||
514 |
116 |
size_t FileWriter::Write(const PropInfo& data) { |
|
515 |
✗✓ | 116 |
if (is_debug) { |
516 |
std::string str = ToStr(data); |
||
517 |
Debug("Write<PropInfo>() %s\n", str.c_str()); |
||
518 |
} |
||
519 |
|||
520 |
116 |
size_t written_total = WriteString(data.name); |
|
521 |
116 |
written_total += Write<uint32_t>(data.id); |
|
522 |
116 |
written_total += Write<SnapshotIndex>(data.index); |
|
523 |
|||
524 |
116 |
Debug("Write<PropInfo>() wrote %d bytes\n", written_total); |
|
525 |
116 |
return written_total; |
|
526 |
} |
||
527 |
|||
528 |
// Layout of AsyncHooks::SerializeInfo |
||
529 |
// [ 4/8 bytes ] snapshot index of async_ids_stack |
||
530 |
// [ 4/8 bytes ] snapshot index of fields |
||
531 |
// [ 4/8 bytes ] snapshot index of async_id_fields |
||
532 |
// [ 4/8 bytes ] snapshot index of js_execution_async_resources |
||
533 |
// [ 4/8 bytes ] length of native_execution_async_resources |
||
534 |
// [ ... ] snapshot indices of each element in |
||
535 |
// native_execution_async_resources |
||
536 |
template <> |
||
537 |
2 |
AsyncHooks::SerializeInfo FileReader::Read() { |
|
538 |
2 |
Debug("Read<AsyncHooks::SerializeInfo>()\n"); |
|
539 |
|||
540 |
2 |
AsyncHooks::SerializeInfo result; |
|
541 |
2 |
result.async_ids_stack = Read<AliasedBufferIndex>(); |
|
542 |
2 |
result.fields = Read<AliasedBufferIndex>(); |
|
543 |
2 |
result.async_id_fields = Read<AliasedBufferIndex>(); |
|
544 |
2 |
result.js_execution_async_resources = Read<SnapshotIndex>(); |
|
545 |
2 |
result.native_execution_async_resources = ReadVector<SnapshotIndex>(); |
|
546 |
|||
547 |
✗✓ | 2 |
if (is_debug) { |
548 |
std::string str = ToStr(result); |
||
549 |
Debug("Read<AsyncHooks::SerializeInfo>() %s\n", str.c_str()); |
||
550 |
} |
||
551 |
|||
552 |
2 |
return result; |
|
553 |
} |
||
554 |
template <> |
||
555 |
2 |
size_t FileWriter::Write(const AsyncHooks::SerializeInfo& data) { |
|
556 |
✗✓ | 2 |
if (is_debug) { |
557 |
std::string str = ToStr(data); |
||
558 |
Debug("Write<AsyncHooks::SerializeInfo>() %s\n", str.c_str()); |
||
559 |
} |
||
560 |
|||
561 |
2 |
size_t written_total = Write<AliasedBufferIndex>(data.async_ids_stack); |
|
562 |
2 |
written_total += Write<AliasedBufferIndex>(data.fields); |
|
563 |
2 |
written_total += Write<AliasedBufferIndex>(data.async_id_fields); |
|
564 |
2 |
written_total += Write<SnapshotIndex>(data.js_execution_async_resources); |
|
565 |
2 |
written_total += |
|
566 |
2 |
WriteVector<SnapshotIndex>(data.native_execution_async_resources); |
|
567 |
|||
568 |
2 |
Debug("Write<AsyncHooks::SerializeInfo>() wrote %d bytes\n", written_total); |
|
569 |
2 |
return written_total; |
|
570 |
} |
||
571 |
|||
572 |
// Layout of TickInfo::SerializeInfo |
||
573 |
// [ 4/8 bytes ] snapshot index of fields |
||
574 |
template <> |
||
575 |
2 |
TickInfo::SerializeInfo FileReader::Read() { |
|
576 |
2 |
Debug("Read<TickInfo::SerializeInfo>()\n"); |
|
577 |
|||
578 |
TickInfo::SerializeInfo result; |
||
579 |
2 |
result.fields = Read<AliasedBufferIndex>(); |
|
580 |
|||
581 |
✗✓ | 2 |
if (is_debug) { |
582 |
std::string str = ToStr(result); |
||
583 |
Debug("Read<TickInfo::SerializeInfo>() %s\n", str.c_str()); |
||
584 |
} |
||
585 |
|||
586 |
2 |
return result; |
|
587 |
} |
||
588 |
|||
589 |
template <> |
||
590 |
2 |
size_t FileWriter::Write(const TickInfo::SerializeInfo& data) { |
|
591 |
✗✓ | 2 |
if (is_debug) { |
592 |
std::string str = ToStr(data); |
||
593 |
Debug("Write<TickInfo::SerializeInfo>() %s\n", str.c_str()); |
||
594 |
} |
||
595 |
|||
596 |
2 |
size_t written_total = Write<AliasedBufferIndex>(data.fields); |
|
597 |
|||
598 |
2 |
Debug("Write<TickInfo::SerializeInfo>() wrote %d bytes\n", written_total); |
|
599 |
2 |
return written_total; |
|
600 |
} |
||
601 |
|||
602 |
// Layout of TickInfo::SerializeInfo |
||
603 |
// [ 4/8 bytes ] snapshot index of fields |
||
604 |
template <> |
||
605 |
2 |
ImmediateInfo::SerializeInfo FileReader::Read() { |
|
606 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
607 |
"Read<ImmediateInfo::SerializeInfo>()\n"); |
||
608 |
|||
609 |
ImmediateInfo::SerializeInfo result; |
||
610 |
2 |
result.fields = Read<AliasedBufferIndex>(); |
|
611 |
✗✓ | 2 |
if (is_debug) { |
612 |
std::string str = ToStr(result); |
||
613 |
Debug("Read<ImmediateInfo::SerializeInfo>() %s\n", str.c_str()); |
||
614 |
} |
||
615 |
2 |
return result; |
|
616 |
} |
||
617 |
|||
618 |
template <> |
||
619 |
2 |
size_t FileWriter::Write(const ImmediateInfo::SerializeInfo& data) { |
|
620 |
✗✓ | 2 |
if (is_debug) { |
621 |
std::string str = ToStr(data); |
||
622 |
Debug("Write<ImmeidateInfo::SerializeInfo>() %s\n", str.c_str()); |
||
623 |
} |
||
624 |
|||
625 |
2 |
size_t written_total = Write<AliasedBufferIndex>(data.fields); |
|
626 |
|||
627 |
2 |
Debug("Write<ImmeidateInfo::SerializeInfo>() wrote %d bytes\n", |
|
628 |
written_total); |
||
629 |
2 |
return written_total; |
|
630 |
} |
||
631 |
|||
632 |
// Layout of PerformanceState::SerializeInfo |
||
633 |
// [ 4/8 bytes ] snapshot index of root |
||
634 |
// [ 4/8 bytes ] snapshot index of milestones |
||
635 |
// [ 4/8 bytes ] snapshot index of observers |
||
636 |
template <> |
||
637 |
2 |
performance::PerformanceState::SerializeInfo FileReader::Read() { |
|
638 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
639 |
"Read<PerformanceState::SerializeInfo>()\n"); |
||
640 |
|||
641 |
performance::PerformanceState::SerializeInfo result; |
||
642 |
2 |
result.root = Read<AliasedBufferIndex>(); |
|
643 |
2 |
result.milestones = Read<AliasedBufferIndex>(); |
|
644 |
2 |
result.observers = Read<AliasedBufferIndex>(); |
|
645 |
✗✓ | 2 |
if (is_debug) { |
646 |
std::string str = ToStr(result); |
||
647 |
Debug("Read<PerformanceState::SerializeInfo>() %s\n", str.c_str()); |
||
648 |
} |
||
649 |
2 |
return result; |
|
650 |
} |
||
651 |
|||
652 |
template <> |
||
653 |
2 |
size_t FileWriter::Write( |
|
654 |
const performance::PerformanceState::SerializeInfo& data) { |
||
655 |
✗✓ | 2 |
if (is_debug) { |
656 |
std::string str = ToStr(data); |
||
657 |
Debug("Write<PerformanceState::SerializeInfo>() %s\n", str.c_str()); |
||
658 |
} |
||
659 |
|||
660 |
2 |
size_t written_total = Write<AliasedBufferIndex>(data.root); |
|
661 |
2 |
written_total += Write<AliasedBufferIndex>(data.milestones); |
|
662 |
2 |
written_total += Write<AliasedBufferIndex>(data.observers); |
|
663 |
|||
664 |
2 |
Debug("Write<PerformanceState::SerializeInfo>() wrote %d bytes\n", |
|
665 |
written_total); |
||
666 |
2 |
return written_total; |
|
667 |
} |
||
668 |
|||
669 |
// Layout of IsolateDataSerializeInfo |
||
670 |
// [ 4/8 bytes ] length of primitive_values vector |
||
671 |
// [ ... ] |length| of primitive_values indices |
||
672 |
// [ 4/8 bytes ] length of template_values vector |
||
673 |
// [ ... ] |length| of PropInfo data |
||
674 |
template <> |
||
675 |
2 |
IsolateDataSerializeInfo FileReader::Read() { |
|
676 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
677 |
"Read<IsolateDataSerializeInfo>()\n"); |
||
678 |
|||
679 |
2 |
IsolateDataSerializeInfo result; |
|
680 |
2 |
result.primitive_values = ReadVector<SnapshotIndex>(); |
|
681 |
2 |
result.template_values = ReadVector<PropInfo>(); |
|
682 |
✗✓ | 2 |
if (is_debug) { |
683 |
std::string str = ToStr(result); |
||
684 |
Debug("Read<IsolateDataSerializeInfo>() %s\n", str.c_str()); |
||
685 |
} |
||
686 |
2 |
return result; |
|
687 |
} |
||
688 |
|||
689 |
template <> |
||
690 |
2 |
size_t FileWriter::Write(const IsolateDataSerializeInfo& data) { |
|
691 |
✗✓ | 2 |
if (is_debug) { |
692 |
std::string str = ToStr(data); |
||
693 |
Debug("Write<IsolateDataSerializeInfo>() %s\n", str.c_str()); |
||
694 |
} |
||
695 |
|||
696 |
2 |
size_t written_total = WriteVector<SnapshotIndex>(data.primitive_values); |
|
697 |
2 |
written_total += WriteVector<PropInfo>(data.template_values); |
|
698 |
|||
699 |
2 |
Debug("Write<IsolateDataSerializeInfo>() wrote %d bytes\n", written_total); |
|
700 |
2 |
return written_total; |
|
701 |
} |
||
702 |
|||
703 |
template <> |
||
704 |
2 |
RealmSerializeInfo FileReader::Read() { |
|
705 |
per_process::Debug(DebugCategory::MKSNAPSHOT, "Read<RealmSerializeInfo>()\n"); |
||
706 |
2 |
RealmSerializeInfo result; |
|
707 |
2 |
result.persistent_values = ReadVector<PropInfo>(); |
|
708 |
2 |
result.native_objects = ReadVector<PropInfo>(); |
|
709 |
2 |
result.context = Read<SnapshotIndex>(); |
|
710 |
2 |
return result; |
|
711 |
} |
||
712 |
|||
713 |
template <> |
||
714 |
2 |
size_t FileWriter::Write(const RealmSerializeInfo& data) { |
|
715 |
✗✓ | 2 |
if (is_debug) { |
716 |
std::string str = ToStr(data); |
||
717 |
Debug("\nWrite<RealmSerializeInfo>() %s\n", str.c_str()); |
||
718 |
} |
||
719 |
|||
720 |
// Use += here to ensure order of evaluation. |
||
721 |
2 |
size_t written_total = WriteVector<PropInfo>(data.persistent_values); |
|
722 |
2 |
written_total += WriteVector<PropInfo>(data.native_objects); |
|
723 |
2 |
written_total += Write<SnapshotIndex>(data.context); |
|
724 |
|||
725 |
2 |
Debug("Write<RealmSerializeInfo>() wrote %d bytes\n", written_total); |
|
726 |
2 |
return written_total; |
|
727 |
} |
||
728 |
|||
729 |
template <> |
||
730 |
2 |
EnvSerializeInfo FileReader::Read() { |
|
731 |
per_process::Debug(DebugCategory::MKSNAPSHOT, "Read<EnvSerializeInfo>()\n"); |
||
732 |
2 |
EnvSerializeInfo result; |
|
733 |
2 |
result.builtins = ReadVector<std::string>(); |
|
734 |
2 |
result.async_hooks = Read<AsyncHooks::SerializeInfo>(); |
|
735 |
2 |
result.tick_info = Read<TickInfo::SerializeInfo>(); |
|
736 |
2 |
result.immediate_info = Read<ImmediateInfo::SerializeInfo>(); |
|
737 |
2 |
result.performance_state = |
|
738 |
Read<performance::PerformanceState::SerializeInfo>(); |
||
739 |
2 |
result.exiting = Read<AliasedBufferIndex>(); |
|
740 |
2 |
result.stream_base_state = Read<AliasedBufferIndex>(); |
|
741 |
2 |
result.should_abort_on_uncaught_toggle = Read<AliasedBufferIndex>(); |
|
742 |
2 |
result.principal_realm = Read<RealmSerializeInfo>(); |
|
743 |
2 |
return result; |
|
744 |
} |
||
745 |
|||
746 |
template <> |
||
747 |
2 |
size_t FileWriter::Write(const EnvSerializeInfo& data) { |
|
748 |
✗✓ | 2 |
if (is_debug) { |
749 |
std::string str = ToStr(data); |
||
750 |
Debug("\nWrite<EnvSerializeInfo>() %s\n", str.c_str()); |
||
751 |
} |
||
752 |
|||
753 |
// Use += here to ensure order of evaluation. |
||
754 |
2 |
size_t written_total = WriteVector<std::string>(data.builtins); |
|
755 |
2 |
written_total += Write<AsyncHooks::SerializeInfo>(data.async_hooks); |
|
756 |
2 |
written_total += Write<TickInfo::SerializeInfo>(data.tick_info); |
|
757 |
2 |
written_total += Write<ImmediateInfo::SerializeInfo>(data.immediate_info); |
|
758 |
2 |
written_total += Write<performance::PerformanceState::SerializeInfo>( |
|
759 |
data.performance_state); |
||
760 |
2 |
written_total += Write<AliasedBufferIndex>(data.exiting); |
|
761 |
2 |
written_total += Write<AliasedBufferIndex>(data.stream_base_state); |
|
762 |
2 |
written_total += |
|
763 |
2 |
Write<AliasedBufferIndex>(data.should_abort_on_uncaught_toggle); |
|
764 |
2 |
written_total += Write<RealmSerializeInfo>(data.principal_realm); |
|
765 |
|||
766 |
2 |
Debug("Write<EnvSerializeInfo>() wrote %d bytes\n", written_total); |
|
767 |
2 |
return written_total; |
|
768 |
} |
||
769 |
|||
770 |
// Layout of SnapshotMetadata |
||
771 |
// [ 1 byte ] type of the snapshot |
||
772 |
// [ 4/8 bytes ] length of the node version string |
||
773 |
// [ ... ] |length| bytes of node version |
||
774 |
// [ 4/8 bytes ] length of the node arch string |
||
775 |
// [ ... ] |length| bytes of node arch |
||
776 |
// [ 4/8 bytes ] length of the node platform string |
||
777 |
// [ ... ] |length| bytes of node platform |
||
778 |
// [ 4 bytes ] v8 cache version tag |
||
779 |
template <> |
||
780 |
2 |
SnapshotMetadata FileReader::Read() { |
|
781 |
per_process::Debug(DebugCategory::MKSNAPSHOT, "Read<SnapshotMetadata>()\n"); |
||
782 |
|||
783 |
2 |
SnapshotMetadata result; |
|
784 |
2 |
result.type = static_cast<SnapshotMetadata::Type>(Read<uint8_t>()); |
|
785 |
2 |
result.node_version = ReadString(); |
|
786 |
2 |
result.node_arch = ReadString(); |
|
787 |
2 |
result.node_platform = ReadString(); |
|
788 |
2 |
result.v8_cache_version_tag = Read<uint32_t>(); |
|
789 |
|||
790 |
✗✓ | 2 |
if (is_debug) { |
791 |
std::string str = ToStr(result); |
||
792 |
Debug("Read<SnapshotMetadata>() %s\n", str.c_str()); |
||
793 |
} |
||
794 |
2 |
return result; |
|
795 |
} |
||
796 |
|||
797 |
template <> |
||
798 |
2 |
size_t FileWriter::Write(const SnapshotMetadata& data) { |
|
799 |
✗✓ | 2 |
if (is_debug) { |
800 |
std::string str = ToStr(data); |
||
801 |
Debug("\nWrite<SnapshotMetadata>() %s\n", str.c_str()); |
||
802 |
} |
||
803 |
2 |
size_t written_total = 0; |
|
804 |
// We need the Node.js version, platform and arch to match because |
||
805 |
// Node.js may perform synchronizations that are platform-specific and they |
||
806 |
// can be changed in semver-patches. |
||
807 |
2 |
Debug("Write snapshot type %" PRIu8 "\n", static_cast<uint8_t>(data.type)); |
|
808 |
2 |
written_total += Write<uint8_t>(static_cast<uint8_t>(data.type)); |
|
809 |
2 |
Debug("Write Node.js version %s\n", data.node_version.c_str()); |
|
810 |
2 |
written_total += WriteString(data.node_version); |
|
811 |
2 |
Debug("Write Node.js arch %s\n", data.node_arch); |
|
812 |
2 |
written_total += WriteString(data.node_arch); |
|
813 |
2 |
Debug("Write Node.js platform %s\n", data.node_platform); |
|
814 |
2 |
written_total += WriteString(data.node_platform); |
|
815 |
2 |
Debug("Write V8 cached data version tag %" PRIx32 "\n", |
|
816 |
data.v8_cache_version_tag); |
||
817 |
2 |
written_total += Write<uint32_t>(data.v8_cache_version_tag); |
|
818 |
2 |
return written_total; |
|
819 |
} |
||
820 |
|||
821 |
// Layout of the snapshot blob |
||
822 |
// [ 4 bytes ] kMagic |
||
823 |
// [ 4/8 bytes ] length of Node.js version string |
||
824 |
// [ ... ] contents of Node.js version string |
||
825 |
// [ 4/8 bytes ] length of Node.js arch string |
||
826 |
// [ ... ] contents of Node.js arch string |
||
827 |
// [ ... ] v8_snapshot_blob_data from SnapshotCreator::CreateBlob() |
||
828 |
// [ ... ] isolate_data_info |
||
829 |
// [ ... ] env_info |
||
830 |
// [ ... ] code_cache |
||
831 |
|||
832 |
2 |
void SnapshotData::ToBlob(FILE* out) const { |
|
833 |
4 |
FileWriter w(out); |
|
834 |
2 |
w.Debug("SnapshotData::ToBlob()\n"); |
|
835 |
|||
836 |
2 |
size_t written_total = 0; |
|
837 |
|||
838 |
// Metadata |
||
839 |
2 |
w.Debug("Write magic %" PRIx32 "\n", kMagic); |
|
840 |
2 |
written_total += w.Write<uint32_t>(kMagic); |
|
841 |
2 |
w.Debug("Write metadata\n"); |
|
842 |
2 |
written_total += w.Write<SnapshotMetadata>(metadata); |
|
843 |
|||
844 |
2 |
written_total += w.Write<v8::StartupData>(v8_snapshot_blob_data); |
|
845 |
2 |
w.Debug("Write isolate_data_indices\n"); |
|
846 |
2 |
written_total += w.Write<IsolateDataSerializeInfo>(isolate_data_info); |
|
847 |
2 |
written_total += w.Write<EnvSerializeInfo>(env_info); |
|
848 |
2 |
w.Debug("Write code_cache\n"); |
|
849 |
2 |
written_total += w.WriteVector<builtins::CodeCacheInfo>(code_cache); |
|
850 |
2 |
w.Debug("SnapshotData::ToBlob() Wrote %d bytes\n", written_total); |
|
851 |
2 |
} |
|
852 |
|||
853 |
2 |
bool SnapshotData::FromBlob(SnapshotData* out, FILE* in) { |
|
854 |
4 |
FileReader r(in); |
|
855 |
2 |
r.Debug("SnapshotData::FromBlob()\n"); |
|
856 |
|||
857 |
DCHECK_EQ(out->data_ownership, SnapshotData::DataOwnership::kOwned); |
||
858 |
|||
859 |
// Metadata |
||
860 |
2 |
uint32_t magic = r.Read<uint32_t>(); |
|
861 |
2 |
r.Debug("Read magic %" PRIx32 "\n", magic); |
|
862 |
✗✓ | 2 |
CHECK_EQ(magic, kMagic); |
863 |
2 |
out->metadata = r.Read<SnapshotMetadata>(); |
|
864 |
2 |
r.Debug("Read metadata\n"); |
|
865 |
✗✓ | 2 |
if (!out->Check()) { |
866 |
return false; |
||
867 |
} |
||
868 |
|||
869 |
2 |
out->v8_snapshot_blob_data = r.Read<v8::StartupData>(); |
|
870 |
2 |
r.Debug("Read isolate_data_info\n"); |
|
871 |
2 |
out->isolate_data_info = r.Read<IsolateDataSerializeInfo>(); |
|
872 |
2 |
out->env_info = r.Read<EnvSerializeInfo>(); |
|
873 |
2 |
r.Debug("Read code_cache\n"); |
|
874 |
2 |
out->code_cache = r.ReadVector<builtins::CodeCacheInfo>(); |
|
875 |
|||
876 |
2 |
r.Debug("SnapshotData::FromBlob() read %d bytes\n", r.read_total); |
|
877 |
2 |
return true; |
|
878 |
} |
||
879 |
|||
880 |
5545 |
bool SnapshotData::Check() const { |
|
881 |
✗✓ | 5545 |
if (metadata.node_version != per_process::metadata.versions.node) { |
882 |
fprintf(stderr, |
||
883 |
"Failed to load the startup snapshot because it was built with" |
||
884 |
"Node.js version %s and the current Node.js version is %s.\n", |
||
885 |
metadata.node_version.c_str(), |
||
886 |
NODE_VERSION); |
||
887 |
return false; |
||
888 |
} |
||
889 |
|||
890 |
✗✓ | 5545 |
if (metadata.node_arch != per_process::metadata.arch) { |
891 |
fprintf(stderr, |
||
892 |
"Failed to load the startup snapshot because it was built with" |
||
893 |
"architecture %s and the architecture is %s.\n", |
||
894 |
metadata.node_arch.c_str(), |
||
895 |
NODE_ARCH); |
||
896 |
return false; |
||
897 |
} |
||
898 |
|||
899 |
✗✓ | 5545 |
if (metadata.node_platform != per_process::metadata.platform) { |
900 |
fprintf(stderr, |
||
901 |
"Failed to load the startup snapshot because it was built with" |
||
902 |
"platform %s and the current platform is %s.\n", |
||
903 |
metadata.node_platform.c_str(), |
||
904 |
NODE_PLATFORM); |
||
905 |
return false; |
||
906 |
} |
||
907 |
|||
908 |
5545 |
uint32_t current_cache_version = v8::ScriptCompiler::CachedDataVersionTag(); |
|
909 |
✓✓✗✓ |
5674 |
if (metadata.v8_cache_version_tag != current_cache_version && |
910 |
129 |
metadata.type == SnapshotMetadata::Type::kFullyCustomized) { |
|
911 |
// For now we only do this check for the customized snapshots - we know |
||
912 |
// that the flags we use in the default snapshot are limited and safe |
||
913 |
// enough so we can relax the constraints for it. |
||
914 |
fprintf(stderr, |
||
915 |
"Failed to load the startup snapshot because it was built with " |
||
916 |
"a different version of V8 or with different V8 configurations.\n" |
||
917 |
"Expected tag %" PRIx32 ", read %" PRIx32 "\n", |
||
918 |
current_cache_version, |
||
919 |
metadata.v8_cache_version_tag); |
||
920 |
return false; |
||
921 |
} |
||
922 |
|||
923 |
// TODO(joyeecheung): check incompatible Node.js flags. |
||
924 |
5545 |
return true; |
|
925 |
} |
||
926 |
|||
927 |
11236 |
SnapshotData::~SnapshotData() { |
|
928 |
✓✓✓✓ |
5627 |
if (data_ownership == DataOwnership::kOwned && |
929 |
9 |
v8_snapshot_blob_data.data != nullptr) { |
|
930 |
✓✗ | 8 |
delete[] v8_snapshot_blob_data.data; |
931 |
} |
||
932 |
5618 |
} |
|
933 |
|||
934 |
template <typename T> |
||
935 |
1788 |
void WriteVector(std::ostream* ss, const T* vec, size_t size) { |
|
936 |
✓✓✓✓ |
13311311 |
for (size_t i = 0; i < size; i++) { |
937 |
✓✓✓✓ |
13309523 |
*ss << std::to_string(vec[i]) << (i == size - 1 ? '\n' : ','); |
938 |
} |
||
939 |
1788 |
} |
|
940 |
|||
941 |
3564 |
static std::string GetCodeCacheDefName(const std::string& id) { |
|
942 |
3564 |
char buf[64] = {0}; |
|
943 |
3564 |
size_t size = id.size(); |
|
944 |
✗✓ | 3564 |
CHECK_LT(size, sizeof(buf)); |
945 |
✓✓ | 79860 |
for (size_t i = 0; i < size; ++i) { |
946 |
76296 |
char ch = id[i]; |
|
947 |
✓✓✓✓ |
76296 |
buf[i] = (ch == '-' || ch == '/') ? '_' : ch; |
948 |
} |
||
949 |
3564 |
return std::string(buf) + std::string("_cache_data"); |
|
950 |
} |
||
951 |
|||
952 |
1782 |
static std::string FormatSize(size_t size) { |
|
953 |
1782 |
char buf[64] = {0}; |
|
954 |
✓✓ | 1782 |
if (size < 1024) { |
955 |
162 |
snprintf(buf, sizeof(buf), "%.2fB", static_cast<double>(size)); |
|
956 |
✓✗ | 1620 |
} else if (size < 1024 * 1024) { |
957 |
1620 |
snprintf(buf, sizeof(buf), "%.2fKB", static_cast<double>(size / 1024)); |
|
958 |
} else { |
||
959 |
snprintf( |
||
960 |
buf, sizeof(buf), "%.2fMB", static_cast<double>(size / 1024 / 1024)); |
||
961 |
} |
||
962 |
1782 |
return buf; |
|
963 |
} |
||
964 |
|||
965 |
1782 |
static void WriteStaticCodeCacheData(std::ostream* ss, |
|
966 |
const builtins::CodeCacheInfo& info) { |
||
967 |
1782 |
*ss << "static const uint8_t " << GetCodeCacheDefName(info.id) << "[] = {\n"; |
|
968 |
1782 |
WriteVector(ss, info.data.data(), info.data.size()); |
|
969 |
1782 |
*ss << "};"; |
|
970 |
1782 |
} |
|
971 |
|||
972 |
1782 |
static void WriteCodeCacheInitializer(std::ostream* ss, const std::string& id) { |
|
973 |
3564 |
std::string def_name = GetCodeCacheDefName(id); |
|
974 |
1782 |
*ss << " { \"" << id << "\",\n"; |
|
975 |
1782 |
*ss << " {" << def_name << ",\n"; |
|
976 |
1782 |
*ss << " " << def_name << " + arraysize(" << def_name << "),\n"; |
|
977 |
1782 |
*ss << " }\n"; |
|
978 |
1782 |
*ss << " },\n"; |
|
979 |
1782 |
} |
|
980 |
|||
981 |
6 |
void FormatBlob(std::ostream& ss, SnapshotData* data) { |
|
982 |
6 |
ss << R"(#include <cstddef> |
|
983 |
#include "env.h" |
||
984 |
#include "node_snapshot_builder.h" |
||
985 |
#include "v8.h" |
||
986 |
|||
987 |
// This file is generated by tools/snapshot. Do not edit. |
||
988 |
|||
989 |
namespace node { |
||
990 |
|||
991 |
static const char v8_snapshot_blob_data[] = { |
||
992 |
)"; |
||
993 |
6 |
WriteVector(&ss, |
|
994 |
data->v8_snapshot_blob_data.data, |
||
995 |
6 |
data->v8_snapshot_blob_data.raw_size); |
|
996 |
6 |
ss << R"(}; |
|
997 |
|||
998 |
static const int v8_snapshot_blob_size = )" |
||
999 |
6 |
<< data->v8_snapshot_blob_data.raw_size << ";"; |
|
1000 |
|||
1001 |
// Windows can't deal with too many large vector initializers. |
||
1002 |
// Store the data into static arrays first. |
||
1003 |
✓✓ | 1788 |
for (const auto& item : data->code_cache) { |
1004 |
1782 |
WriteStaticCodeCacheData(&ss, item); |
|
1005 |
} |
||
1006 |
|||
1007 |
6 |
ss << R"(SnapshotData snapshot_data { |
|
1008 |
// -- data_ownership begins -- |
||
1009 |
SnapshotData::DataOwnership::kNotOwned, |
||
1010 |
// -- data_ownership ends -- |
||
1011 |
// -- metadata begins -- |
||
1012 |
6 |
)" << data->metadata |
|
1013 |
6 |
<< R"(, |
|
1014 |
// -- metadata ends -- |
||
1015 |
// -- v8_snapshot_blob_data begins -- |
||
1016 |
{ v8_snapshot_blob_data, v8_snapshot_blob_size }, |
||
1017 |
// -- v8_snapshot_blob_data ends -- |
||
1018 |
// -- isolate_data_indices begins -- |
||
1019 |
6 |
)" << data->isolate_data_info |
|
1020 |
6 |
<< R"( |
|
1021 |
// -- isolate_data_indices ends -- |
||
1022 |
, |
||
1023 |
// -- env_info begins -- |
||
1024 |
6 |
)" << data->env_info |
|
1025 |
6 |
<< R"( |
|
1026 |
// -- env_info ends -- |
||
1027 |
, |
||
1028 |
// -- code_cache begins -- |
||
1029 |
{)"; |
||
1030 |
✓✓ | 1788 |
for (const auto& item : data->code_cache) { |
1031 |
1782 |
WriteCodeCacheInitializer(&ss, item.id); |
|
1032 |
} |
||
1033 |
6 |
ss << R"( |
|
1034 |
} |
||
1035 |
// -- code_cache ends -- |
||
1036 |
}; |
||
1037 |
|||
1038 |
const SnapshotData* SnapshotBuilder::GetEmbeddedSnapshotData() { |
||
1039 |
Mutex::ScopedLock lock(snapshot_data_mutex_); |
||
1040 |
return &snapshot_data; |
||
1041 |
} |
||
1042 |
} // namespace node |
||
1043 |
)"; |
||
1044 |
6 |
} |
|
1045 |
|||
1046 |
// Reset context settings that need to be initialized again after |
||
1047 |
// deserialization. |
||
1048 |
13 |
static void ResetContextSettingsBeforeSnapshot(Local<Context> context) { |
|
1049 |
// Reset the AllowCodeGenerationFromStrings flag to true (default value) so |
||
1050 |
// that it can be re-initialized with v8 flag |
||
1051 |
// --disallow-code-generation-from-strings and recognized in |
||
1052 |
// node::InitializeContextRuntime. |
||
1053 |
13 |
context->AllowCodeGenerationFromStrings(true); |
|
1054 |
13 |
} |
|
1055 |
|||
1056 |
5623 |
Mutex SnapshotBuilder::snapshot_data_mutex_; |
|
1057 |
|||
1058 |
6287 |
const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() { |
|
1059 |
✓✓✓✗ |
6287 |
static auto registry = std::make_unique<ExternalReferenceRegistry>(); |
1060 |
6287 |
return registry->external_references(); |
|
1061 |
} |
||
1062 |
|||
1063 |
6280 |
void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data, |
|
1064 |
Isolate::CreateParams* params) { |
||
1065 |
6280 |
params->external_references = CollectExternalReferences().data(); |
|
1066 |
6280 |
params->snapshot_blob = |
|
1067 |
6280 |
const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data)); |
|
1068 |
6280 |
} |
|
1069 |
|||
1070 |
7 |
ExitCode SnapshotBuilder::Generate(SnapshotData* out, |
|
1071 |
const std::vector<std::string> args, |
||
1072 |
const std::vector<std::string> exec_args) { |
||
1073 |
const std::vector<intptr_t>& external_references = |
||
1074 |
7 |
CollectExternalReferences(); |
|
1075 |
7 |
Isolate* isolate = Isolate::Allocate(); |
|
1076 |
// Must be done before the SnapshotCreator creation so that the |
||
1077 |
// memory reducer can be initialized. |
||
1078 |
14 |
per_process::v8_platform.Platform()->RegisterIsolate(isolate, |
|
1079 |
7 |
uv_default_loop()); |
|
1080 |
|||
1081 |
14 |
SnapshotCreator creator(isolate, external_references.data()); |
|
1082 |
|||
1083 |
7 |
isolate->SetCaptureStackTraceForUncaughtExceptions( |
|
1084 |
true, 10, v8::StackTrace::StackTraceOptions::kDetailed); |
||
1085 |
|||
1086 |
7 |
Environment* env = nullptr; |
|
1087 |
std::unique_ptr<NodeMainInstance> main_instance = |
||
1088 |
NodeMainInstance::Create(isolate, |
||
1089 |
uv_default_loop(), |
||
1090 |
7 |
per_process::v8_platform.Platform(), |
|
1091 |
args, |
||
1092 |
14 |
exec_args); |
|
1093 |
|||
1094 |
// The cleanups should be done in case of an early exit due to errors. |
||
1095 |
7 |
auto cleanup = OnScopeLeave([&]() { |
|
1096 |
// Must be done while the snapshot creator isolate is entered i.e. the |
||
1097 |
// creator is still alive. The snapshot creator destructor will destroy |
||
1098 |
// the isolate. |
||
1099 |
✓✗ | 7 |
if (env != nullptr) { |
1100 |
7 |
FreeEnvironment(env); |
|
1101 |
} |
||
1102 |
7 |
main_instance->Dispose(); |
|
1103 |
7 |
per_process::v8_platform.Platform()->UnregisterIsolate(isolate); |
|
1104 |
21 |
}); |
|
1105 |
|||
1106 |
// It's only possible to be kDefault in node_mksnapshot. |
||
1107 |
SnapshotMetadata::Type snapshot_type = |
||
1108 |
7 |
per_process::cli_options->build_snapshot |
|
1109 |
✓✓ | 7 |
? SnapshotMetadata::Type::kFullyCustomized |
1110 |
7 |
: SnapshotMetadata::Type::kDefault; |
|
1111 |
|||
1112 |
{ |
||
1113 |
13 |
HandleScope scope(isolate); |
|
1114 |
✓✓ | 13 |
TryCatch bootstrapCatch(isolate); |
1115 |
|||
1116 |
7 |
auto print_Exception = OnScopeLeave([&]() { |
|
1117 |
✓✓ | 7 |
if (bootstrapCatch.HasCaught()) { |
1118 |
2 |
PrintCaughtException( |
|
1119 |
2 |
isolate, isolate->GetCurrentContext(), bootstrapCatch); |
|
1120 |
} |
||
1121 |
✓✓ | 20 |
}); |
1122 |
|||
1123 |
// The default context with only things created by V8. |
||
1124 |
14 |
Local<Context> default_context = Context::New(isolate); |
|
1125 |
|||
1126 |
// The context used by the vm module. |
||
1127 |
Local<Context> vm_context; |
||
1128 |
{ |
||
1129 |
Local<ObjectTemplate> global_template = |
||
1130 |
7 |
main_instance->isolate_data()->contextify_global_template(); |
|
1131 |
✗✓ | 7 |
CHECK(!global_template.IsEmpty()); |
1132 |
✗✓ | 14 |
if (!contextify::ContextifyContext::CreateV8Context( |
1133 |
7 |
isolate, global_template, nullptr, nullptr) |
|
1134 |
7 |
.ToLocal(&vm_context)) { |
|
1135 |
return ExitCode::kStartupSnapshotFailure; |
||
1136 |
} |
||
1137 |
} |
||
1138 |
|||
1139 |
// The Node.js-specific context with primodials, can be used by workers |
||
1140 |
// TODO(joyeecheung): investigate if this can be used by vm contexts |
||
1141 |
// without breaking compatibility. |
||
1142 |
7 |
Local<Context> base_context = NewContext(isolate); |
|
1143 |
✗✓ | 7 |
if (base_context.IsEmpty()) { |
1144 |
return ExitCode::kBootstrapFailure; |
||
1145 |
} |
||
1146 |
7 |
ResetContextSettingsBeforeSnapshot(base_context); |
|
1147 |
|||
1148 |
7 |
Local<Context> main_context = NewContext(isolate); |
|
1149 |
✗✓ | 7 |
if (main_context.IsEmpty()) { |
1150 |
return ExitCode::kBootstrapFailure; |
||
1151 |
} |
||
1152 |
// Initialize the main instance context. |
||
1153 |
{ |
||
1154 |
6 |
Context::Scope context_scope(main_context); |
|
1155 |
|||
1156 |
// Create the environment. |
||
1157 |
14 |
env = new Environment(main_instance->isolate_data(), |
|
1158 |
main_context, |
||
1159 |
args, |
||
1160 |
exec_args, |
||
1161 |
nullptr, |
||
1162 |
node::EnvironmentFlags::kDefaultFlags, |
||
1163 |
7 |
{}); |
|
1164 |
|||
1165 |
// Run scripts in lib/internal/bootstrap/ |
||
1166 |
✗✓ | 14 |
if (env->principal_realm()->RunBootstrapping().IsEmpty()) { |
1167 |
return ExitCode::kBootstrapFailure; |
||
1168 |
} |
||
1169 |
// If --build-snapshot is true, lib/internal/main/mksnapshot.js would be |
||
1170 |
// loaded via LoadEnvironment() to execute process.argv[1] as the entry |
||
1171 |
// point (we currently only support this kind of entry point, but we |
||
1172 |
// could also explore snapshotting other kinds of execution modes |
||
1173 |
// in the future). |
||
1174 |
✓✓ | 7 |
if (snapshot_type == SnapshotMetadata::Type::kFullyCustomized) { |
1175 |
#if HAVE_INSPECTOR |
||
1176 |
// TODO(joyeecheung): handle the exit code returned by |
||
1177 |
// InitializeInspector(). |
||
1178 |
1 |
env->InitializeInspector({}); |
|
1179 |
#endif |
||
1180 |
✓✗ | 2 |
if (LoadEnvironment(env, StartExecutionCallback{}).IsEmpty()) { |
1181 |
1 |
return ExitCode::kGenericUserError; |
|
1182 |
} |
||
1183 |
// FIXME(joyeecheung): right now running the loop in the snapshot |
||
1184 |
// builder seems to introduces inconsistencies in JS land that need to |
||
1185 |
// be synchronized again after snapshot restoration. |
||
1186 |
ExitCode exit_code = |
||
1187 |
SpinEventLoopInternal(env).FromMaybe(ExitCode::kGenericUserError); |
||
1188 |
if (exit_code != ExitCode::kNoFailure) { |
||
1189 |
return exit_code; |
||
1190 |
} |
||
1191 |
} |
||
1192 |
|||
1193 |
✗✓ | 6 |
if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) { |
1194 |
env->ForEachRealm([](Realm* realm) { realm->PrintInfoForSnapshot(); }); |
||
1195 |
printf("Environment = %p\n", env); |
||
1196 |
} |
||
1197 |
|||
1198 |
// Serialize the native states |
||
1199 |
6 |
out->isolate_data_info = |
|
1200 |
12 |
main_instance->isolate_data()->Serialize(&creator); |
|
1201 |
6 |
out->env_info = env->Serialize(&creator); |
|
1202 |
|||
1203 |
#ifdef NODE_USE_NODE_CODE_CACHE |
||
1204 |
// Regenerate all the code cache. |
||
1205 |
✗✓ | 6 |
if (!builtins::BuiltinLoader::CompileAllBuiltins(main_context)) { |
1206 |
return ExitCode::kGenericUserError; |
||
1207 |
} |
||
1208 |
6 |
builtins::BuiltinLoader::CopyCodeCache(&(out->code_cache)); |
|
1209 |
✓✓ | 1788 |
for (const auto& item : out->code_cache) { |
1210 |
3564 |
std::string size_str = FormatSize(item.data.size()); |
|
1211 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1212 |
"Generated code cache for %d: %s\n", |
||
1213 |
3564 |
item.id.c_str(), |
|
1214 |
3564 |
size_str.c_str()); |
|
1215 |
} |
||
1216 |
#endif |
||
1217 |
|||
1218 |
6 |
ResetContextSettingsBeforeSnapshot(main_context); |
|
1219 |
} |
||
1220 |
|||
1221 |
// Global handles to the contexts can't be disposed before the |
||
1222 |
// blob is created. So initialize all the contexts before adding them. |
||
1223 |
// TODO(joyeecheung): figure out how to remove this restriction. |
||
1224 |
6 |
creator.SetDefaultContext(default_context); |
|
1225 |
6 |
size_t index = creator.AddContext(vm_context); |
|
1226 |
✗✓ | 6 |
CHECK_EQ(index, SnapshotData::kNodeVMContextIndex); |
1227 |
6 |
index = creator.AddContext(base_context); |
|
1228 |
✗✓ | 6 |
CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex); |
1229 |
6 |
index = creator.AddContext(main_context, |
|
1230 |
{SerializeNodeContextInternalFields, env}); |
||
1231 |
✗✓✓✓ |
6 |
CHECK_EQ(index, SnapshotData::kNodeMainContextIndex); |
1232 |
} |
||
1233 |
|||
1234 |
// Must be out of HandleScope |
||
1235 |
6 |
out->v8_snapshot_blob_data = |
|
1236 |
creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kKeep); |
||
1237 |
|||
1238 |
// We must be able to rehash the blob when we restore it or otherwise |
||
1239 |
// the hash seed would be fixed by V8, introducing a vulnerability. |
||
1240 |
✗✓ | 6 |
if (!out->v8_snapshot_blob_data.CanBeRehashed()) { |
1241 |
return ExitCode::kStartupSnapshotFailure; |
||
1242 |
} |
||
1243 |
|||
1244 |
18 |
out->metadata = SnapshotMetadata{snapshot_type, |
|
1245 |
per_process::metadata.versions.node, |
||
1246 |
per_process::metadata.arch, |
||
1247 |
per_process::metadata.platform, |
||
1248 |
12 |
v8::ScriptCompiler::CachedDataVersionTag()}; |
|
1249 |
|||
1250 |
// We cannot resurrect the handles from the snapshot, so make sure that |
||
1251 |
// no handles are left open in the environment after the blob is created |
||
1252 |
// (which should trigger a GC and close all handles that can be closed). |
||
1253 |
bool queues_are_empty = |
||
1254 |
✓✗✓✗ |
6 |
env->req_wrap_queue()->IsEmpty() && env->handle_wrap_queue()->IsEmpty(); |
1255 |
✓✗✗✓ ✗✓ |
12 |
if (!queues_are_empty || |
1256 |
6 |
per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) { |
|
1257 |
PrintLibuvHandleInformation(env->event_loop(), stderr); |
||
1258 |
} |
||
1259 |
✗✓ | 6 |
if (!queues_are_empty) { |
1260 |
return ExitCode::kStartupSnapshotFailure; |
||
1261 |
} |
||
1262 |
6 |
return ExitCode::kNoFailure; |
|
1263 |
} |
||
1264 |
|||
1265 |
6 |
ExitCode SnapshotBuilder::Generate(std::ostream& out, |
|
1266 |
const std::vector<std::string> args, |
||
1267 |
const std::vector<std::string> exec_args) { |
||
1268 |
12 |
SnapshotData data; |
|
1269 |
6 |
ExitCode exit_code = Generate(&data, args, exec_args); |
|
1270 |
✗✓ | 6 |
if (exit_code != ExitCode::kNoFailure) { |
1271 |
return exit_code; |
||
1272 |
} |
||
1273 |
6 |
FormatBlob(out, &data); |
|
1274 |
6 |
return exit_code; |
|
1275 |
} |
||
1276 |
|||
1277 |
57939 |
SnapshotableObject::SnapshotableObject(Environment* env, |
|
1278 |
Local<Object> wrap, |
||
1279 |
57939 |
EmbedderObjectType type) |
|
1280 |
57939 |
: BaseObject(env, wrap), type_(type) { |
|
1281 |
57939 |
} |
|
1282 |
|||
1283 |
48 |
const char* SnapshotableObject::GetTypeNameChars() const { |
|
1284 |
✓✓✓✓ ✗✗ |
48 |
switch (type_) { |
1285 |
#define V(PropertyName, NativeTypeName) \ |
||
1286 |
case EmbedderObjectType::k_##PropertyName: { \ |
||
1287 |
return NativeTypeName::type_name.c_str(); \ |
||
1288 |
} |
||
1289 |
12 |
SERIALIZABLE_OBJECT_TYPES(V) |
|
1290 |
#undef V |
||
1291 |
default: { UNREACHABLE(); } |
||
1292 |
} |
||
1293 |
} |
||
1294 |
|||
1295 |
22180 |
void DeserializeNodeInternalFields(Local<Object> holder, |
|
1296 |
int index, |
||
1297 |
StartupData payload, |
||
1298 |
void* env) { |
||
1299 |
✗✓ | 22180 |
if (payload.raw_size == 0) { |
1300 |
holder->SetAlignedPointerInInternalField(index, nullptr); |
||
1301 |
return; |
||
1302 |
} |
||
1303 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1304 |
"Deserialize internal field %d of %p, size=%d\n", |
||
1305 |
44360 |
static_cast<int>(index), |
|
1306 |
44360 |
(*holder), |
|
1307 |
44360 |
static_cast<int>(payload.raw_size)); |
|
1308 |
|||
1309 |
✗✓ | 22180 |
if (payload.raw_size == 0) { |
1310 |
holder->SetAlignedPointerInInternalField(index, nullptr); |
||
1311 |
return; |
||
1312 |
} |
||
1313 |
|||
1314 |
DCHECK_EQ(index, BaseObject::kEmbedderType); |
||
1315 |
|||
1316 |
22180 |
Environment* env_ptr = static_cast<Environment*>(env); |
|
1317 |
22180 |
const InternalFieldInfoBase* info = |
|
1318 |
reinterpret_cast<const InternalFieldInfoBase*>(payload.data); |
||
1319 |
// TODO(joyeecheung): we can add a constant kNodeEmbedderId to the |
||
1320 |
// beginning of every InternalFieldInfoBase to ensure that we don't |
||
1321 |
// step on payloads that were not serialized by Node.js. |
||
1322 |
✓✓✓✓ ✗✗ |
22180 |
switch (info->type) { |
1323 |
#define V(PropertyName, NativeTypeName) \ |
||
1324 |
case EmbedderObjectType::k_##PropertyName: { \ |
||
1325 |
per_process::Debug(DebugCategory::MKSNAPSHOT, \ |
||
1326 |
"Object %p is %s\n", \ |
||
1327 |
(*holder), \ |
||
1328 |
NativeTypeName::type_name.c_str()); \ |
||
1329 |
env_ptr->EnqueueDeserializeRequest( \ |
||
1330 |
NativeTypeName::Deserialize, \ |
||
1331 |
holder, \ |
||
1332 |
index, \ |
||
1333 |
info->Copy<NativeTypeName::InternalFieldInfo>()); \ |
||
1334 |
break; \ |
||
1335 |
} |
||
1336 |
49905 |
SERIALIZABLE_OBJECT_TYPES(V) |
|
1337 |
#undef V |
||
1338 |
default: { |
||
1339 |
// This should only be reachable during development when trying to |
||
1340 |
// deserialize a snapshot blob built by a version of Node.js that |
||
1341 |
// has more recognizable EmbedderObjectTypes than the deserializing |
||
1342 |
// Node.js binary. |
||
1343 |
fprintf(stderr, |
||
1344 |
"Unknown embedder object type %" PRIu8 ", possibly caused by " |
||
1345 |
"mismatched Node.js versions\n", |
||
1346 |
static_cast<uint8_t>(info->type)); |
||
1347 |
ABORT(); |
||
1348 |
} |
||
1349 |
} |
||
1350 |
} |
||
1351 |
|||
1352 |
828 |
StartupData SerializeNodeContextInternalFields(Local<Object> holder, |
|
1353 |
int index, |
||
1354 |
void* env) { |
||
1355 |
// We only do one serialization for the kEmbedderType slot, the result |
||
1356 |
// contains everything necessary for deserializing the entire object, |
||
1357 |
// including the fields whose index is bigger than kEmbedderType |
||
1358 |
// (most importantly, BaseObject::kSlot). |
||
1359 |
// For Node.js this design is enough for all the native binding that are |
||
1360 |
// serializable. |
||
1361 |
✓✓ | 828 |
if (index != BaseObject::kEmbedderType) { |
1362 |
414 |
return StartupData{nullptr, 0}; |
|
1363 |
} |
||
1364 |
|||
1365 |
828 |
void* type_ptr = holder->GetAlignedPointerFromInternalField(index); |
|
1366 |
✓✓ | 414 |
if (type_ptr == nullptr) { |
1367 |
390 |
return StartupData{nullptr, 0}; |
|
1368 |
} |
||
1369 |
|||
1370 |
24 |
uint16_t type = *(static_cast<uint16_t*>(type_ptr)); |
|
1371 |
per_process::Debug(DebugCategory::MKSNAPSHOT, "type = 0x%x\n", type); |
||
1372 |
✗✓ | 24 |
if (type != kNodeEmbedderId) { |
1373 |
return StartupData{nullptr, 0}; |
||
1374 |
} |
||
1375 |
|||
1376 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1377 |
"Serialize internal field, index=%d, holder=%p\n", |
||
1378 |
48 |
static_cast<int>(index), |
|
1379 |
48 |
*holder); |
|
1380 |
|||
1381 |
void* native_ptr = |
||
1382 |
48 |
holder->GetAlignedPointerFromInternalField(BaseObject::kSlot); |
|
1383 |
per_process::Debug(DebugCategory::MKSNAPSHOT, "native = %p\n", native_ptr); |
||
1384 |
DCHECK(static_cast<BaseObject*>(native_ptr)->is_snapshotable()); |
||
1385 |
24 |
SnapshotableObject* obj = static_cast<SnapshotableObject*>(native_ptr); |
|
1386 |
|||
1387 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1388 |
"Object %p is %s, ", |
||
1389 |
48 |
*holder, |
|
1390 |
48 |
obj->GetTypeNameChars()); |
|
1391 |
24 |
InternalFieldInfoBase* info = obj->Serialize(index); |
|
1392 |
|||
1393 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1394 |
"payload size=%d\n", |
||
1395 |
48 |
static_cast<int>(info->length)); |
|
1396 |
return StartupData{reinterpret_cast<const char*>(info), |
||
1397 |
24 |
static_cast<int>(info->length)}; |
|
1398 |
} |
||
1399 |
|||
1400 |
6 |
void SerializeSnapshotableObjects(Realm* realm, |
|
1401 |
SnapshotCreator* creator, |
||
1402 |
RealmSerializeInfo* info) { |
||
1403 |
12 |
HandleScope scope(realm->isolate()); |
|
1404 |
6 |
Local<Context> context = realm->context(); |
|
1405 |
6 |
uint32_t i = 0; |
|
1406 |
30 |
realm->ForEachBaseObject([&](BaseObject* obj) { |
|
1407 |
// If there are any BaseObjects that are not snapshotable left |
||
1408 |
// during context serialization, V8 would crash due to unregistered |
||
1409 |
// global handles and print detailed information about them. |
||
1410 |
✗✓ | 24 |
if (!obj->is_snapshotable()) { |
1411 |
return; |
||
1412 |
} |
||
1413 |
24 |
SnapshotableObject* ptr = static_cast<SnapshotableObject*>(obj); |
|
1414 |
|||
1415 |
24 |
const char* type_name = ptr->GetTypeNameChars(); |
|
1416 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1417 |
"Serialize snapshotable object %i (%p), " |
||
1418 |
"object=%p, type=%s\n", |
||
1419 |
96 |
static_cast<int>(i), |
|
1420 |
ptr, |
||
1421 |
72 |
*(ptr->object()), |
|
1422 |
type_name); |
||
1423 |
|||
1424 |
✓✗ | 48 |
if (ptr->PrepareForSerialization(context, creator)) { |
1425 |
48 |
SnapshotIndex index = creator->AddData(context, obj->object()); |
|
1426 |
per_process::Debug(DebugCategory::MKSNAPSHOT, |
||
1427 |
"Serialized with index=%d\n", |
||
1428 |
48 |
static_cast<int>(index)); |
|
1429 |
48 |
info->native_objects.push_back({type_name, i, index}); |
|
1430 |
} |
||
1431 |
24 |
i++; |
|
1432 |
}); |
||
1433 |
6 |
} |
|
1434 |
|||
1435 |
namespace mksnapshot { |
||
1436 |
|||
1437 |
1 |
void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) { |
|
1438 |
✗✓ | 3 |
CHECK(args[0]->IsString()); |
1439 |
2 |
Local<String> filename = args[0].As<String>(); |
|
1440 |
2 |
Local<String> source = args[1].As<String>(); |
|
1441 |
1 |
Isolate* isolate = args.GetIsolate(); |
|
1442 |
1 |
Local<Context> context = isolate->GetCurrentContext(); |
|
1443 |
1 |
ScriptOrigin origin(isolate, filename, 0, 0, true); |
|
1444 |
// TODO(joyeecheung): do we need all of these? Maybe we would want a less |
||
1445 |
// internal version of them. |
||
1446 |
std::vector<Local<String>> parameters = { |
||
1447 |
FIXED_ONE_BYTE_STRING(isolate, "require"), |
||
1448 |
FIXED_ONE_BYTE_STRING(isolate, "__filename"), |
||
1449 |
FIXED_ONE_BYTE_STRING(isolate, "__dirname"), |
||
1450 |
2 |
}; |
|
1451 |
ScriptCompiler::Source script_source(source, origin); |
||
1452 |
Local<Function> fn; |
||
1453 |
✓✗ | 2 |
if (ScriptCompiler::CompileFunction(context, |
1454 |
&script_source, |
||
1455 |
parameters.size(), |
||
1456 |
parameters.data(), |
||
1457 |
0, |
||
1458 |
nullptr, |
||
1459 |
1 |
ScriptCompiler::kEagerCompile) |
|
1460 |
1 |
.ToLocal(&fn)) { |
|
1461 |
2 |
args.GetReturnValue().Set(fn); |
|
1462 |
} |
||
1463 |
1 |
} |
|
1464 |
|||
1465 |
1 |
void SetSerializeCallback(const FunctionCallbackInfo<Value>& args) { |
|
1466 |
1 |
Environment* env = Environment::GetCurrent(args); |
|
1467 |
✗✓ | 2 |
CHECK(env->snapshot_serialize_callback().IsEmpty()); |
1468 |
✗✓ | 2 |
CHECK(args[0]->IsFunction()); |
1469 |
2 |
env->set_snapshot_serialize_callback(args[0].As<Function>()); |
|
1470 |
1 |
} |
|
1471 |
|||
1472 |
1 |
void SetDeserializeCallback(const FunctionCallbackInfo<Value>& args) { |
|
1473 |
1 |
Environment* env = Environment::GetCurrent(args); |
|
1474 |
✗✓ | 2 |
CHECK(env->snapshot_deserialize_callback().IsEmpty()); |
1475 |
✗✓ | 2 |
CHECK(args[0]->IsFunction()); |
1476 |
2 |
env->set_snapshot_deserialize_callback(args[0].As<Function>()); |
|
1477 |
1 |
} |
|
1478 |
|||
1479 |
void SetDeserializeMainFunction(const FunctionCallbackInfo<Value>& args) { |
||
1480 |
Environment* env = Environment::GetCurrent(args); |
||
1481 |
CHECK(env->snapshot_deserialize_main().IsEmpty()); |
||
1482 |
CHECK(args[0]->IsFunction()); |
||
1483 |
env->set_snapshot_deserialize_main(args[0].As<Function>()); |
||
1484 |
} |
||
1485 |
|||
1486 |
786 |
void Initialize(Local<Object> target, |
|
1487 |
Local<Value> unused, |
||
1488 |
Local<Context> context, |
||
1489 |
void* priv) { |
||
1490 |
786 |
SetMethod(context, target, "compileSerializeMain", CompileSerializeMain); |
|
1491 |
786 |
SetMethod(context, target, "setSerializeCallback", SetSerializeCallback); |
|
1492 |
786 |
SetMethod(context, target, "setDeserializeCallback", SetDeserializeCallback); |
|
1493 |
786 |
SetMethod(context, |
|
1494 |
target, |
||
1495 |
"setDeserializeMainFunction", |
||
1496 |
SetDeserializeMainFunction); |
||
1497 |
786 |
} |
|
1498 |
|||
1499 |
5552 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
|
1500 |
5552 |
registry->Register(CompileSerializeMain); |
|
1501 |
5552 |
registry->Register(SetSerializeCallback); |
|
1502 |
5552 |
registry->Register(SetDeserializeCallback); |
|
1503 |
5552 |
registry->Register(SetDeserializeMainFunction); |
|
1504 |
5552 |
} |
|
1505 |
} // namespace mksnapshot |
||
1506 |
} // namespace node |
||
1507 |
|||
1508 |
5623 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(mksnapshot, node::mksnapshot::Initialize) |
|
1509 |
✓✗✓✗ |
22421 |
NODE_MODULE_EXTERNAL_REFERENCE(mksnapshot, |
1510 |
node::mksnapshot::RegisterExternalReferences) |
Generated by: GCOVR (Version 4.2) |