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:
authorMike7581 <168859477+Mike7581@users.noreply.github.com>2026-11-25 01:38:28 -0300
committerMike7581 <168859477+Mike7581@users.noreply.github.com>2026-11-25 01:38:28 -0300
commit582e14d922ad9b24a63ba55f7b8e57bd4aef3c08 (patch)
tree160dfe95899342140734b6e5b4126132fcb66f7e /runtime/test/loader_test.cpp
parentc7e82e26eadeac237da66949b64e7a52b718361e (diff)
loader: a run-time LoadLibrary takes the LEONIDA_COMPAT_DIR module too
LEONIDA_COMPAT_DIR only applied to static imports: runtime/README.md said "a run-time LoadLibrary of the same name does not yet". A mapped image's LoadLibrary of a module the folder holds got the shim's pseudo-handle instead of the drop-in DLL, and GetProcAddress on the compat DLL's real handle looked the name up in the shim of the same module name first, so one console module could be served half by the drop-in and half by the shim, which the static step exists to prevent. Now shims::BundledModuleOps gains compat_module / owns_compat_handle, which rt::Loader answers from the same compat_handle() its static step (resolve_compat) uses, so both get one HMODULE per module. In shims/library_loader.cpp: - LoadLibrary* checks the compat folder before the shim registry and the bundled package DLLs; - GetModuleHandle* answers a compat module's handle once it is loaded and NULL before (never the shim's pseudo-handle); - GetProcAddress on a compat handle gives only that DLL's exports; - FreeLibrary on it returns TRUE and keeps it loaded (it is the Loader's reference, static imports may be bound through it; compat modules stay for the process lifetime). A compat file Windows refuses fails that LoadLibrary with Windows' error (logged) instead of throwing into the title. loader_test links library_loader.cpp and gains test_compat_runtime_loads: eight cases with a synthetic drop-in DLL and the test shim module, no game data.
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index d985ba423..4bc3c8e01 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -1444,6 +1444,92 @@ void test_compat_modules() {
1444 while (HMODULE h = GetModuleHandleW(p.c_str())) FreeLibrary(h); 1444 while (HMODULE h = GetModuleHandleW(p.c_str())) FreeLibrary(h);
1445} 1445}
1446 1446
1447// LEONIDA_COMPAT_DIR at run time: a mapped image's LoadLibrary / GetModuleHandle / GetProcAddress / FreeLibrary go
1448// through shims/library_loader.cpp, which must give the compat DLL ahead of the shim, as static imports get it.
1449void test_compat_runtime_loads() {
1450 using LoadW = HMODULE(WINAPI*)(LPCWSTR);
1451 using ModuleW = HMODULE(WINAPI*)(LPCWSTR);
1452 using ProcA = FARPROC(WINAPI*)(HMODULE, LPCSTR);
1453 using FreeLib = BOOL(WINAPI*)(HMODULE);
1454 const auto load_w = reinterpret_cast<LoadW>(rt::shims::find("kernel32.dll", "LoadLibraryW"));
1455 const auto module_w = reinterpret_cast<ModuleW>(rt::shims::find("kernel32.dll", "GetModuleHandleW"));
1456 const auto proc = reinterpret_cast<ProcA>(rt::shims::find("kernel32.dll", "GetProcAddress"));
1457 const auto free_lib = reinterpret_cast<FreeLib>(rt::shims::find("kernel32.dll", "FreeLibrary"));
1458 if (!load_w || !module_w || !proc || !free_lib) {
1459 failed("LEONIDA_COMPAT_DIR at run time", "the library_loader shims are not registered");
1460 return;
1461 }
1462 const std::filesystem::path compat = g_dir / "compat_rt";
1463 std::filesystem::create_directories(compat);
1464 const auto dll_path = compat / "testshim.dll";
1465 write_image_as(make_os_dll(0x1C0000000), "compat_rt/testshim.dll"); // has Answer, not the shim's F
1466 write_image_as(std::vector<uint8_t>(64, 0x5A), "compat_rt/testbad.dll");
1467 struct Reset {
1468 ~Reset() { SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr); }
1469 } reset;
1470 try {
1471 {
1472 const char* what = "without LEONIDA_COMPAT_DIR a run-time load of a shim's module gives the shim";
1473 SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr);
1474 rt::Loader loader(g_dir);
1475 HMODULE h = load_w(L"testshim.dll");
1476 if (!h) failed(what, "LoadLibraryW returned NULL");
1477 else if (proc(h, "F") != reinterpret_cast<FARPROC>(&test_shim_function)) failed(what, "F is not the shim's");
1478 else pass(what);
1479 }
1480 SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.c_str());
1481 rt::Loader loader(g_dir);
1482 {
1483 const char* what = "with LEONIDA_COMPAT_DIR GetModuleHandle of a compat module not loaded yet is NULL";
1484 if (module_w(L"testshim.dll")) failed(what, "it returned a handle (the shim's?)");
1485 else pass(what);
1486 }
1487 HMODULE h = load_w(L"TestShim.dll");
1488 {
1489 const char* what = "with LEONIDA_COMPAT_DIR a run-time LoadLibrary gives the drop-in DLL";
1490 if (!h) failed(what, "LoadLibraryW returned NULL");
1491 else if (h != GetModuleHandleW(dll_path.c_str())) failed(what, "the handle is not the compat DLL's");
1492 else pass(what);
1493 }
1494 if (h) {
1495 const char* what = "a run-time compat module answers GetModuleHandle, with or without the extension";
1496 if (module_w(L"testshim.dll") != h || module_w(L"TESTSHIM") != h) failed(what, "another handle came back");
1497 else pass(what);
1498 }
1499 if (h) {
1500 const char* what = "GetProcAddress on a run-time compat module gives its own exports, never the shim's";
1501 const FARPROC answer = proc(h, "Answer");
1502 if (!answer || reinterpret_cast<int (*)()>(answer)() != 42) failed(what, "Answer is missing or wrong");
1503 else if (proc(h, "F")) failed(what, "F, which the DLL lacks, resolved (to the shim?)");
1504 else pass(what);
1505 }
1506 if (h) {
1507 const char* what = "a static import of the same module binds to the run-time handle's export";
1508 rt::Module& m = loader.load(write_image(make_import_image("testshim.dll", "Answer")));
1509 void* bound = *reinterpret_cast<void**>(m.base + 0x1180);
1510 if (bound != reinterpret_cast<void*>(GetProcAddress(h, "Answer"))) failed(what, "another export was bound");
1511 else pass(what);
1512 }
1513 if (h) {
1514 const char* what = "FreeLibrary leaves a compat module loaded (static imports may use it)";
1515 if (!free_lib(h) || GetModuleHandleW(dll_path.c_str()) != h) failed(what, "the DLL was unloaded");
1516 else pass(what);
1517 }
1518 {
1519 const char* what = "a compat file Windows cannot load fails that LoadLibrary, without the shim or a throw";
1520 SetLastError(ERROR_SUCCESS);
1521 HMODULE bad = load_w(L"testbad.dll");
1522 const DWORD error = GetLastError();
1523 if (bad) failed(what, "LoadLibraryW returned a handle");
1524 else if (error == ERROR_SUCCESS) failed(what, "no Windows error was set");
1525 else pass(what);
1526 }
1527 } catch (const std::exception& e) {
1528 failed("LEONIDA_COMPAT_DIR at run time", e.what());
1529 }
1530 while (HMODULE h = GetModuleHandleW(dll_path.c_str())) FreeLibrary(h);
1531}
1532
1447} // namespace 1533} // namespace
1448 1534
1449int main() { 1535int main() {
@@ -1491,6 +1577,7 @@ int main() {
1491 test_unwind_registration(); 1577 test_unwind_registration();
1492 test_lookup_during_loads(); 1578 test_lookup_during_loads();
1493 test_compat_modules(); 1579 test_compat_modules();
1580 test_compat_runtime_loads();
1494 } catch (const std::exception& e) { 1581 } catch (const std::exception& e) {
1495 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. 1582 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process.
1496 failed("writing the test images", e.what()); 1583 failed("writing the test images", e.what());