fix(savedata): preserve memory on write failure #1109

Merged
Sanjaysrinivas merged 1 commit from upstream-pr/1109 into main 2026-10-08 05:28:15 +00:00
Sanjaysrinivas commented 2026-10-07 12:44:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Sanjaysrinivas on 2026-10-07T12:44:37Z.


What

Native sceSaveDataSetSaveDataMemory2 writes each range directly into the existing .bin save. A later I/O failure returns SAVE_DATA_ERROR_INTERNAL after earlier bytes have already changed. Reproduced with two valid updates to a 32-byte save under a 16-byte file-size limit: the call fails and the original save is changed.

Read the existing blob, apply the already-validated ranges in memory, and reuse write_file_replace. That helper is already used by native save setup and metadata persistence, and only replaces the original after a complete temporary-file write, flush and close. Verify the loaded size still matches the size used to validate the ranges.

Extend the existing memory regression to check temporary-file creation failure, partial write failure on POSIX, invalid later ranges, binary payloads, untouched bytes, zero-sized ranges, and the existing data_num == 0 single-entry convention. No new dependency, abstraction or test target.

This copies and writes the complete memory blob per update (setup permits at most 16 MiB). The change protects the .bin update; .bin and .param remain separate replacements, and this does not add power-loss durability beyond the existing replacement helper.

Tested

Earlier focused validation of the unchanged contribution patch:

Ubuntu 24.04 under WSL2, GCC 13.3.0, x86-64; synthetic saves, no game files:

  • Original implementation: controlled partial-write reproduction returns INTERNAL but fails original save preserved.
  • Original implementation: extended SaveDataMemoryMetadata fails when a blocked temporary path should prevent updating the original save.
  • Complete libc and native save-data shared libraries built using their repository CMake source lists in a local, untracked validation harness.
  • Seven neighboring tests pass three consecutive runs each: savedata_memory_metadata, savedata_memory_growth, savedata_memory_growth_read_failure, guest_savedata_delete, guest_savedata_ps4, mspace, and guest_reallocalign.
  • Modified native save-data source and memory regression pass AddressSanitizer and UndefinedBehaviorSanitizer; regression compiles with -Wall -Wextra -Werror.
  • git diff --check passes.

With the full repository configured and built with BUILD_TESTING=ON:

ctest --test-dir build -R '^(savedata_memory_metadata|savedata_memory_growth|savedata_memory_growth_read_failure|guest_savedata_delete|guest_savedata_ps4)$' --output-on-failure

Local validation harnesses and investigation notes are excluded from this patch. Full-build results and remaining platform limits are recorded below.

PR overlap (earlier checks)

Direct per-file three-way merge checks confirm a conflict with the whole-tree formatting PR #1128 in core/libs/tests/SaveDataMemoryMetadata.cpp. That PR does not implement this bug fix. #1128 currently conflicts with upstream in StdioExtras.cpp, so the repository overlap tool skips its pairwise conflict check; its empty result does not clear this overlap. This PR merges cleanly into upstream 1e5bb880; merging it and #1128 will require rebasing whichever lands second.

Earlier local validation after rebase

Rebased onto upstream main at cea1ef13. Tested head: b00dd85c2050bc0e04b00befdc31d17d516d6241.
Ubuntu 24.04 under WSL2, GCC 13.3, Release; the contribution patch is preserved and its tree matches the checked merge result.

  • Repository conventions and git diff --check passed.
  • Full project CMake build passed with BUILD_TESTING=ON.
  • Focused CTest regression selection: 8 passed, 0 skipped, 0 failed (8 registered).

Title/homebrew execution remains untested. The GitHub-hosted fork checks below are separate from these local results.

Independent fork CI

Build run 37676598080 passed on exact PR head c4cfb8e23d92d8fcdfb721d87093999288a0db48.
Rebased onto upstream main at 4764e59c37b1fc6c89284fdecaef5a5718b6119e. The unchanged contribution's source tree matches the checked automatic merge result; conventions and git diff --check passed.
Both jobs completed the full Release CMake build with BUILD_TESTING=ON, the libs target and the full CTest suite with --timeout 120 --repeat until-fail:3.

  • Linux: Ubuntu 24.04, GCC 13.3, lavapipe: 379 passed, 21 skipped, 0 failed (400 registered).
  • Native Windows: WinLibs GCC 15.2: 239 passed, 156 skipped, 0 failed (395 registered).
  • The newly merged upstream guest_force_tls_destructor regression passed in all three Linux repetitions.

These fork checks do not replace upstream workflow approval or maintainer review. No title/homebrew execution or RDNA hardware measurement is claimed.

