OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
6 | 6 |
7 #include "bin/crypto.h" | 7 #include "bin/crypto.h" |
8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
9 #include "bin/extensions.h" | 9 #include "bin/extensions.h" |
10 #include "bin/file.h" | 10 #include "bin/file.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 const char* const DartUtils::kBuiltinLibURL = "dart:_builtin"; | 39 const char* const DartUtils::kBuiltinLibURL = "dart:_builtin"; |
40 const char* const DartUtils::kCoreLibURL = "dart:core"; | 40 const char* const DartUtils::kCoreLibURL = "dart:core"; |
41 const char* const DartUtils::kInternalLibURL = "dart:_internal"; | 41 const char* const DartUtils::kInternalLibURL = "dart:_internal"; |
42 const char* const DartUtils::kIsolateLibURL = "dart:isolate"; | 42 const char* const DartUtils::kIsolateLibURL = "dart:isolate"; |
43 const char* const DartUtils::kIOLibURL = "dart:io"; | 43 const char* const DartUtils::kIOLibURL = "dart:io"; |
44 const char* const DartUtils::kIOLibPatchURL = "dart:io-patch"; | 44 const char* const DartUtils::kIOLibPatchURL = "dart:io-patch"; |
45 const char* const DartUtils::kUriLibURL = "dart:uri"; | 45 const char* const DartUtils::kUriLibURL = "dart:uri"; |
46 const char* const DartUtils::kHttpScheme = "http:"; | 46 const char* const DartUtils::kHttpScheme = "http:"; |
47 const char* const DartUtils::kVMServiceLibURL = "dart:vmservice"; | 47 const char* const DartUtils::kVMServiceLibURL = "dart:vmservice"; |
48 | 48 |
49 const uint8_t DartUtils::magic_number[] = { 0xf5, 0xf5, 0xdc, 0xdc }; | 49 |
| 50 struct MagicNumberData { |
| 51 static const intptr_t kLength = 4; |
| 52 |
| 53 const uint8_t bytes[kLength]; |
| 54 bool should_skip; |
| 55 }; |
| 56 |
| 57 |
| 58 MagicNumberData snapshot_magic_number = { { 0xf5, 0xf5, 0xdc, 0xdc }, true }; |
| 59 MagicNumberData kernel_magic_number = { {0x90, 0xab, 0xcd, 0xef}, false }; |
| 60 |
| 61 |
| 62 bool TryReadKernel(const char* script_uri, |
| 63 const uint8_t** dil_file, |
| 64 intptr_t* dil_length) { |
| 65 *dil_file = NULL; |
| 66 *dil_length = -1; |
| 67 bool is_kernel_file = false; |
| 68 void* script_file = DartUtils::OpenFile(script_uri, false); |
| 69 if (script_file != NULL) { |
| 70 const uint8_t* buffer = NULL; |
| 71 DartUtils::ReadFile(&buffer, dil_length, script_file); |
| 72 DartUtils::CloseFile(script_file); |
| 73 if (*dil_length > 0 && buffer != NULL) { |
| 74 *dil_file = buffer; |
| 75 if (DartUtils::SniffForMagicNumber(&buffer, dil_length) != |
| 76 DartUtils::kKernelMagicNumber) { |
| 77 // Do not free buffer if we this is a kernel file - dil_file will be |
| 78 // backed by the same memory as the buffer and VM will own it. |
| 79 free(const_cast<uint8_t*>(buffer)); |
| 80 *dil_file = NULL; |
| 81 } else { |
| 82 is_kernel_file = true; |
| 83 } |
| 84 } |
| 85 } |
| 86 return is_kernel_file; |
| 87 } |
50 | 88 |
51 | 89 |
52 static bool IsWindowsHost() { | 90 static bool IsWindowsHost() { |
53 #if defined(TARGET_OS_WINDOWS) | 91 #if defined(TARGET_OS_WINDOWS) |
54 return true; | 92 return true; |
55 #else // defined(TARGET_OS_WINDOWS) | 93 #else // defined(TARGET_OS_WINDOWS) |
56 return false; | 94 return false; |
57 #endif // defined(TARGET_OS_WINDOWS) | 95 #endif // defined(TARGET_OS_WINDOWS) |
58 } | 96 } |
59 | 97 |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 extension_path, | 498 extension_path, |
461 library); | 499 library); |
462 } | 500 } |
463 | 501 |
464 // Handle 'import' or 'part' requests for all other URIs. Call dart code to | 502 // Handle 'import' or 'part' requests for all other URIs. Call dart code to |
465 // read the source code asynchronously. | 503 // read the source code asynchronously. |
466 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); | 504 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); |
467 } | 505 } |
468 | 506 |
469 | 507 |
470 const uint8_t* DartUtils::SniffForMagicNumber(const uint8_t* text_buffer, | 508 static bool CheckMagicNumber(const uint8_t** buf, |
471 intptr_t* buffer_len, | 509 intptr_t* len, |
472 bool* is_snapshot) { | 510 const MagicNumberData& magic_number) { |
473 intptr_t len = sizeof(magic_number); | 511 if ((*len >= MagicNumberData::kLength) && |
474 if (*buffer_len <= len) { | 512 (memcmp(*buf, magic_number.bytes, MagicNumberData::kLength) == 0)) { |
475 *is_snapshot = false; | 513 if (magic_number.should_skip) { |
476 return text_buffer; | 514 *buf += MagicNumberData::kLength; |
| 515 *len -= MagicNumberData::kLength; |
| 516 } |
| 517 return true; |
477 } | 518 } |
478 for (intptr_t i = 0; i < len; i++) { | 519 return false; |
479 if (text_buffer[i] != magic_number[i]) { | 520 } |
480 *is_snapshot = false; | 521 |
481 return text_buffer; | 522 |
482 } | 523 DartUtils::MagicNumber DartUtils::SniffForMagicNumber(const uint8_t** buf, |
| 524 intptr_t* len) { |
| 525 if (CheckMagicNumber(buf, len, snapshot_magic_number)) { |
| 526 return kSnapshotMagicNumber; |
483 } | 527 } |
484 *is_snapshot = true; | 528 |
485 ASSERT(*buffer_len > len); | 529 if (CheckMagicNumber(buf, len, kernel_magic_number)) { |
486 *buffer_len -= len; | 530 return kKernelMagicNumber; |
487 return text_buffer + len; | 531 } |
| 532 |
| 533 return kUnknownMagicNumber; |
488 } | 534 } |
489 | 535 |
490 | 536 |
491 void DartUtils::WriteMagicNumber(File* file) { | 537 void DartUtils::WriteMagicNumber(File* file) { |
492 // Write a magic number and version information into the snapshot file. | 538 // Write a magic number and version information into the snapshot file. |
493 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 539 bool bytes_written = file->WriteFully(snapshot_magic_number.bytes, |
| 540 MagicNumberData::kLength); |
494 ASSERT(bytes_written); | 541 ASSERT(bytes_written); |
495 } | 542 } |
496 | 543 |
497 | 544 |
498 Dart_Handle DartUtils::LoadScript(const char* script_uri) { | 545 Dart_Handle DartUtils::LoadScript(const char* script_uri) { |
499 Dart_TimelineEvent("LoadScript", | 546 Dart_TimelineEvent("LoadScript", |
500 Dart_TimelineGetMicros(), | 547 Dart_TimelineGetMicros(), |
501 Dart_GetMainPortId(), | 548 Dart_GetMainPortId(), |
502 Dart_Timeline_Event_Async_Begin, | 549 Dart_Timeline_Event_Async_Begin, |
503 0, NULL, NULL); | 550 0, NULL, NULL); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 // If the buffer is not external, take a copy. | 598 // If the buffer is not external, take a copy. |
552 buffer_copy = reinterpret_cast<uint8_t*>(malloc(num_bytes)); | 599 buffer_copy = reinterpret_cast<uint8_t*>(malloc(num_bytes)); |
553 memmove(buffer_copy, data, num_bytes); | 600 memmove(buffer_copy, data, num_bytes); |
554 data = buffer_copy; | 601 data = buffer_copy; |
555 } | 602 } |
556 | 603 |
557 Dart_TypedDataReleaseData(source_data); | 604 Dart_TypedDataReleaseData(source_data); |
558 | 605 |
559 if (Dart_IsNull(tag_in) && Dart_IsNull(library_uri)) { | 606 if (Dart_IsNull(tag_in) && Dart_IsNull(library_uri)) { |
560 // Entry file. Check for payload and load accordingly. | 607 // Entry file. Check for payload and load accordingly. |
561 bool is_snapshot = false; | 608 const uint8_t* payload = data; |
562 const uint8_t *payload = | 609 const DartUtils::MagicNumber payload_type = |
563 DartUtils::SniffForMagicNumber(data, &num_bytes, &is_snapshot); | 610 DartUtils::SniffForMagicNumber(&payload, &num_bytes); |
564 | 611 |
565 if (is_snapshot) { | 612 if (payload_type == DartUtils::kSnapshotMagicNumber) { |
566 result = Dart_LoadScriptFromSnapshot(payload, num_bytes); | 613 result = Dart_LoadScriptFromSnapshot(payload, num_bytes); |
| 614 } else if (payload_type == DartUtils::kKernelMagicNumber) { |
| 615 UNREACHABLE(); |
567 } else { | 616 } else { |
568 Dart_Handle source = Dart_NewStringFromUTF8(data, num_bytes); | 617 Dart_Handle source = Dart_NewStringFromUTF8(data, num_bytes); |
569 if (Dart_IsError(source)) { | 618 if (Dart_IsError(source)) { |
570 result = DartUtils::NewError("%s is not a valid UTF-8 script", | 619 result = DartUtils::NewError("%s is not a valid UTF-8 script", |
571 resolved_script_uri); | 620 resolved_script_uri); |
572 } else { | 621 } else { |
573 result = Dart_LoadScript(resolved_script_uri, Dart_Null(), | 622 result = Dart_LoadScript(resolved_script_uri, Dart_Null(), |
574 source, 0, 0); | 623 source, 0, 0); |
575 } | 624 } |
576 } | 625 } |
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1263 new CObjectString(CObject::NewString(os_error->message())); | 1312 new CObjectString(CObject::NewString(os_error->message())); |
1264 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 1313 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1265 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 1314 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1266 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 1315 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1267 result->SetAt(2, error_message); | 1316 result->SetAt(2, error_message); |
1268 return result; | 1317 return result; |
1269 } | 1318 } |
1270 | 1319 |
1271 } // namespace bin | 1320 } // namespace bin |
1272 } // namespace dart | 1321 } // namespace dart |
OLD | NEW |