GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_snapshotable.cc Lines: 584 722 80.9 %
Date: 2022-09-02 04:23:42 Branches: 140 242 57.9 %

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
360
  for (const auto& info : vec) {
78
342
    output << "  " << info << ",\n";
79
  }
80
18
  output << "}";
81
18
  return output;
82
}
83
84
342
std::ostream& operator<<(std::ostream& output, const PropInfo& info) {
85
342
  output << "{ \"" << info.name << "\", " << std::to_string(info.id) << ", "
86
684
         << std::to_string(info.index) << " }";
87
342
  return output;
88
}
89
90
6
std::ostream& operator<<(std::ostream& output,
91
                         const std::vector<std::string>& vec) {
92
6
  output << "{\n";
93
702
  for (const auto& info : vec) {
94
696
    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
6
         << "// -- persistent_values ends --\n"
105
6
         << i.context << ",  // context\n"
106
6
         << "}";
107
6
  return output;
108
}
109
110
6
std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) {
111
  output << "{\n"
112
6
         << "// -- native_objects begins --\n"
113
6
         << i.native_objects << ",\n"
114
         << "// -- native_objects ends --\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
4
            DebugCategory::MKSNAPSHOT)) {}
143
144
  template <typename... Args>
145
4068
  void Debug(const char* format, Args&&... args) const {
146
2632
    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
3584
  T Read() {
195
    static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");
196
    T result;
197
3584
    Read(&result, 1);
198
3584
    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
1192
  std::vector<T> ReadVector() {
206
1192
    if (is_debug) {
207
      std::string name = GetName<T>();
208
      Debug("\nReadVector<%s>()(%d-byte)\n", name.c_str(), sizeof(T));
209
    }
210
1192
    size_t count = static_cast<size_t>(Read<size_t>());
211
1192
    if (count == 0) {
212
4
      return std::vector<T>();
213
    }
214
1188
    if (is_debug) {
215
      Debug("Reading %d vector elements...\n", count);
216
    }
217
2376
    std::vector<T> result = ReadVector<T>(count, std::is_arithmetic<T>{});
218
1188
    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
1188
    return result;
224
  }
225
226
934
  std::string ReadString() {
227
934
    size_t length = Read<size_t>();
228
229
934
    if (is_debug) {
230
      Debug("ReadString(), length=%d: ", length);
231
    }
232
233
934
    CHECK_GT(length, 0);  // There should be no empty strings.
234
1868
    MallocedBuffer<char> buf(length + 1);
235
934
    size_t r = fread(buf.data, 1, length + 1, f);
236
934
    CHECK_EQ(r, length + 1);
237
934
    std::string result(buf.data, length);  // This creates a copy of buf.data.
238
239
934
    if (is_debug) {
240
      Debug("\"%s\", read %d bytes\n", result.c_str(), r);
241
    }
242
243
934
    read_total += r;
244
934
    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
4756
  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
4756
    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
4756
    size_t r = fread(out, sizeof(T), count, f);
261
4756
    CHECK_EQ(r, count);
262
263
4756
    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
4756
    read_total += r;
269
4756
  }
270
271
  // Helper for reading numeric vectors.
272
  template <typename Number>
273
1168
  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
1168
    std::vector<Number> result(count);
277
1168
    Read(result.data(), count);
278
1168
    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
1876
    for (size_t i = 0; i < count; ++i) {
291
1856
      if (is_debug) {
292
        Debug("\n[%d] ", i);
293
      }
294
1856
      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
2
  explicit FileWriter(FILE* file) : FileIO(file) {}
305
2
  ~FileWriter() {}
306
307
  // Helper for writing numeric types.
308
  template <typename T>
309
3584
  size_t Write(const T& data) {
310
    static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");
311
3584
    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
1192
  size_t WriteVector(const std::vector<T>& data) {
319
1192
    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
1192
    size_t written_total = Write<size_t>(data.size());
330
1192
    if (data.size() == 0) {
331
4
      return written_total;
332
    }
333
1188
    written_total += WriteVector<T>(data, std::is_arithmetic<T>{});
334
335
1188
    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
1188
    return written_total;
341
  }
342
343
  // The layout of a written string:
344
  // [  4/8 bytes     ] length
345
  // [ |length| bytes ] contents
346
934
  size_t WriteString(const std::string& data) {
347
934
    CHECK_GT(data.size(), 0);  // No empty strings should be written.
348
934
    size_t written_total = Write<size_t>(data.size());
349
934
    if (is_debug) {
350
      std::string str = ToStr(data);
351
      Debug("WriteString(), length=%d: \"%s\"\n", data.size(), data.c_str());
352
    }
353
354
934
    size_t r = fwrite(data.c_str(), 1, data.size() + 1, f);
355
934
    CHECK_EQ(r, data.size() + 1);
356
934
    written_total += r;
357
358
934
    if (is_debug) {
359
      Debug("WriteString() wrote %d bytes\n", written_total);
360
    }
361
362
934
    return written_total;
363
  }
364
365
 private:
366
  // Helper for writing an array of numeric types.
367
  template <typename T>
368
4756
  size_t Write(const T* data, size_t count) {
369
    DCHECK_GT(count, 0);  // Should not write contents for vectors of size 0.
370
4756
    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
4756
    size_t r = fwrite(data, sizeof(T), count, f);
382
4756
    CHECK_EQ(r, count);
383
384
4756
    if (is_debug) {
385
      Debug(", wrote %d bytes\n", r);
386
    }
387
4756
    return r;
388
  }
389
390
  // Helper for writing numeric vectors.
391
  template <typename Number>
392
1168
  size_t WriteVector(const std::vector<Number>& data, std::true_type) {
393
1168
    return Write(data.data(), data.size());
394
  }
395
396
  // Helper for writing non-numeric vectors.
397
  template <typename T>
398
20
  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
20
    size_t written_total = 0;
402
20
    bool original_is_debug = is_debug;
403
20
    is_debug = original_is_debug && !std::is_same_v<T, std::string>;
404
1876
    for (size_t i = 0; i < data.size(); ++i) {
405
1856
      if (is_debug) {
406
        Debug("\n[%d] ", i);
407
      }
408
1856
      written_total += Write<T>(data[i]);
409
    }
410
20
    is_debug = original_is_debug;
411
412
20
    return written_total;
413
  }
414
};
415
416
// Layout of serialized std::string:
417
// [  4/8 bytes     ] length
418
// [ |length| bytes ] contents
419
template <>
420
232
std::string FileReader::Read() {
421
232
  return ReadString();
422
}
423
template <>
424
232
size_t FileWriter::Write(const std::string& data) {
425
232
  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
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
582
builtins::CodeCacheInfo FileReader::Read() {
465
582
  Debug("Read<builtins::CodeCacheInfo>()\n");
466
467
582
  builtins::CodeCacheInfo result{ReadString(), ReadVector<uint8_t>()};
468
469
582
  if (is_debug) {
470
    std::string str = ToStr(result);
471
    Debug("Read<builtins::CodeCacheInfo>() %s\n", str.c_str());
472
  }
473
582
  return result;
474
}
475
476
template <>
477
582
size_t FileWriter::Write(const builtins::CodeCacheInfo& data) {
478
1164
  Debug("\nWrite<builtins::CodeCacheInfo>() id = %s"
479
        ", size=%d\n",
480
582
        data.id.c_str(),
481
582
        data.data.size());
482
483
582
  size_t written_total = WriteString(data.id);
484
582
  written_total += WriteVector<uint8_t>(data.data);
485
486
582
  Debug("Write<builtins::CodeCacheInfo>() wrote %d bytes\n", written_total);
487
582
  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
114
PropInfo FileReader::Read() {
498
114
  Debug("Read<PropInfo>()\n");
499
500
114
  PropInfo result;
501
114
  result.name = ReadString();
502
114
  result.id = Read<uint32_t>();
503
114
  result.index = Read<SnapshotIndex>();
504
505
114
  if (is_debug) {
506
    std::string str = ToStr(result);
507
    Debug("Read<PropInfo>() %s\n", str.c_str());
508
  }
509
510
114
  return result;
511
}
512
513
template <>
514
114
size_t FileWriter::Write(const PropInfo& data) {
515
114
  if (is_debug) {
516
    std::string str = ToStr(data);
517
    Debug("Write<PropInfo>() %s\n", str.c_str());
518
  }
519
520
114
  size_t written_total = WriteString(data.name);
521
114
  written_total += Write<uint32_t>(data.id);
522
114
  written_total += Write<SnapshotIndex>(data.index);
523
524
114
  Debug("Write<PropInfo>() wrote %d bytes\n", written_total);
525
114
  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.context = Read<SnapshotIndex>();
709
2
  return result;
710
}
711
712
template <>
713
2
size_t FileWriter::Write(const RealmSerializeInfo& data) {
714
2
  if (is_debug) {
715
    std::string str = ToStr(data);
716
    Debug("\nWrite<RealmSerializeInfo>() %s\n", str.c_str());
717
  }
718
719
  // Use += here to ensure order of evaluation.
720
2
  size_t written_total = WriteVector<PropInfo>(data.persistent_values);
721
2
  written_total += Write<SnapshotIndex>(data.context);
722
723
2
  Debug("Write<RealmSerializeInfo>() wrote %d bytes\n", written_total);
724
2
  return written_total;
725
}
726
727
template <>
728
2
EnvSerializeInfo FileReader::Read() {
729
  per_process::Debug(DebugCategory::MKSNAPSHOT, "Read<EnvSerializeInfo>()\n");
730
2
  EnvSerializeInfo result;
731
2
  result.native_objects = ReadVector<PropInfo>();
732
2
  result.builtins = ReadVector<std::string>();
733
2
  result.async_hooks = Read<AsyncHooks::SerializeInfo>();
734
2
  result.tick_info = Read<TickInfo::SerializeInfo>();
735
2
  result.immediate_info = Read<ImmediateInfo::SerializeInfo>();
736
  result.performance_state =
737
2
      Read<performance::PerformanceState::SerializeInfo>();
738
2
  result.exiting = Read<AliasedBufferIndex>();
739
2
  result.stream_base_state = Read<AliasedBufferIndex>();
740
2
  result.should_abort_on_uncaught_toggle = Read<AliasedBufferIndex>();
741
2
  result.principal_realm = Read<RealmSerializeInfo>();
742
2
  return result;
743
}
744
745
template <>
746
2
size_t FileWriter::Write(const EnvSerializeInfo& data) {
747
2
  if (is_debug) {
748
    std::string str = ToStr(data);
749
    Debug("\nWrite<EnvSerializeInfo>() %s\n", str.c_str());
750
  }
751
752
  // Use += here to ensure order of evaluation.
753
2
  size_t written_total = WriteVector<PropInfo>(data.native_objects);
754
2
  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
4
  written_total += Write<performance::PerformanceState::SerializeInfo>(
759
2
      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
4
  Debug("Write V8 cached data version tag %" PRIx32 "\n",
816
2
        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
5358
bool SnapshotData::Check() const {
881
5358
  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
5358
  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
5358
  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
5358
  uint32_t current_cache_version = v8::ScriptCompiler::CachedDataVersionTag();
909
5358
  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
5358
  return true;
925
}
926
927
5422
SnapshotData::~SnapshotData() {
928
5422
  if (data_ownership == DataOwnership::kOwned &&
929
9
      v8_snapshot_blob_data.data != nullptr) {
930
8
    delete[] v8_snapshot_blob_data.data;
931
  }
932
5422
}
933
934
template <typename T>
935
3504
void WriteVector(std::ostream* ss, const T* vec, size_t size) {
936
26148646
  for (size_t i = 0; i < size; i++) {
937
26145142
    *ss << std::to_string(vec[i]) << (i == size - 1 ? '\n' : ',');
938
  }
939
3504
}
940
941
3492
static std::string GetCodeCacheDefName(const std::string& id) {
942
3492
  char buf[64] = {0};
943
3492
  size_t size = id.size();
944
3492
  CHECK_LT(size, sizeof(buf));
945
78108
  for (size_t i = 0; i < size; ++i) {
946
74616
    char ch = id[i];
947

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

6089
  static auto registry = std::make_unique<ExternalReferenceRegistry>();
1060
6089
  return registry->external_references();
1061
}
1062
1063
6082
void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
1064
                                              Isolate::CreateParams* params) {
1065
6082
  params->external_references = CollectExternalReferences().data();
1066
6082
  params->snapshot_blob =
1067
6082
      const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data));
1068
6082
}
1069
1070
// TODO(joyeecheung): share these exit code constants across the code base.
1071
constexpr int UNCAUGHT_EXCEPTION_ERROR = 1;
1072
constexpr int BOOTSTRAP_ERROR = 10;
1073
constexpr int SNAPSHOT_ERROR = 14;
1074
1075
7
int SnapshotBuilder::Generate(SnapshotData* out,
1076
                              const std::vector<std::string> args,
1077
                              const std::vector<std::string> exec_args) {
1078
  const std::vector<intptr_t>& external_references =
1079
7
      CollectExternalReferences();
1080
7
  Isolate* isolate = Isolate::Allocate();
1081
  // Must be done before the SnapshotCreator creation so  that the
1082
  // memory reducer can be initialized.
1083
14
  per_process::v8_platform.Platform()->RegisterIsolate(isolate,
1084
7
                                                       uv_default_loop());
1085
1086
14
  SnapshotCreator creator(isolate, external_references.data());
1087
1088
7
  isolate->SetCaptureStackTraceForUncaughtExceptions(
1089
      true, 10, v8::StackTrace::StackTraceOptions::kDetailed);
1090
1091
7
  Environment* env = nullptr;
1092
  std::unique_ptr<NodeMainInstance> main_instance =
1093
      NodeMainInstance::Create(isolate,
1094
                               uv_default_loop(),
1095
7
                               per_process::v8_platform.Platform(),
1096
                               args,
1097
14
                               exec_args);
1098
1099
  // The cleanups should be done in case of an early exit due to errors.
1100
7
  auto cleanup = OnScopeLeave([&]() {
1101
    // Must be done while the snapshot creator isolate is entered i.e. the
1102
    // creator is still alive. The snapshot creator destructor will destroy
1103
    // the isolate.
1104
7
    if (env != nullptr) {
1105
7
      FreeEnvironment(env);
1106
    }
1107
7
    main_instance->Dispose();
1108
7
    per_process::v8_platform.Platform()->UnregisterIsolate(isolate);
1109
14
  });
1110
1111
  // It's only possible to be kDefault in node_mksnapshot.
1112
  SnapshotMetadata::Type snapshot_type =
1113
7
      per_process::cli_options->build_snapshot
1114
7
          ? SnapshotMetadata::Type::kFullyCustomized
1115
7
          : SnapshotMetadata::Type::kDefault;
1116
1117
  {
1118
7
    HandleScope scope(isolate);
1119
7
    TryCatch bootstrapCatch(isolate);
1120
1121
7
    auto print_Exception = OnScopeLeave([&]() {
1122
7
      if (bootstrapCatch.HasCaught()) {
1123
2
        PrintCaughtException(
1124
1
            isolate, isolate->GetCurrentContext(), bootstrapCatch);
1125
      }
1126
7
    });
1127
1128
    // The default context with only things created by V8.
1129
7
    Local<Context> default_context = Context::New(isolate);
1130
1131
    // The context used by the vm module.
1132
    Local<Context> vm_context;
1133
    {
1134
      Local<ObjectTemplate> global_template =
1135
7
          main_instance->isolate_data()->contextify_global_template();
1136
7
      CHECK(!global_template.IsEmpty());
1137
7
      if (!contextify::ContextifyContext::CreateV8Context(
1138
7
               isolate, global_template, nullptr, nullptr)
1139
7
               .ToLocal(&vm_context)) {
1140
        return SNAPSHOT_ERROR;
1141
      }
1142
    }
1143
1144
    // The Node.js-specific context with primodials, can be used by workers
1145
    // TODO(joyeecheung): investigate if this can be used by vm contexts
1146
    // without breaking compatibility.
1147
7
    Local<Context> base_context = NewContext(isolate);
1148
7
    if (base_context.IsEmpty()) {
1149
      return BOOTSTRAP_ERROR;
1150
    }
1151
7
    ResetContextSettingsBeforeSnapshot(base_context);
1152
1153
7
    Local<Context> main_context = NewContext(isolate);
1154
7
    if (main_context.IsEmpty()) {
1155
      return BOOTSTRAP_ERROR;
1156
    }
1157
    // Initialize the main instance context.
1158
    {
1159
7
      Context::Scope context_scope(main_context);
1160
1161
      // Create the environment.
1162
7
      env = new Environment(main_instance->isolate_data(),
1163
                            main_context,
1164
                            args,
1165
                            exec_args,
1166
                            nullptr,
1167
                            node::EnvironmentFlags::kDefaultFlags,
1168
7
                            {});
1169
1170
      // Run scripts in lib/internal/bootstrap/
1171
14
      if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
1172
        return BOOTSTRAP_ERROR;
1173
      }
1174
      // If --build-snapshot is true, lib/internal/main/mksnapshot.js would be
1175
      // loaded via LoadEnvironment() to execute process.argv[1] as the entry
1176
      // point (we currently only support this kind of entry point, but we
1177
      // could also explore snapshotting other kinds of execution modes
1178
      // in the future).
1179
7
      if (snapshot_type == SnapshotMetadata::Type::kFullyCustomized) {
1180
#if HAVE_INSPECTOR
1181
        // TODO(joyeecheung): move this before RunBootstrapping().
1182
1
        env->InitializeInspector({});
1183
#endif
1184
2
        if (LoadEnvironment(env, StartExecutionCallback{}).IsEmpty()) {
1185
1
          return UNCAUGHT_EXCEPTION_ERROR;
1186
        }
1187
        // FIXME(joyeecheung): right now running the loop in the snapshot
1188
        // builder seems to introduces inconsistencies in JS land that need to
1189
        // be synchronized again after snapshot restoration.
1190
        int exit_code = SpinEventLoop(env).FromMaybe(UNCAUGHT_EXCEPTION_ERROR);
1191
        if (exit_code != 0) {
1192
          return exit_code;
1193
        }
1194
      }
1195
1196
6
      if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
1197
        env->PrintAllBaseObjects();
1198
        printf("Environment = %p\n", env);
1199
      }
1200
1201
      // Serialize the native states
1202
      out->isolate_data_info =
1203
6
          main_instance->isolate_data()->Serialize(&creator);
1204
6
      out->env_info = env->Serialize(&creator);
1205
1206
#ifdef NODE_USE_NODE_CODE_CACHE
1207
      // Regenerate all the code cache.
1208
6
      if (!builtins::BuiltinLoader::CompileAllBuiltins(main_context)) {
1209
        return UNCAUGHT_EXCEPTION_ERROR;
1210
      }
1211
6
      builtins::BuiltinLoader::CopyCodeCache(&(out->code_cache));
1212
1752
      for (const auto& item : out->code_cache) {
1213
1746
        std::string size_str = FormatSize(item.data.size());
1214
        per_process::Debug(DebugCategory::MKSNAPSHOT,
1215
                           "Generated code cache for %d: %s\n",
1216
1746
                           item.id.c_str(),
1217
3492
                           size_str.c_str());
1218
      }
1219
#endif
1220
1221
6
      ResetContextSettingsBeforeSnapshot(main_context);
1222
    }
1223
1224
    // Global handles to the contexts can't be disposed before the
1225
    // blob is created. So initialize all the contexts before adding them.
1226
    // TODO(joyeecheung): figure out how to remove this restriction.
1227
6
    creator.SetDefaultContext(default_context);
1228
6
    size_t index = creator.AddContext(vm_context);
1229
6
    CHECK_EQ(index, SnapshotData::kNodeVMContextIndex);
1230
6
    index = creator.AddContext(base_context);
1231
6
    CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex);
1232
6
    index = creator.AddContext(main_context,
1233
                               {SerializeNodeContextInternalFields, env});
1234
6
    CHECK_EQ(index, SnapshotData::kNodeMainContextIndex);
1235
  }
1236
1237
  // Must be out of HandleScope
1238
  out->v8_snapshot_blob_data =
1239
6
      creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kKeep);
1240
1241
  // We must be able to rehash the blob when we restore it or otherwise
1242
  // the hash seed would be fixed by V8, introducing a vulnerability.
1243
6
  if (!out->v8_snapshot_blob_data.CanBeRehashed()) {
1244
    return SNAPSHOT_ERROR;
1245
  }
1246
1247
12
  out->metadata = SnapshotMetadata{snapshot_type,
1248
                                   per_process::metadata.versions.node,
1249
                                   per_process::metadata.arch,
1250
                                   per_process::metadata.platform,
1251
6
                                   v8::ScriptCompiler::CachedDataVersionTag()};
1252
1253
  // We cannot resurrect the handles from the snapshot, so make sure that
1254
  // no handles are left open in the environment after the blob is created
1255
  // (which should trigger a GC and close all handles that can be closed).
1256
  bool queues_are_empty =
1257

6
      env->req_wrap_queue()->IsEmpty() && env->handle_wrap_queue()->IsEmpty();
1258

12
  if (!queues_are_empty ||
1259
6
      per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
1260
    PrintLibuvHandleInformation(env->event_loop(), stderr);
1261
  }
1262
6
  if (!queues_are_empty) {
1263
    return SNAPSHOT_ERROR;
1264
  }
1265
6
  return 0;
1266
}
1267
1268
6
int SnapshotBuilder::Generate(std::ostream& out,
1269
                              const std::vector<std::string> args,
1270
                              const std::vector<std::string> exec_args) {
1271
12
  SnapshotData data;
1272
6
  int exit_code = Generate(&data, args, exec_args);
1273
6
  if (exit_code != 0) {
1274
    return exit_code;
1275
  }
1276
6
  FormatBlob(out, &data);
1277
6
  return exit_code;
1278
}
1279
1280
56372
SnapshotableObject::SnapshotableObject(Environment* env,
1281
                                       Local<Object> wrap,
1282
56372
                                       EmbedderObjectType type)
1283
56372
    : BaseObject(env, wrap), type_(type) {
1284
56372
}
1285
1286
48
const char* SnapshotableObject::GetTypeNameChars() const {
1287

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

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