From c1c22863e8ec6d52198f9c1b465226f538cbacb1 Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:26:34 +0000 Subject: [PATCH] D3D12.X: texture workers copy title memory instead of borrowing it A texture subresource's bytes come from title_bytes(), which lends the title's page in place when eadable says the range reads. A worker detiles them while the title may decommit a page, and the borrow faults in the copy (#2841). The workers now read through ead_committed: it copies the committed pages with ReadProcessMemory, run by run, and zeroes the rest, so a decommit during the copy is read short instead of faulting. The title's own submit keeps the in-place path. Test: a committed, readable page is still borrowed by title_bytes, and the worker's title_bytes_read copies it (fails when the worker path borrows instead). --- crates/xcl/src/hle/d3d12x/title_textures.rs | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 6d97bc6e4e..04bd99b1dc 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -1464,18 +1464,25 @@ unsafe fn title_bytes(at: usize, len: usize) -> std::borrow::Cow<'static, [u8]> } } +/// `title_bytes` for a texture worker: a copy (`read_committed`) even where the range reads now. +/// The title can decommit a page between that check and the detile, and a borrow would fault +/// (#2841); `read_committed` reads through `ReadProcessMemory`, page run by page run. +unsafe fn title_bytes_read(at: usize, len: usize) -> std::borrow::Cow<'static, [u8]> { + std::borrow::Cow::Owned(read_committed(at, len)) +} + /// Every subresource (D3D12 order) of a texture from the title's memory at `va`. unsafe fn subresources(va: usize, tex: &xg::Texture) -> Vec { let Some(tex) = tex.texels() else { return Vec::new() }; let c = &tex.chain; let bpe = tex.bpe as usize; if tex.depth > 0 { - let mem = title_bytes(va, tex.size() as usize); + let mem = title_bytes_read(va, tex.size() as usize); return c.levels.iter().enumerate().map(|(i, l)| Subresource { depth: l.depth, rows: l.height, row_bytes: l.width * bpe, data: c.detile_volume(&mem, i) }).collect(); } let mut out = Vec::with_capacity(tex.array as usize * c.levels.len()); for s in 0..tex.array as usize { - let slice = title_bytes(va + s * c.slice_bytes, c.slice_bytes); + let slice = title_bytes_read(va + s * c.slice_bytes, c.slice_bytes); for (i, l) in c.levels.iter().enumerate() { out.push(Subresource { depth: 1, rows: l.height, row_bytes: l.width * bpe, data: c.detile_level(&slice, i, s) }); } @@ -1837,6 +1844,25 @@ mod tests { } } + /// A texture worker does not borrow title memory: a page the title decommits between the + /// readability check and the detile would fault the borrow (#2841). + #[test] + fn a_texture_worker_copies_title_memory_instead_of_borrowing_it() { + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE}; + unsafe { + let base = VirtualAlloc(std::ptr::null(), 1 << 12, MEM_RESERVE, PAGE_READWRITE) as usize; + assert_ne!(base, 0); + VirtualAlloc(base as *const c_void, 1 << 12, MEM_COMMIT, PAGE_READWRITE); + std::ptr::write_bytes(base as *mut u8, 0x5A, 1 << 12); + // this range reads now: `title_bytes` lends it in place + assert!(matches!(title_bytes(base, 1 << 12), std::borrow::Cow::Borrowed(_))); + let read = title_bytes_read(base, 1 << 12); + assert!(matches!(read, std::borrow::Cow::Owned(_)), "a worker must not hold title memory"); + assert!(read.iter().all(|&b| b == 0x5A)); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + /// Only the wanted parts of the watched regions are scanned, merged, one region each. #[test] fn watch_ranges_clip_and_merge() {