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" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 21 | #include "base/rand_util.h" |
| 22 | #include "base/strings/sys_string_conversions.h" |
rmcilroy | 542f61c | 2016-06-06 16:08:19 | [diff] [blame] | 23 | #include "base/sys_info.h" |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 24 | #include "base/threading/platform_thread.h" |
| 25 | #include "base/time/time.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 26 | #include "crypto/sha2.h" |
rmcilroy | fe515ad | 2016-04-08 17:59:10 | [diff] [blame] | 27 | #include "gin/public/gin_features.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 28 | |
| 29 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 30 | #if defined(OS_ANDROID) |
| 31 | #include "base/android/apk_assets.h" |
| 32 | #endif |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 33 | #if defined(OS_MACOSX) |
| 34 | #include "base/mac/foundation_util.h" |
| 35 | #endif // OS_MACOSX |
| 36 | #include "base/path_service.h" |
| 37 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 38 | |
| 39 | namespace gin { |
| 40 | |
| 41 | namespace { |
| 42 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 43 | // None of these globals are ever freed nor closed. |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 44 | base::MemoryMappedFile* g_mapped_natives = nullptr; |
| 45 | base::MemoryMappedFile* g_mapped_snapshot = nullptr; |
| 46 | |
| 47 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 48 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 49 | // File handles intentionally never closed. Not using File here because its |
| 50 | // Windows implementation guards against two instances owning the same |
| 51 | // PlatformFile (which we allow since we know it is never freed). |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 52 | typedef std::map<const char*, |
| 53 | std::pair<base::PlatformFile, base::MemoryMappedFile::Region>> |
| 54 | OpenedFileMap; |
| 55 | static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files = |
| 56 | LAZY_INSTANCE_INITIALIZER; |
| 57 | |
| 58 | OpenedFileMap::mapped_type& GetOpenedFile(const char* file) { |
| 59 | OpenedFileMap& opened_files(g_opened_files.Get()); |
| 60 | if (opened_files.find(file) == opened_files.end()) { |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 61 | opened_files[file] = std::make_pair(base::kInvalidPlatformFile, |
| 62 | base::MemoryMappedFile::Region()); |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 63 | } |
| 64 | return opened_files[file]; |
| 65 | } |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 66 | |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 67 | const char kNativesFileName[] = "natives_blob.bin"; |
| 68 | |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 69 | #if defined(OS_ANDROID) |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 70 | const char kSnapshotFileName64[] = "snapshot_blob_64.bin"; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 71 | const char kSnapshotFileName32[] = "snapshot_blob_32.bin"; |
| 72 | |
| 73 | #if defined(__LP64__) |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 74 | #define kSnapshotFileName kSnapshotFileName64 |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 75 | #else |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 76 | #define kSnapshotFileName kSnapshotFileName32 |
| 77 | #endif |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 78 | |
| 79 | #else // defined(OS_ANDROID) |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 80 | const char kSnapshotFileName[] = "snapshot_blob.bin"; |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 81 | #endif // defined(OS_ANDROID) |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 82 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 83 | void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 84 | #if !defined(OS_MACOSX) |
| 85 | base::FilePath data_path; |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 86 | #if defined(OS_ANDROID) |
| 87 | // This is the path within the .apk. |
| 88 | data_path = base::FilePath(FILE_PATH_LITERAL("assets")); |
| 89 | #elif defined(OS_POSIX) |
| 90 | PathService::Get(base::DIR_EXE, &data_path); |
| 91 | #elif defined(OS_WIN) |
| 92 | PathService::Get(base::DIR_MODULE, &data_path); |
| 93 | #endif |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 94 | DCHECK(!data_path.empty()); |
| 95 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 96 | *path_out = data_path.AppendASCII(file_name); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 97 | #else // !defined(OS_MACOSX) |
| 98 | base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 99 | base::SysUTF8ToCFStringRef(file_name)); |
| 100 | *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 101 | #endif // !defined(OS_MACOSX) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 102 | DCHECK(!path_out->empty()); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 103 | } |
| 104 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 105 | static bool MapV8File(base::PlatformFile platform_file, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 106 | base::MemoryMappedFile::Region region, |
| 107 | base::MemoryMappedFile** mmapped_file_out) { |
| 108 | DCHECK(*mmapped_file_out == NULL); |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 109 | std::unique_ptr<base::MemoryMappedFile> mmapped_file( |
| 110 | new base::MemoryMappedFile()); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 111 | if (mmapped_file->Initialize(base::File(platform_file), region)) { |
| 112 | *mmapped_file_out = mmapped_file.release(); |
| 113 | return true; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 114 | } |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 115 | return false; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 116 | } |
| 117 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 118 | base::PlatformFile OpenV8File(const char* file_name, |
| 119 | base::MemoryMappedFile::Region* region_out) { |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 120 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 121 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 122 | |
| 123 | // These match tools/metrics/histograms.xml |
| 124 | enum OpenV8FileResult { |
| 125 | OPENED = 0, |
| 126 | OPENED_RETRY, |
| 127 | FAILED_IN_USE, |
| 128 | FAILED_OTHER, |
| 129 | MAX_VALUE |
| 130 | }; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 131 | base::FilePath path; |
| 132 | GetV8FilePath(file_name, &path); |
| 133 | |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 134 | #if defined(OS_ANDROID) |
| 135 | base::File file(base::android::OpenApkAsset(path.value(), region_out)); |
| 136 | OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED |
| 137 | : OpenV8FileResult::FAILED_OTHER; |
| 138 | #else |
| 139 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 140 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 141 | const int kMaxOpenAttempts = 5; |
| 142 | const int kOpenRetryDelayMillis = 250; |
| 143 | |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 144 | OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 145 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 146 | base::File file; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 147 | for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { |
| 148 | file.Initialize(path, flags); |
| 149 | if (file.IsValid()) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 150 | *region_out = base::MemoryMappedFile::Region::kWholeFile; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 151 | if (attempt == 0) { |
| 152 | result = OpenV8FileResult::OPENED; |
| 153 | break; |
| 154 | } else { |
| 155 | result = OpenV8FileResult::OPENED_RETRY; |
| 156 | break; |
| 157 | } |
| 158 | } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) { |
| 159 | result = OpenV8FileResult::FAILED_OTHER; |
oth | 29c7ed9 | 2015-06-19 14:40:00 | [diff] [blame] | 160 | #ifdef OS_WIN |
| 161 | // TODO(oth): temporary diagnostics for http://crbug.com/479537 |
| 162 | std::string narrow(kNativesFileName); |
| 163 | base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end()); |
| 164 | if (path.BaseName().value() == nativesBlob) { |
| 165 | base::File::Error file_error = file.error_details(); |
| 166 | base::debug::Alias(&file_error); |
| 167 | LOG(FATAL) << "Failed to open V8 file '" << path.value() |
| 168 | << "' (reason: " << file.error_details() << ")"; |
| 169 | } |
| 170 | #endif // OS_WIN |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 171 | break; |
| 172 | } else if (kMaxOpenAttempts - 1 != attempt) { |
| 173 | base::PlatformThread::Sleep( |
| 174 | base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis)); |
| 175 | } |
| 176 | } |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 177 | #endif // defined(OS_ANDROID) |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 178 | |
| 179 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
| 180 | result, |
| 181 | OpenV8FileResult::MAX_VALUE); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 182 | return file.TakePlatformFile(); |
| 183 | } |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 184 | |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 185 | static const OpenedFileMap::mapped_type OpenFileIfNecessary( |
| 186 | const char* file_name) { |
| 187 | OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name); |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 188 | if (opened.first == base::kInvalidPlatformFile) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 189 | opened.first = OpenV8File(file_name, &opened.second); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 190 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 191 | return opened; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 192 | } |
| 193 | |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 194 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 195 | bool VerifyV8StartupFile(base::MemoryMappedFile** file, |
| 196 | const unsigned char* fingerprint) { |
| 197 | unsigned char output[crypto::kSHA256Length]; |
| 198 | crypto::SHA256HashString( |
| 199 | base::StringPiece(reinterpret_cast<const char*>((*file)->data()), |
| 200 | (*file)->length()), |
| 201 | output, sizeof(output)); |
| 202 | if (!memcmp(fingerprint, output, sizeof(output))) { |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | // TODO(oth): Remove this temporary diagnostics for http://crbug.com/501799 |
| 207 | uint64_t input[sizeof(output)]; |
| 208 | memcpy(input, fingerprint, sizeof(input)); |
| 209 | |
| 210 | base::debug::Alias(output); |
| 211 | base::debug::Alias(input); |
| 212 | |
| 213 | const uint64_t* o64 = reinterpret_cast<const uint64_t*>(output); |
| 214 | const uint64_t* f64 = reinterpret_cast<const uint64_t*>(fingerprint); |
| 215 | LOG(FATAL) << "Natives length " << (*file)->length() |
| 216 | << " H(computed) " << o64[0] << o64[1] << o64[2] << o64[3] |
| 217 | << " H(expected) " << f64[0] << f64[1] << f64[2] << f64[3]; |
| 218 | |
| 219 | delete *file; |
| 220 | *file = NULL; |
| 221 | return false; |
| 222 | } |
| 223 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 224 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 225 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 226 | bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 227 | base::RandBytes(buffer, amount); |
| 228 | return true; |
| 229 | } |
| 230 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 231 | } // namespace |
| 232 | |
| 233 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 234 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 235 | // Defined in gen/gin/v8_snapshot_fingerprint.cc |
| 236 | extern const unsigned char g_natives_fingerprint[]; |
| 237 | extern const unsigned char g_snapshot_fingerprint[]; |
| 238 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 239 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 240 | enum LoadV8FileResult { |
| 241 | V8_LOAD_SUCCESS = 0, |
| 242 | V8_LOAD_FAILED_OPEN, |
| 243 | V8_LOAD_FAILED_MAP, |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 244 | V8_LOAD_FAILED_VERIFY, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 245 | V8_LOAD_MAX_VALUE |
| 246 | }; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 247 | |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 248 | static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region, |
| 249 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 250 | const unsigned char* fingerprint, |
| 251 | #endif |
| 252 | base::MemoryMappedFile** mmapped_file_out) { |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 253 | if (file_region.first == base::kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 254 | return V8_LOAD_FAILED_OPEN; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 255 | if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 256 | return V8_LOAD_FAILED_MAP; |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 257 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 258 | if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) |
| 259 | return V8_LOAD_FAILED_VERIFY; |
| 260 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 261 | return V8_LOAD_SUCCESS; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // static |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 265 | void V8Initializer::LoadV8Snapshot() { |
| 266 | if (g_mapped_snapshot) |
| 267 | return; |
| 268 | |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 269 | OpenFileIfNecessary(kSnapshotFileName); |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 270 | LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName), |
| 271 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 272 | g_snapshot_fingerprint, |
| 273 | #endif |
| 274 | &g_mapped_snapshot); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 275 | // V8 can't start up without the source of the natives, but it can |
| 276 | // start up (slower) without the snapshot. |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 277 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 278 | V8_LOAD_MAX_VALUE); |
| 279 | } |
| 280 | |
| 281 | void V8Initializer::LoadV8Natives() { |
| 282 | if (g_mapped_natives) |
| 283 | return; |
| 284 | |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 285 | OpenFileIfNecessary(kNativesFileName); |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 286 | LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName), |
| 287 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 288 | g_natives_fingerprint, |
| 289 | #endif |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 290 | &g_mapped_natives); |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 291 | if (result != V8_LOAD_SUCCESS) { |
| 292 | LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
| 293 | << static_cast<int>(result); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // static |
| 298 | void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 299 | int64_t snapshot_offset, |
| 300 | int64_t snapshot_size) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 301 | if (g_mapped_snapshot) |
| 302 | return; |
| 303 | |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 304 | if (snapshot_pf == base::kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 305 | return; |
| 306 | |
| 307 | base::MemoryMappedFile::Region snapshot_region = |
| 308 | base::MemoryMappedFile::Region::kWholeFile; |
| 309 | if (snapshot_size != 0 || snapshot_offset != 0) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 310 | snapshot_region.offset = snapshot_offset; |
| 311 | snapshot_region.size = snapshot_size; |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | LoadV8FileResult result = V8_LOAD_SUCCESS; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 315 | if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 316 | result = V8_LOAD_FAILED_MAP; |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 317 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 318 | if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint)) |
| 319 | result = V8_LOAD_FAILED_VERIFY; |
| 320 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
mnaganov | d6920a6 | 2015-08-25 17:20:31 | [diff] [blame] | 321 | if (result == V8_LOAD_SUCCESS) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 322 | g_opened_files.Get()[kSnapshotFileName] = |
| 323 | std::make_pair(snapshot_pf, snapshot_region); |
mnaganov | d6920a6 | 2015-08-25 17:20:31 | [diff] [blame] | 324 | } |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 325 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 326 | V8_LOAD_MAX_VALUE); |
| 327 | } |
| 328 | |
| 329 | // static |
| 330 | void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 331 | int64_t natives_offset, |
| 332 | int64_t natives_size) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 333 | if (g_mapped_natives) |
| 334 | return; |
| 335 | |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 336 | CHECK_NE(natives_pf, base::kInvalidPlatformFile); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 337 | |
| 338 | base::MemoryMappedFile::Region natives_region = |
| 339 | base::MemoryMappedFile::Region::kWholeFile; |
| 340 | if (natives_size != 0 || natives_offset != 0) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 341 | natives_region.offset = natives_offset; |
| 342 | natives_region.size = natives_size; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 343 | } |
| 344 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 345 | if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 346 | LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 347 | } |
oth | 2a653192 | 2016-07-13 10:06:14 | [diff] [blame] | 348 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 349 | if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) { |
| 350 | LOG(FATAL) << "Couldn't verify contents of v8 natives data file"; |
| 351 | } |
| 352 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 353 | g_opened_files.Get()[kNativesFileName] = |
| 354 | std::make_pair(natives_pf, natives_region); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 355 | } |
| 356 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 357 | // static |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 358 | base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( |
| 359 | base::MemoryMappedFile::Region* region_out) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 360 | const OpenedFileMap::mapped_type& opened = |
| 361 | OpenFileIfNecessary(kNativesFileName); |
| 362 | *region_out = opened.second; |
| 363 | return opened.first; |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 364 | } |
| 365 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 366 | // static |
| 367 | base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 368 | base::MemoryMappedFile::Region* region_out) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 369 | const OpenedFileMap::mapped_type& opened = |
| 370 | OpenFileIfNecessary(kSnapshotFileName); |
| 371 | *region_out = opened.second; |
| 372 | return opened.first; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 373 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 374 | |
| 375 | #if defined(OS_ANDROID) |
| 376 | // static |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 377 | base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 378 | base::MemoryMappedFile::Region* region_out, |
| 379 | bool abi_32_bit) { |
| 380 | const char* snapshot_file = |
| 381 | abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64; |
| 382 | const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file); |
| 383 | *region_out = opened.second; |
| 384 | return opened.first; |
| 385 | } |
mrunal.kapade | ded427a | 2016-04-26 00:10:44 | [diff] [blame] | 386 | |
| 387 | // static |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 388 | base::FilePath V8Initializer::GetNativesFilePath() { |
mrunal.kapade | ded427a | 2016-04-26 00:10:44 | [diff] [blame] | 389 | base::FilePath path; |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 390 | GetV8FilePath(kNativesFileName, &path); |
mrunal.kapade | ded427a | 2016-04-26 00:10:44 | [diff] [blame] | 391 | return path; |
| 392 | } |
| 393 | |
| 394 | // static |
| 395 | base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) { |
| 396 | base::FilePath path; |
| 397 | GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path); |
| 398 | return path; |
| 399 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 400 | #endif // defined(OS_ANDROID) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 401 | #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 402 | |
| 403 | // static |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 404 | void V8Initializer::Initialize(IsolateHolder::ScriptMode mode, |
| 405 | IsolateHolder::V8ExtrasMode v8_extras_mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 406 | static bool v8_is_initialized = false; |
| 407 | if (v8_is_initialized) |
| 408 | return; |
| 409 | |
| 410 | v8::V8::InitializePlatform(V8Platform::Get()); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 411 | |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 412 | if (IsolateHolder::kStrictMode == mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 413 | static const char use_strict[] = "--use_strict"; |
| 414 | v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); |
| 415 | } |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 416 | if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) { |
| 417 | static const char flag[] = "--experimental_extras"; |
| 418 | v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1); |
| 419 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 420 | |
rmcilroy | 462e47e | 2016-06-24 20:45:08 | [diff] [blame] | 421 | const char* ignition_enabled_crash_key = "N"; |
rmcilroy | 2c39aa4 | 2016-08-24 12:27:22 | [diff] [blame] | 422 | if (base::FeatureList::IsEnabled(features::kV8Ignition)) { |
| 423 | ignition_enabled_crash_key = "Y"; |
| 424 | std::string flag("--ignition-staging"); |
| 425 | v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size())); |
| 426 | } else if (base::FeatureList::IsEnabled(features::kV8IgnitionLowEnd) && |
| 427 | base::SysInfo::IsLowEndDevice()) { |
rmcilroy | 462e47e | 2016-06-24 20:45:08 | [diff] [blame] | 428 | ignition_enabled_crash_key = "Y"; |
rmcilroy | fe515ad | 2016-04-08 17:59:10 | [diff] [blame] | 429 | std::string flag("--ignition"); |
| 430 | v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size())); |
| 431 | } |
rmcilroy | 462e47e | 2016-06-24 20:45:08 | [diff] [blame] | 432 | static const char kIgnitionEnabledKey[] = "v8-ignition"; |
| 433 | base::debug::SetCrashKeyValue(kIgnitionEnabledKey, |
| 434 | ignition_enabled_crash_key); |
rmcilroy | fe515ad | 2016-04-08 17:59:10 | [diff] [blame] | 435 | |
rmcilroy | c7caaca | 2016-05-12 17:29:40 | [diff] [blame] | 436 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 437 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 438 | v8::StartupData natives; |
| 439 | natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 440 | natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
| 441 | v8::V8::SetNativesDataBlob(&natives); |
| 442 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 443 | if (g_mapped_snapshot != NULL) { |
| 444 | v8::StartupData snapshot; |
| 445 | snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 446 | snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
| 447 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 448 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 449 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 450 | |
| 451 | v8::V8::SetEntropySource(&GenerateEntropy); |
| 452 | v8::V8::Initialize(); |
| 453 | |
| 454 | v8_is_initialized = true; |
| 455 | } |
| 456 | |
| 457 | // static |
| 458 | void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
| 459 | int* natives_size_out, |
| 460 | const char** snapshot_data_out, |
| 461 | int* snapshot_size_out) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 462 | if (g_mapped_natives) { |
| 463 | *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 464 | *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
| 465 | } else { |
| 466 | *natives_data_out = NULL; |
| 467 | *natives_size_out = 0; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 468 | } |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 469 | if (g_mapped_snapshot) { |
| 470 | *snapshot_data_out = |
| 471 | reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 472 | *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
| 473 | } else { |
| 474 | *snapshot_data_out = NULL; |
| 475 | *snapshot_size_out = 0; |
| 476 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 477 | } |
| 478 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 479 | } // namespace gin |