Skip to content

Runtime: the mapped executable's module identity (GetModuleHandle/Ex, GetModuleFileName, GetProcAddress) - #2075

Open
dracbat wants to merge 4 commits into
ItsNotRexed:mainfrom
dracbat:rt-module-identity
Open

dracbat wants to merge 4 commits into
ItsNotRexed:mainfrom
dracbat:rt-module-identity

Conversation

@dracbat

@dracbat dracbat commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #1548 (comment) (new RT item)

Depends on: #1839, which itself depends on #1786. All three regenerate the same docs/runtime_imports.md table, so this branch has #1839's branch merged in (a merge, nothing rewritten). This PR's own work is dd107b39 plus the fixture and table adjustments in the merge.

What and why

Mapped code asking "which module am I" got gta6rt.exe's answers, or a failure for its own image (runtime/shims/library_loader.cpp):

  • GetModuleHandleA/W(NULL) passed the null name to Windows, giving gta6rt.exe where Windows gives the process's executable, the game.
  • GetModuleFileNameA/W wasn't shimmed. With NULL it named gta6rt.exe, and titles often derive their install folder from that. With a mapped image's HMODULE it failed, because Windows doesn't know VirtualAlloc'd images.
  • GetModuleHandleExA/W wasn't shimmed, so FROM_ADDRESS on an address inside a mapped image failed.
  • GetModuleHandle("<mapped DLL>") and GetProcAddress on a mapped image went to Windows and failed.

The retail package's DLLs import these from KERNEL32.dll and api-ms-win-core-libraryloader-l1-2-0.dll.

The change:

  • Answering for mapped images: GetModuleHandle*, GetModuleHandleEx*, GetModuleFileName* and GetProcAddress now answer for the images a live Loader maps, under all seven names library_loader.cpp already serves. They find a module by file name or full path, by base, and by any address inside it (FROM_ADDRESS).
    • Windows' name rules, return values and last errors are kept: .dll is added, a trailing . means no extension, GetModuleFileName truncates as Windows does, GetModuleHandleEx validates its arguments first, and GetProcAddress sets ERROR_PROC_NOT_FOUND, ERROR_INVALID_ORDINAL or ERROR_INVALID_PARAMETER. These were measured on Windows 11.
    • Owned modules keep their pseudo-handles.
  • The main executable: NULL is the EXE gta6rt --run runs (Loader::set_main_module, set before initialise()). Without one it stays Windows' answer, so --dry-run/--init behave as before.
  • Forwarders: GetProcAddress follows a mapped module's forwarders as imports do (Loader::export_address), but never maps a module while image code runs.
  • FreeLibrary on a mapped image succeeds and changes nothing.
  • Registry and Loader: a new registry hook (ModuleQuery/ModuleFind/ModuleProc in shims.h) asks each live Loader. The Loader is found under the registry lock and asked outside it, since a forwarder can wait on the Windows loader lock. Module::path is now made absolute at map time.
  • Docs: docs/runtime_imports.md, runtime/README.md, and the regenerated dry-run table.

Tested

