mksnapshot.cc 6 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include <errno.h>
6
#include <signal.h>
7
#include <stdio.h>
8

9
#include "include/libplatform/libplatform.h"
10
#include "src/assembler.h"
11
#include "src/base/platform/platform.h"
12
#include "src/flags.h"
13
#include "src/msan.h"
14
#include "src/snapshot/natives.h"
15 16
#include "src/snapshot/partial-serializer.h"
#include "src/snapshot/startup-serializer.h"
17

18 19
class SnapshotWriter {
 public:
20 21
  SnapshotWriter()
      : snapshot_cpp_path_(nullptr), snapshot_blob_path_(nullptr) {}
22

23 24
  void SetSnapshotFile(const char* snapshot_cpp_file) {
    snapshot_cpp_path_ = snapshot_cpp_file;
25
  }
26

27 28
  void SetStartupBlobFile(const char* snapshot_blob_file) {
    snapshot_blob_path_ = snapshot_blob_file;
29 30
  }

31
  void WriteSnapshot(v8::StartupData blob) const {
32 33 34 35
    // TODO(crbug/633159): if we crash before the files have been fully created,
    // we end up with a corrupted snapshot file. The build step would succeed,
    // but the build target is unusable. Ideally we would write out temporary
    // files and only move them to the final destination as last step.
36 37
    i::Vector<const i::byte> blob_vector(
        reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
38
    MaybeWriteSnapshotFile(blob_vector);
39
    MaybeWriteStartupBlob(blob_vector);
40 41 42
  }

 private:
43
  void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
44
    if (!snapshot_blob_path_) return;
45

46 47 48
    FILE* fp = GetFileDescriptorOrDie(snapshot_blob_path_);
    size_t written = fwrite(blob.begin(), 1, blob.length(), fp);
    fclose(fp);
49
    if (written != static_cast<size_t>(blob.length())) {
50
      i::PrintF("Writing snapshot file failed.. Aborting.\n");
51
      remove(snapshot_blob_path_);
52 53 54 55
      exit(1);
    }
  }

56
  void MaybeWriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
57 58 59 60 61 62 63
    if (!snapshot_cpp_path_) return;

    FILE* fp = GetFileDescriptorOrDie(snapshot_cpp_path_);

    WriteFilePrefix(fp);
    WriteData(fp, blob);
    WriteFileSuffix(fp);
64

65
    fclose(fp);
66 67
  }

68 69 70 71 72 73 74
  static void WriteFilePrefix(FILE* fp) {
    fprintf(fp, "// Autogenerated snapshot file. Do not edit.\n\n");
    fprintf(fp, "#include \"src/v8.h\"\n");
    fprintf(fp, "#include \"src/base/platform/platform.h\"\n\n");
    fprintf(fp, "#include \"src/snapshot/snapshot.h\"\n\n");
    fprintf(fp, "namespace v8 {\n");
    fprintf(fp, "namespace internal {\n\n");
75 76
  }

77 78 79 80 81 82
  static void WriteFileSuffix(FILE* fp) {
    fprintf(fp, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n");
    fprintf(fp, "  return &blob;\n");
    fprintf(fp, "}\n\n");
    fprintf(fp, "}  // namespace internal\n");
    fprintf(fp, "}  // namespace v8\n");
83 84
  }

85 86 87 88 89 90 91
  static void WriteData(FILE* fp, const i::Vector<const i::byte>& blob) {
    fprintf(fp, "static const byte blob_data[] = {\n");
    WriteSnapshotData(fp, blob);
    fprintf(fp, "};\n");
    fprintf(fp, "static const int blob_size = %d;\n", blob.length());
    fprintf(fp, "static const v8::StartupData blob =\n");
    fprintf(fp, "{ (const char*) blob_data, blob_size };\n");
92 93
  }

94 95
  static void WriteSnapshotData(FILE* fp,
                                const i::Vector<const i::byte>& blob) {
96
    for (int i = 0; i < blob.length(); i++) {
97 98 99
      if ((i & 0x1f) == 0x1f) fprintf(fp, "\n");
      if (i > 0) fprintf(fp, ",");
      fprintf(fp, "%u", static_cast<unsigned char>(blob.at(i)));
100
    }
101
    fprintf(fp, "\n");
102 103
  }

104
  static FILE* GetFileDescriptorOrDie(const char* filename) {
105
    FILE* fp = v8::base::OS::FOpen(filename, "wb");
106
    if (fp == nullptr) {
107 108 109 110 111
      i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
      exit(1);
    }
    return fp;
  }
112

113 114
  const char* snapshot_cpp_path_;
  const char* snapshot_blob_path_;
115
};
116

117
char* GetExtraCode(char* filename, const char* description) {
118
  if (filename == nullptr || strlen(filename) == 0) return nullptr;
119
  ::printf("Loading script for %s: %s\n", description, filename);
120
  FILE* file = v8::base::OS::FOpen(filename, "rb");
121
  if (file == nullptr) {
122 123 124 125
    fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
    exit(1);
  }
  fseek(file, 0, SEEK_END);
126
  size_t size = ftell(file);
127 128 129
  rewind(file);
  char* chars = new char[size + 1];
  chars[size] = '\0';
130 131 132
  for (size_t i = 0; i < size;) {
    size_t read = fread(&chars[i], 1, size - i, file);
    if (ferror(file)) {
133 134 135 136 137 138 139 140 141 142
      fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
      exit(1);
    }
    i += read;
  }
  fclose(file);
  return chars;
}


143
int main(int argc, char** argv) {
144 145
  // Make mksnapshot runs predictable to create reproducible snapshots.
  i::FLAG_predictable = true;
146

147 148
  // Print the usage if an error occurs when parsing the command line
  // flags or if the help flag is set.
149
  int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
150
  if (result > 0 || (argc > 3) || i::FLAG_help) {
151 152
    ::printf("Usage: %s --startup_src=... --startup_blob=... [extras]\n",
             argv[0]);
153
    i::FlagList::PrintHelp();
154
    return !i::FLAG_help;
155
  }
156 157

  i::CpuFeatures::Probe(true);
158
  v8::V8::InitializeICUDefaultLocation(argv[0]);
159 160 161 162
  v8::Platform* platform = v8::platform::CreateDefaultPlatform();
  v8::V8::InitializePlatform(platform);
  v8::V8::Initialize();

163
  {
164 165
    SnapshotWriter writer;
    if (i::FLAG_startup_src) writer.SetSnapshotFile(i::FLAG_startup_src);
166
    if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob);
167

168 169
    char* embed_script =
        GetExtraCode(argc >= 2 ? argv[1] : nullptr, "embedding");
170
    v8::StartupData blob = v8::V8::CreateSnapshotDataBlob(embed_script);
171 172
    delete[] embed_script;

173 174
    char* warmup_script =
        GetExtraCode(argc >= 3 ? argv[2] : nullptr, "warm up");
175
    if (warmup_script) {
176
      v8::StartupData cold = blob;
177 178 179 180 181
      blob = v8::V8::WarmUpSnapshotDataBlob(cold, warmup_script);
      delete[] cold.data;
      delete[] warmup_script;
    }

182 183 184
    CHECK(blob.data);
    writer.WriteSnapshot(blob);
    delete[] blob.data;
185
  }
186

187 188
  v8::V8::Dispose();
  v8::V8::ShutdownPlatform();
189
  delete platform;
190 191
  return 0;
}