fix(savedata): read every sceSaveDataSetSaveDataMemory2 data entry at its 64-byte stride #5659

Merged
wachi35676 merged 1 commit from upstream-pr/5659 into main 2026-10-15 11:56:05 +00:00
wachi35676 commented 2026-10-14 04:56:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T04:56:02Z.


What

SaveDataMemoryData in core/libs/SceTypes.hpp was declared as 24 bytes (buf, buf_size, offset). sceSaveDataSetSaveDataMemory2 in libSceSaveData.native indexes the guest's data array with that stride (Export.cpp, the set_param->data[i] loops), so with 64-byte guest entries it read entry 1 from the reserved bytes of entry 0, saw a zero size, skipped it, and returned SAVE_DATA_OK with only the first range written.

The entry is 64 bytes (buffer, size, offset, 40 reserved bytes), as the sibling library already declares and asserts: core/libs/prx/libSceSaveData/SaveDataCore.hpp static_assert(sizeof(MemoryData) == 64, "SceSaveDataMemoryData is 64 bytes"). shadPS4's OrbisSaveDataMemoryData has the same layout. This adds the reserved bytes and a static_assert. Only the Set path indexes the array; the Get path reads a single entry.

With the shared type the right size, SaveDataCore.hpp no longer needs its private MemoryData copy: it now uses SaveDataMemoryData and drops the three reinterpret_casts to MemoryData. Its valid_memory_range takes the shared type's unsigned offset; a negative guest offset is still rejected, because as an unsigned value it exceeds any memory size. The old and new checks agree on 40,000,000 edge and random (buffer, size, offset) cases for every memory size below 2^63 bytes.

savedata_memory_metadata gets a block that lays out two entries by hand at a 64-byte stride and checks both ranges reach the save. The existing 3-entry case is unchanged.

Tested

Windows 11 26200, WinLibs GCC 15.2.0 UCRT, full build with BUILD_TESTING=ON on main 75690a117.

  • New test on the current header: savedata_memory_metadata fails (Save-data memory check failed at line 138, the file-content check; the call itself returned 0).
  • With this change, rebased on main 2582ec119: all 13 save data tests pass (savedata_* and guest_savedata_*, both libraries).
  • python tools/check_conventions.py --base origin/main passes.

Not tested: Linux and macOS builds; titles (no title ID known to call sceSaveDataSetSaveDataMemory2 with more than one entry).

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4853159449 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5659). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-14T04:56:02Z. --- ### What `SaveDataMemoryData` in `core/libs/SceTypes.hpp` was declared as 24 bytes (`buf`, `buf_size`, `offset`). `sceSaveDataSetSaveDataMemory2` in libSceSaveData.native indexes the guest's `data` array with that stride (`Export.cpp`, the `set_param->data[i]` loops), so with 64-byte guest entries it read entry 1 from the reserved bytes of entry 0, saw a zero size, skipped it, and returned `SAVE_DATA_OK` with only the first range written. The entry is 64 bytes (buffer, size, offset, 40 reserved bytes), as the sibling library already declares and asserts: `core/libs/prx/libSceSaveData/SaveDataCore.hpp` `static_assert(sizeof(MemoryData) == 64, "SceSaveDataMemoryData is 64 bytes")`. shadPS4's `OrbisSaveDataMemoryData` has the same layout. This adds the reserved bytes and a `static_assert`. Only the Set path indexes the array; the Get path reads a single entry. With the shared type the right size, `SaveDataCore.hpp` no longer needs its private `MemoryData` copy: it now uses `SaveDataMemoryData` and drops the three `reinterpret_cast`s to `MemoryData`. Its `valid_memory_range` takes the shared type's unsigned offset; a negative guest offset is still rejected, because as an unsigned value it exceeds any memory size. The old and new checks agree on 40,000,000 edge and random (buffer, size, offset) cases for every memory size below 2^63 bytes. `savedata_memory_metadata` gets a block that lays out two entries by hand at a 64-byte stride and checks both ranges reach the save. The existing 3-entry case is unchanged. ### Tested Windows 11 26200, WinLibs GCC 15.2.0 UCRT, full build with `BUILD_TESTING=ON` on `main` `75690a117`. - New test on the current header: `savedata_memory_metadata` fails (`Save-data memory check failed at line 138`, the file-content check; the call itself returned 0). - With this change, rebased on `main` `2582ec119`: all 13 save data tests pass (`savedata_*` and `guest_savedata_*`, both libraries). - `python tools/check_conventions.py --base origin/main` passes. Not tested: Linux and macOS builds; titles (no title ID known to call `sceSaveDataSetSaveDataMemory2` with more than one entry). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
AlexOfTheForest commented 2026-10-14 06:06:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexOfTheForest on 2026-10-14T06:06:46Z.


