upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

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