fix(libc): skip holes inside an allocation when unmapping a range #3440

Merged
alfalfa-star merged 2 commits from upstream-pr/3440 into main 2026-10-11 10:46:06 +00:00
alfalfa-star commented 2026-10-11 10:34:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-11T10:34:51Z.


What

A fixed direct mapping whose range starts in a hole inside an earlier allocation aborts with:

terminate called after throwing an instance of 'std::runtime_error'
  what():  guest protection or unmap range has a hole

The call path is sceKernelBatchMap2 → DoMapDirect → ReplaceFixedOverlap → UnmapRegistered → GuestAllocationsUnmap → replaceRange.

Title: RIDE 4 (PPSA01599, Unreal Engine 4) on main 8ec0e131, a few seconds in, before any window. Under gdb (ReplaceFixedOverlap breakpoint plus APS5_TRACE_MEMORY=1):

  • The title first unmaps 0x2008e40000+0x2000000, which is part of a larger direct mapping.
  • It then maps fixed (flags 0x10) at 0x2009d20000+0x1790000. That range starts inside the unmapped part and ends in the still-mapped rest.

On FreeBSD both the munmap and the fixed map over a partly unmapped range are legal.

Cause: GuestAllocationsUnmap decides which range contains the cursor from the allocation's original extent (allocationAddress + allocationBytes), and runs each piece to that extent's end.

  • Once part of an allocation has been unmapped, a cursor in that hole still matches the range before the hole.
  • replaceRange then meets the hole and throws.

Holes between allocations were already skipped (962fc34e); holes inside one were not.

Fix

Two lines: test containment against the registered range itself (address + bytes), and end each piece at that range's end. A cursor in a hole then moves on to the next registered range, exactly as between allocations.

  • last is still computed from what remains of the allocation, so the Windows munmap_release of the whole allocation happens on the same final piece as before.
  • Pieces with several protections inside one allocation are now unmapped range by range rather than in one call.

Tested

Linux (Arch), GCC, Release, on main 99c1c263.

  • New guest_memory case CheckFixedMappingOverHoleInsideAllocation:
    • a 4-page direct mapping loses page 1 to sceKernelMunmap;
    • pages 1-2 are then mapped fixed; this aborts on main with the line above;
    • with the fix it succeeds, and pages 0 and 3 keep their data.
  • Full ctest: 825/825 (4 video-out flip tests skipped as usual).
  • RIDE 4 with this fix gets past the stop. It then reaches #3435, and with that, the Unreal Engine logo and legal screen.
  • tools/check_conventions.py passes.

Note: TibixDev's stacked series (#1324 and the PRs built on it) edits the last check a few lines below. It doesn't touch these lines, but it may need a trivial rebase.

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:4818429021 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3440). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-11T10:34:51Z. --- ### What A fixed direct mapping whose range starts in a hole inside an earlier allocation aborts with: ```text terminate called after throwing an instance of 'std::runtime_error' what(): guest protection or unmap range has a hole ``` The call path is `sceKernelBatchMap2` → `DoMapDirect` → `ReplaceFixedOverlap` → `UnmapRegistered` → `GuestAllocationsUnmap` → `replaceRange`. **Title:** RIDE 4 (PPSA01599, Unreal Engine 4) on main 8ec0e131, a few seconds in, before any window. Under gdb (`ReplaceFixedOverlap` breakpoint plus `APS5_TRACE_MEMORY=1`): - The title first unmaps `0x2008e40000+0x2000000`, which is part of a larger direct mapping. - It then maps fixed (flags `0x10`) at `0x2009d20000+0x1790000`. That range starts inside the unmapped part and ends in the still-mapped rest. On FreeBSD both the munmap and the fixed map over a partly unmapped range are legal. **Cause:** `GuestAllocationsUnmap` decides which range contains the cursor from the allocation's original extent (`allocationAddress + allocationBytes`), and runs each piece to that extent's end. - Once part of an allocation has been unmapped, a cursor in that hole still matches the range before the hole. - `replaceRange` then meets the hole and throws. Holes *between* allocations were already skipped (962fc34e); holes *inside* one were not. ### Fix Two lines: test containment against the registered range itself (`address + bytes`), and end each piece at that range's end. A cursor in a hole then moves on to the next registered range, exactly as between allocations. - `last` is still computed from what remains of the allocation, so the Windows `munmap_release` of the whole allocation happens on the same final piece as before. - Pieces with several protections inside one allocation are now unmapped range by range rather than in one call. ### Tested Linux (Arch), GCC, Release, on main 99c1c263. - New `guest_memory` case `CheckFixedMappingOverHoleInsideAllocation`: - a 4-page direct mapping loses page 1 to `sceKernelMunmap`; - pages 1-2 are then mapped fixed; this aborts on main with the line above; - with the fix it succeeds, and pages 0 and 3 keep their data. - Full `ctest`: 825/825 (4 video-out flip tests skipped as usual). - RIDE 4 with this fix gets past the stop. It then reaches #3435, and with that, the Unreal Engine logo and legal screen. - `tools/check_conventions.py` passes. Note: TibixDev's stacked series (#1324 and the PRs built on it) edits the `last` check a few lines below. It doesn't touch these lines, but it may need a trivial rebase. ### 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)
adriadam10 commented 2026-10-11 10:46:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:46:01Z.


Checked build and ctest on the PR merged onto main: guest_memory passes with the change and the new CheckFixedMappingOverHoleInsideAllocation fails without it. The fix is correct: containment and piece end now use the registered range, so a cursor in a hole inside an allocation moves on like one between allocations, and last is unchanged. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108210662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3440#issuecomment-6108210662). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T10:46:01Z. --- Checked build and ctest on the PR merged onto main: `guest_memory` passes with the change and the new `CheckFixedMappingOverHoleInsideAllocation` fails without it. The fix is correct: containment and piece end now use the registered range, so a cursor in a hole inside an allocation moves on like one between allocations, and `last` is unchanged. 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!3440
No description provided.