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