OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/v8_initializer.h" | 5 #include "gin/v8_initializer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 26 matching lines...) Expand all Loading... |
37 namespace gin { | 37 namespace gin { |
38 | 38 |
39 namespace { | 39 namespace { |
40 | 40 |
41 // None of these globals are ever freed nor closed. | 41 // None of these globals are ever freed nor closed. |
42 base::MemoryMappedFile* g_mapped_natives = nullptr; | 42 base::MemoryMappedFile* g_mapped_natives = nullptr; |
43 base::MemoryMappedFile* g_mapped_snapshot = nullptr; | 43 base::MemoryMappedFile* g_mapped_snapshot = nullptr; |
44 | 44 |
45 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 45 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
46 | 46 |
47 const base::PlatformFile kInvalidPlatformFile = | |
48 #if defined(OS_WIN) | |
49 INVALID_HANDLE_VALUE; | |
50 #else | |
51 -1; | |
52 #endif | |
53 | |
54 // File handles intentionally never closed. Not using File here because its | 47 // File handles intentionally never closed. Not using File here because its |
55 // Windows implementation guards against two instances owning the same | 48 // Windows implementation guards against two instances owning the same |
56 // PlatformFile (which we allow since we know it is never freed). | 49 // PlatformFile (which we allow since we know it is never freed). |
57 typedef std::map<const char*, | 50 typedef std::map<const char*, |
58 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>> | 51 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>> |
59 OpenedFileMap; | 52 OpenedFileMap; |
60 static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files = | 53 static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files = |
61 LAZY_INSTANCE_INITIALIZER; | 54 LAZY_INSTANCE_INITIALIZER; |
62 | 55 |
63 OpenedFileMap::mapped_type& GetOpenedFile(const char* file) { | 56 OpenedFileMap::mapped_type& GetOpenedFile(const char* file) { |
64 OpenedFileMap& opened_files(g_opened_files.Get()); | 57 OpenedFileMap& opened_files(g_opened_files.Get()); |
65 if (opened_files.find(file) == opened_files.end()) { | 58 if (opened_files.find(file) == opened_files.end()) { |
66 opened_files[file] = | 59 opened_files[file] = std::make_pair(base::kInvalidPlatformFile, |
67 std::make_pair(kInvalidPlatformFile, base::MemoryMappedFile::Region()); | 60 base::MemoryMappedFile::Region()); |
68 } | 61 } |
69 return opened_files[file]; | 62 return opened_files[file]; |
70 } | 63 } |
71 | 64 |
72 #if defined(OS_ANDROID) | 65 #if defined(OS_ANDROID) |
73 const char kNativesFileName64[] = "natives_blob_64.bin"; | 66 const char kNativesFileName64[] = "natives_blob_64.bin"; |
74 const char kSnapshotFileName64[] = "snapshot_blob_64.bin"; | 67 const char kSnapshotFileName64[] = "snapshot_blob_64.bin"; |
75 const char kNativesFileName32[] = "natives_blob_32.bin"; | 68 const char kNativesFileName32[] = "natives_blob_32.bin"; |
76 const char kSnapshotFileName32[] = "snapshot_blob_32.bin"; | 69 const char kSnapshotFileName32[] = "snapshot_blob_32.bin"; |
77 | 70 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 | 179 |
187 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", | 180 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
188 result, | 181 result, |
189 OpenV8FileResult::MAX_VALUE); | 182 OpenV8FileResult::MAX_VALUE); |
190 return file.TakePlatformFile(); | 183 return file.TakePlatformFile(); |
191 } | 184 } |
192 | 185 |
193 static const OpenedFileMap::mapped_type OpenFileIfNecessary( | 186 static const OpenedFileMap::mapped_type OpenFileIfNecessary( |
194 const char* file_name) { | 187 const char* file_name) { |
195 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name); | 188 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name); |
196 if (opened.first == kInvalidPlatformFile) { | 189 if (opened.first == base::kInvalidPlatformFile) { |
197 opened.first = OpenV8File(file_name, &opened.second); | 190 opened.first = OpenV8File(file_name, &opened.second); |
198 } | 191 } |
199 return opened; | 192 return opened; |
200 } | 193 } |
201 | 194 |
202 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 195 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
203 bool VerifyV8StartupFile(base::MemoryMappedFile** file, | 196 bool VerifyV8StartupFile(base::MemoryMappedFile** file, |
204 const unsigned char* fingerprint) { | 197 const unsigned char* fingerprint) { |
205 unsigned char output[crypto::kSHA256Length]; | 198 unsigned char output[crypto::kSHA256Length]; |
206 crypto::SHA256HashString( | 199 crypto::SHA256HashString( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 V8_LOAD_FAILED_MAP, | 244 V8_LOAD_FAILED_MAP, |
252 V8_LOAD_FAILED_VERIFY, | 245 V8_LOAD_FAILED_VERIFY, |
253 V8_LOAD_MAX_VALUE | 246 V8_LOAD_MAX_VALUE |
254 }; | 247 }; |
255 | 248 |
256 static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region, | 249 static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region, |
257 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 250 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
258 const unsigned char* fingerprint, | 251 const unsigned char* fingerprint, |
259 #endif | 252 #endif |
260 base::MemoryMappedFile** mmapped_file_out) { | 253 base::MemoryMappedFile** mmapped_file_out) { |
261 if (file_region.first == kInvalidPlatformFile) | 254 if (file_region.first == base::kInvalidPlatformFile) |
262 return V8_LOAD_FAILED_OPEN; | 255 return V8_LOAD_FAILED_OPEN; |
263 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) | 256 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) |
264 return V8_LOAD_FAILED_MAP; | 257 return V8_LOAD_FAILED_MAP; |
265 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 258 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
266 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) | 259 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) |
267 return V8_LOAD_FAILED_VERIFY; | 260 return V8_LOAD_FAILED_VERIFY; |
268 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA | 261 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
269 return V8_LOAD_SUCCESS; | 262 return V8_LOAD_SUCCESS; |
270 } | 263 } |
271 | 264 |
(...skipping 30 matching lines...) Expand all Loading... |
302 } | 295 } |
303 } | 296 } |
304 | 297 |
305 // static | 298 // static |
306 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, | 299 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
307 int64_t snapshot_offset, | 300 int64_t snapshot_offset, |
308 int64_t snapshot_size) { | 301 int64_t snapshot_size) { |
309 if (g_mapped_snapshot) | 302 if (g_mapped_snapshot) |
310 return; | 303 return; |
311 | 304 |
312 if (snapshot_pf == kInvalidPlatformFile) | 305 if (snapshot_pf == base::kInvalidPlatformFile) |
313 return; | 306 return; |
314 | 307 |
315 base::MemoryMappedFile::Region snapshot_region = | 308 base::MemoryMappedFile::Region snapshot_region = |
316 base::MemoryMappedFile::Region::kWholeFile; | 309 base::MemoryMappedFile::Region::kWholeFile; |
317 if (snapshot_size != 0 || snapshot_offset != 0) { | 310 if (snapshot_size != 0 || snapshot_offset != 0) { |
318 snapshot_region.offset = snapshot_offset; | 311 snapshot_region.offset = snapshot_offset; |
319 snapshot_region.size = snapshot_size; | 312 snapshot_region.size = snapshot_size; |
320 } | 313 } |
321 | 314 |
322 LoadV8FileResult result = V8_LOAD_SUCCESS; | 315 LoadV8FileResult result = V8_LOAD_SUCCESS; |
(...skipping 11 matching lines...) Expand all Loading... |
334 V8_LOAD_MAX_VALUE); | 327 V8_LOAD_MAX_VALUE); |
335 } | 328 } |
336 | 329 |
337 // static | 330 // static |
338 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, | 331 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
339 int64_t natives_offset, | 332 int64_t natives_offset, |
340 int64_t natives_size) { | 333 int64_t natives_size) { |
341 if (g_mapped_natives) | 334 if (g_mapped_natives) |
342 return; | 335 return; |
343 | 336 |
344 CHECK_NE(natives_pf, kInvalidPlatformFile); | 337 CHECK_NE(natives_pf, base::kInvalidPlatformFile); |
345 | 338 |
346 base::MemoryMappedFile::Region natives_region = | 339 base::MemoryMappedFile::Region natives_region = |
347 base::MemoryMappedFile::Region::kWholeFile; | 340 base::MemoryMappedFile::Region::kWholeFile; |
348 if (natives_size != 0 || natives_offset != 0) { | 341 if (natives_size != 0 || natives_offset != 0) { |
349 natives_region.offset = natives_offset; | 342 natives_region.offset = natives_offset; |
350 natives_region.size = natives_size; | 343 natives_region.size = natives_size; |
351 } | 344 } |
352 | 345 |
353 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { | 346 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
354 LOG(FATAL) << "Couldn't mmap v8 natives data file"; | 347 LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 *snapshot_data_out = | 484 *snapshot_data_out = |
492 reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 485 reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
493 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); | 486 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
494 } else { | 487 } else { |
495 *snapshot_data_out = NULL; | 488 *snapshot_data_out = NULL; |
496 *snapshot_size_out = 0; | 489 *snapshot_size_out = 0; |
497 } | 490 } |
498 } | 491 } |
499 | 492 |
500 } // namespace gin | 493 } // namespace gin |
OLD | NEW |