[Data integrity] Fix save-memory metadata failures and setup recovery #1667

Closed
opened 2026-10-08 16:03:07 +00:00 by Sanjaysrinivas · 0 comments
Sanjaysrinivas commented 2026-10-08 16:03:07 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Sanjaysrinivas on 2026-10-08T16:03:07Z.


Split from #1659. Category: data integrity, starting with save-memory initialization and recovery after a failed metadata write.

Problem and user-visible consequence

sceSaveDataSetupSaveDataMemory2 can return success after creating or growing the memory blob even when it failed to persist the requested initial SaveDataParam. The caller therefore receives a successful setup result for an incomplete saved state.

A return-code fix alone is insufficient. The blob is committed first, and a later call with the same size can skip the initialization branch because that blob now exists. Recovery must be designed so the failed attempt does not permanently bypass the requested initialization or destroy an older valid save.

Evidence and affected code

Baseline checked: upstream 4b005b229e24be9ec3f185def9e59cbc03aa9e4a.

  • setupSaveDataMemory2, lines 611–650 enters initialization when the blob is missing or smaller than memory_size.
  • The blob replacement at line 637 is checked and returns SAVE_DATA_ERROR_INTERNAL on failure.
  • When init_param != nullptr and option bit 1 is set, the parameter replacement at line 643 ignores its boolean result. Execution continues to initialize the result structure and return SAVE_DATA_OK.
  • SaveDataFile.hpp writes to a .tmp path, checks stream operations, and replaces the destination. Its failure result is available; this setup caller drops it.

The review reproduced a success return with the blob present and the parameter file absent on checkout 34d4cf1796054a31feb7ced250bea8cbb8f66058. The unchecked setup call remains at the upstream baseline above. This was a synthetic filesystem test, not a claim of observed game-save loss.

Deterministic reproduction

Use an isolated temporary working directory and the native save-data library:

  1. Call sceSaveDataInitialize3.
  2. Create a directory at _sd_mem/u42/slot0.param.tmp; opening that path as a metadata output file must fail.
  3. Prepare a zero-initialized SaveDataParam with a recognizable user_param value.
  4. Set up SaveDataMemorySetup2 with user_id=42, slot_id=0, memory_size=128, option=1, and init_param pointing to that parameter structure.
  5. Call sceSaveDataSetupSaveDataMemory2, recording its return code and result fields.
  6. Inspect _sd_mem/u42/slot0.bin and _sd_mem/u42/slot0.param.
  7. Remove any remaining obstruction and repeat the same setup request with the same size. Read back the resulting parameter data.

The current first call can return zero while only the blob exists. The retry needs explicit verification because an already-created 128-byte blob no longer satisfies the initialization condition. The replacement helper may remove the empty temporary directory itself; tests must tolerate that cleanup and recreate the obstruction for independent cases.

Required behavior and design decisions

  • Return the appropriate save-data error when required initial metadata cannot be written. Use the API's established error representation; do not substitute a host exception for an expected filesystem failure.
  • Choose and document a consistent commit/recovery policy for the blob and parameter pair. A single-file replacement is not automatically a transaction spanning both files.
  • Ensure the same-size retry after a failed initialization can reach a correct state. Avoid silently reporting successful setup while leaving required initialization missing.
  • Preserve previously valid blob bytes and metadata during failed growth or replacement. Define what remains visible after every intermediate failure.
  • Define when output result fields are populated on failure, consistent with the existing API behavior and tests. Do not report a successful initialized size for a failed operation.
  • Keep successful initialization semantics, zero-filled growth, and the option/null-parameter rules unchanged.
  • Keep the existing mutex/serialization guarantees intact so concurrent setup calls cannot observe or commit contradictory slot state.

Atomic rollback is one possible implementation; a clearly defined recoverable intermediate state may be another if supported by the intended API contract. Choose the smallest correct approach and verify it. Do not promise resilience to sudden power loss without testing/documenting the required persistence guarantees.

Test matrix

