feat(libc): implement malloc_stats_fast through the application allocator #2039

Merged
Aryansh1520 merged 3 commits from upstream-pr/2039 into main 2026-10-09 21:58:31 +00:00
Aryansh1520 commented 2026-10-09 05:36:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T05:36:57Z.


What

A title that replaces the allocator through its libc parameters also supplies malloc_stats_fast in that table, at the entry already recorded in MallocReplace. PPSA28997 (God of War Sons of Sparta) calls it. malloc_stats_fast is now exported and forwarded to the title's function, under the same checks and callback scope as the other application-heap calls. Without a replacement allocator that provides it, the call throws.

Tested

  • Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls.
  • tools/check_conventions.py --base origin/main and tools/pr_overlap.py --branch pass.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796456428 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T05:36:57Z. --- ### What A title that replaces the allocator through its libc parameters also supplies `malloc_stats_fast` in that table, at the entry already recorded in `MallocReplace`. PPSA28997 (God of War Sons of Sparta) calls it. `malloc_stats_fast` is now exported and forwarded to the title's function, under the same checks and callback scope as the other application-heap calls. Without a replacement allocator that provides it, the call throws. ### Tested - Linux (CachyOS, kernel 7.2), GCC: PPSA28997 (God of War Sons of Sparta) runs past the calls. - `tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py --branch` pass. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
MarcZX10 commented 2026-10-09 05:57:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T05:57:53Z.


The callback path follows the existing heap lock and callback-scope pattern. Please add coverage for forwarding a supplied slot-8 callback and for the missing-allocator/missing-slot error paths; these are the new behavior and currently have no regression test. GitHub reports no checks on this head, so please get the relevant CI jobs running before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075239954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6075239954). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T05:57:53Z. --- The callback path follows the existing heap lock and callback-scope pattern. Please add coverage for forwarding a supplied slot-8 callback and for the missing-allocator/missing-slot error paths; these are the new behavior and currently have no regression test. GitHub reports no checks on this head, so please get the relevant CI jobs running before merge.
oneandonlydean commented 2026-10-09 09:22:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:22:54Z.


Same as #2038: the log line where PPSA28997 stops on main at malloc_stats_fast, please.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078129930 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6078129930). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:22:54Z. --- Same as #2038: the log line where PPSA28997 stops on main at `malloc_stats_fast`, please.
Aryansh1520 commented 2026-10-09 10:36:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T10:36:55Z.


Added coverage in bdeef942 (application_heap_tests):

  • forwarding: a replacement table with a slot-8 callback. malloc_stats_fast passes the caller's pointer through, including null, and returns the callback's result.
  • callback scope: a slot-8 callback that calls back into the heap is rejected as a recursive allocator callback.
  • missing allocator: malloc_stats_fast before any allocator is registered throws.
  • missing slot: it throws with the default host allocator (application_heap_default) and with a replacement allocator whose slot 8 is null (new application_heap_missing_stats). In the second case the other heap calls still work and no callback runs.

Removing the forwarding, the missing-slot check or the callback scope each makes a test fail.

Where PPSA28997 stops: malloc_stats_fast is imported by eboot.elf. When it was a not-implemented stub in my local build, the title aborted during startup:

[libc] unhandled exception 'St13runtime_error' thrown from 0x7fcc74250769
terminate called after throwing an instance of 'std::runtime_error'
  what():  malloc_stats_fast not implemented

Main doesn't export it at all (KuOuD58hqn4). On main (as of Oct 8) the title stops earlier, at LTov9gMEqCU (std::ios_base::_Addstd, #545).

The build workflows on this head are waiting for approval to run (fork PR).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079203126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6079203126). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T10:36:55Z. --- Added coverage in bdeef942 (`application_heap_tests`): - forwarding: a replacement table with a slot-8 callback. `malloc_stats_fast` passes the caller's pointer through, including null, and returns the callback's result. - callback scope: a slot-8 callback that calls back into the heap is rejected as a recursive allocator callback. - missing allocator: `malloc_stats_fast` before any allocator is registered throws. - missing slot: it throws with the default host allocator (`application_heap_default`) and with a replacement allocator whose slot 8 is null (new `application_heap_missing_stats`). In the second case the other heap calls still work and no callback runs. Removing the forwarding, the missing-slot check or the callback scope each makes a test fail. Where PPSA28997 stops: `malloc_stats_fast` is imported by `eboot.elf`. When it was a not-implemented stub in my local build, the title aborted during startup: ``` [libc] unhandled exception 'St13runtime_error' thrown from 0x7fcc74250769 terminate called after throwing an instance of 'std::runtime_error' what(): malloc_stats_fast not implemented ``` Main doesn't export it at all (`KuOuD58hqn4`). On main (as of Oct 8) the title stops earlier, at `LTov9gMEqCU` (`std::ios_base::_Addstd`, #545). The build workflows on this head are waiting for approval to run (fork PR).
Aryansh1520 commented 2026-10-09 10:53:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryansh1520 on 2026-10-09T10:53:17Z.


