fix(libSceSaveData): hold a host write over save memory reads into guest buffers #12444

Merged
BunnyFl merged 1 commit from upstream-pr/12444 into main 2026-10-29 11:56:56 +00:00
BunnyFl commented 2026-10-28 13:42:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-10-28T13:42:41Z.


What

sceSaveDataGetSaveDataMemory2 reads the requested range of the save memory file with std::ifstream::read straight into data->buf, with no GuestArena::HostWrite over it. A read larger than the stream buffer reaches ReadFile directly, so the kernel writes into guest memory. CONVENTIONS requires a HostWrite over such a write.

On Windows, when data->buf is in a shared view whose pages the write watch left read-only, the read fails. The call then returns SAVE_DATA_ERROR_INTERNAL (0x809F000B) and the save data is not read.

This PR opens a HostWrite over buf/buf_size before the read. A destination the guest mapped read-only throws, since the console's result for that case is not known.

Tested

Windows 11, WinLibs GCC 15.2.0 (Release):

  • New savedata_memory_host_write (Windows only, modelled on guest_socket_host_write):
    • Setup: direct memory mapped twice, write watch armed, a 0x5000-byte read at offset 3 across three 16 KiB pages.
    • The test requires the call to succeed, the bytes to match, the guard bytes to be untouched and the write to be tracked. It also requires a read-only destination to throw.
    • With this PR: passes.
    • With Export.cpp from main (same test): fails 3/3 at read save memory into watched memory; the call returns 0x809F000B.
  • ctest -R savedata: 14/14 pass.
  • python tools/check_conventions.py --base upstream/main: passed.
  • python tools/pr_overlap.py --branch fix/savedata-memory-host-write --base main:
    • No changed lines shared with an open PR.
    • The PRs it could not compare share only core/libs/CMakeLists.txt, except #11720, which changes sceSaveDataCancel in the same Export.cpp, not this function.
  • python tools/progress.py --compare: unchanged.
  • Not tested on Linux or macOS.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5051502489 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12444). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-10-28T13:42:41Z. --- ### What `sceSaveDataGetSaveDataMemory2` reads the requested range of the save memory file with `std::ifstream::read` straight into `data->buf`, with no `GuestArena::HostWrite` over it. A read larger than the stream buffer reaches `ReadFile` directly, so the kernel writes into guest memory. [CONVENTIONS](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md#host-writes-into-guest-memory) requires a `HostWrite` over such a write. On Windows, when `data->buf` is in a shared view whose pages the write watch left read-only, the read fails. The call then returns `SAVE_DATA_ERROR_INTERNAL` (`0x809F000B`) and the save data is not read. This PR opens a `HostWrite` over `buf`/`buf_size` before the read. A destination the guest mapped read-only throws, since the console's result for that case is not known. ### Tested Windows 11, WinLibs GCC 15.2.0 (Release): - New `savedata_memory_host_write` (Windows only, modelled on `guest_socket_host_write`): - Setup: direct memory mapped twice, write watch armed, a 0x5000-byte read at offset 3 across three 16 KiB pages. - The test requires the call to succeed, the bytes to match, the guard bytes to be untouched and the write to be tracked. It also requires a read-only destination to throw. - With this PR: passes. - With `Export.cpp` from `main` (same test): fails 3/3 at `read save memory into watched memory`; the call returns `0x809F000B`. - `ctest -R savedata`: 14/14 pass. - `python tools/check_conventions.py --base upstream/main`: passed. - `python tools/pr_overlap.py --branch fix/savedata-memory-host-write --base main`: - No changed lines shared with an open PR. - The PRs it could not compare share only `core/libs/CMakeLists.txt`, except #11720, which changes `sceSaveDataCancel` in the same `Export.cpp`, not this function. - `python tools/progress.py --compare`: unchanged. - Not tested on Linux or macOS. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-28 13:50:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T13:50:26Z.

Review: COMMENTED


I reviewed the exact head ff2e92939f. The new HostWrite spans the validated save-memory destination before ifstream::read, and the Windows-only regression covers a write-watched buffer across page boundaries, byte/guard preservation, dirty-page reporting, and rejection of a read-only destination. I found no issue in the changed path. I could not execute this Windows-specific test in the current Linux environment; the author reports the focused regression and savedata suite passing, while hosted CI was still queued at my last check.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5683710901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12444#pullrequestreview-5683710901). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T13:50:26Z. **Review:** COMMENTED --- I reviewed the exact head `ff2e92939f`. The new `HostWrite` spans the validated save-memory destination before `ifstream::read`, and the Windows-only regression covers a write-watched buffer across page boundaries, byte/guard preservation, dirty-page reporting, and rejection of a read-only destination. I found no issue in the changed path. I could not execute this Windows-specific test in the current Linux environment; the author reports the focused regression and `savedata` suite passing, while hosted CI was still queued at my last check.
MarcZX10 commented 2026-10-28 14:09:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:09:04Z.

Review: APPROVED


Approved at exact head ff2e92939f. I reviewed the save-memory read path and the Windows-specific regression. The HostWrite guard covers the validated guest destination before ifstream::read; the test checks a watched buffer spanning page boundaries, preserved surrounding bytes, dirty-page reporting, and refusal of a read-only destination. The PR reports the focused regression and savedata suite passing on Windows, and the hosted Windows, conventions, eligibility, intake, labels, and report checks now pass. I could not run the Windows-only test locally on this Linux host.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5683992893 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12444#pullrequestreview-5683992893). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T14:09:04Z. **Review:** APPROVED --- Approved at exact head `ff2e92939f`. I reviewed the save-memory read path and the Windows-specific regression. The `HostWrite` guard covers the validated guest destination before `ifstream::read`; the test checks a watched buffer spanning page boundaries, preserved surrounding bytes, dirty-page reporting, and refusal of a read-only destination. The PR reports the focused regression and savedata suite passing on Windows, and the hosted Windows, conventions, eligibility, intake, labels, and report checks now pass. I could not run the Windows-only test locally on this Linux host.
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!12444
No description provided.