Index: gin/v8_initializer.cc |
diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc |
index 8f79d0d8693b23b01820e9906dad5da48a64bb77..0568fe77db9300785c3830a0461028d93e89b47c 100644 |
--- a/gin/v8_initializer.cc |
+++ b/gin/v8_initializer.cc |
@@ -50,53 +50,31 @@ const char kSnapshotFileName[] = "snapshot_blob.bin"; |
const int kMaxOpenAttempts = 5; |
const int kOpenRetryDelayMillis = 250; |
-void GetV8FilePaths(base::FilePath* natives_path_out, |
- base::FilePath* snapshot_path_out) { |
+void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
#if !defined(OS_MACOSX) |
base::FilePath data_path; |
PathService::Get(kV8SnapshotBasePathKey, &data_path); |
DCHECK(!data_path.empty()); |
- *natives_path_out = data_path.AppendASCII(kNativesFileName); |
- *snapshot_path_out = data_path.AppendASCII(kSnapshotFileName); |
+ *path_out = data_path.AppendASCII(file_name); |
#else // !defined(OS_MACOSX) |
base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
- base::SysUTF8ToCFStringRef(kNativesFileName)); |
- *natives_path_out = |
- base::mac::PathForFrameworkBundleResource(natives_file_name); |
- base::ScopedCFTypeRef<CFStringRef> snapshot_file_name( |
- base::SysUTF8ToCFStringRef(kSnapshotFileName)); |
- *snapshot_path_out = |
- base::mac::PathForFrameworkBundleResource(snapshot_file_name); |
- DCHECK(!natives_path_out->empty()); |
- DCHECK(!snapshot_path_out->empty()); |
+ base::SysUTF8ToCFStringRef(file_name)); |
+ *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); |
#endif // !defined(OS_MACOSX) |
+ DCHECK(!path_out->empty()); |
} |
-static bool MapV8Files(base::File natives_file, |
- base::File snapshot_file, |
- base::MemoryMappedFile::Region natives_region = |
- base::MemoryMappedFile::Region::kWholeFile, |
- base::MemoryMappedFile::Region snapshot_region = |
- base::MemoryMappedFile::Region::kWholeFile) { |
- g_mapped_natives = new base::MemoryMappedFile; |
- if (!g_mapped_natives->IsValid()) { |
- if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) { |
- delete g_mapped_natives; |
- g_mapped_natives = NULL; |
- LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
- return false; |
- } |
- } |
- |
- g_mapped_snapshot = new base::MemoryMappedFile; |
- if (!g_mapped_snapshot->IsValid()) { |
- if (!g_mapped_snapshot->Initialize(snapshot_file.Pass(), snapshot_region)) { |
- delete g_mapped_snapshot; |
- g_mapped_snapshot = NULL; |
- LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
- return false; |
- } |
+static bool MapV8File(base::File file, |
+ base::MemoryMappedFile::Region region, |
+ base::MemoryMappedFile** mmapped_file_out) { |
+ DCHECK(*mmapped_file_out == NULL); |
+ base::MemoryMappedFile* mmapped_file = *mmapped_file_out = |
+ new base::MemoryMappedFile; |
+ if (!mmapped_file->Initialize(file.Pass(), region)) { |
+ delete mmapped_file; |
+ *mmapped_file_out = NULL; |
+ return false; |
} |
return true; |
@@ -146,14 +124,19 @@ static bool OpenV8File(const base::FilePath& path, |
} |
#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
-bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file, |
- const unsigned char* fingerprint) { |
+bool VerifyV8StartupFile(base::MemoryMappedFile** file, |
+ const unsigned char* fingerprint) { |
unsigned char output[crypto::kSHA256Length]; |
crypto::SHA256HashString( |
- base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()), |
- snapshot_file->length()), |
+ base::StringPiece(reinterpret_cast<const char*>((*file)->data()), |
+ (*file)->length()), |
output, sizeof(output)); |
- return !memcmp(fingerprint, output, sizeof(output)); |
+ if (!memcmp(fingerprint, output, sizeof(output))) { |
+ return true; |
+ } |
+ delete *file; |
+ *file = NULL; |
+ return false; |
} |
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
#endif // V8_USE_EXTERNAL_STARTUP_DATA |
@@ -172,58 +155,103 @@ extern const unsigned char g_natives_fingerprint[]; |
extern const unsigned char g_snapshot_fingerprint[]; |
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
-// static |
-bool V8Initializer::LoadV8Snapshot() { |
- |
- enum LoadV8SnapshotResult { |
- SUCCESS = 0, |
- FAILED_OPEN, |
- FAILED_MAP, |
- FAILED_VERIFY, |
- MAX_VALUE |
- }; |
- |
- if (g_mapped_natives && g_mapped_snapshot) |
- return true; |
+enum LoadV8FileResult { |
+ V8_LOAD_SUCCESS = 0, |
+ V8_LOAD_FAILED_OPEN, |
+ V8_LOAD_FAILED_MAP, |
+ V8_LOAD_FAILED_VERIFY, |
+ V8_LOAD_MAX_VALUE |
+}; |
- base::FilePath natives_data_path; |
- base::FilePath snapshot_data_path; |
- GetV8FilePaths(&natives_data_path, &snapshot_data_path); |
+static LoadV8FileResult OpenMapVerify( |
+ const char* file_name, |
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
+ const unsigned char* fingerprint, |
+#endif |
+ base::MemoryMappedFile** mmapped_file_out) { |
+ base::FilePath path; |
+ GetV8FilePath(file_name, &path); |
- base::File natives_file; |
- base::File snapshot_file; |
+ base::File file; |
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
- LoadV8SnapshotResult result; |
- if (!OpenV8File(natives_data_path, flags, natives_file) || |
- !OpenV8File(snapshot_data_path, flags, snapshot_file)) { |
- result = LoadV8SnapshotResult::FAILED_OPEN; |
- } else if (!MapV8Files(natives_file.Pass(), snapshot_file.Pass())) { |
- result = LoadV8SnapshotResult::FAILED_MAP; |
+ if (!OpenV8File(path, flags, file)) |
+ return V8_LOAD_FAILED_OPEN; |
+ if (!MapV8File(file.Pass(), base::MemoryMappedFile::Region::kWholeFile, |
+ mmapped_file_out)) |
+ return V8_LOAD_FAILED_MAP; |
#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
- } else if (!VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) || |
- !VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint)) { |
- result = LoadV8SnapshotResult::FAILED_VERIFY; |
+ if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) |
+ return V8_LOAD_FAILED_VERIFY; |
#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
- } else { |
- result = LoadV8SnapshotResult::SUCCESS; |
- } |
+ return V8_LOAD_SUCCESS; |
+} |
- UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", |
- result, |
- LoadV8SnapshotResult::MAX_VALUE); |
- return result == LoadV8SnapshotResult::SUCCESS; |
+// static |
+void V8Initializer::LoadV8Snapshot() { |
+ if (g_mapped_snapshot) |
+ return; |
+ |
+ LoadV8FileResult result = OpenMapVerify(kSnapshotFileName, |
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
+ g_snapshot_fingerprint, |
+#endif |
+ &g_mapped_snapshot); |
+ UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
+ V8_LOAD_MAX_VALUE); |
+} |
+ |
+void V8Initializer::LoadV8Natives() { |
+ if (g_mapped_natives) |
+ return; |
+ |
+ LoadV8FileResult result = OpenMapVerify(kNativesFileName, |
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
+ g_natives_fingerprint, |
+#endif |
+ &g_mapped_natives); |
+ if (result != V8_LOAD_SUCCESS) { |
+ LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
+ << static_cast<int>(result); |
+ } |
} |
// static |
-bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf, |
- int64 natives_offset, |
- int64 natives_size, |
- base::PlatformFile snapshot_pf, |
+void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
int64 snapshot_offset, |
int64 snapshot_size) { |
- if (g_mapped_natives && g_mapped_snapshot) |
- return true; |
+ if (g_mapped_snapshot) |
+ return; |
+ |
+ if (snapshot_pf == reinterpret_cast<base::PlatformFile>(-1)) |
+ return; |
+ |
+ base::MemoryMappedFile::Region snapshot_region = |
+ base::MemoryMappedFile::Region::kWholeFile; |
+ if (snapshot_size != 0 || snapshot_offset != 0) { |
+ snapshot_region = |
+ base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); |
+ } |
+ |
+ LoadV8FileResult result = V8_LOAD_SUCCESS; |
+ if (!MapV8File(base::File(snapshot_pf), snapshot_region, &g_mapped_snapshot)) |
+ result = V8_LOAD_FAILED_MAP; |
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
+ if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint)) |
+ result = V8_LOAD_FAILED_VERIFY; |
+#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
+ UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
+ V8_LOAD_MAX_VALUE); |
+} |
+ |
+// static |
+void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
+ int64 natives_offset, |
+ int64 natives_size) { |
+ if (g_mapped_natives) |
+ return; |
+ |
+ CHECK_NE(natives_pf, reinterpret_cast<base::PlatformFile>(-1)); |
base::MemoryMappedFile::Region natives_region = |
base::MemoryMappedFile::Region::kWholeFile; |
@@ -232,15 +260,14 @@ bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf, |
base::MemoryMappedFile::Region(natives_offset, natives_size); |
} |
- base::MemoryMappedFile::Region snapshot_region = |
- base::MemoryMappedFile::Region::kWholeFile; |
- if (natives_size != 0 || natives_offset != 0) { |
- snapshot_region = |
- base::MemoryMappedFile::Region(snapshot_offset, snapshot_size); |
+ if (!MapV8File(base::File(natives_pf), natives_region, &g_mapped_natives)) { |
+ LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
} |
- |
- return MapV8Files(base::File(natives_pf), base::File(snapshot_pf), |
- natives_region, snapshot_region); |
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
+ if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) { |
+ LOG(FATAL) << "Couldn't verify contents of v8 natives data file"; |
+ } |
+#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
} |
// static |
@@ -249,19 +276,25 @@ bool V8Initializer::OpenV8FilesForChildProcesses( |
base::PlatformFile* snapshot_fd_out) { |
base::FilePath natives_data_path; |
base::FilePath snapshot_data_path; |
- GetV8FilePaths(&natives_data_path, &snapshot_data_path); |
+ GetV8FilePath(kNativesFileName, &natives_data_path); |
+ GetV8FilePath(kSnapshotFileName, &snapshot_data_path); |
base::File natives_data_file; |
base::File snapshot_data_file; |
int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
- bool success = OpenV8File(natives_data_path, file_flags, natives_data_file) && |
- OpenV8File(snapshot_data_path, file_flags, snapshot_data_file); |
- if (success) { |
+ bool natives_success = |
+ OpenV8File(natives_data_path, file_flags, natives_data_file); |
+ if (natives_success) { |
*natives_fd_out = natives_data_file.TakePlatformFile(); |
+ } |
+ bool snapshot_success = |
+ OpenV8File(snapshot_data_path, file_flags, snapshot_data_file); |
+ if (snapshot_success) { |
*snapshot_fd_out = snapshot_data_file.TakePlatformFile(); |
} |
- return success; |
+ // We can start up without the snapshot file, but not without the natives. |
+ return natives_success; |
} |
#endif // V8_USE_EXTERNAL_STARTUP_DATA |
@@ -285,10 +318,12 @@ void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode) { |
natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
v8::V8::SetNativesDataBlob(&natives); |
- v8::StartupData snapshot; |
- snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
- snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
- v8::V8::SetSnapshotDataBlob(&snapshot); |
+ if (g_mapped_snapshot != NULL) { |
+ v8::StartupData snapshot; |
+ snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
+ snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
+ v8::V8::SetSnapshotDataBlob(&snapshot); |
+ } |
#endif // V8_USE_EXTERNAL_STARTUP_DATA |
v8::V8::SetEntropySource(&GenerateEntropy); |
@@ -302,15 +337,21 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
int* natives_size_out, |
const char** snapshot_data_out, |
int* snapshot_size_out) { |
- if (!g_mapped_natives || !g_mapped_snapshot) { |
- *natives_data_out = *snapshot_data_out = NULL; |
- *natives_size_out = *snapshot_size_out = 0; |
- return; |
+ if (g_mapped_natives) { |
+ *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
+ *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
+ } else { |
+ *natives_data_out = NULL; |
+ *natives_size_out = 0; |
+ } |
+ if (g_mapped_snapshot) { |
+ *snapshot_data_out = |
+ reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
+ *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
+ } else { |
+ *snapshot_data_out = NULL; |
+ *snapshot_size_out = 0; |
} |
- *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
- *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
- *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
- *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
} |
} // namespace gin |