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:
authorAREA52 Agent <area52@localhost>2026-11-21 15:31:09 -0800
committerAREA52 Agent <area52@localhost>2026-11-21 15:31:09 -0800
commit25ca1d63f2883ff88ab8bfcd3afd8cc426ab141c (patch)
tree8cdca7f6e87736acea8d1e1105d8b4d8eb05ad24 /runtime/test/loader_test.cpp
parenteef019d7baf91e819d80f3aa12fd6d3a050b7c5f (diff)
parentd937b7f092dbc188b59754e5c73c1a9dac553cf9 (diff)
Merge upstream main into SHADER-7 and retain filtered clip dependencies
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 6fd1dced4..b756cc1b6 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -14,6 +14,7 @@
14#include <string> 14#include <string>
15#include <vector> 15#include <vector>
16 16
17#include "../shims/shims.h"
17#include "../src/pe_loader.h" 18#include "../src/pe_loader.h"
18 19
19namespace { 20namespace {
@@ -137,6 +138,12 @@ void* host_symbol(const char* dll, const char* name) {
137 return h ? reinterpret_cast<void*>(GetProcAddress(h, name)) : nullptr; 138 return h ? reinterpret_cast<void*>(GetProcAddress(h, name)) : nullptr;
138} 139}
139 140
141// A shim table for the forwarder test, so the test does not depend on the real xmem shim. Registering it at startup
142// exercises the same registry a packaged module reaches.
143int g_test_shim_target = 0;
144void* test_shim_function() { return &g_test_shim_target; }
145SHIM_TABLE(test_forwarder_shim, {"testshim.dll", "F", reinterpret_cast<void*>(&test_shim_function)});
146
140// 0 when the image loaded, 1 when it was rejected. A crash here is a test failure, which is the point. 147// 0 when the image loaded, 1 when it was rejected. A crash here is a test failure, which is the point.
141int load(const std::filesystem::path& p, std::string& error) { 148int load(const std::filesystem::path& p, std::string& error) {
142 try { 149 try {
@@ -583,6 +590,22 @@ void test_forwarded_exports() {
583 failed(what, e.what()); 590 failed(what, e.what());
584 } 591 }
585 } 592 }
593 {
594 const char* what = "a forwarder to a shimmed module binds to the shim";
595 write_image_as(make_forwarder_image("testshim.F", 0x180000000), "prov_shim.dll");
596 auto importer = write_image_as(make_import_image("prov_shim.dll", "fwd"), "importer_shim.exe");
597 try {
598 rt::Loader loader(g_dir);
599 rt::Module& m = loader.load(importer);
600 if (m.imports.size() != 1 || m.imports[0].binding != rt::Binding::Shim)
601 return failed(what, "the import did not bind as a shim");
602 if (*reinterpret_cast<void**>(m.base + 0x1180) != reinterpret_cast<void*>(&test_shim_function))
603 return failed(what, "the address table does not hold the shim function");
604 pass(what);
605 } catch (const std::exception& e) {
606 failed(what, e.what());
607 }
608 }
586} 609}
587 610
588void test_delay_descriptor_fields() { 611void test_delay_descriptor_fields() {
@@ -631,6 +654,90 @@ void test_delay_descriptor_fields() {
631 } 654 }
632} 655}
633 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
634} // namespace 741} // namespace
635 742
636int main() { 743int main() {
@@ -655,6 +762,7 @@ int main() {
655 test_initialise_rejects(); 762 test_initialise_rejects();
656 test_delay_descriptor_fields(); 763 test_delay_descriptor_fields();
657 test_forwarded_exports(); 764 test_forwarded_exports();
765 test_unwind_registration();
658 766
659 std::filesystem::remove_all(g_dir, ec); 767 std::filesystem::remove_all(g_dir, ec);
660 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);