diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:13:33 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 15:13:33 -0500 |
| commit | 9142e222e6078885827ad566fbbd7409ad7db45f (patch) | |
| tree | 06576881654980af1cc41315184ee72b7e89941a /runtime/src/pe_loader.cpp | |
| parent | ee44e79112fafaf3d43dea3c305093f8a96fcc4f (diff) | |
| parent | 5e3ec685a3a60e3f7597b96824b7ee52ba3396af (diff) | |
Merge pull request #2954 from airp1ane
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index c7a4e870f..3b43ea89a 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -617,10 +617,19 @@ Module& Loader::load(const std::filesystem::path& image) { | |||
| 617 | std::lock_guard lock(mutex_); | 617 | std::lock_guard lock(mutex_); |
| 618 | std::string key = lower(image.filename().string()); | 618 | std::string key = lower(image.filename().string()); |
| 619 | if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; | 619 | if (auto it = by_name_.find(key); it != by_name_.end()) return *it->second; |
| 620 | Module& m = map_image(image); | 620 | Module* mapped = nullptr; |
| 621 | bind_imports(m); // loads every bundled module m imports first, so they land in init_order_ before m | 621 | try { |
| 622 | protect_sections(m); | 622 | mapped = &map_image(image); |
| 623 | register_unwind(m); | 623 | bind_imports(*mapped); // loads every bundled module m imports first, so they land in init_order_ before m |
| 624 | protect_sections(*mapped); | ||
| 625 | register_unwind(*mapped); | ||
| 626 | } catch (...) { | ||
| 627 | // A failed load, nested ones (a dependency of a run-time load) included, must not stay findable by name: | ||
| 628 | // a later lookup would get a half-mapped image with unbound imports. ~Loader still frees it through order_. | ||
| 629 | by_name_.erase(key); | ||
| 630 | throw; | ||
| 631 | } | ||
| 632 | Module& m = *mapped; | ||
| 624 | init_order_.push_back(&m); | 633 | init_order_.push_back(&m); |
| 625 | if (startup_trace::enabled()) | 634 | if (startup_trace::enabled()) |
| 626 | startup_trace::event("map", "\"module\":" + startup_trace::quote(m.name) + | 635 | startup_trace::event("map", "\"module\":" + startup_trace::quote(m.name) + |
| @@ -675,8 +684,8 @@ void* Loader::load_runtime_module(const std::string& dll_lower, uint32_t flags, | |||
| 675 | std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str()); | 684 | std::fprintf(stderr, "[rt] loader: %s mapped from game_dir (run-time load)\n", m.name.c_str()); |
| 676 | return static_cast<void*>(m.base); | 685 | return static_cast<void*>(m.base); |
| 677 | } catch (const std::exception& e) { | 686 | } catch (const std::exception& e) { |
| 678 | // Do not leave a failed run-time load indexed by name so later GetModuleHandle/LoadLibrary calls do not | 687 | // load() drops a module that failed to map or bind (dependencies included); this also drops one whose |
| 679 | // observe a half-initialised module. ~Loader still frees any VirtualAlloc'd mapping in order_. | 688 | // initialise() failed, so later GetModuleHandle/LoadLibrary calls don't see it. ~Loader frees it via order_. |
| 680 | by_name_.erase(dll_lower); | 689 | by_name_.erase(dll_lower); |
| 681 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); | 690 | std::fprintf(stderr, "[rt] loader: run-time load of %s failed: %s\n", dll_lower.c_str(), e.what()); |
| 682 | SetLastError(ERROR_DLL_INIT_FAILED); | 691 | SetLastError(ERROR_DLL_INIT_FAILED); |