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 15:05:13 -0300
committerMike7581 <168859477+Mike7581@users.noreply.github.com>2026-11-25 15:15:43 -0300
commit2cca47b5c8971c885961c35741ab8b0f0debaa01 (patch)
tree0184aca6153b5a15775ec358ed905e3910e20acb /runtime/src/pe_loader.cpp
parent582e14d922ad9b24a63ba55f7b8e57bd4aef3c08 (diff)
loader: compat run-time loads honour LoadLibraryEx flags and Windows-loaded dependencies
Review follow-up for #1565: - LoadLibraryEx with DONT_RESOLVE_DLL_REFERENCES on a LEONIDA_COMPAT_DIR module fails with ERROR_NOT_SUPPORTED, before loading or returning a loaded handle, as for bundled DLLs. - A compat DLL that Windows loaded as another compat DLL's dependency is recognised: compat_handle() finds it by its full path, and owns_compat_handle() by its folder (read outside mutex_, since GetModuleFileNameW can take the loader lock), so GetModuleHandle and LoadLibrary return it and GetProcAddress never fills in a missing export from the shim. - A name with a directory gets the compat DLL only when it is that file; any other path goes on as without the folder. - Names outside ASCII reach the compat lookup (it runs before the ASCII / wide split). - The folder's file names are listed once per Loader, so lookups no longer touch the disk. loader_test: test_compat_runtime_edges adds six cases for the first four points, through LoadLibraryExA/W, LoadLibraryW, GetModuleHandleW and GetProcAddress; all six fail on 582e14d. The listing has no functional test.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp126
1 files changed, 95 insertions, 31 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index d076c5aa0..c5e97e649 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -1341,8 +1341,9 @@ 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) { 1344void* bundled_compat_module(void* ctx, const std::wstring& basename, const std::wstring& name, bool load,
1345 return static_cast<Loader*>(ctx)->runtime_compat_module(dll_lower, load, handled); 1345 uint32_t flags, bool& handled) {
1346 return static_cast<Loader*>(ctx)->runtime_compat_module(basename, name, load, flags, handled);
1346} 1347}
1347 1348
1348bool bundled_owns_compat_handle(void* ctx, void* module) { 1349bool bundled_owns_compat_handle(void* ctx, void* module) {
@@ -1373,7 +1374,18 @@ struct RuntimeLoadScope {
1373Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { 1374Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) {
1374 std::vector<wchar_t> compat(32768); 1375 std::vector<wchar_t> compat(32768);
1375 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.data(), static_cast<DWORD>(compat.size())); 1376 const DWORD n = GetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.data(), static_cast<DWORD>(compat.size()));
1376 if (n > 0 && n < compat.size()) compat_dir_ = std::filesystem::path(compat.data()); 1377 if (n > 0 && n < compat.size()) {
1378 // Absolute, with backslashes and no trailing separator: LoadLibraryExW with LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
1379 // needs a full path, and owns_compat_handle() compares it with a module's own folder.
1380 std::error_code ec;
1381 compat_dir_ = std::filesystem::absolute(std::filesystem::path(compat.data()), ec).lexically_normal();
1382 compat_dir_.make_preferred();
1383 if (!compat_dir_.has_filename()) compat_dir_ = compat_dir_.parent_path();
1384 for (std::filesystem::directory_iterator it(compat_dir_, ec), end; !ec && it != end; it.increment(ec)) {
1385 std::error_code file_ec;
1386 if (it->is_regular_file(file_ec)) compat_files_.push_back(it->path().filename());
1387 }
1388 }
1377 shims::add_module_lookup(this, &lookup_in_loader); 1389 shims::add_module_lookup(this, &lookup_in_loader);
1378 shims::add_bundled_loader(this, &kBundledOps); 1390 shims::add_bundled_loader(this, &kBundledOps);
1379} 1391}
@@ -2050,19 +2062,27 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
2050 return reinterpret_cast<void*>(p); 2062 return reinterpret_cast<void*>(p);
2051} 2063}
2052 2064
2053void* Loader::compat_handle(const std::string& key, const std::string& dll, bool load, bool& exists, 2065const std::filesystem::path* Loader::compat_file(const std::wstring& basename) const {
2054 uint32_t& error) { 2066 if (basename.empty()) return nullptr;
2055 exists = false; 2067 for (const auto& file : compat_files_)
2068 if (same_module_name(file.c_str(), basename.c_str())) return &file;
2069 return nullptr;
2070}
2071
2072void* Loader::compat_handle(const std::filesystem::path& file, bool load, uint32_t& error) {
2056 error = ERROR_SUCCESS; 2073 error = ERROR_SUCCESS;
2057 if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return nullptr; 2074 const std::string key = lower(path_utf8(file));
2058 if (auto it = compat_modules_.find(key); it != compat_modules_.end()) { 2075 if (auto it = compat_modules_.find(key); it != compat_modules_.end()) return it->second;
2059 exists = true; 2076 const std::filesystem::path candidate = compat_dir_ / file;
2060 return it->second; 2077 // Already live without a lookup through here: Windows loaded it from the folder as another compat DLL's
2078 // dependency (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR), or something loaded it by its path. That is the module a
2079 // static import or a run-time load must share. The reference taken here is kept, as for the DLLs loaded below.
2080 if (HMODULE live = nullptr; GetModuleHandleExW(0, candidate.c_str(), &live) && live) {
2081 compat_modules_.emplace(key, live);
2082 std::fprintf(stderr, "[rt] compat: %s -> %s (already loaded)\n", path_utf8(file).c_str(),
2083 path_utf8(candidate).c_str());
2084 return live;
2061 } 2085 }
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; 2086 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 2087 // 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. 2088 // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL.
@@ -2076,48 +2096,92 @@ void* Loader::compat_handle(const std::string& key, const std::string& dll, bool
2076 if (mode_set) SetThreadErrorMode(previous_mode, nullptr); 2096 if (mode_set) SetThreadErrorMode(previous_mode, nullptr);
2077 if (!h) return nullptr; 2097 if (!h) return nullptr;
2078 compat_modules_.emplace(key, h); 2098 compat_modules_.emplace(key, h);
2079 std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str()); 2099 std::fprintf(stderr, "[rt] compat: %s -> %s\n", path_utf8(file).c_str(), path_utf8(candidate).c_str());
2080 return h; 2100 return h;
2081} 2101}
2082 2102
2083bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal, 2103bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal,
2084 bool by_ordinal, void*& out) { 2104 bool by_ordinal, void*& out) {
2085 out = nullptr; 2105 out = nullptr;
2086 bool exists = false; 2106 if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false;
2107 const std::filesystem::path* file = compat_file(acp_to_wide(dll));
2108 if (!file) return false;
2087 uint32_t error = ERROR_SUCCESS; 2109 uint32_t error = ERROR_SUCCESS;
2088 HMODULE h = static_cast<HMODULE>(compat_handle(key, dll, true, exists, error)); 2110 HMODULE h = static_cast<HMODULE>(compat_handle(*file, true, error));
2089 if (!exists) return false;
2090 if (!h) 2111 if (!h)
2091 throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(compat_dir_ / std::filesystem::path(dll)) + 2112 throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(compat_dir_ / *file) +
2092 " exists but Windows could not load it (error " + std::to_string(error) + ")"); 2113 " exists but Windows could not load it (error " + std::to_string(error) + ")");
2093 out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str())); 2114 out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()));
2094 return true; 2115 return true;
2095} 2116}
2096 2117
2097void* Loader::runtime_compat_module(const std::string& dll_lower, bool load, bool& handled) { 2118void* Loader::runtime_compat_module(const std::wstring& basename, const std::wstring& name, bool load,
2119 uint32_t flags, bool& handled) {
2120 handled = false;
2121 if (compat_dir_.empty()) return nullptr;
2098 std::lock_guard lock(mutex_); 2122 std::lock_guard lock(mutex_);
2099 bool exists = false; 2123 const std::filesystem::path* file = compat_file(basename);
2124 if (!file) return nullptr;
2125 const std::filesystem::path compat_path = compat_dir_ / *file;
2126 // A name with a directory names one file, which Windows would load even when another of the same name is in use:
2127 // the compat DLL answers it only when it is that file (".dll" added as LoadLibrary adds it). Any other path goes
2128 // on as without the folder.
2129 if (name.find_first_of(L"\\/:") != std::wstring::npos) {
2130 std::filesystem::path given(name);
2131 given.replace_filename(basename);
2132 std::error_code ec;
2133 if (!std::filesystem::equivalent(given, compat_path, ec)) return nullptr;
2134 }
2135 handled = true;
2136 // DONT_RESOLVE_DLL_REFERENCES maps a DLL without its dependencies, TLS or DllMain. The compat DLL is shared with
2137 // static imports, initialised, so refuse it before loading or returning a handle, as for bundled DLLs
2138 // (load_runtime_candidate).
2139 if (load && (flags & DONT_RESOLVE_DLL_REFERENCES) != 0) {
2140 std::fprintf(stderr, "[rt] compat: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported\n",
2141 path_utf8(compat_path).c_str());
2142 SetLastError(ERROR_NOT_SUPPORTED);
2143 return nullptr;
2144 }
2100 uint32_t error = ERROR_SUCCESS; 2145 uint32_t error = ERROR_SUCCESS;
2101 void* h = compat_handle(dll_lower, dll_lower, load, exists, error); 2146 void* h = compat_handle(*file, load, error);
2102 handled = exists; 2147 if (!h && load) {
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 2148 // 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). 2149 // 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", 2150 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); 2151 path_utf8(compat_path).c_str(), error);
2108 SetLastError(error); 2152 SetLastError(error);
2109 } else if (exists && !h) { 2153 } else if (!h) {
2110 SetLastError(ERROR_MOD_NOT_FOUND); // GetModuleHandle of a module nothing has loaded yet 2154 SetLastError(ERROR_MOD_NOT_FOUND); // GetModuleHandle of a module nothing has loaded yet
2111 } 2155 }
2112 return h; 2156 return h;
2113} 2157}
2114 2158
2115bool Loader::owns_compat_handle(void* module) const { 2159bool Loader::owns_compat_handle(void* module) {
2116 if (!module) return false; 2160 if (!module || compat_dir_.empty()) return false;
2161 {
2162 std::lock_guard lock(mutex_);
2163 for (const auto& [key, h] : compat_modules_)
2164 if (h == module) return true;
2165 }
2166 // A DLL from the folder that no lookup has seen yet (Windows loaded it as another compat DLL's dependency): its
2167 // exports are its own too. Recorded, with a kept reference, through compat_handle(). GetModuleFileNameW can take
2168 // the Windows loader lock, so the path is read without mutex_ held; compat_dir_ and compat_files_ do not change
2169 // after construction.
2170 std::vector<wchar_t> path(MAX_PATH);
2171 DWORD length = 0;
2172 for (;;) {
2173 length = GetModuleFileNameW(static_cast<HMODULE>(module), path.data(), static_cast<DWORD>(path.size()));
2174 if (!length || length < path.size() || path.size() >= 32768) break;
2175 path.resize(path.size() * 2);
2176 }
2177 if (!length || length >= path.size()) return false;
2178 const std::filesystem::path file(std::wstring(path.data(), length));
2179 if (!same_module_name(file.parent_path().c_str(), compat_dir_.c_str())) return false;
2180 const std::filesystem::path* entry = compat_file(file.filename().native());
2181 if (!entry) return false;
2117 std::lock_guard lock(mutex_); 2182 std::lock_guard lock(mutex_);
2118 for (const auto& [key, h] : compat_modules_) 2183 uint32_t error = ERROR_SUCCESS;
2119 if (h == module) return true; 2184 return compat_handle(*entry, false, error) == module;
2120 return false;
2121} 2185}
2122 2186
2123void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { 2187void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) {