fix(libc): reject a malloc_stats_fast block that is not size 0x28 version 1 #11870

Merged
ionut27001 merged 2 commits from upstream-pr/11870 into main 2026-11-08 08:17:06 +00:00
ionut27001 commented 2026-10-27 10:34:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-27T10:34:55Z.


What

With the default allocator, malloc_stats_fast returned 22 for a block size below 0x28, accepted any larger size and any version, and filled the block.

In a payload process on a PS5 (firmware 12.70), malloc_stats_fast exported by libSceLibcInternal.sprx was called with a 0x40-byte buffer filled with 0x5a and the size and version fields set per case. Only size 0x28 with version 1 returned 0 and wrote the four 64-bit fields; every other case returned 1 and left all four fields at 0x5a5a5a5a5a5a5a5a. In every case the reserved bytes 4..8 and the 8 bytes at 0x28 stayed 0x5a.

Measured log (case lines, payload process, firmware 12.70):

case malloc_stats_fast size=0x28 version=1 ret=0 head=280001005a5a5a5a max_system=327680 system=327680 max_inuse=247456 inuse=247456 after_0x28_untouched=1
case malloc_stats_fast size=0x28 version=0 ret=1 head=280000005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1
case malloc_stats_fast size=0x28 version=2 ret=1 head=280002005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1
case malloc_stats_fast size=0x27 version=1 ret=1 head=270001005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1
case malloc_stats_fast size=0x30 version=1 ret=1 head=300001005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1

This change returns 1 without writing anything when the size is not exactly 0x28 or the version is not 1. A null pointer, which returned 22 on main, now throws: it was not probed, as the console would likely fault, so its behaviour is unknown. sceLibcMspaceMallocStatsFast is left alone, as it was not measured. The debt entry now records that the null case throws because it is unmeasured. Ship of Harkinian (PPSA99320), which calls malloc_stats_fast, passes a valid 0x28 / version 1 block, so its behaviour is unchanged.

Overlaps: no open PR changes malloc_stats_fast (searched malloc_stats_fast, MallocStatsFast, GuestHeapStatsFast, malloc stats). #11850 and #11468 also change GuestHeap.cpp (and #11850 docs/dev/debt/libc.md), but not this function; git merge-tree shows no conflict between this head and either. #11846, #11848 and #11333 change the neighbouring lines of docs/dev/debt/libc.md, so whichever PR merges second has a one-line debt conflict to resolve; none of them changes malloc_stats_fast, so there is no dependency and any merge order works. If this merges after them, I will rebase and resolve the debt line here.

Tested

Windows 11, WinLibs GCC 15.2.0, Release, on main 3fea2a937b (the head also merges cleanly onto main 431a5156b9):

  • With this change: application_heap_stats, application_heap_stats_default and application_heap_stats_missing pass twice. The default-allocator test now checks that size 0x28 / version 1 returns 0 and that 0x28 / 0, 0x28 / 2, 0x27 / 1 and 0x30 / 1 return 1 with the 0x5a-filled block byte-identical afterwards; null throws and does not reach the allocator.
  • Revert check: with GuestHeap.cpp from main, application_heap_stats_default aborts (0xc0000409), because main's code returns 0 and fills the block for version 0 and 2 and for size 0x30, and returns 22 for size 0x27. With only the null check put back to return 22, it aborts the same way.

