fix(libkernel): support NoOverwrite mappings on Windows #347

Merged
Pouare514 merged 2 commits from upstream-pr/347 into main 2026-10-04 21:11:34 +00:00
Pouare514 commented 2026-10-02 10:45:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-02T10:45:06Z.


What

Host-side enforcement for the NoOverwrite mapping flag (0x80, MAP_FIXED_NOREPLACE semantics) in MapPlaced on Windows: probe the range with VirtualQuery first and fail with EEXIST if anything is already committed there, instead of blindly placing with MAP_FIXED. The guest-registry overlap check one level up (RequireAvailable) cannot see host-side occupancy (e.g. a host-committed page inside the arena that no guest allocation tracks), so without the probe such a mapping silently overwrites host memory.

Tested

  • New guest_memory case CheckNoOverwriteRejectsHostOccupiedMapping: host-commits a page inside the arena without registering a guest allocation, then requests a Fixed|NoOverwrite direct mapping over it. This branch rejects (EEXIST from the probe); with the probe reverted to current main the same call returns 0 (verified by swapping in main's DirectMemory.cpp: test fails there).
  • Windows Release Ninja: libs target and guest_memory test build clean; guest_memory passes.

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; silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4715520215 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/347). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-02T10:45:06Z. --- ### What Host-side enforcement for the NoOverwrite mapping flag (0x80, `MAP_FIXED_NOREPLACE` semantics) in `MapPlaced` on Windows: probe the range with `VirtualQuery` first and fail with `EEXIST` if anything is already committed there, instead of blindly placing with `MAP_FIXED`. The guest-registry overlap check one level up (`RequireAvailable`) cannot see host-side occupancy (e.g. a host-committed page inside the arena that no guest allocation tracks), so without the probe such a mapping silently overwrites host memory. ### Tested - New `guest_memory` case `CheckNoOverwriteRejectsHostOccupiedMapping`: host-commits a page inside the arena without registering a guest allocation, then requests a Fixed|NoOverwrite direct mapping over it. This branch rejects (EEXIST from the probe); with the probe reverted to current main the same call returns 0 (verified by swapping in main's `DirectMemory.cpp`: test fails there). - Windows Release Ninja: `libs` target and `guest_memory` test build clean; `guest_memory` passes. ### 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 - [x] Unimplemented paths throw; silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [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
oneandonlydean commented 2026-10-04 05:42:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:42:12Z.


Thanks for picking this up. Since #286, main already accepts NoOverwrite (0x80) fixed mappings on Windows, so the "unsupported flags" failure from the description no longer happens there. For guest ranges the overlap check also already happens one level up: DoMapAnon, DoMapDirect and DoReserveVirtual call mutation.RequireAvailable for every fixed mapping while holding the registry lock, so a NoOverwrite mapping over a reservation or any other registered guest allocation is already refused on both platforms.

What this branch adds is the host side VirtualQuery probe. Could you add a case to core/libs/tests/GuestMemory.cpp with a Fixed|NoOverwrite mapping that this probe rejects and current main accepts? Without one I can't tell what it changes in practice. Also, GuestArena.hpp is included twice now.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977017084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/347#issuecomment-5977017084). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:42:12Z. --- Thanks for picking this up. Since #286, main already accepts NoOverwrite (0x80) fixed mappings on Windows, so the "unsupported flags" failure from the description no longer happens there. For guest ranges the overlap check also already happens one level up: `DoMapAnon`, `DoMapDirect` and `DoReserveVirtual` call `mutation.RequireAvailable` for every fixed mapping while holding the registry lock, so a NoOverwrite mapping over a reservation or any other registered guest allocation is already refused on both platforms. What this branch adds is the host side `VirtualQuery` probe. Could you add a case to `core/libs/tests/GuestMemory.cpp` with a Fixed|NoOverwrite mapping that this probe rejects and current main accepts? Without one I can't tell what it changes in practice. Also, `GuestArena.hpp` is included twice now.
Pouare514 commented 2026-10-04 14:48:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-04T14:48:08Z.


Done: added CheckNoOverwriteRejectsHostOccupiedMapping to guest_memory (Windows-only): host-commits a page inside the arena without registering a guest allocation, then requests a Fixed|NoOverwrite direct mapping over it. The probe rejects with EEXIST; with main's DirectMemory.cpp swapped in, the same call returns 0 and the test fails. Also removed the duplicated GuestArena.hpp include. guest_memory passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5981234088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/347#issuecomment-5981234088). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-04T14:48:08Z. --- Done: added CheckNoOverwriteRejectsHostOccupiedMapping to guest_memory (Windows-only): host-commits a page inside the arena without registering a guest allocation, then requests a Fixed|NoOverwrite direct mapping over it. The probe rejects with EEXIST; with main's DirectMemory.cpp swapped in, the same call returns 0 and the test fails. Also removed the duplicated GuestArena.hpp include. guest_memory passes.
github-actions[bot] commented 2026-10-04 18:37:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T18:37:33Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#359 core/libs/tests/GuestMemory.cpp L29, L181
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5983138347 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/347#issuecomment-5983138347). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-04T18:37:33Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #359 | `core/libs/tests/GuestMemory.cpp` [L29](https://github.com/boykopovar/AnyPS5/blob/412665dfdb949aea80d3f75c4ba72b92c7161b26/core/libs/tests/GuestMemory.cpp#L29), [L181](https://github.com/boykopovar/AnyPS5/blob/412665dfdb949aea80d3f75c4ba72b92c7161b26/core/libs/tests/GuestMemory.cpp#L181) | | |
oneandonlydean commented 2026-10-04 21:07:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:07:49Z.


Thanks, that test is what I was after. The guest heap commits arena pages through the same GuestArenaCommit call without a registry entry, so on Windows a Fixed|NoOverwrite mapping over heap pages was accepted before, and Linux's MAP_FIXED_NOREPLACE already refuses it. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984394567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/347#issuecomment-5984394567). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:07:49Z. --- Thanks, that test is what I was after. The guest heap commits arena pages through the same `GuestArenaCommit` call without a registry entry, so on Windows a Fixed|NoOverwrite mapping over heap pages was accepted before, and Linux's MAP_FIXED_NOREPLACE already refuses it. Ready to merge from my side.
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!347
No description provided.