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:
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp35
1 files changed, 26 insertions, 9 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index a49a75385..926becd12 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -654,10 +654,11 @@ 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 657// IMAGE_FILE_RELOCS_STRIPPED against a relocation directory, when the preferred base is taken. Measured on the
658// move an image only when IMAGE_FILE_RELOCS_STRIPPED is set; with the flag clear it maps it elsewhere and fixes up 658// Windows 11 (26200) loader with synthetic DLLs (#8847): with no relocation directory the flag refuses the move and
659// nothing, as it does for a second copy of such a DLL. The DllMain stub stores through an absolute address in this 659// an image with the flag clear is moved with nothing fixed up; with a relocation table the image is moved and fixed
660// process, not in the image, so it runs correctly wherever the image lands. 660// up whatever the flag says. The DllMain stub stores through an absolute address in this process, not in the image,
661// so it runs correctly wherever the image lands.
661void test_moved_without_relocations() { 662void test_moved_without_relocations() {
662 auto img = make_image(); 663 auto img = make_image();
663 auto* nt = nt_of(img); 664 auto* nt = nt_of(img);
@@ -704,13 +705,16 @@ void test_moved_without_relocations() {
704 } 705 }
705 if (held) VirtualFree(held, 0, MEM_RELEASE); 706 if (held) VirtualFree(held, 0, MEM_RELEASE);
706 { 707 {
707 // The flag only forbids moving: at a free base such an image maps as usual. 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 (#8847); runtime/README.md keeps the question.
708 auto stripped = img; 710 auto stripped = img;
709 nt_of(stripped)->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; 711 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); 712 expect_loads("an image with IMAGE_FILE_RELOCS_STRIPPED and no relocations maps at its free base", stripped);
711 } 713 }
712 { 714 {
713 // Windows refuses on the flag, whatever the relocation directory holds. ImageBase 0 makes the loader move it. 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";
714 auto stripped = make_image(); 718 auto stripped = make_image();
715 auto* snt = nt_of(stripped); 719 auto* snt = nt_of(stripped);
716 snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED; 720 snt->FileHeader.Characteristics |= IMAGE_FILE_RELOCS_STRIPPED;
@@ -719,8 +723,21 @@ void test_moved_without_relocations() {
719 auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200); 723 auto* block = reinterpret_cast<IMAGE_BASE_RELOCATION*>(stripped.data() + 0x200);
720 block->VirtualAddress = 0x1000; 724 block->VirtualAddress = 0x1000;
721 block->SizeOfBlock = 0x0A; 725 block->SizeOfBlock = 0x0A;
722 reinterpret_cast<uint16_t*>(block + 1)[0] = IMAGE_REL_BASED_DIR64 << 12; 726 reinterpret_cast<uint16_t*>(block + 1)[0] = (IMAGE_REL_BASED_DIR64 << 12) | 0x100;
723 expect_rejects("an image with IMAGE_FILE_RELOCS_STRIPPED and a relocation table is not moved", stripped); 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 }
724 } 741 }
725} 742}
726 743