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:
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 71a5ede73..3ed83747a 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"
@@ -1149,6 +1151,58 @@ void test_unwind_registration() {
1149 1151
1150// An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is 1152// An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is
1151// refused instead of being stored as base + rva, a pointer outside the mapping. 1153// refused instead of being stored as base + rva, a pointer outside the mapping.
1154// The address-to-module lookup the shim registry uses (on every C++ throw from a mapped image, and for trace and
1155// stop lines) runs on whatever thread asks, while another thread may be inside load(). It used to walk the Loader's
1156// module vector with no lock while load() appended to it. Without the fix this fails for certain only under
1157// AddressSanitizer; here it checks that every answer given during 300 loads is right.
1158void test_lookup_during_loads() {
1159 const char* what = "the address lookup is right while another thread loads modules";
1160 try {
1161 rt::Loader loader(g_dir);
1162 auto first = make_image();
1163 auto* first_nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(first.data() + 0x80);
1164 first_nt->OptionalHeader.ImageBase = 0x200000000ull;
1165 rt::Module& anchor = loader.load(write_image(first));
1166 const uint64_t inside = reinterpret_cast<uint64_t>(anchor.base) + 0x1004;
1167
1168 std::atomic<bool> stop{false};
1169 std::atomic<unsigned> lookups{0}, wrong{0};
1170 std::thread reader([&] {
1171 while (!stop.load()) {
1172 uint64_t base = 0;
1173 if (!rt::shims::mapped_image_base(inside, base) || base != reinterpret_cast<uint64_t>(anchor.base))
1174 ++wrong;
1175 if (rt::shims::module_offset(inside).find("+0x1004") == std::string::npos) ++wrong;
1176 uint64_t none = 0;
1177 if (rt::shims::mapped_image_base(0x10, none)) ++wrong; // in no image
1178 ++lookups;
1179 }
1180 });
1181 unsigned loaded = 0;
1182 for (unsigned i = 1; i <= 300; ++i) {
1183 auto img = make_image();
1184 auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(img.data() + 0x80);
1185 nt->OptionalHeader.ImageBase = 0x200000000ull + i * 0x10000ull;
1186 rt::Module& m = loader.load(write_image(img));
1187 uint64_t base = 0;
1188 if (rt::shims::mapped_image_base(reinterpret_cast<uint64_t>(m.base) + 0x1000, base) &&
1189 base == reinterpret_cast<uint64_t>(m.base))
1190 ++loaded;
1191 }
1192 while (lookups.load() < 1000) std::this_thread::yield();
1193 stop = true;
1194 reader.join();
1195 if (wrong.load())
1196 failed(what, std::to_string(wrong.load()) + " wrong answers in " + std::to_string(lookups.load()) + " lookups");
1197 else if (loaded != 300)
1198 failed(what, "only " + std::to_string(loaded) + " of 300 new modules were found by address");
1199 else
1200 pass(what);
1201 } catch (const std::exception& e) {
1202 failed(what, e.what());
1203 }
1204}
1205
1152void test_export_bounds() { 1206void test_export_bounds() {
1153 auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); 1207 auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000);
1154 nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY); 1208 nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY);
@@ -1256,6 +1310,7 @@ int main() {
1256 test_export_bounds(); 1310 test_export_bounds();
1257 test_tls_directory_count(); 1311 test_tls_directory_count();
1258 test_unwind_registration(); 1312 test_unwind_registration();
1313 test_lookup_during_loads();
1259 } catch (const std::exception& e) { 1314 } catch (const std::exception& e) {
1260 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. 1315 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process.
1261 failed("writing the test images", e.what()); 1316 failed("writing the test images", e.what());