fix(libc): implement mspace heap emptiness query #10397

Closed
MarcZX10 wants to merge 7 commits from upstream-pr/10397 into main
MarcZX10 commented 2026-10-23 21:08:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T21:08:03Z.


What

Implement sceLibcMspaceIsHeapEmpty by checking the arena's tracked live-allocation bytes, and route the PAF variant through the same query. The PS4 Developer Wiki confirms that both symbols exist, but does not document their signatures or behavior. fpPS4's cited lines are generic allocator bitmap helpers and do not implement either export. The valid-handle behavior here is inferred from the function name and remains unverified against PS5; unknown handles set errno to EINVAL and throw because their console behavior is unknown.

Tested

  • The focused Mspace test, compiled with GCC using C++20, -Wall -Wextra -Werror, passed after the follow-up and rebase. It covers empty, active, partially freed, fully freed, null-handle and unknown-handle cases.
  • The same focused test passed with AddressSanitizer and UndefinedBehaviorSanitizer, leak detection enabled, and UBSan configured to halt on error.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check upstream/main...HEAD passed on the rebased head.
  • python3 tools/pr_overlap.py --branch codex/fix-mspace-empty-invalid-handle reports the shared mspace debt paragraph with #10236; this PR depends on #10236 to retain both debt updates. Its uncomparable-file entries for #1060 and #4847 are unrelated AGC changes; neither changes the mspace source, test, or debt files.
  • Full project configuration and hosted product builds were not run. The local full configure is unavailable because the SDL2 submodule source checkout and XRandR development support are missing. No PS5 console run was performed.

Checklist

  • Based on current main; no other open PR implements the same function
  • One topic per PR; follow-up work goes 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: #10236
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4989276800 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10397). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T21:08:03Z. --- ### What Implement `sceLibcMspaceIsHeapEmpty` by checking the arena's tracked live-allocation bytes, and route the PAF variant through the same query. The PS4 Developer Wiki confirms that both symbols exist, but does not document their signatures or behavior. fpPS4's cited lines are generic allocator bitmap helpers and do not implement either export. The valid-handle behavior here is inferred from the function name and remains unverified against PS5; unknown handles set `errno` to `EINVAL` and throw because their console behavior is unknown. ### Tested - The focused `Mspace` test, compiled with GCC using C++20, `-Wall -Wextra -Werror`, passed after the follow-up and rebase. It covers empty, active, partially freed, fully freed, null-handle and unknown-handle cases. - The same focused test passed with AddressSanitizer and UndefinedBehaviorSanitizer, leak detection enabled, and UBSan configured to halt on error. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check upstream/main...HEAD` passed on the rebased head. - `python3 tools/pr_overlap.py --branch codex/fix-mspace-empty-invalid-handle` reports the shared mspace debt paragraph with #10236; this PR depends on #10236 to retain both debt updates. Its uncomparable-file entries for #1060 and #4847 are unrelated AGC changes; neither changes the mspace source, test, or debt files. - Full project configuration and hosted product builds were not run. The local full configure is unavailable because the SDL2 submodule source checkout and XRandR development support are missing. No PS5 console run was performed. ### 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 function - [x] One topic per PR; follow-up work goes 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: #10236 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 01:10:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T01:10:31Z.

Review: COMMENTED


Checked against main 11d6efa06c and red-prig/fpPS4 src/libcinternal/ps4_mspace_internal.pas:

  • fpPS4 does implement this export: _sceLibcMspaceIsHeapEmpty (around lines 3509-3526, registered as $A7351AECA128C9DC in ps4_libscelibcinternal.pas). For a user heap it returns currentInuseSize = 1440, and _sceLibcMspaceCreate sets 1440 as the starting value, so "empty" means no live user allocations. That's what inUse == 0 gives here, since main doesn't count its 64-byte header. It could be cited in the description and the debt entry as the source for the valid-handle behaviour.
  • The same function returns 1 without touching errno for a null handle, a bad magic or a failed lock. That's PS4, so throwing for PS5 is still fine by the rules, but setting errno to EINVAL before the throw, and the test's == 22 check, encode a value nothing supports. Maybe drop the errno write and that assertion, and have the debt entry note the PS4 result instead.
  • Nit: tests/Mspace.cpp declares sceLibcMspaceIsHeapEmpty_nid_postfix and sceLibcPafMspaceIsHeapEmpty_nid_postfix twice.
  • Nit: the new debt entry is missing the (libc) - the other entries have.
  • It still merges cleanly onto main, and the lock and return type look right to me (int 0/1, matching fpPS4's Integer and orbital's int (void* msp)).

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636031418 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10397#pullrequestreview-5636031418). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T01:10:31Z. **Review:** COMMENTED --- Checked against main 11d6efa06c and red-prig/fpPS4 `src/libcinternal/ps4_mspace_internal.pas`: - fpPS4 does implement this export: `_sceLibcMspaceIsHeapEmpty` (around lines 3509-3526, registered as `$A7351AECA128C9DC` in `ps4_libscelibcinternal.pas`). For a user heap it returns `currentInuseSize = 1440`, and `_sceLibcMspaceCreate` sets 1440 as the starting value, so "empty" means no live user allocations. That's what `inUse == 0` gives here, since main doesn't count its 64-byte header. It could be cited in the description and the debt entry as the source for the valid-handle behaviour. - The same function returns 1 without touching errno for a null handle, a bad magic or a failed lock. That's PS4, so throwing for PS5 is still fine by the rules, but setting `errno` to `EINVAL` before the throw, and the test's `== 22` check, encode a value nothing supports. Maybe drop the errno write and that assertion, and have the debt entry note the PS4 result instead. - Nit: `tests/Mspace.cpp` declares `sceLibcMspaceIsHeapEmpty_nid_postfix` and `sceLibcPafMspaceIsHeapEmpty_nid_postfix` twice. - Nit: the new debt entry is missing the `(libc) - ` the other entries have. - It still merges cleanly onto main, and the lock and return type look right to me (`int` 0/1, matching fpPS4's `Integer` and orbital's `int (void* msp)`). Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-11-02 04:57:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:57:18Z.

