upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

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