Moved the malloc_stats_fast checks out of ApplicationHeap.cpp into their own test, ApplicationHeapStats.cpp, in 7fc6fac1. It runs in three modes: application_heap_stats, application_heap_stats_default and application_heap_stats_missing. This PR no longer touches the file #1900 changes. The coverage is the same as before: forwarding, the nested-call rejection, no allocator, the default allocator and a missing slot 8. Removing the forwarding, the slot check or the callback scope still fails a test. I also checked that the PR merges cleanly with #1900 and that malloc_stats_fast only uses state #1900 keeps.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079440055 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6079440055). Originally posted by [Aryansh1520](https://github.com/Aryansh1520) on 2026-10-09T10:53:17Z. --- Moved the `malloc_stats_fast` checks out of `ApplicationHeap.cpp` into their own test, `ApplicationHeapStats.cpp`, in 7fc6fac1. It runs in three modes: `application_heap_stats`, `application_heap_stats_default` and `application_heap_stats_missing`. This PR no longer touches the file #1900 changes. The coverage is the same as before: forwarding, the nested-call rejection, no allocator, the default allocator and a missing slot 8. Removing the forwarding, the slot check or the callback scope still fails a test. I also checked that the PR merges cleanly with #1900 and that `malloc_stats_fast` only uses state #1900 keeps.
popcoder565 commented 2026-10-09 16:07:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T16:07:04Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 7fc6fac1, merged onto main d70b8998 together with #1324, #2193 and #2247.

  • This PR's tests pass: application_heap_stats, application_heap_stats_default, application_heap_stats_missing.
  • #2193 adds a stub, sceLibcUnknown_BnMAMrsfVWo, under the NID that malloc_stats_fast patches to. With both merged, libc.prx fails with duplicate patched export name: BnMAMrsfVWo; I've noted it on #2193.
  • With that stub dropped locally (and #2193's Windows build error worked around), the full suite passes in this build (512 of 512).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084617196 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6084617196). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T16:07:04Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `7fc6fac1`, merged onto `main` d70b8998 together with #1324, #2193 and #2247. - This PR's tests pass: `application_heap_stats`, `application_heap_stats_default`, `application_heap_stats_missing`. - #2193 adds a stub, `sceLibcUnknown_BnMAMrsfVWo`, under the NID that `malloc_stats_fast` patches to. With both merged, `libc.prx` fails with `duplicate patched export name: BnMAMrsfVWo`; I've noted it on #2193. - With that stub dropped locally (and #2193's Windows build error worked around), the full suite passes in this build (512 of 512). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
adriadam10 commented 2026-10-09 21:58:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T21:58:28Z.


Checked on Linux (GCC), merged onto current main: build + ctest green (532/532); the three application_heap_stats* tests pass and do not link without the change. Slot 8 of MallocReplace matches malloc_stats_fast in KytyPS5's replace table, and the checks and callback scope match the other application-heap calls. Windows run by @popcoder565 also passes. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089945453 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6089945453). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T21:58:28Z. --- Checked on Linux (GCC), merged onto current main: build + ctest green (532/532); the three `application_heap_stats*` tests pass and do not link without the change. Slot 8 of `MallocReplace` matches `malloc_stats_fast` in KytyPS5's replace table, and the checks and callback scope match the other application-heap calls. Windows run by @popcoder565 also passes. Merging.
github-actions[bot] commented 2026-10-09 22:16:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:16:02Z.


Progress report (d70b899 → 7fc6fac)

📈 System libraries: 84.7% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libc malloc_stats_fast_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090157954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2039#issuecomment-6090157954). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:16:02Z. --- <!-- progress-report --> ### Progress report (d70b899 → 7fc6fac) 📈 **System libraries**: 84.7% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `malloc_stats_fast_nid_postfix` | </details>
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!2039
No description provided.