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

Open
opened 2026-11-26 20:21:26 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1461
Author: @piyonchu
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: pr/libloader-module-filename


📝 Commits (1)

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

📊 Changes

7 files changed (+337 additions, -19 deletions)

View changed files

📝 runtime/CMakeLists.txt (+21 -0)
📝 runtime/shims/library_loader.cpp (+84 -19)
📝 runtime/shims/registry.cpp (+13 -0)
📝 runtime/shims/shims.h (+5 -0)
📝 runtime/src/pe_loader.cpp (+34 -0)
📝 runtime/src/pe_loader.h (+4 -0)
➕ runtime/test/library_loader_test.cpp (+176 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1461 **Author:** [@piyonchu](https://github.com/piyonchu) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/libloader-module-filename` --- ### 📝 Commits (1) - [`bada1af`](https://github.com/ubiquitarianism/leonida/commit/bada1af60c91cc6360ebd59d72bef134d0146e7d) library_loader: shim GetModuleFileNameA/W and GetModuleHandle(NULL) (RT-LIBLOADER-MODULE-FILENAME) ### 📊 Changes **7 files changed** (+337 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+21 -0) 📝 `runtime/shims/library_loader.cpp` (+84 -19) 📝 `runtime/shims/registry.cpp` (+13 -0) 📝 `runtime/shims/shims.h` (+5 -0) 📝 `runtime/src/pe_loader.cpp` (+34 -0) 📝 `runtime/src/pe_loader.h` (+4 -0) ➕ `runtime/test/library_loader_test.cpp` (+176 -0) </details> ### 📄 Description 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).* --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1490
No description provided.