blob: 56011d9ff5be2e8152d01c7f41249dad6d441cc4 [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
27#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrieve6f3002d2015-06-19 16:49:0628#if defined(OS_ANDROID)
29#include "base/android/apk_assets.h"
Hitoshi Yoshidaf6a50162017-08-17 08:13:5930#endif
Max Morinf0d13c92017-08-17 10:04:5931#if defined(OS_MACOSX)
32#include "base/mac/foundation_util.h"
33#endif // OS_MACOSX
34#include "base/path_service.h"
oth05c26fde2015-04-05 14:30:5735#endif // V8_USE_EXTERNAL_STARTUP_DATA
36
37namespace gin {
38
39namespace {
40
agrievefd2d44ab2015-06-19 04:33:0341// None of these globals are ever freed nor closed.
oth05c26fde2015-04-05 14:30:5742base::MemoryMappedFile* g_mapped_natives = nullptr;
43base::MemoryMappedFile* g_mapped_snapshot = nullptr;
44
Max Morinf0d13c92017-08-17 10:04:5945#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:0346
agrievefd2d44ab2015-06-19 04:33:0347// File handles intentionally never closed. Not using File here because its
48// Windows implementation guards against two instances owning the same
49// PlatformFile (which we allow since we know it is never freed).
Max Morinf0d13c92017-08-17 10:04:5950typedef std::map<const char*,
51 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>
52 OpenedFileMap;
53static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
tobiasjsb20016272016-02-10 11:54:1254 LAZY_INSTANCE_INITIALIZER;
55
56OpenedFileMap::mapped_type& GetOpenedFile(const char* file) {
57 OpenedFileMap& opened_files(g_opened_files.Get());
58 if (opened_files.find(file) == opened_files.end()) {
rockot5d4213ff2016-05-25 19:07:1059 opened_files[file] = std::make_pair(base::kInvalidPlatformFile,
60 base::MemoryMappedFile::Region());
tobiasjsb20016272016-02-10 11:54:1261 }
62 return opened_files[file];
63}
agrievefd2d44ab2015-06-19 04:33:0364
michaelbai020375882016-06-21 16:08:1565const char kNativesFileName[] = "natives_blob.bin";
66
michaelbai016306732015-11-03 19:48:0067#if defined(OS_ANDROID)
tobiasjsb20016272016-02-10 11:54:1268const char kSnapshotFileName64[] = "snapshot_blob_64.bin";
tobiasjsb20016272016-02-10 11:54:1269const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
70
71#if defined(__LP64__)
tobiasjsb20016272016-02-10 11:54:1272#define kSnapshotFileName kSnapshotFileName64
michaelbai016306732015-11-03 19:48:0073#else
tobiasjsb20016272016-02-10 11:54:1274#define kSnapshotFileName kSnapshotFileName32
75#endif
michaelbai016306732015-11-03 19:48:0076
77#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5878const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0079#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5880
erikcorryc94eff12015-06-08 11:29:1681void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5882#if !defined(OS_MACOSX)
83 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0684#if defined(OS_ANDROID)
85 // This is the path within the .apk.
86 data_path = base::FilePath(FILE_PATH_LITERAL("assets"));
87#elif defined(OS_POSIX)
88 PathService::Get(base::DIR_EXE, &data_path);
89#elif defined(OS_WIN)
90 PathService::Get(base::DIR_MODULE, &data_path);
91#endif
rmcilroy54fab5e2015-04-06 21:14:5892 DCHECK(!data_path.empty());
93
erikcorryc94eff12015-06-08 11:29:1694 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:5895#else // !defined(OS_MACOSX)
96 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:1697 base::SysUTF8ToCFStringRef(file_name));
98 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:5899#endif // !defined(OS_MACOSX)
Max Morinf0d13c92017-08-17 10:04:59100 DCHECK(!path_out->empty());
rmcilroy54fab5e2015-04-06 21:14:58101}
102
Max Morinf0d13c92017-08-17 10:04:59103static bool MapV8File(base::PlatformFile platform_file,
104 base::MemoryMappedFile::Region region,
105 base::MemoryMappedFile** mmapped_file_out) {
erikcorryc94eff12015-06-08 11:29:16106 DCHECK(*mmapped_file_out == NULL);
mostynbc862da82016-04-03 15:54:33107 std::unique_ptr<base::MemoryMappedFile> mmapped_file(
108 new base::MemoryMappedFile());
agrievefd2d44ab2015-06-19 04:33:03109 if (mmapped_file->Initialize(base::File(platform_file), region)) {
110 *mmapped_file_out = mmapped_file.release();
111 return true;
oth05c26fde2015-04-05 14:30:57112 }
agrievefd2d44ab2015-06-19 04:33:03113 return false;
oth05c26fde2015-04-05 14:30:57114}
115
agrievefd2d44ab2015-06-19 04:33:03116base::PlatformFile OpenV8File(const char* file_name,
117 base::MemoryMappedFile::Region* region_out) {
oth575f7fb52015-05-08 17:35:00118 // Re-try logic here is motivated by http://crbug.com/479537
119 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
120
121 // These match tools/metrics/histograms.xml
122 enum OpenV8FileResult {
123 OPENED = 0,
124 OPENED_RETRY,
125 FAILED_IN_USE,
126 FAILED_OTHER,
127 MAX_VALUE
128 };
agrievefd2d44ab2015-06-19 04:33:03129 base::FilePath path;
130 GetV8FilePath(file_name, &path);
131
agrieve6f3002d2015-06-19 16:49:06132#if defined(OS_ANDROID)
133 base::File file(base::android::OpenApkAsset(path.value(), region_out));
134 OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED
135 : OpenV8FileResult::FAILED_OTHER;
136#else
137 // Re-try logic here is motivated by http://crbug.com/479537
138 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
139 const int kMaxOpenAttempts = 5;
140 const int kOpenRetryDelayMillis = 250;
141
oth575f7fb52015-05-08 17:35:00142 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE;
agrievefd2d44ab2015-06-19 04:33:03143 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
144 base::File file;
oth575f7fb52015-05-08 17:35:00145 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) {
146 file.Initialize(path, flags);
147 if (file.IsValid()) {
agrievefd2d44ab2015-06-19 04:33:03148 *region_out = base::MemoryMappedFile::Region::kWholeFile;
oth575f7fb52015-05-08 17:35:00149 if (attempt == 0) {
150 result = OpenV8FileResult::OPENED;
151 break;
152 } else {
153 result = OpenV8FileResult::OPENED_RETRY;
154 break;
155 }
156 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) {
157 result = OpenV8FileResult::FAILED_OTHER;
oth29c7ed92015-06-19 14:40:00158#ifdef OS_WIN
159 // TODO(oth): temporary diagnostics for http://crbug.com/479537
160 std::string narrow(kNativesFileName);
161 base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end());
162 if (path.BaseName().value() == nativesBlob) {
163 base::File::Error file_error = file.error_details();
164 base::debug::Alias(&file_error);
165 LOG(FATAL) << "Failed to open V8 file '" << path.value()
166 << "' (reason: " << file.error_details() << ")";
167 }
168#endif // OS_WIN
oth575f7fb52015-05-08 17:35:00169 break;
170 } else if (kMaxOpenAttempts - 1 != attempt) {
171 base::PlatformThread::Sleep(
172 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis));
173 }
174 }
agrieve6f3002d2015-06-19 16:49:06175#endif // defined(OS_ANDROID)
oth575f7fb52015-05-08 17:35:00176
177 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
178 result,
179 OpenV8FileResult::MAX_VALUE);
agrievefd2d44ab2015-06-19 04:33:03180 return file.TakePlatformFile();
181}
oth575f7fb52015-05-08 17:35:00182
Max Morinf0d13c92017-08-17 10:04:59183static const OpenedFileMap::mapped_type OpenFileIfNecessary(
184 const char* file_name) {
tobiasjsb20016272016-02-10 11:54:12185 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
rockot5d4213ff2016-05-25 19:07:10186 if (opened.first == base::kInvalidPlatformFile) {
tobiasjsb20016272016-02-10 11:54:12187 opened.first = OpenV8File(file_name, &opened.second);
agrievefd2d44ab2015-06-19 04:33:03188 }
tobiasjsb20016272016-02-10 11:54:12189 return opened;
oth575f7fb52015-05-08 17:35:00190}
191
Max Morinf0d13c92017-08-17 10:04:59192#endif // V8_USE_EXTERNAL_STARTUP_DATA
193
rmcilroy54fab5e2015-04-06 21:14:58194bool GenerateEntropy(unsigned char* buffer, size_t amount) {
195 base::RandBytes(buffer, amount);
196 return true;
197}
198
Max Morinf0d13c92017-08-17 10:04:59199} // namespace
200
201#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
202
203namespace {
204
erikcorryc94eff12015-06-08 11:29:16205enum LoadV8FileResult {
206 V8_LOAD_SUCCESS = 0,
207 V8_LOAD_FAILED_OPEN,
208 V8_LOAD_FAILED_MAP,
jcivellidbe3dec2017-02-07 16:58:23209 V8_LOAD_FAILED_VERIFY, // Deprecated.
erikcorryc94eff12015-06-08 11:29:16210 V8_LOAD_MAX_VALUE
211};
oth575f7fb52015-05-08 17:35:00212
Max Morinf0d13c92017-08-17 10:04:59213static LoadV8FileResult MapOpenedFile(
214 const OpenedFileMap::mapped_type& file_region,
215 base::MemoryMappedFile** mmapped_file_out) {
rockot5d4213ff2016-05-25 19:07:10216 if (file_region.first == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16217 return V8_LOAD_FAILED_OPEN;
tobiasjsb20016272016-02-10 11:54:12218 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16219 return V8_LOAD_FAILED_MAP;
erikcorryc94eff12015-06-08 11:29:16220 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57221}
222
jcivellidbe3dec2017-02-07 16:58:23223} // namespace
224
oth05c26fde2015-04-05 14:30:57225// static
erikcorryc94eff12015-06-08 11:29:16226void V8Initializer::LoadV8Snapshot() {
227 if (g_mapped_snapshot)
228 return;
229
tobiasjsb20016272016-02-10 11:54:12230 OpenFileIfNecessary(kSnapshotFileName);
jcivellidbe3dec2017-02-07 16:58:23231 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kSnapshotFileName),
232 &g_mapped_snapshot);
agrievefd2d44ab2015-06-19 04:33:03233 // V8 can't start up without the source of the natives, but it can
234 // start up (slower) without the snapshot.
erikcorryc94eff12015-06-08 11:29:16235 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
236 V8_LOAD_MAX_VALUE);
237}
238
239void V8Initializer::LoadV8Natives() {
240 if (g_mapped_natives)
241 return;
242
tobiasjsb20016272016-02-10 11:54:12243 OpenFileIfNecessary(kNativesFileName);
jcivellidbe3dec2017-02-07 16:58:23244 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kNativesFileName),
245 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16246 if (result != V8_LOAD_SUCCESS) {
247 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is "
248 << static_cast<int>(result);
249 }
250}
251
252// static
253void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
avi90e658dd2015-12-21 07:16:19254 int64_t snapshot_offset,
255 int64_t snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16256 if (g_mapped_snapshot)
257 return;
258
rockot5d4213ff2016-05-25 19:07:10259 if (snapshot_pf == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16260 return;
261
262 base::MemoryMappedFile::Region snapshot_region =
263 base::MemoryMappedFile::Region::kWholeFile;
264 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03265 snapshot_region.offset = snapshot_offset;
266 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16267 }
268
269 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03270 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16271 result = V8_LOAD_FAILED_MAP;
mnaganovd6920a62015-08-25 17:20:31272 if (result == V8_LOAD_SUCCESS) {
tobiasjsb20016272016-02-10 11:54:12273 g_opened_files.Get()[kSnapshotFileName] =
274 std::make_pair(snapshot_pf, snapshot_region);
mnaganovd6920a62015-08-25 17:20:31275 }
erikcorryc94eff12015-06-08 11:29:16276 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
277 V8_LOAD_MAX_VALUE);
278}
279
280// static
281void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
avi90e658dd2015-12-21 07:16:19282 int64_t natives_offset,
283 int64_t natives_size) {
erikcorryc94eff12015-06-08 11:29:16284 if (g_mapped_natives)
285 return;
286
rockot5d4213ff2016-05-25 19:07:10287 CHECK_NE(natives_pf, base::kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57288
289 base::MemoryMappedFile::Region natives_region =
290 base::MemoryMappedFile::Region::kWholeFile;
291 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03292 natives_region.offset = natives_offset;
293 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57294 }
295
agrievefd2d44ab2015-06-19 04:33:03296 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16297 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57298 }
tobiasjsb20016272016-02-10 11:54:12299 g_opened_files.Get()[kNativesFileName] =
300 std::make_pair(natives_pf, natives_region);
oth05c26fde2015-04-05 14:30:57301}
302
rmcilroy54fab5e2015-04-06 21:14:58303// static
agrievefd2d44ab2015-06-19 04:33:03304base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
305 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12306 const OpenedFileMap::mapped_type& opened =
307 OpenFileIfNecessary(kNativesFileName);
308 *region_out = opened.second;
309 return opened.first;
rmcilroy54fab5e2015-04-06 21:14:58310}
311
agrievefd2d44ab2015-06-19 04:33:03312// static
313base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
314 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12315 const OpenedFileMap::mapped_type& opened =
316 OpenFileIfNecessary(kSnapshotFileName);
317 *region_out = opened.second;
318 return opened.first;
agrievefd2d44ab2015-06-19 04:33:03319}
tobiasjsb20016272016-02-10 11:54:12320
321#if defined(OS_ANDROID)
322// static
tobiasjsb20016272016-02-10 11:54:12323base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
324 base::MemoryMappedFile::Region* region_out,
325 bool abi_32_bit) {
326 const char* snapshot_file =
327 abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64;
328 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file);
329 *region_out = opened.second;
330 return opened.first;
331}
mrunal.kapadeded427a2016-04-26 00:10:44332
333// static
michaelbai020375882016-06-21 16:08:15334base::FilePath V8Initializer::GetNativesFilePath() {
mrunal.kapadeded427a2016-04-26 00:10:44335 base::FilePath path;
michaelbai020375882016-06-21 16:08:15336 GetV8FilePath(kNativesFileName, &path);
mrunal.kapadeded427a2016-04-26 00:10:44337 return path;
338}
339
340// static
341base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) {
342 base::FilePath path;
343 GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path);
344 return path;
345}
tobiasjsb20016272016-02-10 11:54:12346#endif // defined(OS_ANDROID)
agrievefd2d44ab2015-06-19 04:33:03347#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57348
349// static
yhirano93150242015-12-07 12:28:33350void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
351 IsolateHolder::V8ExtrasMode v8_extras_mode) {
oth05c26fde2015-04-05 14:30:57352 static bool v8_is_initialized = false;
353 if (v8_is_initialized)
354 return;
355
356 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57357
yhirano93150242015-12-07 12:28:33358 if (IsolateHolder::kStrictMode == mode) {
oth05c26fde2015-04-05 14:30:57359 static const char use_strict[] = "--use_strict";
360 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
361 }
yhirano93150242015-12-07 12:28:33362 if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) {
363 static const char flag[] = "--experimental_extras";
364 v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1);
365 }
oth05c26fde2015-04-05 14:30:57366
367#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
368 v8::StartupData natives;
369 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
370 natives.raw_size = static_cast<int>(g_mapped_natives->length());
371 v8::V8::SetNativesDataBlob(&natives);
372
Max Morinf0d13c92017-08-17 10:04:59373 if (g_mapped_snapshot != NULL) {
erikcorryc94eff12015-06-08 11:29:16374 v8::StartupData snapshot;
375 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
376 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
377 v8::V8::SetSnapshotDataBlob(&snapshot);
378 }
oth05c26fde2015-04-05 14:30:57379#endif // V8_USE_EXTERNAL_STARTUP_DATA
380
381 v8::V8::SetEntropySource(&GenerateEntropy);
382 v8::V8::Initialize();
383
384 v8_is_initialized = true;
385}
386
387// static
388void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
389 int* natives_size_out,
390 const char** snapshot_data_out,
391 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16392 if (g_mapped_natives) {
393 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
394 *natives_size_out = static_cast<int>(g_mapped_natives->length());
395 } else {
396 *natives_data_out = NULL;
397 *natives_size_out = 0;
oth05c26fde2015-04-05 14:30:57398 }
erikcorryc94eff12015-06-08 11:29:16399 if (g_mapped_snapshot) {
400 *snapshot_data_out =
401 reinterpret_cast<const char*>(g_mapped_snapshot->data());
402 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
403 } else {
404 *snapshot_data_out = NULL;
405 *snapshot_size_out = 0;
406 }
oth05c26fde2015-04-05 14:30:57407}
408
oth05c26fde2015-04-05 14:30:57409} // namespace gin