diff options
| author | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 21:02:46 +0000 |
|---|---|---|
| committer | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 21:02:46 +0000 |
| commit | f3442f6cbddbdb20b9c3a28e9bb58f30936721f5 (patch) | |
| tree | e011f105adb826bd992fc19ab1428f09bd5c4feb /runtime/src | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
loader: the address lookup reads its own table, not the module vector
lookup_in_loader walked Loader::modules() with no lock while map_image
appends to it under Loader::mutex_. The lookup runs on any thread: behind
the RtlPcToFileHeader redirect on every C++ throw from a mapped image, and
for trace and stop lines. A throw during another thread's run-time load
read a vector that was being reallocated.
find_address reads a table of (base, size, name) under a leaf lock;
map_image adds an entry once the image's memory exists.
loader_test: lookups from one thread while another loads 300 images. With
the old code it crashes under AddressSanitizer at the vector read.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 28 | ||||
| -rw-r--r-- | runtime/src/pe_loader.h | 16 |
2 files changed, 35 insertions, 9 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f2c62ec91..f894fcb43 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -454,15 +454,7 @@ void redirect_pc_to_file_header_at(const void* resolved) { | |||
| 454 | 454 | ||
| 455 | namespace { | 455 | namespace { |
| 456 | bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { | 456 | bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { |
| 457 | for (const Module* m : static_cast<Loader*>(ctx)->modules()) { | 457 | return static_cast<Loader*>(ctx)->find_address(address, module, rva); |
| 458 | const auto base = reinterpret_cast<uint64_t>(m->base); | ||
| 459 | if (m->base && address >= base && address - base < m->size) { | ||
| 460 | module = m->name; | ||
| 461 | rva = static_cast<uint32_t>(address - base); | ||
| 462 | return true; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | return false; | ||
| 466 | } | 458 | } |
| 467 | 459 | ||
| 468 | void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { | 460 | void* bundled_find_loaded(void* ctx, const std::string& dll_lower) { |
| @@ -637,6 +629,18 @@ void* Loader::resolve_module_export(void* module_handle, const std::string& sym, | |||
| 637 | return nullptr; | 629 | return nullptr; |
| 638 | } | 630 | } |
| 639 | 631 | ||
| 632 | bool Loader::find_address(uint64_t address, std::string& module, uint32_t& rva) const { | ||
| 633 | std::lock_guard lock(ranges_mutex_); | ||
| 634 | for (const MappedRange& r : ranges_) { | ||
| 635 | if (address >= r.base && address - r.base < r.size) { | ||
| 636 | module = r.name; | ||
| 637 | rva = static_cast<uint32_t>(address - r.base); | ||
| 638 | return true; | ||
| 639 | } | ||
| 640 | } | ||
| 641 | return false; | ||
| 642 | } | ||
| 643 | |||
| 640 | bool Loader::owns_module_handle(void* module_handle) const { | 644 | bool Loader::owns_module_handle(void* module_handle) const { |
| 641 | if (!module_handle) return false; | 645 | if (!module_handle) return false; |
| 642 | std::lock_guard lock(mutex_); | 646 | std::lock_guard lock(mutex_); |
| @@ -765,6 +769,12 @@ Module& Loader::map_image(const std::filesystem::path& path) { | |||
| 765 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 769 | m->base = static_cast<uint8_t*>(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| 766 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); | 770 | if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); |
| 767 | } | 771 | } |
| 772 | { | ||
| 773 | // From here an address inside the image names it. The memory stays until ~Loader, which unregisters the | ||
| 774 | // lookup first. | ||
| 775 | std::lock_guard lock(ranges_mutex_); | ||
| 776 | ranges_.push_back({reinterpret_cast<uint64_t>(m->base), m->size, m->name}); | ||
| 777 | } | ||
| 768 | 778 | ||
| 769 | std::memcpy(m->base, file.data(), oh.SizeOfHeaders); | 779 | std::memcpy(m->base, file.data(), oh.SizeOfHeaders); |
| 770 | auto* sec = IMAGE_FIRST_SECTION(nt); | 780 | auto* sec = IMAGE_FIRST_SECTION(nt); |
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h index 7cfe7a771..e78bef888 100644 --- a/runtime/src/pe_loader.h +++ b/runtime/src/pe_loader.h | |||
| @@ -78,6 +78,11 @@ public: | |||
| 78 | 78 | ||
| 79 | const std::vector<Module*>& modules() const { return order_; } | 79 | const std::vector<Module*>& modules() const { return order_; } |
| 80 | 80 | ||
| 81 | // The mapped module `address` lies in, with its RVA there. Safe from any thread at any time, also while another | ||
| 82 | // thread is in load(): it is what the shim registry asks for a caller's module (rt::shims::mapped_image_base on | ||
| 83 | // every C++ throw from a mapped image, and the trace and stop lines), and it reads its own table, not modules(). | ||
| 84 | bool find_address(uint64_t address, std::string& module, uint32_t& rva) const; | ||
| 85 | |||
| 81 | // The order initialise() runs modules in: each module after every module it imports (a cycle is broken at the | 86 | // The order initialise() runs modules in: each module after every module it imports (a cycle is broken at the |
| 82 | // module that closed it). modules() is mapping order, which puts an importer before its imports. | 87 | // module that closed it). modules() is mapping order, which puts an importer before its imports. |
| 83 | const std::vector<Module*>& initialisation_order() const { return init_order_; } | 88 | const std::vector<Module*>& initialisation_order() const { return init_order_; } |
| @@ -127,6 +132,17 @@ private: | |||
| 127 | std::map<std::string, Module*> by_name_; // lower-case file name | 132 | std::map<std::string, Module*> by_name_; // lower-case file name |
| 128 | std::vector<Module*> order_; // mapping order; owns the modules | 133 | std::vector<Module*> order_; // mapping order; owns the modules |
| 129 | std::vector<Module*> init_order_; // dependencies first, see initialisation_order() | 134 | std::vector<Module*> init_order_; // dependencies first, see initialisation_order() |
| 135 | |||
| 136 | // What find_address reads: one entry per mapped image, added once its memory exists. Under ranges_mutex_, a leaf | ||
| 137 | // lock: mutex_ is held while image code runs and the registry holds its own lock around the lookup, so the | ||
| 138 | // lookup must not wait for either. | ||
| 139 | struct MappedRange { | ||
| 140 | uint64_t base = 0; | ||
| 141 | uint32_t size = 0; | ||
| 142 | std::string name; | ||
| 143 | }; | ||
| 144 | mutable std::mutex ranges_mutex_; | ||
| 145 | std::vector<MappedRange> ranges_; | ||
| 130 | }; | 146 | }; |
| 131 | 147 | ||
| 132 | std::string lower(std::string s); | 148 | std::string lower(std::string s); |