From edc2b4580381c7b795a6930625a185957b94fc05 Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Fri, 27 Nov 2026 07:04:24 -0800 Subject: [PATCH 1/2] VFS: XCL_DRIVER_SHADER_CACHE=0 refuses the driver its shader cache file The NVIDIA driver maps its shader cache file of a process (AppData\LocalNVIDIA\DXCache\.nvph) whole and reads all of it at start. For a retail title on an RTX 3060 Ti the file had grown to 4 GiB: 3.9 GB of xcl.exe's working set from the first second (sampled from outside with QueryWorkingSetEx), on a 32 GB machine the title fills until Windows has under 1 GB left. XCL keeps the driver's pipeline blobs in its own cache, so the file holds them a second time. With XCL_DRIVER_SHADER_CACHE=0 the driver's opens of such a file fail with access denied. Off by default. Co-Authored-By: Claude Fable 5.2 --- crates/xcl/src/vfs_hooks.rs | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/xcl/src/vfs_hooks.rs b/crates/xcl/src/vfs_hooks.rs index 437951d401..a62515a1c1 100644 --- a/crates/xcl/src/vfs_hooks.rs +++ b/crates/xcl/src/vfs_hooks.rs @@ -82,6 +82,33 @@ fn log_open(api: &str, path: &str, flags: u32) { } } +/// Whether `path` is the NVIDIA driver's shader cache file of a process +/// (`…\NVIDIA\DXCache\.nvph`, under `AppData\Local` or `AppData\LocalLow`). +fn is_driver_shader_cache(path: &str) -> bool { + let lower = path.to_ascii_lowercase(); + lower.ends_with(".nvph") && lower.contains(r"\nvidia\dxcache\") +} + +/// `XCL_DRIVER_SHADER_CACHE=0`: the display driver's opens of its own shader cache file fail +/// (access denied), so it runs without one. The NVIDIA driver maps that file whole into the +/// process and reads all of it at start: for a retail title whose file had grown to 4 GiB, +/// 3.9 GB of the process's working set from the first second, on a machine whose memory the +/// title fills. XCL keeps the driver's pipeline blobs in its own cache +/// (`XCL_PSO_DISK_CACHE_MIN_MS`), so the driver's file holds them a second time. +fn driver_cache_refused(path: &str) -> bool { + static OFF: std::sync::OnceLock = std::sync::OnceLock::new(); + if !*OFF.get_or_init(|| std::env::var("XCL_DRIVER_SHADER_CACHE").is_ok_and(|v| v.trim() == "0")) || !is_driver_shader_cache(path) { + return false; + } + static N: AtomicUsize = AtomicUsize::new(0); + let n = N.fetch_add(1, Ordering::Relaxed) + 1; + if n <= 4 || n.is_power_of_two() { + crate::xlog_info!("VFS: the driver's shader cache file {path} is refused (XCL_DRIVER_SHADER_CACHE=0): #{n}"); + } + unsafe { SetLastError(vfs::ERROR_ACCESS_DENIED) }; + true +} + fn apply_open_decision( decision: Decision, disposition: u32, @@ -111,6 +138,9 @@ unsafe extern "system" fn hk_create_file_w(n: *const u16, acc: u32, sh: u32, sa: let orig = orig!(O_CREATE_FILE_W as FnCreateFileW); let (Some(_g), Some(v)) = (Guard::enter(), vfs::get()) else { return orig(n, acc, sh, sa, disp, fl, t) }; let Some(path) = from_wide_ptr(n) else { return orig(n, acc, sh, sa, disp, fl, t) }; + if driver_cache_refused(&path) { + return INVALID_HANDLE_VALUE; + } log_open("CreateFileW", &path, fl); apply_open_decision( v.open_decision(&path, acc, disp, fl), @@ -124,6 +154,9 @@ unsafe extern "system" fn hk_create_file_a(n: *const u8, acc: u32, sh: u32, sa: let orig = orig!(O_CREATE_FILE_A as FnCreateFileA); let (Some(_g), Some(v)) = (Guard::enter(), vfs::get()) else { return orig(n, acc, sh, sa, disp, fl, t) }; let Some(path) = from_ansi_ptr(n) else { return orig(n, acc, sh, sa, disp, fl, t) }; + if driver_cache_refused(&path) { + return INVALID_HANDLE_VALUE; + } log_open("CreateFileA", &path, fl); apply_open_decision( v.open_decision(&path, acc, disp, fl), @@ -139,6 +172,9 @@ unsafe extern "system" fn hk_create_file_2(n: *const u16, acc: u32, sh: u32, dis let Some(path) = from_wide_ptr(n) else { return orig(n, acc, sh, disp, params) }; // CREATEFILE2_EXTENDED_PARAMETERS { dwSize, dwFileAttributes, dwFileFlags, ... } let flags = if params.is_null() { 0 } else { *((params as *const u8).add(8) as *const u32) }; + if driver_cache_refused(&path) { + return INVALID_HANDLE_VALUE; + } log_open("CreateFile2", &path, flags); apply_open_decision( v.open_decision(&path, acc, disp, flags), @@ -1249,6 +1285,15 @@ mod tests { CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, }; + #[test] + fn only_the_drivers_shader_cache_files_are_recognised() { + assert!(is_driver_shader_cache(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\32e6a9dbc29925ed.nvph")); + assert!(is_driver_shader_cache(r"\\?\c:\users\a\appdata\locallow\nvidia\dxcache\0002A9DBC29925ED.NVPH")); + assert!(!is_driver_shader_cache(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\"), "the folder itself"); + assert!(!is_driver_shader_cache(r"C:\Games\Title\data\cache.nvph"), "a title's own file of that type"); + assert!(!is_driver_shader_cache(r"C:\ProgramData\NVIDIA Corporation\Drs\nvdrssel.bin"), "the driver's settings"); + } + struct HookFixture { dir: PathBuf, vfs: vfs::Vfs, From 7cd669763e407b3ba1fd80a32e48c9b3a4f8e2ff Mon Sep 17 00:00:00 2001 From: Prime <215761102+Prime1997@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:14:42 -0800 Subject: [PATCH 2/2] VFS: only the driver's own cache folders count as its shader cache is_driver_shader_cache took any path that contained \NVIDIA\DXCache\ and ended in .nvph, so with XCL_DRIVER_SHADER_CACHE=0 a title's own file such as G:\assets\NVIDIA\DXCache\title.nvph was refused too (found in review on #3048). Now only a .nvph file directly in NVIDIA\DXCache of this user's AppData\Local (LOCALAPPDATA) or of the LocalLow folder beside it counts, with or without \?\. The test has the review's path, a file in a subfolder and another user's folder among its negative cases. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/vfs_hooks.rs | 54 ++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/crates/xcl/src/vfs_hooks.rs b/crates/xcl/src/vfs_hooks.rs index a62515a1c1..365807bcb8 100644 --- a/crates/xcl/src/vfs_hooks.rs +++ b/crates/xcl/src/vfs_hooks.rs @@ -82,11 +82,37 @@ fn log_open(api: &str, path: &str, flags: u32) { } } -/// Whether `path` is the NVIDIA driver's shader cache file of a process -/// (`…\NVIDIA\DXCache\.nvph`, under `AppData\Local` or `AppData\LocalLow`). +/// Whether `path` is the NVIDIA driver's shader cache file of a process: a `.nvph` file directly +/// in `NVIDIA\DXCache` of this user's `AppData\Local` (`LOCALAPPDATA`) or of the `LocalLow` +/// folder beside it, where the driver keeps them. A title's own file in folders of those names +/// elsewhere is not one. fn is_driver_shader_cache(path: &str) -> bool { - let lower = path.to_ascii_lowercase(); - lower.ends_with(".nvph") && lower.contains(r"\nvidia\dxcache\") + static ROOTS: std::sync::OnceLock> = std::sync::OnceLock::new(); + let roots = ROOTS.get_or_init(|| driver_cache_roots(&std::env::var("LOCALAPPDATA").unwrap_or_default())); + is_driver_cache_in(path, roots) +} + +/// The folders the driver keeps its shader cache files in for the user's `AppData\Local` folder +/// `local`, in lowercase: `\nvidia\dxcache\` and `\..\locallow\nvidia\dxcache\`. +fn driver_cache_roots(local: &str) -> Vec { + let local = local.trim().replace('/', "\\").trim_end_matches('\\').to_ascii_lowercase(); + if local.is_empty() { + return Vec::new(); + } + let mut roots = vec![format!(r"{local}\nvidia\dxcache\")]; + if let Some((parent, _)) = local.rsplit_once('\\') { + roots.push(format!(r"{parent}\locallow\nvidia\dxcache\")); + } + roots +} + +/// Whether `path` (with or without `\\?\`) is a `.nvph` file directly in one of `roots`. +fn is_driver_cache_in(path: &str, roots: &[String]) -> bool { + let lower = path.replace('/', "\\").to_ascii_lowercase(); + let plain = lower.strip_prefix(r"\\?\").unwrap_or(&lower); + roots.iter().any(|root| { + plain.strip_prefix(root.as_str()).is_some_and(|name| name.len() > ".nvph".len() && name.ends_with(".nvph") && !name.contains('\\')) + }) } /// `XCL_DRIVER_SHADER_CACHE=0`: the display driver's opens of its own shader cache file fail @@ -1287,11 +1313,21 @@ mod tests { #[test] fn only_the_drivers_shader_cache_files_are_recognised() { - assert!(is_driver_shader_cache(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\32e6a9dbc29925ed.nvph")); - assert!(is_driver_shader_cache(r"\\?\c:\users\a\appdata\locallow\nvidia\dxcache\0002A9DBC29925ED.NVPH")); - assert!(!is_driver_shader_cache(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\"), "the folder itself"); - assert!(!is_driver_shader_cache(r"C:\Games\Title\data\cache.nvph"), "a title's own file of that type"); - assert!(!is_driver_shader_cache(r"C:\ProgramData\NVIDIA Corporation\Drs\nvdrssel.bin"), "the driver's settings"); + let roots = driver_cache_roots(r"C:\Users\a\AppData\Local"); + let is = |path: &str| is_driver_cache_in(path, &roots); + assert!(is(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\32e6a9dbc29925ed.nvph")); + assert!(is(r"\\?\c:\users\a\appdata\locallow\nvidia\dxcache\0002A9DBC29925ED.NVPH")); + assert!(!is(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\"), "the folder itself"); + assert!(!is(r"C:\Games\Title\data\cache.nvph"), "a title's own file of that type"); + assert!(!is(r"G:\assets\NVIDIA\DXCache\title.nvph"), "a title's path with the driver's folder names"); + assert!(!is(r"C:\Users\a\AppData\Local\NVIDIA\DXCache\sub\title.nvph"), "not directly in the driver's folder"); + assert!(!is(r"C:\Users\b\AppData\Local\NVIDIA\DXCache\32e6a9dbc29925ed.nvph"), "another user's"); + assert!(!is(r"C:\ProgramData\NVIDIA Corporation\Drs\nvdrssel.bin"), "the driver's settings"); + // this process's own folder, as the driver opens it + if let Ok(local) = std::env::var("LOCALAPPDATA") { + assert!(is_driver_shader_cache(&format!(r"{local}\NVIDIA\DXCache\32e6a9dbc29925ed.nvph"))); + assert!(!is_driver_shader_cache(r"G:\assets\NVIDIA\DXCache\title.nvph")); + } } struct HookFixture {