From 682fd0561cd5e649d199adb9633b81b26baf26a6 Mon Sep 17 00:00:00 2001 From: natterman12 <92529018+natterman12@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:50:56 -0500 Subject: 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 --- runtime/src/pe_loader.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'runtime/src/pe_loader.cpp') 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 if (!std::filesystem::is_regular_file(candidate, ec)) return false; // The full path, with the compat folder on the DLL's own search path for its dependencies; never the game // folder. Windows maps it, so its CRT, TLS and SEH work as for any host DLL. + // A file Windows cannot map (truncated, wrong machine, not a PE image) otherwise raises a modal "Bad Image" + // box and LoadLibraryExW does not return until someone clicks it: a headless run would hang on a failure + // that is reported right below anyway. The mode is this thread's, and goes back as it was. + DWORD previous_mode = 0; + const bool mode_set = SetThreadErrorMode(GetThreadErrorMode() | SEM_FAILCRITICALERRORS, &previous_mode) != 0; h = LoadLibraryExW(candidate.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); + const DWORD load_error = h ? ERROR_SUCCESS : GetLastError(); + if (mode_set) SetThreadErrorMode(previous_mode, nullptr); if (!h) throw std::runtime_error("LEONIDA_COMPAT_DIR: " + path_utf8(candidate) + " exists but Windows could not " + - "load it (error " + std::to_string(GetLastError()) + ")"); + "load it (error " + std::to_string(load_error) + ")"); compat_modules_.emplace(key, h); std::fprintf(stderr, "[rt] compat: %s -> %s\n", dll.c_str(), path_utf8(candidate).c_str()); } -- cgit v1.2.3