Linux: the application heap is finalized before guest destructors run, so normal process exit aborts #948
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 jeffrey12cali on 2026-10-06T19:49:48Z.
On a clean exit (
kill -TERM,exit(), anything that runsatexithandlers) a session ends with anuncaught exception and
SIGABRT:ApplicationHeapInitialize_nid_no_patchregistersfinalize()withstd::atexitwhen the heaphandshake runs.
std::atexitexecutes handlers in reverse registration order, so the heap - registeredfairly early - is torn down before handlers registered later:
libc's own__cxa_atexitrunner(
RuntimeSupport.cpp, first guest destructor registers it) and the title's exit callbacks(
Process.cpp,atexit_nid_no_patch). Those then free and reallocate through a finalized heap, and theentry points answer with a thrown
std::runtime_error, which escapes a destructor path and terminates.The caller is legitimate title code, measured with a host backtrace at the throw:
Note it is
Realign, notFree, and the alignment is honoured by the title's allocator - this is anordinary exit-time realloc, not a use-after-free of stale state.
Why it shows up now: while
sceKernelLoadStartModulediscardedargs/argp(#918) the heap handshakenever completed, no finalizer was registered, and the ordering problem was invisible. Fixing #918 makes
the handshake run and exposes it, so any title that passes the heap API now aborts at exit.
Fix (23 lines, one file): register the finalizer when
libc.prxis loaded instead of when the handshakeruns, so reverse-registration order makes it the last handler to run and the heap outlives every exit
user; and make
finalize()idempotent rather than throwing on a second pass, since the load-timeregistration and the handshake can both reach it. The
application heap: allocator has been finalizedcheck itself stays - it is a real bug detector for use after a deliberate finalization - it just no
longer fires during normal teardown.
Measured in our tree: exit no longer reports an unhandled exception or
terminate(0 occurrences, was2 per session), teardown progresses further (the title's exit callback and the GPU object teardown now
run), and boot is unchanged.
Happy to send this as a PR. One alternative I considered and rejected: making the release paths
(
Free, zero-sizeReallocate/Realign) no-ops after finalization instead of throwing. That hidesgenuine use-after-finalize bugs and still leaves
Allocatethrowing, so ordering is the better fix.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-06T22:40:55Z.
Implemented this in #1001: the finalizer is now registered from a namespace-scope object when libc is loaded (so it runs last), and
finalize()is idempotent instead of throwing on a second pass. Addedapplication_heap_exit_order, which registers a guest exit callback that allocates through the heap and reproduces the abort on the previous code (what(): application heap: allocator has been finalized); 368/368 tests pass with the change. @jeffrey12cali if you already have this in a branch, say so and I will close mine.Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-06T23:09:02Z.
Go ahead with #1001 — please keep it. I do have the same fix, but only as a patch in my vendored tree plus a commit on my title-side bring-up branch, so there is no engine PR of mine that this duplicates. The design conclusion matches exactly: register the finalizer when the library loads so reverse registration order makes it run last, and make finalization idempotent.
Two data points that may sharpen the test in #1001:
libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <- <game executable>+0x822e2c. A test that exercises allocation or free alone would not have caught it. Yours (a guest exit callback that allocates) is the right shape; I would add aRealign/Reallocatevariant next to it.I will drop my vendored copy as soon as #1001 lands and re-pin onto it.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T02:03:29Z.
One thing I should have asked before opening #1001: which title ID did you measure the abort on?
The maintainer's rule is that a system-library change is merged when a title ID and the log line show a title stopping there, so #1001 is missing that half. You measured it with a host backtrace at the throw, so if you can name the title (and the command that reproduces it), I will put it in the PR description rather than leave it without one.
Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-07T05:16:12Z.
PPSA28997 - and here is the evidence in the form the rule asks for, since #1001 is the fix that carries it.
The log lines, from stderr, on a clean exit (the session stopped with
SIGTERM, which runs theatexithandlers). Exit status was134(SIGABRT), and it reproduced in every session once the title completed the application-heap handshake:Who asks for it: the title's own teardown, reallocating aligned memory. A host backtrace taken at the throw:
which is why I suggested the regression test also cover
Realign/Reallocate, not only alloc/free.Repro conditions, in case they matter to the description: the title has to reach the point where it registers the allocator API (
ApplicationHeapInitialize), because that is what registers the finalizer - a title whose handshake never lands registers no finalizer and therefore exits silently, which is probably why this stayed unreported. So the abort needs both halves: a completed handshake and an exit path that runs handlers.Confirmed fixed here: 0 occurrences of those two lines after the change, and teardown now progresses further (the title's exit callback and the GPU object teardown both run instead of dying inside the first handler). Thanks for picking it up so quickly - #1001 landed within hours of the report.