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:
authorSpaghetih <191181391+Spaghetih@users.noreply.github.com>2026-11-21 21:47:59 +0100
committerSpaghetih <191181391+Spaghetih@users.noreply.github.com>2026-11-21 23:18:59 +0100
commit27d1b11b649dca7bb7627061320e5af1612bedef (patch)
treebea25811ebbd5f8f1c1be50decabdf40e68ab095 /runtime/src/pe_loader.cpp
parent36ace7135e897c228eede5d1d8c28e972285cb14 (diff)
Runtime: register each mapped image's x64 unwind table (.pdata) with RtlAddFunctionTable
Images the loader maps by hand are unknown to the system unwinder, so an exception raised in or passing through one couldn't be dispatched and killed the process. load() now validates the exception directory (entries in bounds, sorted, non-overlapping; zero-length entries allowed, as in System32's symcryptk.dll) and registers it; the loader removes it before freeing the image. Rules measured over 3,780 x64 System32 images (5,023,071 entries) and the 16 package DLLs. Tests: loader_test covers registration, removal and six corrupt tables on hand-built images; pe_loader_test checks a fixture SEH fault is caught by the image's own handler (fails without registration) and, with LEONIDA_GAME_DIR, every package DLL's table. AI-assisted.
Diffstat (limited to 'runtime/src/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index a0198c033..1e90c8438 100644
--- a/runtime/src/pe_loader.cpp
+++ b/runtime/src/pe_loader.cpp
@@ -56,6 +56,8 @@ bool prefer_host_copy(const std::string& dll) {
56 56
57Loader::~Loader() { 57Loader::~Loader() {
58 for (auto* m : order_) { 58 for (auto* m : order_) {
59 // Before the image is freed: a stale table would let a later stack walk read unmapped memory.
60 if (m->function_table) RtlDeleteFunctionTable(static_cast<PRUNTIME_FUNCTION>(m->function_table));
59 if (m->tls_block) VirtualFree(m->tls_block, 0, MEM_RELEASE); 61 if (m->tls_block) VirtualFree(m->tls_block, 0, MEM_RELEASE);
60 if (m->has_tls_slot) TlsFree(m->tls_slot); 62 if (m->has_tls_slot) TlsFree(m->tls_slot);
61 if (m->base) VirtualFree(m->base, 0, MEM_RELEASE); 63 if (m->base) VirtualFree(m->base, 0, MEM_RELEASE);
@@ -69,10 +71,49 @@ Module& Loader::load(const std::filesystem::path& image) {
69 Module& m = map_image(image); 71 Module& m = map_image(image);
70 bind_imports(m); // loads every bundled module m imports first, so they land in init_order_ before m 72 bind_imports(m); // loads every bundled module m imports first, so they land in init_order_ before m
71 protect_sections(m); 73 protect_sections(m);
74 register_unwind(m);
72 init_order_.push_back(&m); 75 init_order_.push_back(&m);
73 return m; 76 return m;
74} 77}
75 78
79// x64 has no frame-pointer chain to fall back on: without its .pdata registered, an exception raised in (or passing
80// through) a mapped image can't be dispatched past that frame, and the process dies. Images the system loader maps
81// are found through its own module list; ours are VirtualAlloc'd, so they are registered as dynamic function tables.
82//
83// The table is checked before it is handed over, because RtlLookupFunctionEntry trusts it: it binary-searches the
84// entries by BeginAddress and follows UnwindData without bounds checks. Measured over 3,780 x64 images in
85// System32 (5,023,071 entries) and the package's FidelityFX DLLs: every table is sorted and non-overlapping and in
86// bounds, and one shipped image (symcryptk.dll) has a zero-length entry (BeginAddress == EndAddress), so that is
87// allowed. No entry in that set uses the indirect form (UnwindData bit 0).
88void Loader::register_unwind(Module& m) {
89 auto* nt = at<IMAGE_NT_HEADERS64>(m.base, reinterpret_cast<IMAGE_DOS_HEADER*>(m.base)->e_lfanew);
90 if (nt->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXCEPTION) return;
91 auto& dir = nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION];
92 // Like the system loader, a trailing partial entry is ignored.
93 const uint32_t count = dir.Size / sizeof(RUNTIME_FUNCTION);
94 if (!count) return;
95 require_range(m.name, dir.VirtualAddress, static_cast<uint64_t>(count) * sizeof(RUNTIME_FUNCTION), m.size,
96 "the exception directory");
97 auto* table = at<RUNTIME_FUNCTION>(m.base, dir.VirtualAddress);
98 auto reject = [&](uint32_t i, const char* why) {
99 throw std::runtime_error(m.name + ": unwind entry " + std::to_string(i) + " " + why);
100 };
101 uint32_t previous_end = 0;
102 for (uint32_t i = 0; i < count; ++i) {
103 const RUNTIME_FUNCTION& f = table[i];
104 if (f.EndAddress < f.BeginAddress || f.EndAddress > m.size) reject(i, "covers code outside the image");
105 if (f.BeginAddress < previous_end) reject(i, "is out of order or overlaps the one before it");
106 // UNWIND_INFO starts with a 4-byte header; its codes and handler data are the unwinder's to read.
107 const uint32_t info = f.UnwindData & ~1u;
108 if (info > m.size || m.size - info < 4) reject(i, "has unwind data outside the image");
109 previous_end = f.EndAddress;
110 }
111 if (!RtlAddFunctionTable(table, count, reinterpret_cast<DWORD64>(m.base)))
112 throw std::runtime_error(m.name + ": RtlAddFunctionTable failed");
113 m.function_table = table;
114 m.function_count = count;
115}
116
76void Loader::initialise() { 117void Loader::initialise() {
77 // Not order_: map_image pushes a module there before bind_imports maps its dependencies, so order_ lists an 118 // Not order_: map_image pushes a module there before bind_imports maps its dependencies, so order_ lists an
78 // importer ahead of the DLLs it imports. init_order_ is filled once a module's imports are all loaded. 119 // importer ahead of the DLLs it imports. init_order_ is filled once a module's imports are all loaded.