blob: 99276f4a272bd457dd9ead20d895bc895e11d8d2 [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
avi90e658dd2015-12-21 07:16:197#include <stddef.h>
8#include <stdint.h>
9
mostynbc862da82016-04-03 15:54:3310#include <memory>
11
oth29c7ed92015-06-19 14:40:0012#include "base/debug/alias.h"
rmcilroy462e47e2016-06-24 20:45:0813#include "base/debug/crash_logging.h"
rmcilroyfe515ad2016-04-08 17:59:1014#include "base/feature_list.h"
oth05c26fde2015-04-05 14:30:5715#include "base/files/file.h"
16#include "base/files/file_path.h"
17#include "base/files/memory_mapped_file.h"
tobiasjsb20016272016-02-10 11:54:1218#include "base/lazy_instance.h"
oth05c26fde2015-04-05 14:30:5719#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0820#include "base/metrics/histogram_macros.h"
oth05c26fde2015-04-05 14:30:5721#include "base/rand_util.h"
22#include "base/strings/sys_string_conversions.h"
rmcilroy542f61c2016-06-06 16:08:1923#include "base/sys_info.h"
oth575f7fb52015-05-08 17:35:0024#include "base/threading/platform_thread.h"
25#include "base/time/time.h"
oth05c26fde2015-04-05 14:30:5726#include "crypto/sha2.h"
rmcilroyfe515ad2016-04-08 17:59:1027#include "gin/public/gin_features.h"
oth05c26fde2015-04-05 14:30:5728
29#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrieve6f3002d2015-06-19 16:49:0630#if defined(OS_ANDROID)
31#include "base/android/apk_assets.h"
32#endif
oth05c26fde2015-04-05 14:30:5733#if defined(OS_MACOSX)
34#include "base/mac/foundation_util.h"
35#endif // OS_MACOSX
36#include "base/path_service.h"
37#endif // V8_USE_EXTERNAL_STARTUP_DATA
38
39namespace gin {
40
41namespace {
42
agrievefd2d44ab2015-06-19 04:33:0343// None of these globals are ever freed nor closed.
oth05c26fde2015-04-05 14:30:5744base::MemoryMappedFile* g_mapped_natives = nullptr;
45base::MemoryMappedFile* g_mapped_snapshot = nullptr;
46
47#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:0348
agrievefd2d44ab2015-06-19 04:33:0349// File handles intentionally never closed. Not using File here because its
50// Windows implementation guards against two instances owning the same
51// PlatformFile (which we allow since we know it is never freed).
tobiasjsb20016272016-02-10 11:54:1252typedef std::map<const char*,
53 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>
54 OpenedFileMap;
55static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
56 LAZY_INSTANCE_INITIALIZER;
57
58OpenedFileMap::mapped_type& GetOpenedFile(const char* file) {
59 OpenedFileMap& opened_files(g_opened_files.Get());
60 if (opened_files.find(file) == opened_files.end()) {
rockot5d4213ff2016-05-25 19:07:1061 opened_files[file] = std::make_pair(base::kInvalidPlatformFile,
62 base::MemoryMappedFile::Region());
tobiasjsb20016272016-02-10 11:54:1263 }
64 return opened_files[file];
65}
agrievefd2d44ab2015-06-19 04:33:0366
michaelbai020375882016-06-21 16:08:1567const char kNativesFileName[] = "natives_blob.bin";
68
michaelbai016306732015-11-03 19:48:0069#if defined(OS_ANDROID)
tobiasjsb20016272016-02-10 11:54:1270const char kSnapshotFileName64[] = "snapshot_blob_64.bin";
tobiasjsb20016272016-02-10 11:54:1271const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
72
73#if defined(__LP64__)
tobiasjsb20016272016-02-10 11:54:1274#define kSnapshotFileName kSnapshotFileName64
michaelbai016306732015-11-03 19:48:0075#else
tobiasjsb20016272016-02-10 11:54:1276#define kSnapshotFileName kSnapshotFileName32
77#endif
michaelbai016306732015-11-03 19:48:0078
79#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5880const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0081#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5882
erikcorryc94eff12015-06-08 11:29:1683void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5884#if !defined(OS_MACOSX)
85 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0686#if defined(OS_ANDROID)
87 // This is the path within the .apk.
88 data_path = base::FilePath(FILE_PATH_LITERAL("assets"));
89#elif defined(OS_POSIX)
90 PathService::Get(base::DIR_EXE, &data_path);
91#elif defined(OS_WIN)
92 PathService::Get(base::DIR_MODULE, &data_path);
93#endif
rmcilroy54fab5e2015-04-06 21:14:5894 DCHECK(!data_path.empty());
95
erikcorryc94eff12015-06-08 11:29:1696 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:5897#else // !defined(OS_MACOSX)
98 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:1699 base::SysUTF8ToCFStringRef(file_name));
100 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:58101#endif // !defined(OS_MACOSX)
erikcorryc94eff12015-06-08 11:29:16102 DCHECK(!path_out->empty());
rmcilroy54fab5e2015-04-06 21:14:58103}
104
agrievefd2d44ab2015-06-19 04:33:03105static bool MapV8File(base::PlatformFile platform_file,
erikcorryc94eff12015-06-08 11:29:16106 base::MemoryMappedFile::Region region,
107 base::MemoryMappedFile** mmapped_file_out) {
108 DCHECK(*mmapped_file_out == NULL);
mostynbc862da82016-04-03 15:54:33109 std::unique_ptr<base::MemoryMappedFile> mmapped_file(
110 new base::MemoryMappedFile());
agrievefd2d44ab2015-06-19 04:33:03111 if (mmapped_file->Initialize(base::File(platform_file), region)) {
112 *mmapped_file_out = mmapped_file.release();
113 return true;
oth05c26fde2015-04-05 14:30:57114 }
agrievefd2d44ab2015-06-19 04:33:03115 return false;
oth05c26fde2015-04-05 14:30:57116}
117
agrievefd2d44ab2015-06-19 04:33:03118base::PlatformFile OpenV8File(const char* file_name,
119 base::MemoryMappedFile::Region* region_out) {
oth575f7fb52015-05-08 17:35:00120 // Re-try logic here is motivated by http://crbug.com/479537
121 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
122
123 // These match tools/metrics/histograms.xml
124 enum OpenV8FileResult {
125 OPENED = 0,
126 OPENED_RETRY,
127 FAILED_IN_USE,
128 FAILED_OTHER,
129 MAX_VALUE
130 };
agrievefd2d44ab2015-06-19 04:33:03131 base::FilePath path;
132 GetV8FilePath(file_name, &path);
133
agrieve6f3002d2015-06-19 16:49:06134#if defined(OS_ANDROID)
135 base::File file(base::android::OpenApkAsset(path.value(), region_out));
136 OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED
137 : OpenV8FileResult::FAILED_OTHER;
138#else
139 // Re-try logic here is motivated by http://crbug.com/479537
140 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
141 const int kMaxOpenAttempts = 5;
142 const int kOpenRetryDelayMillis = 250;
143
oth575f7fb52015-05-08 17:35:00144 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE;
agrievefd2d44ab2015-06-19 04:33:03145 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
146 base::File file;
oth575f7fb52015-05-08 17:35:00147 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) {
148 file.Initialize(path, flags);
149 if (file.IsValid()) {
agrievefd2d44ab2015-06-19 04:33:03150 *region_out = base::MemoryMappedFile::Region::kWholeFile;
oth575f7fb52015-05-08 17:35:00151 if (attempt == 0) {
152 result = OpenV8FileResult::OPENED;
153 break;
154 } else {
155 result = OpenV8FileResult::OPENED_RETRY;
156 break;
157 }
158 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) {
159 result = OpenV8FileResult::FAILED_OTHER;
oth29c7ed92015-06-19 14:40:00160#ifdef OS_WIN
161 // TODO(oth): temporary diagnostics for http://crbug.com/479537
162 std::string narrow(kNativesFileName);
163 base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end());
164 if (path.BaseName().value() == nativesBlob) {
165 base::File::Error file_error = file.error_details();
166 base::debug::Alias(&file_error);
167 LOG(FATAL) << "Failed to open V8 file '" << path.value()
168 << "' (reason: " << file.error_details() << ")";
169 }
170#endif // OS_WIN
oth575f7fb52015-05-08 17:35:00171 break;
172 } else if (kMaxOpenAttempts - 1 != attempt) {
173 base::PlatformThread::Sleep(
174 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis));
175 }
176 }
agrieve6f3002d2015-06-19 16:49:06177#endif // defined(OS_ANDROID)
oth575f7fb52015-05-08 17:35:00178
179 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
180 result,
181 OpenV8FileResult::MAX_VALUE);
agrievefd2d44ab2015-06-19 04:33:03182 return file.TakePlatformFile();
183}
oth575f7fb52015-05-08 17:35:00184
tobiasjsb20016272016-02-10 11:54:12185static const OpenedFileMap::mapped_type OpenFileIfNecessary(
186 const char* file_name) {
187 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
rockot5d4213ff2016-05-25 19:07:10188 if (opened.first == base::kInvalidPlatformFile) {
tobiasjsb20016272016-02-10 11:54:12189 opened.first = OpenV8File(file_name, &opened.second);
agrievefd2d44ab2015-06-19 04:33:03190 }
tobiasjsb20016272016-02-10 11:54:12191 return opened;
oth575f7fb52015-05-08 17:35:00192}
193
oth2a6531922016-07-13 10:06:14194#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
195bool VerifyV8StartupFile(base::MemoryMappedFile** file,
196 const unsigned char* fingerprint) {
197 unsigned char output[crypto::kSHA256Length];
198 crypto::SHA256HashString(
199 base::StringPiece(reinterpret_cast<const char*>((*file)->data()),
200 (*file)->length()),
201 output, sizeof(output));
202 if (!memcmp(fingerprint, output, sizeof(output))) {
203 return true;
204 }
205
206 // TODO(oth): Remove this temporary diagnostics for http://crbug.com/501799
207 uint64_t input[sizeof(output)];
208 memcpy(input, fingerprint, sizeof(input));
209
210 base::debug::Alias(output);
211 base::debug::Alias(input);
212
213 const uint64_t* o64 = reinterpret_cast<const uint64_t*>(output);
214 const uint64_t* f64 = reinterpret_cast<const uint64_t*>(fingerprint);
215 LOG(FATAL) << "Natives length " << (*file)->length()
216 << " H(computed) " << o64[0] << o64[1] << o64[2] << o64[3]
217 << " H(expected) " << f64[0] << f64[1] << f64[2] << f64[3];
218
219 delete *file;
220 *file = NULL;
221 return false;
222}
223#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
oth05c26fde2015-04-05 14:30:57224#endif // V8_USE_EXTERNAL_STARTUP_DATA
225
rmcilroy54fab5e2015-04-06 21:14:58226bool GenerateEntropy(unsigned char* buffer, size_t amount) {
227 base::RandBytes(buffer, amount);
228 return true;
229}
230
oth05c26fde2015-04-05 14:30:57231} // namespace
232
233#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth2a6531922016-07-13 10:06:14234#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
235// Defined in gen/gin/v8_snapshot_fingerprint.cc
236extern const unsigned char g_natives_fingerprint[];
237extern const unsigned char g_snapshot_fingerprint[];
238#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
oth05c26fde2015-04-05 14:30:57239
erikcorryc94eff12015-06-08 11:29:16240enum LoadV8FileResult {
241 V8_LOAD_SUCCESS = 0,
242 V8_LOAD_FAILED_OPEN,
243 V8_LOAD_FAILED_MAP,
oth2a6531922016-07-13 10:06:14244 V8_LOAD_FAILED_VERIFY,
erikcorryc94eff12015-06-08 11:29:16245 V8_LOAD_MAX_VALUE
246};
oth575f7fb52015-05-08 17:35:00247
oth2a6531922016-07-13 10:06:14248static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region,
249#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
250 const unsigned char* fingerprint,
251#endif
252 base::MemoryMappedFile** mmapped_file_out) {
rockot5d4213ff2016-05-25 19:07:10253 if (file_region.first == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16254 return V8_LOAD_FAILED_OPEN;
tobiasjsb20016272016-02-10 11:54:12255 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16256 return V8_LOAD_FAILED_MAP;
oth2a6531922016-07-13 10:06:14257#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
258 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint))
259 return V8_LOAD_FAILED_VERIFY;
260#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
erikcorryc94eff12015-06-08 11:29:16261 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57262}
263
264// static
erikcorryc94eff12015-06-08 11:29:16265void V8Initializer::LoadV8Snapshot() {
266 if (g_mapped_snapshot)
267 return;
268
tobiasjsb20016272016-02-10 11:54:12269 OpenFileIfNecessary(kSnapshotFileName);
oth2a6531922016-07-13 10:06:14270 LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName),
271#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
272 g_snapshot_fingerprint,
273#endif
274 &g_mapped_snapshot);
agrievefd2d44ab2015-06-19 04:33:03275 // V8 can't start up without the source of the natives, but it can
276 // start up (slower) without the snapshot.
erikcorryc94eff12015-06-08 11:29:16277 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
278 V8_LOAD_MAX_VALUE);
279}
280
281void V8Initializer::LoadV8Natives() {
282 if (g_mapped_natives)
283 return;
284
tobiasjsb20016272016-02-10 11:54:12285 OpenFileIfNecessary(kNativesFileName);
oth2a6531922016-07-13 10:06:14286 LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName),
287#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
288 g_natives_fingerprint,
289#endif
agrievefd2d44ab2015-06-19 04:33:03290 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16291 if (result != V8_LOAD_SUCCESS) {
292 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is "
293 << static_cast<int>(result);
294 }
295}
296
297// static
298void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
avi90e658dd2015-12-21 07:16:19299 int64_t snapshot_offset,
300 int64_t snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16301 if (g_mapped_snapshot)
302 return;
303
rockot5d4213ff2016-05-25 19:07:10304 if (snapshot_pf == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16305 return;
306
307 base::MemoryMappedFile::Region snapshot_region =
308 base::MemoryMappedFile::Region::kWholeFile;
309 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03310 snapshot_region.offset = snapshot_offset;
311 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16312 }
313
314 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03315 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16316 result = V8_LOAD_FAILED_MAP;
oth2a6531922016-07-13 10:06:14317#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
318 if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint))
319 result = V8_LOAD_FAILED_VERIFY;
320#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
mnaganovd6920a62015-08-25 17:20:31321 if (result == V8_LOAD_SUCCESS) {
tobiasjsb20016272016-02-10 11:54:12322 g_opened_files.Get()[kSnapshotFileName] =
323 std::make_pair(snapshot_pf, snapshot_region);
mnaganovd6920a62015-08-25 17:20:31324 }
erikcorryc94eff12015-06-08 11:29:16325 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
326 V8_LOAD_MAX_VALUE);
327}
328
329// static
330void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
avi90e658dd2015-12-21 07:16:19331 int64_t natives_offset,
332 int64_t natives_size) {
erikcorryc94eff12015-06-08 11:29:16333 if (g_mapped_natives)
334 return;
335
rockot5d4213ff2016-05-25 19:07:10336 CHECK_NE(natives_pf, base::kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57337
338 base::MemoryMappedFile::Region natives_region =
339 base::MemoryMappedFile::Region::kWholeFile;
340 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03341 natives_region.offset = natives_offset;
342 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57343 }
344
agrievefd2d44ab2015-06-19 04:33:03345 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16346 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57347 }
oth2a6531922016-07-13 10:06:14348#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
349 if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) {
350 LOG(FATAL) << "Couldn't verify contents of v8 natives data file";
351 }
352#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
tobiasjsb20016272016-02-10 11:54:12353 g_opened_files.Get()[kNativesFileName] =
354 std::make_pair(natives_pf, natives_region);
oth05c26fde2015-04-05 14:30:57355}
356
rmcilroy54fab5e2015-04-06 21:14:58357// static
agrievefd2d44ab2015-06-19 04:33:03358base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
359 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12360 const OpenedFileMap::mapped_type& opened =
361 OpenFileIfNecessary(kNativesFileName);
362 *region_out = opened.second;
363 return opened.first;
rmcilroy54fab5e2015-04-06 21:14:58364}
365
agrievefd2d44ab2015-06-19 04:33:03366// static
367base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
368 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12369 const OpenedFileMap::mapped_type& opened =
370 OpenFileIfNecessary(kSnapshotFileName);
371 *region_out = opened.second;
372 return opened.first;
agrievefd2d44ab2015-06-19 04:33:03373}
tobiasjsb20016272016-02-10 11:54:12374
375#if defined(OS_ANDROID)
376// static
tobiasjsb20016272016-02-10 11:54:12377base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
378 base::MemoryMappedFile::Region* region_out,
379 bool abi_32_bit) {
380 const char* snapshot_file =
381 abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64;
382 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file);
383 *region_out = opened.second;
384 return opened.first;
385}
mrunal.kapadeded427a2016-04-26 00:10:44386
387// static
michaelbai020375882016-06-21 16:08:15388base::FilePath V8Initializer::GetNativesFilePath() {
mrunal.kapadeded427a2016-04-26 00:10:44389 base::FilePath path;
michaelbai020375882016-06-21 16:08:15390 GetV8FilePath(kNativesFileName, &path);
mrunal.kapadeded427a2016-04-26 00:10:44391 return path;
392}
393
394// static
395base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) {
396 base::FilePath path;
397 GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path);
398 return path;
399}
tobiasjsb20016272016-02-10 11:54:12400#endif // defined(OS_ANDROID)
agrievefd2d44ab2015-06-19 04:33:03401#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57402
403// static
yhirano93150242015-12-07 12:28:33404void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
405 IsolateHolder::V8ExtrasMode v8_extras_mode) {
oth05c26fde2015-04-05 14:30:57406 static bool v8_is_initialized = false;
407 if (v8_is_initialized)
408 return;
409
410 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57411
yhirano93150242015-12-07 12:28:33412 if (IsolateHolder::kStrictMode == mode) {
oth05c26fde2015-04-05 14:30:57413 static const char use_strict[] = "--use_strict";
414 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
415 }
yhirano93150242015-12-07 12:28:33416 if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) {
417 static const char flag[] = "--experimental_extras";
418 v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1);
419 }
oth05c26fde2015-04-05 14:30:57420
rmcilroy462e47e2016-06-24 20:45:08421 const char* ignition_enabled_crash_key = "N";
rmcilroy2c39aa42016-08-24 12:27:22422 if (base::FeatureList::IsEnabled(features::kV8Ignition)) {
423 ignition_enabled_crash_key = "Y";
424 std::string flag("--ignition-staging");
425 v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size()));
426 } else if (base::FeatureList::IsEnabled(features::kV8IgnitionLowEnd) &&
427 base::SysInfo::IsLowEndDevice()) {
rmcilroy462e47e2016-06-24 20:45:08428 ignition_enabled_crash_key = "Y";
rmcilroyfe515ad2016-04-08 17:59:10429 std::string flag("--ignition");
430 v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size()));
431 }
rmcilroy462e47e2016-06-24 20:45:08432 static const char kIgnitionEnabledKey[] = "v8-ignition";
433 base::debug::SetCrashKeyValue(kIgnitionEnabledKey,
434 ignition_enabled_crash_key);
rmcilroyfe515ad2016-04-08 17:59:10435
rmcilroyc7caaca2016-05-12 17:29:40436
oth05c26fde2015-04-05 14:30:57437#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
438 v8::StartupData natives;
439 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
440 natives.raw_size = static_cast<int>(g_mapped_natives->length());
441 v8::V8::SetNativesDataBlob(&natives);
442
erikcorryc94eff12015-06-08 11:29:16443 if (g_mapped_snapshot != NULL) {
444 v8::StartupData snapshot;
445 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
446 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
447 v8::V8::SetSnapshotDataBlob(&snapshot);
448 }
oth05c26fde2015-04-05 14:30:57449#endif // V8_USE_EXTERNAL_STARTUP_DATA
450
451 v8::V8::SetEntropySource(&GenerateEntropy);
452 v8::V8::Initialize();
453
454 v8_is_initialized = true;
455}
456
457// static
458void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
459 int* natives_size_out,
460 const char** snapshot_data_out,
461 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16462 if (g_mapped_natives) {
463 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
464 *natives_size_out = static_cast<int>(g_mapped_natives->length());
465 } else {
466 *natives_data_out = NULL;
467 *natives_size_out = 0;
oth05c26fde2015-04-05 14:30:57468 }
erikcorryc94eff12015-06-08 11:29:16469 if (g_mapped_snapshot) {
470 *snapshot_data_out =
471 reinterpret_cast<const char*>(g_mapped_snapshot->data());
472 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
473 } else {
474 *snapshot_data_out = NULL;
475 *snapshot_size_out = 0;
476 }
oth05c26fde2015-04-05 14:30:57477}
478
oth05c26fde2015-04-05 14:30:57479} // namespace gin