fix(libc): alias non-contiguous shared guest views on Windows #4367

Merged
MarcZX10 merged 2 commits from upstream-pr/4367 into main 2026-10-13 09:00:56 +00:00
MarcZX10 commented 2026-10-12 07:50:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T07:50:14Z.


What

Carries Angel21PC's alias-run implementation from closed PR #2254, preserving the original commit author. It lets WindowsMappings::MapAlias compose one alias from adjacent shared pages even when they come from different sections or nonconsecutive section offsets. The existing single-view path remains for contiguous pages, and ranges containing private pages are still rejected.

This covers the Windows alias part of #4260. It does not add host imports across registered ranges; that separate change remains outside this PR.

Tested

  • Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release: AlxcBTW reports guest_memory passed on code head 5573e3238 with all three alias checks; windows_exceptions and fiber passed too. The non-GPU suite was 589/590, with log_macros failing the same way on main.
  • Sword of the Sea (PPSA25344) on main reaches a 368 MiB buffer spanning two direct-memory allocations; MapAlias refuses it on the current base, forcing CPU copies. The exact current-main rebased head has not been run on Windows or in the title yet.
  • Linux configure could not reach the build because SDL2 is uninitialized and PipeWire, PulseAudio, and ALSA development files are unavailable.

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
  • No third-party code added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826463482 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T07:50:14Z. --- ### What Carries Angel21PC's alias-run implementation from closed PR #2254, preserving the original commit author. It lets `WindowsMappings::MapAlias` compose one alias from adjacent shared pages even when they come from different sections or nonconsecutive section offsets. The existing single-view path remains for contiguous pages, and ranges containing private pages are still rejected. This covers the Windows alias part of #4260. It does not add host imports across registered ranges; that separate change remains outside this PR. ### Tested - Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release: AlxcBTW reports `guest_memory` passed on code head `5573e3238` with all three alias checks; `windows_exceptions` and `fiber` passed too. The non-GPU suite was 589/590, with `log_macros` failing the same way on `main`. - Sword of the Sea (PPSA25344) on `main` reaches a 368 MiB buffer spanning two direct-memory allocations; `MapAlias` refuses it on the current base, forcing CPU copies. The exact current-main rebased head has not been run on Windows or in the title yet. - Linux configure could not reach the build because SDL2 is uninitialized and PipeWire, PulseAudio, and ALSA development files are unavailable. ### 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 - [x] No third-party code added - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
AlxcBTW commented 2026-10-12 11:46:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T11:46:32Z.


@MarcZX100 here is the Windows validation this draft asks for, plus a title run that needs exactly this change.

Unit tests on Windows, on exactly this head (5573e3238). Windows 11 Pro 26200, Ryzen 7 7730U, WinLibs GCC 15.2.0 r7, Release. guest_memory passes (1.49 s) with all three new checks: CheckAliasAcrossDirectAllocations, CheckAliasOfReorderedSharedPages and CheckAliasRefusesPagesThatAreNotSharedViews. windows_exceptions and fiber pass too. Of the non-GPU set (ctest -E "^agc_driver|^video_out|^guest_video"), 589 of 590 pass; the one failure, log_macros, fails the same way on main on that machine.

Sword of the Sea (PPSA25344). Its main menu binds a 368 MiB buffer across two adjacent direct memory ranges (320.75 MiB + 47.3 MiB). Each range is its own allocation, so the pair is views of two sections. An import that covers the buffer needs an alias across both, and on main MapAlias refuses that ("the range is not one contiguous run of views of a section"). As a result the buffer is copied on the CPU on almost every use. A before/after on that title with this PR follows in a separate comment once the rows have run.

Reading the change. I compared it with an alias-run version we had written for the same case. Both group the 16 KiB views into runs of one section's consecutive pages. Both map each run into a split placeholder, so no run needs 64 KiB alignment, and both refuse a range with a page that is not a shared view. Where they differ, yours is better:

  • On a failed map, releaseAlias releases whatever the reservation holds by querying it (views and the remaining placeholders), so the cleanup doesn't depend on bookkeeping.
  • UnmapAlias releases every run and forgets the alias even if one unmap fails. Ours stopped at the first failure and kept the entry.

Your tests cover what ours did: a run that starts at an offset into its section, two sections in one call, the views per run, the freed range after unmapping, and the refusal of a private page. So I'd drop ours.