Scenario Assertions
Fresh slot, all writes succeed Success; expected zero-filled blob; exact requested metadata persisted
Fresh slot, metadata temporary file cannot open Error; resulting files match the chosen recovery policy
Same-size retry after that failure Correct metadata eventually persisted; no false successful incomplete state
Existing slot grows successfully Original blob prefix preserved, extension zero-filled, expected metadata behavior
Growth metadata write fails Error; previous valid state preserved or recoverable exactly as documented
Blob write fails before metadata Error; old blob and metadata unchanged; no misleading result fields
Existing same-size slot without a preceding failed initialization Existing API semantics preserved; no unintended metadata reset
init_param == nullptr or initialization option disabled No new mandatory metadata operation
Separate users/slots No cross-slot or cross-user changes
Fresh-process readback after successful retry Persisted bytes and parameter values match expectations

Use deterministic failures such as the temporary-path obstruction instead of relying solely on permissions that behave differently for privileged runners. Add replacement/write failure injection only where existing helpers support it or a concrete missing case requires it.

Build on SaveDataMemoryMetadata.cpp, SaveDataMemoryGrowth.cpp, and the registered replacement/failure tests in core/libs/CMakeLists.txt.

#312 already covers metadata failure in the set-memory path. #244 and #1109 improve replacement/write failure handling. Retain those fixes; extend coverage for setup and its retry state rather than duplicating existing tests.

