feat(runtime): redirect run-time loads of served DLLs to AnyBox's libraries #69

Manually merged
github merged 1 commit from gh-pr/69/head into gh-pr/69/base 2026-11-26 19:17:23 +00:00
Owner

@⁠Dark3581 on GitHub · 2026-11-23 04:36 UTC · original
Dark3581:feat/loader-redirect → main · +372 −3 · 10 files · 1 commits · merged 2026-11-23 04:39 UTC

What

The relinker only rewrites import tables, so a DLL a title loads by name at run time bypasses it. GDK modules do exactly that for the game runtime:

  • Two modules bundled with one title package (an HTTP client and a curl build) link the GDK's runtime thunks. Neither imports anything from xgameruntime.dll. Instead, a helper calls LoadLibraryExW(L"xgameruntime.dll", NULL, flags) with flags 0x800 (LOAD_LIBRARY_SEARCH_SYSTEM32) or 0xA00 (adding the application directory). It then calls GetProcAddress for QueryApiImpl, InitializeApiImpl, InitializeApiImplEx, InitializeApiImplEx2, XErrorReport and UninitializeApiImpl, and returns 0x89240101 if the load fails. (Measured with objdump on one module's helper; the call sites are in the comments below.)
  • On a PC with Gaming Services, C:\Windows\System32\xgameruntime.dll exists and exports exactly those six functions plus DllCanUnloadNow. So these modules would silently get the desktop runtime, not AnyBox's, and the title's runtime objects would be split between two implementations.

runtime/src/LoaderRedirect.cpp:

  • At start-up, ab_runtime points the import slots for LoadLibraryExW/ExA/W/A and GetModuleHandleW/A/ExW/ExA in title-side modules at its own functions. Title-side means not AnyBox's ab_* libraries and not modules under the Windows directory; slots are matched by the addresses kernel32 and kernelbase export.
  • Modules loaded later are patched from the loader's DLL notification (LdrRegisterDllNotification).
  • A requested name that matches a route in LibraryMapData.inc (the relinker's own table, generated from each library's Imports.txt) loads AnyBox's library beside ab_runtime.dll, whatever search flags the caller passed. Other names, data-file loads and address lookups pass through unchanged.
  • Each redirected name is logged once with the caller's module and offset: [anybox] loader: LoadLibraryExW(xgameruntime.dll) from title.exe+0x7c9f loads ab_xgameruntime.dll.

ARCHITECTURE and the game testing guide describe it, and the debt fragment lists what it does not cover: delay-loaded imports, GetProcAddress-reached loaders, direct LdrLoadDll, and loads from inside Windows modules.

This is the first half of supporting GDK titles' runtime calls. The second half, QueryApiImpl and the related entry points in ab_xgameruntime, follows in its own PR.

Tested

