library_loader: shim GetModuleFileNameA/W and GetModuleHandle(NULL) (RT-LIBLOADER-MODULE-FILENAME) #1461

Open
github-bot wants to merge 1 commit from github-bot/gh-1461 into main AGit
Owner

@⁠piyonchu on GitHub · 2026-11-25 00:41 UTC · original
piyonchu:pr/libloader-module-filename → main · +337 −19 · 7 files · 1 commits · open

Claimed in the coordination issue as: RT-LIBLOADER-MODULE-FILENAME

Summary

  • Measures from game_scarlett_master.exe call sites show both api-ms-win-core-libraryloader-l1-2-0.dll!GetModuleFileNameA and api-ms-win-core-libraryloader-l1-2-0.dll!GetModuleFileNameW called with hModule = NULL and nSize = MAX_PATH (0x104), with the wide caller turning 0 into HRESULT_FROM_WIN32(GetLastError()). Without shims, both return gta6rt.exe rather than the mapped executable, and calls for bundled DLL handles fail with ERROR_MOD_NOT_FOUND.
  • Record each mapped module's absolute path (full_path) and EXE flag (is_exe) in rt::Module / rt::Loader (runtime/src/pe_loader.h, runtime/src/pe_loader.cpp), exposed through optional callbacks on rt::shims::BundledModuleOps (runtime/shims/shims.h, runtime/shims/registry.cpp).
  • Shim GetModuleFileNameA, GetModuleFileNameW, and GetModuleHandleA/GetModuleHandleW(nullptr) in runtime/shims/library_loader.cpp for mapped executables, bundled DLLs, and owned shim pseudo-handles while matching Win32 buffer truncation (nSize == len, nSize == 0, ERROR_INSUFFICIENT_BUFFER / ERROR_SUCCESS) and preserving host fallback when no mapped EXE is active.
  • Add CTest target library_loader (runtime/test/library_loader_test.cpp) verifying mapped EXE nullptr/handle resolution across SetCurrentDirectory, DLL-only host fallback, bundled dependency handle resolution, pseudo-handle resolution, and truncation/size=0 parity against native ::GetModuleFileNameW.

Verification

  • Built with MSVC (RelWithDebInfo, Windows 11, CPU-only build without Vulkan headers) and ran ctest -C RelWithDebInfo -R "^(library_loader|dll_thread_calls|pe_loader|xgameruntime)$" --output-on-failure: 4/4 passed (pe_loader, dll_thread_calls, library_loader, xgameruntime).
  • Verified gta6rt.exe --dry-run against a local game_scarlett_master.exe confirms both api-ms-win-core-libraryloader-l1-2-0.dll!GetModuleFileNameA and GetModuleFileNameW now bind to shim.
  • python -I tests/test_build_runtime_fallback.py: 10 tests OK.
  • python -I tests/test_text_write_line_endings.py: 3 tests OK.
  • git diff --check: clean.

AI-assisted; synthetic tests + --dry-run import check on Windows 11 (CPU-only build; no game files included).

