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:
authorairp1ane <279825357+airp1ane@users.noreply.github.com>2026-11-22 13:53:36 -0500
committerairp1ane <279825357+airp1ane@users.noreply.github.com>2026-11-22 13:53:36 -0500
commit5e3ec685a3a60e3f7597b96824b7ee52ba3396af (patch)
tree6227c65185a815e23a1cd94b2dce67fbf91977f6 /runtime/test/loader_test.cpp
parent77444fa0fbbcfe99d56e44a89f21330b669ccc51 (diff)
pe_loader: a failed load, dependencies included, is not left findable by name
map_image records a module in by_name_ before mapping, binding and section protection can throw. The run-time load path erased only the DLL it was asked for, so a bundled dependency that failed while being bound stayed registered with unbound imports, and a later LoadLibrary or GetModuleHandle of it returned that half-loaded image. load() now erases its own key on any failure; ~Loader still frees the mapping through order_. loader_test: a parent imports fail_dep.dll, whose import table names a DLL outside the image. After the failed load neither name is found and a second load of either fails again (on the old code the dependency was still found). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 194174b00..c86ade637 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -783,6 +783,26 @@ void test_process_detach_dependencies() {
783 g_live_modules.clear(); 783 g_live_modules.clear();
784} 784}
785 785
786// A dependency whose import table is corrupt fails while the parent binds it. Neither must stay findable by name:
787// a later load or GetModuleHandle would otherwise get a mapped image whose imports were never bound.
788void test_failed_dependency_not_registered() {
789 const char* what = "a failed load leaves neither the module nor its failed dependency loaded";
790 auto dep = make_import_image("kernel32.dll", "GetCurrentProcessId");
791 reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(dep.data() + 0x200)->Name = 0x5000; // past SizeOfImage
792 const auto dep_path = write_image_as(dep, "fail_dep.dll");
793 const auto parent_path = write_image_as(make_import_image("fail_dep.dll", "x"), "fail_parent.dll");
794 rt::Loader loader(g_dir);
795 auto throws = [&](const std::filesystem::path& p) {
796 try { loader.load(p); return false; } catch (const std::exception&) { return true; }
797 };
798 if (!throws(parent_path)) return failed(what, "the parent loaded over a corrupt dependency");
799 if (loader.find_loaded_module("fail_dep.dll") || loader.find_loaded_module("fail_parent.dll"))
800 return failed(what, "a failed module is still found by name");
801 if (!throws(dep_path)) return failed(what, "a second load of the dependency returned the half-loaded module");
802 if (!throws(parent_path)) return failed(what, "a second load of the parent returned the half-loaded module");
803 pass(what);
804}
805
786void test_forwarded_exports() { 806void test_forwarded_exports() {
787 { 807 {
788 const char* what = "a forwarded export binds to its target instead of into the string"; 808 const char* what = "a forwarded export binds to its target instead of into the string";
@@ -1106,6 +1126,7 @@ int main() {
1106 test_process_detach_invalid_tls(); 1126 test_process_detach_invalid_tls();
1107 test_process_detach_without_entry(); 1127 test_process_detach_without_entry();
1108 test_process_detach_dependencies(); 1128 test_process_detach_dependencies();
1129 test_failed_dependency_not_registered();
1109 test_delay_descriptor_fields(); 1130 test_delay_descriptor_fields();
1110 test_forwarded_exports(); 1131 test_forwarded_exports();
1111 test_export_bounds(); 1132 test_export_bounds();