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:
authorVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 23:21:41 +0100
committerVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 23:21:41 +0100
commit3986b50821272791333c00df80bdb8526a8fdb9c (patch)
treeeef468ac670636d2736815da64982834b289ee84 /runtime/test/loader_test.cpp
parentdd0abd45b33e77b887e8f910a0a74f8cb5b0a32a (diff)
parentaa2988cc555bad7ecd508b48a702adbb32fe7c95 (diff)
Merge main into hw4-ps-oracle (#3061)
docs/shader_validation.md AI-assisted.
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp123
1 files changed, 116 insertions, 7 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 73bc49206..71a5ede73 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -20,6 +20,7 @@
20 20
21#include "../shims/shims.h" 21#include "../shims/shims.h"
22#include "../src/pe_loader.h" 22#include "../src/pe_loader.h"
23#include "free_image_base.h"
23 24
24namespace { 25namespace {
25 26
@@ -56,7 +57,7 @@ std::vector<uint8_t> make_image() {
56 nt->FileHeader.NumberOfSections = 1; 57 nt->FileHeader.NumberOfSections = 1;
57 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64); 58 nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64);
58 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC; 59 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
59 nt->OptionalHeader.ImageBase = 0x140000000; 60 nt->OptionalHeader.ImageBase = leonida_test::free_image_base(0x140000000);
60 nt->OptionalHeader.AddressOfEntryPoint = 0x1000; 61 nt->OptionalHeader.AddressOfEntryPoint = 0x1000;
61 nt->OptionalHeader.SizeOfHeaders = 0x200; 62 nt->OptionalHeader.SizeOfHeaders = 0x200;
62 nt->OptionalHeader.SizeOfImage = 0x2000; 63 nt->OptionalHeader.SizeOfImage = 0x2000;
@@ -101,7 +102,7 @@ std::filesystem::path write_image_as(const std::vector<uint8_t>& bytes, const st
101std::vector<uint8_t> make_forwarder_image(const char* target, uint64_t image_base) { 102std::vector<uint8_t> make_forwarder_image(const char* target, uint64_t image_base) {
102 auto img = make_image(); 103 auto img = make_image();
103 std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0}); // real tables end in a zero descriptor 104 std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0}); // real tables end in a zero descriptor
104 nt_of(img)->OptionalHeader.ImageBase = image_base; 105 nt_of(img)->OptionalHeader.ImageBase = leonida_test::free_image_base(image_base);
105 auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200); 106 auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200);
106 ed->Base = 1; 107 ed->Base = 1;
107 ed->NumberOfFunctions = 1; 108 ed->NumberOfFunctions = 1;
@@ -381,6 +382,56 @@ void test_exports() {
381 } 382 }
382} 383}
383 384
385void test_empty_named_export() {
386 const char* what = "an empty named export stays unresolved without hiding valid exports";
387 auto img = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000);
388 auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200);
389 ed->NumberOfFunctions = 3;
390 ed->NumberOfNames = 3;
391 nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = 0x1A0;
392 auto* funcs = reinterpret_cast<uint32_t*>(img.data() + 0x300);
393 funcs[1] = 0;
394 funcs[2] = 0x11C0; // a code export outside the export directory
395 auto* names = reinterpret_cast<uint32_t*>(img.data() + 0x320);
396 names[0] = 0x1188;
397 names[1] = 0x1180;
398 names[2] = 0x1190;
399 auto* ords = reinterpret_cast<uint16_t*>(img.data() + 0x340);
400 ords[0] = 1;
401 ords[1] = 0;
402 ords[2] = 2;
403 std::memcpy(img.data() + 0x388, "empty\0", 6);
404 std::memcpy(img.data() + 0x390, "valid\0", 6);
405 auto provider = write_image_as(img, "empty_exports.dll");
406 try {
407 rt::Loader loader(g_dir);
408 rt::Module& m = loader.load(provider);
409 for (const char* sym : {"empty", "#2"}) {
410 bool owns = false;
411 if (m.exports.count(sym) || m.forwarders.count(sym) ||
412 loader.resolve_module_export(m.base, sym, owns) || !owns)
413 return failed(what, std::string(sym) + " did not remain absent from the mapped module");
414 }
415 for (const char* sym : {"valid", "#3", "fwd", "#1"}) {
416 bool owns = false;
417 void* expected = (std::strcmp(sym, "valid") == 0 || std::strcmp(sym, "#3") == 0)
418 ? m.base + 0x11C0 : host_symbol("kernel32.dll", "GetCurrentProcessId");
419 if (!expected || loader.resolve_module_export(m.base, sym, owns) != expected || !owns)
420 return failed(what, std::string(sym) + " no longer resolves to its valid target");
421 }
422 auto importer = write_image_as(make_import_image("empty_exports.dll", "empty"), "empty_import.exe");
423 rt::Module& importing = loader.load(importer);
424 if (importing.imports.size() != 1 || importing.imports[0].binding != rt::Binding::Unimplemented)
425 return failed(what, "an import of the empty slot did not bind as unimplemented");
426 void* bound = *reinterpret_cast<void**>(importing.base + 0x1180);
427 if (!bound || bound == m.base)
428 return failed(what, "the empty import was not given a failure thunk");
429 pass(what);
430 } catch (const std::exception& e) {
431 failed(what, e.what());
432 }
433}
434
384void test_imports() { 435void test_imports() {
385 { 436 {
386 auto img = make_image(); 437 auto img = make_image();
@@ -419,6 +470,42 @@ void test_imports() {
419 } 470 }
420} 471}
421 472
473void test_import_name_rvas() {
474 for (bool delay : {false, true}) {
475 auto img = make_import_image("testshim.dll", "F");
476 if (delay) {
477 std::fill(img.begin() + 0x200, img.begin() + 0x240, uint8_t{0});
478 auto* d = reinterpret_cast<IMAGE_DELAYLOAD_DESCRIPTOR*>(img.data() + 0x200);
479 d->Attributes.AllAttributes = 1;
480 d->DllNameRVA = 0x1060;
481 d->ImportNameTableRVA = 0x1100;
482 d->ImportAddressTableRVA = 0x1180;
483 auto* nt = nt_of(img);
484 nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] = {};
485 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT];
486 dir.VirtualAddress = 0x1000;
487 dir.Size = 2 * sizeof(IMAGE_DELAYLOAD_DESCRIPTOR);
488 }
489 const char* valid = delay ? "a valid delay-import name RVA loads" : "a valid import name RVA loads";
490 std::string error;
491 if (load(write_image(img), error) != 0)
492 failed(valid, error);
493 else
494 pass(valid);
495
496 *reinterpret_cast<uint64_t*>(img.data() + 0x300) = (1ull << 32) | 0x1120;
497 const char* invalid = delay ? "a delay-import name RVA with high bits is rejected"
498 : "an import name RVA with high bits is rejected";
499 error.clear();
500 if (load(write_image(img), error) == 0)
501 failed(invalid, "the image loaded");
502 else if (error.find("an import name runs past the image") == std::string::npos)
503 failed(invalid, error);
504 else
505 pass(invalid);
506 }
507}
508
422void test_delay_imports() { 509void test_delay_imports() {
423 { 510 {
424 auto img = make_image(); 511 auto img = make_image();
@@ -469,8 +556,6 @@ void emit_store_stub(std::vector<uint8_t>& img, size_t off, uint64_t addr, uint3
469 img[n++] = 0xC3; 556 img[n++] = 0xC3;
470} 557}
471 558
472constexpr uint64_t kInitBase = 0x140000000;
473
474// make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a 559// make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a
475// sentinel in this process, so the test can tell a callback ran rather than only that it was parsed. 560// sentinel in this process, so the test can tell a callback ran rather than only that it was parsed.
476std::vector<uint8_t> make_init_image() { 561std::vector<uint8_t> make_init_image() {
@@ -478,7 +563,8 @@ std::vector<uint8_t> make_init_image() {
478 auto* nt = nt_of(img); 563 auto* nt = nt_of(img);
479 nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; 564 nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;
480 nt->OptionalHeader.AddressOfEntryPoint = 0x1040; 565 nt->OptionalHeader.AddressOfEntryPoint = 0x1040;
481 auto va = [](uint32_t rva) { return kInitBase + rva; }; 566 const uint64_t base = nt->OptionalHeader.ImageBase; // the TLS callback addresses below are absolute
567 auto va = [base](uint32_t rva) { return base + rva; };
482 constexpr size_t text = 0x200; // file offset of RVA 0x1000 568 constexpr size_t text = 0x200; // file offset of RVA 0x1000
483 emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0); 569 emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0);
484 emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1); 570 emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1);
@@ -555,7 +641,7 @@ void test_initialise_rejects() {
555 { 641 {
556 auto img = make_init_image(); 642 auto img = make_init_image();
557 auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090 643 auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090
558 callbacks[0] = kInitBase + 0x9000; // well past SizeOfImage 644 callbacks[0] = nt_of(img)->OptionalHeader.ImageBase + 0x9000; // well past SizeOfImage
559 expect_init_rejects("a TLS callback outside the image is rejected", img); 645 expect_init_rejects("a TLS callback outside the image is rejected", img);
560 } 646 }
561 { 647 {
@@ -753,7 +839,7 @@ void test_process_detach_without_entry() {
753 reset_notifications(); 839 reset_notifications();
754 auto first = make_lifecycle_image(); 840 auto first = make_lifecycle_image();
755 auto second = make_image(); 841 auto second = make_image();
756 nt_of(second)->OptionalHeader.ImageBase = 0x180000000; 842 nt_of(second)->OptionalHeader.ImageBase = leonida_test::free_image_base(0x180000000);
757 nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL; 843 nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL;
758 emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain)); 844 emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain));
759 void* first_base = nullptr; 845 void* first_base = nullptr;
@@ -805,6 +891,26 @@ void test_process_detach_dependencies() {
805 g_live_modules.clear(); 891 g_live_modules.clear();
806} 892}
807 893
894// A dependency whose import table is corrupt fails while the parent binds it. Neither must stay findable by name:
895// a later load or GetModuleHandle would otherwise get a mapped image whose imports were never bound.
896void test_failed_dependency_not_registered() {
897 const char* what = "a failed load leaves neither the module nor its failed dependency loaded";
898 auto dep = make_import_image("kernel32.dll", "GetCurrentProcessId");
899 reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(dep.data() + 0x200)->Name = 0x5000; // past SizeOfImage
900 const auto dep_path = write_image_as(dep, "fail_dep.dll");
901 const auto parent_path = write_image_as(make_import_image("fail_dep.dll", "x"), "fail_parent.dll");
902 rt::Loader loader(g_dir);
903 auto throws = [&](const std::filesystem::path& p) {
904 try { loader.load(p); return false; } catch (const std::exception&) { return true; }
905 };
906 if (!throws(parent_path)) return failed(what, "the parent loaded over a corrupt dependency");
907 if (loader.find_loaded_module("fail_dep.dll") || loader.find_loaded_module("fail_parent.dll"))
908 return failed(what, "a failed module is still found by name");
909 if (!throws(dep_path)) return failed(what, "a second load of the dependency returned the half-loaded module");
910 if (!throws(parent_path)) return failed(what, "a second load of the parent returned the half-loaded module");
911 pass(what);
912}
913
808void test_forwarded_exports() { 914void test_forwarded_exports() {
809 { 915 {
810 const char* what = "a forwarded export binds to its target instead of into the string"; 916 const char* what = "a forwarded export binds to its target instead of into the string";
@@ -1134,7 +1240,9 @@ int main() {
1134 test_truncation(); 1240 test_truncation();
1135 test_relocations(); 1241 test_relocations();
1136 test_exports(); 1242 test_exports();
1243 test_empty_named_export();
1137 test_imports(); 1244 test_imports();
1245 test_import_name_rvas();
1138 test_delay_imports(); 1246 test_delay_imports();
1139 test_initialise(); 1247 test_initialise();
1140 test_initialise_rejects(); 1248 test_initialise_rejects();
@@ -1142,6 +1250,7 @@ int main() {
1142 test_process_detach_invalid_tls(); 1250 test_process_detach_invalid_tls();
1143 test_process_detach_without_entry(); 1251 test_process_detach_without_entry();
1144 test_process_detach_dependencies(); 1252 test_process_detach_dependencies();
1253 test_failed_dependency_not_registered();
1145 test_delay_descriptor_fields(); 1254 test_delay_descriptor_fields();
1146 test_forwarded_exports(); 1255 test_forwarded_exports();
1147 test_export_bounds(); 1256 test_export_bounds();