The fix is right: libSceSaveData.native indexes set_param->data[i], so a 24-byte struct read entries 2+ from the first entry's reserved bytes.

The same guest struct now exists twice: SaveDataMemoryData in SceTypes.hpp and MemoryData in libSceSaveData/SaveDataCore.hpp (which already asserts 64). Both are asserted now, so they can't drift silently, but if one can include the other, a single definition would be cleaner.

Nice that the test builds the entries at a raw 64-byte stride instead of using the struct, so it would still catch a wrong struct size.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162067229 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5659#issuecomment-6162067229). Originally posted by [AlexOfTheForest](https://github.com/AlexOfTheForest) on 2026-10-14T06:06:46Z. --- The fix is right: libSceSaveData.native indexes set_param->data[i], so a 24-byte struct read entries 2+ from the first entry's reserved bytes. The same guest struct now exists twice: SaveDataMemoryData in SceTypes.hpp and MemoryData in libSceSaveData/SaveDataCore.hpp (which already asserts 64). Both are asserted now, so they can't drift silently, but if one can include the other, a single definition would be cleaner. Nice that the test builds the entries at a raw 64-byte stride instead of using the struct, so it would still catch a wrong struct size.
wachi35676 commented 2026-10-14 09:00:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T09:00:59Z.


Thanks, good point. With SaveDataMemoryData now 64 bytes, SaveDataCore.hpp no longer needs its own copy, so I dropped MemoryData and the three reinterpret_casts to it and it uses the shared type directly. The one difference was the offset type (std::int64_t there, std::size_t in SceTypes.hpp): valid_memory_range now takes the unsigned offset, and a negative guest offset is still rejected because it exceeds any memory size. I compared the old and new checks on 40M edge and random cases with zero differences for every memory size below 2^63. All 13 save data tests pass on current main. Amended into the same commit and updated the description.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6164751379 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5659#issuecomment-6164751379). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-14T09:00:59Z. --- Thanks, good point. With `SaveDataMemoryData` now 64 bytes, `SaveDataCore.hpp` no longer needs its own copy, so I dropped `MemoryData` and the three `reinterpret_cast`s to it and it uses the shared type directly. The one difference was the offset type (`std::int64_t` there, `std::size_t` in `SceTypes.hpp`): `valid_memory_range` now takes the unsigned offset, and a negative guest offset is still rejected because it exceeds any memory size. I compared the old and new checks on 40M edge and random cases with zero differences for every memory size below 2^63. All 13 save data tests pass on current `main`. Amended into the same commit and updated the description.
adriadam10 commented 2026-10-15 11:43:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:43:23Z.


Checked on Linux: build + core/libs ctest 32/32 green; savedata_memory_metadata fails without the header change and passes with it. The shared SaveDataMemoryData is now 64 bytes, matching libSceSaveData's old private copy, and libSceSaveData.native already bounds-checks the offset as unsigned. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191238966 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5659#issuecomment-6191238966). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T11:43:23Z. --- Checked on Linux: build + core/libs ctest 32/32 green; `savedata_memory_metadata` fails without the header change and passes with it. The shared `SaveDataMemoryData` is now 64 bytes, matching `libSceSaveData`'s old private copy, and `libSceSaveData.native` already bounds-checks the offset as unsigned. Merging once the full Build is green.
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!5659
No description provided.