Line data Source code
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/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.h"
14 : #include "src/utils.h"
15 :
16 :
17 : namespace v8 {
18 : namespace internal {
19 :
20 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
21 :
22 : namespace {
23 :
24 : v8::StartupData g_natives;
25 : v8::StartupData g_snapshot;
26 :
27 :
28 : void ClearStartupData(v8::StartupData* data) {
29 238368 : data->data = nullptr;
30 238368 : data->raw_size = 0;
31 : }
32 :
33 :
34 : void DeleteStartupData(v8::StartupData* data) {
35 119184 : delete[] data->data;
36 : ClearStartupData(data);
37 : }
38 :
39 :
40 59592 : void FreeStartupData() {
41 : DeleteStartupData(&g_natives);
42 : DeleteStartupData(&g_snapshot);
43 59592 : }
44 :
45 :
46 119184 : void Load(const char* blob_file, v8::StartupData* startup_data,
47 : void (*setter_fn)(v8::StartupData*)) {
48 : ClearStartupData(startup_data);
49 :
50 119184 : CHECK(blob_file);
51 :
52 119184 : FILE* file = fopen(blob_file, "rb");
53 119184 : if (!file) {
54 0 : PrintF(stderr, "Failed to open startup resource '%s'.\n", blob_file);
55 119184 : return;
56 : }
57 :
58 119184 : fseek(file, 0, SEEK_END);
59 119184 : startup_data->raw_size = static_cast<int>(ftell(file));
60 119184 : rewind(file);
61 :
62 119184 : startup_data->data = new char[startup_data->raw_size];
63 : int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data),
64 238368 : 1, startup_data->raw_size, file));
65 119184 : fclose(file);
66 :
67 119184 : if (startup_data->raw_size == read_size) {
68 119184 : (*setter_fn)(startup_data);
69 : } else {
70 0 : PrintF(stderr, "Corrupted startup resource '%s'.\n", blob_file);
71 : }
72 : }
73 :
74 :
75 59592 : void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
76 59592 : Load(natives_blob, &g_natives, v8::V8::SetNativesDataBlob);
77 59592 : Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob);
78 :
79 59592 : atexit(&FreeStartupData);
80 59592 : }
81 :
82 : } // namespace
83 : #endif // V8_USE_EXTERNAL_STARTUP_DATA
84 :
85 :
86 59592 : void InitializeExternalStartupData(const char* directory_path) {
87 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
88 : char* natives;
89 : char* snapshot;
90 : const char* snapshot_name = "snapshot_blob.bin";
91 : #ifdef V8_MULTI_SNAPSHOTS
92 : if (!FLAG_untrusted_code_mitigations) {
93 : snapshot_name = "snapshot_blob_trusted.bin";
94 : }
95 : #endif
96 : LoadFromFiles(
97 59592 : base::RelativePath(&natives, directory_path, "natives_blob.bin"),
98 119184 : base::RelativePath(&snapshot, directory_path, snapshot_name));
99 59592 : free(natives);
100 59592 : free(snapshot);
101 : #endif // V8_USE_EXTERNAL_STARTUP_DATA
102 59592 : }
103 :
104 :
105 0 : void InitializeExternalStartupData(const char* natives_blob,
106 : const char* snapshot_blob) {
107 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
108 0 : LoadFromFiles(natives_blob, snapshot_blob);
109 : #endif // V8_USE_EXTERNAL_STARTUP_DATA
110 0 : }
111 :
112 : } // namespace internal
113 178779 : } // namespace v8
|