upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.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/src/pe_loader.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/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp21
1 files changed, 15 insertions, 6 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index a59f9d0b1..6f19f5b2f 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -540,10 +540,19 @@ Module& Loader::load(const std::filesystem::path& image) {
540 std::lock_guard lock(mutex_); 540 std::lock_guard lock(mutex_);
541 std::string key = lower(image.filename().string()); 541 std::string key = lower(image.filename().string());
542 if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; 542 if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second;
543 Module& m = map_image(image); 543 Module* mapped = nullptr;
544 bind_imports(m); // loads every bundled module m imports first, so they land in init_order_ before m 544 try {
545 protect_sections(m); 545 mapped = &map_image(image);
546 register_unwind(m); 546 bind_imports(*mapped); // loads every bundled module m imports first, so they land in init_order_ before m
547 protect_sections(*mapped);
548 register_unwind(*mapped);
549 } catch (...) {
550 // A failed load, nested ones (a dependency of a run-time load) included, must not stay findable by name:
551 // a later lookup would get a half-mapped image with unbound imports. ~Loader still frees it through order_.
552 by_name_.erase(key);
553 throw;
554 }
555 Module& m = *mapped;
547 init_order_.push_back(&m); 556 init_order_.push_back(&m);
548 if (startup_trace::enabled()) 557 if (startup_trace::enabled())
549 startup_trace::event("map", "\"module\":" + startup_trace::quote(m.name) + 558 startup_trace::event("map", "\"module\":" + startup_trace::quote(m.name) +
@@ -598,8 +607,8 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags,
598 std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str()); 607 std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str());
599 return static_cast<void*>(m.base); 608 return static_cast<void*>(m.base);
600 } catch (const std::exception& e) { 609 } catch (const std::exception& e) {
601 // Do not leave a failed run-time load indexed by name so later GetModuleHandle/LoadLibrary calls do not 610 // load() drops a module that failed to map or bind (dependencies included); this also drops one whose
602 // observe a half-initialised module. ~Loader still frees any VirtualAlloc'd mapping in order_. 611 // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_.
603 by_name_.erase(dll_lower); 612 by_name_.erase(dll_lower);
604 std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); 613 std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what());
605 SetLastError(ERROR_DLL_INIT_FAILED); 614 SetLastError(ERROR_DLL_INIT_FAILED);