startup-data-util.cc 2.72 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/startup-data-util.h"

#include <stdlib.h>
#include <string.h>

10
#include "src/base/file-utils.h"
11
#include "src/base/logging.h"
vogelheim's avatar
vogelheim committed
12
#include "src/base/platform/platform.h"
13
#include "src/flags.h"
14
#include "src/utils.h"
15 16 17


namespace v8 {
vogelheim's avatar
vogelheim committed
18
namespace internal {
19 20 21

#ifdef V8_USE_EXTERNAL_STARTUP_DATA

vogelheim's avatar
vogelheim committed
22
namespace {
23

vogelheim's avatar
vogelheim committed
24 25
v8::StartupData g_natives;
v8::StartupData g_snapshot;
26 27


vogelheim's avatar
vogelheim committed
28 29 30
void ClearStartupData(v8::StartupData* data) {
  data->data = nullptr;
  data->raw_size = 0;
31 32 33
}


vogelheim's avatar
vogelheim committed
34 35 36
void DeleteStartupData(v8::StartupData* data) {
  delete[] data->data;
  ClearStartupData(data);
37 38 39
}


vogelheim's avatar
vogelheim committed
40 41 42
void FreeStartupData() {
  DeleteStartupData(&g_natives);
  DeleteStartupData(&g_snapshot);
43 44 45
}


vogelheim's avatar
vogelheim committed
46 47 48
void Load(const char* blob_file, v8::StartupData* startup_data,
          void (*setter_fn)(v8::StartupData*)) {
  ClearStartupData(startup_data);
49

50
  CHECK(blob_file);
51 52

  FILE* file = fopen(blob_file, "rb");
53 54 55 56
  if (!file) {
    PrintF(stderr, "Failed to open startup resource '%s'.\n", blob_file);
    return;
  }
57 58

  fseek(file, 0, SEEK_END);
59
  startup_data->raw_size = static_cast<int>(ftell(file));
60 61 62 63 64 65 66
  rewind(file);

  startup_data->data = new char[startup_data->raw_size];
  int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data),
                                         1, startup_data->raw_size, file));
  fclose(file);

67 68 69 70 71
  if (startup_data->raw_size == read_size) {
    (*setter_fn)(startup_data);
  } else {
    PrintF(stderr, "Corrupted startup resource '%s'.\n", blob_file);
  }
72 73
}

vogelheim's avatar
vogelheim committed
74 75 76 77 78 79 80 81 82

void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
  Load(natives_blob, &g_natives, v8::V8::SetNativesDataBlob);
  Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob);

  atexit(&FreeStartupData);
}

}  // namespace
83 84
#endif  // V8_USE_EXTERNAL_STARTUP_DATA

vogelheim's avatar
vogelheim committed
85 86 87 88 89

void InitializeExternalStartupData(const char* directory_path) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  char* natives;
  char* snapshot;
90 91 92 93 94 95
  const char* snapshot_name = "snapshot_blob.bin";
#ifdef V8_MULTI_SNAPSHOTS
  if (!FLAG_untrusted_code_mitigations) {
    snapshot_name = "snapshot_blob_trusted.bin";
  }
#endif
96 97
  LoadFromFiles(
      base::RelativePath(&natives, directory_path, "natives_blob.bin"),
98
      base::RelativePath(&snapshot, directory_path, snapshot_name));
vogelheim's avatar
vogelheim committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
  free(natives);
  free(snapshot);
#endif  // V8_USE_EXTERNAL_STARTUP_DATA
}


void InitializeExternalStartupData(const char* natives_blob,
                                   const char* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  LoadFromFiles(natives_blob, snapshot_blob);
#endif  // V8_USE_EXTERNAL_STARTUP_DATA
}

}  // namespace internal
113
}  // namespace v8