GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_snapshotable.cc Lines: 588 724 81.2 %
Date: 2022-11-04 03:21:42 Branches: 143 244 58.6 %

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
348
  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
726
  for (const auto& info : vec) {
94
720
    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
5
  explicit FileIO(FILE* file)
140
5
      : f(file),
141
5
        is_debug(per_process::enabled_debug_list.enabled(
142
5
            DebugCategory::MKSNAPSHOT)) {}
143
144
  template <typename... Args>
145
5568
  void Debug(const char* format, Args&&... args) const {
146
4080
    per_process::Debug(
147
        DebugCategory::MKSNAPSHOT, format, std::forward<Args>(args)...);
148
  }
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
3692
  T Read() {
195
    static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");
196
    T result;
197
3692
    Read(&result, 1);
198
3692
    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
1232
  std::vector<T> ReadVector() {
206
1232
    if (is_debug) {
207
      std::string name = GetName<T>();
208
      Debug("\nReadVector<%s>()(%d-byte)\n", name.c_str(), sizeof(T));
209
    }
210
1232
    size_t count = static_cast<size_t>(Read<size_t>());
211
1232
    if (count == 0) {
212
4
      return std::vector<T>();
213
    }
214
1228
    if (is_debug) {
215
      Debug("Reading %d vector elements...\n", count);
216
    }
217
2456
    std::vector<T> result = ReadVector<T>(count, std::is_arithmetic<T>{});
218
1228
    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
1228
    return result;
224
  }
225
226
964
  std::string ReadString() {
227
964
    size_t length = Read<size_t>();
228
229
964
    if (is_debug) {
230
      Debug("ReadString(), length=%d: ", length);
231
    }
232
233
964
    CHECK_GT(length, 0);  // There should be no empty strings.
234
1928
    MallocedBuffer<char> buf(length + 1);
235
964
    size_t r = fread(buf.data, 1, length + 1, f);
236
964
    CHECK_EQ(r, length + 1);
237
964
    std::string result(buf.data, length);  // This creates a copy of buf.data.
238
239
964
    if (is_debug) {
240
      Debug("\"%s\", read %d bytes\n", result.c_str(), r);
241
    }
242
243
964
    read_total += r;
244
964
    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
4904
  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
4904
    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
4904
    size_t r = fread(out, sizeof(T), count, f);
261
4904
    CHECK_EQ(r, count);
262
263
4904
    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
4904
    read_total += r;
269
4904
  }
270
271
  // Helper for reading numeric vectors.
272
  template <typename Number>
273
1208
  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
1208
    std::vector<Number> result(count);
277
1208
    Read(result.data(), count);
278
1208
    return result;
279
  }
280
281
  // Helper for reading non-numeric vectors.
282
  template <typename T>
283
20
  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
20
    std::vector<T> result;
287
20
    result.reserve(count);
288
20
    bool original_is_debug = is_debug;
289
20
    is_debug = original_is_debug && !std::is_same_v<T, std::string>;
290
1936
    for (size_t i = 0; i < count; ++i) {
291
1916
      if (is_debug) {
292
        Debug("\n[%d] ", i);
293
      }
294
1916
      result.push_back(Read<T>());
295
    }
296
20
    is_debug = original_is_debug;
297
298
20
    return result;
299
  }
