fix(libkernel): pread, read and APR reads into write-tracked guest memory through a staging buffer #182

Closed
MichonGoddijn231849 wants to merge 2 commits from upstream-pr/182 into main
MichonGoddijn231849 commented 2026-09-30 11:50:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-09-30T11:50:06Z.


What

On Windows, pread into a guest destination that ReadFile refuses now goes through a staging buffer. When ReadFile fails with ERROR_NOACCESS or ERROR_INVALID_USER_BUFFER, the read lands in a host buffer and is copied to the destination. The copy is an ordinary write, so the write tracking's fault handler sees it as usual. A remaining failure now reports the Windows error code as well as errno.

The same refusal hits two more read paths, handled the same way:

  • read (sceKernelRead): _read fails with EINVAL; the read is retried from the same file position into a staging buffer.
  • APR (sceKernelAprSubmitCommandBuffer read commands): std::ifstream::read into the destination fails; it now reads through a 4 MiB staging buffer in chunks.

Since the shared-mapping and write-tracking commits (abb9c85, f7c53a4, 5e08eb4), a guest page can be read-only until its first write. ReadFile writes into it from kernel mode, which doesn't raise the user-mode fault that would lift the protection, so the read fails and pread throws errno=5.

Tested

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

  • GTA V (PPSA04264) on current main: the Story Mode load aborted with pread_nid_postfix: pread failed, fd=81, errno=5, and gets through with this change.
  • Demon's Souls (PPSA01342) on current main with #167 and #134: the character creation head textures (APR) aborted with APR: read failed for ...head_09_col.chunk1.ctxc, and then the Bink video thread with sceKernelRead: read failed, fd=3, errno=22. Both load with this change.
  • libkernel builds.

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. Written with Claude (Anthropic) under my direction; I reviewed and tested it.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4690061771 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/182). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-09-30T11:50:06Z. --- ### What On Windows, `pread` into a guest destination that `ReadFile` refuses now goes through a staging buffer. When `ReadFile` fails with `ERROR_NOACCESS` or `ERROR_INVALID_USER_BUFFER`, the read lands in a host buffer and is copied to the destination. The copy is an ordinary write, so the write tracking's fault handler sees it as usual. A remaining failure now reports the Windows error code as well as errno. The same refusal hits two more read paths, handled the same way: - `read` (`sceKernelRead`): `_read` fails with `EINVAL`; the read is retried from the same file position into a staging buffer. - APR (`sceKernelAprSubmitCommandBuffer` read commands): `std::ifstream::read` into the destination fails; it now reads through a 4 MiB staging buffer in chunks. Since the shared-mapping and write-tracking commits (abb9c85, f7c53a4, 5e08eb4), a guest page can be read-only until its first write. `ReadFile` writes into it from kernel mode, which doesn't raise the user-mode fault that would lift the protection, so the read fails and `pread` throws `errno=5`. ### Tested Windows 11 Pro, AMD Ryzen 5 7600 (6-core), NVIDIA GeForce RTX 5080 (driver 32.0.16.1074), 32 GB DDR5-5600, MinGW-w64 GCC 15.2.0. - GTA V (PPSA04264) on current `main`: the Story Mode load aborted with `pread_nid_postfix: pread failed, fd=81, errno=5`, and gets through with this change. - Demon's Souls (PPSA01342) on current `main` with #167 and #134: the character creation head textures (APR) aborted with `APR: read failed for ...head_09_col.chunk1.ctxc`, and then the Bink video thread with `sceKernelRead: read failed, fd=3, errno=22`. Both load with this change. - `libkernel` builds. ### 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. Written with Claude (Anthropic) under my direction; I reviewed and tested it.
boykopovar commented 2026-10-01 07:55:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-10-01T07:55:24Z.


This adds error-driven fallbacks to read and pread: a failed direct read is retried through a staging buffer. Such fallbacks are not allowed, and EINVAL does not specifically identify a write-tracking protection fault.

Please implement a consistent read path that supports guest memory protection by design across read, pread, and APR. Choose the appropriate path before performing I/O, rather than retrying after an error. Failures must remain explicit; APR must not silently accept incomplete reads.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5927233182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/182#issuecomment-5927233182). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-10-01T07:55:24Z. --- This adds error-driven fallbacks to `read` and `pread`: a failed direct read is retried through a staging buffer. Such fallbacks are not allowed, and `EINVAL` does not specifically identify a write-tracking protection fault. Please implement a consistent read path that supports guest memory protection by design across `read`, `pread`, and APR. Choose the appropriate path before performing I/O, rather than retrying after an error. Failures must remain explicit; APR must not silently accept incomplete reads.
MichonGoddijn231849 commented 2026-10-01 17:26:24 +00:00 (Migrated from github.com)

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


Thanks for the review. Reworked as #232: one read path for read, pread, AIO and APR that pins write-tracked pages before the I/O (no retry after an error), EFAULT for unwritable destinations, and APR throws on short reads.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5936801461 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/182#issuecomment-5936801461). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-10-01T17:26:24Z. --- Thanks for the review. Reworked as #232: one read path for read, pread, AIO and APR that pins write-tracked pages before the I/O (no retry after an error), EFAULT for unwritable destinations, and APR throws on short reads.

Pull request closed

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!182
No description provided.