**@⁠piyonchu** on GitHub · 2026-11-25 00:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1461) `piyonchu:pr/libloader-module-filename` → `main` · +337 −19 · 7 files · 1 commits · open Claimed in the coordination issue as: RT-LIBLOADER-MODULE-FILENAME ### Summary - Measures from `game_scarlett_master.exe` call sites show both `api-ms-win-core-libraryloader-l1-2-0.dll!GetModuleFileNameA` and `api-ms-win-core-libraryloader-l1-2-0.dll!GetModuleFileNameW` called with `hModule = NULL` and `nSize = MAX_PATH` (`0x104`), with the wide caller turning `0` into `HRESULT_FROM_WIN32(GetLastError())`. Without shims, both return `gta6rt.exe` rather than the mapped executable, and calls for bundled DLL handles fail with `ERROR_MOD_NOT_FOUND`. - Record each mapped module's absolute path (`full_path`) and EXE flag (`is_exe`) in `rt::Module` / `rt::Loader` (`runtime/src/pe_loader.h`, `runtime/src/pe_loader.cpp`), exposed through optional callbacks on `rt::shims::BundledModuleOps` (`runtime/shims/shims.h`, `runtime/shims/registry.cpp`). - Shim `GetModuleFileNameA`, `GetModuleFileNameW`, and `GetModuleHandleA`/`GetModuleHandleW(nullptr)` in `runtime/shims/library_loader.cpp` for mapped executables, bundled DLLs, and owned shim pseudo-handles while matching Win32 buffer truncation (`nSize == len`, `nSize == 0`, `ERROR_INSUFFICIENT_BUFFER` / `ERROR_SUCCESS`) and preserving host fallback when no mapped EXE is active. - Add CTest target `library_loader` (`runtime/test/library_loader_test.cpp`) verifying mapped EXE `nullptr`/handle resolution across `SetCurrentDirectory`, DLL-only host fallback, bundled dependency handle resolution, pseudo-handle resolution, and truncation/size=0 parity against native `::GetModuleFileNameW`. ### Verification - Built with MSVC (`RelWithDebInfo`, Windows 11, CPU-only build without Vulkan headers) and ran `ctest -C RelWithDebInfo -R "^(library_loader|dll_thread_calls|pe_loader|xgameruntime)$" --output-on-failure`: **4/4 passed** (`pe_loader`, `dll_thread_calls`, `library_loader`, `xgameruntime`). - Verified `gta6rt.exe --dry-run` against a local `game_scarlett_master.exe` confirms both `api-ms-win-core-libraryloader-l1-2-0.dll!GetModuleFileNameA` and `GetModuleFileNameW` now bind to `shim`. - `python -I tests/test_build_runtime_fallback.py`: **10 tests OK**. - `python -I tests/test_text_write_line_endings.py`: **3 tests OK**. - `git diff --check`: clean. *AI-assisted; synthetic tests + `--dry-run` import check on Windows 11 (CPU-only build; no game files included).*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:52:15 +00:00
Author
Owner

@⁠Gefik23 on GitHub · 2026-11-25 07:04 UTC · original

Tested bada1af60 on NVIDIA GeForce RTX 4060, driver 617.42, AMD Ryzen 5 7500F, Windows 11 Pro build 26200, native Windows: pass.

Built at this head with scripts\build_runtime.ps1 (MSVC 19.44, VS 2022 Build Tools, Ninja, RelWithDebInfo, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0).

  • ctest -R library_loader -V: passed.
  • Full ctest -C RelWithDebInfo: 309/309 passed (game-data tests skipped, LEONIDA_GAME_DIR unset).

AI-assisted (Claude Code ran the build and tests). No game files.

**@⁠Gefik23** on GitHub · 2026-11-25 07:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1461#issuecomment-7155454679) Tested bada1af60 on NVIDIA GeForce RTX 4060, driver 617.42, AMD Ryzen 5 7500F, Windows 11 Pro build 26200, native Windows: pass. Built at this head with `scripts\build_runtime.ps1` (MSVC 19.44, VS 2022 Build Tools, Ninja, RelWithDebInfo, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0). - `ctest -R library_loader -V`: passed. - Full `ctest -C RelWithDebInfo`: 309/309 passed (game-data tests skipped, LEONIDA_GAME_DIR unset). AI-assisted (Claude Code ran the build and tests). No game files.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1461/head:github-bot/gh-1461'
git switch 'github-bot/gh-1461'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1461'
git switch 'github-bot/gh-1461'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1461'
git switch 'github-bot/gh-1461'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1461'
git switch 'main'
git merge --squash 'github-bot/gh-1461'
git switch 'main'
git merge --ff-only 'github-bot/gh-1461'
git switch 'main'
git merge 'github-bot/gh-1461'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.