300
};
301
302
class FileWriter : public FileIO {
303
 public:
304
3
  explicit FileWriter(FILE* file) : FileIO(file) {}
305
3
  ~FileWriter() {}
306
307
  // Helper for writing numeric types.
308
  template <typename T>
309
5596
  size_t Write(const T& data) {
310
    static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");
311
5596
    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
1848
  size_t WriteVector(const std::vector<T>& data) {
319
1848
    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
1848
    size_t written_total = Write<size_t>(data.size());
330
1848
    if (data.size() == 0) {
331
6
      return written_total;
332
    }
333
1842
    written_total += WriteVector<T>(data, std::is_arithmetic<T>{});
334
335
1842
    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
1842
    return written_total;
341
  }
342
343
  // The layout of a written string:
344
  // [  4/8 bytes     ] length
345
  // [ |length| bytes ] contents
346
1463
  size_t WriteString(const std::string& data) {
347
1463
    CHECK_GT(data.size(), 0);  // No empty strings should be written.
348
1463
    size_t written_total = Write<size_t>(data.size());
349
1463
    if (is_debug) {
350
      std::string str = ToStr(data);
351
      Debug("WriteString(), length=%d: \"%s\"\n", data.size(), data.c_str());
352
    }
353
354
1463
    size_t r = fwrite(data.c_str(), 1, data.size() + 1, f);
355
1463
    CHECK_EQ(r, data.size() + 1);
356
1463
    written_total += r;
357
358
1463
    if (is_debug) {
359
      Debug("WriteString() wrote %d bytes\n", written_total);
360
    }
361
362
1463
    return written_total;
363
  }
364
365
 private:
366
  // Helper for writing an array of numeric types.
367
  template <typename T>
368
7414
  size_t Write(const T* data, size_t count) {
369
    DCHECK_GT(count, 0);  // Should not write contents for vectors of size 0.
370
7414
    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
7414
    size_t r = fwrite(data, sizeof(T), count, f);
382
7414
    CHECK_EQ(r, count);
383
384
7414
    if (is_debug) {
385
      Debug(", wrote %d bytes\n", r);
386
    }
387
7414
    return r;
388
  }
389
390
  // Helper for writing numeric vectors.
391
  template <typename Number>
392
1812
  size_t WriteVector(const std::vector<Number>& data, std::true_type) {
393
1812
    return Write(data.data(), data.size());
394
  }
395
396
  // Helper for writing non-numeric vectors.
397
  template <typename T>
398
30
  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
30
    size_t written_total = 0;
402
30
    bool original_is_debug = is_debug;
403
30
    is_debug = original_is_debug && !std::is_same_v<T, std::string>;
404
2938
    for (size_t i = 0; i < data.size(); ++i) {
405
2908
      if (is_debug) {
406
        Debug("\n[%d] ", i);
407
      }
408
2908
      written_total += Write<T>(data[i]);
409
    }
410
30
    is_debug = original_is_debug;
411
412
30
    return written_total;
413
  }
414
};
415
416
// Layout of serialized std::string:
417
// [  4/8 bytes     ] length
418
// [ |length| bytes ] contents
419
template <>
420
240
std::string FileReader::Read() {
421
240
  return ReadString();
422
}
423
template <>
424
371
size_t FileWriter::Write(const std::string& data) {
425
371
  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
2
  std::unique_ptr<char> buf = std::unique_ptr<char>(new char[raw_size]);
441
2
  Read<char>(buf.get(), raw_size);
442
443
2
  return v8::StartupData{buf.release(), raw_size};
444
}
445
446
template <>
447
3
size_t FileWriter::Write(const v8::StartupData& data) {
448
3
  Debug("\nWrite<v8::StartupData>() size=%d\n", data.raw_size);
449
450
3
  CHECK_GT(data.raw_size, 0);  // There should be no startup data of size 0.
451
3
  size_t written_total = Write<int>(data.raw_size);
452
3
  written_total += Write<char>(data.data, static_cast<size_t>(data.raw_size));
453
454
3
  Debug("Write<v8::StartupData>() wrote %d bytes\n\n", written_total);
455
3
  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
602
builtins::CodeCacheInfo FileReader::Read() {
465
602
  Debug("Read<builtins::CodeCacheInfo>()\n");
466
467
602
  builtins::CodeCacheInfo result{ReadString(), ReadVector<uint8_t>()};
468
469
602
  if (is_debug) {
470
    std::string str = ToStr(result);
471
    Debug("Read<builtins::CodeCacheInfo>() %s\n", str.c_str());
472
  }
473
602
  return result;
474
}
475
476
template <>
477
903
size_t FileWriter::Write(const builtins::CodeCacheInfo& data) {
478
1806
  Debug("\nWrite<builtins::CodeCacheInfo>() id = %s"
479
        ", size=%d\n",
480
903
        data.id.c_str(),
481
903
        data.data.size());
482
483
903
  size_t written_total = WriteString(data.id);
484
903
  written_total += WriteVector<uint8_t>(data.data);
485
486
903
  Debug("Write<builtins::CodeCacheInfo>() wrote %d bytes\n", written_total);
487
903
  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
180
size_t FileWriter::Write(const PropInfo& data) {
515
180
  if (is_debug) {
516
    std::string str = ToStr(data);
517
    Debug("Write<PropInfo>() %s\n", str.c_str());
518
  }
519
520
180
  size_t written_total = WriteString(data.name);
521
180
  written_total += Write<uint32_t>(data.id);
522
180
  written_total += Write<SnapshotIndex>(data.index);
523
524
180
  Debug("Write<PropInfo>() wrote %d bytes\n", written_total);
525
180
  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
3
size_t FileWriter::Write(const AsyncHooks::SerializeInfo& data) {
556
3
  if (is_debug) {
557
    std::string str = ToStr(data);
558
    Debug("Write<AsyncHooks::SerializeInfo>() %s\n", str.c_str());
559
  }
560
561
3
  size_t written_total = Write<AliasedBufferIndex>(data.async_ids_stack);
562
3
  written_total += Write<AliasedBufferIndex>(data.fields);
563
3
  written_total += Write<AliasedBufferIndex>(data.async_id_fields);
564
3
  written_total += Write<SnapshotIndex>(data.js_execution_async_resources);
565
3
  written_total +=
566
3
      WriteVector<SnapshotIndex>(data.native_execution_async_resources);
567
568
3
  Debug("Write<AsyncHooks::SerializeInfo>() wrote %d bytes\n", written_total);
569
3
  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
3
size_t FileWriter::Write(const TickInfo::SerializeInfo& data) {
591
3
  if (is_debug) {
592
    std::string str = ToStr(data);
593
    Debug("Write<TickInfo::SerializeInfo>() %s\n", str.c_str());
594
  }
595
596
3
  size_t written_total = Write<AliasedBufferIndex>(data.fields);
597
598
3
  Debug("Write<TickInfo::SerializeInfo>() wrote %d bytes\n", written_total);
599
3
  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
3
size_t FileWriter::Write(const ImmediateInfo::SerializeInfo& data) {
620
3
  if (is_debug) {
621
    std::string str = ToStr(data);
622
    Debug("Write<ImmeidateInfo::SerializeInfo>() %s\n", str.c_str());
623
  }
624
625
3
  size_t written_total = Write<AliasedBufferIndex>(data.fields);
626
627
3
  Debug("Write<ImmeidateInfo::SerializeInfo>() wrote %d bytes\n",
628
        written_total);
629
3
  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
3
size_t FileWriter::Write(
654
    const performance::PerformanceState::SerializeInfo& data) {
655
3
  if (is_debug) {
656
    std::string str = ToStr(data);
657
    Debug("Write<PerformanceState::SerializeInfo>() %s\n", str.c_str());
658
  }
659
660
3
  size_t written_total = Write<AliasedBufferIndex>(data.root);
661
3
  written_total += Write<AliasedBufferIndex>(data.milestones);
662
3
  written_total += Write<AliasedBufferIndex>(data.observers);
663
664
3
  Debug("Write<PerformanceState::SerializeInfo>() wrote %d bytes\n",
665
        written_total);
666
3
  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
3
size_t FileWriter::Write(const IsolateDataSerializeInfo& data) {
691
3
  if (is_debug) {
692
    std::string str = ToStr(data);
693
    Debug("Write<IsolateDataSerializeInfo>() %s\n", str.c_str());
694
  }
695
696
3
  size_t written_total = WriteVector<SnapshotIndex>(data.primitive_values);
697
3
  written_total += WriteVector<PropInfo>(data.template_values);
698
699
3
  Debug("Write<IsolateDataSerializeInfo>() wrote %d bytes\n", written_total);
700
3
  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
3
size_t FileWriter::Write(const RealmSerializeInfo& data) {
715
3
  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
3
  size_t written_total = WriteVector<PropInfo>(data.persistent_values);
722
3
  written_total += WriteVector<PropInfo>(data.native_objects);
723
3
  written_total += Write<SnapshotIndex>(data.context);
724
725
3
  Debug("Write<RealmSerializeInfo>() wrote %d bytes\n", written_total);
726
3
  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
  result.performance_state =
738
2
      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
3
size_t FileWriter::Write(const EnvSerializeInfo& data) {
748
3
  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
3
  size_t written_total = WriteVector<std::string>(data.builtins);
755
3
  written_total += Write<AsyncHooks::SerializeInfo>(data.async_hooks);
756
3
  written_total += Write<TickInfo::SerializeInfo>(data.tick_info);
757
3
  written_total += Write<ImmediateInfo::SerializeInfo>(data.immediate_info);
758
6
  written_total += Write<performance::PerformanceState::SerializeInfo>(
759
3
      data.performance_state);
760
3
  written_total += Write<AliasedBufferIndex>(data.exiting);
761
3
  written_total += Write<AliasedBufferIndex>(data.stream_base_state);
762
3
  written_total +=
763
3
      Write<AliasedBufferIndex>(data.should_abort_on_uncaught_toggle);
764
3
  written_total += Write<RealmSerializeInfo>(data.principal_realm);
765
766
3
  Debug("Write<EnvSerializeInfo>() wrote %d bytes\n", written_total);
767
3
  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
3
size_t FileWriter::Write(const SnapshotMetadata& data) {
799
3
  if (is_debug) {
800
    std::string str = ToStr(data);
801
    Debug("\nWrite<SnapshotMetadata>() %s\n", str.c_str());
802
  }
803
3
  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
3
  Debug("Write snapshot type %" PRIu8 "\n", static_cast<uint8_t>(data.type));
808
3
  written_total += Write<uint8_t>(static_cast<uint8_t>(data.type));
809
3
  Debug("Write Node.js version %s\n", data.node_version.c_str());
810
3
  written_total += WriteString(data.node_version);
811
3
  Debug("Write Node.js arch %s\n", data.node_arch);
812
3
  written_total += WriteString(data.node_arch);
813
3
  Debug("Write Node.js platform %s\n", data.node_platform);
814
3
  written_total += WriteString(data.node_platform);
815
6
  Debug("Write V8 cached data version tag %" PRIx32 "\n",
816
3
        data.v8_cache_version_tag);
817
3
  written_total += Write<uint32_t>(data.v8_cache_version_tag);
818
3
  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
3
void SnapshotData::ToBlob(FILE* out) const {
833
6
  FileWriter w(out);
834
3
  w.Debug("SnapshotData::ToBlob()\n");
835
836
3
  size_t written_total = 0;
837
838
  // Metadata
839
3
  w.Debug("Write magic %" PRIx32 "\n", kMagic);
840
3
  written_total += w.Write<uint32_t>(kMagic);
841
3
  w.Debug("Write metadata\n");
842
3
  written_total += w.Write<SnapshotMetadata>(metadata);
843
844
3
  written_total += w.Write<v8::StartupData>(v8_snapshot_blob_data);
845
3
  w.Debug("Write isolate_data_indices\n");
846
3
  written_total += w.Write<IsolateDataSerializeInfo>(isolate_data_info);
847
3
  written_total += w.Write<EnvSerializeInfo>(env_info);
848
3
  w.Debug("Write code_cache\n");
849
3
  written_total += w.WriteVector<builtins::CodeCacheInfo>(code_cache);
850
3
  w.Debug("SnapshotData::ToBlob() Wrote %d bytes\n", written_total);
851
3
}
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
5578
bool SnapshotData::Check() const {
881
5578
  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
5578
  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
5578
  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
5578
  uint32_t current_cache_version = v8::ScriptCompiler::CachedDataVersionTag();
909
5578
  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
5578
  return true;
925
}
926
927
5653
SnapshotData::~SnapshotData() {
928
5653
  if (data_ownership == DataOwnership::kOwned &&
929
10
      v8_snapshot_blob_data.data != nullptr) {
930
9
    delete[] v8_snapshot_blob_data.data;
931
  }
932
5653
}
933
934
template <typename T>
935
3624
void WriteVector(std::ostream* ss, const T* vec, size_t size) {
936
27149566
  for (size_t i = 0; i < size; i++) {
937
27145942
    *ss << std::to_string(vec[i]) << (i == size - 1 ? '\n' : ',');
938
  }
939
3624
}
940
941
3612
static std::string GetCodeCacheDefName(const std::string& id) {
942
3612
  char buf[64] = {0};
943
3612
  size_t size = id.size();
944
3612
  CHECK_LT(size, sizeof(buf));
945
80796
  for (size_t i = 0; i < size; ++i) {
946
77184
    char ch = id[i];
947

77184
    buf[i] = (ch == '-' || ch == '/') ? '_' : ch;
948
  }
949
3612
  return std::string(buf) + std::string("_cache_data");
950
}
951
952
2107
static std::string FormatSize(size_t size) {
953
2107
  char buf[64] = {0};
954
2107
  if (size < 1024) {
955
196
    snprintf(buf, sizeof(buf), "%.2fB", static_cast<double>(size));
956
1911
  } else if (size < 1024 * 1024) {
957
1911
    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
2107
  return buf;
963
}
964
965
1806
static void WriteStaticCodeCacheData(std::ostream* ss,
966
                                     const builtins::CodeCacheInfo& info) {
967
1806
  *ss << "static const uint8_t " << GetCodeCacheDefName(info.id) << "[] = {\n";
968
1806
  WriteVector(ss, info.data.data(), info.data.size());
969
1806
  *ss << "};";
970
1806
}
971
972
1806
static void WriteCodeCacheInitializer(std::ostream* ss, const std::string& id) {
973
3612
  std::string def_name = GetCodeCacheDefName(id);
974
1806
  *ss << "    { \"" << id << "\",\n";
975
1806
  *ss << "      {" << def_name << ",\n";
976
1806
  *ss << "       " << def_name << " + arraysize(" << def_name << "),\n";
977
1806
  *ss << "      }\n";
978
1806
  *ss << "    },\n";
979
1806
}
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
1812
  for (const auto& item : data->code_cache) {
1004
1806
    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
1812
  for (const auto& item : data->code_cache) {
1031
1806
    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
15
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
15
  context->AllowCodeGenerationFromStrings(true);
1054
15
}
1055
1056
Mutex SnapshotBuilder::snapshot_data_mutex_;
1057
1058
6319
const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() {
1059

6319
  static auto registry = std::make_unique<ExternalReferenceRegistry>();
1060
6319
  return registry->external_references();
1061
}
1062
1063
6311
void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
1064
                                              Isolate::CreateParams* params) {
1065
6311
  params->external_references = CollectExternalReferences().data();
1066
6311
  params->snapshot_blob =
1067
6311
      const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data));
1068
6311
}
1069
1070
8
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
8
      CollectExternalReferences();
1075
8
  Isolate* isolate = Isolate::Allocate();
1076
  // Must be done before the SnapshotCreator creation so  that the
1077
  // memory reducer can be initialized.
1078
16
  per_process::v8_platform.Platform()->RegisterIsolate(isolate,
1079
8
                                                       uv_default_loop());
1080
1081
16
  SnapshotCreator creator(isolate, external_references.data());
1082
1083
8
  isolate->SetCaptureStackTraceForUncaughtExceptions(
1084
      true, 10, v8::StackTrace::StackTraceOptions::kDetailed);
1085
1086
8
  Environment* env = nullptr;
1087
  std::unique_ptr<NodeMainInstance> main_instance =
1088
      NodeMainInstance::Create(isolate,
1089
                               uv_default_loop(),
1090
8
                               per_process::v8_platform.Platform(),
1091
                               args,
1092
16
                               exec_args);
1093
1094
  // The cleanups should be done in case of an early exit due to errors.
1095
8
  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
8
    if (env != nullptr) {
1100
8
      FreeEnvironment(env);
1101
    }
1102
8
    main_instance->Dispose();
1103
8
    per_process::v8_platform.Platform()->UnregisterIsolate(isolate);
1104
16
  });
1105
1106
  // It's only possible to be kDefault in node_mksnapshot.
1107
  SnapshotMetadata::Type snapshot_type =
1108
8
      per_process::cli_options->build_snapshot
1109
8
          ? SnapshotMetadata::Type::kFullyCustomized
1110
8
          : SnapshotMetadata::Type::kDefault;
1111
1112
  {
1113
8
    HandleScope scope(isolate);
1114
8
    TryCatch bootstrapCatch(isolate);
1115
1116
8
    auto print_Exception = OnScopeLeave([&]() {
1117
8
      if (bootstrapCatch.HasCaught()) {
1118
2
        PrintCaughtException(
1119
1
            isolate, isolate->GetCurrentContext(), bootstrapCatch);
1120
      }
1121
8
    });
1122
1123
    // The default context with only things created by V8.
1124
8
    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
8
          main_instance->isolate_data()->contextify_global_template();
1131
8
      CHECK(!global_template.IsEmpty());
1132
8
      if (!contextify::ContextifyContext::CreateV8Context(
1133
8
               isolate, global_template, nullptr, nullptr)
1134
8
               .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
8
    Local<Context> base_context = NewContext(isolate);
1143
8
    if (base_context.IsEmpty()) {
1144
      return ExitCode::kBootstrapFailure;
1145
    }
1146
8
    ResetContextSettingsBeforeSnapshot(base_context);
1147
1148
8
    Local<Context> main_context = NewContext(isolate);
1149
8
    if (main_context.IsEmpty()) {
1150
      return ExitCode::kBootstrapFailure;
1151
    }
1152
    // Initialize the main instance context.
1153
    {
1154
8
      Context::Scope context_scope(main_context);
1155
1156
      // Create the environment.
1157
8
      env = new Environment(main_instance->isolate_data(),
1158
                            main_context,
1159
                            args,
1160
                            exec_args,
1161
                            nullptr,
1162
                            node::EnvironmentFlags::kDefaultFlags,
1163
8
                            {});
1164
1165
      // Run scripts in lib/internal/bootstrap/
1166
16
      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
8
      if (snapshot_type == SnapshotMetadata::Type::kFullyCustomized) {
1175
#if HAVE_INSPECTOR
1176
2
        env->InitializeInspector({});
1177
#endif
1178
4
        if (LoadEnvironment(env, StartExecutionCallback{}).IsEmpty()) {
1179
1
          return ExitCode::kGenericUserError;
1180
        }
1181
        // FIXME(joyeecheung): right now running the loop in the snapshot
1182
        // builder seems to introduces inconsistencies in JS land that need to
1183
        // be synchronized again after snapshot restoration.
1184
        ExitCode exit_code =
1185
1
            SpinEventLoopInternal(env).FromMaybe(ExitCode::kGenericUserError);
1186
1
        if (exit_code != ExitCode::kNoFailure) {
1187
          return exit_code;
1188
        }
1189
      }
1190
1191
7
      if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
1192
        env->ForEachRealm([](Realm* realm) { realm->PrintInfoForSnapshot(); });
1193
        printf("Environment = %p\n", env);
1194
      }
1195
1196
      // Serialize the native states
1197
      out->isolate_data_info =
1198
7
          main_instance->isolate_data()->Serialize(&creator);
1199
7
      out->env_info = env->Serialize(&creator);
1200
1201
#ifdef NODE_USE_NODE_CODE_CACHE
1202
      // Regenerate all the code cache.
1203
7
      if (!builtins::BuiltinLoader::CompileAllBuiltins(main_context)) {
1204
        return ExitCode::kGenericUserError;
1205
      }
1206
7
      builtins::BuiltinLoader::CopyCodeCache(&(out->code_cache));
1207
2114
      for (const auto& item : out->code_cache) {
1208
2107
        std::string size_str = FormatSize(item.data.size());
1209
        per_process::Debug(DebugCategory::MKSNAPSHOT,
1210
                           "Generated code cache for %d: %s\n",
1211
2107
                           item.id.c_str(),
1212
4214
                           size_str.c_str());
1213
      }
1214
#endif
1215
1216
7
      ResetContextSettingsBeforeSnapshot(main_context);
1217
    }
1218
1219
    // Global handles to the contexts can't be disposed before the
1220
    // blob is created. So initialize all the contexts before adding them.
1221
    // TODO(joyeecheung): figure out how to remove this restriction.
1222
7
    creator.SetDefaultContext(default_context);
1223
7
    size_t index = creator.AddContext(vm_context);
1224
7
    CHECK_EQ(index, SnapshotData::kNodeVMContextIndex);
1225
7
    index = creator.AddContext(base_context);
1226
7
    CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex);
1227
7
    index = creator.AddContext(main_context,
1228
                               {SerializeNodeContextInternalFields, env});
1229
7
    CHECK_EQ(index, SnapshotData::kNodeMainContextIndex);
1230
  }
1231
1232
  // Must be out of HandleScope
1233
  out->v8_snapshot_blob_data =
1234
7
      creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kKeep);
1235
1236
  // We must be able to rehash the blob when we restore it or otherwise
1237
  // the hash seed would be fixed by V8, introducing a vulnerability.
1238
7
  if (!out->v8_snapshot_blob_data.CanBeRehashed()) {
1239
    return ExitCode::kStartupSnapshotFailure;
1240
  }
1241
1242
14
  out->metadata = SnapshotMetadata{snapshot_type,
1243
                                   per_process::metadata.versions.node,
1244
                                   per_process::metadata.arch,
1245
                                   per_process::metadata.platform,
1246
7
                                   v8::ScriptCompiler::CachedDataVersionTag()};
1247
1248
  // We cannot resurrect the handles from the snapshot, so make sure that
1249
  // no handles are left open in the environment after the blob is created
1250
  // (which should trigger a GC and close all handles that can be closed).
1251
  bool queues_are_empty =
1252

7
      env->req_wrap_queue()->IsEmpty() && env->handle_wrap_queue()->IsEmpty();
1253

14
  if (!queues_are_empty ||
1254
7
      per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
1255
    PrintLibuvHandleInformation(env->event_loop(), stderr);
1256
  }
1257
7
  if (!queues_are_empty) {
1258
    return ExitCode::kStartupSnapshotFailure;
1259
  }
1260
7
  return ExitCode::kNoFailure;
1261
}
1262
1263
6
ExitCode SnapshotBuilder::Generate(std::ostream& out,
1264
                                   const std::vector<std::string> args,
1265
                                   const std::vector<std::string> exec_args) {
1266
12
  SnapshotData data;
1267
6
  ExitCode exit_code = Generate(&data, args, exec_args);
1268
6
  if (exit_code != ExitCode::kNoFailure) {
1269
    return exit_code;
1270
  }
1271
6
  FormatBlob(out, &data);
1272
6
  return exit_code;
1273
}
1274
1275
58224
SnapshotableObject::SnapshotableObject(Environment* env,
1276
                                       Local<Object> wrap,
1277
58224
                                       EmbedderObjectType type)
1278
58224
    : BaseObject(env, wrap), type_(type) {
1279
58224
}
1280
1281
56
const char* SnapshotableObject::GetTypeNameChars() const {
1282

56
  switch (type_) {
1283
#define V(PropertyName, NativeTypeName)                                        \
1284
  case EmbedderObjectType::k_##PropertyName: {                                 \
1285
    return NativeTypeName::type_name.c_str();                                  \
1286
  }
1287
56
    SERIALIZABLE_OBJECT_TYPES(V)
1288
#undef V
1289
    default: { UNREACHABLE(); }
1290
  }
1291
}
1292
1293
22312
void DeserializeNodeInternalFields(Local<Object> holder,
1294
                                   int index,
1295
                                   StartupData payload,
1296
                                   void* env) {
1297
22312
  if (payload.raw_size == 0) {
1298
    holder->SetAlignedPointerInInternalField(index, nullptr);
1299
    return;
1300
  }
1301
  per_process::Debug(DebugCategory::MKSNAPSHOT,
1302
                     "Deserialize internal field %d of %p, size=%d\n",
1303
44624
                     static_cast<int>(index),
1304
22312
                     (*holder),
1305
22312
                     static_cast<int>(payload.raw_size));
1306
1307
22312
  if (payload.raw_size == 0) {
1308
    holder->SetAlignedPointerInInternalField(index, nullptr);
1309
    return;
1310
  }
1311
1312
  DCHECK_EQ(index, BaseObject::kEmbedderType);
1313
1314
22312
  Environment* env_ptr = static_cast<Environment*>(env);
1315
22312
  const InternalFieldInfoBase* info =
1316
      reinterpret_cast<const InternalFieldInfoBase*>(payload.data);
1317
  // TODO(joyeecheung): we can add a constant kNodeEmbedderId to the
1318
  // beginning of every InternalFieldInfoBase to ensure that we don't
1319
  // step on payloads that were not serialized by Node.js.
1320

22312
  switch (info->type) {
1321
#define V(PropertyName, NativeTypeName)                                        \
1322
  case EmbedderObjectType::k_##PropertyName: {                                 \
1323
    per_process::Debug(DebugCategory::MKSNAPSHOT,                              \
1324
                       "Object %p is %s\n",                                    \
1325
                       (*holder),                                              \
1326
                       NativeTypeName::type_name.c_str());                     \
1327
    env_ptr->EnqueueDeserializeRequest(                                        \
1328
        NativeTypeName::Deserialize,                                           \
1329
        holder,                                                                \
1330
        index,                                                                 \
1331
        info->Copy<NativeTypeName::InternalFieldInfo>());                      \
1332
    break;                                                                     \
1333
  }
1334
44624
    SERIALIZABLE_OBJECT_TYPES(V)
1335
#undef V
1336
    default: {
1337
      // This should only be reachable during development when trying to
1338
      // deserialize a snapshot blob built by a version of Node.js that
1339
      // has more recognizable EmbedderObjectTypes than the deserializing
1340
      // Node.js binary.
1341
      fprintf(stderr,
1342
              "Unknown embedder object type %" PRIu8 ", possibly caused by "
1343
              "mismatched Node.js versions\n",
1344
              static_cast<uint8_t>(info->type));
1345
      ABORT();
1346
    }
1347
  }
1348
}
1349
1350
966
StartupData SerializeNodeContextInternalFields(Local<Object> holder,
1351
                                               int index,
1352
                                               void* env) {
1353
  // We only do one serialization for the kEmbedderType slot, the result
1354
  // contains everything necessary for deserializing the entire object,
1355
  // including the fields whose index is bigger than kEmbedderType
1356
  // (most importantly, BaseObject::kSlot).
1357
  // For Node.js this design is enough for all the native binding that are
1358
  // serializable.
1359
966
  if (index != BaseObject::kEmbedderType) {
1360
483
    return StartupData{nullptr, 0};
1361
  }
1362
1363
483
  void* type_ptr = holder->GetAlignedPointerFromInternalField(index);
1364
483
  if (type_ptr == nullptr) {
1365
455
    return StartupData{nullptr, 0};
1366
  }
1367
1368
28
  uint16_t type = *(static_cast<uint16_t*>(type_ptr));
1369
  per_process::Debug(DebugCategory::MKSNAPSHOT, "type = 0x%x\n", type);
1370
28
  if (type != kNodeEmbedderId) {
1371
    return StartupData{nullptr, 0};
1372
  }
1373
1374
  per_process::Debug(DebugCategory::MKSNAPSHOT,
1375
                     "Serialize internal field, index=%d, holder=%p\n",
1376
56
                     static_cast<int>(index),
1377
56
                     *holder);
1378
1379
  void* native_ptr =
1380
56
      holder->GetAlignedPointerFromInternalField(BaseObject::kSlot);
1381
  per_process::Debug(DebugCategory::MKSNAPSHOT, "native = %p\n", native_ptr);
1382
  DCHECK(static_cast<BaseObject*>(native_ptr)->is_snapshotable());
1383
28
  SnapshotableObject* obj = static_cast<SnapshotableObject*>(native_ptr);
1384
1385
  per_process::Debug(DebugCategory::MKSNAPSHOT,
1386
                     "Object %p is %s, ",
1387
28
                     *holder,
1388
28
                     obj->GetTypeNameChars());
1389
28
  InternalFieldInfoBase* info = obj->Serialize(index);
1390
1391
  per_process::Debug(DebugCategory::MKSNAPSHOT,
1392
                     "payload size=%d\n",
1393
28
                     static_cast<int>(info->length));
1394
  return StartupData{reinterpret_cast<const char*>(info),
1395
28
                     static_cast<int>(info->length)};
1396
}
1397
1398
7
void SerializeSnapshotableObjects(Realm* realm,
1399
                                  SnapshotCreator* creator,
1400
                                  RealmSerializeInfo* info) {
1401
7
  HandleScope scope(realm->isolate());
1402
7
  Local<Context> context = realm->context();
1403
7
  uint32_t i = 0;
1404
7
  realm->ForEachBaseObject([&](BaseObject* obj) {
1405
    // If there are any BaseObjects that are not snapshotable left
1406
    // during context serialization, V8 would crash due to unregistered
1407
    // global handles and print detailed information about them.
1408
28
    if (!obj->is_snapshotable()) {
1409
      return;
1410
    }
1411
28
    SnapshotableObject* ptr = static_cast<SnapshotableObject*>(obj);
1412
1413
28
    const char* type_name = ptr->GetTypeNameChars();
1414
    per_process::Debug(DebugCategory::MKSNAPSHOT,
1415
                       "Serialize snapshotable object %i (%p), "
1416
                       "object=%p, type=%s\n",
1417
112
                       static_cast<int>(i),
1418
                       ptr,
1419
56
                       *(ptr->object()),
1420
                       type_name);
1421
1422
56
    if (ptr->PrepareForSerialization(context, creator)) {
1423
28
      SnapshotIndex index = creator->AddData(context, obj->object());
1424
      per_process::Debug(DebugCategory::MKSNAPSHOT,
1425
                         "Serialized with index=%d\n",
1426
28
                         static_cast<int>(index));
1427
28
      info->native_objects.push_back({type_name, i, index});
1428
    }
1429
28
    i++;
1430
  });
1431
7
}
1432
1433
namespace mksnapshot {
1434
1435
2
void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) {
1436
4
  CHECK(args[0]->IsString());
1437
4
  Local<String> filename = args[0].As<String>();
1438
4
  Local<String> source = args[1].As<String>();
1439
2
  Isolate* isolate = args.GetIsolate();
1440
2
  Local<Context> context = isolate->GetCurrentContext();
1441
2
  ScriptOrigin origin(isolate, filename, 0, 0, true);
1442
  // TODO(joyeecheung): do we need all of these? Maybe we would want a less
1443
  // internal version of them.
1444
  std::vector<Local<String>> parameters = {
1445
2
      FIXED_ONE_BYTE_STRING(isolate, "require"),
1446
2
      FIXED_ONE_BYTE_STRING(isolate, "__filename"),
1447
2
      FIXED_ONE_BYTE_STRING(isolate, "__dirname"),
1448
6
  };
1449
2
  ScriptCompiler::Source script_source(source, origin);
1450
  Local<Function> fn;
1451
2
  if (ScriptCompiler::CompileFunction(context,
1452
                                      &script_source,
1453
                                      parameters.size(),
1454
                                      parameters.data(),
1455
                                      0,
1456
                                      nullptr,
1457
2
                                      ScriptCompiler::kEagerCompile)
1458
2
          .ToLocal(&fn)) {
1459
4
    args.GetReturnValue().Set(fn);
1460
  }
1461
2
}
1462
1463
2
void SetSerializeCallback(const FunctionCallbackInfo<Value>& args) {
1464
2
  Environment* env = Environment::GetCurrent(args);
1465
4
  CHECK(env->snapshot_serialize_callback().IsEmpty());
1466
2
  CHECK(args[0]->IsFunction());
1467
4
  env->set_snapshot_serialize_callback(args[0].As<Function>());
1468
2
}
1469
1470
2
void SetDeserializeCallback(const FunctionCallbackInfo<Value>& args) {
1471
2
  Environment* env = Environment::GetCurrent(args);
1472
4
  CHECK(env->snapshot_deserialize_callback().IsEmpty());
1473
2
  CHECK(args[0]->IsFunction());
1474
4
  env->set_snapshot_deserialize_callback(args[0].As<Function>());
1475
2
}
1476
1477
void SetDeserializeMainFunction(const FunctionCallbackInfo<Value>& args) {
1478
  Environment* env = Environment::GetCurrent(args);
1479
  CHECK(env->snapshot_deserialize_main().IsEmpty());
1480
  CHECK(args[0]->IsFunction());
1481
  env->set_snapshot_deserialize_main(args[0].As<Function>());
1482
}
1483
1484
784
void Initialize(Local<Object> target,
1485
                Local<Value> unused,
1486
                Local<Context> context,
1487
                void* priv) {
1488
784
  SetMethod(context, target, "compileSerializeMain", CompileSerializeMain);
1489
784
  SetMethod(context, target, "setSerializeCallback", SetSerializeCallback);
1490
784
  SetMethod(context, target, "setDeserializeCallback", SetDeserializeCallback);
1491
784
  SetMethod(context,
1492
            target,
1493
            "setDeserializeMainFunction",
1494
            SetDeserializeMainFunction);
1495
784
}
1496
1497
5586
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
1498
5586
  registry->Register(CompileSerializeMain);
1499
5586
  registry->Register(SetSerializeCallback);
1500
5586
  registry->Register(SetDeserializeCallback);
1501
5586
  registry->Register(SetDeserializeMainFunction);
1502
5586
}
1503
}  // namespace mksnapshot
1504
}  // namespace node
1505
1506
5657
NODE_MODULE_CONTEXT_AWARE_INTERNAL(mksnapshot, node::mksnapshot::Initialize)
1507
5586
NODE_MODULE_EXTERNAL_REFERENCE(mksnapshot,
1508
                               node::mksnapshot::RegisterExternalReferences)