diff options
| author | Zack <zmasi21@gmail.com> | 2026-11-28 01:32:59 -0800 |
|---|---|---|
| committer | Zack <zmasi21@gmail.com> | 2026-11-28 01:32:59 -0800 |
| commit | 126058daed271ea3f8497541cd2e12fc31ab3067 (patch) | |
| tree | d76dd55bbb3a316b025fdddda928b1f85b89c15b /runtime/src/pe_loader.cpp | |
| parent | 67ec1d35afaa9300e0e725dbbc06e7c851496841 (diff) | |
| parent | 3ca0707ddec7fd868ecd6df924d751901102fd9c (diff) | |
Merge remote-tracking branch 'origin/main' into results/gpu-vega20-radv
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 161 |
1 files changed, 137 insertions, 24 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index fc73f37f5..5e89ac2ab 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1341,6 +1341,15 @@ 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 | ||
| 1344 | void* bundled_compat_module(void* ctx, const std::wstring& basename, const std::wstring& name, bool load, | ||
| 1345 | uint32_t flags, bool& handled) { | ||
| 1346 | return static_cast<Loader*>(ctx)->runtime_compat_module(basename, name, load, flags, handled); | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | bool bundled_owns_compat_handle(void* ctx, void* module) { | ||
| 1350 | return static_cast<Loader*>(ctx)->owns_compat_handle(module); | ||
| 1351 | } | ||
| 1352 | |||
| 1344 | const shims::BundledModuleOps kBundledOps{ | 1353 | const shims::BundledModuleOps kBundledOps{ |
| 1345 | &bundled_find_loaded, | 1354 | &bundled_find_loaded, |
| 1346 | &bundled_load, | 1355 | &bundled_load, |
| @@ -1348,6 +1357,8 @@ const shims::BundledModuleOps kBundledOps{ | |||
| 1348 | &bundled_owns_handle, | 1357 | &bundled_owns_handle, |
| 1349 | &bundled_find_loaded_w, | 1358 | &bundled_find_loaded_w, |
| 1350 | &bundled_load_w, | 1359 | &bundled_load_w, |
| 1360 | &bundled_compat_module, | ||
| 1361 | &bundled_owns_compat_handle, | ||
| 1351 | }; | 1362 | }; |
| 1352 | 1363 | ||
| 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). | 1364 | // 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,7 +1374,18 @@ struct RuntimeLoadScope { | |||
| 1363 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { | 1374 | Loader::Loader(std::filesystem::path game_dir) : game_dir_(std::move(game_dir)) { |
| 1364 | std::vector<wchar_t> compat(32768); | 1375 | std::vector<wchar_t> compat(32768); |
| 1365 | 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())); |
| 1366 | 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 | } | ||
| 1367 | shims::add_module_lookup(this, &lookup_in_loader); | 1389 | shims::add_module_lookup(this, &lookup_in_loader); |
| 1368 | shims::add_bundled_loader(this, &kBundledOps); | 1390 | shims::add_bundled_loader(this, &kBundledOps); |
| 1369 | } | 1391 | } |
| @@ -2046,37 +2068,128 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 2046 | return reinterpret_cast<void*>(p); | 2068 | return reinterpret_cast<void*>(p); |
| 2047 | } | 2069 | } |
| 2048 | 2070 | ||
| 2071 | const std::filesystem::path* Loader::compat_file(const std::wstring& basename) const { | ||
| 2072 | if (basename.empty()) return nullptr; | ||
| 2073 | for (const auto& file : compat_files_) | ||
| 2074 | if (same_module_name(file.c_str(), basename.c_str())) return &file; | ||
| 2075 | return nullptr; | ||
| 2076 | } | ||
| 2077 | |||
| 2078 | void* Loader::compat_handle(const std::filesystem::path& file, bool load, uint32_t& error) { | ||
| 2079 | error = ERROR_SUCCESS; | ||
| 2080 | const std::string key = lower(path_utf8(file)); | ||
| 2081 | if (auto it = compat_modules_.find(key); it != compat_modules_.end()) return it->second; | ||
| 2082 | const std::filesystem::path candidate = compat_dir_ / file; | ||
| 2083 | // Already live without a lookup through here: Windows loaded it from the folder as another compat DLL's | ||
| 2084 | // dependency (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR), or something loaded it by its path. That is the module a | ||
| 2085 | // static import or a run-time load must share. The reference taken here is kept, as for the DLLs loaded below. | ||
| 2086 | if (HMODULE live = nullptr; GetModuleHandleExW(0, candidate.c_str(), &live) && live) { | ||
| 2087 | compat_modules_.emplace(key, live); | ||
| 2088 | std::fprintf(stderr, "[rt] compat: %s -> %s (already loaded)\n", path_utf8(file).c_str(), | ||
| 2089 | path_utf8(candidate).c_str()); | ||
| 2090 | return live; | ||
| 2091 | } | ||
| 2092 | if (!load) return nullptr; | ||
| 2093 | // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game | ||
| 2094 | // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL. | ||
| 2095 | // A file Windows cannot map (truncated, wrong machine, not a PE image) otherwise raises a modal "Bad Image" | ||
| 2096 | // box and LoadLibraryExW does not return until someone clicks it: a headless run would hang on a failure | ||
| 2097 | // that the caller reports anyway. The mode is this thread's, and goes back as it was. | ||
| 2098 | DWORD previous_mode = 0; | ||
| 2099 | const bool mode_set = SetThreadErrorMode(GetThreadErrorMode() | SEM_FAILCRITICALERRORS, &previous_mode) != 0; | ||
| 2100 | HMODULE h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| 2101 | error = h ? ERROR_SUCCESS : GetLastError(); | ||
| 2102 | if (mode_set) SetThreadErrorMode(previous_mode, nullptr); | ||
| 2103 | if (!h) return nullptr; | ||
| 2104 | compat_modules_.emplace(key, h); | ||
| 2105 | std::fprintf(stderr, "[rt] compat: %s -> %s\n", path_utf8(file).c_str(), path_utf8(candidate).c_str()); | ||
| 2106 | return h; | ||
| 2107 | } | ||
| 2108 | |||
| 2049 | bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal, | 2109 | bool Loader::resolve_compat(const std::string& key, const std::string& dll, const std::string& name, uint16_t ordinal, |
| 2050 | bool by_ordinal, void*& out) { | 2110 | bool by_ordinal, void*& out) { |
| 2051 | out = nullptr; | 2111 | out = nullptr; |
| 2052 | if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false; | 2112 | if (compat_dir_.empty() || key.empty() || key.find_first_of("/\\:") != std::string::npos) return false; |
| 2053 | HMODULE h = nullptr; | 2113 | const std::filesystem::path* file = compat_file(acp_to_wide(dll)); |
| 2054 | if (auto it = compat_modules_.find(key); it != compat_modules_.end()) { | 2114 | if (!file) return false; |
| 2055 | h = static_cast<HMODULE>(it->second); | 2115 | uint32_t error = ERROR_SUCCESS; |
| 2056 | } else { | 2116 | HMODULE h = static_cast<HMODULE>(compat_handle(*file, true, error)); |
| 2057 | const std::filesystem::path candidate = compat_dir_ / std::filesystem::path(dll); | 2117 | if (!h) |
| 2058 | std::error_code ec; | 2118 | throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(compat_dir_ / *file) + |
| 2059 | if (!std::filesystem::is_regular_file(candidate, ec)) return false; | 2119 | " exists but Windows could not load it (error " + std::to_string(error) + ")"); |
| 2060 | // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game | ||
| 2061 | // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL. | ||
| 2062 | // A file Windows cannot map (truncated, wrong machine, not a PE image) otherwise raises a modal "Bad Image" | ||
| 2063 | // box and LoadLibraryExW does not return until someone clicks it: a headless run would hang on a failure | ||
| 2064 | // that is reported right below anyway. The mode is this thread's, and goes back as it was. | ||
| 2065 | DWORD previous_mode = 0; | ||
| 2066 | const bool mode_set = SetThreadErrorMode(GetThreadErrorMode() | SEM_FAILCRITICALERRORS, &previous_mode) != 0; | ||
| 2067 | h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| 2068 | const DWORD load_error = h ? ERROR_SUCCESS : GetLastError(); | ||
| 2069 | if (mode_set) SetThreadErrorMode(previous_mode, nullptr); | ||
| 2070 | if (!h) | ||
| 2071 | throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " + | ||
| 2072 | "load it (error " + std::to_string(load_error) + ")"); | ||
| 2073 | compat_modules_.emplace(key, h); | ||
| 2074 | std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str()); | ||
| 2075 | } | ||
| 2076 | out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str())); | 2120 | out = reinterpret_cast<void*>(GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str())); |
| 2077 | return true; | 2121 | return true; |
| 2078 | } | 2122 | } |
| 2079 | 2123 | ||
| 2124 | void* Loader::runtime_compat_module(const std::wstring& basename, const std::wstring& name, bool load, | ||
| 2125 | uint32_t flags, bool& handled) { | ||
| 2126 | handled = false; | ||
| 2127 | if (compat_dir_.empty()) return nullptr; | ||
| 2128 | std::lock_guard lock(mutex_); | ||
| 2129 | const std::filesystem::path* file = compat_file(basename); | ||
| 2130 | if (!file) return nullptr; | ||
| 2131 | const std::filesystem::path compat_path = compat_dir_ / *file; | ||
| 2132 | // A name with a directory names one file, which Windows would load even when another of the same name is in use: | ||
| 2133 | // the compat DLL answers it only when it is that file (".dll" added as LoadLibrary adds it). Any other path goes | ||
| 2134 | // on as without the folder. | ||
| 2135 | if (name.find_first_of(L"\\/:") != std::wstring::npos) { | ||
| 2136 | std::filesystem::path given(name); | ||
| 2137 | given.replace_filename(basename); | ||
| 2138 | std::error_code ec; | ||
| 2139 | if (!std::filesystem::equivalent(given, compat_path, ec)) return nullptr; | ||
| 2140 | } | ||
| 2141 | handled = true; | ||
| 2142 | // DONT_RESOLVE_DLL_REFERENCES maps a DLL without its dependencies, TLS or DllMain. The compat DLL is shared with | ||
| 2143 | // static imports, initialised, so refuse it before loading or returning a handle, as for bundled DLLs | ||
| 2144 | // (load_runtime_candidate). | ||
| 2145 | if (load && (flags & DONT_RESOLVE_DLL_REFERENCES) != 0) { | ||
| 2146 | std::fprintf(stderr, "[rt] compat: LoadLibraryEx(%s, DONT_RESOLVE_DLL_REFERENCES) is not supported\n", | ||
| 2147 | path_utf8(compat_path).c_str()); | ||
| 2148 | SetLastError(ERROR_NOT_SUPPORTED); | ||
| 2149 | return nullptr; | ||
| 2150 | } | ||
| 2151 | uint32_t error = ERROR_SUCCESS; | ||
| 2152 | void* h = compat_handle(*file, load, error); | ||
| 2153 | if (!h && load) { | ||
| 2154 | // As a LoadLibrary of a file Windows refuses: null and its error, never the shim (the player put the DLL | ||
| 2155 | // there to own the module). | ||
| 2156 | std::fprintf(stderr, "[rt] compat: %s exists but Windows could not load it (error %u, run-time load)\n", | ||
| 2157 | path_utf8(compat_path).c_str(), error); | ||
| 2158 | SetLastError(error); | ||
| 2159 | } else if (!h) { | ||
| 2160 | SetLastError(ERROR_MOD_NOT_FOUND); // GetModuleHandle of a module nothing has loaded yet | ||
| 2161 | } | ||
| 2162 | return h; | ||
| 2163 | } | ||
| 2164 | |||
| 2165 | bool Loader::owns_compat_handle(void* module) { | ||
| 2166 | if (!module || compat_dir_.empty()) return false; | ||
| 2167 | { | ||
| 2168 | std::lock_guard lock(mutex_); | ||
| 2169 | for (const auto& [key, h] : compat_modules_) | ||
| 2170 | if (h == module) return true; | ||
| 2171 | } | ||
| 2172 | // A DLL from the folder that no lookup has seen yet (Windows loaded it as another compat DLL's dependency): its | ||
| 2173 | // exports are its own too. Recorded, with a kept reference, through compat_handle(). GetModuleFileNameW can take | ||
| 2174 | // the Windows loader lock, so the path is read without mutex_ held; compat_dir_ and compat_files_ do not change | ||
| 2175 | // after construction. | ||
| 2176 | std::vector<wchar_t> path(MAX_PATH); | ||
| 2177 | DWORD length = 0; | ||
| 2178 | for (;;) { | ||
| 2179 | length = GetModuleFileNameW(static_cast<HMODULE>(module), path.data(), static_cast<DWORD>(path.size())); | ||
| 2180 | if (!length || length < path.size() || path.size() >= 32768) break; | ||
| 2181 | path.resize(path.size() * 2); | ||
| 2182 | } | ||
| 2183 | if (!length || length >= path.size()) return false; | ||
| 2184 | const std::filesystem::path file(std::wstring(path.data(), length)); | ||
| 2185 | if (!same_module_name(file.parent_path().c_str(), compat_dir_.c_str())) return false; | ||
| 2186 | const std::filesystem::path* entry = compat_file(file.filename().native()); | ||
| 2187 | if (!entry) return false; | ||
| 2188 | std::lock_guard lock(mutex_); | ||
| 2189 | uint32_t error = ERROR_SUCCESS; | ||
| 2190 | return compat_handle(*entry, false, error) == module; | ||
| 2191 | } | ||
| 2192 | |||
| 2080 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { | 2193 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { |
| 2081 | std::string key = lower(dll); | 2194 | std::string key = lower(dll); |
| 2082 | std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; | 2195 | std::string sym = by_ordinal ? "#" + std::to_string(ordinal) : name; |