One thing I'd add to the PR. The host-import entry in docs/dev/TechnicalDebt.md still says: "On Windows, a run of adjacent registered ranges whose shared views are not one contiguous run of views of a section gets no read-write alias, so its ranges are imported one at a time, as before runs were imported whole, and a buffer that crosses two of them is copied." With this change only a run that is not all shared views (a private range next to a shared one) gets no alias. Shared views of several sections, or of one section at offsets that do not follow on, now get one alias made of one view per run in a placeholder reservation.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6123624866 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6123624866). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T11:46:32Z. --- @MarcZX100 here is the Windows validation this draft asks for, plus a title run that needs exactly this change. **Unit tests on Windows, on exactly this head (5573e3238).** Windows 11 Pro 26200, Ryzen 7 7730U, WinLibs GCC 15.2.0 r7, Release. `guest_memory` passes (1.49 s) with all three new checks: `CheckAliasAcrossDirectAllocations`, `CheckAliasOfReorderedSharedPages` and `CheckAliasRefusesPagesThatAreNotSharedViews`. `windows_exceptions` and `fiber` pass too. Of the non-GPU set (`ctest -E "^agc_driver|^video_out|^guest_video"`), 589 of 590 pass; the one failure, `log_macros`, fails the same way on `main` on that machine. **Sword of the Sea (PPSA25344).** Its main menu binds a 368 MiB buffer across two adjacent direct memory ranges (320.75 MiB + 47.3 MiB). Each range is its own allocation, so the pair is views of two sections. An import that covers the buffer needs an alias across both, and on `main` `MapAlias` refuses that ("the range is not one contiguous run of views of a section"). As a result the buffer is copied on the CPU on almost every use. A before/after on that title with this PR follows in a separate comment once the rows have run. **Reading the change.** I compared it with an alias-run version we had written for the same case. Both group the 16 KiB views into runs of one section's consecutive pages. Both map each run into a split placeholder, so no run needs 64 KiB alignment, and both refuse a range with a page that is not a shared view. Where they differ, yours is better: - On a failed map, `releaseAlias` releases whatever the reservation holds by querying it (views and the remaining placeholders), so the cleanup doesn't depend on bookkeeping. - `UnmapAlias` releases every run and forgets the alias even if one unmap fails. Ours stopped at the first failure and kept the entry. Your tests cover what ours did: a run that starts at an offset into its section, two sections in one call, the views per run, the freed range after unmapping, and the refusal of a private page. So I'd drop ours. One thing I'd add to the PR. The host-import entry in `docs/dev/TechnicalDebt.md` still says: "On Windows, a run of adjacent registered ranges whose shared views are not one contiguous run of views of a section gets no read-write alias, so its ranges are imported one at a time, as before runs were imported whole, and a buffer that crosses two of them is copied." With this change only a run that is not all shared views (a private range next to a shared one) gets no alias. Shared views of several sections, or of one section at offsets that do not follow on, now get one alias made of one view per run in a placeholder reservation. AI-assisted: yes (Claude Code)
xXPBSXx commented 2026-10-12 12:20:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T12:20:24Z.


