diff options
| author | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 20:39:59 -0700 |
|---|---|---|
| committer | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 20:39:59 -0700 |
| commit | fa5f26db8eafd2cc27f627b68c0a5a5949bcf2ff (patch) | |
| tree | fc57e238be829295fc216dff89a6d93e64c16b50 /runtime/test/loader_test.cpp | |
| parent | 9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff) | |
| parent | 7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff) | |
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 58aae185b..7a4e10cfd 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" |
| @@ -1166,6 +1168,58 @@ void test_unwind_registration() { | |||
| 1166 | 1168 | ||
| 1167 | // An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is | 1169 | // An export whose address-table entry is past SizeOfImage (not a forwarder: outside the export directory) is |
| 1168 | // refused instead of being stored as base + rva, a pointer outside the mapping. | 1170 | // refused instead of being stored as base + rva, a pointer outside the mapping. |
| 1171 | // The address-to-module lookup the shim registry uses (on every C++ throw from a mapped image, and for trace and | ||
| 1172 | // stop lines) runs on whatever thread asks, while another thread may be inside load(). It used to walk the Loader's | ||
| 1173 | // module vector with no lock while load() appended to it. Without the fix this fails for certain only under | ||
| 1174 | // AddressSanitizer; here it checks that every answer given during 300 loads is right. | ||
| 1175 | void test_lookup_during_loads() { | ||
| 1176 | const char* what = "the address lookup is right while another thread loads modules"; | ||
| 1177 | try { | ||
| 1178 | rt::Loader loader(g_dir); | ||
| 1179 | auto first = make_image(); | ||
| 1180 | auto* first_nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(first.data() + 0x80); | ||
| 1181 | first_nt->OptionalHeader.ImageBase = 0x200000000ull; | ||
| 1182 | rt::Module& anchor = loader.load(write_image(first)); | ||
| 1183 | const uint64_t inside = reinterpret_cast<uint64_t>(anchor.base) + 0x1004; | ||
| 1184 | |||
| 1185 | std::atomic<bool> stop{false}; | ||
| 1186 | std::atomic<unsigned> lookups{0}, wrong{0}; | ||
| 1187 | std::thread reader([&] { | ||
| 1188 | while (!stop.load()) { | ||
| 1189 | uint64_t base = 0; | ||
| 1190 | if (!rt::shims::mapped_image_base(inside, base) || base != reinterpret_cast<uint64_t>(anchor.base)) | ||
| 1191 | ++wrong; | ||
| 1192 | if (rt::shims::module_offset(inside).find("+0x1004") == std::string::npos) ++wrong; | ||
| 1193 | uint64_t none = 0; | ||
| 1194 | if (rt::shims::mapped_image_base(0x10, none)) ++wrong; // in no image | ||
| 1195 | ++lookups; | ||
| 1196 | } | ||
| 1197 | }); | ||
| 1198 | unsigned loaded = 0; | ||
| 1199 | for (unsigned i = 1; i <= 300; ++i) { | ||
| 1200 | auto img = make_image(); | ||
| 1201 | auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(img.data() + 0x80); | ||
| 1202 | nt->OptionalHeader.ImageBase = 0x200000000ull + i * 0x10000ull; | ||
| 1203 | rt::Module& m = loader.load(write_image(img)); | ||
| 1204 | uint64_t base = 0; | ||
| 1205 | if (rt::shims::mapped_image_base(reinterpret_cast<uint64_t>(m.base) + 0x1000, base) && | ||
| 1206 | base == reinterpret_cast<uint64_t>(m.base)) | ||
| 1207 | ++loaded; | ||
| 1208 | } | ||
| 1209 | while (lookups.load() < 1000) std::this_thread::yield(); | ||
| 1210 | stop = true; | ||
| 1211 | reader.join(); | ||
| 1212 | if (wrong.load()) | ||
| 1213 | failed(what, std::to_string(wrong.load()) + " wrong answers in " + std::to_string(lookups.load()) + " lookups"); | ||
| 1214 | else if (loaded != 300) | ||
| 1215 | failed(what, "only " + std::to_string(loaded) + " of 300 new modules were found by address"); | ||
| 1216 | else | ||
| 1217 | pass(what); | ||
| 1218 | } catch (const std::exception& e) { | ||
| 1219 | failed(what, e.what()); | ||
| 1220 | } | ||
| 1221 | } | ||
| 1222 | |||
| 1169 | void test_export_bounds() { | 1223 | void test_export_bounds() { |
| 1170 | auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); | 1224 | auto outside = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000); |
| 1171 | nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY); | 1225 | nt_of(outside)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sizeof(IMAGE_EXPORT_DIRECTORY); |
| @@ -1273,6 +1327,7 @@ int main() { | |||
| 1273 | test_export_bounds(); | 1327 | test_export_bounds(); |
| 1274 | test_tls_directory_count(); | 1328 | test_tls_directory_count(); |
| 1275 | test_unwind_registration(); | 1329 | test_unwind_registration(); |
| 1330 | test_lookup_during_loads(); | ||
| 1276 | } catch (const std::exception& e) { | 1331 | } catch (const std::exception& e) { |
| 1277 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. | 1332 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. |
| 1278 | failed("writing the test images", e.what()); | 1333 | failed("writing the test images", e.what()); |