diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:14:56 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:14:56 -0500 |
| commit | 706111e1702022181d367ff2cdd89f801ad857e1 (patch) | |
| tree | ac6b1a1c617e163c062ef5c235bca8340b30f484 /runtime/test/loader_test.cpp | |
| parent | 39c984fe4a4f36909d65bbbefd2da3ef89e82524 (diff) | |
| parent | cc4799abdd73f4983e4e89f6586bf081083bb3e9 (diff) | |
Merge pull request #3119 from Ash Hopkins
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 00ef5affd..b9c48f0b3 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; |
| @@ -505,8 +506,6 @@ void emit_store_stub(std::vector<uint8_t>& img, size_t off, uint64_t addr, uint3 | |||
| 505 | img[n++] = 0xC3; | 506 | img[n++] = 0xC3; |
| 506 | } | 507 | } |
| 507 | 508 | ||
| 508 | constexpr uint64_t kInitBase = 0x140000000; | ||
| 509 | |||
| 510 | // make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a | 509 | // make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a |
| 511 | // sentinel in this process, so the test can tell a callback ran rather than only that it was parsed. | 510 | // sentinel in this process, so the test can tell a callback ran rather than only that it was parsed. |
| 512 | std::vector<uint8_t> make_init_image() { | 511 | std::vector<uint8_t> make_init_image() { |
| @@ -514,7 +513,8 @@ std::vector<uint8_t> make_init_image() { | |||
| 514 | auto* nt = nt_of(img); | 513 | auto* nt = nt_of(img); |
| 515 | nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; | 514 | nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; |
| 516 | nt->OptionalHeader.AddressOfEntryPoint = 0x1040; | 515 | nt->OptionalHeader.AddressOfEntryPoint = 0x1040; |
| 517 | auto va = [](uint32_t rva) { return kInitBase + rva; }; | 516 | const uint64_t base = nt->OptionalHeader.ImageBase; // the TLS callback addresses below are absolute |
| 517 | auto va = [base](uint32_t rva) { return base + rva; }; | ||
| 518 | constexpr size_t text = 0x200; // file offset of RVA 0x1000 | 518 | constexpr size_t text = 0x200; // file offset of RVA 0x1000 |
| 519 | emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0); | 519 | emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0); |
| 520 | emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1); | 520 | emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1); |
| @@ -591,7 +591,7 @@ void test_initialise_rejects() { | |||
| 591 | { | 591 | { |
| 592 | auto img = make_init_image(); | 592 | auto img = make_init_image(); |
| 593 | auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090 | 593 | auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090 |
| 594 | callbacks[0] = kInitBase + 0x9000; // well past SizeOfImage | 594 | callbacks[0] = nt_of(img)->OptionalHeader.ImageBase + 0x9000; // well past SizeOfImage |
| 595 | expect_init_rejects("a TLS callback outside the image is rejected", img); | 595 | expect_init_rejects("a TLS callback outside the image is rejected", img); |
| 596 | } | 596 | } |
| 597 | { | 597 | { |
| @@ -789,7 +789,7 @@ void test_process_detach_without_entry() { | |||
| 789 | reset_notifications(); | 789 | reset_notifications(); |
| 790 | auto first = make_lifecycle_image(); | 790 | auto first = make_lifecycle_image(); |
| 791 | auto second = make_image(); | 791 | auto second = make_image(); |
| 792 | nt_of(second)->OptionalHeader.ImageBase = 0x180000000; | 792 | nt_of(second)->OptionalHeader.ImageBase = leonida_test::free_image_base(0x180000000); |
| 793 | nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL; | 793 | nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL; |
| 794 | emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain)); | 794 | emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain)); |
| 795 | void* first_base = nullptr; | 795 | void* first_base = nullptr; |