diff options
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 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); |