feat(libc): implement malloc_usable_size #3225

Merged
adriadam10 merged 1 commit from upstream-pr/3225 into main 2026-10-11 02:49:42 +00:00
adriadam10 commented 2026-10-11 02:04:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T02:04:40Z.


What

malloc_usable_size (NDcSfcYZRC8, imported from libSceLibcInternal) isn't exported. Since #1529, Linux binds those imports to the system libraries, so a catalog homebrew title stops on it at load on main:

Title Stop on main f1ae427b6
OpenRCT2 Native Port (PPSA99728) ./app.elf: symbol lookup error: ./app.elf: undefined symbol: NDcSfcYZRC8

It goes through the application heap the same way malloc_stats_fast does:

  • The title registered its own allocator: the call goes to entry 9 of the replacement table, the pointer passed through unchanged, null included. Sony's libc.prx (the one Astro's Playroom ships) does the same: its malloc_usable_size is a jump through that table slot. The entry order follows KytyPS5's MallocReplace (malloc, free, calloc, realloc, memalign, reallocalign, posix_memalign, malloc_stats, malloc_stats_fast, malloc_usable_size). If the table leaves that entry null, it throws, as malloc_stats_fast does.
  • The default allocator: null gives 0, as in Sony's default allocator. Any other pointer gives the size the block was requested with, i.e. the range registered for it in GuestAllocations. Sony's allocator reports its rounded-up chunk size instead. Both satisfy "at least the requested size", and handing out bytes past the registered range would let the title write memory that isn't write-watched. TechnicalDebt records this.

Tested

Linux (CachyOS), GCC 16, Release.

  • application_heap_stats, application_heap_stats_default and application_heap_stats_missing pass. The new cases:
    • replacement allocator: the callback gets the pointer, and null, and its value comes back;
    • default allocator: 16-byte and 100000-byte blocks report 16 and 100000, and null reports 0 without calling anything;
    • table without entry 9: both a block and null throw.
  • On main the test doesn't link.
  • Negative checks, each one makes the test fail: not rejecting a null entry, not calling the replacement, the default returning 0, the default returning the raw block size, and null not handled specially.
  • Title stop: catalog release (sha256-checked) relinked for Linux with main's relinker and launched. It stops at load with the line above.
  • tools/check_conventions.py passes.

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:4815926211 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3225). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T02:04:40Z. --- ### What `malloc_usable_size` (`NDcSfcYZRC8`, imported from `libSceLibcInternal`) isn't exported. Since #1529, Linux binds those imports to the system libraries, so a catalog homebrew title stops on it at load on `main`: | Title | Stop on `main` f1ae427b6 | |---|---| | OpenRCT2 Native Port (PPSA99728) | `./app.elf: symbol lookup error: ./app.elf: undefined symbol: NDcSfcYZRC8` | It goes through the application heap the same way `malloc_stats_fast` does: - **The title registered its own allocator:** the call goes to entry 9 of the replacement table, the pointer passed through unchanged, null included. Sony's `libc.prx` (the one Astro's Playroom ships) does the same: its `malloc_usable_size` is a jump through that table slot. The entry order follows KytyPS5's `MallocReplace` (`malloc`, `free`, `calloc`, `realloc`, `memalign`, `reallocalign`, `posix_memalign`, `malloc_stats`, `malloc_stats_fast`, `malloc_usable_size`). If the table leaves that entry null, it throws, as `malloc_stats_fast` does. - **The default allocator:** null gives 0, as in Sony's default allocator. Any other pointer gives the size the block was requested with, i.e. the range registered for it in `GuestAllocations`. Sony's allocator reports its rounded-up chunk size instead. Both satisfy "at least the requested size", and handing out bytes past the registered range would let the title write memory that isn't write-watched. TechnicalDebt records this. ### Tested Linux (CachyOS), GCC 16, Release. - `application_heap_stats`, `application_heap_stats_default` and `application_heap_stats_missing` pass. The new cases: - **replacement allocator:** the callback gets the pointer, and null, and its value comes back; - **default allocator:** 16-byte and 100000-byte blocks report 16 and 100000, and null reports 0 without calling anything; - **table without entry 9:** both a block and null throw. - On `main` the test doesn't link. - Negative checks, each one makes the test fail: not rejecting a null entry, not calling the replacement, the default returning 0, the default returning the raw block size, and null not handled specially. - Title stop: catalog release (sha256-checked) relinked for Linux with `main`'s relinker and launched. It stops at load with the line above. - `tools/check_conventions.py` passes. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-11 02:05:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T02:05:02Z.


Progress report (aac0f1b → 859f2f3)

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

✅ 1 implemented
Library Name
libc malloc_usable_size_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104456418 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3225#issuecomment-6104456418). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T02:05:02Z. --- <!-- progress-report --> ### Progress report (aac0f1b → 859f2f3) 📈 **System libraries**: 86.0% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libc | `malloc_usable_size_nid_postfix` | </details>
adriadam10 commented 2026-10-11 02:41:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T02:41:55Z.


Rebased on main after #3229. The only conflict was the TechnicalDebt line's neighbour; the line moved next to the other heap entries. application_heap_stats* pass again.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104708790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3225#issuecomment-6104708790). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T02:41:55Z. --- Rebased on main after #3229. The only conflict was the TechnicalDebt line's neighbour; the line moved next to the other heap entries. `application_heap_stats*` pass again.
adriadam10 commented 2026-10-11 02:49:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T02:49:55Z.


Merged: Linux CI is red only on agc_unit_shadow, which main has failed since #2864 (reported in #3215). Windows passed, and application_heap_stats* passed on both.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104761737 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3225#issuecomment-6104761737). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T02:49:55Z. --- Merged: Linux CI is red only on `agc_unit_shadow`, which main has failed since #2864 (reported in #3215). Windows passed, and `application_heap_stats*` passed on both.
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!3225
No description provided.