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: |
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.mdtable, so this branch has #1839's branch merged in (a merge, nothing rewritten). This PR's own work isdd107b39plus 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/Wwasn't shimmed. With NULL it named gta6rt.exe, and titles often derive their install folder from that. With a mapped image'sHMODULEit failed, because Windows doesn't know VirtualAlloc'd images.GetModuleHandleExA/Wwasn't shimmed, soFROM_ADDRESSon an address inside a mapped image failed.GetModuleHandle("<mapped DLL>")andGetProcAddresson a mapped image went to Windows and failed.The retail package's DLLs import these from
KERNEL32.dllandapi-ms-win-core-libraryloader-l1-2-0.dll.The change:
GetModuleHandle*,GetModuleHandleEx*,GetModuleFileName*andGetProcAddressnow answer for the images a liveLoadermaps, under all seven nameslibrary_loader.cppalready serves. They find a module by file name or full path, by base, and by any address inside it (FROM_ADDRESS)..dllis added, a trailing.means no extension,GetModuleFileNametruncates as Windows does,GetModuleHandleExvalidates its arguments first, andGetProcAddresssetsERROR_PROC_NOT_FOUND,ERROR_INVALID_ORDINALorERROR_INVALID_PARAMETER. These were measured on Windows 11.gta6rt --runruns (Loader::set_main_module, set beforeinitialise()). Without one it stays Windows' answer, so--dry-run/--initbehave as before.GetProcAddressfollows a mapped module's forwarders as imports do (Loader::export_address), but never maps a module while image code runs.FreeLibraryon a mapped image succeeds and changes nothing.ModuleQuery/ModuleFind/ModuleProcinshims.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::pathis now made absolute at map time.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.dllrule, full and relative paths,FROM_ADDRESSon the first and last byte and one past,GetModuleHandleExflag errors,GetModuleFileNametruncation and ANSI,GetProcAddressby name, by ordinal and through a forwarder (newtiny_forwardedexport intiny_dll), and Windows' answers coming back once the Loader is gone.tiny_game, undergta6rt --run, now checks that it sees itself:GetModuleHandle(NULL)is its image;GetModuleFileName(NULL)is its own file, checked by comparing its headers;FROM_ADDRESSon its code and data finds its image.These checks pass in
run_tiny_game*and also under the static CRT (run_tiny_game_static_ehfrom Runtime: RtlPcToFileHeader for a mapped image's own imports (static-CRT C++ exceptions) #1839).Negative control: with
main'slibrary_loader.cpp,pe_loader,xgameruntime,run_tiny_gdk_loadand allrun_tiny_game*fail. The game is told its file isgta6rt.exe.Full runtime
ctestof this branch: 107/109 pass, 6 skipped. The two failures fail onmainhere too:d3d12_vulkan_device's tiled-MSAA row from d3d12: support tiled placed textures (D3D-191) #1674's first merge;d3d12_draw, on a warm AMD driver cache (finding).gta6rt --dry-runon 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
docs/design.md, with how it was confirmed. (N/A: no translator change.)🤖 Generated with Claude Code