oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "gin/v8_initializer.h" |
| 6 | |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
oth | 29c7ed9 | 2015-06-19 14:40:00 | [diff] [blame] | 12 | #include "base/debug/alias.h" |
rmcilroy | 462e47e | 2016-06-24 20:45:08 | [diff] [blame] | 13 | #include "base/debug/crash_logging.h" |
rmcilroy | fe515ad | 2016-04-08 17:59:10 | [diff] [blame] | 14 | #include "base/feature_list.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 15 | #include "base/files/file.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/files/memory_mapped_file.h" |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 18 | #include "base/lazy_instance.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 19 | #include "base/logging.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 20 | #include "base/metrics/histogram_macros.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 21 | #include "base/path_service.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 22 | #include "base/rand_util.h" |
| 23 | #include "base/strings/sys_string_conversions.h" |
rmcilroy | 542f61c | 2016-06-06 16:08:19 | [diff] [blame] | 24 | #include "base/sys_info.h" |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 25 | #include "base/threading/platform_thread.h" |
| 26 | #include "base/time/time.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 27 | #include "build/build_config.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 28 | |
| 29 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 30 | #if defined(OS_ANDROID) |
| 31 | #include "base/android/apk_assets.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 32 | #elif defined(OS_MACOSX) |
Max Morin | f0d13c9 | 2017-08-17 10:04:59 | [diff] [blame] | 33 | #include "base/mac/foundation_util.h" |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 34 | #endif |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 35 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 36 | |
| 37 | namespace gin { |
| 38 | |
| 39 | namespace { |
| 40 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 41 | // None of these globals are ever freed nor closed. |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 42 | base::MemoryMappedFile* g_mapped_natives = nullptr; |
| 43 | base::MemoryMappedFile* g_mapped_snapshot = nullptr; |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 44 | base::MemoryMappedFile* g_mapped_v8_context_snapshot = nullptr; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 45 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 46 | const char kV8ContextSnapshotFileName[] = "v8_context_snapshot.bin"; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 47 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 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). |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 51 | using OpenedFileMap = |
| 52 | std::map<const char*, |
| 53 | std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>; |
| 54 | base::LazyInstance<OpenedFileMap>::Leaky g_opened_files = |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 55 | LAZY_INSTANCE_INITIALIZER; |
| 56 | |
| 57 | OpenedFileMap::mapped_type& GetOpenedFile(const char* file) { |
| 58 | OpenedFileMap& opened_files(g_opened_files.Get()); |
| 59 | if (opened_files.find(file) == opened_files.end()) { |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 60 | opened_files[file] = std::make_pair(base::kInvalidPlatformFile, |
| 61 | base::MemoryMappedFile::Region()); |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 62 | } |
| 63 | return opened_files[file]; |
| 64 | } |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 65 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 66 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 67 | |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 68 | const char kNativesFileName[] = "natives_blob.bin"; |
| 69 | |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 70 | #if defined(OS_ANDROID) |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 71 | const char kSnapshotFileName64[] = "snapshot_blob_64.bin"; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 72 | const char kSnapshotFileName32[] = "snapshot_blob_32.bin"; |
| 73 | |
| 74 | #if defined(__LP64__) |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 75 | #define kSnapshotFileName kSnapshotFileName64 |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 76 | #else |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 77 | #define kSnapshotFileName kSnapshotFileName32 |
| 78 | #endif |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 79 | |
| 80 | #else // defined(OS_ANDROID) |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 81 | const char kSnapshotFileName[] = "snapshot_blob.bin"; |
michaelbai | 01630673 | 2015-11-03 19:48:00 | [diff] [blame] | 82 | #endif // defined(OS_ANDROID) |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 83 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 84 | #endif // defined(V8_USE_EXTERNAL_STATUP_DATA) |
| 85 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 86 | void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 87 | #if !defined(OS_MACOSX) |
| 88 | base::FilePath data_path; |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 89 | #if defined(OS_ANDROID) |
| 90 | // This is the path within the .apk. |
| 91 | data_path = base::FilePath(FILE_PATH_LITERAL("assets")); |
| 92 | #elif defined(OS_POSIX) |
| 93 | PathService::Get(base::DIR_EXE, &data_path); |
| 94 | #elif defined(OS_WIN) |
| 95 | PathService::Get(base::DIR_MODULE, &data_path); |
| 96 | #endif |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 97 | DCHECK(!data_path.empty()); |
| 98 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 99 | *path_out = data_path.AppendASCII(file_name); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 100 | #else // !defined(OS_MACOSX) |
| 101 | base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 102 | base::SysUTF8ToCFStringRef(file_name)); |
| 103 | *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 104 | #endif // !defined(OS_MACOSX) |
| 105 | } |
| 106 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 107 | bool MapV8File(base::PlatformFile platform_file, |
| 108 | base::MemoryMappedFile::Region region, |
| 109 | base::MemoryMappedFile** mmapped_file_out) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 110 | DCHECK(*mmapped_file_out == NULL); |
mostynb | c862da8 | 2016-04-03 15:54:33 | [diff] [blame] | 111 | std::unique_ptr<base::MemoryMappedFile> mmapped_file( |
| 112 | new base::MemoryMappedFile()); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 113 | if (mmapped_file->Initialize(base::File(platform_file), region)) { |
| 114 | *mmapped_file_out = mmapped_file.release(); |
| 115 | return true; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 116 | } |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 117 | return false; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 118 | } |
| 119 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 120 | base::PlatformFile OpenV8File(const char* file_name, |
| 121 | base::MemoryMappedFile::Region* region_out) { |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 122 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 123 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 124 | |
| 125 | // These match tools/metrics/histograms.xml |
| 126 | enum OpenV8FileResult { |
| 127 | OPENED = 0, |
| 128 | OPENED_RETRY, |
| 129 | FAILED_IN_USE, |
| 130 | FAILED_OTHER, |
| 131 | MAX_VALUE |
| 132 | }; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 133 | base::FilePath path; |
| 134 | GetV8FilePath(file_name, &path); |
| 135 | |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 136 | #if defined(OS_ANDROID) |
| 137 | base::File file(base::android::OpenApkAsset(path.value(), region_out)); |
| 138 | OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED |
| 139 | : OpenV8FileResult::FAILED_OTHER; |
| 140 | #else |
| 141 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 142 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 143 | const int kMaxOpenAttempts = 5; |
| 144 | const int kOpenRetryDelayMillis = 250; |
| 145 | |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 146 | OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 147 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 148 | base::File file; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 149 | for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { |
| 150 | file.Initialize(path, flags); |
| 151 | if (file.IsValid()) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 152 | *region_out = base::MemoryMappedFile::Region::kWholeFile; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 153 | if (attempt == 0) { |
| 154 | result = OpenV8FileResult::OPENED; |
| 155 | break; |
| 156 | } else { |
| 157 | result = OpenV8FileResult::OPENED_RETRY; |
| 158 | break; |
| 159 | } |
| 160 | } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) { |
| 161 | result = OpenV8FileResult::FAILED_OTHER; |
oth | 29c7ed9 | 2015-06-19 14:40:00 | [diff] [blame] | 162 | #ifdef OS_WIN |
| 163 | // TODO(oth): temporary diagnostics for http://crbug.com/479537 |
| 164 | std::string narrow(kNativesFileName); |
| 165 | base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end()); |
| 166 | if (path.BaseName().value() == nativesBlob) { |
| 167 | base::File::Error file_error = file.error_details(); |
| 168 | base::debug::Alias(&file_error); |
| 169 | LOG(FATAL) << "Failed to open V8 file '" << path.value() |
| 170 | << "' (reason: " << file.error_details() << ")"; |
| 171 | } |
| 172 | #endif // OS_WIN |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 173 | break; |
| 174 | } else if (kMaxOpenAttempts - 1 != attempt) { |
| 175 | base::PlatformThread::Sleep( |
| 176 | base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis)); |
| 177 | } |
| 178 | } |
agrieve | 6f3002d | 2015-06-19 16:49:06 | [diff] [blame] | 179 | #endif // defined(OS_ANDROID) |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 180 | |
| 181 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
| 182 | result, |
| 183 | OpenV8FileResult::MAX_VALUE); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 184 | return file.TakePlatformFile(); |
| 185 | } |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 186 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 187 | const OpenedFileMap::mapped_type OpenFileIfNecessary(const char* file_name) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 188 | OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name); |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 189 | if (opened.first == base::kInvalidPlatformFile) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 190 | opened.first = OpenV8File(file_name, &opened.second); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 191 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 192 | return opened; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 193 | } |
| 194 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 195 | bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 196 | base::RandBytes(buffer, amount); |
| 197 | return true; |
| 198 | } |
| 199 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 200 | enum LoadV8FileResult { |
| 201 | V8_LOAD_SUCCESS = 0, |
| 202 | V8_LOAD_FAILED_OPEN, |
| 203 | V8_LOAD_FAILED_MAP, |
jcivelli | dbe3dec | 2017-02-07 16:58:23 | [diff] [blame] | 204 | V8_LOAD_FAILED_VERIFY, // Deprecated. |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 205 | V8_LOAD_MAX_VALUE |
| 206 | }; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 207 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 208 | LoadV8FileResult MapOpenedFile(const OpenedFileMap::mapped_type& file_region, |
| 209 | base::MemoryMappedFile** mmapped_file_out) { |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 210 | if (file_region.first == base::kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 211 | return V8_LOAD_FAILED_OPEN; |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 212 | if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 213 | return V8_LOAD_FAILED_MAP; |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 214 | return V8_LOAD_SUCCESS; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 215 | } |
| 216 | |
jcivelli | dbe3dec | 2017-02-07 16:58:23 | [diff] [blame] | 217 | } // namespace |
| 218 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 219 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 220 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 221 | // static |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 222 | void V8Initializer::LoadV8Snapshot() { |
| 223 | if (g_mapped_snapshot) |
| 224 | return; |
| 225 | |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 226 | OpenFileIfNecessary(kSnapshotFileName); |
jcivelli | dbe3dec | 2017-02-07 16:58:23 | [diff] [blame] | 227 | LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kSnapshotFileName), |
| 228 | &g_mapped_snapshot); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 229 | // V8 can't start up without the source of the natives, but it can |
| 230 | // start up (slower) without the snapshot. |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 231 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 232 | V8_LOAD_MAX_VALUE); |
| 233 | } |
| 234 | |
| 235 | void V8Initializer::LoadV8Natives() { |
| 236 | if (g_mapped_natives) |
| 237 | return; |
| 238 | |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 239 | OpenFileIfNecessary(kNativesFileName); |
jcivelli | dbe3dec | 2017-02-07 16:58:23 | [diff] [blame] | 240 | LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kNativesFileName), |
| 241 | &g_mapped_natives); |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 242 | if (result != V8_LOAD_SUCCESS) { |
| 243 | LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
| 244 | << static_cast<int>(result); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // static |
| 249 | void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 250 | int64_t snapshot_offset, |
| 251 | int64_t snapshot_size) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 252 | if (g_mapped_snapshot) |
| 253 | return; |
| 254 | |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 255 | if (snapshot_pf == base::kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 256 | return; |
| 257 | |
| 258 | base::MemoryMappedFile::Region snapshot_region = |
| 259 | base::MemoryMappedFile::Region::kWholeFile; |
| 260 | if (snapshot_size != 0 || snapshot_offset != 0) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 261 | snapshot_region.offset = snapshot_offset; |
| 262 | snapshot_region.size = snapshot_size; |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | LoadV8FileResult result = V8_LOAD_SUCCESS; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 266 | if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 267 | result = V8_LOAD_FAILED_MAP; |
mnaganov | d6920a6 | 2015-08-25 17:20:31 | [diff] [blame] | 268 | if (result == V8_LOAD_SUCCESS) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 269 | g_opened_files.Get()[kSnapshotFileName] = |
| 270 | std::make_pair(snapshot_pf, snapshot_region); |
mnaganov | d6920a6 | 2015-08-25 17:20:31 | [diff] [blame] | 271 | } |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 272 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 273 | V8_LOAD_MAX_VALUE); |
| 274 | } |
| 275 | |
| 276 | // static |
| 277 | void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 278 | int64_t natives_offset, |
| 279 | int64_t natives_size) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 280 | if (g_mapped_natives) |
| 281 | return; |
| 282 | |
rockot | 5d4213ff | 2016-05-25 19:07:10 | [diff] [blame] | 283 | CHECK_NE(natives_pf, base::kInvalidPlatformFile); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 284 | |
| 285 | base::MemoryMappedFile::Region natives_region = |
| 286 | base::MemoryMappedFile::Region::kWholeFile; |
| 287 | if (natives_size != 0 || natives_offset != 0) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 288 | natives_region.offset = natives_offset; |
| 289 | natives_region.size = natives_size; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 290 | } |
| 291 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 292 | if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 293 | LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 294 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 295 | g_opened_files.Get()[kNativesFileName] = |
| 296 | std::make_pair(natives_pf, natives_region); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 297 | } |
| 298 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 299 | // static |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 300 | base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( |
| 301 | base::MemoryMappedFile::Region* region_out) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 302 | const OpenedFileMap::mapped_type& opened = |
| 303 | OpenFileIfNecessary(kNativesFileName); |
| 304 | *region_out = opened.second; |
| 305 | return opened.first; |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 306 | } |
| 307 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 308 | // static |
| 309 | base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 310 | base::MemoryMappedFile::Region* region_out) { |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 311 | const OpenedFileMap::mapped_type& opened = |
| 312 | OpenFileIfNecessary(kSnapshotFileName); |
| 313 | *region_out = opened.second; |
| 314 | return opened.first; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 315 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 316 | |
| 317 | #if defined(OS_ANDROID) |
| 318 | // static |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 319 | base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 320 | base::MemoryMappedFile::Region* region_out, |
| 321 | bool abi_32_bit) { |
| 322 | const char* snapshot_file = |
| 323 | abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64; |
| 324 | const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file); |
| 325 | *region_out = opened.second; |
| 326 | return opened.first; |
| 327 | } |
mrunal.kapade | ded427a | 2016-04-26 00:10:44 | [diff] [blame] | 328 | |
| 329 | // static |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 330 | base::FilePath V8Initializer::GetNativesFilePath() { |
mrunal.kapade | ded427a | 2016-04-26 00:10:44 | [diff] [blame] | 331 | base::FilePath path; |
michaelbai | 02037588 | 2016-06-21 16:08:15 | [diff] [blame] | 332 | GetV8FilePath(kNativesFileName, &path); |
mrunal.kapade | ded427a | 2016-04-26 00:10:44 | [diff] [blame] | 333 | return path; |
| 334 | } |
| 335 | |
| 336 | // static |
| 337 | base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) { |
| 338 | base::FilePath path; |
| 339 | GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path); |
| 340 | return path; |
| 341 | } |
tobiasjs | b2001627 | 2016-02-10 11:54:12 | [diff] [blame] | 342 | #endif // defined(OS_ANDROID) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame] | 343 | #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 344 | |
| 345 | // static |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 346 | void V8Initializer::Initialize(IsolateHolder::ScriptMode mode, |
| 347 | IsolateHolder::V8ExtrasMode v8_extras_mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 348 | static bool v8_is_initialized = false; |
| 349 | if (v8_is_initialized) |
| 350 | return; |
| 351 | |
| 352 | v8::V8::InitializePlatform(V8Platform::Get()); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 353 | |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 354 | if (IsolateHolder::kStrictMode == mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 355 | static const char use_strict[] = "--use_strict"; |
| 356 | v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); |
| 357 | } |
yhirano | 9315024 | 2015-12-07 12:28:33 | [diff] [blame] | 358 | if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) { |
| 359 | static const char flag[] = "--experimental_extras"; |
| 360 | v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1); |
| 361 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 362 | |
| 363 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 364 | v8::StartupData natives; |
| 365 | natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 366 | natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
| 367 | v8::V8::SetNativesDataBlob(&natives); |
| 368 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 369 | if (g_mapped_snapshot) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 370 | v8::StartupData snapshot; |
| 371 | snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 372 | snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
| 373 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 374 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 375 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 376 | |
| 377 | v8::V8::SetEntropySource(&GenerateEntropy); |
| 378 | v8::V8::Initialize(); |
| 379 | |
| 380 | v8_is_initialized = true; |
| 381 | } |
| 382 | |
| 383 | // static |
| 384 | void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
| 385 | int* natives_size_out, |
| 386 | const char** snapshot_data_out, |
| 387 | int* snapshot_size_out) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 388 | if (g_mapped_natives) { |
| 389 | *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 390 | *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
| 391 | } else { |
| 392 | *natives_data_out = NULL; |
| 393 | *natives_size_out = 0; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 394 | } |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 395 | if (g_mapped_snapshot) { |
| 396 | *snapshot_data_out = |
| 397 | reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 398 | *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
| 399 | } else { |
| 400 | *snapshot_data_out = NULL; |
| 401 | *snapshot_size_out = 0; |
| 402 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 403 | } |
| 404 | |
Hitoshi Yoshida | f2f50de | 2017-08-22 13:23:55 | [diff] [blame^] | 405 | // static |
| 406 | void V8Initializer::LoadV8ContextSnapshot() { |
| 407 | if (g_mapped_v8_context_snapshot) |
| 408 | return; |
| 409 | |
| 410 | OpenFileIfNecessary(kV8ContextSnapshotFileName); |
| 411 | MapOpenedFile(GetOpenedFile(kV8ContextSnapshotFileName), |
| 412 | &g_mapped_v8_context_snapshot); |
| 413 | |
| 414 | // TODO(peria): Check if the snapshot file is loaded successfully. |
| 415 | } |
| 416 | |
| 417 | // static |
| 418 | void V8Initializer::LoadV8ContextSnapshotFromFD(base::PlatformFile snapshot_pf, |
| 419 | int64_t snapshot_offset, |
| 420 | int64_t snapshot_size) { |
| 421 | if (g_mapped_v8_context_snapshot) |
| 422 | return; |
| 423 | CHECK_NE(base::kInvalidPlatformFile, snapshot_pf); |
| 424 | |
| 425 | base::MemoryMappedFile::Region snapshot_region = |
| 426 | base::MemoryMappedFile::Region::kWholeFile; |
| 427 | if (snapshot_size != 0 || snapshot_offset != 0) { |
| 428 | snapshot_region.offset = snapshot_offset; |
| 429 | snapshot_region.size = snapshot_size; |
| 430 | } |
| 431 | |
| 432 | if (MapV8File(snapshot_pf, snapshot_region, &g_mapped_v8_context_snapshot)) { |
| 433 | g_opened_files.Get()[kV8ContextSnapshotFileName] = |
| 434 | std::make_pair(snapshot_pf, snapshot_region); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // static |
| 439 | void V8Initializer::GetV8ContextSnapshotData(const char** snapshot_data_out, |
| 440 | int* snapshot_size_out) { |
| 441 | if (g_mapped_v8_context_snapshot) { |
| 442 | *snapshot_data_out = |
| 443 | reinterpret_cast<const char*>(g_mapped_v8_context_snapshot->data()); |
| 444 | *snapshot_size_out = |
| 445 | static_cast<int>(g_mapped_v8_context_snapshot->length()); |
| 446 | } else { |
| 447 | *snapshot_data_out = nullptr; |
| 448 | *snapshot_size_out = 0; |
| 449 | } |
| 450 | } |
| 451 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 452 | } // namespace gin |