Windows 10, MinGW (GCC) and MSVC 2022. This PC has the desktop xgameruntime.dll in System32.

  • New e2e_dynamic_load: the relinked fake title calls:

    • LoadLibraryExW(L"xgameruntime.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32) and gets ab_xgameruntime.dll (checked by module path);
    • GetModuleHandleW(L"XGameRuntime.dll"), GetModuleHandleExA and LoadLibraryA("xgameruntime"), which return the same module;
    • GetProcAddress for a runtime export, which resolves;
    • LoadLibraryW(L"kernel32.dll"), which is unchanged.

    The log has one loader: line per call style.

  • MinGW ctest: 74 of 74. With MSVC, e2e_dynamic_load, stub, crash, async and task queue e2e pass, and every target builds except ab_gameinput, which needs gameinput.lib, which this machine doesn't have. python tools/check_conventions.py and debt_fold.py --check: ok.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • python tools/check_conventions.py passes (no comments in code, one declaration per export, Conventional Commit subjects)
  • Unimplemented paths stop (AbxNotImplemented / AbxUnsupported); silent stubs are listed under Silent stubs through a debt fragment
  • Known gaps added, changed or removed through docs/dev/debt/changes/<branch>.md
  • Generic behaviour, not specific to one title
  • No game files, keys, notes or logs added to the repository
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • Source of behaviour: measured in two bundled GDK modules (load helper and lookups, below); Win32 documentation for the loader functions and LdrRegisterDllNotification
  • AI-assisted: yes
**@⁠Dark3581** on GitHub · 2026-11-23 04:36 UTC · [original](https://github.com/Dark3581/AnyBox/pull/69) `Dark3581:feat/loader-redirect` → `main` · +372 −3 · 10 files · 1 commits · merged 2026-11-23 04:39 UTC ### What The relinker only rewrites import tables, so a DLL a title loads by name at run time bypasses it. GDK modules do exactly that for the game runtime: - Two modules bundled with one title package (an HTTP client and a curl build) link the GDK's runtime thunks. Neither imports anything from `xgameruntime.dll`. Instead, a helper calls `LoadLibraryExW(L"xgameruntime.dll", NULL, flags)` with flags `0x800` (`LOAD_LIBRARY_SEARCH_SYSTEM32`) or `0xA00` (adding the application directory). It then calls `GetProcAddress` for `QueryApiImpl`, `InitializeApiImpl`, `InitializeApiImplEx`, `InitializeApiImplEx2`, `XErrorReport` and `UninitializeApiImpl`, and returns `0x89240101` if the load fails. (Measured with `objdump` on one module's helper; the call sites are in the comments below.) - On a PC with Gaming Services, `C:\Windows\System32\xgameruntime.dll` exists and exports exactly those six functions plus `DllCanUnloadNow`. So these modules would silently get the desktop runtime, not AnyBox's, and the title's runtime objects would be split between two implementations. **`runtime/src/LoaderRedirect.cpp`:** - At start-up, `ab_runtime` points the import slots for `LoadLibraryExW/ExA/W/A` and `GetModuleHandleW/A/ExW/ExA` in title-side modules at its own functions. Title-side means not AnyBox's `ab_*` libraries and not modules under the Windows directory; slots are matched by the addresses `kernel32` and `kernelbase` export. - Modules loaded later are patched from the loader's DLL notification (`LdrRegisterDllNotification`). - A requested name that matches a route in `LibraryMapData.inc` (the relinker's own table, generated from each library's `Imports.txt`) loads AnyBox's library beside `ab_runtime.dll`, whatever search flags the caller passed. Other names, data-file loads and address lookups pass through unchanged. - Each redirected name is logged once with the caller's module and offset: `[anybox] loader: LoadLibraryExW(xgameruntime.dll) from title.exe+0x7c9f loads ab_xgameruntime.dll`. ARCHITECTURE and the game testing guide describe it, and the debt fragment lists what it does not cover: delay-loaded imports, `GetProcAddress`-reached loaders, direct `LdrLoadDll`, and loads from inside Windows modules. This is the first half of supporting GDK titles' runtime calls. The second half, `QueryApiImpl` and the related entry points in `ab_xgameruntime`, follows in its own PR. ### Tested Windows 10, MinGW (GCC) and MSVC 2022. This PC has the desktop `xgameruntime.dll` in System32. - New `e2e_dynamic_load`: the relinked fake title calls: - `LoadLibraryExW(L"xgameruntime.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)` and gets `ab_xgameruntime.dll` (checked by module path); - `GetModuleHandleW(L"XGameRuntime.dll")`, `GetModuleHandleExA` and `LoadLibraryA("xgameruntime")`, which return the same module; - `GetProcAddress` for a runtime export, which resolves; - `LoadLibraryW(L"kernel32.dll")`, which is unchanged. The log has one `loader:` line per call style. - MinGW `ctest`: 74 of 74. With MSVC, `e2e_dynamic_load`, stub, crash, async and task queue e2e pass, and every target builds except `ab_gameinput`, which needs `gameinput.lib`, which this machine doesn't have. `python tools/check_conventions.py` and `debt_fold.py --check`: ok. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] `python tools/check_conventions.py` passes (no comments in code, one declaration per export, Conventional Commit subjects) - [x] Unimplemented paths stop (`AbxNotImplemented` / `AbxUnsupported`); silent stubs are listed under Silent stubs through a debt fragment - [x] Known gaps added, changed or removed through `docs/dev/debt/changes/<branch>.md` - [x] Generic behaviour, not specific to one title - [x] No game files, keys, notes or logs added to the repository - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] Source of behaviour: measured in two bundled GDK modules (load helper and lookups, below); Win32 documentation for the loader functions and `LdrRegisterDllNotification` - [x] AI-assisted: yes
GDK modules load xgameruntime.dll by name with LoadLibraryExW and System32-only search, which bypasses the relinker and on a PC with Gaming Services loads the desktop runtime. The runtime now patches the loader import slots of title-side modules so any DLL name AnyBox serves loads its library, and logs each redirect with its caller.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:17:21 +00:00
github manually merged commit c8b0f4c754 into gh-pr/69/base 2026-11-26 19:17:23 +00:00
Author
Owner

@⁠Dark3581 on GitHub · 2026-11-23 04:36 UTC · original

Call sites behind the measurement (one bundled HTTP client module, image base 0x180000000):

  • load helper at +0x5d85c: mov r8d,0x800 or mov r8d,0xa00, xor edx,edx, call [LoadLibraryExW]; returns 0x89240101 when the module is null.
  • lookups at +0x5d3ee..+0x5d4ca: lea rdx,<name>; call [GetProcAddress] for QueryApiImpl, InitializeApiImpl, InitializeApiImplEx, InitializeApiImplEx2, XErrorReport, UninitializeApiImpl.
  • QueryApiImpl wrapper at +0x5e6d0: three arguments passed through (rcx, rdx, r8); 404 call sites pass two GUID pointers in rcx and rdx and an output pointer in r8.
  • InitializeApiImpl gets ecx=0x6336, edx=0x2712; InitializeApiImplEx/Ex2 get rcx=0x0000633600002712, rdx=0x000065f400001832, r8=flags (Ex2 also r9).
**@⁠Dark3581** on GitHub · 2026-11-23 04:36 UTC · [original](https://github.com/Dark3581/AnyBox/pull/69#issuecomment-7091714286) Call sites behind the measurement (one bundled HTTP client module, image base 0x180000000): - load helper at +0x5d85c: `mov r8d,0x800` or `mov r8d,0xa00`, `xor edx,edx`, `call [LoadLibraryExW]`; returns `0x89240101` when the module is null. - lookups at +0x5d3ee..+0x5d4ca: `lea rdx,<name>; call [GetProcAddress]` for QueryApiImpl, InitializeApiImpl, InitializeApiImplEx, InitializeApiImplEx2, XErrorReport, UninitializeApiImpl. - QueryApiImpl wrapper at +0x5e6d0: three arguments passed through (rcx, rdx, r8); 404 call sites pass two GUID pointers in rcx and rdx and an output pointer in r8. - InitializeApiImpl gets ecx=0x6336, edx=0x2712; InitializeApiImplEx/Ex2 get rcx=0x0000633600002712, rdx=0x000065f400001832, r8=flags (Ex2 also r9).
This discussion has been locked. Commenting is limited to contributors.
No description provided.