Windows test: head 5573e3238 merged onto current main ffa198407 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j4: full build 0 errors; ctest with the filter guest_memory: guest_memory_pool_stats passed, guest_memory passed (2 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124146408 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6124146408). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T12:20:24Z. --- Windows test: head `5573e3238` merged onto current `main` `ffa198407` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j4`: full build 0 errors; ctest with the filter `guest_memory`: guest_memory_pool_stats passed, guest_memory passed (2 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
queeeef commented 2026-10-12 13:42:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by queeeef on 2026-10-12T13:42:20Z.


Tested on Windows at this head (5573e3238).

  • Setup: Windows 11 Pro 26300, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON.
  • guest_memory: passes, including CheckAliasAcrossDirectAllocations, CheckAliasOfReorderedSharedPages and CheckAliasRefusesPagesThatAreNotSharedViews.
  • Without the fix: with WindowsMappings.hpp restored to the base and the rest of this PR kept, guest_memory aborts with read-write alias of shared guest memory 0x200d60000+0xc000: the range is not one contiguous run of views of a section. That is the error behind cause 2 in #4260.

I reviewed the change against a local equivalent I wrote for #4260 (one placeholder split into one view per run of section offsets). The two do the same thing, so I'm dropping mine in favour of this PR. In Palworld (PPSA23126) the local version made the 43 spans that failed at the alias import, and the CPU copies of the regions under them went from 2–56 GB per 10 s to 0. I'll run this PR together with oneandonlydean's up4/host-import-bind-join on that scene and report on #4260.

AI-assisted (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6125484086 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6125484086). Originally posted by [queeeef](https://github.com/queeeef) on 2026-10-12T13:42:20Z. --- Tested on Windows at this head (`5573e3238`). - **Setup:** Windows 11 Pro 26300, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `BUILD_TESTING=ON`. - **`guest_memory`:** passes, including `CheckAliasAcrossDirectAllocations`, `CheckAliasOfReorderedSharedPages` and `CheckAliasRefusesPagesThatAreNotSharedViews`. - **Without the fix:** with `WindowsMappings.hpp` restored to the base and the rest of this PR kept, `guest_memory` aborts with `read-write alias of shared guest memory 0x200d60000+0xc000: the range is not one contiguous run of views of a section`. That is the error behind cause 2 in #4260. I reviewed the change against a local equivalent I wrote for #4260 (one placeholder split into one view per run of section offsets). The two do the same thing, so I'm dropping mine in favour of this PR. In Palworld (PPSA23126) the local version made the 43 spans that failed at the alias import, and the CPU copies of the regions under them went from 2–56 GB per 10 s to 0. I'll run this PR together with oneandonlydean's `up4/host-import-bind-join` on that scene and report on #4260. AI-assisted (Claude Code)
oneandonlydean commented 2026-10-13 00:33:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:33:16Z.


The Windows runs above cover what the draft was waiting for, so please mark it ready. One change first: the host-import entry in docs/dev/debt/libSceAgcDriver.md still says a run of adjacent registered ranges whose shared views are not one contiguous run of views of a section gets no read-write alias. With this, only a run with a page that is not a shared view is refused, so that sentence needs to say so.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135331077 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6135331077). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:33:16Z. --- The Windows runs above cover what the draft was waiting for, so please mark it ready. One change first: the host-import entry in `docs/dev/debt/libSceAgcDriver.md` still says a run of adjacent registered ranges whose shared views are not one contiguous run of views of a section gets no read-write alias. With this, only a run with a page that is not a shared view is refused, so that sentence needs to say so.
MarcZX10 commented 2026-10-13 07:17:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T07:17:50Z.


Thanks, that debt sentence is stale. The current limitation is that alias creation refuses a run if any page is not a shared view; non-contiguous section offsets alone are now supported. I have not changed the PR text or marked it ready yet. I will correct the debt entry and then mark the PR ready once that edit is on the branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140403317 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6140403317). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T07:17:50Z. --- Thanks, that debt sentence is stale. The current limitation is that alias creation refuses a run if any page is not a shared view; non-contiguous section offsets alone are now supported. I have not changed the PR text or marked it ready yet. I will correct the debt entry and then mark the PR ready once that edit is on the branch.
MarcZX10 commented 2026-10-13 07:32:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T07:32:05Z.


Addressed the debt wording in docs/dev/debt/libSceAgcDriver.md: aliases are refused when a range contains a page that is not a shared view, while fully shared ranges can be mapped across section runs. I rebased onto current main (resolving the cleanRanges/freshRanges additions alongside the alias-run helpers) and marked the PR ready. The Windows report remains for the code at 5573e3238; the rebased head has not had a new Windows runtime run, and the PR description states that explicitly.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140620336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6140620336). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T07:32:05Z. --- Addressed the debt wording in `docs/dev/debt/libSceAgcDriver.md`: aliases are refused when a range contains a page that is not a shared view, while fully shared ranges can be mapped across section runs. I rebased onto current `main` (resolving the `cleanRanges`/`freshRanges` additions alongside the alias-run helpers) and marked the PR ready. The Windows report remains for the code at `5573e3238`; the rebased head has not had a new Windows runtime run, and the PR description states that explicitly.
github-actions[bot] commented 2026-10-13 07:33:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T07:33:08Z.


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
#4603 docs/dev/debt/libSceAgcDriver.md L70-71
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140636163 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4367#issuecomment-6140636163). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T07:33:08Z. --- <!-- 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 | |---|---|---|---| | #4603 | `docs/dev/debt/libSceAgcDriver.md` [L70-71](https://github.com/boykopovar/AnyPS5/blob/02d4839b604d068341b5514995a5af16f1befe0d/docs/dev/debt/libSceAgcDriver.md#L70-L71) | | |
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!4367
No description provided.