diff options
| author | natterman12 <92529018+natterman12@users.noreply.github.com> | 2026-11-23 21:50:56 -0500 |
|---|---|---|
| committer | natterman12 <92529018+natterman12@users.noreply.github.com> | 2026-11-23 21:50:56 -0500 |
| commit | 682fd0561cd5e649d199adb9633b81b26baf26a6 (patch) | |
| tree | 836965070f58d2a27939fffa69a63747dfa078fd /runtime/src/pe_loader.cpp | |
| parent | 84347bbb61ed406db10195f50d5911893b8b9a4b (diff) | |
pe_loader: a compat DLL Windows cannot load is refused without a "Bad Image" box
resolve_compat() calls LoadLibraryExW on a file from LEONIDA_COMPAT_DIR. For
a file Windows cannot map (truncated, wrong machine, not a PE image) the
system raises a modal "Bad Image" hard-error box, and LoadLibraryExW does
not return until someone clicks OK. The loader reports that failure itself
one line later, so the box only stalls it: gta6rt hangs on a headless
machine, and loader_test, which writes such a file on purpose, waits for a
click in every unattended ctest run.
The load now runs with SEM_FAILCRITICALERRORS set on the calling thread, and
the thread's previous error mode is restored afterwards. The Windows error
code is read before the mode is put back.
loader_test checks that the mode comes back unchanged, that the message
still carries the error code, and that the refusal does not wait on a box.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/pe_loader.cpp')
| -rw-r--r-- | runtime/src/pe_loader.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index 5e55ac1f9..ff0693a15 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1721,10 +1721,17 @@ bool Loader::resolve_compat(const std::string& key, const std::string& dll, cons | |||
| 1721 | if (!std::filesystem::is_regular_file(candidate, ec)) return false; | 1721 | if (!std::filesystem::is_regular_file(candidate, ec)) return false; |
| 1722 | // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game | 1722 | // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game |
| 1723 | // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL. | 1723 | // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL. |
| 1724 | // A file Windows cannot map (truncated, wrong machine, not a PE image) otherwise raises a modal "Bad Image" | ||
| 1725 | // box and LoadLibraryExW does not return until someone clicks it: a headless run would hang on a failure | ||
| 1726 | // that is reported right below anyway. The mode is this thread's, and goes back as it was. | ||
| 1727 | DWORD previous_mode = 0; | ||
| 1728 | const bool mode_set = SetThreadErrorMode(GetThreadErrorMode() | SEM_FAILCRITICALERRORS, &previous_mode) != 0; | ||
| 1724 | h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); | 1729 | h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 1730 | const DWORD load_error = h ? ERROR_SUCCESS : GetLastError(); | ||
| 1731 | if (mode_set) SetThreadErrorMode(previous_mode, nullptr); | ||
| 1725 | if (!h) | 1732 | if (!h) |
| 1726 | throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " + | 1733 | throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " + |
| 1727 | "load it (error " + std::to_string(GetLastError()) + ")"); | 1734 | "load it (error " + std::to_string(load_error) + ")"); |
| 1728 | compat_modules_.emplace(key, h); | 1735 | compat_modules_.emplace(key, h); |
| 1729 | std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str()); | 1736 | std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str()); |
| 1730 | } | 1737 | } |