Windows 11, AMD Radeon RX 5700 (Adrenalin 26.11.2), MSVC 14.44, Ninja RelWithDebInfo:

  • pe_loader_test (113 checks) covers each function: names in any case, the .dll rule, full and relative paths, FROM_ADDRESS on the first and last byte and one past, GetModuleHandleEx flag errors, GetModuleFileName truncation and ANSI, GetProcAddress by name, by ordinal and through a forwarder (new tiny_forwarded export in tiny_dll), and Windows' answers coming back once the Loader is gone.

  • tiny_game, under gta6rt --run, now checks that it sees itself:

    • GetModuleHandle(NULL) is its image;
    • GetModuleFileName(NULL) is its own file, checked by comparing its headers;
    • FROM_ADDRESS on its code and data finds its image.

    These checks pass in run_tiny_game* and also under the static CRT (run_tiny_game_static_eh from Runtime: RtlPcToFileHeader for a mapped image's own imports (static-CRT C++ exceptions) #1839).

  • Negative control: with main's library_loader.cpp, pe_loader, xgameruntime, run_tiny_gdk_load and all run_tiny_game* fail. The game is told its file is gta6rt.exe.

  • Full runtime ctest of this branch: 107/109 pass, 6 skipped. The two failures fail on main here too:

  • gta6rt --dry-run on the 16 package DLLs of my own 1.0.2.1 copy: every import binds, 0 unimplemented. The table holds the counts.

Not covered: LoadLibrary* of a mapped module's name still goes to Windows. Not run under Wine; the new checks are answered by the shims themselves.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (N/A: no translator change.)

🤖 Generated with Claude Code

dracbat and others added 4 commits November 22, 2026 14:28
Windows' RtlPcToFileHeader returns NULL for an image it didn't map. The loader
redirected only the host copies of the MSVC runtime, so a mapped image that
imports it itself, such as a static-CRT _CxxThrowException or a crash or
stack-walk helper, got NULL. A static-CRT C++ throw then records image base 0,
and its catch never matches.

Serve RtlPcToFileHeader under kernel32, kernelbase, ntdll and
api-ms-win-core-rtlsupport-l1-1-0/-l1-2-0. The package's XCurl.dll,
vcruntime140.dll and msvcp140.dll import it from the l1-1-0 set. Look the
Windows function up on first use when no host copy has been redirected yet.

pe_loader_test checks every name and the answers. tiny_game_static, the
stand-in game linked with the static CRT, runs its C++ exception check under
gta6rt. docs/runtime_imports.md: the dry-run table at this commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mapped code asking which module it is got gta6rt.exe's answers, or a failure
for its own image. GetModuleHandle(NULL) passed the null name to Windows, and
GetModuleFileName and GetModuleHandleEx weren't shimmed. So a title saw
gta6rt.exe as the process's executable (and took its install folder from it),
and couldn't find its own image by address or name, because Windows doesn't
know VirtualAlloc'd images.

GetModuleHandle*, GetModuleHandleEx*, GetModuleFileName* and GetProcAddress
now answer for the images a live Loader maps, by file name or full path, by
base, and by any address inside one (FROM_ADDRESS). They use Windows' name
rules, return values and last errors. NULL is the EXE that `gta6rt --run` runs
(Loader::set_main_module); without one it stays Windows' answer. GetProcAddress
follows a mapped module's forwarders as imports do, without mapping a module
while image code runs. FreeLibrary on a mapped image succeeds and changes
nothing. Owned modules keep their pseudo-handles.

pe_loader_test covers every function; tiny_game checks the answers under
gta6rt --run; tiny_dll gains a forwarded export.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rt-module-identity

Conflicts resolved: runtime/test/pe_loader_test.cpp (one shims.h include; test_pc_to_file_header and its call right after test_unwind, then test_module_identity and test_proc_address) and docs/runtime_imports.md (all three branches' paragraphs kept; the Shim/Host columns regenerated from this merge's gta6rt --dry-run). tiny_game.cpp's module-identity checks no longer assume the file name tiny_game.exe, which ItsNotRexed#1839's tiny_game_static (same source) doesn't have.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hanged it

The merge regenerated it, so "at the commit that added this paragraph" (ItsNotRexed#1839) no longer names the right commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

The new main-executable shortcut is applied to the wrong API. mapped_proc_address treats HMODULE == NULL as the mapped main module, and the new test requires GetProcAddress(NULL, name) to return its export. Win32 documents GetProcAddress's hModule as a module handle returned by LoadLibrary/LoadLibraryEx/LoadPackagedLibrary/GetModuleHandle; the executable shorthand is GetModuleHandle(NULL), whose returned HMODULE can then be passed to GetProcAddress. Please leave a null hModule to the host's error behavior and keep export lookup working through the non-null handle returned by GetModuleHandle(NULL). References: https://learn.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress and https://learn.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandlew

@ItsNotRexed

Copy link
Copy Markdown
Owner

This conflicts with main in runtime/src/pe_loader.cpp (and the shim registry/loader). The loader is safety-critical, so I won't resolve it by hand: please rebase on current main and push, and it'll be picked up in the next round.

@poke900310-lgtm

Copy link
Copy Markdown

For the rebase requested above, two things to watch, separate from the GetProcAddress(NULL) point above. Since this branched off 003e781, main has picked up 47f4cdc (the #2021 routing), and that breaks two assumptions this PR makes.

  1. Modules now get mapped while image code is running. On main, LoadLibrary of a bundled DLL goes to Loader::load_runtime_module, which runs load() + initialise() under the new Loader::mutex_ and pushes into order_ / by_name_. Here, find_in_loader / proc_in_loader (pe_loader.cpp:376-411) walk modules() with no lock, on the basis that "load() fills it before image code runs". resolve_forwarder(..., map_bundled=false) also reads by_name_ unlocked (:789-790). After a rebase, a GetModuleHandleEx(FROM_ADDRESS) / GetModuleFileName / GetProcAddress on one thread can race a LoadLibrary on another, and g_lookup_mutex doesn't help because it's a different lock. (Main's lookup_in_loader has the same unlocked walk, so one fix could cover all three.)
  2. Main already answers GetModuleHandle(name), GetProcAddress and FreeLibrary for mapped bundled modules through BundledModuleOps. Adding ModuleFind/ModuleProc alongside it means two hooks serve the same handles under different rules. One concrete case: a forwarder into a bundled DLL that isn't mapped yet. Main's resolve_module_export maps and initialises the target, but this PR's export_address returns NULL (map_bundled=false). Which result the game sees would depend on check order in the merged get_proc_address.

GitHub already shows conflicts in library_loader.cpp, registry.cpp, shims.h and pe_loader.cpp. I'd suggest building the new queries (by full path, by address, the main EXE, GetModuleFileName) on top of BundledModuleOps and mutex_, and keeping a single GetProcAddress path. The name matching rules, the GetModuleHandleEx argument checks and the GetModuleFileName truncation all look right to me.

AI-assisted review (Claude Code), read-only; I didn't build or run it.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

A note from #3667, which touches the neighbouring code: find_in_loader and proc_in_loader here read Loader::modules() without a lock, with the comment that load() fills the list before image code runs. A run-time LoadLibrary of a bundled DLL (load_runtime_module, merged in #2021) is the case where that does not hold: map_image appends to that vector while image code runs on other threads. For the existing reader, lookup_in_loader, a stress test of lookups during 300 loads crashes under AddressSanitizer at the vector read, 3 of 3 runs (details in #3667). #3667 moves that reader to a small table under its own leaf lock (taking Loader::mutex_ is not an option, it is held while image code runs). It does not touch this PR's two readers; whichever lands second can put them on the same table, which would need the path next to the name. Happy to do that on either side. AI-assisted (Claude Code).

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.

5 participants