diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 17:03:18 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 17:03:18 -0500 |
| commit | 8bbb3ba6cee29442dfe5f2fa7127686e8011bd65 (patch) | |
| tree | bb0a397abcad64d15e5e44eaa68edbd3d26acfdd /runtime/src | |
| parent | 8e31e268716c1223d3596af32c6d1945940cec40 (diff) | |
| parent | b1fdafc2f32984933e10b60693b560f80cb5ab69 (diff) | |
Merge pull request #3667 from kotorvr
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 28 | ||||
| -rw-r--r-- | runtime/src/pe_loader.h | 17 |
2 files changed, 36 insertions, 9 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f4ecc0de2..f3323e4c0 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -529,15 +529,7 @@ void cleanup_tls_clones() { cleanup_tls_clones_internal(); } | |||
| 529 | 529 | ||
| 530 | namespace { | 530 | namespace { |
| 531 | bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { | 531 | bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { |
| 532 | for (const Module* m : static_cast<Loader*>(ctx)->modules()) { | 532 | return static_cast<Loader*>(ctx)->find_address(address, module, rva); |
| 533 | const auto base = reinterpret_cast<uint64_t>(m->base); | ||
| 534 | if (m->base && address >= base && address - base < m->size) { | ||
| 535 | module = m->name; | ||
| 536 | rva = static_cast<uint32_t>(address - base); | ||
| 537 | return true; | ||
| 538 | } | ||
| 539 | } | ||
| 540 | return false; | ||
| 541 | } | 533 | } |
| 542 | 534 | ||
| 543 | void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { | 535 | void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { |
| @@ -722,6 +714,18 @@ void* Loader::resolve_module_export(void* module_handle, const std::string& sym, | |||
| 722 | return nullptr; | 714 | return nullptr; |
| 723 | } | 715 | } |
| 724 | 716 | ||
| 717 | bool Loader::find_address(uint64_t address, std::string& module, uint32_t& rva) const { | ||
| 718 | std::shared_lock lock(ranges_mutex_); | ||
| 719 | for (const MappedRange& r : ranges_) { | ||
| 720 | if (address >= r.base && address - r.base < r.size) { | ||
| 721 | module = r.name; | ||
| 722 | rva = static_cast<uint32_t>(address - r.base); | ||
| 723 | return true; | ||
| 724 | } | ||
| 725 | } | ||
| 726 | return false; | ||
| 727 | } | ||
| 728 | |||
| 725 | bool Loader::owns_module_handle(void* module_handle) const { | 729 | bool Loader::owns_module_handle(void* module_handle) const { |
| 726 | if (!module_handle) return false; | 730 | if (!module_handle) return false; |
| 727 | std::lock_guard lock(mutex_); | 731 | std::lock_guard lock(mutex_); |
| @@ -850,6 +854,12 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 850 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 854 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 851 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); | 855 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); |
| 852 | } | 856 | } |
| 857 | { | ||
| 858 | // From here an address inside the image names it. The memory stays until ~Loader, which unregisters the | ||
| 859 | // lookup first. | ||
| 860 | std::unique_lock lock(ranges_mutex_); | ||
| 861 | ranges_.push_back({reinterpret_cast<uint64_t>(m->base), m->size, m->name}); | ||
| 862 | } | ||
| 853 | 863 | ||
| 854 | std::memcpy(m->base, file.data(), oh.SizeOfHeaders); | 864 | std::memcpy(m->base, file.data(), oh.SizeOfHeaders); |
| 855 | auto* sec = IMAGE_FIRST_SECTION(nt); | 865 | auto* sec = IMAGE_FIRST_SECTION(nt); |
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h index 8fb24349f..c87f157a4 100644 --- a/runtime/src/pe_loader.h +++ b/runtime/src/pe_loader.h | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include <filesystem> | 9 | #include <filesystem> |
| 10 | #include <map> | 10 | #include <map> |
| 11 | #include <mutex> | 11 | #include <mutex> |
| 12 | #include <shared_mutex> | ||
| 12 | #include <string> | 13 | #include <string> |
| 13 | #include <vector> | 14 | #include <vector> |
| 14 | 15 | ||
| @@ -79,6 +80,11 @@ public: | |||
| 79 | 80 | ||
| 80 | const std::vector<Module*>& modules() const { return order_; } | 81 | const std::vector<Module*>& modules() const { return order_; } |
| 81 | 82 | ||
| 83 | // The mapped module `address` lies in, with its RVA there. Safe from any thread at any time, also while another | ||
| 84 | // thread is in load(): it is what the shim registry asks for a caller's module (rt::shims::mapped_image_base on | ||
| 85 | // every C++ throw from a mapped image, and the trace and stop lines), and it reads its own table, not modules(). | ||
| 86 | bool find_address(uint64_t address, std::string& module, uint32_t& rva) const; | ||
| 87 | |||
| 82 | // The order initialise() runs modules in: each module after every module it imports (a cycle is broken at the | 88 | // The order initialise() runs modules in: each module after every module it imports (a cycle is broken at the |
| 83 | // module that closed it). modules() is mapping order, which puts an importer before its imports. | 89 | // module that closed it). modules() is mapping order, which puts an importer before its imports. |
| 84 | const std::vector<Module*>& initialisation_order() const { return init_order_; } | 90 | const std::vector<Module*>& initialisation_order() const { return init_order_; } |
| @@ -129,6 +135,17 @@ private: | |||
| 129 | std::map<std::string, Module*> by_name_; // lower-case file name | 135 | std::map<std::string, Module*> by_name_; // lower-case file name |
| 130 | std::vector<Module*> order_; // mapping order; owns the modules | 136 | std::vector<Module*> order_; // mapping order; owns the modules |
| 131 | std::vector<Module*> init_order_; // dependencies first, see initialisation_order() | 137 | std::vector<Module*> init_order_; // dependencies first, see initialisation_order() |
| 138 | |||
| 139 | // What find_address reads: one entry per mapped image, added once its memory exists. Under ranges_mutex_, a leaf | ||
| 140 | // lock: mutex_ is held while image code runs and the registry holds its own lock around the lookup, so the | ||
| 141 | // lookup must not wait for either. | ||
| 142 | struct MappedRange { | ||
| 143 | uint64_t base = 0; | ||
| 144 | uint32_t size = 0; | ||
| 145 | std::string name; | ||
| 146 | }; | ||
| 147 | mutable std::shared_mutex ranges_mutex_; // shared in find_address: lookups on different threads do not queue | ||
| 148 | std::vector<MappedRange> ranges_; | ||
| 132 | }; | 149 | }; |
| 133 | 150 | ||
| 134 | // Frees the TLS helper clones load() created and deletes their files (Module::tls_index background). load() | 151 | // Frees the TLS helper clones load() created and deletes their files (Module::tls_index background). load() |