diff options
| author | Ash Hopkins <ashleighhopkins87@gmail.com> | 2026-11-22 19:26:59 +0000 |
|---|---|---|
| committer | Ash Hopkins <ashleighhopkins87@gmail.com> | 2026-11-22 19:26:59 +0000 |
| commit | cc4799abdd73f4983e4e89f6586bf081083bb3e9 (patch) | |
| tree | ace433c1b444907b7791c3041bdd4174da67fee6 /runtime/test/loader_test.cpp | |
| parent | 162821017ea3c286c8bec860f3ff94f037912061 (diff) | |
runtime tests: synthetic fixed-base images take a base that is free under arm64 Wine
loader_test, test_pe_loader_bounds and unimplemented_policy_test build relocation-less images fixed at
0x140000000..0x1a0000000. Under arm64 Wine (CrossOver on Apple Silicon) the process's own allocations sit at
0x100000000 and up and macOS maps its shared region at 0x180000000 (RT-22), so the loader rejected those images
with "preferred base is taken" before the checks ran: loader stopped after 31 checks with 3 failures,
pe_loader_bounds failed 13 checks and unimplemented_policy failed.
New test/free_image_base.h: on Windows it returns the constants unchanged. Under Wine (ntdll exports
wine_get_version) it moves the whole 0x140000000..0x200000000 span to a free window picked top-down, away from
the heap, so each constant keeps its offset and images loaded together still get different bases. The init
image reads its TLS callback addresses from its own ImageBase instead of a constant.
AI-assisted. No game files.
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 194174b00..bcfb1605e 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -20,6 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | #include "../shims/shims.h" | 21 | #include "../shims/shims.h" |
| 22 | #include "../src/pe_loader.h" | 22 | #include "../src/pe_loader.h" |
| 23 | #include "free_image_base.h" | ||
| 23 | 24 | ||
| 24 | namespace { | 25 | namespace { |
| 25 | 26 | ||
| @@ -56,7 +57,7 @@ std::vector<uint8_t> make_image() { | |||
| 56 | nt->FileHeader.NumberOfSections = 1; | 57 | nt->FileHeader.NumberOfSections = 1; |
| 57 | nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64); | 58 | nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64); |
| 58 | nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC; | 59 | nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC; |
| 59 | nt->OptionalHeader.ImageBase = 0x140000000; | 60 | nt->OptionalHeader.ImageBase = leonida_test::free_image_base(0x140000000); |
| 60 | nt->OptionalHeader.AddressOfEntryPoint = 0x1000; | 61 | nt->OptionalHeader.AddressOfEntryPoint = 0x1000; |
| 61 | nt->OptionalHeader.SizeOfHeaders = 0x200; | 62 | nt->OptionalHeader.SizeOfHeaders = 0x200; |
| 62 | nt->OptionalHeader.SizeOfImage = 0x2000; | 63 | nt->OptionalHeader.SizeOfImage = 0x2000; |
| @@ -101,7 +102,7 @@ std::filesystem::path write_image_as(const std::vector<uint8_t>& bytes, const st | |||
| 101 | std::vector<uint8_t> make_forwarder_image(const char* target, uint64_t image_base) { | 102 | std::vector<uint8_t> make_forwarder_image(const char* target, uint64_t image_base) { |
| 102 | auto img = make_image(); | 103 | auto img = make_image(); |
| 103 | std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0}); // real tables end in a zero descriptor | 104 | std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0}); // real tables end in a zero descriptor |
| 104 | nt_of(img)->OptionalHeader.ImageBase = image_base; | 105 | nt_of(img)->OptionalHeader.ImageBase = leonida_test::free_image_base(image_base); |
| 105 | auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200); | 106 | auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200); |
| 106 | ed->Base = 1; | 107 | ed->Base = 1; |
| 107 | ed->NumberOfFunctions = 1; | 108 | ed->NumberOfFunctions = 1; |
| @@ -447,8 +448,6 @@ void emit_store_stub(std::vector<uint8_t>& img, size_t off, uint64_t addr, uint3 | |||
| 447 | img[n++] = 0xC3; | 448 | img[n++] = 0xC3; |
| 448 | } | 449 | } |
| 449 | 450 | ||
| 450 | constexpr uint64_t kInitBase = 0x140000000; | ||
| 451 | |||
| 452 | // make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a | 451 | // make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a |
| 453 | // sentinel in this process, so the test can tell a callback ran rather than only that it was parsed. | 452 | // sentinel in this process, so the test can tell a callback ran rather than only that it was parsed. |
| 454 | std::vector<uint8_t> make_init_image() { | 453 | std::vector<uint8_t> make_init_image() { |
| @@ -456,7 +455,8 @@ std::vector<uint8_t> make_init_image() { | |||
| 456 | auto* nt = nt_of(img); | 455 | auto* nt = nt_of(img); |
| 457 | nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; | 456 | nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; |
| 458 | nt->OptionalHeader.AddressOfEntryPoint = 0x1040; | 457 | nt->OptionalHeader.AddressOfEntryPoint = 0x1040; |
| 459 | auto va = [](uint32_t rva) { return kInitBase + rva; }; | 458 | const uint64_t base = nt->OptionalHeader.ImageBase; // the TLS callback addresses below are absolute |
| 459 | auto va = [base](uint32_t rva) { return base + rva; }; | ||
| 460 | constexpr size_t text = 0x200; // file offset of RVA 0x1000 | 460 | constexpr size_t text = 0x200; // file offset of RVA 0x1000 |
| 461 | emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0); | 461 | emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0); |
| 462 | emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1); | 462 | emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1); |
| @@ -533,7 +533,7 @@ void test_initialise_rejects() { | |||
| 533 | { | 533 | { |
| 534 | auto img = make_init_image(); | 534 | auto img = make_init_image(); |
| 535 | auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090 | 535 | auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090 |
| 536 | callbacks[0] = kInitBase + 0x9000; // well past SizeOfImage | 536 | callbacks[0] = nt_of(img)->OptionalHeader.ImageBase + 0x9000; // well past SizeOfImage |
| 537 | expect_init_rejects("a TLS callback outside the image is rejected", img); | 537 | expect_init_rejects("a TLS callback outside the image is rejected", img); |
| 538 | } | 538 | } |
| 539 | { | 539 | { |
| @@ -731,7 +731,7 @@ void test_process_detach_without_entry() { | |||
| 731 | reset_notifications(); | 731 | reset_notifications(); |
| 732 | auto first = make_lifecycle_image(); | 732 | auto first = make_lifecycle_image(); |
| 733 | auto second = make_image(); | 733 | auto second = make_image(); |
| 734 | nt_of(second)->OptionalHeader.ImageBase = 0x180000000; | 734 | nt_of(second)->OptionalHeader.ImageBase = leonida_test::free_image_base(0x180000000); |
| 735 | nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL; | 735 | nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL; |
| 736 | emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain)); | 736 | emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain)); |
| 737 | void* first_base = nullptr; | 737 | void* first_base = nullptr; |