Skip to content

runtime: route LoadLibrary/GetProcAddress of bundled package DLLs through rt::Loader - #2021

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
jaybeebabageyus:rt-bundled-loadlibrary
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
jaybeebabageyus:rt-bundled-loadlibrary

Conversation

@jaybeebabageyus

Copy link
Copy Markdown
Contributor

Summary

Closes the gap noted in docs/runtime_imports.md ("Run-time loaded modules"): when a mapped title calls LoadLibraryA/W, LoadLibraryExA/W, GetModuleHandleA/W, GetProcAddress, or FreeLibrary on a bundled package DLL (such as Bink2Scarlett.dll, XboxBvhBuilder.dll, or the FidelityFX DLLs), runtime/shims/library_loader.cpp previously fell through to the host Windows loader, which cannot resolve console imports like xmem.dll or d3d12_xs.dll through the shim registry.

What changed

  • runtime/shims/shims.h & runtime/shims/registry.cpp: Added a BundledModuleOps hook registry (add_bundled_loader / remove_bundled_loader), mirroring add_module_lookup. Snapshots the registered loaders under an immortal mutex before calling out so recursive loads from DllMain cannot deadlock and late teardown remains safe.
  • runtime/src/pe_loader.h & runtime/src/pe_loader.cpp:
    • Guarded Loader module tables with a std::recursive_mutex and registered Loader with the bundled-module hook in Loader::Loader / unregistered in Loader::~Loader.
    • Added find_loaded_module, load_runtime_module (respects prefer_host_copy for CRTs, maps from game_dir_, runs initialise(), sets ERROR_DLL_INIT_FAILED on failure without leaking exceptions across WINAPI), resolve_module_export (handles direct exports, #ordinal, and forwarders to shims/bundled/host modules), and owns_module_handle.
  • runtime/shims/library_loader.cpp:
    • Routes non-datafile LoadLibrary* calls for bundled DLLs through load_bundled_module after checking shim-owned DLLs.
    • Routes GetModuleHandleA/W (for non-null names, leaving nullptr untouched) to find_loaded_bundled_module.
    • Routes GetProcAddress (by name or ordinal, including forwarders) and FreeLibrary for mapped module handles through rt::Loader.
  • End-to-end test (run_tiny_bundled_load): Added synthetic fixtures (xmem_stub, tiny_bundled_dll importing xmem.dll!XMemAlloc/XMemFree plus forwarded exports, and tiny_bundled_load exercising LoadLibraryW, LoadLibraryExA, GetModuleHandleA/W, GetProcAddress by name/ordinal/forwarder/missing/null, and FreeLibrary) and wired them into runtime/CMakeLists.txt via runtime/test/check_loaded_bundled.cmake.
  • docs/runtime_imports.md: Updated the "Run-time loaded modules" note to reflect bundled DLL run-time loading.

Verification

  • AI-assisted: Yes (implemented and verified with an AI coding assistant).
  • Environment: Windows 11 x64, MSVC 19.44.35230.0 (VS 2022 Build Tools), Windows SDK 10.0.26100.0, CMake 4.4.4 (RelWithDebInfo).
  • Tests run (all passed, 0 failures, no game data used):
    • ctest -C RelWithDebInfo -R "^(run_tiny_bundled_load|run_tiny_gdk_load|loader|pe_loader|xgameruntime)$" --output-on-failure — 5/5 passed (loader: 64/64 checks, xgameruntime: 34/34 checks, pe_loader, run_tiny_gdk_load, run_tiny_bundled_load).

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One LoadLibraryEx contract is currently silently changed. load_library_ex_a/w routes any bundled module whose flags are not the three data-file flags through load_bundled_module, but the hook drops dwFlags; Loader::load_runtime_module then calls initialise(). Thus DONT_RESOLVE_DLL_REFERENCES (0x1) still resolves dependencies and runs TLS/DllMain, while Win32 requires that flag not to load dependencies or call DllMain. Please carry the flags through and preserve the non-resolving behavior, or fail explicitly for this unsupported mode instead of silently doing a normal load. A synthetic check should distinguish this from the existing flags=0 case. Reference: https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa

@ezwinxdlmao

Copy link
Copy Markdown
Contributor

Tested 47f4cdc on Windows 11 with native MSVC (VS 2022 Build Tools, Ninja, RelWithDebInfo), GPU an NVIDIA GeForce RTX 3050 Laptop GPU with driver 32.0.16.2101: pass.

  • ctest -R "^(run_tiny_bundled_load|run_tiny_gdk_load|loader|pe_loader|xgameruntime)$": 5/5 pass.
  • Full ctest -j3: 112/114. The two failures are d3d12_compute (device_proxy_ref_count(device.p) == 1, line 515) and d3d12_draw (bundle blend pixel 127,0,127,255; TEST-266). Both fail identically at this PR's merge base 6a6704df on the same machine. Current main has since fixed d3d12_compute.

AI-assisted (Claude Code).

@jaybeebabageyus

Copy link
Copy Markdown
Contributor Author

Good catch, fixed in f4a3fe8: dwFlags is now propagated through BundledModuleOps::load_bundled / Loader::load_runtime_module, and DONT_RESOLVE_DLL_REFERENCES on a bundled DLL fails explicitly with ERROR_NOT_SUPPORTED without mapping, resolving imports, or running TLS/DllMain. Added a synthetic check in iny_bundled_load.cpp verifying this before the lags = 0 load (re-tested: xgameruntime,
un_tiny_gdk_load,
un_tiny_bundled_load all pass).

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested f4a3fe8d merged onto main 4eb6155 (clean merge) on Windows 11, MSVC Release: pass. ctest -R "bundled|tiny_gdk|loader|library|xgameruntime|run_tiny" gives 17/17 (one corpus test skipped as usual), including the new run_tiny_bundled_load, pe_loader_bounds, loader_tls, run_tiny_gdk_load and run_tiny_game_eh.

A concrete consumer in the package, from a string scan of the bundled DLLs (names only, nothing else read): amd_fidelityfx_loader_xbox.dll carries the ASCII names amd_fidelityfx_upscaler_xbox.dll and amd_fidelityfx_framegeneration_xbox.dll, the LoadLibraryA pattern. The upscaler ships in the package; the frame-generation DLL doesn't, so that load should fall through to "not found". Other run-time names in the package's DLLs:

module wide (LoadLibraryW-style) names
XCurl.dll xgameruntime.dll, gameplatformservices.dll
libHttpClient.GDK.dll xgameruntime.dll, winhttp.dll, ntdll.dll
msvcp140.dll api-ms-win-appmodel-runtime-l1-1-1.dll, appmodel.dll
msvcp140_atomic_wait.dll icu.dll
vccorlib140.dll gcombase.dll
vcomp140.dll vcomp120ui.dll

All of those are host or system modules, so they correctly stay with the host loader. I also checked the host-CRT rule: load_bundled_module returns early for prefer_host_copy names, so a run-time LoadLibrary("msvcp140.dll") keeps the host copy, as a direct import does. (#1910 fixes the forwarder path's version of the same rule.) 👍

AI-assisted review (Claude Code); runs on my machine.

@NRXM

NRXM commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 47f4cdc (on main 6a6704d; the head has since moved to f4a3fe8, not re-run) on an NVIDIA RTX 4090 (621.24) and an AMD Radeon iGPU (Ryzen 7000, RDNA2 gfx1036, driver 26.11.2 / Vulkan 2.0.353), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0, RelWithDebInfo: pass for this PR.

(AI-assisted test run.)

@jaybeebabageyus

Copy link
Copy Markdown
Contributor Author

Thanks — the RDNA2 iGPU in the mix is useful coverage. On the delta you flagged: f4a3fe8 (since 47f4cdc) only adds dwFlags propagation through BundledModuleOps::load_bundled and Loader::load_runtime_module — DONT_RESOLVE_DLL_REFERENCES on a bundled DLL now fails explicitly with ERROR_NOT_SUPPORTED before mapping, imports, TLS or DllMain — plus one synthetic check in tiny_bundled_load; nothing in the loader/pe_loader paths you ran, so your pass carries over. The two ctest failures you saw are the known main-side ones (d3d12_draw TEST-266 via #1995, d3d12_compute refcount via #1898), as you noted. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants