diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 76f31d4daf..b80c7d2399 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4428,8 +4428,10 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL // … and one built in an earlier run gets the blob kept on disk then (`driver_cache`) let disk = driver_cache::path_for(dxil, copy.Flags.0, mode == ComputeBuild::Chain); let from_disk = cached.is_none(); + let mut loaded = None; if from_disk { - cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new); + loaded = disk.as_deref().and_then(driver_cache::load); + cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared); } if let Some(b) = &cached { copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: b.as_ptr() as *const c_void, CachedBlobSizeInBytes: b.len() }; @@ -4448,7 +4450,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL // a blob the driver does not take (another root signature at the same address…): compiled forget_compute_blob(key); if from_disk { - driver_cache::note(false, disk.as_deref()); + driver_cache::note(false, loaded.take()); } if mode == ComputeBuild::CachedWhole { return (hr, in_driver, false); @@ -4471,7 +4473,7 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL driver_cache::keep(p, &b, in_driver); } } else if reused && from_disk { - driver_cache::note(true, None); + driver_cache::note(true, loaded.take()); } (hr, in_driver, reused) } @@ -4487,7 +4489,11 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL /// driver refuses the blob of another driver, adapter or root signature (the file is dropped /// and the program compiled as before). `XCL_PSO_DISK_CACHE=0`: off. pub(crate) mod driver_cache { + use std::io::Read; + use std::os::windows::fs::OpenOptionsExt; + use std::os::windows::io::AsRawHandle; use std::path::{Path, PathBuf}; + use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; /// builds shorter than this are not worth a file @@ -4530,20 +4536,72 @@ pub(crate) mod driver_cache { enabled().then(|| crate::gpu::recomp::dxc::cache_dir().join("driver").join(graphics_name(hash, bytes))) } - pub fn load(p: &Path) -> Option> { - std::fs::read(p).ok().filter(|b| !b.is_empty()) + /// Bytes read from one exact cache-file generation. Keeping its handle open lets a later + /// refusal dispose that file object without touching a replacement at the same pathname. + pub struct LoadedBlob { + bytes: Arc>, + /// `None` when the blob was readable but the cache entry could not be opened for deletion. + file: Option, + } + + impl LoadedBlob { + #[cfg(test)] + pub fn bytes(&self) -> &[u8] { &self.bytes } + pub fn shared(&self) -> Arc> { self.bytes.clone() } + + /// Remove the file object that supplied these bytes. POSIX disposition frees its name + /// while another process still has this generation open. If that exact operation is not + /// available, leave the stale entry in place rather than remove a pathname that may now + /// name a replacement. + pub fn invalidate(self) -> bool { + use windows_sys::Win32::Storage::FileSystem::{ + FileDispositionInfoEx, SetFileInformationByHandle, + FILE_DISPOSITION_FLAG_DELETE, FILE_DISPOSITION_FLAG_POSIX_SEMANTICS, + FILE_DISPOSITION_INFO_EX, + }; + let Some(file) = self.file else { return false }; + let handle = file.as_raw_handle() as windows_sys::Win32::Foundation::HANDLE; + let ex = FILE_DISPOSITION_INFO_EX { Flags: FILE_DISPOSITION_FLAG_DELETE | FILE_DISPOSITION_FLAG_POSIX_SEMANTICS }; + unsafe { + SetFileInformationByHandle( + handle, + FileDispositionInfoEx, + (&ex as *const FILE_DISPOSITION_INFO_EX).cast(), + std::mem::size_of::() as u32, + ) != 0 + } + } } - /// A pipeline was made from a kept blob (`ok`), or the driver refused the blob: dropped. - pub fn note(ok: bool, p: Option<&Path>) { + pub fn load(p: &Path) -> Option { + use windows_sys::Win32::Foundation::GENERIC_READ; + use windows_sys::Win32::Storage::FileSystem::{ + DELETE, FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, + }; + let open = |access| { + std::fs::OpenOptions::new() + .read(true) + .access_mode(access) + .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE) + .custom_flags(FILE_FLAG_OPEN_REPARSE_POINT) + .open(p) + }; + let (mut file, invalidatable) = open(GENERIC_READ | DELETE).map(|f| (f, true)).or_else(|_| open(GENERIC_READ).map(|f| (f, false))).ok()?; + let mut bytes = Vec::new(); + file.read_to_end(&mut bytes).ok()?; + (!bytes.is_empty()).then(|| LoadedBlob { bytes: Arc::new(bytes), file: invalidatable.then_some(file) }) + } + + /// A pipeline was made from a kept blob (`ok`), or the driver refused that exact blob: dropped. + pub fn note(ok: bool, loaded: Option) { if ok { let n = HITS.fetch_add(1, Ordering::Relaxed) + 1; if n.is_power_of_two() { crate::xlog_info!("D3D12.X: {n} compute pipelines made from the driver's blob of an earlier run (kept on disk)"); } } else { - if let Some(p) = p { - let _ = std::fs::remove_file(p); + if let Some(loaded) = loaded { + let _ = loaded.invalidate(); } if STALE.fetch_add(1, Ordering::Relaxed) == 0 { crate::xlog_info!("D3D12.X: a driver blob kept on disk was refused (another driver or adapter?) – such pipelines are compiled again"); @@ -4601,6 +4659,10 @@ pub(crate) mod driver_cache { mod tests { use super::*; + fn load_bytes(p: &Path) -> Option> { + load(p).map(|b| b.bytes().to_vec()) + } + struct Directory(PathBuf); impl Drop for Directory { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } @@ -4622,20 +4684,73 @@ pub(crate) mod driver_cache { fn a_kept_blob_is_read_back_and_a_refused_one_is_dropped() { let dir = std::env::temp_dir().join(format!("xcl-driver-cache-test-{}", std::process::id())); let p = dir.join("driver").join(name(b"program", 0)); - assert_eq!(load(&p), None, "nothing kept yet"); + assert_eq!(load_bytes(&p), None, "nothing kept yet"); assert!(!write(&p, &[]), "an empty blob is not kept"); keep(&p, &[1, 2, 3], std::time::Duration::from_millis(10)); - assert_eq!(load(&p), None, "a build of milliseconds is not kept"); + assert_eq!(load_bytes(&p), None, "a build of milliseconds is not kept"); keep(&p, &[1, 2, 3], std::time::Duration::from_secs(1)); - assert_eq!(load(&p).as_deref(), Some(&[1u8, 2, 3][..])); + assert_eq!(load_bytes(&p).as_deref(), Some(&[1u8, 2, 3][..])); // no half file under the temporary name assert_eq!(std::fs::read_dir(p.parent().unwrap()).unwrap().count(), 1); // the driver took the blob: it stays; it refused it: gone, the program is compiled again - note(true, Some(&p)); + note(true, load(&p)); assert!(p.exists()); - note(false, Some(&p)); - assert_eq!(load(&p), None); + note(false, load(&p)); + assert_eq!(load_bytes(&p), None); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn an_older_process_cannot_remove_a_replacement_blob() { + const CHILD: &str = "XCL_DRIVER_CACHE_REPLACEMENT_CHILD"; + let bytes = b"same bytes in both generations"; + if let Some(path) = std::env::var_os(CHILD) { + let p = PathBuf::from(path); + assert!(load(&p).unwrap().invalidate()); + assert!(write(&p, bytes), "replacement publication"); + return; + } + let dir = std::env::temp_dir().join(format!("xcl-driver-cache-replace-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir(&dir).unwrap(); + let _directory = Directory(dir.clone()); + let p = dir.join("program.pso"); + // Identical bytes make pathname/content comparisons unable to distinguish the two + // generations: the open file object is the identity that must protect the second. + assert!(write(&p, bytes)); + let delayed_reader = load(&p).unwrap(); + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "hle::d3d12x::real::driver_cache::tests::an_older_process_cannot_remove_a_replacement_blob", "--nocapture"]) + .env(CHILD, &p) + .output() + .unwrap(); + assert!( + output.status.success(), + "replacement process failed:\n{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + let _ = delayed_reader.invalidate(); + assert_eq!(load_bytes(&p).as_deref(), Some(bytes.as_slice())); + } + + #[test] + fn a_cache_symlink_never_disposes_its_target() { + use std::os::windows::fs::symlink_file; + let dir = std::env::temp_dir().join(format!("xcl-driver-cache-link-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir(&dir).unwrap(); + let _directory = Directory(dir.clone()); + let target = dir.join("outside.pso"); + let link = dir.join("cache.pso"); + std::fs::write(&target, b"not the cache entry").unwrap(); + if symlink_file(&target, &link).is_err() { + return; // creating symlinks can require Developer Mode or an elevated token + } + if let Some(loaded) = load(&link) { + let _ = loaded.invalidate(); + } + assert_eq!(std::fs::read(&target).unwrap(), b"not the cache entry"); } #[test] @@ -4649,11 +4764,11 @@ pub(crate) mod driver_cache { 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())); + assert_eq!(load_bytes(&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"); + assert_eq!(load_bytes(&p).as_deref(), Some(complete.as_slice()), "published bytes must not alias another writer's staging file"); } #[test] @@ -4672,7 +4787,7 @@ pub(crate) mod driver_cache { 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!(load_bytes(&p).as_deref(), Some(b"ours".as_slice())); assert_eq!(std::fs::read(&occupied).unwrap(), b"not ours"); } } @@ -5259,8 +5374,10 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const let mut cached = graphics_blob(gkey); let disk = driver_cache::graphics_path(gkey.0, gkey.1); let from_disk = cached.is_none(); + let mut loaded = None; if from_disk { - cached = disk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new); + loaded = disk.as_deref().and_then(driver_cache::load); + cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared); } let build = create_graphics_pso_cached( &mut copy, @@ -5269,7 +5386,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const || { forget_graphics_blob(gkey); if from_disk { - driver_cache::note(false, disk.as_deref()); + driver_cache::note(false, loaded.take()); } }, |label, has_cached, f| driver_compile_identified(label, bytes, gate_compute_driver_compile(has_cached), Some((ps_hash, has_cached)), f), @@ -5282,7 +5399,7 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const driver_cache::keep(p, &b, build.in_driver); } } else if build.reused && from_disk { - driver_cache::note(true, None); + driver_cache::note(true, loaded.take()); } if hr < 0 { note_translation_error(format!( @@ -6013,8 +6130,10 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: let mut ccached = compute_blob(ckey); let cdisk = driver_cache::path(&tp.cs, 0); let cfrom_disk = ccached.is_none(); + let mut cloaded = None; if cfrom_disk { - ccached = cdisk.as_deref().and_then(driver_cache::load).map(std::sync::Arc::new); + cloaded = cdisk.as_deref().and_then(driver_cache::load); + ccached = cloaded.as_ref().map(driver_cache::LoadedBlob::shared); } if let Some(b) = &ccached { cdesc.CachedPSO = D3D12_CACHED_PIPELINE_STATE { pCachedBlob: b.as_ptr() as *const c_void, CachedBlobSizeInBytes: b.len() }; @@ -6027,7 +6146,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: if hc < 0 && ccached.is_some() { forget_compute_blob(ckey); if cfrom_disk { - driver_cache::note(false, cdisk.as_deref()); + driver_cache::note(false, cloaded.take()); } cdesc.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default(); compute = std::ptr::null_mut(); @@ -6044,7 +6163,7 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu: driver_cache::keep(p, &b, cin_driver); } } else if creused && cfrom_disk { - driver_cache::note(true, None); + driver_cache::note(true, cloaded.take()); } drop(_slot); if hc < 0 || compute.is_null() { diff --git a/crates/xcl/src/shaders.rs b/crates/xcl/src/shaders.rs index 0cd2ed7b3a..9c374c4445 100644 --- a/crates/xcl/src/shaders.rs +++ b/crates/xcl/src/shaders.rs @@ -1515,7 +1515,8 @@ impl Pipelines { } driver_cache::path_for(dxil, desc.Flags.0, chained) }); - let cached = path.as_deref().and_then(driver_cache::load); + let mut loaded = path.as_deref().and_then(driver_cache::load); + let cached = loaded.as_ref().map(driver_cache::LoadedBlob::shared); let over_cap = self.max_dxil != 0 && dxil.len() > self.max_dxil; // over the cap and no blob from an earlier run: the driver's compile of this one takes // tens of minutes and gigabytes; the pass finishes without it, and the runtime builds it @@ -1544,8 +1545,8 @@ impl Pipelines { // A stale blob is not a usable cache hit. Do not let its presence bypass the offline // size cap and start the expensive uncached driver compile. if cached.is_some() && from_cache.is_none() && over_cap { - if let Some(path) = path.as_deref() { - let _ = std::fs::remove_file(path); + if let Some(loaded) = loaded.take() { + let _ = loaded.invalidate(); } *self.running.0.lock().unwrap() -= 1; self.running.1.notify_one();