fix(libc): return an aligned empty block from reallocalign to 0 bytes #11850

Merged
ionut27001 merged 1 commit from upstream-pr/11850 into main 2026-11-02 12:27:18 +00:00
ionut27001 commented 2026-10-27 09:51:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-27T09:51:08Z.


What

With the default heap, reallocalign(ptr, 0, alignment) threw std::invalid_argument when the block did not already have that alignment. Only the already-aligned case was handled.

The evidence is the console log attached to the merged #11259 (gist, preallign.console.log), taken in a payload process on a PS5 (firmware 13.60) with reallocalign from libSceLibcInternal.sprx. A block from that heap that was 64-aligned but not 256-aligned, given size 0 and alignment 256, came back as a different pointer, 256-aligned, with malloc_usable_size 0 and errno still 1234:

case pick tries=2 found=1 mod256=64 align=64 usable=24
case realign_0_256 ret=ptr same=0 mod256=0 align=256 errno=1234 usable=0

This change returns a new empty block with the requested alignment in that case, as the console did. The old block stays allocated: the log does not show whether the console frees it, and leaving it is safe where freeing it would not be if the console keeps it. Alignment 0 or one that is not a power of two still throws in this path; reallocalign in FileAndHeap.cpp rounds the default heap's alignment up to a power of two of at least 32 before it gets here. The debt entry is cut to the open gap: the old block is left allocated because the console's handling of it is not known.

Overlaps: no open PR mentions reallocalign. #11468 changes other lines of GuestHeapRealign_nid_postfix in GuestHeap.cpp; git merge-tree shows no conflict with this head.

Tested

Windows 11, WinLibs GCC 15.2.0, Release, on main 801d5b9bc1:

  • With this change: guest_reallocalign passes twice. It now takes a block from reallocalign(NULL, 24, 64) that is not 256-aligned and checks that reallocalign(block, 0, 256) returns a different, 256-aligned block with 0 usable bytes and errno unchanged.
  • Revert check: with GuestHeap.cpp from main, the test aborts (0xc0000409), because that call throws.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5031890884 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11850). Originally posted by [ionut27001](https://github.com/ionut27001) on 2026-10-27T09:51:08Z. --- ### What With the default heap, `reallocalign(ptr, 0, alignment)` threw `std::invalid_argument` when the block did not already have that alignment. Only the already-aligned case was handled. The evidence is the console log attached to the merged #11259 ([gist](https://gist.github.com/orezraey/c130db2e3ddb36e1cdb2008f2caa2b17), `preallign.console.log`), taken in a payload process on a PS5 (firmware 13.60) with `reallocalign` from `libSceLibcInternal.sprx`. A block from that heap that was 64-aligned but not 256-aligned, given size 0 and alignment 256, came back as a different pointer, 256-aligned, with `malloc_usable_size` 0 and `errno` still 1234: ``` case pick tries=2 found=1 mod256=64 align=64 usable=24 case realign_0_256 ret=ptr same=0 mod256=0 align=256 errno=1234 usable=0 ``` This change returns a new empty block with the requested alignment in that case, as the console did. The old block stays allocated: the log does not show whether the console frees it, and leaving it is safe where freeing it would not be if the console keeps it. Alignment 0 or one that is not a power of two still throws in this path; `reallocalign` in `FileAndHeap.cpp` rounds the default heap's alignment up to a power of two of at least 32 before it gets here. The debt entry is cut to the open gap: the old block is left allocated because the console's handling of it is not known. Overlaps: no open PR mentions `reallocalign`. #11468 changes other lines of `GuestHeapRealign_nid_postfix` in `GuestHeap.cpp`; `git merge-tree` shows no conflict with this head. ### Tested Windows 11, WinLibs GCC 15.2.0, Release, on `main` 801d5b9bc1: - **With this change:** `guest_reallocalign` passes twice. It now takes a block from `reallocalign(NULL, 24, 64)` that is not 256-aligned and checks that `reallocalign(block, 0, 256)` returns a different, 256-aligned block with 0 usable bytes and `errno` unchanged. - **Revert check:** with `GuestHeap.cpp` from `main`, the test aborts (0xc0000409), because that call throws. 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!11850
No description provided.