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