diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 14:40:15 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 14:40:15 -0500 |
| commit | 8bf99edc7a437f13e01fa4bc8b22b49b473e569c (patch) | |
| tree | 4da9c23f1b292b04922fd7a28023720a40fd7074 /runtime/test/loader_test.cpp | |
| parent | d279b288684060cdfd486a841fbd185ac37d865b (diff) | |
| parent | 126062ae4a8831a564b9e3042684657c58781bef (diff) | |
Merge pull request #95 from Ayinaki
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 1cd92cad0..b6f6e4912 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -22,6 +22,10 @@ int g_failed = 0; | |||
| 22 | unsigned g_files = 0; | 22 | unsigned g_files = 0; |
| 23 | std::filesystem::path g_dir; | 23 | std::filesystem::path g_dir; |
| 24 | 24 | ||
| 25 | // Sentinels that the synthesised TLS callback and DllMain stubs write, so a test can see execution happen. | ||
| 26 | uint32_t g_tls_ran = 0; | ||
| 27 | uint32_t g_main_ran = 0; | ||
| 28 | |||
| 25 | void pass(const char* what) { | 29 | void pass(const char* what) { |
| 26 | ++g_checks; | 30 | ++g_checks; |
| 27 | std::printf("ok %s\n", what); | 31 | std::printf("ok %s\n", what); |
| @@ -361,6 +365,108 @@ void test_delay_imports() { | |||
| 361 | } | 365 | } |
| 362 | } | 366 | } |
| 363 | 367 | ||
| 368 | // movabs rax, addr ; mov dword [rax], value ; mov eax, eax_value ; ret | ||
| 369 | void emit_store_stub(std::vector<uint8_t>& img, size_t off, uint64_t addr, uint32_t value, uint32_t eax_value) { | ||
| 370 | size_t n = off; | ||
| 371 | img[n++] = 0x48; | ||
| 372 | img[n++] = 0xB8; | ||
| 373 | for (int i = 0; i < 8; ++i) img[n++] = static_cast<uint8_t>(addr >> (8 * i)); | ||
| 374 | img[n++] = 0xC7; | ||
| 375 | img[n++] = 0x00; | ||
| 376 | for (int i = 0; i < 4; ++i) img[n++] = static_cast<uint8_t>(value >> (8 * i)); | ||
| 377 | img[n++] = 0xB8; | ||
| 378 | for (int i = 0; i < 4; ++i) img[n++] = static_cast<uint8_t>(eax_value >> (8 * i)); | ||
| 379 | img[n++] = 0xC3; | ||
| 380 | } | ||
| 381 | |||
| 382 | constexpr uint64_t kInitBase = 0x140000000; | ||
| 383 | |||
| 384 | // make_image() plus a TLS directory and an entry point, both aimed at real machine code: a stub that stores a | ||
| 385 | // sentinel in this process, so the test can tell a callback ran rather than only that it was parsed. | ||
| 386 | std::vector<uint8_t> make_init_image() { | ||
| 387 | auto img = make_image(); | ||
| 388 | auto* nt = nt_of(img); | ||
| 389 | nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; | ||
| 390 | nt->OptionalHeader.AddressOfEntryPoint = 0x1040; | ||
| 391 | auto va = [](uint32_t rva) { return kInitBase + rva; }; | ||
| 392 | constexpr size_t text = 0x200; // file offset of RVA 0x1000 | ||
| 393 | emit_store_stub(img, text + 0x00, reinterpret_cast<uint64_t>(&g_tls_ran), 1, 0); | ||
| 394 | emit_store_stub(img, text + 0x40, reinterpret_cast<uint64_t>(&g_main_ran), 2, 1); | ||
| 395 | std::memcpy(img.data() + text + 0x80, "TLS!", 4); | ||
| 396 | auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + text + 0x90); | ||
| 397 | callbacks[0] = va(0x1000); | ||
| 398 | callbacks[1] = 0; | ||
| 399 | auto* tls = reinterpret_cast<IMAGE_TLS_DIRECTORY64*>(img.data() + text + 0xB0); | ||
| 400 | tls->StartAddressOfRawData = va(0x1080); | ||
| 401 | tls->EndAddressOfRawData = va(0x1084); | ||
| 402 | tls->AddressOfIndex = va(0x10A0); | ||
| 403 | tls->AddressOfCallBacks = va(0x1090); | ||
| 404 | tls->SizeOfZeroFill = 8; | ||
| 405 | auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]; | ||
| 406 | dir.VirtualAddress = 0x10B0; | ||
| 407 | dir.Size = sizeof(IMAGE_TLS_DIRECTORY64); | ||
| 408 | return img; | ||
| 409 | } | ||
| 410 | |||
| 411 | void expect_init_rejects(const char* what, const std::vector<uint8_t>& bytes) { | ||
| 412 | auto p = write_image(bytes); | ||
| 413 | try { | ||
| 414 | rt::Loader loader(g_dir); | ||
| 415 | loader.load(p); | ||
| 416 | loader.initialise(); | ||
| 417 | failed(what, "the module initialised"); | ||
| 418 | } catch (const std::exception&) { | ||
| 419 | pass(what); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | void test_initialise() { | ||
| 424 | g_tls_ran = 0; | ||
| 425 | g_main_ran = 0; | ||
| 426 | auto p = write_image(make_init_image()); | ||
| 427 | try { | ||
| 428 | rt::Loader loader(g_dir); | ||
| 429 | rt::Module& m = loader.load(p); | ||
| 430 | loader.initialise(); | ||
| 431 | if (g_tls_ran != 1) | ||
| 432 | failed("initialise runs the TLS callback", "the callback never wrote its sentinel"); | ||
| 433 | else | ||
| 434 | pass("initialise runs the TLS callback"); | ||
| 435 | if (g_main_ran != 2) | ||
| 436 | failed("initialise calls DllMain for a DLL", "the entry point never wrote its sentinel"); | ||
| 437 | else | ||
| 438 | pass("initialise calls DllMain for a DLL"); | ||
| 439 | uint32_t slot = *reinterpret_cast<const uint32_t*>(m.base + 0x10A0); | ||
| 440 | void* block = slot == TLS_OUT_OF_INDEXES ? nullptr : TlsGetValue(slot); | ||
| 441 | if (!block || std::memcmp(block, "TLS!", 4) != 0) | ||
| 442 | failed("the TLS template is copied into the allocated slot", "the slot doesn't hold the template"); | ||
| 443 | else | ||
| 444 | pass("the TLS template is copied into the allocated slot"); | ||
| 445 | } catch (const std::exception& e) { | ||
| 446 | failed("initialise runs a TLS callback and DllMain", e.what()); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | void test_initialise_rejects() { | ||
| 451 | { | ||
| 452 | auto img = make_init_image(); | ||
| 453 | nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = 0x1FF0; | ||
| 454 | expect_init_rejects("a TLS directory past the image is rejected", img); | ||
| 455 | } | ||
| 456 | { | ||
| 457 | auto img = make_init_image(); | ||
| 458 | auto* callbacks = reinterpret_cast<uint64_t*>(img.data() + 0x290); // file offset of RVA 0x1090 | ||
| 459 | callbacks[0] = kInitBase + 0x9000; // well past SizeOfImage | ||
| 460 | expect_init_rejects("a TLS callback outside the image is rejected", img); | ||
| 461 | } | ||
| 462 | { | ||
| 463 | g_main_ran = 0; | ||
| 464 | auto img = make_init_image(); | ||
| 465 | emit_store_stub(img, 0x240, reinterpret_cast<uint64_t>(&g_main_ran), 3, 0); // DllMain returns FALSE | ||
| 466 | expect_init_rejects("a DllMain that returns FALSE is rejected", img); | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 364 | } // namespace | 470 | } // namespace |
| 365 | 471 | ||
| 366 | int main() { | 472 | int main() { |
| @@ -381,6 +487,8 @@ int main() { | |||
| 381 | test_exports(); | 487 | test_exports(); |
| 382 | test_imports(); | 488 | test_imports(); |
| 383 | test_delay_imports(); | 489 | test_delay_imports(); |
| 490 | test_initialise(); | ||
| 491 | test_initialise_rejects(); | ||
| 384 | 492 | ||
| 385 | std::filesystem::remove_all(g_dir, ec); | 493 | std::filesystem::remove_all(g_dir, ec); |
| 386 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); | 494 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); |