upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index f4579ec27..8865e6ddc 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -9,6 +9,7 @@
9#include <cstdlib> 9#include <cstdlib>
10#include <cstring> 10#include <cstring>
11#include <fstream> 11#include <fstream>
12#include <mutex>
12#include <stdexcept> 13#include <stdexcept>
13#include <vector> 14#include <vector>
14 15
@@ -266,6 +267,59 @@ bool prefer_host_copy(const std::string& dll) {
266 return false; 267 return false;
267} 268}
268 269
270// MSVC C++ exceptions in a mapped image. _CxxThrowException (vcruntime140.dll) puts the throwing image's base in the
271// exception record, from RtlPcToFileHeader(pThrowInfo); the frame handler adds the ThrowInfo's RVAs (catchable types,
272// copy constructor, destructor) to it. RtlPcToFileHeader only knows images the Windows loader mapped, so for ours the
273// base is 0 and the exception can't be matched to a catch. The host copies the loader binds get their import of it
274// redirected: the Windows answer first, then the modules a live Loader has mapped (shims::mapped_image_base).
275using PcToFileHeaderFn = PVOID(NTAPI*)(PVOID, PVOID*);
276std::atomic<PcToFileHeaderFn> g_host_pc_to_file_header{nullptr};
277
278PVOID NTAPI pc_to_file_header(PVOID pc, PVOID* base) {
279 if (PVOID found = g_host_pc_to_file_header.load()(pc, base)) return found;
280 uint64_t mapped = 0;
281 if (!shims::mapped_image_base(reinterpret_cast<uint64_t>(pc), mapped)) {
282 *base = nullptr;
283 return nullptr;
284 }
285 *base = reinterpret_cast<PVOID>(mapped);
286 return *base;
287}
288
289// Points every import of RtlPcToFileHeader by name in a host module at pc_to_file_header, once per module. The
290// module is a system DLL the Windows loader has already validated and bound, so its tables are read unchecked.
291// The redirect stays for the process lifetime; with no live Loader it answers exactly as Windows does.
292void redirect_pc_to_file_header(HMODULE host) {
293 static std::mutex mutex;
294 static std::vector<HMODULE> done;
295 std::lock_guard lock(mutex);
296 if (std::find(done.begin(), done.end(), host) != done.end()) return;
297 done.push_back(host);
298
299 auto* base = reinterpret_cast<uint8_t*>(host);
300 auto* nt = at<IMAGE_NT_HEADERS64>(base, reinterpret_cast<IMAGE_DOS_HEADER*>(base)->e_lfanew);
301 const auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
302 if (!dir.Size || !dir.VirtualAddress) return;
303 for (auto* d = at<IMAGE_IMPORT_DESCRIPTOR>(base, dir.VirtualAddress); d->Name; ++d) {
304 if (!d->OriginalFirstThunk) continue; // no name table to match against
305 auto* names = at<IMAGE_THUNK_DATA64>(base, d->OriginalFirstThunk);
306 auto* iat = at<IMAGE_THUNK_DATA64>(base, d->FirstThunk);
307 for (; names->u1.AddressOfData; ++names, ++iat) {
308 if (IMAGE_SNAP_BY_ORDINAL64(names->u1.Ordinal)) continue;
309 auto* by_name = at<IMAGE_IMPORT_BY_NAME>(base, static_cast<uint32_t>(names->u1.AddressOfData));
310 if (std::strcmp(reinterpret_cast<const char*>(by_name->Name), "RtlPcToFileHeader") != 0) continue;
311 auto* slot = reinterpret_cast<void**>(&iat->u1.Function);
312 if (*slot == reinterpret_cast<void*>(&pc_to_file_header)) continue;
313 PcToFileHeaderFn expected = nullptr; // the first bound address is the Windows function
314 g_host_pc_to_file_header.compare_exchange_strong(expected, reinterpret_cast<PcToFileHeaderFn>(*slot));
315 DWORD old = 0;
316 if (!VirtualProtect(slot, sizeof(void*), PAGE_READWRITE, &old)) continue;
317 *slot = reinterpret_cast<void*>(&pc_to_file_header);
318 VirtualProtect(slot, sizeof(void*), old, &old);
319 }
320 }
321}
322
269} // namespace 323} // namespace
270 324
271namespace { 325namespace {
@@ -653,6 +707,7 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d
653 707
654 HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); 708 HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
655 if (!h) return nullptr; 709 if (!h) return nullptr;
710 if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h);
656 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); 711 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str());
657 if (!p) return nullptr; 712 if (!p) return nullptr;
658 binding = Binding::Host; 713 binding = Binding::Host;
@@ -685,6 +740,7 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name
685 } 740 }
686 // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. 741 // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search.
687 if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { 742 if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) {
743 if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it
688 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); 744 FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str());
689 if (p) { 745 if (p) {
690 m.imports.push_back({dll, sym, Binding::Host}); 746 m.imports.push_back({dll, sym, Binding::Host});