Files
mandy/runtime/test/library_loader_names_test.cpp
Tyrannys f6ec7480ff library_loader_names: run with Wine's native wsock32, and check the copy is the fixture
Under Wine, loading a wsock32.dll by path gives Wine's builtin wsock32, not the
fixture copy, so the same-base checks failed (Tauwni, Wine 11 on macOS, #2525).
CTest now runs the test with WINEDLLOVERRIDES=wsock32=n,b, which Windows ignores
and scripts/run_wine_tests.py keeps, and the test checks by path that the module
it loaded is its own copy, saying why when it is not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 22:36:30 -06:00

177 lines
7.8 KiB
C++

// library_loader's module names (shims/library_loader.cpp). kevincommits' finding in #1608 (item 2):
//
// - An empty name, or a path that ends in a slash, names no module but came out as ".dll", which then went to the
// bundled loader's lookups as if it were a module's name.
// - GetProcAddress on a host module looked its name up once and cached it by HMODULE. FreeLibrary kept the entry, so a
// different DLL loaded later at the same base was served the first one's shims. The name is now asked of Windows on
// every call, which also covers another thread loading a DLL at that base between FreeLibrary and any cache update.
//
// The bundled loader here is a fake that records the names it is asked for. The second case uses fixed_base_probe.dll
// (no relocations, one fixed base) copied under two names: wsock32.dll, for which this test registers a shim (the
// shims do not own wsock32.dll, so it stays a host module), and a plain name. Wine serves its builtin wsock32 for a
// wsock32.dll loaded by path, so CMake runs the test with WINEDLLOVERRIDES=wsock32=n,b (Windows ignores it), and the
// test checks that the module it got is the copy it made (Tauwni's Wine 11 run on #2525). No game files.
#include <windows.h>
#include <cstdio>
#include <filesystem>
#include <string>
#include <vector>
#include "shims.h"
namespace {
int g_failures = 0;
void check(bool ok, const char* what) {
std::printf("%s %s\n", ok ? "PASS" : "FAIL", what);
std::fflush(stdout);
if (!ok) ++g_failures;
}
int probe_export() { return 42; }
const rt::shims::Entry kProbe[] = {{"wsock32.dll", "leonida_loader_probe", reinterpret_cast<void*>(&probe_export)}};
const rt::shims::Table kProbeTable(kProbe, 1);
std::vector<std::string> g_asked;
std::vector<std::wstring> g_asked_w;
void* fake_find_loaded(void*, const std::string& dll) {
g_asked.push_back(dll);
return nullptr;
}
void* fake_load(void*, const std::string& dll, uint32_t, bool& handled) {
g_asked.push_back(dll);
handled = false;
return nullptr;
}
void* fake_get_proc(void*, void*, const std::string&, bool& owns) {
owns = false;
return nullptr;
}
bool fake_owns(void*, void*) { return false; }
void* fake_find_loaded_w(void*, const std::wstring& dll) {
g_asked_w.push_back(dll);
return nullptr;
}
void* fake_load_w(void*, const std::wstring& dll, uint32_t, bool& handled) {
g_asked_w.push_back(dll);
handled = false;
return nullptr;
}
const rt::shims::BundledModuleOps kFake{fake_find_loaded, fake_load, fake_get_proc, fake_owns, fake_find_loaded_w,
fake_load_w};
template <class F>
F shim(const char* name) {
return reinterpret_cast<F>(rt::shims::find("kernel32.dll", name));
}
// Whether module is the file at path (not, say, a host's own DLL of the same name).
bool is_file(HMODULE module, const std::filesystem::path& path) {
wchar_t got[MAX_PATH * 4];
const DWORD n = GetModuleFileNameW(module, got, static_cast<DWORD>(std::size(got)));
std::error_code ec;
return n && n < std::size(got) && std::filesystem::equivalent(got, path, ec);
}
bool asked_for_dot_dll() {
for (const auto& n : g_asked)
if (n == ".dll") return true;
for (const auto& n : g_asked_w)
if (n == L".dll") return true;
return false;
}
} // namespace
int main(int argc, char** argv) {
if (argc != 2) {
std::fprintf(stderr, "usage: library_loader_names_test <fixed_base_probe.dll>\n");
return 2;
}
using LoadA = HMODULE(WINAPI*)(LPCSTR);
using LoadW = HMODULE(WINAPI*)(LPCWSTR);
using GetProc = FARPROC(WINAPI*)(HMODULE, LPCSTR);
using Free = BOOL(WINAPI*)(HMODULE);
const auto load_a = shim<LoadA>("LoadLibraryA");
const auto load_w = shim<LoadW>("LoadLibraryW");
const auto get_mod_a = shim<LoadA>("GetModuleHandleA");
const auto get_mod_w = shim<LoadW>("GetModuleHandleW");
const auto get_proc = shim<GetProc>("GetProcAddress");
const auto free_lib = shim<Free>("FreeLibrary");
if (!load_a || !load_w || !get_mod_a || !get_mod_w || !get_proc || !free_lib) {
std::fprintf(stderr, "FAIL the loader shims are not registered\n");
return 1;
}
// 1. Names that name no module never reach the bundled loader as ".dll".
int ctx = 0;
rt::shims::add_bundled_loader(&ctx, &kFake);
load_a("");
load_a("C:\\leonida_no_such_dir\\");
load_a("C:/leonida_no_such_dir/");
load_w(L"");
load_w(L"C:\\leonida_no_such_dir\\");
load_w(L"C:\\leonida_\u00e9t\u00e9\\");
get_mod_a("");
get_mod_a("C:\\leonida_no_such_dir\\");
get_mod_w(L"C:\\leonida_no_such_dir\\");
get_mod_w(L"C:\\leonida_\u00e9t\u00e9\\");
check(!asked_for_dot_dll(), "an empty name or a path ending in a slash is not looked up as \".dll\"");
g_asked.clear();
load_a("C:\\leonida_no_such_dir\\some_module");
check(g_asked.size() == 1 && g_asked[0] == "some_module.dll", "a name without an extension still gets \".dll\"");
rt::shims::remove_bundled_loader(&ctx);
// 2. FreeLibrary drops the cached name: the next DLL at the same base is not served the old one's shims.
const std::filesystem::path dir =
std::filesystem::temp_directory_path() / ("leonida_loader_names_" + std::to_string(GetCurrentProcessId()));
std::error_code ec;
std::filesystem::create_directories(dir, ec);
const auto shimmed = dir / "wsock32.dll";
const auto plain = dir / "leonida_plain_probe.dll";
std::filesystem::copy_file(argv[1], shimmed, std::filesystem::copy_options::overwrite_existing, ec);
std::filesystem::copy_file(argv[1], plain, std::filesystem::copy_options::overwrite_existing, ec);
HMODULE first = load_w(shimmed.c_str());
const bool fixture = first && is_file(first, shimmed);
check(fixture, "the fixture loads as wsock32.dll");
if (first && !fixture)
std::puts(" the host served its own wsock32.dll instead of the copy (under Wine: run with "
"WINEDLLOVERRIDES=wsock32=n,b, as CTest does)");
if (fixture) {
check(reinterpret_cast<void*>(get_proc(first, "leonida_loader_probe")) ==
reinterpret_cast<void*>(&probe_export),
"GetProcAddress on it serves wsock32.dll's shim");
check(free_lib(first) == TRUE, "FreeLibrary unloads it");
HMODULE second = load_w(plain.c_str());
check(second == first, "the plain copy loads at the same base (fixed, no relocations)");
if (second == first)
check(get_proc(second, "leonida_loader_probe") == nullptr,
"GetProcAddress on the new module does not serve the freed module's shim");
if (second) free_lib(second);
// The race timbot103 found in #2525: the module is unloaded and another DLL is mapped at its base (by another
// thread) before the unloading FreeLibrary could drop any cached name. The host's FreeLibrary is called
// directly here, so the shim never sees the unload: the name must still be the new module's.
HMODULE third = load_w(shimmed.c_str());
check(third == first && reinterpret_cast<void*>(get_proc(third, "leonida_loader_probe")) ==
reinterpret_cast<void*>(&probe_export),
"the fixture loads as wsock32.dll again and is served its shim");
check(::FreeLibrary(third) == TRUE, "the host's FreeLibrary unloads it, past the shim");
HMODULE fourth = load_w(plain.c_str());
check(fourth == first, "the plain copy loads at the same base again");
if (fourth == first)
check(get_proc(fourth, "leonida_loader_probe") == nullptr,
"a module mapped where an unloaded one was is not served its shims, without the shim seeing the unload");
if (fourth) free_lib(fourth);
}
std::filesystem::remove_all(dir, ec);
std::printf("%d failure(s)\n", g_failures);
return g_failures ? 1 : 0;
}