Skip to content

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

Open
piyonchu wants to merge 1 commit into
ProjectLeonida:mainfrom
piyonchu:pr/libloader-module-filename
Open

piyonchu wants to merge 1 commit into
ProjectLeonida:mainfrom
piyonchu:pr/libloader-module-filename

Conversation

@piyonchu

Copy link
Copy Markdown
Contributor

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).

@Gefik23

Gefik23 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tested bada1af 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.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Hi @piyonchu, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/shims/library_loader.cpp, runtime/shims/shims.h, runtime/src/pe_loader.cpp, runtime/src/pe_loader.h), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

@piyonchu

Copy link
Copy Markdown
Contributor Author

Rebased onto main (16f505304) at 05a2c5dff:

  • Resolved merge conflicts across runtime/CMakeLists.txt, runtime/shims/library_loader.cpp, runtime/shims/shims.h, runtime/src/pe_loader.cpp, and runtime/src/pe_loader.h against the newly merged LEONIDA_COMPAT_DIR / library_loader_names_test changes, keeping both compat_first/compat_module handling and the GetModuleHandleA/W(nullptr) + GetModuleFileNameA/W path intact without duplicating lookup logic.
  • Verified:
    • git diff --check cont/main..HEAD: clean
    • python -I scripts/check_no_game_data.py ...: PASS
    • python -I tests/test_text_encoding.py: Ran 2 tests, OK
    • python -I tests/test_ctest_registration.py: Ran 7 tests, OK
    • cmake --build runtime/build --config RelWithDebInfo --target gta6rt library_loader_test library_loader_names_test loader_test dll_thread_calls_test: build succeeded
    • ctest --test-dir runtime/build -C RelWithDebInfo -R "^(loader|dll_thread_calls|library_loader|library_loader_names)$" --output-on-failure: 4/4 passed (loader, dll_thread_calls, library_loader_names, library_loader)

@piyonchu
piyonchu force-pushed the pr/libloader-module-filename branch from bada1af to 05a2c5d Compare November 29, 2026 01:37
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 29, 2026
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.

3 participants