diff options
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 71 |
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. | ||
| 661 | void 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. |
| 659 | void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) { | 729 | void 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(); |