Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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/logging.h" |
| 11 | #include "src/base/platform/platform.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 12 | #include "src/flags.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | data->data = nullptr; |
| 29 | data->raw_size = 0; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | void DeleteStartupData(v8::StartupData* data) { |
| 34 | delete[] data->data; |
| 35 | ClearStartupData(data); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void FreeStartupData() { |
| 40 | DeleteStartupData(&g_natives); |
| 41 | DeleteStartupData(&g_snapshot); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void Load(const char* blob_file, v8::StartupData* startup_data, |
| 46 | void (*setter_fn)(v8::StartupData*)) { |
| 47 | ClearStartupData(startup_data); |
| 48 | |
| 49 | CHECK(blob_file); |
| 50 | |
| 51 | FILE* file = fopen(blob_file, "rb"); |
| 52 | if (!file) { |
| 53 | PrintF(stderr, "Failed to open startup resource '%s'.\n", blob_file); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | fseek(file, 0, SEEK_END); |
| 58 | startup_data->raw_size = static_cast<int>(ftell(file)); |
| 59 | rewind(file); |
| 60 | |
| 61 | startup_data->data = new char[startup_data->raw_size]; |
| 62 | int read_size = static_cast<int>(fread(const_cast<char*>(startup_data->data), |
| 63 | 1, startup_data->raw_size, file)); |
| 64 | fclose(file); |
| 65 | |
| 66 | if (startup_data->raw_size == read_size) { |
| 67 | (*setter_fn)(startup_data); |
| 68 | } else { |
| 69 | PrintF(stderr, "Corrupted startup resource '%s'.\n", blob_file); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) { |
| 75 | Load(natives_blob, &g_natives, v8::V8::SetNativesDataBlob); |
| 76 | Load(snapshot_blob, &g_snapshot, v8::V8::SetSnapshotDataBlob); |
| 77 | |
| 78 | atexit(&FreeStartupData); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | char* RelativePath(char** buffer, const char* exec_path, const char* name) { |
| 83 | DCHECK(exec_path); |
| 84 | int path_separator = static_cast<int>(strlen(exec_path)) - 1; |
| 85 | while (path_separator >= 0 && |
| 86 | !base::OS::isDirectorySeparator(exec_path[path_separator])) { |
| 87 | path_separator--; |
| 88 | } |
| 89 | if (path_separator >= 0) { |
| 90 | int name_length = static_cast<int>(strlen(name)); |
| 91 | *buffer = |
| 92 | reinterpret_cast<char*>(calloc(path_separator + name_length + 2, 1)); |
| 93 | *buffer[0] = '\0'; |
| 94 | strncat(*buffer, exec_path, path_separator + 1); |
| 95 | strncat(*buffer, name, name_length); |
| 96 | } else { |
| 97 | *buffer = strdup(name); |
| 98 | } |
| 99 | return *buffer; |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 104 | |
| 105 | |
| 106 | void InitializeExternalStartupData(const char* directory_path) { |
| 107 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 108 | char* natives; |
| 109 | char* snapshot; |
| 110 | LoadFromFiles(RelativePath(&natives, directory_path, "natives_blob.bin"), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 111 | RelativePath(&snapshot, directory_path, |
| 112 | FLAG_ignition ? "snapshot_blob_ignition.bin" |
| 113 | : "snapshot_blob.bin")); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 114 | free(natives); |
| 115 | free(snapshot); |
| 116 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void InitializeExternalStartupData(const char* natives_blob, |
| 121 | const char* snapshot_blob) { |
| 122 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 123 | LoadFromFiles(natives_blob, snapshot_blob); |
| 124 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 125 | } |
| 126 | |
| 127 | } // namespace internal |
| 128 | } // namespace v8 |