Checklist

  • Based on current main at 4764e59c; no other open PR implements the same fix
  • One topic per PR; follow-ups go in a new PR
  • No comments added to code
  • No unimplemented paths or silent stubs added
  • No investigation notes or images added to the tracked repository
  • No new third-party code
  • Generic behavior, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4772815275 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1109). Originally posted by [Sanjaysrinivas](https://github.com/Sanjaysrinivas) on 2026-10-07T12:44:37Z. --- ### What Native `sceSaveDataSetSaveDataMemory2` writes each range directly into the existing `.bin` save. A later I/O failure returns `SAVE_DATA_ERROR_INTERNAL` after earlier bytes have already changed. Reproduced with two valid updates to a 32-byte save under a 16-byte file-size limit: the call fails and the original save is changed. Read the existing blob, apply the already-validated ranges in memory, and reuse `write_file_replace`. That helper is already used by native save setup and metadata persistence, and only replaces the original after a complete temporary-file write, flush and close. Verify the loaded size still matches the size used to validate the ranges. Extend the existing memory regression to check temporary-file creation failure, partial write failure on POSIX, invalid later ranges, binary payloads, untouched bytes, zero-sized ranges, and the existing `data_num == 0` single-entry convention. No new dependency, abstraction or test target. This copies and writes the complete memory blob per update (setup permits at most 16 MiB). The change protects the `.bin` update; `.bin` and `.param` remain separate replacements, and this does not add power-loss durability beyond the existing replacement helper. ### Tested Earlier focused validation of the unchanged contribution patch: Ubuntu 24.04 under WSL2, GCC 13.3.0, x86-64; synthetic saves, no game files: - Original implementation: controlled partial-write reproduction returns INTERNAL but fails `original save preserved`. - Original implementation: extended `SaveDataMemoryMetadata` fails when a blocked temporary path should prevent updating the original save. - Complete libc and native save-data shared libraries built using their repository CMake source lists in a local, untracked validation harness. - Seven neighboring tests pass three consecutive runs each: `savedata_memory_metadata`, `savedata_memory_growth`, `savedata_memory_growth_read_failure`, `guest_savedata_delete`, `guest_savedata_ps4`, `mspace`, and `guest_reallocalign`. - Modified native save-data source and memory regression pass AddressSanitizer and UndefinedBehaviorSanitizer; regression compiles with `-Wall -Wextra -Werror`. - `git diff --check` passes. With the full repository configured and built with `BUILD_TESTING=ON`: ```sh ctest --test-dir build -R '^(savedata_memory_metadata|savedata_memory_growth|savedata_memory_growth_read_failure|guest_savedata_delete|guest_savedata_ps4)$' --output-on-failure ``` Local validation harnesses and investigation notes are excluded from this patch. Full-build results and remaining platform limits are recorded below. ### PR overlap (earlier checks) Direct per-file three-way merge checks confirm a conflict with the whole-tree formatting PR #1128 in `core/libs/tests/SaveDataMemoryMetadata.cpp`. That PR does not implement this bug fix. #1128 currently conflicts with upstream in `StdioExtras.cpp`, so the repository overlap tool skips its pairwise conflict check; its empty result does not clear this overlap. This PR merges cleanly into upstream `1e5bb880`; merging it and #1128 will require rebasing whichever lands second. ### Earlier local validation after rebase Rebased onto upstream `main` at `cea1ef13`. Tested head: `b00dd85c2050bc0e04b00befdc31d17d516d6241`. Ubuntu 24.04 under WSL2, GCC 13.3, Release; the contribution patch is preserved and its tree matches the checked merge result. - Repository conventions and `git diff --check` passed. - Full project CMake build passed with `BUILD_TESTING=ON`. - Focused CTest regression selection: 8 passed, 0 skipped, 0 failed (8 registered). Title/homebrew execution remains untested. The GitHub-hosted fork checks below are separate from these local results. ### Independent fork CI [Build run 37676598080](https://github.com/Sanjaysrinivas/AnyPS5/actions/runs/37676598080) passed on exact PR head `c4cfb8e23d92d8fcdfb721d87093999288a0db48`. Rebased onto upstream `main` at `4764e59c37b1fc6c89284fdecaef5a5718b6119e`. The unchanged contribution's source tree matches the checked automatic merge result; conventions and `git diff --check` passed. Both jobs completed the full Release CMake build with `BUILD_TESTING=ON`, the `libs` target and the full CTest suite with `--timeout 120 --repeat until-fail:3`. - Linux: Ubuntu 24.04, GCC 13.3, lavapipe: **379 passed, 21 skipped, 0 failed** (400 registered). - Native Windows: WinLibs GCC 15.2: **239 passed, 156 skipped, 0 failed** (395 registered). - The newly merged upstream `guest_force_tls_destructor` regression passed in all three Linux repetitions. These fork checks do not replace upstream workflow approval or maintainer review. No title/homebrew execution or RDNA hardware measurement is claimed. ### Checklist - [x] Based on current `main` at `4764e59c`; no other open PR implements the same fix - [x] One topic per PR; follow-ups go in a new PR - [x] No comments added to code - [x] No unimplemented paths or silent stubs added - [x] No investigation notes or images added to the tracked repository - [x] No new third-party code - [x] Generic behavior, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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!1109
No description provided.