fix(libc): reject overflowing mspace resizes #1110

Merged
Sanjaysrinivas merged 1 commit from upstream-pr/1110 into main 2026-10-08 06:46:43 +00:00
Sanjaysrinivas commented 2026-10-07 12:47:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Sanjaysrinivas on 2026-10-07T12:47:53Z.


What

sceLibcMspaceRealloc and sceLibcMspaceReallocalign round sizes to 16 bytes before checking whether an existing chunk can hold them. Near SIZE_MAX, that rounding wraps to zero: both exports return the original pointer as success and replace its requested-size metadata with a value the allocation cannot hold. A later relocating resize uses that metadata as a copy length.

Reject rounding overflow with guest ENOMEM before either fast path updates the chunk. This follows the overflow check already present in Allocate, without changing normal allocation, zero-size or alignment handling.

Extend the existing Mspace regression to reject SIZE_MAX through SIZE_MAX - 15 for both resize exports, checking errno, original contents, usable capacity and usage/peak statistics. Its existing relocating growth immediately afterward also verifies the allocation remains usable.

Tested

Earlier focused validation of the unchanged contribution patch:

Ubuntu 24.04 under WSL2, GCC 13.3.0, x86-64:

  • Against the original full libc, a focused maximum-size check shows both resize exports returning success; the extended Mspace regression fails.
  • Complete libc built using its repository CMake source list in a local, untracked harness. mspace and guest_reallocalign pass three consecutive runs each. Five neighboring native save tests also pass.
  • The actual modified Mspace.cpp and regression pass with AddressSanitizer and UndefinedBehaviorSanitizer, -Wall -Wextra -Werror -pthread, and a local thread-local errno provider.
  • git diff --check passes.

With the full repository configured and built with BUILD_TESTING=ON:

ctest --test-dir build -R '^(mspace|guest_reallocalign)$' --output-on-failure

No affected retail title or exploitation is claimed. Local validation helpers and investigation notes are excluded from the patch. Full-build results and remaining platform limits are recorded below.

PR overlap (earlier checks)

Direct per-file three-way merge checks confirm a conflict with the whole-tree formatting PR #1128 in core/libs/prx/libc/src/Mspace.cpp. That PR does not implement this bug fix. #1128 currently conflicts with upstream in StdioExtras.cpp, so the repository overlap tool skips its pairwise conflict check; its empty result does not clear this overlap. This PR merges cleanly into upstream 1e5bb880; merging it and #1128 will require rebasing whichever lands second.

Earlier local validation after rebase

Rebased onto upstream main at cea1ef13. Tested head: bc1f7e694e02f4bd63fa718295860a71dfe7a07b.
Ubuntu 24.04 under WSL2, GCC 13.3, Release; the contribution patch is preserved and its tree matches the checked merge result.

  • Repository conventions and git diff --check passed.
  • Full project CMake build passed with BUILD_TESTING=ON.
  • Focused CTest regression selection: 6 passed, 0 skipped, 0 failed (6 registered).

Title/homebrew execution remains untested. The GitHub-hosted fork checks below are separate from these local results.

Independent fork CI

Build run 37676602286 passed on exact PR head 131e05a26bea1775cbb990224501aa255c7389d7.
Rebased onto upstream main at 4764e59c37b1fc6c89284fdecaef5a5718b6119e. The unchanged contribution's source tree matches the checked automatic merge result; conventions and git diff --check passed.
Both jobs completed the full Release CMake build with BUILD_TESTING=ON, the libs target and the full CTest suite with --timeout 120 --repeat until-fail:3.

  • Linux: Ubuntu 24.04, GCC 13.3, lavapipe: 379 passed, 21 skipped, 0 failed (400 registered).
  • Native Windows: WinLibs GCC 15.2: 239 passed, 156 skipped, 0 failed (395 registered).
  • The newly merged upstream guest_force_tls_destructor regression passed in all three Linux repetitions.

These fork checks do not replace upstream workflow approval or maintainer review. No title/homebrew execution or RDNA hardware measurement is claimed.

