Skip to content

Commit

Permalink
bootstrap: handle snapshot errors gracefully
Browse files Browse the repository at this point in the history
This patch refactors the SnapshotBuilder::Generate() routines
so that when running into errors during the snapshot building
process, they can exit gracefully by printing the error
and return a non-zero exit code. If the error is likely to
be caused by internal scripts, the return code would be 12,
if the error is caused by user scripts the return code would
be 1. In addition this refactors the generation of embedded
snapshots and directly writes to the output file stream
instead of producing an intermediate string with string
streams.
  • Loading branch information
joyeecheung committed Jul 13, 2022
1 parent 8542a77 commit e9765f3
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 173 deletions.
21 changes: 18 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -991,13 +991,17 @@ struct EnvSerializeInfo {
};

struct SnapshotData {
// The result of v8::SnapshotCreator::CreateBlob() during the snapshot
// building process.
v8::StartupData v8_snapshot_blob_data;
enum class DataOwnership { kOwned, kNotOwned };

static const size_t kNodeBaseContextIndex = 0;
static const size_t kNodeMainContextIndex = kNodeBaseContextIndex + 1;

DataOwnership data_ownership;

// The result of v8::SnapshotCreator::CreateBlob() during the snapshot
// building process.
v8::StartupData v8_snapshot_blob_data;

std::vector<size_t> isolate_data_indices;
// TODO(joyeecheung): there should be a vector of env_info once we snapshot
// the worker environments.
Expand All @@ -1007,6 +1011,17 @@ struct SnapshotData {
// read only space. We use native_module::CodeCacheInfo because
// v8::ScriptCompiler::CachedData is not copyable.
std::vector<native_module::CodeCacheInfo> code_cache;

static std::unique_ptr<SnapshotData> New();
~SnapshotData();

SnapshotData(const SnapshotData&) = delete;
SnapshotData& operator=(const SnapshotData&) = delete;
SnapshotData(SnapshotData&&) = delete;
SnapshotData& operator=(SnapshotData&&) = delete;

// Only invoked by New().
SnapshotData() = default;
};

class Environment : public MemoryRetainer {
Expand Down
11 changes: 6 additions & 5 deletions src/node_snapshot_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ struct SnapshotData;

class NODE_EXTERN_PRIVATE SnapshotBuilder {
public:
static std::string Generate(const std::vector<std::string> args,
const std::vector<std::string> exec_args);
static int Generate(std::ostream& out,
const std::vector<std::string> args,
const std::vector<std::string> exec_args);

// Generate the snapshot into out.
static void Generate(SnapshotData* out,
const std::vector<std::string> args,
const std::vector<std::string> exec_args);
static int Generate(SnapshotData* out,
const std::vector<std::string> args,
const std::vector<std::string> exec_args);

// If nullptr is returned, the binary is not built with embedded
// snapshot.
Expand Down
Loading

0 comments on commit e9765f3

Please sign in to comment.