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