Skip to content

Loader: resolve forwarders to shimmed modules through the shim registry - #191

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
dea7hsmiless:fix/forwarder-shim
Nov 21, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
dea7hsmiless:fix/forwarder-shim

Conversation

@dea7hsmiless

@dea7hsmiless dea7hsmiless commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Fixes #180.

Loader::resolve checks shims::find before bundled and host modules, but Loader::resolve_forwarder only searched game_dir and System32. A forwarder whose target is a shimmed module, such as xmem.XMemAlloc or d3d12_xs.D3D12XboxCreateDevice, therefore became an unimplemented thunk instead of binding the shim.

This normalizes the forwarder's target module to a shim key (append .dll when absent) and checks the shim registry first, matching the order a direct import already uses. A forwarder to a module with no shim still follows the existing bundled/host path.

Tested

Windows, Visual Studio 2022 BuildTools 17.14.60, MSVC 19.44.35229.0, CMake 4.4.4. Vulkan SDK absent; the GPU tools are not involved in this change. Rebased onto main at 4e390e0; head 8d8ebee.

cmake -S runtime -B runtime/build-review -G "Visual Studio 17 2022" -A x64 -DBUILD_TESTING=ON
cmake --build runtime/build-review --config Release --parallel
ctest --test-dir runtime/build-review -C Release --output-on-failure
  • ctest: 10 of 10 passed.
  • loader_test: 38 checks, 0 failed.
  • The new case is a forwarder to a test-local shim table (testshim.F), so the loader test does not depend on the real xmem shim. It checks the import binding is Shim and the IAT holds the shim function.
  • Negative control after the rebase: with only the resolve_forwarder lookup removed, the new case fails with the import did not bind as a shim; restoring it passes.
  • Conflict check: cherry-picked onto Runtime: install static TLS for mapped images #93's head d76accb; it applied cleanly and loader_test passed with 39 checks, 0 failed. Runtime: install static TLS for mapped images #93 and Runtime: register each mapped image's x64 unwind table (.pdata) #176 are now CONFLICTING with main; this PR is MERGEABLE.
  • No game files, keys, decryption or extracted data. Both PE fixtures are built byte by byte; the shim table is test-local.
  • CI has not started: this is a first-time fork PR and the workflows need maintainer approval. The results above are from the exact head 8d8ebee.

AI-assisted: yes. The change was written and tested by an AI agent; the reproduction and negative control were run locally.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • N/A: no translator rule changed, so nothing to add to the contract table in docs/design.md.

Coordination

Marked ready after the owner's comment on #180: it will be merged once out of draft and the Windows loader tests pass. #93 and #176 still change the same pe_loader.cpp and currently conflict with main; this change is in resolve_forwarder and adds one test, so it does not overlap their hunks. Happy to rebase again if main moves.

A forwarder target is resolved through game_dir and System32, but not through shims::find, so a forwarder to xmem or d3d12_xs became an unimplemented thunk. Check the normalized module name against the shim registry first, as a direct import already does. A synthetic shim table and a forwarder-to-shim test cover it.
@dea7hsmiless
dea7hsmiless marked this pull request as ready for review November 21, 2026 21:21
@dea7hsmiless

Copy link
Copy Markdown
Contributor Author

Rebased onto main 4e390e0 and marked ready per #180.

Windows, MSVC 19.44.35229.0:

  • ctest 10/10
  • loader_test 38 checks, 0 failed
  • Negative control after the rebase: removing only the shim lookup fails the new case; restoring it passes
  • Head 8d8ebee

CI has not started because this is a first-time fork PR and the workflows need maintainer approval.

@dea7hsmiless

Copy link
Copy Markdown
Contributor Author

Additional local check, not in the diff: I built a real MSVC DLL with /DEF exporting realFwd = KERNEL32.Sleep. A synthetic importer loaded through rt::Loader bound it as Host to kernel32!Sleep; loader_test passed 39 checks, 0 failed with the real DLL supplied through LEONIDA_REAL_FWD. That validates the export-directory forwarder detection against real linker output, in addition to the synthetic fixtures. No game data.

@dea7hsmiless

Copy link
Copy Markdown
Contributor Author

Conflict re-check after #93 was rebased onto main 4e390e0: cherry-picked 8d8ebee onto #93's new head d9c8dc5. It applied cleanly and loader_test passed 39 checks, 0 failed. No overlap with #93's hunks.

@dcastell1

Copy link
Copy Markdown
Contributor

Independent check at 8d8ebee on Windows 11 build 26300.9974, MSVC 14.44 (VS 2022 Build Tools), CMake Visual Studio generator, no Vulkan SDK. Built the runtime from the PR head with -DBUILD_TESTING=ON and no game files: ctest 10/10, loader_test 38 checks, 0 failed, including "a forwarder to a shimmed module binds to the shim". The forwarder-to-shim path and the negative forwarder cases behave as described.

@dea7hsmiless

Copy link
Copy Markdown
Contributor Author

End-to-end check with real Microsoft forwarders, not just the synthetic fixtures: copied kernel32.dll into a temp game dir and loaded a probe that imports kernel32!AddDllDirectory (forwarded to api-ms-win-core-libraryloader-l1-1-0.AddDllDirectory). The loader mapped the copied kernel32, followed the forwarder, and bound the probe import as Host: probe host=9 module=67 unimplemented=0. The copied kernel32 had one unrelated unimplemented optional import, ext-ms-win-oobe-query-l1-1-0!QueryOOBESupport. No game data.

@ItsNotRexed
ItsNotRexed merged commit e28fe72 into ItsNotRexed:main Nov 21, 2026
@dea7hsmiless

Copy link
Copy Markdown
Contributor Author

Post-merge verification on main 8bd0087 (which includes #191 and #176):

  • ctest: 12/12 passed
  • loader_test: 48 checks, 0 failed
  • pe_loader_test: 74 checks, 0 failed

No game files; clean MSVC Release build.

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.

PE loader: a forwarder to a shimmed module bypasses the shim registry

3 participants