fix(libc): align guest heap allocations to 32 bytes #1621

Merged
TheAggie merged 1 commit from upstream-pr/1621 into main 2026-10-10 01:41:57 +00:00
TheAggie commented 2026-10-08 15:05:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by TheAggie on 2026-10-08T15:05:35Z.


What

Clang's PS4/PS5 target sets the default new alignment and the suitable alignment to 32 bytes: PSOSTargetInfo in clang/lib/Basic/Targets/OSTargets.h has NewAlign = 256 and SuitableAlign = 256 (bits). So __STDCPP_DEFAULT_NEW_ALIGNMENT__ and alignof(max_align_t) are 32 on the console, and PS5 code may store to a fresh malloc or plain operator new result with 32-byte-aligned AVX instructions.

The guest heap aligned blocks to 16 bytes. Worse, the arena's size classes are multiples of 32 bytes on page-aligned spans, so after the 16-byte header every small block landed exactly 16 bytes past a 32-byte boundary. ccleste (PPSA59322, darkxex/PS5_Celeste) faults on its first such store: operator new(40) returns 0x200104f90 from malloc, and the next instruction is vmovaps %ymm0,(%rax).

  • GuestHeap: the minimum alignment is 32 bytes. The padding is now sized from the 16-byte alignment the raw blocks guarantee (RawAlignment), so a 32-byte-aligned pointer always fits its block.
  • guest_memory test (CheckHeapAlignment):
    • Allocates every size from 1 to 600 bytes, three large sizes and a grown reallocation.
    • Requires 32-byte alignment.
    • Fills each block completely, then checks that every block's contents survived, to catch overrun into a neighbour.
    • It fails without the change.

Tested

Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, NVIDIA GeForce RTX 5070 Ti.

  • The full suite (456 tests) passes, run from the generated CTestTestfile.cmake commands because antivirus blocks ctest.exe on this machine. That run was on a local branch that also carries my other pending changes; every guest_* test also passes on this branch alone.
  • ccleste gets past the fault. It still needs other pending changes to render.

Not tested on Linux.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4788802998 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1621). Originally posted by [TheAggie](https://github.com/TheAggie) on 2026-10-08T15:05:35Z. --- ### What Clang's PS4/PS5 target sets the default new alignment and the suitable alignment to 32 bytes: `PSOSTargetInfo` in `clang/lib/Basic/Targets/OSTargets.h` has `NewAlign = 256` and `SuitableAlign = 256` (bits). So `__STDCPP_DEFAULT_NEW_ALIGNMENT__` and `alignof(max_align_t)` are 32 on the console, and PS5 code may store to a fresh `malloc` or plain `operator new` result with 32-byte-aligned AVX instructions. The guest heap aligned blocks to 16 bytes. Worse, the arena's size classes are multiples of 32 bytes on page-aligned spans, so after the 16-byte header every small block landed exactly 16 bytes past a 32-byte boundary. ccleste (PPSA59322, [darkxex/PS5_Celeste](https://github.com/darkxex/PS5_Celeste)) faults on its first such store: `operator new(40)` returns `0x200104f90` from `malloc`, and the next instruction is `vmovaps %ymm0,(%rax)`. - `GuestHeap`: the minimum alignment is 32 bytes. The padding is now sized from the 16-byte alignment the raw blocks guarantee (`RawAlignment`), so a 32-byte-aligned pointer always fits its block. - `guest_memory` test (`CheckHeapAlignment`): - Allocates every size from 1 to 600 bytes, three large sizes and a grown reallocation. - Requires 32-byte alignment. - Fills each block completely, then checks that every block's contents survived, to catch overrun into a neighbour. - It fails without the change. ### Tested Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, NVIDIA GeForce RTX 5070 Ti. - The full suite (456 tests) passes, run from the generated `CTestTestfile.cmake` commands because antivirus blocks `ctest.exe` on this machine. That run was on a local branch that also carries my other pending changes; every `guest_*` test also passes on this branch alone. - ccleste gets past the fault. It still needs other pending changes to render. Not tested on Linux. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!1621
No description provided.