diff options
| author | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 10:49:02 +0000 |
|---|---|---|
| committer | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 10:58:12 +0000 |
| commit | 6f5b59ea8159dd99fba20933616f778135cc1fa7 (patch) | |
| tree | 42bab2780508bea94893321c607cabc5810fb0d6 /runtime/shims/library_loader.cpp | |
| parent | c2967a5e60060e4068fb992196d8f4b494a5046c (diff) | |
runtime: serve LoadLibrary/GetProcAddress of shim-owned modules; xgameruntime.lib's run-time entry points
The package's GDK modules don't import xgameruntime.dll. libHttpClient.GDK.dll and XCurl.dll have no import
entry for it, but both carry the wide string "xgameruntime.dll" and the names InitializeApiImpl[Ex|Ex2],
QueryApiImpl and UninitializeApiImpl next to a statically linked XGameRuntimeInitialize: xgameruntime.lib's
thunks LoadLibraryW the runtime and GetProcAddress those names. Bound to the host, that bypasses the offline
shim. The executable is very likely built the same way.
- library_loader.cpp: LoadLibrary*/GetModuleHandle*/GetProcAddress/FreeLibrary for kernel32, kernelbase and the
api-ms-win-core-libraryloader sets. A module the registry owns (rt::shims::owns_dll) gets a pseudo-handle and
GetProcAddress resolves through the registry; an unserved name is NULL with ERROR_PROC_NOT_FOUND. Every other
module goes to the host.
- xgameruntime.cpp: the five entry points, with the signatures and the unknown-class error documented by
xodus-gaming/xgameruntime-docs (GPL-3.0, facts only). QueryApiImpl logs the class and interface ids and refuses
them, so the first boot with the executable names the COM interfaces to implement.
- xgameruntime_test drives the path through XCurl.dll's api-set binding; docs/runtime_imports.md records the
measurement and the new dry-run counts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/library_loader.cpp')
| -rw-r--r-- | runtime/shims/library_loader.cpp | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/runtime/shims/library_loader.cpp b/runtime/shims/library_loader.cpp new file mode 100644 index 000000000..4bcc57704 --- /dev/null +++ b/runtime/shims/library_loader.cpp | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | // library_loader: LoadLibrary / GetModuleHandle / GetProcAddress for modules the shims own. | ||
| 2 | // | ||
| 3 | // GDK code does not always import the console runtime. The package's libHttpClient.GDK.dll and XCurl.dll have no | ||
| 4 | // import entry for xgameruntime.dll at all: their statically linked xgameruntime.lib thunks hold the wide string | ||
| 5 | // "xgameruntime.dll" and the names InitializeApiImpl, InitializeApiImplEx, InitializeApiImplEx2, QueryApiImpl and | ||
| 6 | // UninitializeApiImpl, and look them up at run time (docs/runtime_imports.md, "Run-time loaded modules"). Bound to the | ||
| 7 | // host, that LoadLibraryW would load the PC's Gaming Services runtime or fail, and never reach the offline shims. | ||
| 8 | // | ||
| 9 | // So for a module the shim registry owns (rt::shims::owns_dll: xgameruntime.dll, d3d12_xs.dll, xmem.dll, ...), these | ||
| 10 | // return a pseudo-handle, and GetProcAddress on it resolves names (and "#ordinal") through rt::shims::find. A name the | ||
| 11 | // registry doesn't serve returns NULL with ERROR_PROC_NOT_FOUND, as a real module would, and is logged once. Every | ||
| 12 | // other module goes to the host unchanged. Only images mapped by the loader see these: they are bound through the | ||
| 13 | // registry, the runtime's own code calls the host directly. | ||
| 14 | #include <windows.h> | ||
| 15 | |||
| 16 | #include <cstdio> | ||
| 17 | #include <deque> | ||
| 18 | #include <mutex> | ||
| 19 | #include <set> | ||
| 20 | #include <string> | ||
| 21 | |||
| 22 | #include "shims.h" | ||
| 23 | |||
| 24 | namespace rt::shims::library_loader { | ||
| 25 | |||
| 26 | // A module the shims own, identified by a pointer to one of these. Host modules are image bases (64 KiB aligned); | ||
| 27 | // these live in the runtime's heap, and GetProcAddress tells them apart by lookup, not by address range. | ||
| 28 | struct Pseudo { | ||
| 29 | char tag[8] = {'L', 'E', 'O', 'S', 'H', 'I', 'M', 0}; | ||
| 30 | std::string dll; // lower case, with .dll | ||
| 31 | }; | ||
| 32 | |||
| 33 | namespace { | ||
| 34 | |||
| 35 | std::mutex g_mutex; | ||
| 36 | std::deque<Pseudo>& pseudos() { // a deque never moves its elements, so handed-out handles stay valid | ||
| 37 | static std::deque<Pseudo> d; | ||
| 38 | return d; | ||
| 39 | } | ||
| 40 | |||
| 41 | std::string lower_ascii(std::string s) { | ||
| 42 | for (auto& c : s) | ||
| 43 | if (c >= 'A' && c <= 'Z') c = static_cast<char>(c + 32); | ||
| 44 | return s; | ||
| 45 | } | ||
| 46 | |||
| 47 | std::string narrow(const wchar_t* w) { | ||
| 48 | std::string s; | ||
| 49 | for (; w && *w; ++w) s += (*w < 0x80) ? static_cast<char>(*w) : '?'; | ||
| 50 | return s; | ||
| 51 | } | ||
| 52 | |||
| 53 | // LoadLibrary's file-name rules for the module a path names: the last path component, lower case, and ".dll" added | ||
| 54 | // when there is no extension (a trailing '.' means "no extension, add nothing"). | ||
| 55 | std::string module_name(std::string path) { | ||
| 56 | const size_t slash = path.find_last_of("\\/"); | ||
| 57 | if (slash != std::string::npos) path.erase(0, slash + 1); | ||
| 58 | path = lower_ascii(path); | ||
| 59 | if (!path.empty() && path.back() == '.') path.pop_back(); | ||
| 60 | else if (path.find('.') == std::string::npos) path += ".dll"; | ||
| 61 | return path; | ||
| 62 | } | ||
| 63 | |||
| 64 | } // namespace | ||
| 65 | |||
| 66 | // The pseudo-handle for an owned module, or nullptr when the shims don't own `path`'s module. | ||
| 67 | HMODULE owned_handle(const std::string& path) { | ||
| 68 | const std::string dll = module_name(path); | ||
| 69 | if (dll.empty() || !owns_dll(dll)) return nullptr; | ||
| 70 | std::lock_guard lock(g_mutex); | ||
| 71 | for (auto& p : pseudos()) | ||
| 72 | if (p.dll == dll) return reinterpret_cast<HMODULE>(&p); | ||
| 73 | pseudos().push_back(Pseudo{}); | ||
| 74 | pseudos().back().dll = dll; | ||
| 75 | std::fprintf(stderr, "[rt] loader: %s is served by the shims (run-time load)\n", dll.c_str()); | ||
| 76 | return reinterpret_cast<HMODULE>(&pseudos().back()); | ||
| 77 | } | ||
| 78 | |||
| 79 | // The owned module a handle names, or nullptr for a host module. | ||
| 80 | const Pseudo* pseudo(HMODULE h) { | ||
| 81 | std::lock_guard lock(g_mutex); | ||
| 82 | for (auto& p : pseudos()) | ||
| 83 | if (reinterpret_cast<HMODULE>(&p) == h) return &p; | ||
| 84 | return nullptr; | ||
| 85 | } | ||
| 86 | |||
| 87 | namespace { | ||
| 88 | |||
| 89 | HMODULE WINAPI load_library_a(LPCSTR name) { | ||
| 90 | if (HMODULE h = name ? owned_handle(name) : nullptr) return h; | ||
| 91 | return LoadLibraryA(name); | ||
| 92 | } | ||
| 93 | |||
| 94 | HMODULE WINAPI load_library_w(LPCWSTR name) { | ||
| 95 | if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h; | ||
| 96 | return LoadLibraryW(name); | ||
| 97 | } | ||
| 98 | |||
| 99 | HMODULE WINAPI load_library_ex_a(LPCSTR name, HANDLE file, DWORD flags) { | ||
| 100 | if (HMODULE h = name ? owned_handle(name) : nullptr) return h; | ||
| 101 | return LoadLibraryExA(name, file, flags); | ||
| 102 | } | ||
| 103 | |||
| 104 | HMODULE WINAPI load_library_ex_w(LPCWSTR name, HANDLE file, DWORD flags) { | ||
| 105 | if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h; | ||
| 106 | return LoadLibraryExW(name, file, flags); | ||
| 107 | } | ||
| 108 | |||
| 109 | // GetModuleHandle answers for an owned module as if it were loaded: the GDK may probe before it loads. | ||
| 110 | HMODULE WINAPI get_module_handle_a(LPCSTR name) { | ||
| 111 | if (HMODULE h = name ? owned_handle(name) : nullptr) return h; | ||
| 112 | return GetModuleHandleA(name); | ||
| 113 | } | ||
| 114 | |||
| 115 | HMODULE WINAPI get_module_handle_w(LPCWSTR name) { | ||
| 116 | if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h; | ||
| 117 | return GetModuleHandleW(name); | ||
| 118 | } | ||
| 119 | |||
| 120 | FARPROC WINAPI get_proc_address(HMODULE module, LPCSTR name) { | ||
| 121 | const Pseudo* p = pseudo(module); | ||
| 122 | if (!p) return GetProcAddress(module, name); | ||
| 123 | // A value below 0x10000 is an ordinal (IS_INTRESOURCE), as in the real GetProcAddress. | ||
| 124 | const std::string what = IS_INTRESOURCE(name) ? "#" + std::to_string(reinterpret_cast<uintptr_t>(name)) : name; | ||
| 125 | if (void* fn = find(p->dll, what)) return reinterpret_cast<FARPROC>(fn); | ||
| 126 | { | ||
| 127 | static std::mutex m; | ||
| 128 | static std::set<std::string> seen; | ||
| 129 | std::lock_guard lock(m); | ||
| 130 | if (seen.insert(p->dll + "!" + what).second) | ||
| 131 | std::fprintf(stderr, "[rt] loader: GetProcAddress(%s, %s): not served\n", p->dll.c_str(), what.c_str()); | ||
| 132 | } | ||
| 133 | SetLastError(ERROR_PROC_NOT_FOUND); | ||
| 134 | return nullptr; | ||
| 135 | } | ||
| 136 | |||
| 137 | BOOL WINAPI free_library(HMODULE module) { | ||
| 138 | if (pseudo(module)) return TRUE; // owned modules are never unloaded | ||
| 139 | return FreeLibrary(module); | ||
| 140 | } | ||
| 141 | |||
| 142 | #define LOADER(dll) \ | ||
| 143 | {dll, "LoadLibraryA", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_a)}, \ | ||
| 144 | {dll, "LoadLibraryW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_w)}, \ | ||
| 145 | {dll, "LoadLibraryExA", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_a)}, \ | ||
| 146 | {dll, "LoadLibraryExW", reinterpret_cast<void*>(&rt::shims::library_loader::load_library_ex_w)}, \ | ||
| 147 | {dll, "GetModuleHandleA", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_a)}, \ | ||
| 148 | {dll, "GetModuleHandleW", reinterpret_cast<void*>(&rt::shims::library_loader::get_module_handle_w)}, \ | ||
| 149 | {dll, "GetProcAddress", reinterpret_cast<void*>(&rt::shims::library_loader::get_proc_address)}, \ | ||
| 150 | {dll, "FreeLibrary", reinterpret_cast<void*>(&rt::shims::library_loader::free_library)} | ||
| 151 | |||
| 152 | } // namespace | ||
| 153 | } // namespace rt::shims::library_loader | ||
| 154 | |||
| 155 | // The package's DLLs import the loader functions through these api sets (XCurl.dll: libraryloader-l1-2-0 and | ||
| 156 | // -l1-2-1); kernel32 and kernelbase cover code linked against the classic libraries. | ||
| 157 | SHIM_TABLE(library_loader, | ||
| 158 | LOADER("kernel32.dll"), | ||
| 159 | LOADER("kernelbase.dll"), | ||
| 160 | LOADER("api-ms-win-core-libraryloader-l1-1-0.dll"), | ||
| 161 | LOADER("api-ms-win-core-libraryloader-l1-1-1.dll"), | ||
| 162 | LOADER("api-ms-win-core-libraryloader-l1-2-0.dll"), | ||
| 163 | LOADER("api-ms-win-core-libraryloader-l1-2-1.dll"), | ||
| 164 | LOADER("api-ms-win-core-libraryloader-l1-2-2.dll"), | ||
| 165 | ); | ||