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:
authorAndréLanD <andreviniciusmerlia@gmail.com>2026-11-28 00:48:31 -0300
committerGitHub <noreply@github.com>2026-11-28 00:48:31 -0300
commitf77359c5b42fdba8025bf4bf6cf553a945a8451b (patch)
tree2a15c470752c59b081ea16c94818cd0ccac7bee5 /runtime/test/loader_test.cpp
parent477f56cc1afa204d51e32c2f24de3862657d1dce (diff)
parent2cca47b5c8971c885961c35741ab8b0f0debaa01 (diff)
Merge pull request #1565 from Mike7581/rt-compat-dir-runtime-loads
loader: a run-time LoadLibrary takes the LEONIDA_COMPAT_DIR module too (RT-COMPAT-DIR-RUNTIME-LOADS)
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp252
1 files changed, 249 insertions, 3 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 6930878a9..79e78e32d 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -1396,8 +1396,8 @@ std::string written_images_blocked() {
1396} 1396}
1397 1397
1398// A DLL Windows' own loader accepts (LEONIDA_COMPAT_DIR loads with LoadLibraryExW, not with rt::Loader): no entry 1398// A DLL Windows' own loader accepts (LEONIDA_COMPAT_DIR loads with LoadLibraryExW, not with rt::Loader): no entry
1399// point, no imports, one export "Answer" (ordinal 1) whose code returns 42. 1399// point, no imports, one export "Answer" (ordinal 1) whose code returns `answer` (42 unless a case tells two apart).
1400std::vector<uint8_t> make_os_dll(uint64_t image_base) { 1400std::vector<uint8_t> make_os_dll(uint64_t image_base, uint8_t answer = 42) {
1401 auto img = make_image(); 1401 auto img = make_image();
1402 std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0}); 1402 std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0});
1403 auto* nt = nt_of(img); 1403 auto* nt = nt_of(img);
@@ -1426,7 +1426,7 @@ std::vector<uint8_t> make_os_dll(uint64_t image_base) {
1426 *reinterpret_cast<uint32_t*>(img.data() + 0x320) = 0x1160; 1426 *reinterpret_cast<uint32_t*>(img.data() + 0x320) = 0x1160;
1427 *reinterpret_cast<uint16_t*>(img.data() + 0x340) = 0; 1427 *reinterpret_cast<uint16_t*>(img.data() + 0x340) = 0;
1428 std::memcpy(img.data() + 0x360, "Answer\0", 7); 1428 std::memcpy(img.data() + 0x360, "Answer\0", 7);
1429 const uint8_t code[] = {0xB8, 0x2A, 0x00, 0x00, 0x00, 0xC3}; // mov eax, 42; ret 1429 const uint8_t code[] = {0xB8, answer, 0x00, 0x00, 0x00, 0xC3}; // mov eax, answer; ret
1430 std::memcpy(img.data() + 0x380, code, sizeof(code)); 1430 std::memcpy(img.data() + 0x380, code, sizeof(code));
1431 auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; 1431 auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
1432 dir.VirtualAddress = 0x1000; 1432 dir.VirtualAddress = 0x1000;
@@ -1434,6 +1434,32 @@ std::vector<uint8_t> make_os_dll(uint64_t image_base) {
1434 return img; 1434 return img;
1435} 1435}
1436 1436
1437// make_os_dll with one import, `dll`!`symbol` by name, which Windows resolves when it loads the DLL. The section
1438// grows to 0x400 bytes for the import tables at RVA 0x1200, and is writable: Windows fills the address table in place.
1439std::vector<uint8_t> make_os_importer_dll(uint64_t image_base, const char* dll, const char* symbol) {
1440 auto img = make_os_dll(image_base);
1441 img.resize(0x600, 0);
1442 auto* nt = nt_of(img);
1443 auto* sec = IMAGE_FIRST_SECTION(nt);
1444 sec->Misc.VirtualSize = 0x400;
1445 sec->SizeOfRawData = 0x400;
1446 sec->Characteristics |= IMAGE_SCN_MEM_WRITE;
1447 nt->OptionalHeader.SizeOfCode = 0x400;
1448 auto* d = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(img.data() + 0x400); // then the zero descriptor
1449 d->OriginalFirstThunk = 0x1240;
1450 d->FirstThunk = 0x1260;
1451 d->Name = 0x1280;
1452 *reinterpret_cast<uint64_t*>(img.data() + 0x440) = 0x12A0; // lookup table -> hint/name
1453 *reinterpret_cast<uint64_t*>(img.data() + 0x460) = 0x12A0; // address table, before binding
1454 std::memcpy(img.data() + 0x480, dll, std::strlen(dll) + 1);
1455 *reinterpret_cast<uint16_t*>(img.data() + 0x4A0) = 0;
1456 std::memcpy(img.data() + 0x4A2, symbol, std::strlen(symbol) + 1);
1457 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
1458 dir.VirtualAddress = 0x1200;
1459 dir.Size = 2 * sizeof(IMAGE_IMPORT_DESCRIPTOR);
1460 return img;
1461}
1462
1437// An importer of `dll`!`symbol` bound under LEONIDA_COMPAT_DIR = `compat` (empty = unset). Returns its one record and 1463// An importer of `dll`!`symbol` bound under LEONIDA_COMPAT_DIR = `compat` (empty = unset). Returns its one record and
1438// writes the IAT slot to `bound`. 1464// writes the IAT slot to `bound`.
1439rt::ImportRecord bind_with_compat(const std::filesystem::path& compat, const char* dll, const char* symbol, 1465rt::ImportRecord bind_with_compat(const std::filesystem::path& compat, const char* dll, const char* symbol,
@@ -1531,6 +1557,224 @@ void test_compat_modules() {
1531 while (HMODULE h = GetModuleHandleW(p.c_str())) FreeLibrary(h); 1557 while (HMODULE h = GetModuleHandleW(p.c_str())) FreeLibrary(h);
1532} 1558}
1533 1559
1560// LEONIDA_COMPAT_DIR at run time: a mapped image's LoadLibrary / GetModuleHandle / GetProcAddress / FreeLibrary go
1561// through shims/library_loader.cpp, which must give the compat DLL ahead of the shim, as static imports get it.
1562void test_compat_runtime_loads() {
1563 using LoadW = HMODULE(WINAPI*)(LPCWSTR);
1564 using ModuleW = HMODULE(WINAPI*)(LPCWSTR);
1565 using ProcA = FARPROC(WINAPI*)(HMODULE, LPCSTR);
1566 using FreeLib = BOOL(WINAPI*)(HMODULE);
1567 const auto load_w = reinterpret_cast<LoadW>(rt::shims::find("kernel32.dll", "LoadLibraryW"));
1568 const auto module_w = reinterpret_cast<ModuleW>(rt::shims::find("kernel32.dll", "GetModuleHandleW"));
1569 const auto proc = reinterpret_cast<ProcA>(rt::shims::find("kernel32.dll", "GetProcAddress"));
1570 const auto free_lib = reinterpret_cast<FreeLib>(rt::shims::find("kernel32.dll", "FreeLibrary"));
1571 if (!load_w || !module_w || !proc || !free_lib) {
1572 failed("LEONIDA_COMPAT_DIR at run time", "the library_loader shims are not registered");
1573 return;
1574 }
1575 const std::filesystem::path compat = g_dir / "compat_rt";
1576 std::filesystem::create_directories(compat);
1577 const auto dll_path = compat / "testshim.dll";
1578 write_image_as(make_os_dll(0x1C0000000), "compat_rt/testshim.dll"); // has Answer, not the shim's F
1579 write_image_as(std::vector<uint8_t>(64, 0x5A), "compat_rt/testbad.dll");
1580 struct Reset {
1581 ~Reset() { SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr); }
1582 } reset;
1583 try {
1584 {
1585 const char* what = "without LEONIDA_COMPAT_DIR a run-time load of a shim's module gives the shim";
1586 SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr);
1587 rt::Loader loader(g_dir);
1588 HMODULE h = load_w(L"testshim.dll");
1589 if (!h) failed(what, "LoadLibraryW returned NULL");
1590 else if (proc(h, "F") != reinterpret_cast<FARPROC>(&test_shim_function)) failed(what, "F is not the shim's");
1591 else pass(what);
1592 }
1593 SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.c_str());
1594 rt::Loader loader(g_dir);
1595 {
1596 const char* what = "with LEONIDA_COMPAT_DIR GetModuleHandle of a compat module not loaded yet is NULL";
1597 if (module_w(L"testshim.dll")) failed(what, "it returned a handle (the shim's?)");
1598 else pass(what);
1599 }
1600 HMODULE h = load_w(L"TestShim.dll");
1601 {
1602 const char* what = "with LEONIDA_COMPAT_DIR a run-time LoadLibrary gives the drop-in DLL";
1603 if (!h) failed(what, "LoadLibraryW returned NULL");
1604 else if (h != GetModuleHandleW(dll_path.c_str())) failed(what, "the handle is not the compat DLL's");
1605 else pass(what);
1606 }
1607 if (h) {
1608 const char* what = "a run-time compat module answers GetModuleHandle, with or without the extension";
1609 if (module_w(L"testshim.dll") != h || module_w(L"TESTSHIM") != h) failed(what, "another handle came back");
1610 else pass(what);
1611 }
1612 if (h) {
1613 const char* what = "GetProcAddress on a run-time compat module gives its own exports, never the shim's";
1614 const FARPROC answer = proc(h, "Answer");
1615 if (!answer || reinterpret_cast<int (*)()>(answer)() != 42) failed(what, "Answer is missing or wrong");
1616 else if (proc(h, "F")) failed(what, "F, which the DLL lacks, resolved (to the shim?)");
1617 else pass(what);
1618 }
1619 if (h) {
1620 const char* what = "a static import of the same module binds to the run-time handle's export";
1621 rt::Module& m = loader.load(write_image(make_import_image("testshim.dll", "Answer")));
1622 void* bound = *reinterpret_cast<void**>(m.base + 0x1180);
1623 if (bound != reinterpret_cast<void*>(GetProcAddress(h, "Answer"))) failed(what, "another export was bound");
1624 else pass(what);
1625 }
1626 if (h) {
1627 const char* what = "FreeLibrary leaves a compat module loaded (static imports may use it)";
1628 if (!free_lib(h) || GetModuleHandleW(dll_path.c_str()) != h) failed(what, "the DLL was unloaded");
1629 else pass(what);
1630 }
1631 {
1632 const char* what = "a compat file Windows cannot load fails that LoadLibrary, without the shim or a throw";
1633 SetLastError(ERROR_SUCCESS);
1634 HMODULE bad = load_w(L"testbad.dll");
1635 const DWORD error = GetLastError();
1636 if (bad) failed(what, "LoadLibraryW returned a handle");
1637 else if (error == ERROR_SUCCESS) failed(what, "no Windows error was set");
1638 else pass(what);
1639 }
1640 } catch (const std::exception& e) {
1641 failed("LEONIDA_COMPAT_DIR at run time", e.what());
1642 }
1643 while (HMODULE h = GetModuleHandleW(dll_path.c_str())) FreeLibrary(h);
1644}
1645
1646// Run-time LEONIDA_COMPAT_DIR cases where Windows itself decides: LoadLibraryEx flags, a path naming another file of a
1647// compat module's name, a name outside ASCII, and a compat DLL that Windows loaded as another compat DLL's dependency.
1648void test_compat_runtime_edges() {
1649 using LoadW = HMODULE(WINAPI*)(LPCWSTR);
1650 using LoadExA = HMODULE(WINAPI*)(LPCSTR, HANDLE, DWORD);
1651 using LoadExW = HMODULE(WINAPI*)(LPCWSTR, HANDLE, DWORD);
1652 using ModuleW = HMODULE(WINAPI*)(LPCWSTR);
1653 using ProcA = FARPROC(WINAPI*)(HMODULE, LPCSTR);
1654 const auto load_w = reinterpret_cast<LoadW>(rt::shims::find("kernel32.dll", "LoadLibraryW"));
1655 const auto load_ex_a = reinterpret_cast<LoadExA>(rt::shims::find("kernel32.dll", "LoadLibraryExA"));
1656 const auto load_ex_w = reinterpret_cast<LoadExW>(rt::shims::find("kernel32.dll", "LoadLibraryExW"));
1657 const auto module_w = reinterpret_cast<ModuleW>(rt::shims::find("kernel32.dll", "GetModuleHandleW"));
1658 const auto proc = reinterpret_cast<ProcA>(rt::shims::find("kernel32.dll", "GetProcAddress"));
1659 if (!load_w || !load_ex_a || !load_ex_w || !module_w || !proc) {
1660 failed("LEONIDA_COMPAT_DIR at run time (edges)", "the library_loader shims are not registered");
1661 return;
1662 }
1663 const std::filesystem::path compat = g_dir / "compat_edges";
1664 const std::filesystem::path other = g_dir / "compat_edges_other";
1665 const std::filesystem::path dep = g_dir / "compat_dep";
1666 for (const auto& d : {compat, other, dep}) std::filesystem::create_directories(d);
1667 write_image_as(make_os_dll(0x1D0000000, 42), "compat_edges/testpath.dll");
1668 write_image_as(make_os_dll(0x1E0000000, 43), "compat_edges_other/testpath.dll");
1669 const auto wide_dll = compat / L"tëstcompat.dll"; // write_image_as takes a narrow name
1670 {
1671 const auto img = make_os_dll(0x1F0000000, 44);
1672 std::ofstream out(wide_dll, std::ios::binary);
1673 out.write(reinterpret_cast<const char*>(img.data()), static_cast<long long>(img.size()));
1674 }
1675 write_image_as(make_os_dll(0x200000000), "compat_dep/testshim.dll"); // has Answer, not the shim's F
1676 write_image_as(make_os_importer_dll(0x210000000, "testshim.dll", "Answer"), "compat_dep/depimporter.dll");
1677 const auto path_dll = compat / "testpath.dll";
1678 const auto other_dll = other / "testpath.dll";
1679 const auto dep_shim = dep / "testshim.dll";
1680 const auto dep_importer = dep / "depimporter.dll";
1681 const auto answer = [](HMODULE h) {
1682 const FARPROC f = h ? GetProcAddress(h, "Answer") : nullptr;
1683 return f ? reinterpret_cast<int (*)()>(f)() : -1;
1684 };
1685 struct Reset {
1686 ~Reset() { SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr); }
1687 } reset;
1688 try {
1689 SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.c_str());
1690 {
1691 rt::Loader loader(g_dir);
1692 {
1693 const char* what = "LoadLibraryExA/W of a compat module with DONT_RESOLVE_DLL_REFERENCES is refused";
1694 SetLastError(ERROR_SUCCESS);
1695 const HMODULE a = load_ex_a("testpath.dll", nullptr, DONT_RESOLVE_DLL_REFERENCES);
1696 const DWORD error_a = GetLastError();
1697 SetLastError(ERROR_SUCCESS);
1698 const HMODULE w = load_ex_w(L"testpath.dll", nullptr, DONT_RESOLVE_DLL_REFERENCES);
1699 const DWORD error_w = GetLastError();
1700 if (a || w) failed(what, "a handle came back");
1701 else if (error_a != ERROR_NOT_SUPPORTED || error_w != ERROR_NOT_SUPPORTED)
1702 failed(what, "errors " + std::to_string(error_a) + " and " + std::to_string(error_w));
1703 else if (GetModuleHandleW(path_dll.c_str())) failed(what, "the DLL was loaded anyway");
1704 else pass(what);
1705 }
1706 const HMODULE h = load_w(L"testpath");
1707 {
1708 const char* what = "DONT_RESOLVE_DLL_REFERENCES is refused for a compat module already loaded too";
1709 SetLastError(ERROR_SUCCESS);
1710 const HMODULE again = load_ex_w(L"testpath.dll", nullptr, DONT_RESOLVE_DLL_REFERENCES);
1711 if (!h) failed(what, "the plain LoadLibraryW failed");
1712 else if (again || GetLastError() != ERROR_NOT_SUPPORTED) failed(what, "the loaded handle came back");
1713 else pass(what);
1714 }
1715 {
1716 const char* what = "a full path to another file of a compat module's name loads that file";
1717 const HMODULE o = load_w(other_dll.c_str());
1718 if (!o) failed(what, "LoadLibraryW returned NULL");
1719 else if (o == h || answer(o) != 43) failed(what, "the compat DLL came back instead");
1720 else pass(what);
1721 if (o && o != h) FreeLibrary(o);
1722 }
1723 {
1724 const char* what = "a full path to the compat file itself gives the compat module";
1725 if (!h || load_w(path_dll.c_str()) != h || module_w(path_dll.c_str()) != h)
1726 failed(what, "another handle came back");
1727 else pass(what);
1728 }
1729 {
1730 const char* what = "a compat module whose name is outside ASCII answers LoadLibraryW and GetModuleHandleW";
1731 const HMODULE u = load_w(L"TËSTCOMPAT");
1732 if (!u) failed(what, "LoadLibraryW returned NULL");
1733 else if (u != GetModuleHandleW(wide_dll.c_str()) || answer(u) != 44)
1734 failed(what, "the handle is not the compat DLL's");
1735 else if (module_w(L"tëstcompat.dll") != u) failed(what, "GetModuleHandleW did not find it");
1736 else pass(what);
1737 }
1738 }
1739 // Windows loads the pair itself; each case below then asks a fresh Loader, which has looked nothing up yet, so
1740 // the two ways of recognising the dependency (by its handle's folder, by its name) are checked apart.
1741 HMODULE importer = nullptr, live = nullptr;
1742 if (GetModuleHandleW(L"testshim.dll")) {
1743 failed("a compat DLL Windows loaded as another's dependency", "a testshim.dll from an earlier case is loaded");
1744 } else {
1745 importer = LoadLibraryExW(dep_importer.c_str(), nullptr,
1746 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
1747 const DWORD error = GetLastError();
1748 live = GetModuleHandleW(dep_shim.c_str());
1749 if (!importer || !live)
1750 failed("a compat DLL Windows loaded as another's dependency",
1751 "Windows did not load the pair (error " + std::to_string(error) + ")");
1752 }
1753 if (live) {
1754 SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", dep.c_str());
1755 {
1756 const char* what = "GetProcAddress on a compat DLL Windows loaded as a dependency gives only its exports";
1757 rt::Loader loader(g_dir);
1758 if (proc(live, "F")) failed(what, "F, which the DLL lacks, was filled in from the shim");
1759 else pass(what);
1760 }
1761 {
1762 const char* what = "GetModuleHandle / LoadLibrary of a compat DLL Windows loaded as a dependency give it";
1763 rt::Loader loader(g_dir);
1764 if (module_w(L"testshim.dll") != live) failed(what, "GetModuleHandle did not return the live DLL");
1765 else if (load_w(L"testshim.dll") != live) failed(what, "LoadLibrary did not return the live DLL");
1766 else pass(what);
1767 }
1768 }
1769 if (importer) FreeLibrary(importer);
1770 } catch (const std::exception& e) {
1771 failed("LEONIDA_COMPAT_DIR at run time (edges)", e.what());
1772 }
1773 // Loaders keep compat DLLs for the process lifetime; drop the references so the work folder can be removed.
1774 for (const auto& p : {path_dll, other_dll, wide_dll, dep_importer, dep_shim})
1775 while (HMODULE m = GetModuleHandleW(p.c_str())) FreeLibrary(m);
1776}
1777
1534} // namespace 1778} // namespace
1535 1779
1536int main() { 1780int main() {
@@ -1579,6 +1823,8 @@ int main() {
1579 test_unwind_registration(); 1823 test_unwind_registration();
1580 test_lookup_during_loads(); 1824 test_lookup_during_loads();
1581 test_compat_modules(); 1825 test_compat_modules();
1826 test_compat_runtime_loads();
1827 test_compat_runtime_edges();
1582 } catch (const std::exception& e) { 1828 } catch (const std::exception& e) {
1583 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. 1829 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process.
1584 failed("writing the test images", e.what()); 1830 failed("writing the test images", e.what());