upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.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/src/pe_loader.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/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp97
1 files changed, 73 insertions, 24 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index 3558cffb5..d076c5aa0 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -1341,6 +1341,14 @@ void* bundled_load_w(void* ctx, const std::wstring& basename, uint32_t flags, bo
1341 return static_cast<Loader*>(ctx)->load_runtime_module_w(basename, flags, handled); 1341 return static_cast<Loader*>(ctx)->load_runtime_module_w(basename, flags, handled);
1342} 1342}
1343 1343
1344void* bundled_compat_module(void* ctx, const std::string& dll_lower, bool load, bool& handled) {
1345 return static_cast<Loader*>(ctx)->runtime_compat_module(dll_lower, load, handled);
1346}
1347
1348bool bundled_owns_compat_handle(void* ctx, void* module) {
1349 return static_cast<Loader*>(ctx)->owns_compat_handle(module);
1350}
1351
1344const shims::BundledModuleOps kBundledOps{ 1352const shims::BundledModuleOps kBundledOps{
1345 &bundled_find_loaded, 1353 &bundled_find_loaded,
1346 &bundled_load, 1354 &bundled_load,
@@ -1348,6 +1356,8 @@ const shims::BundledModuleOps kBundledOps{
1348 &bundled_owns_handle, 1356 &bundled_owns_handle,
1349 &bundled_find_loaded_w, 1357 &bundled_find_loaded_w,
1350 &bundled_load_w, 1358 &bundled_load_w,
1359 &bundled_compat_module,
1360 &bundled_owns_compat_handle,
1351}; 1361};
1352 1362
1353// Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex). 1363// Marks what a run-time load maps as a dynamic load for as long as it is in scope (the caller holds the mutex).
@@ -2040,37 +2050,76 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
2040 return reinterpret_cast<void*>(p); 2050 return reinterpret_cast<void*>(p);
2041} 2051}
2042 2052
2053void* Loader::compat_handle(const std::string& key, const std::string& dll, bool load, bool& exists,
2054 uint32_t& error) {
2055 exists = false;
2056 error = ERROR_SUCCESS;
2057 if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return nullptr;
2058 if (auto it = compat_modules_.find(key); it != compat_modules_.end()) {
2059 exists = true;
2060 return it->second;
2061 }
2062 const std::filesystem::path candidate = compat_dir_ / std::filesystem::path(dll);
2063 std::error_code ec;
2064 if (!std::filesystem::is_regular_file(candidate, ec)) return nullptr;
2065 exists = true;
2066 if (!load) return nullptr;
2067 // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game
2068 // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL.
2069 // A file Windows cannot map (truncated, wrong machine, not a PE image) otherwise raises a modal "Bad Image"
2070 // box and LoadLibraryExW does not return until someone clicks it: a headless run would hang on a failure
2071 // that the caller reports anyway. The mode is this thread's, and goes back as it was.
2072 DWORD previous_mode = 0;
2073 const bool mode_set = SetThreadErrorMode(GetThreadErrorMode() | SEM_FAILCRITICALERRORS, &previous_mode) != 0;
2074 HMODULE h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
2075 error = h ? ERROR_SUCCESS : GetLastError();
2076 if (mode_set) SetThreadErrorMode(previous_mode, nullptr);
2077 if (!h) return nullptr;
2078 compat_modules_.emplace(key, h);
2079 std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str());
2080 return h;
2081}
2082
2043bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal, 2083bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal,
2044 bool by_ordinal, void*& out) { 2084 bool by_ordinal, void*& out) {
2045 out = nullptr; 2085 out = nullptr;
2046 if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false; 2086 bool exists = false;
2047 HMODULE h = nullptr; 2087 uint32_t error = ERROR_SUCCESS;
2048 if (auto it = compat_modules_.find(key); it != compat_modules_.end()) { 2088 HMODULE h = static_cast<HMODULE>(compat_handle(key, dll, true, exists, error));
2049 h = static_cast<HMODULE>(it->second); 2089 if (!exists) return false;
2050 } else { 2090 if (!h)
2051 const std::filesystem::path candidate = compat_dir_ / std::filesystem::path(dll); 2091 throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(compat_dir_ / std::filesystem::path(dll)) +
2052 std::error_code ec; 2092 " exists but Windows could not load it (error " + std::to_string(error) + ")");
2053 if (!std::filesystem::is_regular_file(candidate, ec)) return false;
2054 // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game
2055 // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL.
2056 // A file Windows cannot map (truncated, wrong machine, not a PE image) otherwise raises a modal "Bad Image"
2057 // box and LoadLibraryExW does not return until someone clicks it: a headless run would hang on a failure
2058 // that is reported right below anyway. The mode is this thread's, and goes back as it was.
2059 DWORD previous_mode = 0;
2060 const bool mode_set = SetThreadErrorMode(GetThreadErrorMode() | SEM_FAILCRITICALERRORS, &previous_mode) != 0;
2061 h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
2062 const DWORD load_error = h ? ERROR_SUCCESS : GetLastError();
2063 if (mode_set) SetThreadErrorMode(previous_mode, nullptr);
2064 if (!h)
2065 throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " +
2066 "load it (error " + std::to_string(load_error) + ")");
2067 compat_modules_.emplace(key, h);
2068 std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str());
2069 }
2070 out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str())); 2093 out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()));
2071 return true; 2094 return true;
2072} 2095}
2073 2096
2097void* Loader::runtime_compat_module(const std::string& dll_lower, bool load, bool& handled) {
2098 std::lock_guard lock(mutex_);
2099 bool exists = false;
2100 uint32_t error = ERROR_SUCCESS;
2101 void* h = compat_handle(dll_lower, dll_lower, load, exists, error);
2102 handled = exists;
2103 if (exists && !h && load) {
2104 // As a LoadLibrary of a file Windows refuses: null and its error, never the shim (the player put the DLL
2105 // there to own the module).
2106 std::fprintf(stderr, "[rt] compat: %s exists but Windows could not load it (error %u, run-time load)\n",
2107 path_utf8(compat_dir_ / std::filesystem::path(dll_lower)).c_str(), error);
2108 SetLastError(error);
2109 } else if (exists && !h) {
2110 SetLastError(ERROR_MOD_NOT_FOUND); // GetModuleHandle of a module nothing has loaded yet
2111 }
2112 return h;
2113}
2114
2115bool Loader::owns_compat_handle(void* module) const {
2116 if (!module) return false;
2117 std::lock_guard lock(mutex_);
2118 for (const auto& [key, h] : compat_modules_)
2119 if (h == module) return true;
2120 return false;
2121}
2122
2074void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { 2123void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) {
2075 std::string key = lower(dll); 2124 std::string key = lower(dll);
2076 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; 2125 std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name;