upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.h
diff options
context:
space:
mode:
authorkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 21:02:46 +0000
committerkotorvr <332716892+kotorvr@users.noreply.github.com>2026-11-22 21:02:46 +0000
commitf3442f6cbddbdb20b9c3a28e9bb58f30936721f5 (patch)
treee011f105adb826bd992fc19ab1428f09bd5c4feb /runtime/src/pe_loader.h
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (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.h')
-rw-r--r--runtime/src/pe_loader.h16
1 files changed, 16 insertions, 0 deletions
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
132std::string lower(std::string s); 148std::string lower(std::string s);