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:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 19:04:24 -0500
commit8d832fd10913064e1c8aec74191e3ba8f70211c8 (patch)
treee365199d7d86ebf592a5a0dc2f07cda4b74b58a7 /runtime/test/loader_test.cpp
parent5da3b6be3cefc1499452ba8c8370a50092674d61 (diff)
parentc50ed94b35b37b697ab9c5fd176f3738480b4db5 (diff)
Merge main into rt-title-every-budget (#2122)
#2098 split the every-pass loop of refresh_title_textures by mode: with LEONIDA_TEXTURE_REFRESH_ASYNC every persistent mapping is polled each pass and only stale unmapped textures are visited, without it all mapped or stale ones are. The async branch is kept as main has it. LEONIDA_TITLE_EVERY_BUDGET_US applies to the synchronous pass, the one before a submission on the submitting thread; under async the pass runs on the refresh worker and the limit does not apply (README says so). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index d985ba423..6930878a9 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -654,6 +654,93 @@ void test_initialise_rejects() {
654 } 654 }
655} 655}
656 656
657// IMAGE_FILE_RELOCS_STRIPPED against a relocation directory, when the preferred base is taken. Measured on the
658// Windows 11 (26200) loader with synthetic DLLs (runtime/README.md): with no relocation directory the flag refuses
659// the move and an image with the flag clear is moved with nothing fixed up; with a relocation table the image is
660// moved and fixed up whatever the flag says. The DllMain stub stores through an absolute address in this process,
661// not in the image, so it runs correctly wherever the image lands.
662void test_moved_without_relocations() {
663 auto img = make_image();
664 auto* nt = nt_of(img);
665 nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;
666 nt->OptionalHeader.ImageBase = leonida_test::free_image_base(0x1C0000000);
667 nt->OptionalHeader.AddressOfEntryPoint = 0x1040;
668 emit_store_stub(img, 0x240, reinterpret_cast<uint64_t>(&g_main_ran), 5, 1);
669 const uint64_t preferred = nt->OptionalHeader.ImageBase;
670 void* held = VirtualAlloc(reinterpret_cast<void*>(preferred), 0x2000, MEM_RESERVE, PAGE_NOACCESS);
671 if (!held) {
672 // Something else holds it already, which is the case under test as well; but make sure it is held.
673 MEMORY_BASIC_INFORMATION info{};
674 if (!VirtualQuery(reinterpret_cast<void*>(preferred), &info, sizeof info) || info.State == MEM_FREE)
675 return failed("an image without relocations moves when its base is taken", "cannot hold its base");
676 }
677 {
678 const char* what = "an image without relocations, flag clear, moves when its base is taken";
679 g_main_ran = 0;
680 auto p = write_image(img);
681 try {
682 rt::Loader loader(g_dir);
683 rt::Module& m = loader.load(p);
684 if (reinterpret_cast<uint64_t>(m.base) == preferred || !m.relocated)
685 failed(what, "it was not moved");
686 else if (std::memcmp(m.base + 0x1000, text_raw(img), 0x200) != 0)
687 failed(what, "the mapped section doesn't match the file (something was fixed up)");
688 else {
689 loader.initialise();
690 if (g_main_ran != 5) failed(what, "its DllMain never ran at the new address");
691 else pass(what);
692 }
693 } catch (const std::exception& e) {
694 failed(what, e.what());
695 }
696 }
697 {
698 auto stripped = img;
699 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
700 std::string error;
701 const char* what = "an image with IMAGE_FILE_RELOCS_STRIPPED is refused when its base is taken";
702 if (load(write_image(stripped), error) == 0) failed(what, "the image loaded");
703 else if (error.find("stripped") == std::string::npos) failed(what, "refused for another reason: " + error);
704 else pass(what);
705 }
706 if (held) VirtualFree(held, 0, MEM_RELEASE);
707 {
708 // At a free base such an image maps, as it did before. Not Windows parity: the Windows 11 loader refused these
709 // DLLs (error 487) at a free base too, for a reason not yet known; runtime/README.md keeps the question.
710 auto stripped = img;
711 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
712 expect_loads("an image with IMAGE_FILE_RELOCS_STRIPPED and no relocations maps at its free base", stripped);
713 }
714 {
715 // With a relocation table the flag does not stop the move: the table is applied, as Windows does it.
716 // ImageBase 0 makes the loader move it; the one DIR64 entry points at the qword at RVA 0x1100.
717 const char* what = "an image with IMAGE_FILE_RELOCS_STRIPPED and a relocation table is moved and fixed up";
718 auto stripped = make_image();
719 auto* snt = nt_of(stripped);
720 snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
721 snt->OptionalHeader.ImageBase = 0;
722 snt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] = {0x1000, 0x0A};
723 auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200);
724 block->VirtualAddress = 0x1000;
725 block->SizeOfBlock = 0x0A;
726 reinterpret_cast<uint16_t*>(block + 1)[0] = (IMAGE_REL_BASED_DIR64 << 12) | 0x100;
727 uint64_t original;
728 std::memcpy(&original, stripped.data() + 0x300, sizeof original);
729 auto p = write_image(stripped);
730 try {
731 rt::Loader loader(g_dir);
732 rt::Module& m = loader.load(p);
733 uint64_t moved;
734 std::memcpy(&moved, m.base + 0x1100, sizeof moved);
735 if (!m.relocated) failed(what, "it was not moved");
736 else if (moved != original + reinterpret_cast<uint64_t>(m.base)) failed(what, "the pointer wasn't fixed up");
737 else pass(what);
738 } catch (const std::exception& e) {
739 failed(what, e.what());
740 }
741 }
742}
743
657// The images below jump to host observers so notifications remain observable after the mappings are freed. 744// The images below jump to host observers so notifications remain observable after the mappings are freed.
658// The FLS destructor itself starts in mapped code: leaving it registered would call unmapped memory at exit. 745// The FLS destructor itself starts in mapped code: leaving it registered would call unmapped memory at exit.
659void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) { 746void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) {
@@ -1471,6 +1558,7 @@ int main() {
1471 test_section_bounds(); 1558 test_section_bounds();
1472 test_truncation(); 1559 test_truncation();
1473 test_relocations(); 1560 test_relocations();
1561 test_moved_without_relocations();
1474 test_exports(); 1562 test_exports();
1475 test_empty_named_export(); 1563 test_empty_named_export();
1476 test_imports(); 1564 test_imports();