From eaecb86dc7315fb52c11cffaaf8c2ec13dca129c Mon Sep 17 00:00:00 2001 From: crastinator <141459787+crastinator@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:18:30 +0200 Subject: [PATCH] Recompiler: a DXC folder that fails to load leaves its dxil.dll unloaded (#2413) load() loads a candidate folder's dxil.dll before its dxcompiler.dll. When dxcompiler.dll did not load, or had no DxcCreateInstance, the loop went on to the next folder and left both libraries loaded. A dxil.dll kept from a failed folder is then the one the next folder's dxcompiler.dll gets when it loads dxil.dll by name, so the validator could come from the wrong DXC. The per-folder attempt is now load_candidate, which frees both on failure. The test gives it a folder with a copy of a real dxil.dll and a dxcompiler.dll that is not a library, and checks that the copy is no longer loaded afterwards; it fails without the FreeLibrary calls. Part of #2413 (the empty-blob half is #2441). --- crates/xcl/src/gpu/recomp/dxc.rs | 80 +++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs index 0a7a79b09..b095c0a31 100644 --- a/crates/xcl/src/gpu/recomp/dxc.rs +++ b/crates/xcl/src/gpu/recomp/dxc.rs @@ -203,6 +203,22 @@ pub fn toolchain() -> Toolchain { mod version_tests { use super::*; + #[test] + fn a_folder_whose_dxcompiler_does_not_load_leaves_its_dxil_unloaded() { + use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW; + let Some(real) = candidates().into_iter().map(|d| d.join("dxil.dll")).find(|p| p.is_file()) else { + return crate::gpu::recomp::runner::skip_test("no DXC"); + }; + let dir = std::env::temp_dir().join(format!("xcl-dxc-candidate-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::copy(&real, dir.join("dxil.dll")).unwrap(); + std::fs::write(dir.join("dxcompiler.dll"), b"not a library").unwrap(); + assert_eq!(unsafe { load_candidate(&dir) }, None); + let left = unsafe { GetModuleHandleW(crate::util::wide(&dir.join("dxil.dll").to_string_lossy()).as_ptr()) }; + let _ = std::fs::remove_dir_all(&dir); + assert!(left.is_null(), "the failed folder's dxil.dll is still loaded"); + } + #[test] fn versions_parse_and_compare_as_numbers() { assert_eq!(parse_version("10.0.26100.0"), Some([10, 0, 26100, 0])); @@ -310,33 +326,9 @@ pub fn location() -> Result<&'static Path, String> { fn load() -> Result<&'static (PathBuf, usize), String> { LIB.get_or_init(|| { - use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW}; for dir in candidates() { - let dxc = dir.join("dxcompiler.dll"); - if !dxc.is_file() { - continue; - } - unsafe { - let dxil = dir.join("dxil.dll"); - let signer = if dxil.is_file() { LoadLibraryW(crate::util::wide(&dxil.to_string_lossy()).as_ptr()) } else { std::ptr::null_mut() }; - let h = LoadLibraryW(crate::util::wide(&dxc.to_string_lossy()).as_ptr()); - if h.is_null() { - continue; - } - if let Some(f) = GetProcAddress(h, b"DxcCreateInstance\0".as_ptr()) { - let mut t = Toolchain { dir: dir.display().to_string(), ..Default::default() }; - t.dxcompiler = library(std::mem::transmute::(f as usize), &CLSID_DxcCompiler, &dxc); - if let Some(g) = if signer.is_null() { None } else { GetProcAddress(signer, b"DxcCreateInstance\0".as_ptr()) } { - t.dxil = library(std::mem::transmute::(g as usize), &windows::Win32::Graphics::Direct3D::Dxc::CLSID_DxcValidator, &dxil); - } - let signer = if signer.is_null() { "no dxil.dll".to_string() } else { format!("dxil {}", t.dxil) }; - crate::xlog_info!("DXC: dxcompiler {}, {signer}, from {}", t.dxcompiler, t.dir); - if t.outdated() { - crate::xlog_warn!("DXC {} is older than {}.{}: {OUTDATED_COST}; {OUTDATED_FIX}", t.dxcompiler, RECOMMENDED.0, RECOMMENDED.1); - } - let _ = TOOLCHAIN.set(t); - return Ok((dir, f as usize)); - } + if let Some(f) = unsafe { load_candidate(&dir) } { + return Ok((dir, f)); } } Err("dxcompiler.dll not found (set XCL_DXC_DIR or install the Windows SDK)".into()) @@ -345,6 +337,42 @@ fn load() -> Result<&'static (PathBuf, usize), String> { .map_err(|e| e.clone()) } +/// `dir`'s DxcCreateInstance (its toolchain recorded), or None. A folder that does not give one +/// leaves nothing of it loaded: a dxil.dll kept from it would be the one a later folder's +/// dxcompiler.dll gets when it loads dxil.dll by name. +unsafe fn load_candidate(dir: &Path) -> Option { + use windows_sys::Win32::Foundation::FreeLibrary; + use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW}; + let dxc = dir.join("dxcompiler.dll"); + if !dxc.is_file() { + return None; + } + let dxil = dir.join("dxil.dll"); + let signer = if dxil.is_file() { LoadLibraryW(crate::util::wide(&dxil.to_string_lossy()).as_ptr()) } else { std::ptr::null_mut() }; + let h = LoadLibraryW(crate::util::wide(&dxc.to_string_lossy()).as_ptr()); + let Some(f) = (if h.is_null() { None } else { GetProcAddress(h, b"DxcCreateInstance\0".as_ptr()) }) else { + if !h.is_null() { + FreeLibrary(h); + } + if !signer.is_null() { + FreeLibrary(signer); + } + return None; + }; + let mut t = Toolchain { dir: dir.display().to_string(), ..Default::default() }; + t.dxcompiler = library(std::mem::transmute::(f as usize), &CLSID_DxcCompiler, &dxc); + if let Some(g) = if signer.is_null() { None } else { GetProcAddress(signer, b"DxcCreateInstance\0".as_ptr()) } { + t.dxil = library(std::mem::transmute::(g as usize), &windows::Win32::Graphics::Direct3D::Dxc::CLSID_DxcValidator, &dxil); + } + let signer = if signer.is_null() { "no dxil.dll".to_string() } else { format!("dxil {}", t.dxil) }; + crate::xlog_info!("DXC: dxcompiler {}, {signer}, from {}", t.dxcompiler, t.dir); + if t.outdated() { + crate::xlog_warn!("DXC {} is older than {}.{}: {OUTDATED_COST}; {OUTDATED_FIX}", t.dxcompiler, RECOMMENDED.0, RECOMMENDED.1); + } + let _ = TOOLCHAIN.set(t); + Some(f as usize) +} + /// The PC compiler's DxcCreateInstance (for titles that use the console's `dxcompiler_xs.dll`). pub fn create_instance() -> Option { load().ok().map(|&(_, f)| f)