fix(agc): free per-thread driver state at thread exit without emutls #590
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/590"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-05T00:09:20Z.
What
agc_driver_flipfails intermittently on the Windows CI job, for example on #574 (run 37242466887): a stack overflow inside libstdc++ on the second--repeat. The cause is a use-after-free at thread exit, in every driverthread_localwith a non-trivial destructor.Root cause. With MinGW GCC,
thread_localis emutls, and thread exit happens in two steps in the wrong order:DLL_THREAD_DETACH, the CRT'stls_callback→run_dtor_listruns the C++thread_localdestructors on those already freed blocks.So
~DeferredLabels()and the others read their vector or map pointers from freed memory and free them. Depending on what reused the memory, that is silent, heap corruption (0xc0000374), an access violation infree, or the stack overflow seen in CI.How it was pinned down:
0xc0000374.testFailure.freewasAgcDriver::DriverDetail::DeferredLabels::~DeferredLabels().thisis filled with0xfeeefeee, the debug heap's freed-memory pattern.Fix.
ShaderRecompiler::ThreadOwned(core/shader/recompiler/ThreadOwned.hpp):thread_local T*, which registers nothing with the CRT's thread-exit list.It lives in the recompiler tree because the recompiler's own disk cache needs it and the recompiler doesn't depend on
libs/prx. The driver already uses recompiler headers, andThreadOwnedis added toshaderCacheReviewedNames, since it cannot change a recompile.Converted sites:
PacketProfile, the CPU-fill block, the six capture-validation scratch vectors, andThreadMemoryCounters. Its destructor also unregisters from the global profile, and that now happens before the storage goes away.ThreadBuildProfile, which merges into the global profile on destruction, the lookup log, the revalidation scratch vectors, and the image-mirror change mask.objdumpoflibSceAgcDriver.prxshows no calls to__cxa_thread_atexitleft; there were 25 before.Follow-ups, not in this PR: the same hazard exists outside the driver.
libkernel.prx:scePthreadGetspecific/scePthreadSetspecificandsceKernelUuidCreate.libSceRandom.prx:sceRandomGetRandomNumber.Tested
Windows 11, MinGW GCC 15.2.0 r7, NVIDIA Vulkan.
agc_driver_flipunder gdb (debug heap): 5/5 pass. Without the fix, every run stops withCritical error detected c0000374.agc_driver_flip, 12 parallelctest --repeat until-fail:100: 1200/1200 pass. Without the fix, the same load reproduced the crash.ctest: 220/221 pass.agc_shader_memoryfails without this change too; #574 fixes it.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:36Z.
Thanks, that's a very clear diagnosis. Keeping the state behind a trivially destructible pointer and deleting it from a pthread key destructor fixes the emutls order, and the remaining
thread_locals in the driver are all trivially destructible.Two small notes, neither blocking:
thread_localslot still points at it. If anything on that thread reachesCounterFor()ordeferredLabels()later in thread exit (another key's destructor, for example), it gets freed memory on Linux, where TLS is native. Clearing the slot in the destroy callback (keeping aT**inOwned) would make a late call allocate a fresh object instead, which the key destructor loop then frees again.exit(), where C++thread_localdestructors did. So the main thread'sThreadBuildProfilemerge and report, and itsThreadMemoryCountersretirement, no longer happen at process exit. That only affects diagnostics.There's no new test; the Windows
agc_driver_fliprun in CI passed, and on Linux the full ctest passes here. PPSA21564 runs on Linux, so this doesn't change anything I can observe. Ready to merge from my side.