From 8be0c3639f1df6e35d489bcf844227067d537f4f Mon Sep 17 00:00:00 2001 From: DHILLON Date: Tue, 24 Nov 2026 23:28:06 -0600 Subject: [PATCH] XGameRuntime: a save read sees all of an update or none of it XGameSaveSubmitUpdate replaces an update's blobs one file at a time under SAVE_MUTATION, but XGameSaveReadBlobData (and its Async form) read the blobs without it. A read that ran during an update could return one blob from before the update and another from after it, and while it had a blob open, MoveFileExW could not replace that file, so the update failed with E_FAIL partway through. read_layout now takes SAVE_MUTATION, like apply and delete_container. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/hle/xgameruntime/gamesave.rs | 43 ++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/xgameruntime/gamesave.rs b/crates/xcl/src/hle/xgameruntime/gamesave.rs index 1a43e53b69..3aac3be88b 100644 --- a/crates/xcl/src/hle/xgameruntime/gamesave.rs +++ b/crates/xcl/src/hle/xgameruntime/gamesave.rs @@ -264,7 +264,8 @@ unsafe fn delete_path(p: *mut Provider, name: *const u8) -> Result = std::sync::Mutex::new(()); fn delete_container(dir: &Path) { @@ -411,6 +412,7 @@ pub unsafe extern "system" fn XGameSaveEnumerateBlobInfoByName(c: *mut Container /// Blobs `names` (all when empty) laid out as the API returns them: `Blob` array, then the /// names and data; pointers stored as offsets from the start (fixed up by the caller). fn read_layout(dir: &Path, names: &[String]) -> Result<(Vec, u32), HRESULT> { + let _mutation = SAVE_MUTATION.lock().unwrap_or_else(|e| e.into_inner()); let all = blobs_of(dir); let picked: Vec<(String, PathBuf)> = if names.is_empty() { all.into_iter().map(|b| (b.0, b.1)).collect() @@ -1184,4 +1186,43 @@ mod tests { assert_eq!(mixed, 0, "{mixed} of {reads} enumerations mixed submitted updates"); } + /// An update replaces its blobs one file at a time: a read in between must not return some of + /// them from before the update and some from after it, nor make the update fail by having one + /// of the files open. + #[test] + fn reads_during_an_update_see_all_of_it_or_none() { + let root = std::env::temp_dir().join(format!("xcl-gamesave-read-update-{}", std::process::id())); + let dir = root.join("slot"); + let names = ["head".to_string(), "tail".to_string()]; + let write = |v: u8| { + let ops: Vec<_> = names.iter().map(|n| (n.clone(), Some(vec![v; 4096]))).collect(); + apply(&dir, None, &ops) + }; + assert_eq!(write(0), S_OK); + let done = std::sync::atomic::AtomicBool::new(false); + let (failed_updates, (reads, mixed, failed_reads)) = std::thread::scope(|s| { + let reader = s.spawn(|| { + let (mut reads, mut mixed, mut failed) = (0u32, 0u32, 0u32); + loop { + reads += 1; + match read_layout(&dir, &names) { + Ok((bytes, 2)) => { + let data_at = |k: usize| k * std::mem::size_of::() + std::mem::offset_of!(Blob, data); + let first = |k: usize| bytes[u64::from_le_bytes(bytes[data_at(k)..data_at(k) + 8].try_into().unwrap()) as usize]; + mixed += (first(0) != first(1)) as u32; + } + _ => failed += 1, + } + if done.load(Ordering::Relaxed) { + return (reads, mixed, failed); + } + } + }); + let failed = (1..=30u8).filter(|&v| write(v) != S_OK).count(); + done.store(true, Ordering::Relaxed); + (failed, reader.join().unwrap()) + }); + let _ = std::fs::remove_dir_all(&root); + assert_eq!((failed_updates, mixed, failed_reads), (0, 0, 0), "failed updates, mixed reads, failed reads of {reads}"); + } }