diff options
| author | farhat <farhat@local> | 2026-11-22 14:56:45 +0700 |
|---|---|---|
| committer | farhat <farhat@local> | 2026-11-22 14:56:50 +0700 |
| commit | cc7651422956d1f362bdf32f900e543b357056eb (patch) | |
| tree | 1df84709391aead6930c97f100cae5e34276074c /runtime/test/loader_test.cpp | |
| parent | d3841c0c0ee41ec014699a985846c6dc76026440 (diff) | |
fix(runtime): detach initialised PE modules before unmapping them
Deliver TLS and DllMain process-detach notifications in reverse dependency order while every mapped image and TLS block is still live. This lets mapped DLLs unregister FLS callbacks before their code is freed. Cover failed attach, map-only loads, partial initialisation, teardown ordering and malformed TLS tables with synthetic PE tests.
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 5ee203e28..f946b9ae5 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -543,6 +543,245 @@ void test_initialise_rejects() { | |||
| 543 | } | 543 | } |
| 544 | } | 544 | } |
| 545 | 545 | ||
| 546 | // The images below jump to host observers so notifications remain observable after the mappings are freed. | ||
| 547 | // The FLS destructor itself starts in mapped code: leaving it registered would call unmapped memory at exit. | ||
| 548 | void emit_jump_stub(std::vector<uint8_t>& img, size_t off, uint64_t target) { | ||
| 549 | img[off++] = 0x48; | ||
| 550 | img[off++] = 0xB8; // movabs rax, target; jmp rax (preserves the notification arguments) | ||
| 551 | std::memcpy(img.data() + off, &target, sizeof(target)); | ||
| 552 | off += sizeof(target); | ||
| 553 | img[off++] = 0xFF; | ||
| 554 | img[off] = 0xE0; | ||
| 555 | } | ||
| 556 | |||
| 557 | struct Notification { | ||
| 558 | void* base; | ||
| 559 | DWORD reason; | ||
| 560 | bool tls; | ||
| 561 | bool operator==(const Notification&) const = default; | ||
| 562 | }; | ||
| 563 | std::vector<Notification> g_notifications; | ||
| 564 | struct LiveImage { | ||
| 565 | void* base; | ||
| 566 | uint32_t tls_index; | ||
| 567 | void* tls_block; | ||
| 568 | }; | ||
| 569 | std::vector<LiveImage> g_live_modules; | ||
| 570 | bool g_detach_state_live = true; | ||
| 571 | bool g_fail_attach = false; | ||
| 572 | bool g_use_fls = false; | ||
| 573 | DWORD g_fls_index = FLS_OUT_OF_INDEXES; | ||
| 574 | int g_fls_callbacks = 0; | ||
| 575 | |||
| 576 | void observe_notification(void* base, DWORD reason, void* reserved, bool tls) { | ||
| 577 | g_notifications.push_back({base, reason, tls}); | ||
| 578 | if (reserved) g_detach_state_live = false; | ||
| 579 | if (reason != DLL_PROCESS_DETACH) return; | ||
| 580 | for (const auto& m : g_live_modules) { | ||
| 581 | MEMORY_BASIC_INFORMATION info{}; | ||
| 582 | if (!VirtualQuery(m.base, &info, sizeof(info)) || info.State != MEM_COMMIT) | ||
| 583 | g_detach_state_live = false; | ||
| 584 | if (m.tls_index != rt::kNoTlsIndex && m.tls_block) { | ||
| 585 | auto** slots = reinterpret_cast<void**>(__readgsqword(0x58)); | ||
| 586 | if (slots[m.tls_index] != m.tls_block || | ||
| 587 | !VirtualQuery(m.tls_block, &info, sizeof(info)) || info.State != MEM_COMMIT || | ||
| 588 | std::memcmp(m.tls_block, "TLS!", 4) != 0) | ||
| 589 | g_detach_state_live = false; | ||
| 590 | } | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | void NTAPI observe_tls(void* base, DWORD reason, void* reserved) { | ||
| 595 | observe_notification(base, reason, reserved, true); | ||
| 596 | } | ||
| 597 | |||
| 598 | void NTAPI observe_fls(void* value) { | ||
| 599 | if (value == reinterpret_cast<void*>(1)) ++g_fls_callbacks; | ||
| 600 | } | ||
| 601 | |||
| 602 | BOOL NTAPI observe_dllmain(void* base, DWORD reason, void* reserved) { | ||
| 603 | observe_notification(base, reason, reserved, false); | ||
| 604 | if (g_use_fls) { | ||
| 605 | if (reason == DLL_PROCESS_ATTACH) { | ||
| 606 | g_fls_index = FlsAlloc(reinterpret_cast<PFLS_CALLBACK_FUNCTION>(static_cast<uint8_t*>(base) + 0x1100)); | ||
| 607 | if (g_fls_index == FLS_OUT_OF_INDEXES || !FlsSetValue(g_fls_index, reinterpret_cast<void*>(1))) | ||
| 608 | return FALSE; | ||
| 609 | } else if (reason == DLL_PROCESS_DETACH && g_fls_index != FLS_OUT_OF_INDEXES) { | ||
| 610 | if (!FlsFree(g_fls_index)) g_detach_state_live = false; | ||
| 611 | g_fls_index = FLS_OUT_OF_INDEXES; | ||
| 612 | } | ||
| 613 | } | ||
| 614 | return !(reason == DLL_PROCESS_ATTACH && g_fail_attach); | ||
| 615 | } | ||
| 616 | |||
| 617 | void reset_notifications() { | ||
| 618 | g_notifications.clear(); | ||
| 619 | g_live_modules.clear(); | ||
| 620 | g_detach_state_live = true; | ||
| 621 | g_fail_attach = false; | ||
| 622 | g_use_fls = false; | ||
| 623 | g_fls_callbacks = 0; | ||
| 624 | } | ||
| 625 | |||
| 626 | void expect_notifications(const char* what, const std::vector<Notification>& expected) { | ||
| 627 | if (g_notifications == expected) pass(what); | ||
| 628 | else failed(what, "unexpected TLS/DllMain notification count or order"); | ||
| 629 | } | ||
| 630 | |||
| 631 | std::vector<uint8_t> make_lifecycle_image() { | ||
| 632 | auto img = make_init_image(); | ||
| 633 | emit_jump_stub(img, 0x200, reinterpret_cast<uint64_t>(&observe_tls)); | ||
| 634 | emit_jump_stub(img, 0x240, reinterpret_cast<uint64_t>(&observe_dllmain)); | ||
| 635 | emit_jump_stub(img, 0x300, reinterpret_cast<uint64_t>(&observe_fls)); | ||
| 636 | return img; | ||
| 637 | } | ||
| 638 | |||
| 639 | void test_process_detach() { | ||
| 640 | // A map-only load must remain free of image-code execution, including at destruction. | ||
| 641 | reset_notifications(); | ||
| 642 | { | ||
| 643 | rt::Loader loader(g_dir); | ||
| 644 | loader.load(write_image(make_lifecycle_image())); | ||
| 645 | } | ||
| 646 | expect_notifications("a never-initialised image receives no detach notifications", {}); | ||
| 647 | |||
| 648 | for (bool fail_attach : {false, true}) { | ||
| 649 | reset_notifications(); | ||
| 650 | g_fail_attach = fail_attach; | ||
| 651 | g_use_fls = true; | ||
| 652 | void* base = nullptr; | ||
| 653 | bool rejected = false; | ||
| 654 | { | ||
| 655 | rt::Loader loader(g_dir); | ||
| 656 | auto& m = loader.load(write_image(make_lifecycle_image())); | ||
| 657 | base = m.base; | ||
| 658 | g_live_modules = {{m.base, m.tls_index, m.tls_block}}; | ||
| 659 | try { | ||
| 660 | loader.initialise(); | ||
| 661 | loader.initialise(); // successful attach is delivered once | ||
| 662 | } catch (const std::runtime_error&) { | ||
| 663 | rejected = true; | ||
| 664 | } | ||
| 665 | } | ||
| 666 | if (rejected == fail_attach) pass("attach success/failure is preserved"); | ||
| 667 | else failed("attach success/failure is preserved", "unexpected initialise result"); | ||
| 668 | expect_notifications(fail_attach ? "a FALSE attach is detached once" : "TLS and DllMain detach once after attach", | ||
| 669 | {{base, DLL_PROCESS_ATTACH, true}, {base, DLL_PROCESS_ATTACH, false}, | ||
| 670 | {base, DLL_PROCESS_DETACH, true}, {base, DLL_PROCESS_DETACH, false}}); | ||
| 671 | if (g_detach_state_live && g_fls_callbacks == 1 && g_fls_index == FLS_OUT_OF_INDEXES) | ||
| 672 | pass("detach frees FLS while the callback image and static TLS are live"); | ||
| 673 | else | ||
| 674 | failed("detach frees FLS while the callback image and static TLS are live", "FLS cleanup missing or state freed early"); | ||
| 675 | // Keep a failing negative-control run from crashing during process exit after reporting the regression. | ||
| 676 | if (g_fls_index != FLS_OUT_OF_INDEXES) { | ||
| 677 | FlsSetValue(g_fls_index, nullptr); | ||
| 678 | FlsFree(g_fls_index); | ||
| 679 | g_fls_index = FLS_OUT_OF_INDEXES; | ||
| 680 | } | ||
| 681 | } | ||
| 682 | g_live_modules.clear(); | ||
| 683 | g_use_fls = false; | ||
| 684 | } | ||
| 685 | |||
| 686 | void test_process_detach_invalid_tls() { | ||
| 687 | reset_notifications(); | ||
| 688 | g_use_fls = true; | ||
| 689 | void* base = nullptr; | ||
| 690 | { | ||
| 691 | rt::Loader loader(g_dir); | ||
| 692 | auto& m = loader.load(write_image(make_lifecycle_image())); | ||
| 693 | base = m.base; | ||
| 694 | loader.initialise(); | ||
| 695 | // The callback table is writable in a normal PE, but this synthetic image keeps it in executable .text. | ||
| 696 | DWORD old_protection = 0; | ||
| 697 | if (!VirtualProtect(m.base + 0x1090, sizeof(uint64_t), PAGE_EXECUTE_READWRITE, &old_protection)) | ||
| 698 | throw std::runtime_error("cannot make the synthetic TLS callback table writable"); | ||
| 699 | *reinterpret_cast<uint64_t*>(m.base + 0x1090) = reinterpret_cast<uint64_t>(m.base) + m.size; | ||
| 700 | DWORD ignored = 0; | ||
| 701 | if (!VirtualProtect(m.base + 0x1090, sizeof(uint64_t), old_protection, &ignored)) | ||
| 702 | throw std::runtime_error("cannot restore the synthetic TLS callback table protection"); | ||
| 703 | } | ||
| 704 | expect_notifications("invalid TLS detach still calls DllMain for cleanup", | ||
| 705 | {{base, DLL_PROCESS_ATTACH, true}, {base, DLL_PROCESS_ATTACH, false}, | ||
| 706 | {base, DLL_PROCESS_DETACH, false}}); | ||
| 707 | if (g_fls_callbacks == 1 && g_fls_index == FLS_OUT_OF_INDEXES) | ||
| 708 | pass("invalid TLS detach still frees the mapped FLS callback"); | ||
| 709 | else | ||
| 710 | failed("invalid TLS detach still frees the mapped FLS callback", "FLS cleanup missing"); | ||
| 711 | reset_notifications(); | ||
| 712 | } | ||
| 713 | |||
| 714 | void test_process_detach_without_entry() { | ||
| 715 | for (bool is_dll : {false, true}) { | ||
| 716 | reset_notifications(); | ||
| 717 | auto img = make_lifecycle_image(); | ||
| 718 | if (is_dll) nt_of(img)->OptionalHeader.AddressOfEntryPoint = 0; | ||
| 719 | else nt_of(img)->FileHeader.Characteristics &= ~IMAGE_FILE_DLL; | ||
| 720 | void* base = nullptr; | ||
| 721 | { | ||
| 722 | rt::Loader loader(g_dir); | ||
| 723 | auto& m = loader.load(write_image(img)); | ||
| 724 | base = m.base; | ||
| 725 | loader.initialise(); | ||
| 726 | } | ||
| 727 | expect_notifications(is_dll ? "a DLL without an entry point still detaches TLS" : "an EXE detaches TLS without calling its entry point", | ||
| 728 | {{base, DLL_PROCESS_ATTACH, true}, {base, DLL_PROCESS_DETACH, true}}); | ||
| 729 | } | ||
| 730 | reset_notifications(); | ||
| 731 | auto first = make_lifecycle_image(); | ||
| 732 | auto second = make_image(); | ||
| 733 | nt_of(second)->OptionalHeader.ImageBase = 0x180000000; | ||
| 734 | nt_of(second)->FileHeader.Characteristics |= IMAGE_FILE_DLL; | ||
| 735 | emit_jump_stub(second, 0x200, reinterpret_cast<uint64_t>(&observe_dllmain)); | ||
| 736 | void* first_base = nullptr; | ||
| 737 | { | ||
| 738 | rt::Loader loader(g_dir); | ||
| 739 | first_base = loader.load(write_image(first)).base; | ||
| 740 | loader.load(write_image(second)); | ||
| 741 | g_fail_attach = true; | ||
| 742 | try { | ||
| 743 | loader.initialise(); | ||
| 744 | failed("partial initialisation rejects the first DLL", "attach succeeded"); | ||
| 745 | } catch (const std::runtime_error&) { | ||
| 746 | pass("partial initialisation rejects the first DLL"); | ||
| 747 | } | ||
| 748 | } | ||
| 749 | expect_notifications("partial initialisation does not detach an untouched later DLL", | ||
| 750 | {{first_base, DLL_PROCESS_ATTACH, true}, {first_base, DLL_PROCESS_ATTACH, false}, | ||
| 751 | {first_base, DLL_PROCESS_DETACH, true}, {first_base, DLL_PROCESS_DETACH, false}}); | ||
| 752 | reset_notifications(); | ||
| 753 | } | ||
| 754 | |||
| 755 | void test_process_detach_dependencies() { | ||
| 756 | reset_notifications(); | ||
| 757 | auto dep = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); | ||
| 758 | auto parent = make_import_image("detach_dep.dll", "fwd"); | ||
| 759 | for (auto* img : {&dep, &parent}) { | ||
| 760 | nt_of(*img)->FileHeader.Characteristics |= IMAGE_FILE_DLL; | ||
| 761 | nt_of(*img)->OptionalHeader.AddressOfEntryPoint = 0x11C0; | ||
| 762 | emit_jump_stub(*img, 0x3C0, reinterpret_cast<uint64_t>(&observe_dllmain)); | ||
| 763 | } | ||
| 764 | write_image_as(dep, "detach_dep.dll"); | ||
| 765 | void* parent_base = nullptr; | ||
| 766 | void* dep_base = nullptr; | ||
| 767 | { | ||
| 768 | rt::Loader loader(g_dir); | ||
| 769 | auto& m = loader.load(write_image_as(parent, "detach_parent.dll")); | ||
| 770 | parent_base = m.base; | ||
| 771 | for (const auto* mod : loader.modules()) | ||
| 772 | g_live_modules.push_back({mod->base, mod->tls_index, mod->tls_block}); | ||
| 773 | dep_base = loader.initialisation_order().front()->base; | ||
| 774 | loader.initialise(); | ||
| 775 | loader.initialise(); | ||
| 776 | } | ||
| 777 | expect_notifications("dependencies attach first and detach last", | ||
| 778 | {{dep_base, DLL_PROCESS_ATTACH, false}, {parent_base, DLL_PROCESS_ATTACH, false}, | ||
| 779 | {parent_base, DLL_PROCESS_DETACH, false}, {dep_base, DLL_PROCESS_DETACH, false}}); | ||
| 780 | if (g_detach_state_live) pass("all dependency mappings remain live until notifications finish"); | ||
| 781 | else failed("all dependency mappings remain live until notifications finish", "a dependency was unmapped early"); | ||
| 782 | g_live_modules.clear(); | ||
| 783 | } | ||
| 784 | |||
| 546 | void test_forwarded_exports() { | 785 | void test_forwarded_exports() { |
| 547 | { | 786 | { |
| 548 | const char* what = "a forwarded export binds to its target instead of into the string"; | 787 | const char* what = "a forwarded export binds to its target instead of into the string"; |
| @@ -820,6 +1059,10 @@ int main() { | |||
| 820 | test_delay_imports(); | 1059 | test_delay_imports(); |
| 821 | test_initialise(); | 1060 | test_initialise(); |
| 822 | test_initialise_rejects(); | 1061 | test_initialise_rejects(); |
| 1062 | test_process_detach(); | ||
| 1063 | test_process_detach_invalid_tls(); | ||
| 1064 | test_process_detach_without_entry(); | ||
| 1065 | test_process_detach_dependencies(); | ||
| 823 | test_delay_descriptor_fields(); | 1066 | test_delay_descriptor_fields(); |
| 824 | test_forwarded_exports(); | 1067 | test_forwarded_exports(); |
| 825 | test_unwind_registration(); | 1068 | test_unwind_registration(); |