fix(libc): align mspace allocations to 32 bytes #3639

Merged
reyani-dev merged 1 commit from upstream-pr/3639 into main 2026-10-11 21:44:19 +00:00
reyani-dev commented 2026-10-11 14:34:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T14:34:13Z.


What

sceLibcMspaceMalloc and the rest of the mspace allocator aligned blocks to 16 bytes. Clang's PS4/PS5 targets set the default new alignment and the suitable alignment to 32 bytes (PSOSTargetInfo: NewAlign = 256, SuitableAlign = 256). So PS5 code may store to a fresh block with 32-byte-aligned AVX instructions. The guest heap has guaranteed 32 bytes since #1621; this does the same for mspaces. Allocate now aligns every block to at least 32 bytes, which covers malloc, calloc, realloc moves, and memalign/posix_memalign/reallocalign with smaller alignments. Block sizes still round to 16 bytes.

Title: ProsperoLichess (PPSA99009) on main a11c8f7f faults right after loading its puzzle pack, in its own log (download0/prosperolichess/app.log):

FATAL: unhandled exception 0xc0000005 on thread 7508 ''
  rip 0x00007ff7aa9e1d0c app.exe+0xe1d0c
  read of 0xffffffffffffffff
  rax 00000002015c65f0 ...

At guest 0xd1d0c the title initialises a function-local static new T (40 bytes) with vmovaps %ymm0,(%rax). Its operator new calls sceLibcMspaceMalloc on a 128 MiB mmap heap made with sceLibcMspaceCreate, and the block came back at ...5f0, 16 bytes past a 32-byte boundary (the #GP is reported as a read of -1). With this change that fault is gone, and the title runs on to its next stop:

  what():  sysctl_nid_postfix: unsupported MIB 1.37

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, on main a11c8f7f:

  • New CheckAlignment in mspace (arena base 16 bytes past a 32-byte boundary; malloc 1 to 64 bytes, calloc, realloc growth, memalign 16, reallocalign 8, posix_memalign 8). It fails with the source change reverted and passes with it. The existing mspace checks still pass.
  • ctest -E "^agc_|video_out": 450 of 453 pass. The 3 failures also fail on main on Windows: windows_exceptions (#3411), guest_kernel_sys (symlink without Developer Mode, #3510) and guest_filesystem (directory time, #3518).
  • ProsperoLichess (PPSA99009): log above, before and after.
  • tools/check_conventions.py and tools/pr_overlap.py: clean.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819812570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3639). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T14:34:13Z. --- ### What `sceLibcMspaceMalloc` and the rest of the mspace allocator aligned blocks to 16 bytes. Clang's PS4/PS5 targets set the default new alignment and the suitable alignment to 32 bytes ([`PSOSTargetInfo`](https://github.com/llvm/llvm-project/blob/main/clang/lib/Basic/Targets/OSTargets.h): `NewAlign = 256`, `SuitableAlign = 256`). So PS5 code may store to a fresh block with 32-byte-aligned AVX instructions. The guest heap has guaranteed 32 bytes since #1621; this does the same for mspaces. `Allocate` now aligns every block to at least 32 bytes, which covers malloc, calloc, realloc moves, and memalign/posix_memalign/reallocalign with smaller alignments. Block sizes still round to 16 bytes. **Title:** ProsperoLichess (PPSA99009) on `main` a11c8f7f faults right after loading its puzzle pack, in its own log (`download0/prosperolichess/app.log`): ``` FATAL: unhandled exception 0xc0000005 on thread 7508 '' rip 0x00007ff7aa9e1d0c app.exe+0xe1d0c read of 0xffffffffffffffff rax 00000002015c65f0 ... ``` At guest `0xd1d0c` the title initialises a function-local static `new T` (40 bytes) with `vmovaps %ymm0,(%rax)`. Its `operator new` calls `sceLibcMspaceMalloc` on a 128 MiB `mmap` heap made with `sceLibcMspaceCreate`, and the block came back at `...5f0`, 16 bytes past a 32-byte boundary (the #GP is reported as a read of -1). With this change that fault is gone, and the title runs on to its next stop: ``` what(): sysctl_nid_postfix: unsupported MIB 1.37 ``` ### Tested Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, on `main` a11c8f7f: - New `CheckAlignment` in `mspace` (arena base 16 bytes past a 32-byte boundary; malloc 1 to 64 bytes, calloc, realloc growth, memalign 16, reallocalign 8, posix_memalign 8). It fails with the source change reverted and passes with it. The existing mspace checks still pass. - `ctest -E "^agc_|video_out"`: 450 of 453 pass. The 3 failures also fail on `main` on Windows: `windows_exceptions` (#3411), `guest_kernel_sys` (symlink without Developer Mode, #3510) and `guest_filesystem` (directory time, #3518). - ProsperoLichess (PPSA99009): log above, before and after. - `tools/check_conventions.py` and `tools/pr_overlap.py`: clean. ### 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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
reyani-dev commented 2026-10-11 15:57:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T15:57:53Z.


A second title has the same fault. ProsperoRadio (PPSA99001) on main a11c8f7f, in download0/prosperoradio.log:

FATAL: unhandled exception 0xc0000005 on thread 40576 ''
  rip 0x00007ff7ba00d02d app.exe+0x1d02d
  read of 0xffffffffffffffff
  rax 0000000200db98d0 ...

At guest 0xd02d it calls operator new(0x100) and clears the block with vmovaps %ymm0,(%rax); rax is 16 bytes past a 32-byte boundary. With this branch, the fault is gone and the title runs to its next stop:

  what():  poll: descriptor 3 is not a socket
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110865941 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3639#issuecomment-6110865941). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T15:57:53Z. --- A second title has the same fault. ProsperoRadio (PPSA99001) on `main` a11c8f7f, in `download0/prosperoradio.log`: ``` FATAL: unhandled exception 0xc0000005 on thread 40576 '' rip 0x00007ff7ba00d02d app.exe+0x1d02d read of 0xffffffffffffffff rax 0000000200db98d0 ... ``` At guest `0xd02d` it calls `operator new(0x100)` and clears the block with `vmovaps %ymm0,(%rax)`; `rax` is 16 bytes past a 32-byte boundary. With this branch, the fault is gone and the title runs to its next stop: ``` what(): poll: descriptor 3 is not a socket ```
adriadam10 commented 2026-10-11 21:44:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:44:15Z.


Checked the alignment change in Allocate (all entry points go through it) and the new CheckAlignment test: it fails with Mspace.cpp reverted and passes with the change; mspace and the rest of the libs ctest scope pass on Linux. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114118771 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3639#issuecomment-6114118771). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:44:15Z. --- Checked the alignment change in `Allocate` (all entry points go through it) and the new `CheckAlignment` test: it fails with `Mspace.cpp` reverted and passes with the change; `mspace` and the rest of the libs ctest scope pass on Linux. Merging.
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!3639
No description provided.