blob: 8bcc78dfa772568d61de2a46bb56e0eaaee95a9d [file] [log] [blame]
oth05c26fde2015-04-05 14:30:571// 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"
oth29c7ed92015-06-19 14:40:008#include "base/debug/alias.h"
oth05c26fde2015-04-05 14:30:579#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"
agrievefd2d44ab2015-06-19 04:33:0313#include "base/memory/scoped_ptr.h"
oth575f7fb52015-05-08 17:35:0014#include "base/metrics/histogram.h"
oth05c26fde2015-04-05 14:30:5715#include "base/rand_util.h"
16#include "base/strings/sys_string_conversions.h"
oth575f7fb52015-05-08 17:35:0017#include "base/threading/platform_thread.h"
18#include "base/time/time.h"
oth05c26fde2015-04-05 14:30:5719#include "crypto/sha2.h"
20
21#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrieve6f3002d2015-06-19 16:49:0622#if defined(OS_ANDROID)
23#include "base/android/apk_assets.h"
24#endif
oth05c26fde2015-04-05 14:30:5725#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
31namespace gin {
32
33namespace {
34
agrievefd2d44ab2015-06-19 04:33:0335// None of these globals are ever freed nor closed.
oth05c26fde2015-04-05 14:30:5736base::MemoryMappedFile* g_mapped_natives = nullptr;
37base::MemoryMappedFile* g_mapped_snapshot = nullptr;
38
39#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:0340
41const 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).
51base::PlatformFile g_natives_pf = kInvalidPlatformFile;
52base::PlatformFile g_snapshot_pf = kInvalidPlatformFile;
53base::MemoryMappedFile::Region g_natives_region;
54base::MemoryMappedFile::Region g_snapshot_region;
55
michaelbai016306732015-11-03 19:48:0056#if defined(OS_ANDROID)
57#ifdef __LP64__
58const char kNativesFileName[] = "natives_blob_64.bin";
59const char kSnapshotFileName[] = "snapshot_blob_64.bin";
60#else
61const char kNativesFileName[] = "natives_blob_32.bin";
62const char kSnapshotFileName[] = "snapshot_blob_32.bin";
63#endif // __LP64__
64
65#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5866const char kNativesFileName[] = "natives_blob.bin";
67const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0068#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5869
erikcorryc94eff12015-06-08 11:29:1670void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5871#if !defined(OS_MACOSX)
72 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0673#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
rmcilroy54fab5e2015-04-06 21:14:5881 DCHECK(!data_path.empty());
82
erikcorryc94eff12015-06-08 11:29:1683 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:5884#else // !defined(OS_MACOSX)
85 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:1686 base::SysUTF8ToCFStringRef(file_name));
87 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:5888#endif // !defined(OS_MACOSX)
erikcorryc94eff12015-06-08 11:29:1689 DCHECK(!path_out->empty());
rmcilroy54fab5e2015-04-06 21:14:5890}
91
agrievefd2d44ab2015-06-19 04:33:0392static bool MapV8File(base::PlatformFile platform_file,
erikcorryc94eff12015-06-08 11:29:1693 base::MemoryMappedFile::Region region,
94 base::MemoryMappedFile** mmapped_file_out) {
95 DCHECK(*mmapped_file_out == NULL);
agrievefd2d44ab2015-06-19 04:33:0396 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;
oth05c26fde2015-04-05 14:30:57100 }
agrievefd2d44ab2015-06-19 04:33:03101 return false;
oth05c26fde2015-04-05 14:30:57102}
103
agrievefd2d44ab2015-06-19 04:33:03104base::PlatformFile OpenV8File(const char* file_name,
105 base::MemoryMappedFile::Region* region_out) {
oth575f7fb52015-05-08 17:35:00106 // 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 };
agrievefd2d44ab2015-06-19 04:33:03117 base::FilePath path;
118 GetV8FilePath(file_name, &path);
119
agrieve6f3002d2015-06-19 16:49:06120#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
oth575f7fb52015-05-08 17:35:00130 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE;
agrievefd2d44ab2015-06-19 04:33:03131 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
132 base::File file;
oth575f7fb52015-05-08 17:35:00133 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) {
134 file.Initialize(path, flags);
135 if (file.IsValid()) {
agrievefd2d44ab2015-06-19 04:33:03136 *region_out = base::MemoryMappedFile::Region::kWholeFile;
oth575f7fb52015-05-08 17:35:00137 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;
oth29c7ed92015-06-19 14:40:00146#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
oth575f7fb52015-05-08 17:35:00157 break;
158 } else if (kMaxOpenAttempts - 1 != attempt) {
159 base::PlatformThread::Sleep(
160 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis));
161 }
162 }
agrieve6f3002d2015-06-19 16:49:06163#endif // defined(OS_ANDROID)
oth575f7fb52015-05-08 17:35:00164
165 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
166 result,
167 OpenV8FileResult::MAX_VALUE);
agrievefd2d44ab2015-06-19 04:33:03168 return file.TakePlatformFile();
169}
oth575f7fb52015-05-08 17:35:00170
agrievefd2d44ab2015-06-19 04:33:03171void OpenNativesFileIfNecessary() {
172 if (g_natives_pf == kInvalidPlatformFile) {
173 g_natives_pf = OpenV8File(kNativesFileName, &g_natives_region);
174 }
175}
176
177void OpenSnapshotFileIfNecessary() {
178 if (g_snapshot_pf == kInvalidPlatformFile) {
179 g_snapshot_pf = OpenV8File(kSnapshotFileName, &g_snapshot_region);
180 }
oth575f7fb52015-05-08 17:35:00181}
182
oth05c26fde2015-04-05 14:30:57183#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16184bool VerifyV8StartupFile(base::MemoryMappedFile** file,
185 const unsigned char* fingerprint) {
oth05c26fde2015-04-05 14:30:57186 unsigned char output[crypto::kSHA256Length];
187 crypto::SHA256HashString(
erikcorryc94eff12015-06-08 11:29:16188 base::StringPiece(reinterpret_cast<const char*>((*file)->data()),
189 (*file)->length()),
oth05c26fde2015-04-05 14:30:57190 output, sizeof(output));
erikcorryc94eff12015-06-08 11:29:16191 if (!memcmp(fingerprint, output, sizeof(output))) {
192 return true;
193 }
oth29c7ed92015-06-19 14:40:00194
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
erikcorryc94eff12015-06-08 11:29:16208 delete *file;
209 *file = NULL;
210 return false;
oth05c26fde2015-04-05 14:30:57211}
212#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
213#endif // V8_USE_EXTERNAL_STARTUP_DATA
214
rmcilroy54fab5e2015-04-06 21:14:58215bool GenerateEntropy(unsigned char* buffer, size_t amount) {
216 base::RandBytes(buffer, amount);
217 return true;
218}
219
oth05c26fde2015-04-05 14:30:57220} // namespace
221
222#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57223#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
224// Defined in gen/gin/v8_snapshot_fingerprint.cc
225extern const unsigned char g_natives_fingerprint[];
226extern const unsigned char g_snapshot_fingerprint[];
227#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
228
erikcorryc94eff12015-06-08 11:29:16229enum 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};
oth575f7fb52015-05-08 17:35:00236
agrievefd2d44ab2015-06-19 04:33:03237static LoadV8FileResult MapVerify(base::PlatformFile platform_file,
238 const base::MemoryMappedFile::Region& region,
erikcorryc94eff12015-06-08 11:29:16239#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03240 const unsigned char* fingerprint,
erikcorryc94eff12015-06-08 11:29:16241#endif
agrievefd2d44ab2015-06-19 04:33:03242 base::MemoryMappedFile** mmapped_file_out) {
243 if (platform_file == kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16244 return V8_LOAD_FAILED_OPEN;
agrievefd2d44ab2015-06-19 04:33:03245 if (!MapV8File(platform_file, region, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16246 return V8_LOAD_FAILED_MAP;
oth05c26fde2015-04-05 14:30:57247#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16248 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint))
249 return V8_LOAD_FAILED_VERIFY;
oth05c26fde2015-04-05 14:30:57250#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
erikcorryc94eff12015-06-08 11:29:16251 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57252}
253
254// static
erikcorryc94eff12015-06-08 11:29:16255void V8Initializer::LoadV8Snapshot() {
256 if (g_mapped_snapshot)
257 return;
258
agrievefd2d44ab2015-06-19 04:33:03259 OpenSnapshotFileIfNecessary();
260 LoadV8FileResult result = MapVerify(g_snapshot_pf, g_snapshot_region,
erikcorryc94eff12015-06-08 11:29:16261#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03262 g_snapshot_fingerprint,
erikcorryc94eff12015-06-08 11:29:16263#endif
agrievefd2d44ab2015-06-19 04:33:03264 &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.
erikcorryc94eff12015-06-08 11:29:16267 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
268 V8_LOAD_MAX_VALUE);
269}
270
271void V8Initializer::LoadV8Natives() {
272 if (g_mapped_natives)
273 return;
274
agrievefd2d44ab2015-06-19 04:33:03275 OpenNativesFileIfNecessary();
276 LoadV8FileResult result = MapVerify(g_natives_pf, g_natives_region,
erikcorryc94eff12015-06-08 11:29:16277#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03278 g_natives_fingerprint,
erikcorryc94eff12015-06-08 11:29:16279#endif
agrievefd2d44ab2015-06-19 04:33:03280 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16281 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
288void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
oth05c26fde2015-04-05 14:30:57289 int64 snapshot_offset,
290 int64 snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16291 if (g_mapped_snapshot)
292 return;
293
agrievefd2d44ab2015-06-19 04:33:03294 if (snapshot_pf == kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16295 return;
296
297 base::MemoryMappedFile::Region snapshot_region =
298 base::MemoryMappedFile::Region::kWholeFile;
299 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03300 snapshot_region.offset = snapshot_offset;
301 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16302 }
303
304 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03305 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16306 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
mnaganovd6920a62015-08-25 17:20:31311 if (result == V8_LOAD_SUCCESS) {
312 g_snapshot_pf = snapshot_pf;
313 g_snapshot_region = snapshot_region;
314 }
erikcorryc94eff12015-06-08 11:29:16315 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
316 V8_LOAD_MAX_VALUE);
317}
318
319// static
320void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
321 int64 natives_offset,
322 int64 natives_size) {
323 if (g_mapped_natives)
324 return;
325
agrievefd2d44ab2015-06-19 04:33:03326 CHECK_NE(natives_pf, kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57327
328 base::MemoryMappedFile::Region natives_region =
329 base::MemoryMappedFile::Region::kWholeFile;
330 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03331 natives_region.offset = natives_offset;
332 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57333 }
334
agrievefd2d44ab2015-06-19 04:33:03335 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16336 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57337 }
erikcorryc94eff12015-06-08 11:29:16338#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
mnaganovd6920a62015-08-25 17:20:31343 g_natives_pf = natives_pf;
344 g_natives_region = natives_region;
oth05c26fde2015-04-05 14:30:57345}
346
rmcilroy54fab5e2015-04-06 21:14:58347// static
agrievefd2d44ab2015-06-19 04:33:03348base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
349 base::MemoryMappedFile::Region* region_out) {
350 OpenNativesFileIfNecessary();
351 *region_out = g_natives_region;
352 return g_natives_pf;
rmcilroy54fab5e2015-04-06 21:14:58353}
354
agrievefd2d44ab2015-06-19 04:33:03355// static
356base::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)
oth05c26fde2015-04-05 14:30:57363
364// static
jochena0b121b2015-04-30 12:56:27365void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode) {
oth05c26fde2015-04-05 14:30:57366 static bool v8_is_initialized = false;
367 if (v8_is_initialized)
368 return;
369
370 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57371
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
erikcorryc94eff12015-06-08 11:29:16383 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 }
oth05c26fde2015-04-05 14:30:57389#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
398void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
399 int* natives_size_out,
400 const char** snapshot_data_out,
401 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16402 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;
oth05c26fde2015-04-05 14:30:57408 }
erikcorryc94eff12015-06-08 11:29:16409 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 }
oth05c26fde2015-04-05 14:30:57417}
418
419} // namespace gin