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 } // namespace | 201 } // namespace |
232 | 202 |
233 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 203 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
234 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 204 |
235 // Defined in gen/gin/v8_snapshot_fingerprint.cc | 205 namespace { |
236 extern const unsigned char g_natives_fingerprint[]; | |
237 extern const unsigned char g_snapshot_fingerprint[]; | |
238 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA | |
239 | 206 |
240 enum LoadV8FileResult { | 207 enum LoadV8FileResult { |
241 V8_LOAD_SUCCESS = 0, | 208 V8_LOAD_SUCCESS = 0, |
242 V8_LOAD_FAILED_OPEN, | 209 V8_LOAD_FAILED_OPEN, |
243 V8_LOAD_FAILED_MAP, | 210 V8_LOAD_FAILED_MAP, |
244 V8_LOAD_FAILED_VERIFY, | 211 V8_LOAD_FAILED_VERIFY, // Deprecated. |
245 V8_LOAD_MAX_VALUE | 212 V8_LOAD_MAX_VALUE |
246 }; | 213 }; |
247 | 214 |
248 static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region, | 215 static LoadV8FileResult MapOpenedFile( |
249 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 216 const OpenedFileMap::mapped_type& file_region, |
250 const unsigned char* fingerprint, | 217 base::MemoryMappedFile** mmapped_file_out) { |
251 #endif | |
252 base::MemoryMappedFile** mmapped_file_out) { | |
253 if (file_region.first == base::kInvalidPlatformFile) | 218 if (file_region.first == base::kInvalidPlatformFile) |
254 return V8_LOAD_FAILED_OPEN; | 219 return V8_LOAD_FAILED_OPEN; |
255 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) | 220 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out)) |
256 return V8_LOAD_FAILED_MAP; | 221 return V8_LOAD_FAILED_MAP; |
257 #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 | |
261 return V8_LOAD_SUCCESS; | 222 return V8_LOAD_SUCCESS; |
262 } | 223 } |
263 | 224 |
| 225 } // namespace |
| 226 |
264 // static | 227 // static |
265 void V8Initializer::LoadV8Snapshot() { | 228 void V8Initializer::LoadV8Snapshot() { |
266 if (g_mapped_snapshot) | 229 if (g_mapped_snapshot) |
267 return; | 230 return; |
268 | 231 |
269 OpenFileIfNecessary(kSnapshotFileName); | 232 OpenFileIfNecessary(kSnapshotFileName); |
270 LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName), | 233 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kSnapshotFileName), |
271 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 234 &g_mapped_snapshot); |
272 g_snapshot_fingerprint, | |
273 #endif | |
274 &g_mapped_snapshot); | |
275 // V8 can't start up without the source of the natives, but it can | 235 // V8 can't start up without the source of the natives, but it can |
276 // start up (slower) without the snapshot. | 236 // start up (slower) without the snapshot. |
277 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, | 237 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
278 V8_LOAD_MAX_VALUE); | 238 V8_LOAD_MAX_VALUE); |
279 } | 239 } |
280 | 240 |
281 void V8Initializer::LoadV8Natives() { | 241 void V8Initializer::LoadV8Natives() { |
282 if (g_mapped_natives) | 242 if (g_mapped_natives) |
283 return; | 243 return; |
284 | 244 |
285 OpenFileIfNecessary(kNativesFileName); | 245 OpenFileIfNecessary(kNativesFileName); |
286 LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName), | 246 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kNativesFileName), |
287 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) | 247 &g_mapped_natives); |
288 g_natives_fingerprint, | |
289 #endif | |
290 &g_mapped_natives); | |
291 if (result != V8_LOAD_SUCCESS) { | 248 if (result != V8_LOAD_SUCCESS) { |
292 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " | 249 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
293 << static_cast<int>(result); | 250 << static_cast<int>(result); |
294 } | 251 } |
295 } | 252 } |
296 | 253 |
297 // static | 254 // static |
298 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, | 255 void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
299 int64_t snapshot_offset, | 256 int64_t snapshot_offset, |
300 int64_t snapshot_size) { | 257 int64_t snapshot_size) { |
301 if (g_mapped_snapshot) | 258 if (g_mapped_snapshot) |
302 return; | 259 return; |
303 | 260 |
304 if (snapshot_pf == base::kInvalidPlatformFile) | 261 if (snapshot_pf == base::kInvalidPlatformFile) |
305 return; | 262 return; |
306 | 263 |
307 base::MemoryMappedFile::Region snapshot_region = | 264 base::MemoryMappedFile::Region snapshot_region = |
308 base::MemoryMappedFile::Region::kWholeFile; | 265 base::MemoryMappedFile::Region::kWholeFile; |
309 if (snapshot_size != 0 || snapshot_offset != 0) { | 266 if (snapshot_size != 0 || snapshot_offset != 0) { |
310 snapshot_region.offset = snapshot_offset; | 267 snapshot_region.offset = snapshot_offset; |
311 snapshot_region.size = snapshot_size; | 268 snapshot_region.size = snapshot_size; |
312 } | 269 } |
313 | 270 |
314 LoadV8FileResult result = V8_LOAD_SUCCESS; | 271 LoadV8FileResult result = V8_LOAD_SUCCESS; |
315 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) | 272 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) |
316 result = V8_LOAD_FAILED_MAP; | 273 result = V8_LOAD_FAILED_MAP; |
317 #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 | |
321 if (result == V8_LOAD_SUCCESS) { | 274 if (result == V8_LOAD_SUCCESS) { |
322 g_opened_files.Get()[kSnapshotFileName] = | 275 g_opened_files.Get()[kSnapshotFileName] = |
323 std::make_pair(snapshot_pf, snapshot_region); | 276 std::make_pair(snapshot_pf, snapshot_region); |
324 } | 277 } |
325 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, | 278 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
326 V8_LOAD_MAX_VALUE); | 279 V8_LOAD_MAX_VALUE); |
327 } | 280 } |
328 | 281 |
329 // static | 282 // static |
330 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, | 283 void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
331 int64_t natives_offset, | 284 int64_t natives_offset, |
332 int64_t natives_size) { | 285 int64_t natives_size) { |
333 if (g_mapped_natives) | 286 if (g_mapped_natives) |
334 return; | 287 return; |
335 | 288 |
336 CHECK_NE(natives_pf, base::kInvalidPlatformFile); | 289 CHECK_NE(natives_pf, base::kInvalidPlatformFile); |
337 | 290 |
338 base::MemoryMappedFile::Region natives_region = | 291 base::MemoryMappedFile::Region natives_region = |
339 base::MemoryMappedFile::Region::kWholeFile; | 292 base::MemoryMappedFile::Region::kWholeFile; |
340 if (natives_size != 0 || natives_offset != 0) { | 293 if (natives_size != 0 || natives_offset != 0) { |
341 natives_region.offset = natives_offset; | 294 natives_region.offset = natives_offset; |
342 natives_region.size = natives_size; | 295 natives_region.size = natives_size; |
343 } | 296 } |
344 | 297 |
345 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { | 298 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
346 LOG(FATAL) << "Couldn't mmap v8 natives data file"; | 299 LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
347 } | 300 } |
348 #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 | |
353 g_opened_files.Get()[kNativesFileName] = | 301 g_opened_files.Get()[kNativesFileName] = |
354 std::make_pair(natives_pf, natives_region); | 302 std::make_pair(natives_pf, natives_region); |
355 } | 303 } |
356 | 304 |
357 // static | 305 // static |
358 base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( | 306 base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( |
359 base::MemoryMappedFile::Region* region_out) { | 307 base::MemoryMappedFile::Region* region_out) { |
360 const OpenedFileMap::mapped_type& opened = | 308 const OpenedFileMap::mapped_type& opened = |
361 OpenFileIfNecessary(kNativesFileName); | 309 OpenFileIfNecessary(kNativesFileName); |
362 *region_out = opened.second; | 310 *region_out = opened.second; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 *snapshot_data_out = | 418 *snapshot_data_out = |
471 reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 419 reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
472 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); | 420 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
473 } else { | 421 } else { |
474 *snapshot_data_out = NULL; | 422 *snapshot_data_out = NULL; |
475 *snapshot_size_out = 0; | 423 *snapshot_size_out = 0; |
476 } | 424 } |
477 } | 425 } |
478 | 426 |
479 } // namespace gin | 427 } // namespace gin |
OLD | NEW |