fix(libc): forget a released section's pages even while views remain on Windows #5730

Merged
oneandonlydean merged 1 commit from upstream-pr/5730 into main 2026-10-14 08:20:26 +00:00
oneandonlydean commented 2026-10-14 08:13:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T08:13:02Z.


What

Fixes the intermittent Windows guest_memory failure at GuestMemory.cpp:1449 (RequireResidentMapping: a fresh mapping of a new direct allocation came back as a view of the section instead of private memory).

WindowsMappings keys each physical page's state by the section handle value. sceKernelReleaseDirectMemory frees the backing before it unmaps the views, so ~PhysicalBacking called ForgetSection while the views were still there, and the slots they used were kept. Once the views were unmapped nothing removed them. When Windows later handed the same handle value to a new direct backing, its pages started with the old state:

  • a page that had been mapped twice stayed "stored", so the new single mapping went to the section (MEM_MAPPED) instead of resident private memory;
  • a singly mapped page that was released while mapped had its bytes saved on unmap, and they were copied into the new allocation, so freshly allocated direct memory was not zero.

CheckReleaseDirectMemoryClearsMappings leaves such a slot at offset 0, and the failure only shows when a later backing happens to get that handle value, which is why it is intermittent.

Views now share ownership of their slot, ForgetSection drops every slot of the section at once, and JIT shared memory calls ForgetSection before closing its section.

Tested

  • New CheckForgottenSectionKeepsNoPages in windows_write_watch: forgets a section while it is still mapped twice, then checks a new mapping under that handle value is resident private memory, and that bytes of an unmapped page do not carry over.
  • No local Windows machine: the new check and the release-then-reuse sequence were run on Linux against a small simulation of the placeholder/section APIs. Both fail on main (mapping not resident, stale byte read back) and pass with this change.
  • Linux build with tests; ctest -R guest_memory and the libc/libkernel tests pass. Windows CI runs the new check.

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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4854940609 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5730). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T08:13:02Z. --- ### What Fixes the intermittent Windows `guest_memory` failure at `GuestMemory.cpp:1449` (`RequireResidentMapping`: a fresh mapping of a new direct allocation came back as a view of the section instead of private memory). `WindowsMappings` keys each physical page's state by the section handle value. `sceKernelReleaseDirectMemory` frees the backing before it unmaps the views, so `~PhysicalBacking` called `ForgetSection` while the views were still there, and the slots they used were kept. Once the views were unmapped nothing removed them. When Windows later handed the same handle value to a new direct backing, its pages started with the old state: - a page that had been mapped twice stayed "stored", so the new single mapping went to the section (MEM_MAPPED) instead of resident private memory; - a singly mapped page that was released while mapped had its bytes saved on unmap, and they were copied into the new allocation, so freshly allocated direct memory was not zero. `CheckReleaseDirectMemoryClearsMappings` leaves such a slot at offset 0, and the failure only shows when a later backing happens to get that handle value, which is why it is intermittent. Views now share ownership of their slot, `ForgetSection` drops every slot of the section at once, and JIT shared memory calls `ForgetSection` before closing its section. ### Tested - New `CheckForgottenSectionKeepsNoPages` in `windows_write_watch`: forgets a section while it is still mapped twice, then checks a new mapping under that handle value is resident private memory, and that bytes of an unmapped page do not carry over. - No local Windows machine: the new check and the release-then-reuse sequence were run on Linux against a small simulation of the placeholder/section APIs. Both fail on main (mapping not resident, stale byte read back) and pass with this change. - Linux build with tests; `ctest -R guest_memory` and the libc/libkernel tests pass. Windows CI runs the new check. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!5730
No description provided.