upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/loader_test.cpp
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/test/loader_test.cpp
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/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 73bc49206..6da961407 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -8,6 +8,7 @@
8#include <intrin.h> 8#include <intrin.h>
9 9
10#include <algorithm> 10#include <algorithm>
11#include <atomic>
11#include <chrono> 12#include <chrono>
12#include <cstdio> 13#include <cstdio>
13#include <cstdlib> 14#include <cstdlib>
@@ -16,6 +17,7 @@
16#include <fstream> 17#include <fstream>
17#include <stdexcept> 18#include <stdexcept>
18#include <string> 19#include <string>
20#include <thread>
19#include <vector> 21#include <vector>
20 22
21#include "../shims/shims.h" 23#include "../shims/shims.h"
@@ -1043,6 +1045,58 @@ void test_unwind_registration() {
1043 1045
1044// An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is 1046// An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is
1045// refused instead of being stored as base + rva, a pointer outside the mapping. 1047// refused instead of being stored as base + rva, a pointer outside the mapping.
1048// The address-to-module lookup the shim registry uses (on every C++ throw from a mapped image, and for trace and
1049// stop lines) runs on whatever thread asks, while another thread may be inside load(). It used to walk the Loader's
1050// module vector with no lock while load() appended to it. Without the fix this fails for certain only under
1051// AddressSanitizer; here it checks that every answer given during 300 loads is right.
1052void test_lookup_during_loads() {
1053 const char* what = "the address lookup is right while another thread loads modules";
1054 try {
1055 rt::Loader loader(g_dir);
1056 auto first = make_image();
1057 auto* first_nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(first.data() + 0x80);
1058 first_nt->OptionalHeader.ImageBase = 0x200000000ull;
1059 rt::Module& anchor = loader.load(write_image(first));
1060 const uint64_t inside = reinterpret_cast<uint64_t>(anchor.base) + 0x1004;
1061
1062 std::atomic<bool> stop{false};
1063 std::atomic<unsigned> lookups{0}, wrong{0};
1064 std::thread reader([&] {
1065 while (!stop.load()) {
1066 uint64_t base = 0;
1067 if (!rt::shims::mapped_image_base(inside, base) || base != reinterpret_cast<uint64_t>(anchor.base))
1068 ++wrong;
1069 if (rt::shims::module_offset(inside).find("+0x1004") == std::string::npos) ++wrong;
1070 uint64_t none = 0;
1071 if (rt::shims::mapped_image_base(0x10, none)) ++wrong; // in no image
1072 ++lookups;
1073 }
1074 });
1075 unsigned loaded = 0;
1076 for (unsigned i = 1; i <= 300; ++i) {
1077 auto img = make_image();
1078 auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(img.data() + 0x80);
1079 nt->OptionalHeader.ImageBase = 0x200000000ull + i * 0x10000ull;
1080 rt::Module& m = loader.load(write_image(img));
1081 uint64_t base = 0;
1082 if (rt::shims::mapped_image_base(reinterpret_cast<uint64_t>(m.base) + 0x1000, base) &&
1083 base == reinterpret_cast<uint64_t>(m.base))
1084 ++loaded;
1085 }
1086 while (lookups.load() < 1000) std::this_thread::yield();
1087 stop = true;
1088 reader.join();
1089 if (wrong.load())
1090 failed(what, std::to_string(wrong.load()) + " wrong answers in " + std::to_string(lookups.load()) + " lookups");
1091 else if (loaded != 300)
1092 failed(what, "only " + std::to_string(loaded) + " of 300 new modules were found by address");
1093 else
1094 pass(what);
1095 } catch (const std::exception& e) {
1096 failed(what, e.what());
1097 }
1098}
1099
1046void test_export_bounds() { 1100void test_export_bounds() {
1047 auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); 1101 auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000);
1048 nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY); 1102 nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY);
@@ -1147,6 +1201,7 @@ int main() {
1147 test_export_bounds(); 1201 test_export_bounds();
1148 test_tls_directory_count(); 1202 test_tls_directory_count();
1149 test_unwind_registration(); 1203 test_unwind_registration();
1204 test_lookup_during_loads();
1150 } catch (const std::exception& e) { 1205 } catch (const std::exception& e) {
1151 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. 1206 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process.
1152 failed("writing the test images", e.what()); 1207 failed("writing the test images", e.what());