• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/init/startup-data-util.h"
6 
7 #include <stdlib.h>
8 #include <string.h>
9 
10 #include "src/base/file-utils.h"
11 #include "src/base/logging.h"
12 #include "src/base/platform/platform.h"
13 #include "src/flags/flags.h"
14 #include "src/utils/utils.h"
15 
16 namespace v8 {
17 namespace internal {
18 
19 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
20 
21 namespace {
22 
23 v8::StartupData g_snapshot;
24 
ClearStartupData(v8::StartupData * data)25 void ClearStartupData(v8::StartupData* data) {
26   data->data = nullptr;
27   data->raw_size = 0;
28 }
29 
DeleteStartupData(v8::StartupData * data)30 void DeleteStartupData(v8::StartupData* data) {
31   delete[] data->data;
32   ClearStartupData(data);
33 }
34 
FreeStartupData()35 void FreeStartupData() {
36   DeleteStartupData(&g_snapshot);
37 }
38 
Load(const char * blob_file,v8::StartupData * startup_data,void (* setter_fn)(v8::StartupData *))39 void Load(const char* blob_file, v8::StartupData* startup_data,
40           void (*setter_fn)(v8::StartupData*)) {
41   ClearStartupData(startup_data);
42 
43   CHECK(blob_file);
44 
45   FILE* file = fopen(blob_file, "rb");
46   if (!file) {
47     PrintF(stderr, "Failed to open startup resource '%s'.\n", blob_file);
48     return;
49   }
50 
51   fseek(file, 0, SEEK_END);
52   startup_data->raw_size = static_cast<int>(ftell(file));
53   rewind(file);
54 
55   startup_data->data = new char[startup_data->raw_size];
56   int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data),
57                                          1, startup_data->raw_size, file));
58   fclose(file);
59 
60   if (startup_data->raw_size == read_size) {
61     (*setter_fn)(startup_data);
62   } else {
63     PrintF(stderr, "Corrupted startup resource '%s'.\n", blob_file);
64   }
65 }
66 
LoadFromFile(const char * snapshot_blob)67 void LoadFromFile(const char* snapshot_blob) {
68   Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob);
69   atexit(&FreeStartupData);
70 }
71 
72 }  // namespace
73 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
74 
InitializeExternalStartupData(const char * directory_path)75 void InitializeExternalStartupData(const char* directory_path) {
76 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
77   const char* snapshot_name = "snapshot_blob.bin";
78 #ifdef V8_MULTI_SNAPSHOTS
79   if (!FLAG_untrusted_code_mitigations) {
80     snapshot_name = "snapshot_blob_trusted.bin";
81   }
82 #endif
83   std::unique_ptr<char[]> snapshot =
84       base::RelativePath(directory_path, snapshot_name);
85   LoadFromFile(snapshot.get());
86 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
87 }
88 
InitializeExternalStartupDataFromFile(const char * snapshot_blob)89 void InitializeExternalStartupDataFromFile(const char* snapshot_blob) {
90 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
91   LoadFromFile(snapshot_blob);
92 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
93 }
94 
95 }  // namespace internal
96 }  // namespace v8
97