From bb052d8575b163974128e98d8325b16e41844e5e Mon Sep 17 00:00:00 2001 From: Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:17:06 -0700 Subject: loader: don't bind shim-owned imports to host namesakes; document import routing --- runtime/src/pe_loader.cpp | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f4ecc0de2..77545d3f6 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -1082,14 +1082,20 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d return nullptr; // the bundled module exists and does not export it; don't look for a host namesake } - HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); - if (!h) return nullptr; - if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); - FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast(ordinal)) : symbol.c_str()); - if (!p) return nullptr; - if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast(p)); - binding = Binding::Host; - return reinterpret_cast(p); + // A module the shims own (xgameruntime.dll, gameinput.dll, d3d12_xs.dll, xmem.dll) never binds to a host + // namesake: xgameruntime.dll and gameinput.dll exist on the PC too, and a call into the real Gaming Services + // runtime would bypass the offline shims the same way a direct host load would (owns_dll, registry.cpp). + if (!shims::owns_dll(shim_key)) { + HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + if (!h) return nullptr; + if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); + FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast(ordinal)) : symbol.c_str()); + if (!p) return nullptr; + if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast(p)); + binding = Binding::Host; + return reinterpret_cast(p); + } + return nullptr; } void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { @@ -1117,13 +1123,18 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name } } // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. - if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { - if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it - FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); - if (p) { - if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast(p)); - m.imports.push_back({dll, sym, Binding::Host}); - return reinterpret_cast(p); + // A shim-owned module is skipped: xgameruntime.dll and gameinput.dll have real PC namesakes in System32, and an + // unregistered import must end unimplemented, exactly as GetProcAddress on the pseudo-handle returns NULL + // (library_loader.cpp). Binding it to Gaming Services would also leave the process online-capable. + if (!shims::owns_dll(key)) { + if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { + if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it + FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); + if (p) { + if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast(p)); + m.imports.push_back({dll, sym, Binding::Host}); + return reinterpret_cast(p); + } } } m.imports.push_back({dll, sym, Binding::Unimplemented}); -- cgit v1.2.3