Repository navigation
fix(agc): free per-thread driver state at thread exit without emutls - #590
Merged
oneandonlydean merged 1 commit intoOct 5, 2026
Merged
Conversation
On MinGW, thread_local storage is emutls: libgcc frees a thread's blocks from a winpthread key destructor when the thread function returns, and the CRT runs the thread_local destructors later, from the module's TLS callback at thread detach. The destructors of the driver's non-trivial thread_locals (the deferred label queue, packet profile, capture and revalidation scratch vectors, build profile, memory counters, disk cache buffers) therefore ran on freed storage. Under the debug heap the flip test failed on every run at the queue worker's exit after the injected flip failure; without it the use-after-free showed up as intermittent heap corruption or a stack overflow on the Windows CI job. ShaderRecompiler::ThreadOwned keeps such state behind a trivially destructible thread_local pointer and deletes it from its own pthread key destructor, which receives the objects directly instead of reading emutls. The driver no longer registers any thread_local destructor. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
|
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 Two small notes, neither blocking:
There's no new test; the Windows |
oneandonlydean
added a commit
to oneandonlydean/AnyPS5
that referenced
this pull request
Oct 5, 2026
Conflicts resolved: - libSceAudioPropagation/Export.cpp: upstream's boykopovar#482 + boykopovar#489 version taken; it is the same no-propagation model as astrobot's stand-in, with SourceRender and the handle-only UnregisterMaterial already in it. - State.cpp (boykopovar#453): astrobot's resident depth model kept; it already skips the tests of an absent plane (boykopovar#453's test is on astrobot since e087a4f). - Graphics.cpp, State.hpp, ShaderResources.cpp: astrobot's DepthTarget model kept (exportIndex, no DepthSurface.hpp), upstream's ThreadOwned include added. - BdaResources.cpp: astrobot's form with boykopovar#390's message. - VulkanDevice.cpp, Context.hpp, PacketExecution.cpp: both sides kept (calibrated timestamps, host query reset, GDS, memory budget; pipeline stats). - Spirv: astrobot's split-wave rules first, then boykopovar#608's wave-mask ballot and boykopovar#587's host subgroup lane; astrobot's function LDS sizing and boykopovar#573's LDS lock both kept; conditionalDepth kept in the atomic update loops; upstream's buffer atomics (boykopovar#321 boykopovar#332 boykopovar#594) and 64-bit LDS atomics taken. - TechnicalDebt.md: both sides, duplicate lines dropped. Build fixes folded in: the upstream boykopovar#453 test block duplicated next to astrobot's adapted copy removed; a lost closing brace after the host query reset block restored; lookupLog() is a function since boykopovar#590; the duplicate MUTEX_TYPE_ADAPTIVE constant (astrobot's 93b2d0e equals boykopovar#156) removed; the ops boykopovar#596 drops removed from the masked-select list and the BDA test message updated (the gate-batch-2 integ commits 559e4fab6 and 2ec968fb5).
9 tasks done
9 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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; fix(shader): splat the bindless table hit into a bool vector for SPIR-V 1.3 #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