Skip to content

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

Closed
dracbat wants to merge 13 commits into
ProjectLeonida:mainfrom
dracbat:rt-module-identity
Closed

dracbat wants to merge 13 commits into
ProjectLeonida: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 (merged). All of them regenerate the same docs/runtime_imports.md table, so this branch has #1839's branch merged in. It also has current main merged in (3faac55, after #2021 and #3667). Both are merges; nothing is rewritten.

What and why

When mapped code asks "which module am I", it gets gta6rt.exe's answers, or a failure for its own images (runtime/shims/library_loader.cpp). On main:

  • GetModuleHandleA/W(NULL) and GetProcAddress(NULL, name) go to Windows, which answers for the process's executable: gta6rt.exe, not the game.
  • GetModuleFileNameA/W isn't shimmed. With NULL it names gta6rt.exe, and titles often derive their install folder from that. With a mapped image's HMODULE it fails, because Windows doesn't know VirtualAlloc'd images.
  • GetModuleHandleExA/W isn't shimmed, so a name, or FROM_ADDRESS on an address inside a mapped image, fails.
  • GetModuleHandle(name) matches a mapped module by its file name whatever folder the name gives, and every GetProcAddress miss is ERROR_PROC_NOT_FOUND.

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

The change follows rules measured natively on Windows 11, and the tests check the shim against each of them:

  • NULL is the game. These answer for the EXE gta6rt --run runs (Loader::set_main_module, set before initialise()):

    • GetModuleHandle*(NULL);
    • GetModuleHandleEx* without a name;
    • GetModuleFileName*(NULL);
    • GetProcAddress(NULL, ...).

    Windows answers GetProcAddress(NULL, name) from the process's EXE: its own export, at the same address as through GetModuleHandle(NULL). So this follows Windows rather than the documented hModule contract (see the review thread). Without --run, all of these stay Windows' answers, so --dry-run and --init behave as before.

  • Names.

    • A mapped module is found by file name in any case.
    • .dll is added when the name has no extension, and trailing dots mean it has none.
    • A full path must be the module's own; /, . and .. are resolved.
    • A relative path with a separator (..\x.dll, .\x.dll) is looked for along the DLL search path, as Windows does (ntdll's LdrGetDllPath), not against the current folder. The first entry where the name exists decides.
  • Handles and addresses. GetModuleFileName* gives the full path the module was mapped from, truncating as Windows does (size - 1 characters, ERROR_INSUFFICIENT_BUFFER). GetModuleHandleEx* checks its arguments first, and answers FROM_ADDRESS for any byte of a mapped image.

  • GetProcAddress errors.

    • A name the module doesn't export gives ERROR_PROC_NOT_FOUND, and an ordinal gives ERROR_INVALID_ORDINAL.
    • A null name gives ERROR_INVALID_PARAMETER when the image has an export directory, and ERROR_INVALID_ORDINAL when it has none. Windows looks only at the export data directory's RVA.
    • A hit leaves the last error alone.
  • One path, main's locks.

    • The queries are one more BundledModuleOps entry, find_module.
    • GetProcAddress on a mapped module, NULL included, goes only through main's resolve_module_export. So a forwarder into a bundled DLL that isn't mapped yet maps and initialises it, as on main.
    • Names are looked up in by_name_ under Loader::mutex_, as main's find_loaded_module does. A base, an address and the main module come from loader: the address lookup reads its own table, not the module vector #3667's range table under its leaf lock, which now keeps each image's path.
    • Module::path is made absolute at map time.
  • Docs: docs/runtime_imports.md (the "Mapped modules and the process's executable" section, and the regenerated dry-run table) and runtime/README.md.

Open question for the maintainers: GetModuleFileName points into the package

GetModuleFileName(NULL), and GetModuleFileName on a bundled DLL's handle, now give mapped code a host path into the package folder. Before, they gave gta6rt.exe's path and a failure. runtime/shims/file_system.cpp sends any path that isn't GAME:, G: or T: straight to the host, and keeps a path under --game-dir a host path on purpose (console_drive).

So a title that writes beside its executable through that path would modify the installed package, which the overlay design promises never to do. That covers CreateFile for writing, DeleteFile, MoveFile, CreateDirectory and SetFileAttributes. Reads and FindFirstFile through the path also miss the overlay's copies, deletions and merged listing. This PR doesn't change that. The options I see:

  1. Return a console path for modules in the package, such as GAME:\<exe> or G:\<exe>. File access then goes through the overlay with no file-shim change. But anything that hands the path to an API that isn't shimmed (a host LoadLibrary, a shell call, a path library) gets a path the host doesn't understand.
  2. Keep the host path, and have file_system.cpp treat a path under --game-dir as the matching GAME: path. Writes then go to the overlay, reads prefer it, and listings merge, while the path stays usable everywhere else. The default overlay (<game-dir>\overlay) is inside --game-dir, so paths under it would have to stay as they are.
  3. Keep the host path, and refuse writes under --game-dir through host paths with ERROR_ACCESS_DENIED, as a read-only package would, leaving reads as they are.

Which would you prefer? I'm happy to do it here or in a follow-up.

Tested

Windows 11 (build 26300), AMD Radeon RX 5700 (driver 32.0.21045.29003), MSVC 14.44, Vulkan SDK 1.4.363.1, Ninja RelWithDebInfo:

  • The Windows rules, measured with native programs, not under gta6rt:

    • an EXE with an export, and one without;
    • DLLs with and without an export directory, plus patched copies (export directory size 0, RVA 0, no functions);
    • GetModuleHandleW with names without a path, full paths, and relative paths under different current folders, SetDllDirectory settings, and unloaded copies earlier in the search.

    An emulation of the search (ntdll's DLL path, then the first entry where the name exists) matched GetModuleHandleW in all 21 relative cases measured.

  • pe_loader_test (125 checks) covers each function:

    • names in any case, the .dll and trailing-dot rules, and full paths;
    • relative paths through the current folder, without it (SetDllDirectory("")), and through a SetDllDirectory folder;
    • FROM_ADDRESS on the first and last byte of an image and one past it;
    • GetModuleHandleEx flag errors, and GetModuleFileName truncation and ANSI;
    • GetProcAddress by name, by ordinal and through a forwarder, and the null-name error with and without an export directory;
    • GetProcAddress(NULL, ...) before, with and after a main module;
    • Windows' answers coming back once the Loader is gone.
  • loader_test (87 checks) adds test_identity_during_loads: lookups by file name, full path, base and main module on one thread during 300 loads on another. An AddressSanitizer build of the same stress crashed in the previous unlocked walk, and is clean now (1,000 lookups, 0 wrong).

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

    • GetModuleHandle(NULL) is its image;
    • GetModuleFileName(NULL) is its own file, compared by headers;
    • FROM_ADDRESS on its code and data finds its image;
    • GetProcAddress(NULL, name) is the same export as GetProcAddress(GetModuleHandle(NULL), name).

    These pass in all run_tiny_game* tests, including the static-CRT run_tiny_game_static_eh.

  • Negative controls:

    • With the NULL mapping in GetProcAddress removed, 2 pe_loader_test checks and 2 tiny_game checks fail.
    • With the previous revision's library_loader.cpp (current folder for relative paths, one trailing dot, ERROR_INVALID_PARAMETER for every null name, NULL to the host), 6 pe_loader_test checks and the same 2 tiny_game checks fail.
  • The PR's tests (pe_loader, loader, loader_tls, xgameruntime, and every run_tiny_* test, run_tiny_gdk_load and run_tiny_bundled_load included): 18 passed, 0 failed.

  • Full runtime ctest -j 2, without the three tests that open a window (d3d12_present, vk_presenter_sync, xbox_present): 167 passed, 9 skipped, 0 failed (176 tests).

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

Not covered:

  • Wine: not run under it. Wine implements LdrGetDllPath, and the new checks are answered by the shims themselves.
  • \\?\ paths: a \\?\-prefixed full path names a module in Windows, but the shim doesn't match it.
  • The search's application folder: it's gta6rt.exe's folder, as it is in this process. A native run of the game would start the search in the game's own folder.

AI-assisted: yes (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
Member

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.

@MrCheese5

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

dracbat and others added 3 commits November 23, 2026 12:15
The only conflict is docs/runtime_imports.md: main's table (with its new
--init column) and paragraphs are kept, plus this branch's
RtlPcToFileHeader paragraph. The table's counts are regenerated from the
dry-run in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tiny_game.cpp: main's usage line with [abort-mode] heads this branch's
description of the identity checks, and this branch's identity checks sit
next to main's abort handlers. docs/runtime_imports.md takes main's table
format (with the --init column); its counts are regenerated from the
dry-run in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…561b65a)

Regenerated with this branch's gta6rt --dry-run and --init on the 16
package DLLs (1.0.2.1), in main's table format with the --init column;
every --init exits 0.

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

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Updated: #1839's branch, which now has main 561b65a merged in, is merged here too, still with no force-push.

Thanks @poke900310-lgtm for re-reading, and @thehollowedone for the check.

  • Conflicts:
    • runtime/test/fixtures/tiny_game.cpp: both sides kept. main's usage line with [abort-mode] heads this PR's description, and this PR's identity checks sit next to main's abort handlers.
    • docs/runtime_imports.md: main's table format (with the --init column). The counts are regenerated from a fresh gta6rt --dry-run and --init on the 16 package DLLs of my own 1.0.2.1 copy with this build, and every --init exits 0.
  • This PR's tests (Windows 11, RX 5700, MSVC 14.44): pe_loader, loader, loader_tls, xgameruntime and every run_tiny_*: 19/19 pass.
  • Unrelated failures: the full suite has the same six D3D12 device failures as main 561b65a on this GPU (d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_pipeline_library_host, d3d12_m2_textured, d3d12_m2_formats, d3d12_m3_frame). It also has d3d12_sparse_queue_family_contract's intermittent crash (check_sparse_queue_family: run api_dump and validation apart (TEST-1089) #4900). None of them touches the loader.

AI-assisted (Claude Code).

@erickbobd

Copy link
Copy Markdown
Contributor

Executable-startup evidence for the module identity fix, AI-assisted with Codex.

I backported the runtime delta from reviewed snapshot 7fc3d5023cf4a572906fa0d335f8d520c4257018 onto the local #4313 TLS test branch. This retains the #3667 range-table lookup dependency and #1839's mapped-image RtlPcToFileHeader support, rather than merging unrelated newer main changes.

With a local Series Minecraft Preview executable:

  • Before this change, content lookup derived its root from the host loader's directory and terminated with 0xc0000409 / fatal-app-exit.
  • With the module-identity change, that failure disappeared and startup progressed to the strict unimplemented-call stop at xmem.dll!XMemTransferMemory.
  • Fifteen focused loader/TLS/GDK CTests passed after a clean rebuild. As a negative control, the new synthetic tiny_game identity checks fail under the saved pre-change loader and pass under the updated loader.

One additional local startup adaptation is essential to reproduce this: expose the direct xgameruntime.dll!XErrorReport(HRESULT, const char*) name and forward it to error_report(nullptr, hr, message), with XGR(XErrorReport) in the table. The previous COM slot alone was insufficient. This is now covered by #5312, whose mapped-thunk tests are integrated locally. The TLS correction is #4313. The title result above does not imply any menu or rendered frame.

For this probe I used a disposable package copy, because returning host paths also exposes the package-write issue tracked separately in #4966. No file-system fix from that PR is part of this test.

Environment: Windows 11 build 26200, MSVC 14.51, Ninja RelWithDebInfo, NVIDIA RTX 5070 Ti / driver 617.14. Functional offline correctness only; no performance claim or game artifacts.

@speed-y

speed-y commented Nov 23, 2026

Copy link
Copy Markdown

Tested on NVIDIA GeForce RTX 5080, driver 621.24, Windows 11: fail

Checked out a92e46a7069103a2e1e10ea32dd512d1b03b1e1e. VS 2022 / Ninja RelWithDebInfo. AI-assisted.

Built pe_loader_test / tiny_game targets; ran pe_loader, run_tiny_game, run_tiny_game_static_eh, run_tiny_bundled_load, run_tiny_game_exit_code, run_tiny_game_console_paths via ctest.

@speed-y

speed-y commented Nov 23, 2026

Copy link
Copy Markdown

Follow-up: earlier fail comment was a false negative (bad exit-code capture in my harness). Retested.

Tested on NVIDIA GeForce RTX 5080, driver 621.24, Windows 11: pass

Head a92e46a7069103a2e1e10ea32dd512d1b03b1e1e. VS 2022 / Ninja RelWithDebInfo. AI-assisted.

pe_loader=0
run_tiny_game=0
run_tiny_game_static_eh=0
run_tiny_bundled_load=0
run_tiny_game_console_paths=0
run_tiny_game_tls=0
run_tiny_game_eh=0
run_tiny_game_dir=0

@itaskaev2

Copy link
Copy Markdown

Tested a92e46a7 (contains main 561b65a8) natively on Windows 11 (build 26300), MSVC 14.51 (VS 2026 Build Tools), Ninja, RelWithDebInfo, NVIDIA RTX 4080 SUPER (616.92): pass. AI-assisted (Claude Code), no game files.

  • The full runtime builds with no errors.
  • The PR's tests, 20 of 20 pass: loader (87 checks), pe_loader (OK (125 checks)), pe_loader_bounds, loader_tls, xgameruntime, run_tiny_async, run_tiny_gdk_load, run_tiny_bundled_load, run_tiny_atexit, and every run_tiny_game*/run_tiny_frame test, run_tiny_game_static_eh included.
  • Full ctest -j 4: 246 of 249 pass. The 3 failures happen on plain main 561b65a8 on this machine too, so none of them is from this change: d3d12_indirect_dispatch_count, d3d12_indirect_rt, and xbox_present (crash).

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a92e46a on NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass, no regression against main 561b65a.

  • ctest: 244/248 passed; d3d12_indirect_dispatch_count, d3d12_indirect_rt, dxgi_device, dxgi_swapchain fails on main 561b65a too (not from this PR), same 11 skips as main 561b65a
  • loader, pe_loader, xgameruntime: 3/3
  • run_gpu_tests (LEONIDA_VK_VALIDATION=1): 183/232 native, 214/232 with LEONIDA_W64_EMULATE=1, 2 failed; ms_wave_info_s3 fails on main 561b65a too
    AI-assisted (Claude Code, Claude Fable 5.1).

@l-CatDemon-l

Copy link
Copy Markdown

Independent Windows check of module identity for a mapped image (synthetic guest, ASCII path, started from Explorer, no implicit layers, own builds of each tree, each result stable ×3). "image" = the guest's &__ImageBase, "gta6rt" = the host exe.

call native main 4beffda #2075 a92e46a #5731 2e0f1a2
GetModuleHandleW/A(NULL) image gta6rt image image
GetModuleHandleExW/A(0, NULL) image gta6rt image gta6rt
GetModuleHandleExW(FROM_ADDRESS, own function) image FALSE, 126 image FALSE, 126
GetModuleFileNameW/A(NULL) guest path gta6rt path guest path guest path
GetModuleFileNameW(&__ImageBase) guest path fails, 126 guest path guest path
GetProcAddress(NULL, export) own export NULL, 127 own export NULL, 127
GetProcAddress(&__ImageBase, export) own export own export own export own export

This covers only these calls; the other parts of #5731 (host binding, ordinal classes, docs) and non-ASCII or Wine paths were not measured. AI-assisted.

Both sides kept; the docs table is regenerated in the next commit.

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

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Merged the latest main (8575d36) in again, keeping both sides, with no force-push. On an RX 5700 (Windows 11, MSVC 14.44), this PR's tests (pe_loader, pe_loader_bounds, loader, loader_tls, xgameruntime* and every run_tiny_*, gta6rt_stack included) pass 30/30. A fresh --dry-run/--init on the 16 package DLLs leaves the docs table unchanged.

AI-assisted (Claude Code).

@LaugeM LaugeM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of the two module identity PRs (this one and #5731), this one matches Windows more closely, and I think its identity part should be the one merged. It needs a rebase first, though. Against upstream/main 5d74c27, git merge-tree reports conflicts in runtime/shims/library_loader.cpp, runtime/src/pe_loader.h and docs/runtime_imports.md.

The new conflicts come from #6287 and #6754 (3092fdf, 0656b0b). Main's BundledModuleOps now has find_loaded_w and load_bundled_w, and get_module_handle_w splits on ascii_basename() before calling narrow(). This branch replaces find_loaded with find_module. When resolving:

  • keep load_bundled_w;
  • check that module_names and run_module_names still pass. Those are #6754's tests for non-ASCII names and ANSI case folding outside ASCII. mapped_module_a's code-page conversion plus CompareStringOrdinal(..., TRUE) in Loader::find_module should cover them, but that needs a run.

I checked the rules this PR relies on with a native program on Windows 11 26200 (VS 18 toolset, not under gta6rt). They all match:

  • GetProcAddress(NULL, "export") returns the EXE's own export and leaves the last error alone.
  • GetModuleFileNameW(NULL, buf, 0) returns 0 with error 122, and (NULL, buf, 4) returns 4 with error 122. That matches copy_file_name (library_loader.cpp:243).
  • GetModuleHandleExW(0, NULL, NULL) returns FALSE with 87. GetModuleHandleExW(0x80, L"a.dll", &out) returns FALSE with 87 and clears out. That matches module_handle_ex_arguments (:259).
  • GetModuleHandleW(L"C:\\no_such_folder\\a.dll") with a.dll loaded from elsewhere returns NULL with 126. The full-path rule in mapped_name gets this right. Main's basename-only module_name(), and #5731 with it, answer with the loaded module.

Two smaller points:

  • searched_path adds three function-local statics with dynamic initialisers (library_loader.cpp:158-160). #9023 and #8195 are removing exactly this from the file: MSVC's _Init_thread_header guard reads TLS through index 0, which is not safe on a title thread if a title's TLS ever sits at index 0. b7a3fed keeps index 0 as gta6rt's, so this is not a bug on main today, but plain GetModuleHandleW/GetProcAddress calls on each use cost nothing on this rare path.
  • The open question about GetModuleFileName pointing into the package is what #4966 addresses: a host path inside --game-dir follows the GAME: rules. Since this PR is what hands the title that path, it would help to say Depends on: #4966, or land it right after, so a title that writes beside its EXE can't modify the package.

AI-assisted review (Opus 5.5). Read the diff against main; not built (merge-tree against upstream/main, and a native Windows probe of the Win32 rules above).

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.