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