fix(libc): register the application heap finalizer when libc loads #1001

Merged
Dextroid17 merged 1 commit from upstream-pr/1001 into main 2026-10-07 02:19:33 +00:00
Dextroid17 commented 2026-10-06 22:40:22 +00:00 (Migrated from github.com)

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::atexit runs handlers in reverse registration order. The finalizer was registered from ApplicationHeapInitialize_nid_no_patch, which runs after libc's own __cxa_atexit runner (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 threw application heap: allocator has been finalized out of a destructor path, which terminates the process.

The application heap: allocator has been finalized check in callback() 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:

libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f)
<game executable>(+0x822e2c)   # 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

  • Linux, CachyOS (Arch), GCC 16.2.1, Release.
  • application_heap_exit_order (new): registers a guest exit callback through atexit_nid_postfix that allocates through the heap, then runs the handshake. On the previous ApplicationHeap.cpp it 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 unchanged ApplicationHeap.cpp.
  • Full suite: ctest --test-dir build -j 8 --timeout 120 → 368/368 pass.
  • Not tested by me on a title; the title measurement above is @jeffrey12cali's.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source — not applicable
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4765440385 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1001). Originally posted by [Dextroid17](https://github.com/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::atexit` runs handlers in reverse registration order. The finalizer was registered from `ApplicationHeapInitialize_nid_no_patch`, which runs after libc's own `__cxa_atexit` runner (`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 threw `application heap: allocator has been finalized` out of a destructor path, which terminates the process. The `application heap: allocator has been finalized` check in `callback()` 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: ``` libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <game executable>(+0x822e2c) # 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 - Linux, CachyOS (Arch), GCC 16.2.1, Release. - `application_heap_exit_order` (new): registers a guest exit callback through `atexit_nid_postfix` that allocates through the heap, then runs the handshake. On the previous `ApplicationHeap.cpp` it 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 unchanged `ApplicationHeap.cpp`. - Full suite: `ctest --test-dir build -j 8 --timeout 120` → 368/368 pass. - Not tested by me on a title; the title measurement above is @jeffrey12cali's. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source — not applicable - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
jeffrey12cali commented 2026-10-07 05:16:23 +00:00 (Migrated from github.com)

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):

  • Title: PPSA28997.
  • Symptom before this PR, on a clean exit (SIGTERM, exit status 134), reproducible every session:
[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
  • Caller: the game's own teardown reallocating aligned memory - host backtrace at the throw libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <- <game executable>+0x822e2c.

Suggestion for the test, for whoever touches that file next: application_heap_exit_order covers an allocation through the heap; adding a Realign/Reallocate case alongside it costs one line and matches how the title actually tripped.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6031463266 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1001#issuecomment-6031463266). Originally posted by [jeffrey12cali](https://github.com/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): - Title: **PPSA28997**. - Symptom before this PR, on a clean exit (`SIGTERM`, exit status `134`), reproducible every session: ``` [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 ``` - Caller: the game's own teardown reallocating **aligned** memory - host backtrace at the throw `libc.prx(ApplicationHeapRealign_nid_no_patch+0x16f) <- <game executable>+0x822e2c`. Suggestion for the test, for whoever touches that file next: `application_heap_exit_order` covers an allocation through the heap; adding a `Realign`/`Reallocate` case alongside it costs one line and matches how the title actually tripped.
Sign in to join this conversation.
No reviewers
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!1001
No description provided.