Not tested on Linux, nor with a title.

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 under Silent stubs in their library's technical debt file
  • 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 (debt-line conflict only with #11846, #11848 and #11333; any merge order, the later PR rebases)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5032451852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11870). Originally posted by [ionut27001](https://github.com/ionut27001) on 2026-10-27T10:34:55Z. --- ### What With the default allocator, `malloc_stats_fast` returned 22 for a block size below 0x28, accepted any larger size and any version, and filled the block. In a payload process on a PS5 (firmware 12.70), `malloc_stats_fast` exported by `libSceLibcInternal.sprx` was called with a 0x40-byte buffer filled with 0x5a and the size and version fields set per case. Only size 0x28 with version 1 returned 0 and wrote the four 64-bit fields; every other case returned 1 and left all four fields at 0x5a5a5a5a5a5a5a5a. In every case the reserved bytes 4..8 and the 8 bytes at 0x28 stayed 0x5a. Measured log (case lines, payload process, firmware 12.70): ``` case malloc_stats_fast size=0x28 version=1 ret=0 head=280001005a5a5a5a max_system=327680 system=327680 max_inuse=247456 inuse=247456 after_0x28_untouched=1 case malloc_stats_fast size=0x28 version=0 ret=1 head=280000005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1 case malloc_stats_fast size=0x28 version=2 ret=1 head=280002005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1 case malloc_stats_fast size=0x27 version=1 ret=1 head=270001005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1 case malloc_stats_fast size=0x30 version=1 ret=1 head=300001005a5a5a5a max_system=6510615555426900570 system=6510615555426900570 max_inuse=6510615555426900570 inuse=6510615555426900570 after_0x28_untouched=1 ``` This change returns 1 without writing anything when the size is not exactly 0x28 or the version is not 1. A null pointer, which returned 22 on `main`, now throws: it was not probed, as the console would likely fault, so its behaviour is unknown. `sceLibcMspaceMallocStatsFast` is left alone, as it was not measured. The debt entry now records that the null case throws because it is unmeasured. Ship of Harkinian (PPSA99320), which calls `malloc_stats_fast`, passes a valid 0x28 / version 1 block, so its behaviour is unchanged. Overlaps: no open PR changes `malloc_stats_fast` (searched `malloc_stats_fast`, `MallocStatsFast`, `GuestHeapStatsFast`, `malloc stats`). #11850 and #11468 also change `GuestHeap.cpp` (and #11850 `docs/dev/debt/libc.md`), but not this function; `git merge-tree` shows no conflict between this head and either. #11846, #11848 and #11333 change the neighbouring lines of `docs/dev/debt/libc.md`, so whichever PR merges second has a one-line debt conflict to resolve; none of them changes `malloc_stats_fast`, so there is no dependency and any merge order works. If this merges after them, I will rebase and resolve the debt line here. ### Tested Windows 11, WinLibs GCC 15.2.0, Release, on `main` 3fea2a937b (the head also merges cleanly onto `main` 431a5156b9): - **With this change:** `application_heap_stats`, `application_heap_stats_default` and `application_heap_stats_missing` pass twice. The default-allocator test now checks that size 0x28 / version 1 returns 0 and that 0x28 / 0, 0x28 / 2, 0x27 / 1 and 0x30 / 1 return 1 with the 0x5a-filled block byte-identical afterwards; null throws and does not reach the allocator. - **Revert check:** with `GuestHeap.cpp` from `main`, `application_heap_stats_default` aborts (0xc0000409), because `main`'s code returns 0 and fills the block for version 0 and 2 and for size 0x30, and returns 22 for size 0x27. With only the null check put back to `return 22`, it aborts the same way. Not tested on Linux, nor with a title. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (debt-line conflict only with #11846, #11848 and #11333; any merge order, the later PR rebases) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-27 10:49:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T10:49:25Z.

Review: CHANGES_REQUESTED


The measured size/version cases are implemented and covered well. I built the exact head on Linux with the repository-pinned submodules and ran all three registered application_heap_stats tests; all 3 passed. Hosted eligibility, conventions, intake, labels, report, Ubuntu/Windows relinker, ASan and register-context checks also pass; macOS and write-watch jobs are still queued, and the full Linux product build is skipped by path policy.

One unresolved behavior is explicitly left unmeasured: a null stats pointer. The PR says the console would fault, but the implementation and regression test return 22. That creates a defined EINVAL-like behavior without console evidence, contrary to AGENTS.md's rule to throw and record behavior that is unknown. Please either provide a probe establishing the null behavior and implement it, or leave the unmeasured case as an explicit unsupported path.

The PR also identifies debt-file merge conflicts with open #11846, #11848 and #11333. Please coordinate merge order with those authors and record any required dependency in the checklist as CONTRIBUTING.md asks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5665828302 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11870#pullrequestreview-5665828302). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T10:49:25Z. **Review:** CHANGES_REQUESTED --- The measured size/version cases are implemented and covered well. I built the exact head on Linux with the repository-pinned submodules and ran all three registered application_heap_stats tests; all 3 passed. Hosted eligibility, conventions, intake, labels, report, Ubuntu/Windows relinker, ASan and register-context checks also pass; macOS and write-watch jobs are still queued, and the full Linux product build is skipped by path policy. One unresolved behavior is explicitly left unmeasured: a null stats pointer. The PR says the console would fault, but the implementation and regression test return 22. That creates a defined EINVAL-like behavior without console evidence, contrary to AGENTS.md's rule to throw and record behavior that is unknown. Please either provide a probe establishing the null behavior and implement it, or leave the unmeasured case as an explicit unsupported path. The PR also identifies debt-file merge conflicts with open #11846, #11848 and #11333. Please coordinate merge order with those authors and record any required dependency in the checklist as CONTRIBUTING.md asks.
MarcZX10 commented 2026-10-29 05:18:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:18:40Z.

Review: APPROVED


Re-checked — looks good

The author changed the unmeasured null-pointer path to throw, records it as unsupported debt, and reports exact-head focused tests with the null case and negative controls; the earlier findings are resolved.

What I ran

  • Reviewed exact head 75ab49fb0e23f222c18d0dbdb372de906c01a62e against current main 52c09d567d2fa453494dd720fcf53a98149468e4.
  • Ran git merge-tree --write-tree and git diff --check against current main.
  • Re-read the null guard, allocator calls, boundary test, technical-debt note, latest Windows report, and hosted checks.

Results

Check Result
Current-main merge preview Clean; tree 5c335ee9eea360e05578b5c81a5e933c3477c367
Whitespace check Passed; exit 0
Focused tests Author reports application_heap_stats, application_heap_stats_default, and application_heap_stats_missing pass twice on Windows; the default test covers size/version cases and null throws before reaching the allocator
Negative control Author reports reverting GuestHeap.cpp makes application_heap_stats_default abort; changing only null back to return 22 also aborts
Hosted checks Windows, conventions, eligibility, labels, report, Ubuntu/Windows/macOS relinker, ASan, register-context and write-watch pass; Linux product test job skipped
Raw output
$ git merge-tree --write-tree refs/review/main-20261029b refs/review/pr-11870-20261029
5c335ee9eea360e05578b5c81a5e933c3477c367
exit=0
$ git diff --check refs/review/main-20261029b...refs/review/pr-11870-20261029
(no output)
DIFF_CHECK:0

The focused tests and negative controls above are author-reported, not rerun by me on this head.

Negative control

The PR author reports both revert controls above. I did not independently repeat them on this head.

Findings

Blocking

  • None found.

Non-blocking

  • Linux product tests were skipped. The exact-head focused test and Windows check provide direct coverage of the changed path; the current-main merge preview is clean.

Sources

Not verified

  • I did not rerun the focused Windows tests or remeasure on a console. The tests and title report are author-provided.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693193272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11870#pullrequestreview-5693193272). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T05:18:40Z. **Review:** APPROVED --- # Re-checked — looks good The author changed the unmeasured null-pointer path to throw, records it as unsupported debt, and reports exact-head focused tests with the null case and negative controls; the earlier findings are resolved. ## What I ran - Reviewed exact head `75ab49fb0e23f222c18d0dbdb372de906c01a62e` against current `main` `52c09d567d2fa453494dd720fcf53a98149468e4`. - Ran `git merge-tree --write-tree` and `git diff --check` against current `main`. - Re-read the null guard, allocator calls, boundary test, technical-debt note, latest Windows report, and hosted checks. ## Results | Check | Result | |---|---| | Current-main merge preview | Clean; tree `5c335ee9eea360e05578b5c81a5e933c3477c367` | | Whitespace check | Passed; exit 0 | | Focused tests | Author reports `application_heap_stats`, `application_heap_stats_default`, and `application_heap_stats_missing` pass twice on Windows; the default test covers size/version cases and null throws before reaching the allocator | | Negative control | Author reports reverting `GuestHeap.cpp` makes `application_heap_stats_default` abort; changing only null back to return 22 also aborts | | Hosted checks | Windows, conventions, eligibility, labels, report, Ubuntu/Windows/macOS relinker, ASan, register-context and write-watch pass; Linux product test job skipped | <details> <summary>Raw output</summary> ```text $ git merge-tree --write-tree refs/review/main-20261029b refs/review/pr-11870-20261029 5c335ee9eea360e05578b5c81a5e933c3477c367 exit=0 $ git diff --check refs/review/main-20261029b...refs/review/pr-11870-20261029 (no output) DIFF_CHECK:0 ``` The focused tests and negative controls above are author-reported, not rerun by me on this head. </details> ## Negative control The PR author reports both revert controls above. I did not independently repeat them on this head. ## Findings ### Blocking - None found. ### Non-blocking - Linux product tests were skipped. The exact-head focused test and Windows check provide direct coverage of the changed path; the current-main merge preview is clean. ## Sources - [Implementation at reviewed head](https://github.com/boykopovar/AnyPS5/blob/75ab49fb0e23f222c18d0dbdb372de906c01a62e/core/libs/prx/libc/src/GuestHeap.cpp) - [Focused test at reviewed head](https://github.com/boykopovar/AnyPS5/blob/75ab49fb0e23f222c18d0dbdb372de906c01a62e/core/libs/tests/ApplicationHeapStats.cpp) - [Debt entry at reviewed head](https://github.com/boykopovar/AnyPS5/blob/75ab49fb0e23f222c18d0dbdb372de906c01a62e/docs/dev/debt/libc.md) - [Contribution rules at current main](https://github.com/boykopovar/AnyPS5/blob/52c09d567d2fa453494dd720fcf53a98149468e4/CONTRIBUTING.md#L57-L64) ## Not verified - I did not rerun the focused Windows tests or remeasure on a console. The tests and title report are author-provided.
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!11870
No description provided.