Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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
|
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. |
|
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.
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 AI-assisted review (Claude Code), read-only; I didn't build or run it. |
|
A note from #3667, which touches the neighbouring code: |
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>
|
Updated: #1839's branch, which now has Thanks @poke900310-lgtm for re-reading, and @thehollowedone for the check.
AI-assisted (Claude Code). |
|
Executable-startup evidence for the module identity fix, AI-assisted with Codex. I backported the runtime delta from reviewed snapshot With a local Series Minecraft Preview executable:
One additional local startup adaptation is essential to reproduce this: expose the direct 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. |
|
Tested on NVIDIA GeForce RTX 5080, driver 621.24, Windows 11: fail Checked out Built |
|
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 pe_loader=0 |
|
Tested
|
|
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.
|
|
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
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>
|
Merged the latest AI-assisted (Claude Code). |
LaugeM
left a comment
There was a problem hiding this comment.
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_namesandrun_module_namesstill pass. Those are #6754's tests for non-ASCII names and ANSI case folding outside ASCII.mapped_module_a's code-page conversion plusCompareStringOrdinal(..., TRUE)inLoader::find_moduleshould 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 matchescopy_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 clearsout. That matchesmodule_handle_ex_arguments(:259).GetModuleHandleW(L"C:\\no_such_folder\\a.dll")witha.dllloaded from elsewhere returns NULL with 126. The full-path rule inmapped_namegets this right. Main's basename-onlymodule_name(), and #5731 with it, answer with the loaded module.
Two smaller points:
searched_pathadds 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_headerguard 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 plainGetModuleHandleW/GetProcAddresscalls on each use cost nothing on this rare path.- The open question about
GetModuleFileNamepointing into the package is what #4966 addresses: a host path inside--game-dirfollows the GAME: rules. Since this PR is what hands the title that path, it would help to sayDepends 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).
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.mdtable, so this branch has #1839's branch merged in. It also has currentmainmerged 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). Onmain:GetModuleHandleA/W(NULL)andGetProcAddress(NULL, name)go to Windows, which answers for the process's executable: gta6rt.exe, not the game.GetModuleFileNameA/Wisn't shimmed. With NULL it names gta6rt.exe, and titles often derive their install folder from that. With a mapped image'sHMODULEit fails, because Windows doesn't know VirtualAlloc'd images.GetModuleHandleExA/Wisn't shimmed, so a name, orFROM_ADDRESSon an address inside a mapped image, fails.GetModuleHandle(name)matches a mapped module by its file name whatever folder the name gives, and everyGetProcAddressmiss isERROR_PROC_NOT_FOUND.The retail package's DLLs import these from
KERNEL32.dllandapi-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 --runruns (Loader::set_main_module, set beforeinitialise()):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 throughGetModuleHandle(NULL). So this follows Windows rather than the documentedhModulecontract (see the review thread). Without--run, all of these stay Windows' answers, so--dry-runand--initbehave as before.Names.
.dllis added when the name has no extension, and trailing dots mean it has none./,.and..are resolved...\x.dll,.\x.dll) is looked for along the DLL search path, as Windows does (ntdll'sLdrGetDllPath), 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 - 1characters,ERROR_INSUFFICIENT_BUFFER).GetModuleHandleEx*checks its arguments first, and answersFROM_ADDRESSfor any byte of a mapped image.GetProcAddresserrors.ERROR_PROC_NOT_FOUND, and an ordinal givesERROR_INVALID_ORDINAL.ERROR_INVALID_PARAMETERwhen the image has an export directory, andERROR_INVALID_ORDINALwhen it has none. Windows looks only at the export data directory's RVA.One path,
main's locks.BundledModuleOpsentry,find_module.GetProcAddresson a mapped module, NULL included, goes only throughmain'sresolve_module_export. So a forwarder into a bundled DLL that isn't mapped yet maps and initialises it, as onmain.by_name_underLoader::mutex_, asmain'sfind_loaded_moduledoes. 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::pathis 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) andruntime/README.md.Open question for the maintainers:
GetModuleFileNamepoints into the packageGetModuleFileName(NULL), andGetModuleFileNameon 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.cppsends any path that isn'tGAME:,G:orT:straight to the host, and keeps a path under--game-dira 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
CreateFilefor writing,DeleteFile,MoveFile,CreateDirectoryandSetFileAttributes. Reads andFindFirstFilethrough the path also miss the overlay's copies, deletions and merged listing. This PR doesn't change that. The options I see:GAME:\<exe>orG:\<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 hostLoadLibrary, a shell call, a path library) gets a path the host doesn't understand.file_system.cpptreat a path under--game-diras the matchingGAME: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.--game-dirthrough host paths withERROR_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:
GetModuleHandleWwith names without a path, full paths, and relative paths under different current folders,SetDllDirectorysettings, 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
GetModuleHandleWin all 21 relative cases measured.pe_loader_test(125 checks) covers each function:.dlland trailing-dot rules, and full paths;SetDllDirectory("")), and through aSetDllDirectoryfolder;FROM_ADDRESSon the first and last byte of an image and one past it;GetModuleHandleExflag errors, andGetModuleFileNametruncation and ANSI;GetProcAddressby 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;loader_test(87 checks) addstest_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, undergta6rt --run, checks that it sees itself:GetModuleHandle(NULL)is its image;GetModuleFileName(NULL)is its own file, compared by headers;FROM_ADDRESSon its code and data finds its image;GetProcAddress(NULL, name)is the same export asGetProcAddress(GetModuleHandle(NULL), name).These pass in all
run_tiny_game*tests, including the static-CRTrun_tiny_game_static_eh.Negative controls:
GetProcAddressremoved, 2pe_loader_testchecks and 2tiny_gamechecks fail.library_loader.cpp(current folder for relative paths, one trailing dot,ERROR_INVALID_PARAMETERfor every null name, NULL to the host), 6pe_loader_testchecks and the same 2tiny_gamechecks fail.The PR's tests (
pe_loader,loader,loader_tls,xgameruntime, and everyrun_tiny_*test,run_tiny_gdk_loadandrun_tiny_bundled_loadincluded): 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-runon 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:
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.AI-assisted: yes (Claude Code).
Checklist
docs/design.md, with how it was confirmed. (N/A: no translator change.)🤖 Generated with Claude Code