Checklist

  • Based on current main at 4764e59c; no other open PR implements the same fix
  • One topic per PR; follow-ups go in a new PR
  • No comments added to code
  • No unimplemented paths or silent stubs added
  • No investigation notes or images added to the tracked repository
  • No new third-party code
  • Generic behavior, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4772852996 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1110). Originally posted by [Sanjaysrinivas](https://github.com/Sanjaysrinivas) on 2026-10-07T12:47:53Z. --- ### What `sceLibcMspaceRealloc` and `sceLibcMspaceReallocalign` round sizes to 16 bytes before checking whether an existing chunk can hold them. Near `SIZE_MAX`, that rounding wraps to zero: both exports return the original pointer as success and replace its requested-size metadata with a value the allocation cannot hold. A later relocating resize uses that metadata as a copy length. Reject rounding overflow with guest ENOMEM before either fast path updates the chunk. This follows the overflow check already present in `Allocate`, without changing normal allocation, zero-size or alignment handling. Extend the existing Mspace regression to reject `SIZE_MAX` through `SIZE_MAX - 15` for both resize exports, checking errno, original contents, usable capacity and usage/peak statistics. Its existing relocating growth immediately afterward also verifies the allocation remains usable. ### Tested Earlier focused validation of the unchanged contribution patch: Ubuntu 24.04 under WSL2, GCC 13.3.0, x86-64: - Against the original full libc, a focused maximum-size check shows both resize exports returning success; the extended Mspace regression fails. - Complete libc built using its repository CMake source list in a local, untracked harness. `mspace` and `guest_reallocalign` pass three consecutive runs each. Five neighboring native save tests also pass. - The actual modified `Mspace.cpp` and regression pass with AddressSanitizer and UndefinedBehaviorSanitizer, `-Wall -Wextra -Werror -pthread`, and a local thread-local errno provider. - `git diff --check` passes. With the full repository configured and built with `BUILD_TESTING=ON`: ```sh ctest --test-dir build -R '^(mspace|guest_reallocalign)$' --output-on-failure ``` No affected retail title or exploitation is claimed. Local validation helpers and investigation notes are excluded from the patch. Full-build results and remaining platform limits are recorded below. ### PR overlap (earlier checks) Direct per-file three-way merge checks confirm a conflict with the whole-tree formatting PR #1128 in `core/libs/prx/libc/src/Mspace.cpp`. That PR does not implement this bug fix. #1128 currently conflicts with upstream in `StdioExtras.cpp`, so the repository overlap tool skips its pairwise conflict check; its empty result does not clear this overlap. This PR merges cleanly into upstream `1e5bb880`; merging it and #1128 will require rebasing whichever lands second. ### Earlier local validation after rebase Rebased onto upstream `main` at `cea1ef13`. Tested head: `bc1f7e694e02f4bd63fa718295860a71dfe7a07b`. Ubuntu 24.04 under WSL2, GCC 13.3, Release; the contribution patch is preserved and its tree matches the checked merge result. - Repository conventions and `git diff --check` passed. - Full project CMake build passed with `BUILD_TESTING=ON`. - Focused CTest regression selection: 6 passed, 0 skipped, 0 failed (6 registered). Title/homebrew execution remains untested. The GitHub-hosted fork checks below are separate from these local results. ### Independent fork CI [Build run 37676602286](https://github.com/Sanjaysrinivas/AnyPS5/actions/runs/37676602286) passed on exact PR head `131e05a26bea1775cbb990224501aa255c7389d7`. Rebased onto upstream `main` at `4764e59c37b1fc6c89284fdecaef5a5718b6119e`. The unchanged contribution's source tree matches the checked automatic merge result; conventions and `git diff --check` passed. Both jobs completed the full Release CMake build with `BUILD_TESTING=ON`, the `libs` target and the full CTest suite with `--timeout 120 --repeat until-fail:3`. - Linux: Ubuntu 24.04, GCC 13.3, lavapipe: **379 passed, 21 skipped, 0 failed** (400 registered). - Native Windows: WinLibs GCC 15.2: **239 passed, 156 skipped, 0 failed** (395 registered). - The newly merged upstream `guest_force_tls_destructor` regression passed in all three Linux repetitions. These fork checks do not replace upstream workflow approval or maintainer review. No title/homebrew execution or RDNA hardware measurement is claimed. ### Checklist - [x] Based on current `main` at `4764e59c`; no other open PR implements the same fix - [x] One topic per PR; follow-ups go in a new PR - [x] No comments added to code - [x] No unimplemented paths or silent stubs added - [x] No investigation notes or images added to the tracked repository - [x] No new third-party code - [x] Generic behavior, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-08 06:46:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T06:46:39Z.


Checked the overflow guard in both resize exports against Allocate, ran mspace on the merge with current main (passes; fails with the Mspace.cpp change reverted), CI green on this head. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6054162346 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1110#issuecomment-6054162346). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T06:46:39Z. --- Checked the overflow guard in both resize exports against `Allocate`, ran `mspace` on the merge with current main (passes; fails with the Mspace.cpp change reverted), CI green on this head. 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!1110
No description provided.