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-23 14:36:22 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-24 04:58:41 -0500
commit0ddf806359d30022a3152e61c93ed077212d25aa (patch)
treecfad9153849f453afbde3123b8e61afd8e436c5a /runtime/test/loader_test.cpp
parent21625bca8c675c62f9dbffc318ce6a59d57faf31 (diff)
pe_loader: move an image with no relocation directory, refuse only on IMAGE_FILE_RELOCS_STRIPPED
The loader refused any image whose preferred base was taken when it had no relocation directory. Windows' loader decides on the file header flag instead (LdrRelocateImage, the same in Wine's perform_relocations and ReactOS's LdrRelocateImageWithBias): IMAGE_FILE_RELOCS_STRIPPED set means the image cannot move and is refused (STATUS_CONFLICTING_ADDRESSES); with the flag clear and an empty directory the image is moved with nothing to fix up, as a second copy of such a DLL is. map_image now does the same, for an EXE and a DLL alike: it refuses to move a flag-set image (also one that has a relocation table, as Windows does), moves a flag-clear image without a directory and logs one "[rt] loader: ... moved ... with no relocations" line, so a move without fix-ups shows in traces. base_reservation::needed keeps reserving the main image's range for both kinds of image without relocations, and now also for a flag-set image with a table, so the main image keeps its base wherever it did before. The retail executable and every package DLL have a relocation table and the flag clear, so neither path applies to them. loader_test covers a flag-clear image moved when its base is held (it loads elsewhere, nothing is fixed up, its DllMain runs), the same image with the flag set (refused), the flag at a free base (maps), and the flag with a relocation table (refused). test_failed_dependency_not_registered's dependency shared its parent's base and was refused for that; it now fails on its corrupt import table, which is what the test describes. 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.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index d985ba423..a49a75385 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -654,6 +654,76 @@ void test_initialise_rejects() {
654 } 654 }
655} 655}
656 656
657// An image with no relocation directory whose preferred base is taken. Windows' loader (LdrRelocateImage) refuses to
658// move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the flag clear it maps it elsewhere and fixes up
659// nothing, as it does for a second copy of such a DLL. The DllMain stub stores through an absolute address in this
660// process, not in the image, so it runs correctly wherever the image lands.
661void test_moved_without_relocations() {
662 auto img = make_image();
663 auto* nt = nt_of(img);
664 nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;
665 nt->OptionalHeader.ImageBase = leonida_test::free_image_base(0x1C0000000);
666 nt->OptionalHeader.AddressOfEntryPoint = 0x1040;
667 emit_store_stub(img, 0x240, reinterpret_cast<uint64_t>(&g_main_ran), 5, 1);
668 const uint64_t preferred = nt->OptionalHeader.ImageBase;
669 void* held = VirtualAlloc(reinterpret_cast<void*>(preferred), 0x2000, MEM_RESERVE, PAGE_NOACCESS);
670 if (!held) {
671 // Something else holds it already, which is the case under test as well; but make sure it is held.
672 MEMORY_BASIC_INFORMATION info{};
673 if (!VirtualQuery(reinterpret_cast<void*>(preferred), &info, sizeof info) || info.State == MEM_FREE)
674 return failed("an image without relocations moves when its base is taken", "cannot hold its base");
675 }
676 {
677 const char* what = "an image without relocations, flag clear, moves when its base is taken";
678 g_main_ran = 0;
679 auto p = write_image(img);
680 try {
681 rt::Loader loader(g_dir);
682 rt::Module& m = loader.load(p);
683 if (reinterpret_cast<uint64_t>(m.base) == preferred || !m.relocated)
684 failed(what, "it was not moved");
685 else if (std::memcmp(m.base + 0x1000, text_raw(img), 0x200) != 0)
686 failed(what, "the mapped section doesn't match the file (something was fixed up)");
687 else {
688 loader.initialise();
689 if (g_main_ran != 5) failed(what, "its DllMain never ran at the new address");
690 else pass(what);
691 }
692 } catch (const std::exception& e) {
693 failed(what, e.what());
694 }
695 }
696 {
697 auto stripped = img;
698 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
699 std::string error;
700 const char* what = "an image with IMAGE_FILE_RELOCS_STRIPPED is refused when its base is taken";
701 if (load(write_image(stripped), error) == 0) failed(what, "the image loaded");
702 else if (error.find("stripped") == std::string::npos) failed(what, "refused for another reason: " + error);
703 else pass(what);
704 }
705 if (held) VirtualFree(held, 0, MEM_RELEASE);
706 {
707 // The flag only forbids moving: at a free base such an image maps as usual.
708 auto stripped = img;
709 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
710 expect_loads("an image with IMAGE_FILE_RELOCS_STRIPPED maps at its free base", stripped);
711 }
712 {
713 // Windows refuses on the flag, whatever the relocation directory holds. ImageBase 0 makes the loader move it.
714 auto stripped = make_image();
715 auto* snt = nt_of(stripped);
716 snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
717 snt->OptionalHeader.ImageBase = 0;
718 snt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] = {0x1000, 0x0A};
719 auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200);
720 block->VirtualAddress = 0x1000;
721 block->SizeOfBlock = 0x0A;
722 reinterpret_cast<uint16_t*>(block + 1)[0] = IMAGE_REL_BASED_DIR64 << 12;
723 expect_rejects("an image with IMAGE_FILE_RELOCS_STRIPPED and a relocation table is not moved", stripped);
724 }
725}
726
657// The images below jump to host observers so notifications remain observable after the mappings are freed. 727// 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. 728// 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) { 729void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) {
@@ -1471,6 +1541,7 @@ int main() {
1471 test_section_bounds(); 1541 test_section_bounds();
1472 test_truncation(); 1542 test_truncation();
1473 test_relocations(); 1543 test_relocations();
1544 test_moved_without_relocations();
1474 test_exports(); 1545 test_exports();
1475 test_empty_named_export(); 1546 test_empty_named_export();
1476 test_imports(); 1547 test_imports();