diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 01:46:33 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 01:46:33 +0100 |
| commit | bcb9f6d5f24363a2ea9fc1e18c7538d39ea4d622 (patch) | |
| tree | 229f22fcf89d9540426e230bab25dd2659017065 /runtime/shims/registry.cpp | |
| parent | 91b36cf86cb92a95836eed8af73abc0671a61e31 (diff) | |
runtime: LEONIDA_UNIMPLEMENTED=log|fail|stop policy for imports with no binding
An unimplemented import used to return 0 silently, which is S_OK for an
HRESULT function with its out parameters never written. The thunk now passes
the caller's return address and rcx/rdx/r8/r9 to a shared stub, and
LEONIDA_UNIMPLEMENTED picks the behaviour:
- log (default): exactly as before.
- fail: E_NOTIMPL for imports known to return HRESULT (D3D12Create*/Serialize*,
CreateDXGIFactory*, XGameRuntimeInitialize, and the GDK XAsync/XTaskQueue/
XUser/XStore/XGameSave/XPackage/XGameUi families minus their documented
void/bool/int functions), 0 otherwise.
- stop: print the import, the caller as module+RVA and the four argument
registers, then break under a debugger or exit with code 3.
--dry-run now counts unimplemented imports by return class. The stub zeroes
xmm0 and has unwind info, so float callers and debuggers are unaffected.
unimplemented_policy_test builds a synthetic image and checks all three modes.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/registry.cpp')
| -rw-r--r-- | runtime/shims/registry.cpp | 284 |
1 files changed, 268 insertions, 16 deletions
diff --git a/runtime/shims/registry.cpp b/runtime/shims/registry.cpp index e10ccb051..ac8e768b7 100644 --- a/runtime/shims/registry.cpp +++ b/runtime/shims/registry.cpp | |||
| @@ -1,11 +1,14 @@ | |||
| 1 | #include <windows.h> | 1 | #include <windows.h> |
| 2 | 2 | ||
| 3 | #include <atomic> | ||
| 4 | #include <cstdint> | ||
| 3 | #include <cstdio> | 5 | #include <cstdio> |
| 4 | #include <cstring> | 6 | #include <cstring> |
| 5 | #include <map> | 7 | #include <map> |
| 6 | #include <mutex> | 8 | #include <mutex> |
| 7 | #include <set> | 9 | #include <set> |
| 8 | #include <string> | 10 | #include <string> |
| 11 | #include <vector> | ||
| 9 | 12 | ||
| 10 | #include "shims.h" | 13 | #include "shims.h" |
| 11 | 14 | ||
| @@ -17,29 +20,275 @@ std::map<std::string, void*>& table() { | |||
| 17 | return t; | 20 | return t; |
| 18 | } | 21 | } |
| 19 | 22 | ||
| 23 | std::string lower_ascii(std::string s) { | ||
| 24 | for (auto& c : s) | ||
| 25 | if (c >= 'A' && c <= 'Z') c = static_cast<char>(c + 32); | ||
| 26 | return s; | ||
| 27 | } | ||
| 28 | |||
| 29 | bool starts_with(const std::string& s, const char* prefix) { return s.rfind(prefix, 0) == 0; } | ||
| 30 | |||
| 20 | std::mutex g_called_mutex; | 31 | std::mutex g_called_mutex; |
| 21 | std::set<std::string> g_called; | 32 | std::set<std::string> g_called; |
| 22 | 33 | ||
| 23 | // Called by the thunks with the import's "dll!name" string. | 34 | // ---- Return-value policy ---------------------------------------------------------------------------------------- |
| 24 | extern "C" uint64_t rt_unimplemented(const char* what) { | 35 | |
| 25 | std::lock_guard lock(g_called_mutex); | 36 | constexpr int kStopExitCode = 3; // what abort() exits with; documented, and checked by the tests |
| 26 | if (g_called.insert(what).second) std::fprintf(stderr, "[rt] unimplemented: %s\n", what); | 37 | |
| 27 | return 0; | 38 | std::atomic<int> g_policy{-1}; // -1: not decided yet, LEONIDA_UNIMPLEMENTED is read on first use |
| 39 | |||
| 40 | struct Lookup { | ||
| 41 | void* ctx; | ||
| 42 | ModuleLookup fn; | ||
| 43 | }; | ||
| 44 | std::mutex g_lookup_mutex; | ||
| 45 | std::vector<Lookup> g_lookups; | ||
| 46 | |||
| 47 | // The imports known to return HRESULT. Everything else is "other" and keeps returning 0 under `fail`. | ||
| 48 | // Each family was checked against the public Microsoft documentation (learn.microsoft.com), one signature per | ||
| 49 | // function; no SDK headers were read. An HRESULT function that is not listed just returns 0, as before, so a missing | ||
| 50 | // entry is the safe direction; a wrong entry would hand E_NOTIMPL (nonzero, "true") to a bool or pointer caller. | ||
| 51 | // | ||
| 52 | // d3d12.dll, d3d12_xs.dll D3D12Create*: D3D12CreateDevice, D3D12CreateRootSignatureDeserializer, | ||
| 53 | // D3D12CreateVersionedRootSignatureDeserializer return HRESULT | ||
| 54 | // (learn.microsoft.com/windows/win32/api/d3d12/nf-d3d12-d3d12createdevice and siblings). | ||
| 55 | // D3D12Serialize*: D3D12SerializeRootSignature, D3D12SerializeVersionedRootSignature: HRESULT. | ||
| 56 | // D3D12GetDebugInterface, D3D12EnableExperimentalFeatures: HRESULT. | ||
| 57 | // D3D12XboxCreateDevice is matched by exact name: the console-only function has no public | ||
| 58 | // reference page that could be reached; it is the factory this repo measured (it returns | ||
| 59 | // an HRESULT with the device in an out parameter, docs/d3d12_vulkan.md), not a documented one. | ||
| 60 | // dxgi.dll CreateDXGIFactory, CreateDXGIFactory1, CreateDXGIFactory2, DXGIGetDebugInterface1 and | ||
| 61 | // DXGIDeclareAdapterRemovalSupport return HRESULT (win32/api/dxgi/nf-dxgi-createdxgifactory, | ||
| 62 | // dxgi1_3 and dxgi1_6 pages). Not the whole DXGI* prefix: only these names were verified. | ||
| 63 | // xgameruntime.dll GDK reference, gaming/gdk/docs/reference/system/<family>/functions/<name>. These families | ||
| 64 | // return HRESULT except the functions in kNotHResult, which the same pages show as | ||
| 65 | // void/bool/int: XAsync*, XTaskQueue*, XUser*, XStore*, XGameSave*, XPackage*, XGameUi*. | ||
| 66 | // XGameRuntimeInitialize and XGameEventWrite: HRESULT. | ||
| 67 | // Mixed families (XSystem*, XGameStreaming*, XThread*, XDisplay*, XGameRuntimeFeature*) are | ||
| 68 | // left out on purpose. | ||
| 69 | const char* const kNotHResult[] = { | ||
| 70 | "XAsyncCancel", "XAsyncComplete", | ||
| 71 | "XTaskQueueCloseHandle", "XTaskQueueDispatch", "XTaskQueueGetCurrentProcessTaskQueue", | ||
| 72 | "XTaskQueueSetCurrentProcessTaskQueue", "XTaskQueueUnregisterMonitor", "XTaskQueueUnregisterWaiter", | ||
| 73 | "XUserCloseHandle", "XUserCloseSignOutDeferralHandle", "XUserCompare", "XUserIsStoreUser", | ||
| 74 | "XUserUnregisterForChangeEvent", "XUserUnregisterForDefaultAudioEndpointUtf16Changed", | ||
| 75 | "XUserUnregisterForDeviceAssociationChanged", | ||
| 76 | "XStoreCloseContextHandle", "XStoreCloseLicenseHandle", "XStoreCloseProductsQueryHandle", "XStoreIsLicenseValid", | ||
| 77 | "XStoreProductsQueryHasMorePages", "XStoreUnregisterGameLicenseChanged", "XStoreUnregisterPackageLicenseLost", | ||
| 78 | "XGameSaveCloseContainer", "XGameSaveCloseProvider", "XGameSaveCloseUpdate", | ||
| 79 | "XPackageCloseInstallationMonitorHandle", "XPackageCloseMountHandle", "XPackageGetInstallationProgress", | ||
| 80 | "XPackageIsPackagedProcess", "XPackageUninstallPackage", "XPackageUnregisterInstallationProgressChanged", | ||
| 81 | "XPackageUnregisterPackageInstalled", "XPackageUpdateInstallationMonitor", | ||
| 82 | }; | ||
| 83 | const char* const kXFamilies[] = {"XAsync", "XTaskQueue", "XUser", "XStore", "XGameSave", "XPackage", "XGameUi"}; | ||
| 84 | |||
| 85 | ReturnClass classify(const std::string& dll, const std::string& name) { | ||
| 86 | if (dll == "d3d12.dll" || dll == "d3d12_xs.dll") { | ||
| 87 | if (starts_with(name, "D3D12Create") || starts_with(name, "D3D12Serialize") || | ||
| 88 | name == "D3D12GetDebugInterface" || name == "D3D12EnableExperimentalFeatures" || | ||
| 89 | name == "D3D12XboxCreateDevice") | ||
| 90 | return ReturnClass::HResult; | ||
| 91 | } else if (dll == "dxgi.dll") { | ||
| 92 | if (starts_with(name, "CreateDXGIFactory") || name == "DXGIGetDebugInterface1" || | ||
| 93 | name == "DXGIDeclareAdapterRemovalSupport") | ||
| 94 | return ReturnClass::HResult; | ||
| 95 | } else if (dll == "xgameruntime.dll") { | ||
| 96 | if (name == "XGameRuntimeInitialize" || name == "XGameEventWrite") return ReturnClass::HResult; | ||
| 97 | for (const char* n : kNotHResult) | ||
| 98 | if (name == n) return ReturnClass::Other; | ||
| 99 | for (const char* f : kXFamilies) | ||
| 100 | if (starts_with(name, f)) return ReturnClass::HResult; | ||
| 101 | } | ||
| 102 | return ReturnClass::Other; | ||
| 103 | } | ||
| 104 | |||
| 105 | struct CallInfo { // what the common stub saved, in this order | ||
| 106 | uint64_t rcx, rdx, r8, r9, ret; | ||
| 107 | }; | ||
| 108 | |||
| 109 | // "0x00007ff6... = module+0x1234", resolved through the loaders' module lists, else through the host's modules. | ||
| 110 | std::string describe_address(uint64_t addr) { | ||
| 111 | char head[32], tail[64]; | ||
| 112 | std::snprintf(head, sizeof head, "0x%016llx", static_cast<unsigned long long>(addr)); | ||
| 113 | std::string module; | ||
| 114 | uint32_t rva = 0; | ||
| 115 | { | ||
| 116 | std::lock_guard lock(g_lookup_mutex); | ||
| 117 | for (auto& l : g_lookups) | ||
| 118 | if (l.fn(l.ctx, addr, module, rva)) { | ||
| 119 | std::snprintf(tail, sizeof tail, "+0x%x", rva); | ||
| 120 | return std::string(head) + " = " + module + tail; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | HMODULE host = nullptr; | ||
| 124 | if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | ||
| 125 | reinterpret_cast<LPCSTR>(addr), &host) && host) { | ||
| 126 | char path[MAX_PATH] = {}; | ||
| 127 | GetModuleFileNameA(host, path, MAX_PATH); | ||
| 128 | const char* slash = std::strrchr(path, '\\'); | ||
| 129 | std::snprintf(tail, sizeof tail, "+0x%llx", | ||
| 130 | static_cast<unsigned long long>(addr - reinterpret_cast<uint64_t>(host))); | ||
| 131 | return std::string(head) + " = host module " + (slash ? slash + 1 : path) + tail; | ||
| 132 | } | ||
| 133 | return std::string(head) + " = not inside any module"; | ||
| 134 | } | ||
| 135 | |||
| 136 | // Called by the common stub with the import's "dll!name" string and the registers it saved. The return value goes | ||
| 137 | // back to the game's caller in rax; the stub zeroes xmm0. | ||
| 138 | extern "C" uint64_t rt_unimplemented(const char* what, const CallInfo* info) { | ||
| 139 | const std::string full = what; | ||
| 140 | const auto bang = full.find('!'); | ||
| 141 | const std::string dll = bang == std::string::npos ? "" : full.substr(0, bang); | ||
| 142 | const ReturnClass cls = classify(lower_ascii(dll), bang == std::string::npos ? full : full.substr(bang + 1)); | ||
| 143 | const UnimplementedPolicy policy = unimplemented_policy(); | ||
| 144 | const uint64_t fail_value = cls == ReturnClass::HResult ? kENotImpl : 0; | ||
| 145 | |||
| 146 | if (policy == UnimplementedPolicy::Stop) { | ||
| 147 | std::fprintf(stderr, | ||
| 148 | "[rt] STOP: unimplemented import %s called\n" | ||
| 149 | "[rt] caller %s\n" | ||
| 150 | "[rt] rcx=0x%llx rdx=0x%llx r8=0x%llx r9=0x%llx\n" | ||
| 151 | "[rt] return class: %s (LEONIDA_UNIMPLEMENTED=fail returns %s)\n", | ||
| 152 | what, describe_address(info->ret).c_str(), static_cast<unsigned long long>(info->rcx), | ||
| 153 | static_cast<unsigned long long>(info->rdx), static_cast<unsigned long long>(info->r8), | ||
| 154 | static_cast<unsigned long long>(info->r9), cls == ReturnClass::HResult ? "hresult" : "other", | ||
| 155 | cls == ReturnClass::HResult ? "E_NOTIMPL" : "0"); | ||
| 156 | if (IsDebuggerPresent()) { | ||
| 157 | std::fprintf(stderr, "[rt] debugger attached: breaking; continuing returns the `fail` value\n"); | ||
| 158 | std::fflush(stderr); | ||
| 159 | __debugbreak(); | ||
| 160 | return fail_value; | ||
| 161 | } | ||
| 162 | std::fprintf(stderr, "[rt] no debugger attached: terminating the process (exit code %d)\n", kStopExitCode); | ||
| 163 | std::fflush(stderr); | ||
| 164 | TerminateProcess(GetCurrentProcess(), kStopExitCode); | ||
| 165 | return 0; // not reached | ||
| 166 | } | ||
| 167 | { | ||
| 168 | std::lock_guard lock(g_called_mutex); | ||
| 169 | if (g_called.insert(what).second) | ||
| 170 | std::fprintf(stderr, "[rt] unimplemented: %s%s\n", what, | ||
| 171 | policy == UnimplementedPolicy::Fail | ||
| 172 | ? (cls == ReturnClass::HResult ? " (returns E_NOTIMPL)" : " (returns 0)") | ||
| 173 | : ""); | ||
| 174 | } | ||
| 175 | return policy == UnimplementedPolicy::Fail ? fail_value : 0; | ||
| 28 | } | 176 | } |
| 29 | 177 | ||
| 30 | // Thunk layout (x86-64): | 178 | // Thunk layout (x86-64), one per import: |
| 31 | // 48 B9 <imm64> mov rcx, what | 179 | // 49 BA <imm64> mov r10, what ("dll!name"; r10 is volatile and carries no argument) |
| 32 | // 48 B8 <imm64> mov rax, rt_unimplemented | 180 | // 48 B8 <imm64> mov rax, common_stub |
| 33 | // 0F 57 C0 xorps xmm0, xmm0 | 181 | // FF E0 jmp rax |
| 34 | // FF E0 jmp rax (tail call: returns straight to the game's caller) | 182 | // The game's `call` pushed the return address, so at thunk entry [rsp] is the caller. rcx/rdx/r8/r9 are untouched. |
| 183 | // | ||
| 184 | // The common stub (one copy, made on first use) saves those registers and the return address, calls | ||
| 185 | // rt_unimplemented(what, &saved), and returns its rax straight to the game's caller with xmm0 zeroed (so a caller | ||
| 186 | // that expects a float or double result sees 0.0): | ||
| 187 | // sub rsp, 0x48 ; 8 (return address) + 0x48 leaves rsp 16-byte aligned at the call | ||
| 188 | // mov [rsp+0x20..0x38], rcx, rdx, r8, r9 ; the CallInfo, above the 32-byte shadow space | ||
| 189 | // mov rax, [rsp+0x48]; mov [rsp+0x40], rax ; the return address | ||
| 190 | // mov rcx, r10; lea rdx, [rsp+0x20]; mov rax, rt_unimplemented; call rax | ||
| 191 | // add rsp, 0x48; xorps xmm0, xmm0; ret | ||
| 192 | // An UNWIND_INFO (UWOP_ALLOC_SMALL 0x48) is registered for the stub, so a debugger stopped inside rt_unimplemented | ||
| 193 | // can walk up into the game. | ||
| 35 | constexpr size_t kThunkSize = 32; | 194 | constexpr size_t kThunkSize = 32; |
| 36 | 195 | ||
| 37 | uint8_t* g_pool = nullptr; | 196 | uint8_t* g_pool = nullptr; |
| 38 | size_t g_pool_used = 0, g_pool_size = 0; | 197 | size_t g_pool_used = 0, g_pool_size = 0; |
| 39 | std::mutex g_pool_mutex; | 198 | std::mutex g_pool_mutex; |
| 199 | void* g_stub = nullptr; // under g_pool_mutex | ||
| 200 | |||
| 201 | void* make_common_stub() { | ||
| 202 | constexpr size_t kUnwindOffset = 0x80, kFunctionOffset = 0x90, kRegionSize = 0x1000; | ||
| 203 | auto* region = | ||
| 204 | static_cast<uint8_t*>(VirtualAlloc(nullptr, kRegionSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); | ||
| 205 | if (!region) return nullptr; | ||
| 206 | static const uint8_t head[] = { | ||
| 207 | 0x48, 0x83, 0xEC, 0x48, // sub rsp, 0x48 | ||
| 208 | 0x48, 0x89, 0x4C, 0x24, 0x20, // mov [rsp+0x20], rcx | ||
| 209 | 0x48, 0x89, 0x54, 0x24, 0x28, // mov [rsp+0x28], rdx | ||
| 210 | 0x4C, 0x89, 0x44, 0x24, 0x30, // mov [rsp+0x30], r8 | ||
| 211 | 0x4C, 0x89, 0x4C, 0x24, 0x38, // mov [rsp+0x38], r9 | ||
| 212 | 0x48, 0x8B, 0x44, 0x24, 0x48, // mov rax, [rsp+0x48] | ||
| 213 | 0x48, 0x89, 0x44, 0x24, 0x40, // mov [rsp+0x40], rax | ||
| 214 | 0x4C, 0x89, 0xD1, // mov rcx, r10 | ||
| 215 | 0x48, 0x8D, 0x54, 0x24, 0x20, // lea rdx, [rsp+0x20] | ||
| 216 | 0x48, 0xB8, // mov rax, imm64 | ||
| 217 | }; | ||
| 218 | static const uint8_t tail[] = { | ||
| 219 | 0xFF, 0xD0, // call rax | ||
| 220 | 0x48, 0x83, 0xC4, 0x48, // add rsp, 0x48 | ||
| 221 | 0x0F, 0x57, 0xC0, // xorps xmm0, xmm0 | ||
| 222 | 0xC3, // ret | ||
| 223 | }; | ||
| 224 | static_assert(sizeof(head) + 8 + sizeof(tail) < kUnwindOffset); | ||
| 225 | const uint64_t fn = reinterpret_cast<uint64_t>(&rt_unimplemented); | ||
| 226 | size_t n = 0; | ||
| 227 | std::memcpy(region + n, head, sizeof head); | ||
| 228 | n += sizeof head; | ||
| 229 | std::memcpy(region + n, &fn, 8); | ||
| 230 | n += 8; | ||
| 231 | std::memcpy(region + n, tail, sizeof tail); | ||
| 232 | n += sizeof tail; | ||
| 233 | |||
| 234 | // UNWIND_INFO: version 1, no flags, prolog of 4 bytes, one code (offset 4, UWOP_ALLOC_SMALL, (0x48 - 8) / 8 = 8). | ||
| 235 | const uint8_t unwind[] = {0x01, 0x04, 0x01, 0x00, 0x04, 0x82, 0x00, 0x00}; | ||
| 236 | std::memcpy(region + kUnwindOffset, unwind, sizeof unwind); | ||
| 237 | auto* rf = reinterpret_cast<RUNTIME_FUNCTION*>(region + kFunctionOffset); | ||
| 238 | rf->BeginAddress = 0; | ||
| 239 | rf->EndAddress = static_cast<DWORD>(n); | ||
| 240 | rf->UnwindData = kUnwindOffset; | ||
| 241 | RtlAddFunctionTable(rf, 1, reinterpret_cast<DWORD64>(region)); | ||
| 242 | FlushInstructionCache(GetCurrentProcess(), region, kRegionSize); | ||
| 243 | return region; | ||
| 244 | } | ||
| 40 | 245 | ||
| 41 | } // namespace | 246 | } // namespace |
| 42 | 247 | ||
| 248 | UnimplementedPolicy parse_unimplemented_policy(const char* text, bool* valid) { | ||
| 249 | if (valid) *valid = true; | ||
| 250 | if (!text || !*text) return UnimplementedPolicy::Log; | ||
| 251 | const std::string v = lower_ascii(text); | ||
| 252 | if (v == "log") return UnimplementedPolicy::Log; | ||
| 253 | if (v == "fail") return UnimplementedPolicy::Fail; | ||
| 254 | if (v == "stop") return UnimplementedPolicy::Stop; | ||
| 255 | if (valid) *valid = false; | ||
| 256 | return UnimplementedPolicy::Log; | ||
| 257 | } | ||
| 258 | |||
| 259 | UnimplementedPolicy unimplemented_policy() { | ||
| 260 | int p = g_policy.load(); | ||
| 261 | if (p < 0) { | ||
| 262 | char buf[32] = {}; | ||
| 263 | const DWORD n = GetEnvironmentVariableA("LEONIDA_UNIMPLEMENTED", buf, sizeof buf); | ||
| 264 | bool valid = true; | ||
| 265 | const UnimplementedPolicy parsed = parse_unimplemented_policy(n < sizeof buf ? buf : "?", &valid); | ||
| 266 | if (!valid) std::fprintf(stderr, "[rt] LEONIDA_UNIMPLEMENTED: expected log, fail or stop; using log\n"); | ||
| 267 | int expected = -1; | ||
| 268 | g_policy.compare_exchange_strong(expected, static_cast<int>(parsed)); | ||
| 269 | p = g_policy.load(); | ||
| 270 | } | ||
| 271 | return static_cast<UnimplementedPolicy>(p); | ||
| 272 | } | ||
| 273 | |||
| 274 | void set_unimplemented_policy(UnimplementedPolicy p) { g_policy.store(static_cast<int>(p)); } | ||
| 275 | |||
| 276 | ReturnClass classify_import(const std::string& dll, const std::string& name) { return classify(lower_ascii(dll), name); } | ||
| 277 | |||
| 278 | void add_module_lookup(void* ctx, ModuleLookup fn) { | ||
| 279 | std::lock_guard lock(g_lookup_mutex); | ||
| 280 | g_lookups.push_back({ctx, fn}); | ||
| 281 | } | ||
| 282 | |||
| 283 | void remove_module_lookup(void* ctx) { | ||
| 284 | std::lock_guard lock(g_lookup_mutex); | ||
| 285 | for (auto it = g_lookups.begin(); it != g_lookups.end(); ++it) | ||
| 286 | if (it->ctx == ctx) { | ||
| 287 | g_lookups.erase(it); | ||
| 288 | break; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 43 | Table::Table(const Entry* entries, size_t count) { | 292 | Table::Table(const Entry* entries, size_t count) { |
| 44 | for (size_t i = 0; i < count; ++i) table()[std::string(entries[i].dll) + "!" + entries[i].name] = entries[i].fn; | 293 | for (size_t i = 0; i < count; ++i) table()[std::string(entries[i].dll) + "!" + entries[i].name] = entries[i].fn; |
| 45 | } | 294 | } |
| @@ -51,6 +300,8 @@ void* find(const std::string& dll_lower, const std::string& name) { | |||
| 51 | 300 | ||
| 52 | void* make_unimplemented_thunk(const std::string& dll, const std::string& name) { | 301 | void* make_unimplemented_thunk(const std::string& dll, const std::string& name) { |
| 53 | std::lock_guard lock(g_pool_mutex); | 302 | std::lock_guard lock(g_pool_mutex); |
| 303 | if (!g_stub) g_stub = make_common_stub(); | ||
| 304 | if (!g_stub) return nullptr; | ||
| 54 | if (!g_pool || g_pool_used + kThunkSize > g_pool_size) { | 305 | if (!g_pool || g_pool_used + kThunkSize > g_pool_size) { |
| 55 | g_pool_size = 1 << 20; | 306 | g_pool_size = 1 << 20; |
| 56 | g_pool = static_cast<uint8_t*>(VirtualAlloc(nullptr, g_pool_size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); | 307 | g_pool = static_cast<uint8_t*>(VirtualAlloc(nullptr, g_pool_size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE)); |
| @@ -60,14 +311,15 @@ void* make_unimplemented_thunk(const std::string& dll, const std::string& name) | |||
| 60 | const char* what = _strdup((dll + "!" + name).c_str()); // lives for the process | 311 | const char* what = _strdup((dll + "!" + name).c_str()); // lives for the process |
| 61 | uint8_t* t = g_pool + g_pool_used; | 312 | uint8_t* t = g_pool + g_pool_used; |
| 62 | g_pool_used += kThunkSize; | 313 | g_pool_used += kThunkSize; |
| 63 | uint64_t a = reinterpret_cast<uint64_t>(what), f = reinterpret_cast<uint64_t>(&rt_unimplemented); | 314 | const uint64_t a = reinterpret_cast<uint64_t>(what), f = reinterpret_cast<uint64_t>(g_stub); |
| 64 | uint8_t code[kThunkSize] = {0x48, 0xB9}; | 315 | uint8_t code[kThunkSize] = {0x49, 0xBA}; |
| 65 | std::memcpy(code + 2, &a, 8); | 316 | std::memcpy(code + 2, &a, 8); |
| 66 | code[10] = 0x48; code[11] = 0xB8; | 317 | code[10] = 0x48; |
| 318 | code[11] = 0xB8; | ||
| 67 | std::memcpy(code + 12, &f, 8); | 319 | std::memcpy(code + 12, &f, 8); |
| 68 | code[20] = 0x0F; code[21] = 0x57; code[22] = 0xC0; | 320 | code[20] = 0xFF; |
| 69 | code[23] = 0xFF; code[24] = 0xE0; | 321 | code[21] = 0xE0; |
| 70 | std::memset(code + 25, 0xCC, kThunkSize - 25); | 322 | std::memset(code + 22, 0xCC, kThunkSize - 22); |
| 71 | std::memcpy(t, code, kThunkSize); | 323 | std::memcpy(t, code, kThunkSize); |
| 72 | FlushInstructionCache(GetCurrentProcess(), t, kThunkSize); | 324 | FlushInstructionCache(GetCurrentProcess(), t, kThunkSize); |
| 73 | return t; | 325 | return t; |