upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-21 17:38:46 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-21 17:38:46 -0500
commit4d010bf4bdd14b0558dc8aefa76ae71fadf6ac57 (patch)
treef127e1bf8e22b4d0fda3c00cd93766a27d53bac8 /runtime/src
parente28fe7281b5094bba2fb279f3b37e8e49b82184d (diff)
parent27d1b11b649dca7bb7627061320e5af1612bedef (diff)
Merge pull request #176 from Spaghetih
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/pe_loader.cpp41
-rw-r--r--runtime/src/pe_loader.h9
2 files changed, 49 insertions, 1 deletions
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp
index c04594c27..82357c859 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.
diff --git a/runtime/src/pe_loader.h b/runtime/src/pe_loader.h
index 9df630ae0..5f6d1b7a5 100644
--- a/runtime/src/pe_loader.h
+++ b/runtime/src/pe_loader.h
@@ -1,5 +1,5 @@
1// Native PE mapper for Xbox Series (x86-64) images. 1// Native PE mapper for Xbox Series (x86-64) images.
2// Maps an image, applies relocations and binds every import through the shim registry. 2// Maps an image, applies relocations, binds every import through the shim registry and registers its unwind table.
3// Mapping never runs code from the image. Loader::initialise() is the one call that does: it sets up the 3// Mapping never runs code from the image. Loader::initialise() is the one call that does: it sets up the
4// module's TLS (slot, template and callbacks) and calls a DLL's entry point as DllMain. 4// module's TLS (slot, template and callbacks) and calls a DLL's entry point as DllMain.
5#pragma once 5#pragma once
@@ -34,6 +34,12 @@ struct Module {
34 unsigned tls_slot = 0; 34 unsigned tls_slot = 0;
35 bool has_tls_slot = false; 35 bool has_tls_slot = false;
36 bool initialised = false; 36 bool initialised = false;
37
38 // The image's exception directory (.pdata), registered with RtlAddFunctionTable by load() so that exception
39 // dispatch and stack walks can unwind through its frames. Removed when the loader is destroyed.
40 void* function_table = nullptr;
41 uint32_t function_count = 0;
42
37 std::vector<ImportRecord> imports; 43 std::vector<ImportRecord> imports;
38 std::map<std::string, void*> exports; // by name, plus "#ordinal" 44 std::map<std::string, void*> exports; // by name, plus "#ordinal"
39 std::map<std::string, std::string> forwarders; // same keys, holding "module.symbol" for re-exports 45 std::map<std::string, std::string> forwarders; // same keys, holding "module.symbol" for re-exports
@@ -66,6 +72,7 @@ private:
66 void* resolve_forwarder(const std::string& spec, Binding& binding, int depth); 72 void* resolve_forwarder(const std::string& spec, Binding& binding, int depth);
67 void parse_exports(Module& m); 73 void parse_exports(Module& m);
68 void protect_sections(Module& m); 74 void protect_sections(Module& m);
75 void register_unwind(Module& m);
69 76
70 std::filesystem::path game_dir_; 77 std::filesystem::path game_dir_;
71 std::map<std::string, Module*> by_name_; // lower-case file name 78 std::map<std::string, Module*> by_name_; // lower-case file name