diff options
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; |