diff options
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 6fd1dced4..490eebedf 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -631,6 +631,90 @@ void test_delay_descriptor_fields() { | |||
| 631 | } | 631 | } |
| 632 | } | 632 | } |
| 633 | 633 | ||
| 634 | // make_image() plus an exception directory at RVA 0x1100 whose entries all share one minimal UNWIND_INFO at 0x1180 | ||
| 635 | // (version 1, no flags, no prologue, no codes). Every RVA here lies inside the single .text section, so file offset | ||
| 636 | // = RVA - 0x1000 + 0x200. | ||
| 637 | constexpr uint32_t kPdataRva = 0x1100; | ||
| 638 | constexpr uint32_t kUnwindRva = 0x1180; | ||
| 639 | |||
| 640 | std::vector<uint8_t> make_unwind_image(const std::vector<RUNTIME_FUNCTION>& entries) { | ||
| 641 | auto img = make_image(); | ||
| 642 | std::memset(img.data() + 0x200 + (kPdataRva - 0x1000), 0, 0x100); | ||
| 643 | const uint8_t unwind_info[4] = {1, 0, 0, 0}; | ||
| 644 | std::memcpy(img.data() + 0x200 + (kUnwindRva - 0x1000), unwind_info, sizeof(unwind_info)); | ||
| 645 | std::memcpy(img.data() + 0x200 + (kPdataRva - 0x1000), entries.data(), entries.size() * sizeof(RUNTIME_FUNCTION)); | ||
| 646 | auto& dir = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION]; | ||
| 647 | dir.VirtualAddress = kPdataRva; | ||
| 648 | dir.Size = static_cast<DWORD>(entries.size() * sizeof(RUNTIME_FUNCTION)); | ||
| 649 | nt_of(img)->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES; | ||
| 650 | return img; | ||
| 651 | } | ||
| 652 | |||
| 653 | void test_unwind_registration() { | ||
| 654 | const std::vector<RUNTIME_FUNCTION> entries = {{0x1000, 0x1010, kUnwindRva}, {0x1010, 0x1020, kUnwindRva}}; | ||
| 655 | uintptr_t pc = 0; | ||
| 656 | { | ||
| 657 | auto img = make_unwind_image(entries); | ||
| 658 | auto p = write_image(img); | ||
| 659 | try { | ||
| 660 | rt::Loader loader(g_dir); | ||
| 661 | rt::Module& m = loader.load(p); | ||
| 662 | pc = reinterpret_cast<uintptr_t>(m.base) + 0x1014; // inside the second function | ||
| 663 | DWORD64 image_base = 0; | ||
| 664 | auto* found = RtlLookupFunctionEntry(pc, &image_base, nullptr); | ||
| 665 | if (m.function_count != 2) | ||
| 666 | failed("the exception directory is registered", "function_count is " + std::to_string(m.function_count)); | ||
| 667 | else if (!found) | ||
| 668 | failed("the exception directory is registered", "RtlLookupFunctionEntry found nothing"); | ||
| 669 | else if (image_base != reinterpret_cast<DWORD64>(m.base) || | ||
| 670 | reinterpret_cast<uint8_t*>(found) != m.base + kPdataRva + sizeof(RUNTIME_FUNCTION)) | ||
| 671 | failed("the exception directory is registered", "the lookup returned the wrong entry or image base"); | ||
| 672 | else | ||
| 673 | pass("the exception directory is registered"); | ||
| 674 | } catch (const std::exception& e) { | ||
| 675 | failed("the exception directory is registered", e.what()); | ||
| 676 | } | ||
| 677 | DWORD64 image_base = 0; | ||
| 678 | if (pc && RtlLookupFunctionEntry(pc, &image_base, nullptr)) | ||
| 679 | failed("the unwind table is removed with the loader", "the lookup still finds the freed image"); | ||
| 680 | else | ||
| 681 | pass("the unwind table is removed with the loader"); | ||
| 682 | } | ||
| 683 | { | ||
| 684 | // The system loader drops a trailing partial entry rather than rejecting the image; so does this one. | ||
| 685 | auto img = make_unwind_image(entries); | ||
| 686 | nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size += 5; | ||
| 687 | auto p = write_image(img); | ||
| 688 | try { | ||
| 689 | rt::Loader loader(g_dir); | ||
| 690 | rt::Module& m = loader.load(p); | ||
| 691 | if (m.function_count != 2) | ||
| 692 | failed("a trailing partial unwind entry is ignored", "function_count is " + std::to_string(m.function_count)); | ||
| 693 | else | ||
| 694 | pass("a trailing partial unwind entry is ignored"); | ||
| 695 | } catch (const std::exception& e) { | ||
| 696 | failed("a trailing partial unwind entry is ignored", e.what()); | ||
| 697 | } | ||
| 698 | } | ||
| 699 | // A zero-length entry appears in a shipped Windows image (symcryptk.dll), so it has to be accepted. | ||
| 700 | expect_loads("a zero-length unwind entry is accepted", | ||
| 701 | make_unwind_image({{0x1000, 0x1000, kUnwindRva}, {0x1000, 0x1020, kUnwindRva}})); | ||
| 702 | { | ||
| 703 | auto img = make_unwind_image(entries); | ||
| 704 | nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress = 0x1FF0; | ||
| 705 | expect_rejects("an exception directory past the image", img); | ||
| 706 | } | ||
| 707 | expect_rejects("an unwind entry ending past the image", | ||
| 708 | make_unwind_image({{0x1000, 0x2001, kUnwindRva}})); | ||
| 709 | expect_rejects("an unwind entry that ends before it starts", | ||
| 710 | make_unwind_image({{0x1010, 0x1000, kUnwindRva}})); | ||
| 711 | expect_rejects("unwind entries out of order", | ||
| 712 | make_unwind_image({{0x1010, 0x1020, kUnwindRva}, {0x1000, 0x1010, kUnwindRva}})); | ||
| 713 | expect_rejects("overlapping unwind entries", | ||
| 714 | make_unwind_image({{0x1000, 0x1018, kUnwindRva}, {0x1010, 0x1020, kUnwindRva}})); | ||
| 715 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); | ||
| 716 | } | ||
| 717 | |||
| 634 | } // namespace | 718 | } // namespace |
| 635 | 719 | ||
| 636 | int main() { | 720 | int main() { |
| @@ -655,6 +739,7 @@ int main() { | |||
| 655 | test_initialise_rejects(); | 739 | test_initialise_rejects(); |
| 656 | test_delay_descriptor_fields(); | 740 | test_delay_descriptor_fields(); |
| 657 | test_forwarded_exports(); | 741 | test_forwarded_exports(); |
| 742 | test_unwind_registration(); | ||
| 658 | 743 | ||
| 659 | std::filesystem::remove_all(g_dir, ec); | 744 | std::filesystem::remove_all(g_dir, ec); |
| 660 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); | 745 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); |