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.
5 #include "gin/v8_initializer.h"
7 #include "base/basictypes.h"
8 #include "base/files/file.h"
9 #include "base/files/file_path.h"
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/rand_util.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "base/threading/platform_thread.h"
16 #include "base/time/time.h"
17 #include "crypto/sha2.h"
19 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
20 #if defined(OS_MACOSX)
21 #include "base/mac/foundation_util.h"
23 #include "base/path_service.h"
24 #endif // V8_USE_EXTERNAL_STARTUP_DATA
30 base::MemoryMappedFile
* g_mapped_natives
= nullptr;
31 base::MemoryMappedFile
* g_mapped_snapshot
= nullptr;
33 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
34 #if !defined(OS_MACOSX)
35 const int kV8SnapshotBasePathKey
=
36 #if defined(OS_ANDROID)
37 base::DIR_ANDROID_APP_DATA
;
38 #elif defined(OS_POSIX)
45 const char kNativesFileName
[] = "natives_blob.bin";
46 const char kSnapshotFileName
[] = "snapshot_blob.bin";
48 // Constants for snapshot loading retries taken from:
49 // https://support.microsoft.com/en-us/kb/316609.
50 const int kMaxOpenAttempts
= 5;
51 const int kOpenRetryDelayMillis
= 250;
53 void GetV8FilePath(const char* file_name
, base::FilePath
* path_out
) {
54 #if !defined(OS_MACOSX)
55 base::FilePath data_path
;
56 PathService::Get(kV8SnapshotBasePathKey
, &data_path
);
57 DCHECK(!data_path
.empty());
59 *path_out
= data_path
.AppendASCII(file_name
);
60 #else // !defined(OS_MACOSX)
61 base::ScopedCFTypeRef
<CFStringRef
> natives_file_name(
62 base::SysUTF8ToCFStringRef(file_name
));
63 *path_out
= base::mac::PathForFrameworkBundleResource(natives_file_name
);
64 #endif // !defined(OS_MACOSX)
65 DCHECK(!path_out
->empty());
68 static bool MapV8File(base::File file
,
69 base::MemoryMappedFile::Region region
,
70 base::MemoryMappedFile
** mmapped_file_out
) {
71 DCHECK(*mmapped_file_out
== NULL
);
72 base::MemoryMappedFile
* mmapped_file
= *mmapped_file_out
=
73 new base::MemoryMappedFile
;
74 if (!mmapped_file
->Initialize(file
.Pass(), region
)) {
76 *mmapped_file_out
= NULL
;
83 static bool OpenV8File(const base::FilePath
& path
,
86 // Re-try logic here is motivated by http://crbug.com/479537
87 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
89 // These match tools/metrics/histograms.xml
90 enum OpenV8FileResult
{
98 OpenV8FileResult result
= OpenV8FileResult::FAILED_IN_USE
;
99 for (int attempt
= 0; attempt
< kMaxOpenAttempts
; attempt
++) {
100 file
.Initialize(path
, flags
);
101 if (file
.IsValid()) {
103 result
= OpenV8FileResult::OPENED
;
106 result
= OpenV8FileResult::OPENED_RETRY
;
109 } else if (file
.error_details() != base::File::FILE_ERROR_IN_USE
) {
110 result
= OpenV8FileResult::FAILED_OTHER
;
112 } else if (kMaxOpenAttempts
- 1 != attempt
) {
113 base::PlatformThread::Sleep(
114 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis
));
118 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
120 OpenV8FileResult::MAX_VALUE
);
122 return result
== OpenV8FileResult::OPENED
123 || result
== OpenV8FileResult::OPENED_RETRY
;
126 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
127 bool VerifyV8StartupFile(base::MemoryMappedFile
** file
,
128 const unsigned char* fingerprint
) {
129 unsigned char output
[crypto::kSHA256Length
];
130 crypto::SHA256HashString(
131 base::StringPiece(reinterpret_cast<const char*>((*file
)->data()),
133 output
, sizeof(output
));
134 if (!memcmp(fingerprint
, output
, sizeof(output
))) {
141 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
142 #endif // V8_USE_EXTERNAL_STARTUP_DATA
144 bool GenerateEntropy(unsigned char* buffer
, size_t amount
) {
145 base::RandBytes(buffer
, amount
);
151 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
152 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
153 // Defined in gen/gin/v8_snapshot_fingerprint.cc
154 extern const unsigned char g_natives_fingerprint
[];
155 extern const unsigned char g_snapshot_fingerprint
[];
156 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
158 enum LoadV8FileResult
{
162 V8_LOAD_FAILED_VERIFY
,
166 static LoadV8FileResult
OpenMapVerify(
167 const char* file_name
,
168 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
169 const unsigned char* fingerprint
,
171 base::MemoryMappedFile
** mmapped_file_out
) {
173 GetV8FilePath(file_name
, &path
);
176 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
178 if (!OpenV8File(path
, flags
, file
))
179 return V8_LOAD_FAILED_OPEN
;
180 if (!MapV8File(file
.Pass(), base::MemoryMappedFile::Region::kWholeFile
,
182 return V8_LOAD_FAILED_MAP
;
183 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
184 if (!VerifyV8StartupFile(mmapped_file_out
, fingerprint
))
185 return V8_LOAD_FAILED_VERIFY
;
186 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
187 return V8_LOAD_SUCCESS
;
191 void V8Initializer::LoadV8Snapshot() {
192 if (g_mapped_snapshot
)
195 LoadV8FileResult result
= OpenMapVerify(kSnapshotFileName
,
196 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
197 g_snapshot_fingerprint
,
200 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result
,
204 void V8Initializer::LoadV8Natives() {
205 if (g_mapped_natives
)
208 LoadV8FileResult result
= OpenMapVerify(kNativesFileName
,
209 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
210 g_natives_fingerprint
,
213 if (result
!= V8_LOAD_SUCCESS
) {
214 LOG(FATAL
) << "Couldn't mmap v8 natives data file, status code is "
215 << static_cast<int>(result
);
220 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf
,
221 int64 snapshot_offset
,
222 int64 snapshot_size
) {
223 if (g_mapped_snapshot
)
226 if (snapshot_pf
== reinterpret_cast<base::PlatformFile
>(-1))
229 base::MemoryMappedFile::Region snapshot_region
=
230 base::MemoryMappedFile::Region::kWholeFile
;
231 if (snapshot_size
!= 0 || snapshot_offset
!= 0) {
233 base::MemoryMappedFile::Region(snapshot_offset
, snapshot_size
);
236 LoadV8FileResult result
= V8_LOAD_SUCCESS
;
237 if (!MapV8File(base::File(snapshot_pf
), snapshot_region
, &g_mapped_snapshot
))
238 result
= V8_LOAD_FAILED_MAP
;
239 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
240 if (!VerifyV8StartupFile(&g_mapped_snapshot
, g_snapshot_fingerprint
))
241 result
= V8_LOAD_FAILED_VERIFY
;
242 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
243 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result
,
248 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf
,
249 int64 natives_offset
,
250 int64 natives_size
) {
251 if (g_mapped_natives
)
254 CHECK_NE(natives_pf
, reinterpret_cast<base::PlatformFile
>(-1));
256 base::MemoryMappedFile::Region natives_region
=
257 base::MemoryMappedFile::Region::kWholeFile
;
258 if (natives_size
!= 0 || natives_offset
!= 0) {
260 base::MemoryMappedFile::Region(natives_offset
, natives_size
);
263 if (!MapV8File(base::File(natives_pf
), natives_region
, &g_mapped_natives
)) {
264 LOG(FATAL
) << "Couldn't mmap v8 natives data file";
266 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
267 if (!VerifyV8StartupFile(&g_mapped_natives
, g_natives_fingerprint
)) {
268 LOG(FATAL
) << "Couldn't verify contents of v8 natives data file";
270 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
274 bool V8Initializer::OpenV8FilesForChildProcesses(
275 base::PlatformFile
* natives_fd_out
,
276 base::PlatformFile
* snapshot_fd_out
) {
277 base::FilePath natives_data_path
;
278 base::FilePath snapshot_data_path
;
279 GetV8FilePath(kNativesFileName
, &natives_data_path
);
280 GetV8FilePath(kSnapshotFileName
, &snapshot_data_path
);
282 base::File natives_data_file
;
283 base::File snapshot_data_file
;
284 int file_flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
286 bool natives_success
=
287 OpenV8File(natives_data_path
, file_flags
, natives_data_file
);
288 if (natives_success
) {
289 *natives_fd_out
= natives_data_file
.TakePlatformFile();
291 bool snapshot_success
=
292 OpenV8File(snapshot_data_path
, file_flags
, snapshot_data_file
);
293 if (snapshot_success
) {
294 *snapshot_fd_out
= snapshot_data_file
.TakePlatformFile();
296 // We can start up without the snapshot file, but not without the natives.
297 return natives_success
;
300 #endif // V8_USE_EXTERNAL_STARTUP_DATA
303 void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode
) {
304 static bool v8_is_initialized
= false;
305 if (v8_is_initialized
)
308 v8::V8::InitializePlatform(V8Platform::Get());
310 if (gin::IsolateHolder::kStrictMode
== mode
) {
311 static const char use_strict
[] = "--use_strict";
312 v8::V8::SetFlagsFromString(use_strict
, sizeof(use_strict
) - 1);
315 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
316 v8::StartupData natives
;
317 natives
.data
= reinterpret_cast<const char*>(g_mapped_natives
->data());
318 natives
.raw_size
= static_cast<int>(g_mapped_natives
->length());
319 v8::V8::SetNativesDataBlob(&natives
);
321 if (g_mapped_snapshot
!= NULL
) {
322 v8::StartupData snapshot
;
323 snapshot
.data
= reinterpret_cast<const char*>(g_mapped_snapshot
->data());
324 snapshot
.raw_size
= static_cast<int>(g_mapped_snapshot
->length());
325 v8::V8::SetSnapshotDataBlob(&snapshot
);
327 #endif // V8_USE_EXTERNAL_STARTUP_DATA
329 v8::V8::SetEntropySource(&GenerateEntropy
);
330 v8::V8::Initialize();
332 v8_is_initialized
= true;
336 void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out
,
337 int* natives_size_out
,
338 const char** snapshot_data_out
,
339 int* snapshot_size_out
) {
340 if (g_mapped_natives
) {
341 *natives_data_out
= reinterpret_cast<const char*>(g_mapped_natives
->data());
342 *natives_size_out
= static_cast<int>(g_mapped_natives
->length());
344 *natives_data_out
= NULL
;
345 *natives_size_out
= 0;
347 if (g_mapped_snapshot
) {
349 reinterpret_cast<const char*>(g_mapped_snapshot
->data());
350 *snapshot_size_out
= static_cast<int>(g_mapped_snapshot
->length());
352 *snapshot_data_out
= NULL
;
353 *snapshot_size_out
= 0;