From f3442f6cbddbdb20b9c3a28e9bb58f30936721f5 Mon Sep 17 00:00:00 2001 From: kotorvr <332716892+kotorvr@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:02:46 +0000 Subject: 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 --- runtime/src/pe_loader.cpp | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'runtime/src/pe_loader.cpp') 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) { namespace { bool lookup_in_loader(void* ctx, uint64_t address, std::string& module, uint32_t& rva) { - for (const Module* m : static_cast(ctx)->modules()) { - const auto base = reinterpret_cast(m->base); - if (m->base && address >= base && address - base < m->size) { - module = m->name; - rva = static_cast(address - base); - return true; - } - } - return false; + return static_cast(ctx)->find_address(address, module, rva); } 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, return nullptr; } +bool Loader::find_address(uint64_t address, std::string& module, uint32_t& rva) const { + std::lock_guard lock(ranges_mutex_); + for (const MappedRange& r : ranges_) { + if (address >= r.base && address - r.base < r.size) { + module = r.name; + rva = static_cast(address - r.base); + return true; + } + } + return false; +} + bool Loader::owns_module_handle(void* module_handle) const { if (!module_handle) return false; std::lock_guard lock(mutex_); @@ -765,6 +769,12 @@ Module& Loader::map_image(const std::filesystem::path& path) { m->base = static_cast(VirtualAlloc(nullptr, oh.SizeOfImage, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); if (!m->base) throw std::runtime_error(m->name + ": VirtualAlloc failed"); } + { + // From here an address inside the image names it. The memory stays until ~Loader, which unregisters the + // lookup first. + std::lock_guard lock(ranges_mutex_); + ranges_.push_back({reinterpret_cast(m->base), m->size, m->name}); + } std::memcpy(m->base, file.data(), oh.SizeOfHeaders); auto* sec = IMAGE_FIRST_SECTION(nt); -- cgit v1.2.3 From b1fdafc2f32984933e10b60693b560f80cb5ab69 Mon Sep 17 00:00:00 2001 From: kotorvr <332716892+kotorvr@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:36:51 +0000 Subject: loader: lookups take the range table's lock shared Concurrent throws on different threads no longer queue on it; map_image takes it exclusively for the one push. Co-Authored-By: Claude Fable 5.2 --- runtime/src/pe_loader.cpp | 4 ++-- runtime/src/pe_loader.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f894fcb43..c630c9abf 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -630,7 +630,7 @@ void* Loader::resolve_module_export(void* module_handle, const std::string& sym, } bool Loader::find_address(uint64_t address, std::string& module, uint32_t& rva) const { - std::lock_guard lock(ranges_mutex_); + std::shared_lock lock(ranges_mutex_); for (const MappedRange& r : ranges_) { if (address >= r.base && address - r.base < r.size) { module = r.name; @@ -772,7 +772,7 @@ Module& Loader::map_image(const std::filesystem::path& path) { { // From here an address inside the image names it. The memory stays until ~Loader, which unregisters the // lookup first. - std::lock_guard lock(ranges_mutex_); + std::unique_lock lock(ranges_mutex_); ranges_.push_back({reinterpret_cast(m->base), m->size, m->name}); } diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h index e78bef888..f961cfaa7 100644 --- a/runtime/src/pe_loader.h +++ b/runtime/src/pe_loader.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -141,7 +142,7 @@ private: uint32_t size = 0; std::string name; }; - mutable std::mutex ranges_mutex_; + mutable std::shared_mutex ranges_mutex_; // shared in find_address: lookups on different threads do not queue std::vector ranges_; }; -- cgit v1.2.3