Acceptance criteria

  • Setup never reports success after failing a required metadata write.
  • The state after partial failure and the same-size retry behavior are documented and tested.
  • Previously valid saves remain intact under the specified failure cases.
  • Tests check return codes, persisted bytes, result fields, and post-failure usability, not just file existence.
  • Applicable cases pass in Release on Windows and Linux, with actual commands/toolchains recorded.
  • Successful save/load/resume in a real title is recorded separately by the compatibility workflow when available; synthetic tests remain identified as synthetic evidence.
  • Repeatable failure/recovery regression execution: #1669.
  • Real-title save/load/resume evidence: #1671.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5765002591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1667). Originally posted by [Sanjaysrinivas](https://github.com/Sanjaysrinivas) on 2026-10-08T16:03:07Z. --- Split from #1659. Category: **data integrity**, starting with save-memory initialization and recovery after a failed metadata write. ## Problem and user-visible consequence `sceSaveDataSetupSaveDataMemory2` can return success after creating or growing the memory blob even when it failed to persist the requested initial `SaveDataParam`. The caller therefore receives a successful setup result for an incomplete saved state. A return-code fix alone is insufficient. The blob is committed first, and a later call with the same size can skip the initialization branch because that blob now exists. Recovery must be designed so the failed attempt does not permanently bypass the requested initialization or destroy an older valid save. ## Evidence and affected code Baseline checked: upstream `4b005b229e24be9ec3f185def9e59cbc03aa9e4a`. - [`setupSaveDataMemory2`, lines 611–650](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/prx/libSceSaveData.native/Export.cpp#L611) enters initialization when the blob is missing or smaller than `memory_size`. - The blob replacement at line 637 is checked and returns `SAVE_DATA_ERROR_INTERNAL` on failure. - When `init_param != nullptr` and option bit 1 is set, the parameter replacement at line 643 ignores its boolean result. Execution continues to initialize the result structure and return `SAVE_DATA_OK`. - [`SaveDataFile.hpp`](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/prx/libSceSaveData/SaveDataFile.hpp) writes to a `.tmp` path, checks stream operations, and replaces the destination. Its failure result is available; this setup caller drops it. The review reproduced a success return with the blob present and the parameter file absent on checkout `34d4cf1796054a31feb7ced250bea8cbb8f66058`. The unchecked setup call remains at the upstream baseline above. This was a synthetic filesystem test, not a claim of observed game-save loss. ## Deterministic reproduction Use an isolated temporary working directory and the native save-data library: 1. Call `sceSaveDataInitialize3`. 2. Create a directory at `_sd_mem/u42/slot0.param.tmp`; opening that path as a metadata output file must fail. 3. Prepare a zero-initialized `SaveDataParam` with a recognizable `user_param` value. 4. Set up `SaveDataMemorySetup2` with `user_id=42`, `slot_id=0`, `memory_size=128`, `option=1`, and `init_param` pointing to that parameter structure. 5. Call `sceSaveDataSetupSaveDataMemory2`, recording its return code and result fields. 6. Inspect `_sd_mem/u42/slot0.bin` and `_sd_mem/u42/slot0.param`. 7. Remove any remaining obstruction and repeat the same setup request with the same size. Read back the resulting parameter data. The current first call can return zero while only the blob exists. The retry needs explicit verification because an already-created 128-byte blob no longer satisfies the initialization condition. The replacement helper may remove the empty temporary directory itself; tests must tolerate that cleanup and recreate the obstruction for independent cases. ## Required behavior and design decisions - [ ] Return the appropriate save-data error when required initial metadata cannot be written. Use the API's established error representation; do not substitute a host exception for an expected filesystem failure. - [ ] Choose and document a consistent commit/recovery policy for the blob and parameter pair. A single-file replacement is not automatically a transaction spanning both files. - [ ] Ensure the same-size retry after a failed initialization can reach a correct state. Avoid silently reporting successful setup while leaving required initialization missing. - [ ] Preserve previously valid blob bytes and metadata during failed growth or replacement. Define what remains visible after every intermediate failure. - [ ] Define when output result fields are populated on failure, consistent with the existing API behavior and tests. Do not report a successful initialized size for a failed operation. - [ ] Keep successful initialization semantics, zero-filled growth, and the option/null-parameter rules unchanged. - [ ] Keep the existing mutex/serialization guarantees intact so concurrent setup calls cannot observe or commit contradictory slot state. Atomic rollback is one possible implementation; a clearly defined recoverable intermediate state may be another if supported by the intended API contract. Choose the smallest correct approach and verify it. Do not promise resilience to sudden power loss without testing/documenting the required persistence guarantees. ## Test matrix | Scenario | Assertions | | --- | --- | | Fresh slot, all writes succeed | Success; expected zero-filled blob; exact requested metadata persisted | | Fresh slot, metadata temporary file cannot open | Error; resulting files match the chosen recovery policy | | Same-size retry after that failure | Correct metadata eventually persisted; no false successful incomplete state | | Existing slot grows successfully | Original blob prefix preserved, extension zero-filled, expected metadata behavior | | Growth metadata write fails | Error; previous valid state preserved or recoverable exactly as documented | | Blob write fails before metadata | Error; old blob and metadata unchanged; no misleading result fields | | Existing same-size slot without a preceding failed initialization | Existing API semantics preserved; no unintended metadata reset | | `init_param == nullptr` or initialization option disabled | No new mandatory metadata operation | | Separate users/slots | No cross-slot or cross-user changes | | Fresh-process readback after successful retry | Persisted bytes and parameter values match expectations | Use deterministic failures such as the temporary-path obstruction instead of relying solely on permissions that behave differently for privileged runners. Add replacement/write failure injection only where existing helpers support it or a concrete missing case requires it. ## Existing coverage and related work Build on [`SaveDataMemoryMetadata.cpp`](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/tests/SaveDataMemoryMetadata.cpp), [`SaveDataMemoryGrowth.cpp`](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/tests/SaveDataMemoryGrowth.cpp), and the registered replacement/failure tests in `core/libs/CMakeLists.txt`. #312 already covers metadata failure in the set-memory path. #244 and #1109 improve replacement/write failure handling. Retain those fixes; extend coverage for setup and its retry state rather than duplicating existing tests. ## Acceptance criteria - [ ] Setup never reports success after failing a required metadata write. - [ ] The state after partial failure and the same-size retry behavior are documented and tested. - [ ] Previously valid saves remain intact under the specified failure cases. - [ ] Tests check return codes, persisted bytes, result fields, and post-failure usability, not just file existence. - [ ] Applicable cases pass in Release on Windows and Linux, with actual commands/toolchains recorded. - [ ] Successful save/load/resume in a real title is recorded separately by the compatibility workflow when available; synthetic tests remain identified as synthetic evidence. ## Related category issues - Repeatable failure/recovery regression execution: #1669. - Real-title save/load/resume evidence: #1671.
Sign in to join this conversation.
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#1667
No description provided.