fix(ajm, apr): keep AJM and APR state alive through static teardown #1926
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1926"
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 fisherjoey on 2026-10-09T00:07:27Z.
What
A window close ends in
std::exiton a helper thread (LibcRequestExit_nid_postfix), andstd::exitdestroys the host libraries' statics while the title's threads are still running. AJM and APR keep their state in namespace-scope statics, so a title thread that calls them during exit reads freed memory:libSceAjm.native:g_instancesandg_batches(and their mutexes). A job on a freedInstancedecodes a garbage codec,sceAjmBatchWaitreturnsSCE_AJM_ERROR_INVALID_BATCHfor a batch it just started, andsceAjmInstanceDestroyfrees an instance twice.libkernelAPR:g_files,g_idsByPath,g_directories,g_counterWritesand the AMM state from_amm(). A read resolves its file id through the freed vector.All of these are now allocated once and never destroyed, the same approach #1197 took for NGS2. Callers don't change: the namespace-scope names are now references to the heap objects.
Closes #1925
Tested
Nobara 44 x86-64 (Linux 7.2), GCC 16.2, RTX 5070 Ti, driver 595.91.07. Base:
mainf1451396.guest_ajm_teardown(UNIX only, it needsdlopen). It registers anatexithandler, thendlopenslibSceAjm.nativeand creates an instance. Because the library is loaded after the handler is registered, its statics are destroyed before the handler runs. The handler then runs a batch on the instance, waits for the batch and destroys the instance. Withmain'sAjm.cppit aborts 3 of 3 times (free(): double free detected in tcache 2). With this change it passes.ctest -j1 --timeout 60: 494 of 495 pass. The one failure,guest_dynamic_loader, also fails here without this change.tools/check_conventions.pyandtools/check_test_registration.pypass.mainfrom 10-07 with #1071, #1202, #1184 and local performance changes) with this commit on top. Before: 15 of 15 closes crashed (SIGABRT onAJM: decoding codec 4026830656 is not implemented,APR: cannot open <garbage>ordouble free or corruption, or SIGSEGV afterNCA DecoderManager: sceAjmBatchWait() failed with error code 0x80930004), and 3 of 3 with #1658. After: 5 of 5 closes exit 0 with no core. One more run aborted at boot, before any close, on an X RandR error (BadRROutput) andVideoOut: presentation thread cannot stop itself. That is unrelated to this change.Other libkernel state lives in namespace-scope statics too (
Equeue.cpp,SyncOnAddress.cpp,DirectMemory.cpp). Demon's Souls didn't hit those in these runs, so they are left as they are.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 repositoryImported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-09T00:46:21Z.
Review: APPROVED
I can approve this.
*new Tidiom to make these statics immortal is the established pattern here (you cite #1197 for NGS2), so it's not a novel hack — it's the project's chosen remedy for "title thread reads freed statics duringstd::exit". Consistent.dlopens the library after registering theatexithandler so the library's statics are destroyed before the handler runs, then exercises AJM from the handler.mainaborts 3/3 (double free in tcache 2), this branch passes. That's a genuine regression test for the exact failure class, not a symbol check.libkernelnamespace statics (Equeue.cpp,SyncOnAddress.cpp,DirectMemory.cpp) that weren't hit — honest boundary, not silent.Non-blocking note: the deliberate leak is the whole point, but a reader glancing at
std::mutex& g_lock = *new std::mutex();might not know it's intentional project policy. A one-line debt/CONVENTIONSpointer (or a shared helperImmortal<T>) would stop a future reviewer from "fixing" it back to a normal static. Worth it given it's now in at least three files.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T10:25:53Z.
Windows build check, since the description says Windows wasn't built. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Merged onto
main745b775etogether with #1926, #1950, #1260 and #1988. Full ctest: 507/507.guest_ajmand APR tests pass.guest_ajm_teardownis registered underif(UNIX), so it didn't run here.🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T15:14:30Z.
@klosixgaming-arch thanks. I added a TechnicalDebt entry for the leaked statics (NGS2, AJM, APR) in f79d69cc, since code comments aren't allowed here. A shared helper would also change NGS2, so I'd leave it for another PR.
@popcoder565 thanks for the Windows build. The teardown test is Unix-only on purpose. It depends on the ELF order in which a
dlopen'd library's statics are destroyed relative toatexithandlers. On Windows a DLL's ownatexithandlers and static destructors run when that DLL detaches (DLL_PROCESS_DETACH), so the same setup doesn't reproduce the ordering there.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T02:45:01Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/TechnicalDebt.mdL398docs/dev/TechnicalDebt.mdL398core/libs/tests/GuestAjmTeardown.cpp