Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
alfalfa-star:fix/guest-unmap-hole-inside-allocation
Oct 11, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
alfalfa-star:fix/guest-unmap-hole-inside-allocation

Conversation

@alfalfa-star

Copy link
Copy Markdown
Contributor

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 8ec0e13, 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 (962fc34); 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 99c1c26.

  • 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 fix(libScePlayGo): take the chunk set from sce_sys/playgo-chunk.dat #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

alfalfa-star and others added 2 commits October 11, 2026 12:32
GuestAllocationsUnmap took the piece containing the cursor from the
allocation's original extent. After part of an allocation was unmapped,
a later unmap or fixed map starting in that hole still matched the
allocation, and replaceRange threw on the hole. Match against the
registered range itself and end each piece at its end. RIDE 4
(PPSA01599) maps fixed over a partly unmapped direct memory range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One 4-page direct mapping loses its second page to munmap, then pages
1-2 are mapped fixed. The range starts in the hole; before the fix the
unmap of the overlap threw "guest protection or unmap range has a
hole". The pages around it keep their data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants