oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 1 | // Copyright 2013 The Chromium 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 "gin/v8_initializer.h" |
| 6 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
oth | 29c7ed9 | 2015-06-19 14:40:00 | [diff] [blame] | 12 | #include "base/debug/alias.h" |
rmcilroy | 462e47e | 2016-06-24 20:45:08 | [diff] [blame] | 13 | #include "base/debug/crash_logging.h" |
rmcilroy | fe515ad | 2016-04-08 17:59:10 | [diff] [blame] | 14 | #include "base/feature_list.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 15 | #include "base/files/file.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/files/memory_mapped_file.h" |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 18 | #include "base/lazy_instance.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 19 | #include "base/logging.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 20 | #include "base/metrics/histogram_macros.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 21 | #include "base/path_service.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 22 | #include "base/rand_util.h" |
| 23 | #include "base/strings/sys_string_conversions.h" |
rmcilroy | 542f61c | 2016-06-06 16:08:19 | [diff] [blame] | 24 | #include "base/sys_info.h" |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 25 | #include "base/threading/platform_thread.h" |
| 26 | #include "base/time/time.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 27 | #include "build/build_config.h" |
Ross McIlroy | 45e9500a | 2018-03-27 17:06:02 | [diff] [blame] | 28 | #include "gin/gin_features.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 29 | |
| 30 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 31 | #if defined(OS_ANDROID) |
| 32 | #include "base/android/apk_assets.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 33 | #elif defined(OS_MACOSX) |
Max Morin | f0d13c9 | 2017-08-17 10:04:59 | [diff] [blame] | 34 | #include "base/mac/foundation_util.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 35 | #endif |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 36 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 37 | |
| 38 | namespace gin { |
| 39 | |
| 40 | namespace { |
| 41 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 42 | // None of these globals are ever freed nor closed. |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 43 | base::MemoryMappedFile* g_mapped_natives = nullptr; |
| 44 | base::MemoryMappedFile* g_mapped_snapshot = nullptr; |
| 45 | |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 46 | bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 47 | base::RandBytes(buffer, amount); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | void GetMappedFileData(base::MemoryMappedFile* mapped_file, |
| 52 | v8::StartupData* data) { |
| 53 | if (mapped_file) { |
| 54 | data->data = reinterpret_cast<const char*>(mapped_file->data()); |
| 55 | data->raw_size = static_cast<int>(mapped_file->length()); |
| 56 | } else { |
| 57 | data->data = nullptr; |
| 58 | data->raw_size = 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 63 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 64 | // File handles intentionally never closed. Not using File here because its |
| 65 | // Windows implementation guards against two instances owning the same |
| 66 | // PlatformFile (which we allow since we know it is never freed). |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 67 | using OpenedFileMap = |
| 68 | std::map<const char*, |
| 69 | std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>; |
| 70 | base::LazyInstance<OpenedFileMap>::Leaky g_opened_files = |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 71 | LAZY_INSTANCE_INITIALIZER; |
| 72 | |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 73 | const char kNativesFileName[] = "natives_blob.bin"; |
| 74 | |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 75 | #if defined(OS_ANDROID) |
Hitoshi Yoshida | 0669723 | 2018-03-05 04:09:15 | [diff] [blame] | 76 | const char kV8ContextSnapshotFileName64[] = "v8_context_snapshot_64.bin"; |
| 77 | const char kV8ContextSnapshotFileName32[] = "v8_context_snapshot_32.bin"; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 78 | const char kSnapshotFileName64[] = "snapshot_blob_64.bin"; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 79 | const char kSnapshotFileName32[] = "snapshot_blob_32.bin"; |
| 80 | |
| 81 | #if defined(__LP64__) |
Hitoshi Yoshida | 0669723 | 2018-03-05 04:09:15 | [diff] [blame] | 82 | #define kV8ContextSnapshotFileName kV8ContextSnapshotFileName64 |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 83 | #define kSnapshotFileName kSnapshotFileName64 |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 84 | #else |
Hitoshi Yoshida | 0669723 | 2018-03-05 04:09:15 | [diff] [blame] | 85 | #define kV8ContextSnapshotFileName kV8ContextSnapshotFileName32 |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 86 | #define kSnapshotFileName kSnapshotFileName32 |
| 87 | #endif |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 88 | |
| 89 | #else // defined(OS_ANDROID) |
Hitoshi Yoshida | 0669723 | 2018-03-05 04:09:15 | [diff] [blame] | 90 | const char kV8ContextSnapshotFileName[] = "v8_context_snapshot.bin"; |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 91 | const char kSnapshotFileName[] = "snapshot_blob.bin"; |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 92 | #endif // defined(OS_ANDROID) |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 93 | |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 94 | const char* GetSnapshotFileName( |
| 95 | const V8Initializer::V8SnapshotFileType file_type) { |
| 96 | switch (file_type) { |
| 97 | case V8Initializer::V8SnapshotFileType::kDefault: |
| 98 | return kSnapshotFileName; |
| 99 | case V8Initializer::V8SnapshotFileType::kWithAdditionalContext: |
| 100 | return kV8ContextSnapshotFileName; |
| 101 | } |
| 102 | NOTREACHED(); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 106 | void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 107 | #if defined(OS_ANDROID) |
| 108 | // This is the path within the .apk. |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 109 | *path_out = |
| 110 | base::FilePath(FILE_PATH_LITERAL("assets")).AppendASCII(file_name); |
| 111 | #elif defined(OS_MACOSX) |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 112 | base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 113 | base::SysUTF8ToCFStringRef(file_name)); |
| 114 | *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 115 | #else |
| 116 | base::FilePath data_path; |
Avi Drissman | ea15ea0 | 2018-05-07 18:55:12 | [diff] [blame] | 117 | bool r = base::PathService::Get(base::DIR_ASSETS, &data_path); |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 118 | DCHECK(r); |
| 119 | *path_out = data_path.AppendASCII(file_name); |
| 120 | #endif |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 121 | } |
| 122 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 123 | bool MapV8File(base::PlatformFile platform_file, |
| 124 | base::MemoryMappedFile::Region region, |
| 125 | base::MemoryMappedFile** mmapped_file_out) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 126 | DCHECK(*mmapped_file_out == NULL); |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 127 | std::unique_ptr<base::MemoryMappedFile> mmapped_file( |
| 128 | new base::MemoryMappedFile()); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 129 | if (mmapped_file->Initialize(base::File(platform_file), region)) { |
| 130 | *mmapped_file_out = mmapped_file.release(); |
| 131 | return true; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 132 | } |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 133 | return false; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 134 | } |
| 135 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 136 | base::PlatformFile OpenV8File(const char* file_name, |
| 137 | base::MemoryMappedFile::Region* region_out) { |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 138 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 139 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 140 | |
| 141 | // These match tools/metrics/histograms.xml |
| 142 | enum OpenV8FileResult { |
| 143 | OPENED = 0, |
| 144 | OPENED_RETRY, |
| 145 | FAILED_IN_USE, |
| 146 | FAILED_OTHER, |
| 147 | MAX_VALUE |
| 148 | }; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 149 | base::FilePath path; |
| 150 | GetV8FilePath(file_name, &path); |
| 151 | |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 152 | #if defined(OS_ANDROID) |
| 153 | base::File file(base::android::OpenApkAsset(path.value(), region_out)); |
| 154 | OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED |
| 155 | : OpenV8FileResult::FAILED_OTHER; |
| 156 | #else |
| 157 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 158 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 159 | const int kMaxOpenAttempts = 5; |
| 160 | const int kOpenRetryDelayMillis = 250; |
| 161 | |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 162 | OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 163 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 164 | base::File file; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 165 | for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { |
| 166 | file.Initialize(path, flags); |
| 167 | if (file.IsValid()) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 168 | *region_out = base::MemoryMappedFile::Region::kWholeFile; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 169 | if (attempt == 0) { |
| 170 | result = OpenV8FileResult::OPENED; |
| 171 | break; |
| 172 | } else { |
| 173 | result = OpenV8FileResult::OPENED_RETRY; |
| 174 | break; |
| 175 | } |
| 176 | } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) { |
| 177 | result = OpenV8FileResult::FAILED_OTHER; |
oth | 29c7ed9 | 2015-06-19 14:40:00 | [diff] [blame] | 178 | #ifdef OS_WIN |
| 179 | // TODO(oth): temporary diagnostics for http://crbug.com/479537 |
| 180 | std::string narrow(kNativesFileName); |
| 181 | base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end()); |
| 182 | if (path.BaseName().value() == nativesBlob) { |
| 183 | base::File::Error file_error = file.error_details(); |
| 184 | base::debug::Alias(&file_error); |
| 185 | LOG(FATAL) << "Failed to open V8 file '" << path.value() |
| 186 | << "' (reason: " << file.error_details() << ")"; |
| 187 | } |
| 188 | #endif // OS_WIN |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 189 | break; |
| 190 | } else if (kMaxOpenAttempts - 1 != attempt) { |
| 191 | base::PlatformThread::Sleep( |
| 192 | base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis)); |
| 193 | } |
| 194 | } |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 195 | #endif // defined(OS_ANDROID) |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 196 | |
| 197 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
| 198 | result, |
| 199 | OpenV8FileResult::MAX_VALUE); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 200 | return file.TakePlatformFile(); |
| 201 | } |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 202 | |
Hitoshi Yoshida | 129ff2b | 2017-08-24 02:19:10 | [diff] [blame] | 203 | OpenedFileMap::mapped_type& GetOpenedFile(const char* filename) { |
| 204 | OpenedFileMap& opened_files(g_opened_files.Get()); |
Jeremy Roman | 6a3b3d4 | 2017-08-24 17:13:51 | [diff] [blame] | 205 | auto result = opened_files.emplace(filename, OpenedFileMap::mapped_type()); |
| 206 | OpenedFileMap::mapped_type& opened_file = result.first->second; |
| 207 | bool is_new_file = result.second; |
Hitoshi Yoshida | 129ff2b | 2017-08-24 02:19:10 | [diff] [blame] | 208 | |
Jeremy Roman | 6a3b3d4 | 2017-08-24 17:13:51 | [diff] [blame] | 209 | // If we have no cache, try to open it and cache the result. |
| 210 | if (is_new_file) |
| 211 | opened_file.first = OpenV8File(filename, &opened_file.second); |
| 212 | |
| 213 | return opened_file; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 214 | } |
| 215 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 216 | enum LoadV8FileResult { |
| 217 | V8_LOAD_SUCCESS = 0, |
| 218 | V8_LOAD_FAILED_OPEN, |
| 219 | V8_LOAD_FAILED_MAP, |
jcivelli | dbe3dec | 2017-02-07 16:58:23 | [diff] [blame] | 220 | V8_LOAD_FAILED_VERIFY, // Deprecated. |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 221 | V8_LOAD_MAX_VALUE |
| 222 | }; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 223 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 224 | LoadV8FileResult MapOpenedFile(const OpenedFileMap::mapped_type& file_region, |
| 225 | base::MemoryMappedFile** mmapped_file_out) { |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 226 | if (file_region.first == base::kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 227 | return V8_LOAD_FAILED_OPEN; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 228 | if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 229 | return V8_LOAD_FAILED_MAP; |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 230 | return V8_LOAD_SUCCESS; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 231 | } |
| 232 | |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 233 | #endif // defined(V8_USE_EXTERNAL_STATUP_DATA) |
Hitoshi Yoshida | 129ff2b | 2017-08-24 02:19:10 | [diff] [blame] | 234 | |
jcivelli | dbe3dec | 2017-02-07 16:58:23 | [diff] [blame] | 235 | } // namespace |
| 236 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 237 | // static |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 238 | void V8Initializer::Initialize(IsolateHolder::ScriptMode mode, |
| 239 | IsolateHolder::V8ExtrasMode v8_extras_mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 240 | static bool v8_is_initialized = false; |
| 241 | if (v8_is_initialized) |
| 242 | return; |
| 243 | |
| 244 | v8::V8::InitializePlatform(V8Platform::Get()); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 245 | |
Ross McIlroy | 45e9500a | 2018-03-27 17:06:02 | [diff] [blame] | 246 | if (base::FeatureList::IsEnabled(features::kV8OptimizeJavascript)) { |
| 247 | static const char optimize[] = "--opt"; |
| 248 | v8::V8::SetFlagsFromString(optimize, sizeof(optimize) - 1); |
| 249 | } else { |
| 250 | static const char no_optimize[] = "--no-opt"; |
| 251 | v8::V8::SetFlagsFromString(no_optimize, sizeof(no_optimize) - 1); |
| 252 | } |
| 253 | |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 254 | if (IsolateHolder::kStrictMode == mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 255 | static const char use_strict[] = "--use_strict"; |
| 256 | v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); |
| 257 | } |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 258 | if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) { |
| 259 | static const char flag[] = "--experimental_extras"; |
| 260 | v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1); |
| 261 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 262 | |
| 263 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 264 | v8::StartupData natives; |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 265 | GetMappedFileData(g_mapped_natives, &natives); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 266 | v8::V8::SetNativesDataBlob(&natives); |
| 267 | |
Hitoshi Yoshida | 231de835 | 2017-12-08 01:06:33 | [diff] [blame] | 268 | if (g_mapped_snapshot) { |
Robert Liao | 1e7eb05 | 2017-12-15 00:30:05 | [diff] [blame] | 269 | v8::StartupData snapshot; |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 270 | GetMappedFileData(g_mapped_snapshot, &snapshot); |
Hitoshi Yoshida | 231de835 | 2017-12-08 01:06:33 | [diff] [blame] | 271 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 272 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 273 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 274 | |
| 275 | v8::V8::SetEntropySource(&GenerateEntropy); |
| 276 | v8::V8::Initialize(); |
| 277 | |
| 278 | v8_is_initialized = true; |
| 279 | } |
| 280 | |
| 281 | // static |
Hitoshi Yoshida | d88a223e | 2017-09-10 05:55:25 | [diff] [blame] | 282 | void V8Initializer::GetV8ExternalSnapshotData(v8::StartupData* natives, |
| 283 | v8::StartupData* snapshot) { |
| 284 | GetMappedFileData(g_mapped_natives, natives); |
| 285 | GetMappedFileData(g_mapped_snapshot, snapshot); |
| 286 | } |
| 287 | |
| 288 | // static |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 289 | void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
| 290 | int* natives_size_out, |
| 291 | const char** snapshot_data_out, |
| 292 | int* snapshot_size_out) { |
Hitoshi Yoshida | d88a223e | 2017-09-10 05:55:25 | [diff] [blame] | 293 | v8::StartupData natives; |
| 294 | v8::StartupData snapshot; |
| 295 | GetV8ExternalSnapshotData(&natives, &snapshot); |
| 296 | *natives_data_out = natives.data; |
| 297 | *natives_size_out = natives.raw_size; |
| 298 | *snapshot_data_out = snapshot.data; |
| 299 | *snapshot_size_out = snapshot.raw_size; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 300 | } |
| 301 | |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 302 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 303 | |
| 304 | // static |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 305 | void V8Initializer::LoadV8Snapshot(V8SnapshotFileType snapshot_file_type) { |
| 306 | if (g_mapped_snapshot) { |
| 307 | // TODO(crbug.com/802962): Confirm not loading different type of snapshot |
| 308 | // files in a process. |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 309 | return; |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 310 | } |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 311 | |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 312 | LoadV8FileResult result = |
| 313 | MapOpenedFile(GetOpenedFile(GetSnapshotFileName(snapshot_file_type)), |
| 314 | &g_mapped_snapshot); |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 315 | // V8 can't start up without the source of the natives, but it can |
| 316 | // start up (slower) without the snapshot. |
| 317 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 318 | V8_LOAD_MAX_VALUE); |
| 319 | } |
| 320 | |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 321 | // static |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 322 | void V8Initializer::LoadV8Natives() { |
| 323 | if (g_mapped_natives) |
| 324 | return; |
| 325 | |
| 326 | LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kNativesFileName), |
| 327 | &g_mapped_natives); |
| 328 | if (result != V8_LOAD_SUCCESS) { |
| 329 | LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
| 330 | << static_cast<int>(result); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // static |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 335 | void V8Initializer::LoadV8SnapshotFromFD( |
| 336 | base::PlatformFile snapshot_pf, |
| 337 | int64_t snapshot_offset, |
| 338 | int64_t snapshot_size, |
| 339 | V8SnapshotFileType snapshot_file_type) { |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 340 | if (g_mapped_snapshot) |
| 341 | return; |
| 342 | |
| 343 | if (snapshot_pf == base::kInvalidPlatformFile) |
| 344 | return; |
| 345 | |
| 346 | base::MemoryMappedFile::Region snapshot_region = |
| 347 | base::MemoryMappedFile::Region::kWholeFile; |
| 348 | if (snapshot_size != 0 || snapshot_offset != 0) { |
| 349 | snapshot_region.offset = snapshot_offset; |
| 350 | snapshot_region.size = snapshot_size; |
| 351 | } |
| 352 | |
| 353 | LoadV8FileResult result = V8_LOAD_SUCCESS; |
| 354 | if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) |
| 355 | result = V8_LOAD_FAILED_MAP; |
| 356 | if (result == V8_LOAD_SUCCESS) { |
Hitoshi Yoshida | 9aff02e | 2018-01-19 16:55:03 | [diff] [blame] | 357 | g_opened_files.Get()[GetSnapshotFileName(snapshot_file_type)] = |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 358 | std::make_pair(snapshot_pf, snapshot_region); |
| 359 | } |
| 360 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 361 | V8_LOAD_MAX_VALUE); |
| 362 | } |
| 363 | |
| 364 | // static |
| 365 | void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
| 366 | int64_t natives_offset, |
| 367 | int64_t natives_size) { |
| 368 | if (g_mapped_natives) |
| 369 | return; |
| 370 | |
| 371 | CHECK_NE(natives_pf, base::kInvalidPlatformFile); |
| 372 | |
| 373 | base::MemoryMappedFile::Region natives_region = |
| 374 | base::MemoryMappedFile::Region::kWholeFile; |
| 375 | if (natives_size != 0 || natives_offset != 0) { |
| 376 | natives_region.offset = natives_offset; |
| 377 | natives_region.size = natives_size; |
| 378 | } |
| 379 | |
| 380 | if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
| 381 | LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
| 382 | } |
| 383 | g_opened_files.Get()[kNativesFileName] = |
| 384 | std::make_pair(natives_pf, natives_region); |
| 385 | } |
| 386 | |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 387 | #if defined(OS_ANDROID) |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 388 | // static |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 389 | base::FilePath V8Initializer::GetNativesFilePath() { |
| 390 | base::FilePath path; |
| 391 | GetV8FilePath(kNativesFileName, &path); |
| 392 | return path; |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame] | 393 | } |
| 394 | |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 395 | // static |
Hitoshi Yoshida | 0669723 | 2018-03-05 04:09:15 | [diff] [blame] | 396 | base::FilePath V8Initializer::GetSnapshotFilePath( |
| 397 | bool abi_32_bit, |
| 398 | V8SnapshotFileType snapshot_file_type) { |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 399 | base::FilePath path; |
Hitoshi Yoshida | 0669723 | 2018-03-05 04:09:15 | [diff] [blame] | 400 | const char* filename = nullptr; |
| 401 | switch (snapshot_file_type) { |
| 402 | case V8Initializer::V8SnapshotFileType::kDefault: |
| 403 | filename = abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64; |
| 404 | break; |
| 405 | case V8Initializer::V8SnapshotFileType::kWithAdditionalContext: |
| 406 | filename = abi_32_bit ? kV8ContextSnapshotFileName32 |
| 407 | : kV8ContextSnapshotFileName64; |
| 408 | break; |
| 409 | } |
| 410 | CHECK(filename); |
| 411 | |
| 412 | GetV8FilePath(filename, &path); |
Hitoshi Yoshida | ba9c2f0f | 2017-11-27 03:11:46 | [diff] [blame] | 413 | return path; |
| 414 | } |
| 415 | #endif // defined(OS_ANDROID) |
| 416 | #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 417 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 418 | } // namespace gin |