Repository navigation
RT-25: loader TLS on every thread and MSVC C++ exceptions in mapped images - #965
Conversation
|
Tested 7e0d3ac on Windows 11, MSVC 14.51: ctest 35/35 on an RTX 4080 SUPER, including the new TLS and C++-exception loader tests. On an Intel UHD 770 it's 34/35, and the one failure is |
quackottu
left a comment
There was a problem hiding this comment.
Tested 7e0d3ac on NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 Pro (26300), VS 2022 Build Tools, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and LEONIDA_D3D12=vulkan: pass
ctest -C RelWithDebInfo: 100% tests passed, 0 tests failed out of 35- 39 test programs in the build pass with 0 validation errors
- Not run (they take an input file, not covered here): vk_overlapping_occlusion_test
d3d12_clear_testfails (R8G8B8A8_UNORM/SNORM 0.5 ties and a D16 clear byte differ from WARP) exactly as onmain4a36bb1 here, so not from this PR; #521 addresses it
Implicit Vulkan layers (overlays) were off through an empty VK_IMPLICIT_LAYER_PATH.
|
Tested 7e0d3ac on RTX 4090 (driver 621.14), Windows 11 26200, MSVC 19.44: pass (no regression against main).
The |
|
Tested 7e0d3ac on Windows: pass. Windows 11 Home 10.0.26300, MSVC 14.44.35207 (the PR was tested with 14.51), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo (
AI-assisted (Claude Code). No game files. |
7e0d3ac to
dcebc5c
Compare
|
Rebased on The red |
The Windows loader calls gta6rt_tls_host.dll's DllMain with DLL_THREAD_ATTACH on every new thread, after it has built the thread's TLS array, and with DLL_THREAD_DETACH before it frees it. A new export, gta6rt_tls_host_set_thread_event, lets the loader receive those events, so it can give each thread the mapped image's TLS. DllMain still touches no TLS (the helper's own slot is the one the loader swaps); the callback pointer is a plain global. Nothing registers it yet.
Until now only the thread that called load() had the mapped image's TLS; any other thread read gta6rt_tls_host.dll's block through _tls_index, so a thread_local on the game's own threads (gta6rt runs the entry point on a new thread) started from the wrong data: tiny_game's thread_local check fails on main with LEONIDA_RUN_TLS=1. The loader now registers for the helper's thread events. On DLL_THREAD_ATTACH the new thread gets a fresh copy of the template in the reserved slot; on DLL_THREAD_DETACH the helper's own block goes back before the Windows loader frees the array. Once initialise() has run the module, the events also run its TLS callbacks (as Windows does: thread events only after DLL_PROCESS_ATTACH), with the copy already in place. An SRW lock orders the owner's publication, its initialised flag and its retirement in ~Loader against the events, which run under the loader lock; the exclusive sections only write fields. ~Loader retires the owner before uninstalling, so a thread that outlives its Loader frees its copy from its own record without touching the freed Module. Module::tls_block stays the loading thread's copy, now set by initialize_tls only. install_tls_for_thread remains for threads that existed before load(); on a thread that already has the copy it is a no-op, which the updated worker check shows.
…utlives its Loader tiny_tls counts DLL_THREAD_ATTACH/DETACH only for a thread the test names, so threads the process starts for itself can't change the counts; the test starts that thread suspended and names it before it runs. Checks: one attach before the thread's code runs, with the template value already readable in the callback; one detach when it ends; and a thread that is still running when its Loader is destroyed gets its copy freed when it ends, with the process carrying on.
…dress Answers from the same per-Loader lookups the unimplemented-import report uses, under the same lock. Used by the next commit.
…ceptions work _CxxThrowException (vcruntime140.dll) stores the throwing image's base in the exception record, from RtlPcToFileHeader(pThrowInfo), and the frame handler adds the throw's RVAs to it. Windows only knows images its own loader mapped, so for a mapped image the base is 0, no catch matches, and the process ends with 0xE06D7363: tiny_game's LEONIDA_RUN_EH=1 check on main. When the loader binds a host copy of the MSVC runtime (prefer_host_copy, direct or through a forwarder), it points that DLL's imports of RtlPcToFileHeader by name at a version that asks Windows first and then the live Loaders' mapped modules. vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll in System32 import it (llvm-readobj --coff-imports); the package's FidelityFX upscaler and XboxBvhBuilder.dll import _CxxThrowException from vcruntime140.dll. The redirect is done once per DLL, before any mapped code runs, and stays for the process lifetime; with no live Loader it answers as Windows does.
The thread_local and C++ exception checks were switched on by LEONIDA_RUN_TLS=1 and LEONIDA_RUN_EH=1. scripts/run_wine_tests.py passes a test's command line but not its ENVIRONMENT property, so under Wine an environment-based test would only ever see the SKIP path. They now take arguments after "hello 42" (`tls`, `eh`), and CTest runs each in its own test, run_tiny_game_tls and run_tiny_game_eh. run_tiny_game keeps running without them. stdout is unbuffered, so a crash still shows how far the run got.
runtime/README.md: the RtlPcToFileHeader redirect, and the thread events in place of "the loader does not deliver thread attach/detach events"; mapped DLLs' DllMain still gets no thread events. design.md: the CPU-code row.
…esolves into GetProcAddress follows forwarders, so a host import can land in another DLL than the one named in the import table. The throw calls the RtlPcToFileHeader import of the module that implements _CxxThrowException, so that module is redirected too. On Windows it is vcruntime140.dll itself (no change). Under Wine, without this, run_tiny_game_eh faults inside a Wine DLL after the throw, reading 0xE080, an offset inside tiny_game's image size (consistent with a base of 0). With it, the fork's CI run of this branch prints "PASS a C++ exception is thrown and caught inside the image" under Wine. Which Wine DLL implements _CxxThrowException was not checked.
…e process detach ~Loader (ItsNotRexed#1172) clears initialised before delivering DLL_PROCESS_DETACH; with the thread events that write goes through the TLS lock, so a thread that ends during or after the detach no longer runs the module's callbacks.
…itialised module With initialise() run, the module's TLS callbacks take thread events, so the thread that ends after ~Loader has delivered the process detach and unmapped the image checks that no DLL_THREAD_DETACH callback runs into freed memory.
dcebc5c to
c8c6400
Compare
|
The red |
…tsNotRexed#965 d3d12_command_objects.md 3.5a: slot 67 is DeserializeComputePipelineStateX with its full desc (kind 0, 1, 6, 7 pointer/size pairs), and the open "what FSPX holds" line now points at fspx.md (13 of 29 translate). d3d12_vulkan.md A.3: the same name, and the throw: before ItsNotRexed#965 it carried an image base of 0; with ItsNotRexed#965 it carries the DLL's base and still reaches the caller (fsr_dispatch_test, LEONIDA_FSR_NO_STANDIN=1).
Claimed in the coordination issue as: RT-25 (#634 (comment))
What and why
Two loader gaps that
tiny_gamealready showed, and that the real executable would hit early:load()had the mapped image's static TLS. Any other thread readgta6rt_tls_host.dll's block through_tls_index. That includes the threadgta6rt --runstarts the entry point on. Onmain, the opt-in check givesFAIL thread_local starts at its initializer on each thread._CxxThrowExceptionputs the throwing image's base in the exception record, fromRtlPcToFileHeader. Windows only knows images its own loader mapped, so for a mapped image that base is 0 and nocatchmatches. Onmain, the opt-in check ends the run with0xE06D7363. The package's FidelityFX upscaler andXboxBvhBuilder.dllimport_CxxThrowExceptionfromvcruntime140.dll.How
gta6rt_tls_host.dll'sDllMainwithDLL_THREAD_ATTACHon every new thread, after it builds that thread's TLS array, and withDLL_THREAD_DETACHbefore it frees it. The helper now passes those events to the loader:initialise()has run the module, the events also run its TLS callbacks, with the copy in place. Windows orders it the same way: no thread events beforeDLL_PROCESS_ATTACH.initialisedand its retirement in~Loaderagainst the events.Loaderfrees its copy from its own record.~Loader(RT-71: deliver process detach before unmapping PE modules #1172) clearsinitialisedbefore the process detach; that write goes through the same lock, so from then on the thread events stop running the module's callbacks.install_tls_for_threadis still there for threads that existed beforeload().prefer_host_copylist), it points that DLL's imports ofRtlPcToFileHeaderat a version that asks Windows first and then the liveLoaders' mapped modules. It does the same for the module an import actually resolves into, sinceGetProcAddressfollows forwarders.vcruntime140.dll,vcruntime140_1.dllandmsvcp140.dllimport it (llvm-readobj --coff-imports).Loaderit answers exactly as Windows does.tiny_game. The opt-in checks now take arguments (tls,eh) instead ofLEONIDA_RUN_TLS/LEONIDA_RUN_EH, becausescripts/run_wine_tests.pypasses a test's command line but not itsENVIRONMENT. CTest runs each check in its own test,run_tiny_game_tlsandrun_tiny_game_eh, and stdout is unbuffered.Not covered
DllMainstill gets no thread events; only the TLS owner's callbacks do.load()get nothing automatically.RtlPcToFileHeaderitself is not redirected. No measured image here does.bvh_builder_teststill ends with0xC0000409after device slot 62 on my machine, with and without this PR, so this is not its cause.Tested
ctest81/81 after rebasing onc2967a5e(over RT-71: deliver process detach before unmapping PE modules #1172). Each commit builds and passescteston its own.run_tiny_game_tls:PASS thread_local starts at its initializer on each thread.run_tiny_game_eh:PASS a C++ exception is thrown and caught inside the image.0xE06D7363again; with only the thread events disabled, thethread_localcheck fails andloader_tlsfails.loader_tls: 45 checks, including oneDLL_THREAD_ATTACHbefore the thread's code with the template readable in the callback, oneDLL_THREAD_DETACH, and the copy freed when a thread of an initialised module ends after itsLoaderhas unmapped the image, with no callback into it.loader_tls,run_tiny_game_tlsandrun_tiny_game_ehpass.loader_tls_test: OK (45 checks).run_tiny_game_ehfaulted under Wine.throwreaches the handler with its real image base (0x180000000, was0x0). The DLL doesn't catch it there, sofsr_dispatch_testreports the same stop.Checklist
docs/design.md, with how it was confirmed. (Not a translator change.)AI-assisted.