fix(libkernel): read, pread, AIO and APR write into guest memory as host writes #232

Merged
MichonGoddijn231849 merged 1 commit from upstream-pr/232 into main 2026-10-02 19:19:46 +00:00
MichonGoddijn231849 commented 2026-10-01 17:26:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-10-01T17:26:01Z.


What

Replaces #182, following the review there: one read path for read, pread, AIO and APR, chosen before the I/O, with no retry after an error.

Shared guest pages are armed read-only for write tracking, and a page becomes writable again only in the access-violation handler. A file read is a kernel-mode write, which never reaches that handler: ReadFile fails with ERROR_NOACCESS and _read with EINVAL. GTA V's Story Mode load stops with pread failed, errno=5.

A file read into guest memory is now a host write into it:

  • GuestArenaBeginHostWrite runs before the I/O. It pins the shared views the destination covers, reopens any that are armed, and marks the pages written.
  • While a view is pinned, Collect does not arm it again.
  • GuestArenaEndHostWrite runs after the I/O. It unpins the views and marks the pages written again, so a collect that ran during the read still sees the read's data as new.
  • Private write-watched memory records kernel writes itself and needs nothing.
  • GuestArena::HostWrite wraps the pair around the I/O at the four call sites.

Failures stay explicit:

  • A destination with a shared view the guest cannot write fails with EFAULT before any I/O. read and pread throw, as for any failed read; AIO reports SCE_KERNEL_ERROR_EFAULT; APR throws.
  • APR throws when a read returns fewer bytes than the command asks for, instead of accepting the short read.

Tested

Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB DDR5-5600, MinGW-w64 GCC 15.2.0.

  • guest_memory_tests gains CheckReadsIntoSharedWriteTracking:
    • sceKernelPread, sceKernelRead and an AIO read into armed shared direct memory land, and collect reports the pages as written;
    • after sceKernelMprotect to read-only, pread throws and AIO reports EFAULT.
    • Without the pinning, the test fails with pread failed, errno=5, the same error as GTA V.
  • GTA V (PPSA04264) gets through the Story Mode load, which stopped at the first such pread before (with #167).
  • ctest: all tests pass.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4707184234 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/232). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-01T17:26:01Z. --- ### What Replaces #182, following the review there: one read path for `read`, `pread`, AIO and APR, chosen before the I/O, with no retry after an error. Shared guest pages are armed read-only for write tracking, and a page becomes writable again only in the access-violation handler. A file read is a kernel-mode write, which never reaches that handler: `ReadFile` fails with `ERROR_NOACCESS` and `_read` with `EINVAL`. GTA V's Story Mode load stops with `pread failed, errno=5`. A file read into guest memory is now a host write into it: - `GuestArenaBeginHostWrite` runs before the I/O. It pins the shared views the destination covers, reopens any that are armed, and marks the pages written. - While a view is pinned, `Collect` does not arm it again. - `GuestArenaEndHostWrite` runs after the I/O. It unpins the views and marks the pages written again, so a collect that ran during the read still sees the read's data as new. - Private write-watched memory records kernel writes itself and needs nothing. - `GuestArena::HostWrite` wraps the pair around the I/O at the four call sites. Failures stay explicit: - A destination with a shared view the guest cannot write fails with EFAULT before any I/O. `read` and `pread` throw, as for any failed read; AIO reports `SCE_KERNEL_ERROR_EFAULT`; APR throws. - APR throws when a read returns fewer bytes than the command asks for, instead of accepting the short read. ### Tested Windows 11 Pro, AMD Ryzen 5 7600, NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB DDR5-5600, MinGW-w64 GCC 15.2.0. - `guest_memory_tests` gains `CheckReadsIntoSharedWriteTracking`: - `sceKernelPread`, `sceKernelRead` and an AIO read into armed shared direct memory land, and collect reports the pages as written; - after `sceKernelMprotect` to read-only, pread throws and AIO reports EFAULT. - Without the pinning, the test fails with `pread failed, errno=5`, the same error as GTA V. - GTA V (PPSA04264) gets through the Story Mode load, which stopped at the first such pread before (with #167). - `ctest`: all tests pass. ### 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 (`NotImplemented_nid_no_patch`); 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. Written with Claude (Anthropic) under my direction; I reviewed and tested it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-02 13:42:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T13:42:04Z.


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 L42
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5953717390 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/232#issuecomment-5953717390). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T13:42:04Z. --- <!-- 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` [L42](https://github.com/boykopovar/AnyPS5/blob/7db340de2b12d43533e0171b9439e04346c4abd3/core/libs/tests/GuestMemory.cpp#L42) | | |
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!232
No description provided.