upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/library_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/shims/library_loader.cpp')
-rw-r--r--runtime/shims/library_loader.cpp165
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
24namespace 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.
28struct Pseudo {
29 char tag[8] = {'L', 'E', 'O', 'S', 'H', 'I', 'M', 0};
30 std::string dll; // lower case, with .dll
31};
32
33namespace {
34
35std::mutex g_mutex;
36std::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
41std::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
47std::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").
55std::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.
67HMODULE 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.
80const 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
87namespace {
88
89HMODULE WINAPI load_library_a(LPCSTR name) {
90 if (HMODULE h = name ? owned_handle(name) : nullptr) return h;
91 return LoadLibraryA(name);
92}
93
94HMODULE WINAPI load_library_w(LPCWSTR name) {
95 if (HMODULE h = name ? owned_handle(narrow(name)) : nullptr) return h;
96 return LoadLibraryW(name);
97}
98
99HMODULE 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
104HMODULE 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.
110HMODULE WINAPI get_module_handle_a(LPCSTR name) {
111 if (HMODULE h = name ? owned_handle(name) : nullptr) return h;
112 return GetModuleHandleA(name);
113}
114
115HMODULE 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
120FARPROC 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
137BOOL 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.
157SHIM_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);