diff options
| author | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 09:17:06 -0700 |
|---|---|---|
| committer | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 16:40:52 -0700 |
| commit | bb052d8575b163974128e98d8325b16e41844e5e (patch) | |
| tree | f96506182bec24666dc354a180dc30674af073ba | |
| parent | 75b2ea486c876b44b8ad7e9cacd9af95e045e989 (diff) | |
loader: don't bind shim-owned imports to host namesakes; document import routing
| -rw-r--r-- | docs/README.md | 1 | ||||
| -rw-r--r-- | docs/import_routing.md | 77 | ||||
| -rw-r--r-- | runtime/src/pe_loader.cpp | 41 |
3 files changed, 104 insertions, 15 deletions
diff --git a/docs/README.md b/docs/README.md index 2998f8191..545f3ba37 100644 --- a/docs/README.md +++ b/docs/README.md | |||
| @@ -66,6 +66,7 @@ method, scope and caveats. No game data is included. | |||
| 66 | | [d3d12_command_objects.md](d3d12_command_objects.md) | Measured command queue/list/fence/`PresentX` surface; verified vs prior art vs open. | | 66 | | [d3d12_command_objects.md](d3d12_command_objects.md) | Measured command queue/list/fence/`PresentX` surface; verified vs prior art vs open. | |
| 67 | | [runtime_imports.md](runtime_imports.md) | The 16 shipped modules' measured imports and shim/host/unimplemented split. | | 67 | | [runtime_imports.md](runtime_imports.md) | The 16 shipped modules' measured imports and shim/host/unimplemented split. | |
| 68 | | [console_libraries.md](console_libraries.md) | The console-DLL surface a title can import (`d3d12_xs`, `xgameruntime`, `xmem`, `gameinput`, `xaudio2_9`, `dstorage_xs`, `xg_xs`, `AcpHal`, `chad`/`xapu`/`xcrdapi`): which are shimmed, classified or unbound. | | 68 | | [console_libraries.md](console_libraries.md) | The console-DLL surface a title can import (`d3d12_xs`, `xgameruntime`, `xmem`, `gameinput`, `xaudio2_9`, `dstorage_xs`, `xg_xs`, `AcpHal`, `chad`/`xapu`/`xcrdapi`): which are shimmed, classified or unbound. | |
| 69 | | [import_routing.md](import_routing.md) | The bind order (shim → bundled → host → unimplemented), `owns_dll` and the pseudo-handle path, per-DLL outcome. | | ||
| 69 | | [gameinput.md](gameinput.md) | The GameInput shim on SDL3: `GameInputCreate` and the v0 `IGameInput`/`IGameInputDevice`/`IGameInputReading`/`IGameInputDispatcher` surface on any SDL gamepad, DualSense adaptive-trigger approximation and lightbar, force-feedback motors and trigger haptics through SDL's haptic API; 261 checks on virtual joysticks and a fake haptic backend, not yet tried on a real pad. | | 70 | | [gameinput.md](gameinput.md) | The GameInput shim on SDL3: `GameInputCreate` and the v0 `IGameInput`/`IGameInputDevice`/`IGameInputReading`/`IGameInputDispatcher` surface on any SDL gamepad, DualSense adaptive-trigger approximation and lightbar, force-feedback motors and trigger haptics through SDL's haptic API; 261 checks on virtual joysticks and a fake haptic backend, not yet tried on a real pad. | |
| 70 | | [saves.md](saves.md) | Save containers: `JSAV` (index + JSON story save, byte-exact rebuild), `ASAV` header, `SLAYER1` packed copy (not decoded); `tools/saves.py` and import into the offline `XGameSave` folder; key hashes are case-sensitive joaat (`tools/save_keys.py`). | | 71 | | [saves.md](saves.md) | Save containers: `JSAV` (index + JSON story save, byte-exact rebuild), `ASAV` header, `SLAYER1` packed copy (not decoded); `tools/saves.py` and import into the offline `XGameSave` folder; key hashes are case-sensitive joaat (`tools/save_keys.py`). | |
| 71 | | [pipeline_check.md](pipeline_check.md) | Driver-level corpus check: 10/12 shards, 22,092 modules, 2,217 compute pipelines, 0 failures. | | 72 | | [pipeline_check.md](pipeline_check.md) | Driver-level corpus check: 10/12 shards, 22,092 modules, 2,217 compute pipelines, 0 failures. | |
diff --git a/docs/import_routing.md b/docs/import_routing.md new file mode 100644 index 000000000..b4aa5c560 --- /dev/null +++ b/docs/import_routing.md | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | # Import routing: shim, bundled, host or unimplemented | ||
| 2 | |||
| 3 | How `gta6rt` binds an import, per DLL. The code paths are `Loader::resolve` for the import table | ||
| 4 | (`runtime/src/pe_loader.cpp`), `Loader::resolve_forwarder` for a forwarded export, and the `LoadLibrary*` / | ||
| 5 | `GetProcAddress` shims for run-time loads (`runtime/shims/library_loader.cpp`). Measured against the 16 shipped | ||
| 6 | modules in [runtime_imports.md](runtime_imports.md): every import binds, 0 unimplemented. | ||
| 7 | |||
| 8 | ## The bind order | ||
| 9 | |||
| 10 | `resolve(dll, name)` tries, in order: | ||
| 11 | |||
| 12 | 1. **Shim** — `rt::shims::find("dll!name")`, the clean-room tables registered by `SHIM_TABLE` in | ||
| 13 | `runtime/shims/*.cpp`. The registry is searched first even for host DLLs, which is how `kernel32!CreateFileW` | ||
| 14 | or `ExitProcess` get redirected without losing the rest of `kernel32`. | ||
| 15 | 2. **Bundled** — `<game-dir>/<dll>` mapped by the same loader, unless the name is in `prefer_host_copy` | ||
| 16 | (the `vcruntime140`/`msvcp140`/`concrt140`/`vccorlib140`/`vcomp140` family: the host CRT is always used, so a | ||
| 17 | mapped image's `RtlPcToFileHeader` can be redirected, `pe_loader.cpp`). A bundled export that is itself a | ||
| 18 | forwarder string is followed by `resolve_forwarder` (8 deep, then a loop is declared). | ||
| 19 | 3. **Host** — `LoadLibraryExA(dll, LOAD_LIBRARY_SEARCH_SYSTEM32)` + `GetProcAddress`. System32 only, so the game | ||
| 20 | folder is never in the search. API-set names (`api-ms-*`, `ext-ms-*`) resolve here through the host's API-set | ||
| 21 | schema; the measured run binds every api-ms import the package asks for. | ||
| 22 | 4. **Unimplemented** — `shims::make_unimplemented_thunk`: a 32-byte stub per import that calls `rt_unimplemented` | ||
| 23 | with `"dll!name"` and the argument registers; `LEONIDA_UNIMPLEMENTED` = log (default) / fail / stop picks the | ||
| 24 | answer (`runtime/shims/registry.cpp`, policy documented in `runtime/README.md` and | ||
| 25 | [runtime_imports.md](runtime_imports.md#when-an-unimplemented-import-is-called)). | ||
| 26 | |||
| 27 | Step 3 is skipped when `shims::owns_dll(dll)` is true. `xgameruntime.dll` and `gameinput.dll` are real PC names | ||
| 28 | (Gaming Services installs `xgameruntime.dll` in System32); without the skip, an import the registry doesn't serve | ||
| 29 | would bind to the live service runtime while a run-time `GetProcAddress` for the same name returns NULL, and the | ||
| 30 | process would be online-capable. `resolve_forwarder` applies the same order, so a forwarder string naming a | ||
| 31 | shimmed module resolves to the shim. | ||
| 32 | |||
| 33 | ## `owns_dll`: whole-module shims | ||
| 34 | |||
| 35 | `owns_dll` (registry.cpp) is false for `api-ms-*`/`ext-ms-*` prefixes and for the eight host DLLs that the tables | ||
| 36 | partially override — `kernel32`, `kernelbase`, `msvcrt`, `ucrtbase`, `ucrtbased`, `vulkan-1`, `ntdll`, `user32` — | ||
| 37 | and otherwise true exactly when the registry holds at least one `dll!` entry. Owned today: `d3d12_xs.dll`, | ||
| 38 | `xgameruntime.dll`, `gameinput.dll`, `xmem.dll`. | ||
| 39 | |||
| 40 | A title's `LoadLibrary*`/`GetModuleHandle*` for an owned module returns a pseudo-handle | ||
| 41 | (`library_loader.cpp`); `GetProcAddress` on it resolves through the registry only — an unserved name is NULL with | ||
| 42 | `ERROR_PROC_NOT_FOUND`, never the host. `FreeLibrary` on a pseudo-handle is a no-op. Every other module goes to | ||
| 43 | the real `LoadLibrary*`/`GetProcAddress`. | ||
| 44 | |||
| 45 | ## Per-DLL outcome | ||
| 46 | |||
| 47 | | DLL | Registry entries | Static import | Run-time load | | ||
| 48 | |---|---|---|---| | ||
| 49 | | `d3d12_xs.dll` | 4: `D3D12XboxCreateDevice`, `D3D12Serialize{,Versioned}RootSignature`, `D3D12CreateRootSignatureDeserializer` | those → shim; any other → unimplemented (no host namesake) | owned → pseudo-handle | | ||
| 50 | | `xgameruntime.dll` | ~40: `*ApiImpl*` entry points, `XGameRuntime*`, `XSystem*`, `XPackage*`, `XTaskQueue*`, `XAsync*`, `GameInputCreate` | registered → shim; unregistered → unimplemented | owned → pseudo-handle (the path `xgameruntime.lib` thunks use) | | ||
| 51 | | `gameinput.dll` | `GameInputCreate` | registered → shim; unregistered → unimplemented | owned → pseudo-handle | | ||
| 52 | | `xmem.dll` | `XMemAlloc`, `XMemFree`, `XMemVirtualQuery`, `XMemGetWorkingSetStatistics` | registered → shim; unregistered → unimplemented | owned → pseudo-handle | | ||
| 53 | | `d3d12.dll`, `dxgi.dll` | none | host (the real PC DLLs; the console surface rides on them through the `d3d12_xs` device proxy and the DXGI COM wrappers) | real host DLL | | ||
| 54 | | `xaudio2_9.dll`, `xaudio2_9redist.dll` | none | host if installed, else unimplemented | real host DLL | | ||
| 55 | | `dstorage.dll`, `dstorage_xs.dll` | none | `dstorage.dll` host if the DirectStorage redist is installed, else unimplemented; `dstorage_xs.dll` unimplemented | real host DLL / fails | | ||
| 56 | | `xg_xs.dll`, `acphal.dll`, `chad*`/`xapu*`/`xcrdapi*` (legacy Xbox names) | none | unimplemented | fails (host can't load them) | | ||
| 57 | | `kernel32`, `kernelbase` | file (`CreateFile*`, `FindFirstFile*`, `GetFileAttributes*`, `DeleteFile*`), loader (`LoadLibrary*`, `GetModuleHandle*`, `GetProcAddress`, `FreeLibrary`), process (`ExitProcess`, `GetCommandLine*`) | registered → shim; everything else → host | real host DLL (not owned) | | ||
| 58 | | `msvcrt`, `ucrtbase`, `ucrtbased` | exit/`abort`/argv/command-line overrides | registered → shim; everything else → host | real host DLL | | ||
| 59 | | `ntdll`, `user32`, `vulkan-1` | none | host | real host DLL | | ||
| 60 | | `api-ms-*`, `ext-ms-*` | only the sets a GDK title links: `core-file-l1-1-0/l1-2-0/l2-1-0`, `core-libraryloader-l1-1-0/l1-1-1/l1-2-0/l1-2-1/l1-2-2`, `core-processthreads-l1-1-0`, `core-processenvironment-l1-1-0`, `crt-runtime-l1-1-0` | registered names → shim; all other names and sets → host API-set schema (this is what covers every UCRT `api-ms-win-crt-*` variant without a table entry) | real host resolution | | ||
| 61 | | `vcruntime140*`, `msvcp140*`, `concrt140`, `vccorlib140`, `vcomp140` | none | host, even when a bundled copy exists (`prefer_host_copy`) | real host DLL | | ||
| 62 | | the package's own DLLs (`Bink2Scarlett.dll`, `XCurl.dll`, …) | none | bundled module, mapped by the loader | host `LoadLibrary` — see the caveat in [runtime_imports.md](runtime_imports.md#run-time-loaded-modules) | | ||
| 63 | | anything else | none | unimplemented | host or failure | | ||
| 64 | |||
| 65 | ## Edge cases | ||
| 66 | |||
| 67 | - **`d3d12.dll`/`dxgi.dll` can never hit the unimplemented thunk** for a name the PC DLL exports: no registry | ||
| 68 | entries exist for them, and they are real System32 DLLs. A name they don't export ends unimplemented, which is | ||
| 69 | the wanted answer. | ||
| 70 | - **Ordinals**: registry keys are `dll!#N` in `Entry.name`, but no shipped table registers an ordinal; an | ||
| 71 | ordinal-only import from a shimmed or console-only DLL lands unimplemented. Host DLLs bind ordinals through | ||
| 72 | `GetProcAddress` normally. | ||
| 73 | - **No console DLL matches the host list.** The `owns_dll` host list holds only names that also exist on the PC | ||
| 74 | and have partial table overrides. A console-only name (`d3d12_xs`, `xmem`, `xg_xs`, …) can't be owned by | ||
| 75 | accident and can't be excluded from the tables by accident. | ||
| 76 | - **Owned ≠ bundled-blocked.** A bundled copy of an owned name would still map through the bundled step before | ||
| 77 | the unimplemented fallback; nothing in the package ships one. | ||
diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index f4ecc0de2..77545d3f6 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp | |||
| @@ -1082,14 +1082,20 @@ void* Loader::resolve_forwarder(const std::string& spec, Binding& binding, int d | |||
| 1082 | return nullptr; // the bundled module exists and does not export it; don't look for a host namesake | 1082 | return nullptr; // the bundled module exists and does not export it; don't look for a host namesake |
| 1083 | } | 1083 | } |
| 1084 | 1084 | ||
| 1085 | HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | 1085 | // A module the shims own (xgameruntime.dll, gameinput.dll, d3d12_xs.dll, xmem.dll) never binds to a host |
| 1086 | if (!h) return nullptr; | 1086 | // namesake: xgameruntime.dll and gameinput.dll exist on the PC too, and a call into the real Gaming Services |
| 1087 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); | 1087 | // runtime would bypass the offline shims the same way a direct host load would (owns_dll, registry.cpp). |
| 1088 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); | 1088 | if (!shims::owns_dll(shim_key)) { |
| 1089 | if (!p) return nullptr; | 1089 | HMODULE h = LoadLibraryExA(module.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 1090 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p)); | 1090 | if (!h) return nullptr; |
| 1091 | binding = Binding::Host; | 1091 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header(h); |
| 1092 | return reinterpret_cast<void*>(p); | 1092 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(static_cast<WORD>(ordinal)) : symbol.c_str()); |
| 1093 | if (!p) return nullptr; | ||
| 1094 | if (prefer_host_copy(shim_key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p)); | ||
| 1095 | binding = Binding::Host; | ||
| 1096 | return reinterpret_cast<void*>(p); | ||
| 1097 | } | ||
| 1098 | return nullptr; | ||
| 1093 | } | 1099 | } |
| 1094 | 1100 | ||
| 1095 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { | 1101 | void* Loader::resolve(Module& m, const std::string& dll, const std::string& name, uint16_t ordinal, bool by_ordinal) { |
| @@ -1117,13 +1123,18 @@ void* Loader::resolve(Module& m, const std::string& dll, const std::string& name | |||
| 1117 | } | 1123 | } |
| 1118 | } | 1124 | } |
| 1119 | // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. | 1125 | // Host Windows: system DLLs and API sets. LOAD_LIBRARY_SEARCH_SYSTEM32 keeps the game folder out of the search. |
| 1120 | if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { | 1126 | // A shim-owned module is skipped: xgameruntime.dll and gameinput.dll have real PC namesakes in System32, and an |
| 1121 | if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it | 1127 | // unregistered import must end unimplemented, exactly as GetProcAddress on the pseudo-handle returns NULL |
| 1122 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); | 1128 | // (library_loader.cpp). Binding it to Gaming Services would also leave the process online-capable. |
| 1123 | if (p) { | 1129 | if (!shims::owns_dll(key)) { |
| 1124 | if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p)); | 1130 | if (HMODULE h = LoadLibraryExA(dll.c_str(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32)) { |
| 1125 | m.imports.push_back({dll, sym, Binding::Host}); | 1131 | if (prefer_host_copy(key)) redirect_pc_to_file_header(h); // before any mapped code can throw through it |
| 1126 | return reinterpret_cast<void*>(p); | 1132 | FARPROC p = GetProcAddress(h, by_ordinal ? MAKEINTRESOURCEA(ordinal) : name.c_str()); |
| 1133 | if (p) { | ||
| 1134 | if (prefer_host_copy(key)) redirect_pc_to_file_header_at(reinterpret_cast<const void*>(p)); | ||
| 1135 | m.imports.push_back({dll, sym, Binding::Host}); | ||
| 1136 | return reinterpret_cast<void*>(p); | ||
| 1137 | } | ||
| 1127 | } | 1138 | } |
| 1128 | } | 1139 | } |
| 1129 | m.imports.push_back({dll, sym, Binding::Unimplemented}); | 1140 | m.imports.push_back({dll, sym, Binding::Unimplemented}); |