Review: COMMENTED


Reviewed — two test expectations still open, current test matches production

The two requests on Mspace.cpp are still in head f3b8323, and the production code behaves as the test currently expects in both places.

What I ran

Head f3b832397ae390282cdd107ccba38bdcc9f6370b (live, OPEN). Read-only: gh pr view, gh api GET on comments, git show and git grep at the head. Nothing was built or run.

Results

Check Result Note
Hosted checks 15 success, none failing mergeStateStatus CLEAN
Test lines 135-140 (invalid handles throw) Still present core/libs/tests/Mspace.cpp:135-140
Test lines 148-150 (realloc size 0 frees) Still present core/libs/tests/Mspace.cpp:148-150
Production on unknown handle Throws Mspace.cpp:291
Production on realloc size 0 Releases the used chunk Mspace.cpp:208

Findings

  • oneandonlydean (2026-11-01 10:13Z), request 1: remove the test that requires an unknown handle to throw. The production throw is at Mspace.cpp:291. Removing the test assertion leaves that production behavior unchanged. Your call whether to keep it.
  • oneandonlydean, request 2: free second with sceLibcMspaceFree_nid_postfix instead of sceLibcMspaceRealloc_nid_postfix(arena, second, 0). At head the realloc with size 0 on a used chunk calls Release and returns nullptr (Mspace.cpp:208), so the current test matches the current implementation. The firmware 13.42 measurement the reviewer cites is not in the repository, and I did not verify it.
  • maskym (2026-10-27 23:05Z) reports a Windows run of all 57 registered tests. No question.

Sources

Not verified

  • The firmware 13.42 measurement that says realloc with size 0 leaves the allocation live.
  • The console behavior for unknown handles.
  • The malloc(0) alternative the reviewer suggests (not run).
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732248369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10397#pullrequestreview-5732248369). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T04:57:18Z. **Review:** COMMENTED --- # Reviewed — two test expectations still open, current test matches production The two requests on `Mspace.cpp` are still in head f3b8323, and the production code behaves as the test currently expects in both places. ## What I ran Head `f3b832397ae390282cdd107ccba38bdcc9f6370b` (live, OPEN). Read-only: `gh pr view`, `gh api` GET on comments, `git show` and `git grep` at the head. Nothing was built or run. ## Results | Check | Result | Note | |---|---|---| | Hosted checks | 15 success, none failing | mergeStateStatus CLEAN | | Test lines 135-140 (invalid handles throw) | Still present | `core/libs/tests/Mspace.cpp:135-140` | | Test lines 148-150 (realloc size 0 frees) | Still present | `core/libs/tests/Mspace.cpp:148-150` | | Production on unknown handle | Throws | `Mspace.cpp:291` | | Production on realloc size 0 | Releases the used chunk | `Mspace.cpp:208` | ## Findings - oneandonlydean (2026-11-01 10:13Z), request 1: remove the test that requires an unknown handle to throw. The production throw is at `Mspace.cpp:291`. Removing the test assertion leaves that production behavior unchanged. Your call whether to keep it. - oneandonlydean, request 2: free `second` with `sceLibcMspaceFree_nid_postfix` instead of `sceLibcMspaceRealloc_nid_postfix(arena, second, 0)`. At head the realloc with size 0 on a used chunk calls `Release` and returns nullptr (`Mspace.cpp:208`), so the current test matches the current implementation. The firmware 13.42 measurement the reviewer cites is not in the repository, and I did not verify it. - maskym (2026-10-27 23:05Z) reports a Windows run of all 57 registered tests. No question. ## Sources - https://github.com/boykopovar/AnyPS5/blob/f3b832397ae390282cdd107ccba38bdcc9f6370b/core/libs/tests/Mspace.cpp#L135-L150 - https://github.com/boykopovar/AnyPS5/blob/f3b832397ae390282cdd107ccba38bdcc9f6370b/core/libs/prx/libc/src/Mspace.cpp#L202-L210 - https://github.com/boykopovar/AnyPS5/blob/f3b832397ae390282cdd107ccba38bdcc9f6370b/core/libs/prx/libc/src/Mspace.cpp#L288-L292 ## Not verified - The firmware 13.42 measurement that says realloc with size 0 leaves the allocation live. - The console behavior for unknown handles. - The `malloc(0)` alternative the reviewer suggests (not run).

Pull request closed

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!10397
No description provided.