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/pe_loader.cpp | |
| 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/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 28 |
1 files changed, 19 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); |