OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/v8_initializer.h" | 5 #include "gin/v8_initializer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 | 184 |
185 static const OpenedFileMap::mapped_type OpenFileIfNecessary( | 185 static const OpenedFileMap::mapped_type OpenFileIfNecessary( |
186 const char* file_name) { | 186 const char* file_name) { |
187 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name); | 187 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name); |
188 if (opened.first == base::kInvalidPlatformFile) { | 188 if (opened.first == base::kInvalidPlatformFile) { |
189 opened.first = OpenV8File(file_name, &opened.second); | 189 opened.first = OpenV8File(file_name, &opened.second); |
190 } | 190 } |
191 return opened; | 191 return opened; |
192 } | 192 } |
193 | 193 |
| 194 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 195 bool 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 |
194 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 224 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
195 | 225 |
196 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 226 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
197 base::RandBytes(buffer, amount); | 227 base::RandBytes(buffer, amount); |
198 return true; | 228 return true; |
199 } | 229 } |
200 | 230 |
201 bool ShouldUseIgnition() { | 231 bool ShouldUseIgnition() { |
202 if (base::FeatureList::IsEnabled(features::kV8Ignition)) return true; | 232 if (base::FeatureList::IsEnabled(features::kV8Ignition)) return true; |
203 #if defined(OS_ANDROID) | 233 #if defined(OS_ANDROID) |
204 if (base::FeatureList::IsEnabled(features::kV8IgnitionLowEnd) && | 234 if (base::FeatureList::IsEnabled(features::kV8IgnitionLowEnd) && |
205 base::SysInfo::IsLowEndDevice()) { | 235 base::SysInfo::IsLowEndDevice()) { |
206 return true; | 236 return true; |
207 } | 237 } |
208 #endif | 238 #endif |
209 return false; | 239 return false; |
210 } | 240 } |
211 | 241 |
212 | 242 |
213 } // namespace | 243 } // namespace |
214 | 244 |
215 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 245 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
216 | 246 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
217 namespace { | 247 // Defined in gen/gin/v8_snapshot_fingerprint.cc |
| 248 extern const unsigned char g_natives_fingerprint[]; |
| 249 extern const unsigned char g_snapshot_fingerprint[]; |
| 250 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
218 | 251 |
219 enum LoadV8FileResult { | 252 enum LoadV8FileResult { |
220 V8_LOAD_SUCCESS = 0, | 253 V8_LOAD_SUCCESS = 0, |
221 V8_LOAD_FAILED_OPEN, | 254 V8_LOAD_FAILED_OPEN, |
222 V8_LOAD_FAILED_MAP, | 255 V8_LOAD_FAILED_MAP, |
223 V8_LOAD_FAILED_VERIFY, // Deprecated. | 256 V8_LOAD_FAILED_VERIFY, |
224 V8_LOAD_MAX_VALUE | 257 V8_LOAD_MAX_VALUE |
225 }; | 258 }; |
226 | 259 |
227 LoadV8FileResult MapOpenedFile( | 260 static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region, |
228 const OpenedFileMap::mapped_type& file_region, | 261 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
229 base::MemoryMappedFile** mmapped_file_out) { | 262 const unsigned char* fingerprint, |
| 263 #endif |
| 264 base::MemoryMappedFile** mmapped_file_out) { |
230 if (file_region.first == base::kInvalidPlatformFile) | 265 if (file_region.first == base::kInvalidPlatformFile) |
231 return V8_LOAD_FAILED_OPEN; | 266 return V8_LOAD_FAILED_OPEN; |
232 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) | 267 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) |
233 return V8_LOAD_FAILED_MAP; | 268 return V8_LOAD_FAILED_MAP; |
| 269 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 270 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) |
| 271 return V8_LOAD_FAILED_VERIFY; |
| 272 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
234 return V8_LOAD_SUCCESS; | 273 return V8_LOAD_SUCCESS; |
235 } | 274 } |
236 | 275 |
237 } // namespace | |
238 | |
239 // static | 276 // static |
240 void V8Initializer::LoadV8Snapshot() { | 277 void V8Initializer::LoadV8Snapshot() { |
241 if (g_mapped_snapshot) | 278 if (g_mapped_snapshot) |
242 return; | 279 return; |
243 | 280 |
244 OpenFileIfNecessary(kSnapshotFileName); | 281 OpenFileIfNecessary(kSnapshotFileName); |
245 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kSnapshotFileName), | 282 LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName), |
246 &g_mapped_snapshot); | 283 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 284 g_snapshot_fingerprint, |
| 285 #endif |
| 286 &g_mapped_snapshot); |
247 // V8 can't start up without the source of the natives, but it can | 287 // V8 can't start up without the source of the natives, but it can |
248 // start up (slower) without the snapshot. | 288 // start up (slower) without the snapshot. |
249 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, | 289 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
250 V8_LOAD_MAX_VALUE); | 290 V8_LOAD_MAX_VALUE); |
251 } | 291 } |
252 | 292 |
253 void V8Initializer::LoadV8Natives() { | 293 void V8Initializer::LoadV8Natives() { |
254 if (g_mapped_natives) | 294 if (g_mapped_natives) |
255 return; | 295 return; |
256 | 296 |
257 OpenFileIfNecessary(kNativesFileName); | 297 OpenFileIfNecessary(kNativesFileName); |
258 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kNativesFileName), | 298 LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName), |
| 299 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 300 g_natives_fingerprint, |
| 301 #endif |
259 &g_mapped_natives); | 302 &g_mapped_natives); |
260 if (result != V8_LOAD_SUCCESS) { | 303 if (result != V8_LOAD_SUCCESS) { |
261 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " | 304 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
262 << static_cast<int>(result); | 305 << static_cast<int>(result); |
263 } | 306 } |
264 } | 307 } |
265 | 308 |
266 // static | 309 // static |
267 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, | 310 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
268 int64_t snapshot_offset, | 311 int64_t snapshot_offset, |
269 int64_t snapshot_size) { | 312 int64_t snapshot_size) { |
270 if (g_mapped_snapshot) | 313 if (g_mapped_snapshot) |
271 return; | 314 return; |
272 | 315 |
273 if (snapshot_pf == base::kInvalidPlatformFile) | 316 if (snapshot_pf == base::kInvalidPlatformFile) |
274 return; | 317 return; |
275 | 318 |
276 base::MemoryMappedFile::Region snapshot_region = | 319 base::MemoryMappedFile::Region snapshot_region = |
277 base::MemoryMappedFile::Region::kWholeFile; | 320 base::MemoryMappedFile::Region::kWholeFile; |
278 if (snapshot_size != 0 || snapshot_offset != 0) { | 321 if (snapshot_size != 0 || snapshot_offset != 0) { |
279 snapshot_region.offset = snapshot_offset; | 322 snapshot_region.offset = snapshot_offset; |
280 snapshot_region.size = snapshot_size; | 323 snapshot_region.size = snapshot_size; |
281 } | 324 } |
282 | 325 |
283 LoadV8FileResult result = V8_LOAD_SUCCESS; | 326 LoadV8FileResult result = V8_LOAD_SUCCESS; |
284 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) | 327 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) |
285 result = V8_LOAD_FAILED_MAP; | 328 result = V8_LOAD_FAILED_MAP; |
| 329 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 330 if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint)) |
| 331 result = V8_LOAD_FAILED_VERIFY; |
| 332 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
286 if (result == V8_LOAD_SUCCESS) { | 333 if (result == V8_LOAD_SUCCESS) { |
287 g_opened_files.Get()[kSnapshotFileName] = | 334 g_opened_files.Get()[kSnapshotFileName] = |
288 std::make_pair(snapshot_pf, snapshot_region); | 335 std::make_pair(snapshot_pf, snapshot_region); |
289 } | 336 } |
290 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, | 337 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
291 V8_LOAD_MAX_VALUE); | 338 V8_LOAD_MAX_VALUE); |
292 } | 339 } |
293 | 340 |
294 // static | 341 // static |
295 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, | 342 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
296 int64_t natives_offset, | 343 int64_t natives_offset, |
297 int64_t natives_size) { | 344 int64_t natives_size) { |
298 if (g_mapped_natives) | 345 if (g_mapped_natives) |
299 return; | 346 return; |
300 | 347 |
301 CHECK_NE(natives_pf, base::kInvalidPlatformFile); | 348 CHECK_NE(natives_pf, base::kInvalidPlatformFile); |
302 | 349 |
303 base::MemoryMappedFile::Region natives_region = | 350 base::MemoryMappedFile::Region natives_region = |
304 base::MemoryMappedFile::Region::kWholeFile; | 351 base::MemoryMappedFile::Region::kWholeFile; |
305 if (natives_size != 0 || natives_offset != 0) { | 352 if (natives_size != 0 || natives_offset != 0) { |
306 natives_region.offset = natives_offset; | 353 natives_region.offset = natives_offset; |
307 natives_region.size = natives_size; | 354 natives_region.size = natives_size; |
308 } | 355 } |
309 | 356 |
310 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { | 357 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
311 LOG(FATAL) << "Couldn't mmap v8 natives data file"; | 358 LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
312 } | 359 } |
| 360 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 361 if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) { |
| 362 LOG(FATAL) << "Couldn't verify contents of v8 natives data file"; |
| 363 } |
| 364 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
313 g_opened_files.Get()[kNativesFileName] = | 365 g_opened_files.Get()[kNativesFileName] = |
314 std::make_pair(natives_pf, natives_region); | 366 std::make_pair(natives_pf, natives_region); |
315 } | 367 } |
316 | 368 |
317 // static | 369 // static |
318 base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( | 370 base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( |
319 base::MemoryMappedFile::Region* region_out) { | 371 base::MemoryMappedFile::Region* region_out) { |
320 const OpenedFileMap::mapped_type& opened = | 372 const OpenedFileMap::mapped_type& opened = |
321 OpenFileIfNecessary(kNativesFileName); | 373 OpenFileIfNecessary(kNativesFileName); |
322 *region_out = opened.second; | 374 *region_out = opened.second; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 *snapshot_data_out = | 489 *snapshot_data_out = |
438 reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 490 reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
439 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); | 491 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
440 } else { | 492 } else { |
441 *snapshot_data_out = NULL; | 493 *snapshot_data_out = NULL; |
442 *snapshot_size_out = 0; | 494 *snapshot_size_out = 0; |
443 } | 495 } |
444 } | 496 } |
445 | 497 |
446 } // namespace gin | 498 } // namespace gin |
OLD | NEW |