From 2cc79fb11140adaccfac198853fb00db6dfda6a8 Mon Sep 17 00:00:00 2001 From: Prime1997 Date: Mon, 23 Nov 2026 19:58:01 -0800 Subject: [PATCH] D3D12.X: publish driver cache blobs from private staging files The per-process staging pathname was shared by concurrent writes of one key. A writer paused after opening it could resume after another writer renamed that file into the cache, changing successfully published bytes through its older handle. Create an exclusive sibling file for each invocation using the process ID and an atomic sequence. Skip occupied names left by an earlier process and clean up only a staging file this invocation actually created. A deterministic Windows regression holding the real staging helper's open handle fails with the old writer and passes with this change. Focused Cargo writer tests: 4 passed, 0 failed, 0 ignored. Full GPU suite and retail smoke have not been run for this commit. Assisted-by: Codex (GPT-6, exact variant not exposed) --- crates/xcl/src/hle/d3d12x/real.rs | 67 ++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index aed907586..7ad090563 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3476,16 +3476,36 @@ mod driver_cache { } } + fn create_staging_file(p: &Path) -> std::io::Result<(PathBuf, std::fs::File)> { + static NEXT: AtomicU64 = AtomicU64::new(0); + create_staging_file_with(p, &NEXT) + } + + fn create_staging_file_with(p: &Path, next: &AtomicU64) -> std::io::Result<(PathBuf, std::fs::File)> { + loop { + let tmp = p.with_extension(format!("tmp{}-{}", std::process::id(), next.fetch_add(1, Ordering::Relaxed))); + match std::fs::OpenOptions::new().write(true).create_new(true).open(&tmp) { + Ok(file) => return Ok((tmp, file)), + // A prior process with this PID may have left a file behind. It is not ours. + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + } + } + } + /// `bytes` to `p`, under another name first: a run that is cut short leaves no half file. pub fn write(p: &Path, bytes: &[u8]) -> bool { + use std::io::Write; if bytes.is_empty() { return false; } if let Some(dir) = p.parent() { let _ = std::fs::create_dir_all(dir); } - let tmp = p.with_extension(format!("tmp{}", std::process::id())); - let ok = std::fs::write(&tmp, bytes).is_ok() && std::fs::rename(&tmp, p).is_ok(); + let Ok((tmp, mut file)) = create_staging_file(p) else { return false }; + let written = file.write_all(bytes).is_ok(); + drop(file); + let ok = written && std::fs::rename(&tmp, p).is_ok(); if !ok { let _ = std::fs::remove_file(&tmp); } @@ -3506,6 +3526,11 @@ mod driver_cache { mod tests { use super::*; + struct Directory(PathBuf); + impl Drop for Directory { + fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } + } + #[test] fn a_file_is_named_after_its_dxil_and_flags() { assert_eq!(name(b"abc", 0), name(b"abc", 0)); @@ -3534,6 +3559,44 @@ mod driver_cache { assert_eq!(load(&p), None); let _ = std::fs::remove_dir_all(&dir); } + + #[test] + fn an_overlapping_writer_cannot_modify_the_published_blob() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("xcl-driver-cache-overlap-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let _directory = Directory(dir.clone()); + let p = dir.join("program.pso"); + // Another writer, paused after the production staging-file creation step. + let (_other_tmp, mut other) = create_staging_file(&p).unwrap(); + let complete = b"complete second blob"; + assert!(write(&p, complete)); + assert_eq!(load(&p).as_deref(), Some(complete.as_slice())); + // Resuming the older writer must not change a file already published by this + // invocation, even when the older payload is shorter than the newer one. + other.write_all(b"old").unwrap(); + assert_eq!(load(&p).as_deref(), Some(complete.as_slice()), "published bytes must not alias another writer's staging file"); + } + + #[test] + fn an_abandoned_staging_file_is_skipped_and_preserved() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("xcl-driver-cache-stale-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let _directory = Directory(dir.clone()); + let p = dir.join("program.pso"); + let occupied = p.with_extension(format!("tmp{}-0", std::process::id())); + std::fs::write(&occupied, b"not ours").unwrap(); + let next = AtomicU64::new(0); + let (tmp, mut file) = create_staging_file_with(&p, &next).unwrap(); + assert_eq!(next.load(Ordering::Relaxed), 2, "the occupied candidate was passed over"); + assert_ne!(tmp, occupied); + file.write_all(b"ours").unwrap(); + drop(file); + std::fs::rename(&tmp, &p).unwrap(); + assert_eq!(load(&p).as_deref(), Some(b"ours".as_slice())); + assert_eq!(std::fs::read(&occupied).unwrap(), b"not ours"); + } } }