Linux: the application heap is finalized before guest destructors run, so normal process exit aborts #948

Closed
opened 2026-10-06 19:49:48 +00:00 by jeffrey12cali · 4 comments
jeffrey12cali commented 2026-10-06 19:49:48 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by jeffrey12cali on 2026-10-06T19:49:48Z.


On a clean exit (kill -TERM, exit(), anything that runs atexit handlers) a session ends with an
uncaught exception and SIGABRT:

[libc] unhandled exception 'St13runtime_error' thrown from ...
terminate called after throwing an instance of 'std::runtime_error'
  what():  application heap: allocator has been finalized

ApplicationHeapInitialize_nid_no_patch registers finalize() with std::atexit when the heap
handshake runs. std::atexit executes handlers in reverse registration order, so the heap - registered
fairly early - is torn down before handlers registered later: libc's own __cxa_atexit runner
(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 the
entry 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:

libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f)
<game executable>(+0x822e2c)   # reallocating aligned memory during its own teardown

Note it is Realign, not Free, and the alignment is honoured by the title's allocator - this is an
ordinary exit-time realloc, not a use-after-free of stale state.

Why it shows up now: while sceKernelLoadStartModule discarded args/argp (#918) the heap handshake
never 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.prx is loaded instead of when the handshake
runs, 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-time
registration and the handshake can both reach it. The application heap: allocator has been finalized
check 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, was
2 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-size Reallocate/Realign) no-ops after finalization instead of throwing. That hides
genuine use-after-finalize bugs and still leaves Allocate throwing, so ordering is the better fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5734484944 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/948). Originally posted by [jeffrey12cali](https://github.com/jeffrey12cali) on 2026-10-06T19:49:48Z. --- On a clean exit (`kill -TERM`, `exit()`, anything that runs `atexit` handlers) a session ends with an uncaught exception and `SIGABRT`: ``` [libc] unhandled exception 'St13runtime_error' thrown from ... terminate called after throwing an instance of 'std::runtime_error' what(): application heap: allocator has been finalized ``` `ApplicationHeapInitialize_nid_no_patch` registers `finalize()` with `std::atexit` when the heap handshake runs. `std::atexit` executes handlers in reverse registration order, so the heap - registered fairly early - is torn down **before** handlers registered later: `libc`'s own `__cxa_atexit` runner (`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 the entry 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: ``` libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <game executable>(+0x822e2c) # reallocating aligned memory during its own teardown ``` Note it is `Realign`, not `Free`, and the alignment is honoured by the title's allocator - this is an ordinary exit-time realloc, not a use-after-free of stale state. Why it shows up now: while `sceKernelLoadStartModule` discarded `args`/`argp` (#918) the heap handshake never 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.prx` is loaded instead of when the handshake runs, 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-time registration and the handshake can both reach it. The `application heap: allocator has been finalized` check 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, was 2 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-size `Reallocate`/`Realign`) no-ops after finalization instead of throwing. That hides genuine use-after-finalize bugs and still leaves `Allocate` throwing, so ordering is the better fix.
Dextroid17 commented 2026-10-06 22:40:55 +00:00 (Migrated from github.com)
Author
Contributor

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. Added application_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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6026769619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/948#issuecomment-6026769619). Originally posted by [Dextroid17](https://github.com/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. Added `application_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.
jeffrey12cali commented 2026-10-06 23:09:02 +00:00 (Migrated from github.com)
Author
Contributor

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:

  • On this title the call that trips it is an aligned realloc from the game binary, not a free. Host backtrace at the throw: 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 a Realign/Reallocate variant next to it.
  • Why it went unreported: the abort needs the heap handshake to have registered a finalizer. A title whose handshake never completes registers none and therefore exits silently, so the ordering inversion is invisible until something makes the handshake land — which is part of what #918 changes for affected titles.

I will drop my vendored copy as soon as #1001 lands and re-pin onto it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6027105998 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/948#issuecomment-6027105998). Originally posted by [jeffrey12cali](https://github.com/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: - On this title the call that trips it is an *aligned realloc from the game binary*, not a free. Host backtrace at the throw: `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 a `Realign`/`Reallocate` variant next to it. - Why it went unreported: the abort needs the heap handshake to have registered a finalizer. A title whose handshake never completes registers none and therefore exits silently, so the ordering inversion is invisible until something makes the handshake land — which is part of what #918 changes for affected titles. I will drop my vendored copy as soon as #1001 lands and re-pin onto it.
Dextroid17 commented 2026-10-07 02:03:29 +00:00 (Migrated from github.com)
Author
Contributor

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6029320542 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/948#issuecomment-6029320542). Originally posted by [Dextroid17](https://github.com/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.
jeffrey12cali commented 2026-10-07 05:16:12 +00:00 (Migrated from github.com)
Author
Contributor

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 the atexit handlers). Exit status was 134 (SIGABRT), and it reproduced in every session once the title completed the application-heap handshake:

[libc] unhandled exception 'St13runtime_error' thrown from 0x7f6541a2e653
terminate called after throwing an instance of 'std::runtime_error'
  what():  application heap: allocator has been finalized

Who asks for it: the title's own teardown, reallocating aligned memory. A host backtrace taken at the throw:

libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f)
<game executable>+0x822e2c

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6031461026 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/948#issuecomment-6031461026). Originally posted by [jeffrey12cali](https://github.com/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 the `atexit` handlers). Exit status was `134` (`SIGABRT`), and it reproduced in every session once the title completed the application-heap handshake: ``` [libc] unhandled exception 'St13runtime_error' thrown from 0x7f6541a2e653 terminate called after throwing an instance of 'std::runtime_error' what(): application heap: allocator has been finalized ``` Who asks for it: the title's own teardown, reallocating **aligned** memory. A host backtrace taken at the throw: ``` libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <game executable>+0x822e2c ``` 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.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#948
No description provided.