diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 15:17:51 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 15:17:51 -0500 |
| commit | da4d686c7a15bc6de6d843fe6525e59c2a99113f (patch) | |
| tree | e1cda7f49735df00e9bbffee47e70097952c18fa /runtime/test/loader_test.cpp | |
| parent | 0a65e6a2ba0c9fb49fafe33f3a95b9f99334e0bb (diff) | |
| parent | ff573ee55ecc402ca2a4242f3b9a43edbb146638 (diff) | |
Merge pull request #122 from Ayinaki
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index b6f6e4912..b05f35b15 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | // a message instead of being followed out of bounds. The truncation case covers every length of a valid image. | 5 | // a message instead of being followed out of bounds. The truncation case covers every length of a valid image. |
| 6 | #include <windows.h> | 6 | #include <windows.h> |
| 7 | 7 | ||
| 8 | #include <algorithm> | ||
| 8 | #include <cstdio> | 9 | #include <cstdio> |
| 9 | #include <cstring> | 10 | #include <cstring> |
| 10 | #include <filesystem> | 11 | #include <filesystem> |
| @@ -81,6 +82,61 @@ std::filesystem::path write_image(const std::vector<uint8_t>& bytes) { | |||
| 81 | return p; | 82 | return p; |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 85 | std::filesystem::path write_image_as(const std::vector<uint8_t>& bytes, const std::string& name) { | ||
| 86 | auto p = g_dir / name; | ||
| 87 | std::ofstream out(p, std::ios::binary); | ||
| 88 | if (!out) throw std::runtime_error("cannot write " + p.string()); | ||
| 89 | out.write(reinterpret_cast<const char*>(bytes.data()), static_cast<long long>(bytes.size())); | ||
| 90 | return p; | ||
| 91 | } | ||
| 92 | |||
| 93 | // An image whose only export, "fwd", forwards to `target`: the address-table entry points at a string inside the | ||
| 94 | // export directory, which is how a forwarder is encoded. | ||
| 95 | std::vector<uint8_t> make_forwarder_image(const char* target, uint64_t image_base) { | ||
| 96 | auto img = make_image(); | ||
| 97 | std::fill(img.begin() + 0x200, img.begin() + 0x400, 0); // real tables end in a zero descriptor | ||
| 98 | nt_of(img)->OptionalHeader.ImageBase = image_base; | ||
| 99 | auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200); | ||
| 100 | ed->Base = 1; | ||
| 101 | ed->NumberOfFunctions = 1; | ||
| 102 | ed->AddressOfFunctions = 0x1100; | ||
| 103 | ed->NumberOfNames = 1; | ||
| 104 | ed->AddressOfNames = 0x1120; | ||
| 105 | ed->AddressOfNameOrdinals = 0x1140; | ||
| 106 | *reinterpret_cast<uint32_t*>(img.data() + 0x300) = 0x1160; // funcs[0]: inside the export directory | ||
| 107 | *reinterpret_cast<uint32_t*>(img.data() + 0x320) = 0x1180; // the export's name | ||
| 108 | *reinterpret_cast<uint16_t*>(img.data() + 0x340) = 0; | ||
| 109 | std::memcpy(img.data() + 0x360, target, std::strlen(target) + 1); | ||
| 110 | std::memcpy(img.data() + 0x380, "fwd\0", 4); | ||
| 111 | auto& dir = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | ||
| 112 | dir.VirtualAddress = 0x1000; | ||
| 113 | dir.Size = 0x200; // covers the forwarder string at RVA 0x1160 | ||
| 114 | return img; | ||
| 115 | } | ||
| 116 | |||
| 117 | // An image importing `dll`!`symbol` by name. | ||
| 118 | std::vector<uint8_t> make_import_image(const char* dll, const char* symbol) { | ||
| 119 | auto img = make_image(); | ||
| 120 | std::fill(img.begin() + 0x200, img.begin() + 0x400, 0); | ||
| 121 | auto* d = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(img.data() + 0x200); | ||
| 122 | d->Name = 0x1060; // past the zero descriptor that ends the table | ||
| 123 | d->OriginalFirstThunk = 0x1100; | ||
| 124 | d->FirstThunk = 0x1180; // the address table the loader fills | ||
| 125 | *reinterpret_cast<uint64_t*>(img.data() + 0x300) = 0x1120; // hint/name | ||
| 126 | *reinterpret_cast<uint16_t*>(img.data() + 0x320) = 0; | ||
| 127 | std::memcpy(img.data() + 0x322, symbol, std::strlen(symbol) + 1); | ||
| 128 | std::memcpy(img.data() + 0x260, dll, std::strlen(dll) + 1); | ||
| 129 | auto& dir = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]; | ||
| 130 | dir.VirtualAddress = 0x1000; | ||
| 131 | dir.Size = sizeof(IMAGE_IMPORT_DESCRIPTOR); | ||
| 132 | return img; | ||
| 133 | } | ||
| 134 | |||
| 135 | void* host_symbol(const char* dll, const char* name) { | ||
| 136 | HMODULE h = GetModuleHandleA(dll); | ||
| 137 | return h ? reinterpret_cast<void*>(GetProcAddress(h, name)) : nullptr; | ||
| 138 | } | ||
| 139 | |||
| 84 | // 0 when the image loaded, 1 when it was rejected. A crash here is a test failure, which is the point. | 140 | // 0 when the image loaded, 1 when it was rejected. A crash here is a test failure, which is the point. |
| 85 | int load(const std::filesystem::path& p, std::string& error) { | 141 | int load(const std::filesystem::path& p, std::string& error) { |
| 86 | try { | 142 | try { |
| @@ -467,6 +523,114 @@ void test_initialise_rejects() { | |||
| 467 | } | 523 | } |
| 468 | } | 524 | } |
| 469 | 525 | ||
| 526 | void test_forwarded_exports() { | ||
| 527 | { | ||
| 528 | const char* what = "a forwarded export binds to its target instead of into the string"; | ||
| 529 | write_image_as(make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000), "prov.dll"); | ||
| 530 | auto importer = write_image_as(make_import_image("prov.dll", "fwd"), "importer.exe"); | ||
| 531 | try { | ||
| 532 | rt::Loader loader(g_dir); | ||
| 533 | rt::Module& m = loader.load(importer); | ||
| 534 | void* expected = host_symbol("kernel32.dll", "GetCurrentProcessId"); | ||
| 535 | if (!expected) return failed(what, "GetCurrentProcessId is not loaded in this process"); | ||
| 536 | if (m.imports.size() != 1) | ||
| 537 | return failed(what, "the importer has " + std::to_string(m.imports.size()) + " import record(s)"); | ||
| 538 | if (m.imports[0].binding != rt::Binding::Host) | ||
| 539 | return failed(what, "the import did not bind through the forwarder to the host"); | ||
| 540 | if (*reinterpret_cast<void**>(m.base + 0x1180) != expected) | ||
| 541 | return failed(what, "the address table does not hold the forwarded target"); | ||
| 542 | rt::Module* prov = nullptr; | ||
| 543 | for (auto* mod : loader.modules()) | ||
| 544 | if (mod->name == "prov.dll") prov = mod; | ||
| 545 | if (!prov) return failed(what, "the provider module was not loaded"); | ||
| 546 | if (prov->exports.count("fwd")) return failed(what, "the forwarder was stored as a code export"); | ||
| 547 | auto it = prov->forwarders.find("fwd"); | ||
| 548 | if (it == prov->forwarders.end() || it->second != "KERNEL32.GetCurrentProcessId") | ||
| 549 | return failed(what, "the forwarder string was not recorded"); | ||
| 550 | pass(what); | ||
| 551 | } catch (const std::exception& e) { | ||
| 552 | failed(what, e.what()); | ||
| 553 | } | ||
| 554 | } | ||
| 555 | { | ||
| 556 | const char* what = "a forwarder to a module that isn't there binds as unimplemented"; | ||
| 557 | write_image_as(make_forwarder_image("NOPE_MISSING_TARGET.f", 0x180000000), "prov.dll"); | ||
| 558 | auto importer = write_image_as(make_import_image("prov.dll", "fwd"), "importer.exe"); | ||
| 559 | try { | ||
| 560 | rt::Loader loader(g_dir); | ||
| 561 | rt::Module& m = loader.load(importer); | ||
| 562 | if (m.imports.size() != 1 || m.imports[0].binding != rt::Binding::Unimplemented) | ||
| 563 | return failed(what, "the import did not end up unimplemented"); | ||
| 564 | if (!*reinterpret_cast<void**>(m.base + 0x1180)) | ||
| 565 | return failed(what, "the address table was left null instead of a failure thunk"); | ||
| 566 | pass(what); | ||
| 567 | } catch (const std::exception& e) { | ||
| 568 | failed(what, e.what()); | ||
| 569 | } | ||
| 570 | } | ||
| 571 | { | ||
| 572 | const char* what = "forwarders that point at each other stop instead of recursing"; | ||
| 573 | write_image_as(make_forwarder_image("prov_b.fwd", 0x180000000), "prov_a.dll"); | ||
| 574 | write_image_as(make_forwarder_image("prov_a.fwd", 0x190000000), "prov_b.dll"); | ||
| 575 | auto importer = write_image_as(make_import_image("prov_a.dll", "fwd"), "importer.exe"); | ||
| 576 | try { | ||
| 577 | rt::Loader loader(g_dir); | ||
| 578 | rt::Module& m = loader.load(importer); | ||
| 579 | if (m.imports.size() != 1 || m.imports[0].binding != rt::Binding::Unimplemented) | ||
| 580 | return failed(what, "a forwarder loop did not end as unimplemented"); | ||
| 581 | pass(what); | ||
| 582 | } catch (const std::exception& e) { | ||
| 583 | failed(what, e.what()); | ||
| 584 | } | ||
| 585 | } | ||
| 586 | } | ||
| 587 | |||
| 588 | void test_delay_descriptor_fields() { | ||
| 589 | { | ||
| 590 | // Without dlattrRva the fields are 32-bit virtual addresses, which cannot address an x64 image. | ||
| 591 | auto img = make_image(); | ||
| 592 | std::fill(img.begin() + 0x200, img.begin() + 0x400, 0); | ||
| 593 | auto* d = reinterpret_cast<IMAGE_DELAYLOAD_DESCRIPTOR*>(img.data() + 0x200); | ||
| 594 | d->Attributes.AllAttributes = 0; | ||
| 595 | d->DllNameRVA = 0x1060; // past the zero descriptor that ends the table | ||
| 596 | d->ImportAddressTableRVA = 0x1180; | ||
| 597 | std::memcpy(img.data() + 0x260, "kernel32.dll\0", 13); | ||
| 598 | auto& dir = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; | ||
| 599 | dir.VirtualAddress = 0x1000; | ||
| 600 | dir.Size = sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); | ||
| 601 | expect_rejects("a delay-import descriptor without the RVA attribute", img); | ||
| 602 | } | ||
| 603 | { | ||
| 604 | const char* what = "a delay import with no name table binds through its address table"; | ||
| 605 | auto img = make_image(); | ||
| 606 | std::fill(img.begin() + 0x200, img.begin() + 0x400, 0); | ||
| 607 | auto* d = reinterpret_cast<IMAGE_DELAYLOAD_DESCRIPTOR*>(img.data() + 0x200); | ||
| 608 | d->Attributes.AllAttributes = 1; // dlattrRva | ||
| 609 | d->DllNameRVA = 0x1060; // past the zero descriptor that ends the table | ||
| 610 | d->ImportNameTableRVA = 0; | ||
| 611 | d->ImportAddressTableRVA = 0x1180; | ||
| 612 | std::memcpy(img.data() + 0x260, "kernel32.dll\0", 13); | ||
| 613 | *reinterpret_cast<uint64_t*>(img.data() + 0x380) = 0x1120; // RVA 0x1180: the single hint/name entry | ||
| 614 | *reinterpret_cast<uint16_t*>(img.data() + 0x320) = 0; | ||
| 615 | std::memcpy(img.data() + 0x322, "GetCurrentProcessId\0", 20); | ||
| 616 | auto& dir = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT]; | ||
| 617 | dir.VirtualAddress = 0x1000; | ||
| 618 | dir.Size = sizeof(IMAGE_DELAYLOAD_DESCRIPTOR); | ||
| 619 | try { | ||
| 620 | rt::Loader loader(g_dir); | ||
| 621 | rt::Module& m = loader.load(write_image_as(img, "delay.exe")); | ||
| 622 | void* expected = host_symbol("kernel32.dll", "GetCurrentProcessId"); | ||
| 623 | if (m.imports.size() != 1) return failed(what, "expected one bound import"); | ||
| 624 | if (m.imports[0].binding != rt::Binding::Host) return failed(what, "the delay import did not bind to the host"); | ||
| 625 | if (*reinterpret_cast<void**>(m.base + 0x1180) != expected) | ||
| 626 | return failed(what, "the address table does not hold the host function"); | ||
| 627 | pass(what); | ||
| 628 | } catch (const std::exception& e) { | ||
| 629 | failed(what, e.what()); | ||
| 630 | } | ||
| 631 | } | ||
| 632 | } | ||
| 633 | |||
| 470 | } // namespace | 634 | } // namespace |
| 471 | 635 | ||
| 472 | int main() { | 636 | int main() { |
| @@ -489,6 +653,8 @@ int main() { | |||
| 489 | test_delay_imports(); | 653 | test_delay_imports(); |
| 490 | test_initialise(); | 654 | test_initialise(); |
| 491 | test_initialise_rejects(); | 655 | test_initialise_rejects(); |
| 656 | test_delay_descriptor_fields(); | ||
| 657 | test_forwarded_exports(); | ||
| 492 | 658 | ||
| 493 | std::filesystem::remove_all(g_dir, ec); | 659 | std::filesystem::remove_all(g_dir, ec); |
| 494 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); | 660 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); |