fix(libc): keep a moved mspace block's usable bytes in realloc #4880

Merged
David-cmrg merged 1 commit from upstream-pr/4880 into main 2026-10-13 02:15:29 +00:00
David-cmrg commented 2026-10-13 01:42:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by David-cmrg on 2026-10-13T01:42:38Z.


What

Fixes #4879.

sceLibcMspaceRealloc and sceLibcMspaceReallocalign copied the size a block was last requested with when they had to move it, not its usable size.

A caller may use every byte sceLibcMspaceMallocUsableSize reports, and ProsperoEden's cache of freed blocks does: it bins blocks by usable size and hands one out for any request up to its bin (headless/heap_arenas.inc). A block first allocated for 8 bytes can therefore carry 32 bytes, and moving it kept only 8. Since #4407 a request of 1 to 16 bytes has 32 usable bytes, which made this common enough to crash ProsperoEden's launcher.

A moved block now keeps its old usable bytes, bounded by the new size, as dlmalloc's mspace_realloc does (chunksize(oldp) - overhead_for(oldp)). The chunk no longer records the requested size, which nothing else read.

mspace gains CheckMovedBlockKeepsUsableBytes: a 1-byte block whose usable bytes are all written keeps them when sceLibcMspaceRealloc or sceLibcMspaceReallocalign moves it.

Tested

Fedora 44, GCC 16.2.1, Release, main 0c3dc9ca2.

  • mspace: with the test change alone it fails 3 of 3; with this change it passes 3 of 3. guest_reallocalign, guest_new_handler_default_heap and the seven application_heap_* tests pass.
  • ProsperoEden (PPSA99008 v1.000.110) on llvmpipe, set up as in #4846 (relinked with --to-intel --trap-sdk-syscall-stubs, with data in the run folder):
    • Without this change: a segmentation fault 5 to 7 s after start, before the launcher closes (2 of 2).
    • With it: the launcher runs and closes (launcher closed: no game (quit)), 2 of 2. The title then stops at exit on #4846.
    • With #4872's change added as well, it exits with code 0 (2 of 2).
  • Not tested on Windows. #4852 reports a crash at the same point there.

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
  • Unblocks: PPSA99008 #4852
  • AI-assisted: yes (Claude Opus 5.5)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4837162992 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4880). Originally posted by [David-cmrg](https://github.com/David-cmrg) on 2026-10-13T01:42:38Z. --- ### What Fixes #4879. `sceLibcMspaceRealloc` and `sceLibcMspaceReallocalign` copied the size a block was last requested with when they had to move it, not its usable size. A caller may use every byte `sceLibcMspaceMallocUsableSize` reports, and ProsperoEden's cache of freed blocks does: it bins blocks by usable size and hands one out for any request up to its bin (`headless/heap_arenas.inc`). A block first allocated for 8 bytes can therefore carry 32 bytes, and moving it kept only 8. Since #4407 a request of 1 to 16 bytes has 32 usable bytes, which made this common enough to crash ProsperoEden's launcher. A moved block now keeps its old usable bytes, bounded by the new size, as dlmalloc's `mspace_realloc` does (`chunksize(oldp) - overhead_for(oldp)`). The chunk no longer records the requested size, which nothing else read. `mspace` gains `CheckMovedBlockKeepsUsableBytes`: a 1-byte block whose usable bytes are all written keeps them when `sceLibcMspaceRealloc` or `sceLibcMspaceReallocalign` moves it. ### Tested Fedora 44, GCC 16.2.1, Release, `main` `0c3dc9ca2`. - `mspace`: with the test change alone it fails 3 of 3; with this change it passes 3 of 3. `guest_reallocalign`, `guest_new_handler_default_heap` and the seven `application_heap_*` tests pass. - ProsperoEden (PPSA99008 v1.000.110) on llvmpipe, set up as in #4846 (relinked with `--to-intel --trap-sdk-syscall-stubs`, with `data` in the run folder): - Without this change: a segmentation fault 5 to 7 s after start, before the launcher closes (2 of 2). - With it: the launcher runs and closes (`launcher closed: no game (quit)`), 2 of 2. The title then stops at exit on #4846. - With #4872's change added as well, it exits with code 0 (2 of 2). - Not tested on Windows. #4852 reports a crash at the same point there. ### 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 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] Unblocks: PPSA99008 #4852 - [x] AI-assisted: yes (Claude Opus 5.5)
Scramblrrrr commented 2026-10-13 02:39:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-13T02:39:23Z.


Windows cross-test of 00c35c159 on the older experimental 007 integration, author-preserving port 93d6507e5edbb9470bcd4f37e87743b4a6d2e618 (parent 2086c5328). mspace, guest_reallocalign and all seven application_heap_* checks pass: 9/9, restored build 0.18 s. This older integration does not register guest_new_handler_default_heap, so that test is not included.

Production-only negative: retained CheckMovedBlockKeepsUsableBytes, restored only Mspace.cpp from parent 2086c5328, rebuilt. mspace fails at the usable-byte check; the eight controls pass. Restored production, rebuilt and reran all nine successfully. Production code applied cleanly; test conflict resolved keeping the new fixture without importing unrelated newer allocator-alignment fixtures.

Separate 007 game comparison is now running with only libc changed, patched SHA256 DE052483D75B672DEFCF472C9CFDE0EBEC85723E318C4D3FFB81396489FD6C00; graphics remains FCB1B8FB... at 2086c5328. No claim yet that the title uses the affected allocator case, or that this fixes its renderer crash. No title code or captures uploaded. AI-assisted (Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136997522 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4880#issuecomment-6136997522). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-13T02:39:23Z. --- Windows cross-test of 00c35c159 on the older experimental 007 integration, author-preserving port 93d6507e5edbb9470bcd4f37e87743b4a6d2e618 (parent 2086c5328). mspace, guest_reallocalign and all seven application_heap_* checks pass: 9/9, restored build 0.18 s. This older integration does not register guest_new_handler_default_heap, so that test is not included. Production-only negative: retained CheckMovedBlockKeepsUsableBytes, restored only Mspace.cpp from parent 2086c5328, rebuilt. mspace fails at the usable-byte check; the eight controls pass. Restored production, rebuilt and reran all nine successfully. Production code applied cleanly; test conflict resolved keeping the new fixture without importing unrelated newer allocator-alignment fixtures. Separate 007 game comparison is now running with only libc changed, patched SHA256 DE052483D75B672DEFCF472C9CFDE0EBEC85723E318C4D3FFB81396489FD6C00; graphics remains FCB1B8FB... at 2086c5328. No claim yet that the title uses the affected allocator case, or that this fixes its renderer crash. No title code or captures uploaded. AI-assisted (Codex).
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!4880
No description provided.