fix(libc): register the application heap finalizer when libc loads #1001
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1001"
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 Dextroid17 on 2026-10-06T22:40:22Z.
What
Registers the application heap finalizer when libc is loaded instead of when the heap handshake runs, so it is the last handler to run at exit, and makes
finalize()idempotent rather than throwing on a second pass. Fixes #948.std::atexitruns handlers in reverse registration order. The finalizer was registered fromApplicationHeapInitialize_nid_no_patch, which runs after libc's own__cxa_atexitrunner (RuntimeSupport.cpp, first guest destructor) and the title's exit callbacks (Process.cpp,atexit_nid_postfix) register theirs, so the heap was torn down first. An exit user that allocated then threwapplication heap: allocator has been finalizedout of a destructor path, which terminates the process.The
application heap: allocator has been finalizedcheck incallback()is unchanged; it still detects use after a deliberate finalization.Which title needs this
This one is not specific to a title, which is the point: the abort fires on any title that passes the heap API, on every clean exit, not on one title's path. Reported and measured by @jeffrey12cali, whose log has the caller as title code reallocating aligned memory during its own teardown:
and measured 2 occurrences per session before the change, 0 after. He has the same fix in his vendored tree and has said to keep this PR.
I have asked him for the title ID and the reproducing command on #948 and will add them here as soon as he answers, so the change carries the title the project asks for.
Tested
application_heap_exit_order(new): registers a guest exit callback throughatexit_nid_postfixthat allocates through the heap, then runs the handshake. On the previousApplicationHeap.cppit aborts with[libc] unhandled exception 'St13runtime_error' ... what(): application heap: allocator has been finalized; with the change it passes. Verified by building the test against the unchangedApplicationHeap.cpp.ctest --test-dir build -j 8 --timeout 120→ 368/368 pass.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)Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-07T05:16:23Z.
Merged, and confirmed fixed in our tree - thanks. Attaching the title evidence here as well, since this is the change that carries it (#948 has the full write-up):
SIGTERM, exit status134), reproducible every session:libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <- <game executable>+0x822e2c.Suggestion for the test, for whoever touches that file next:
application_heap_exit_ordercovers an allocation through the heap; adding aRealign/Reallocatecase alongside it costs one line and matches how the title actually tripped.