From be9423c6285a6a9b6cc373f53fcdd71d9a5a9970 Mon Sep 17 00:00:00 2001 From: LUIS LARIOS Date: Mon, 23 Nov 2026 19:03:01 -0600 Subject: [PATCH 1/2] kernelx: a lazy page whose commit fails stays lazy, and says so commit() removed the page from LAZY before VirtualAlloc, so a failed commit - the system's commit limit, which a retail title on a 16 GB machine gets close to - left the page out of the set with nothing committed. The next access to it was no longer recognised as lazy, the handler returned EXCEPTION_CONTINUE_SEARCH, and the title died with 0xc0000005 on a page that was still MEM_RESERVE, with no log line: the crash named a wild write instead of an out-of-memory. The page now leaves the set only once the allocation succeeded. The allocation is a parameter (commit_with), so a test can make it fail and assert that the page stays lazy, that a retry that succeeds takes it out, and that a page that was never lazy is not touched. --- crates/xcl/src/hle/kernelx/lazy.rs | 49 ++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/kernelx/lazy.rs b/crates/xcl/src/hle/kernelx/lazy.rs index fa750b9a4..42d5fb74a 100644 --- a/crates/xcl/src/hle/kernelx/lazy.rs +++ b/crates/xcl/src/hle/kernelx/lazy.rs @@ -37,11 +37,34 @@ fn install() { /// Commit `page` if it is a lazy one (true: it was). unsafe fn commit(page: usize) -> bool { + // SAFETY: the caller passes a page it marked lazy. + commit_with(page, |p| unsafe { + !VirtualAlloc(p as *const c_void, PAGE, MEM_COMMIT, PAGE_READWRITE).is_null() + }) +} + +/// [`commit`] with the allocation handed in, so a test can make it fail. +/// +/// The page leaves the lazy set only once it is really committed. When the commit fails - the +/// system's commit limit is reached, which a retail title on a 16 GB machine gets close to - the +/// page stays lazy and is retried on the next access, instead of being dropped from the set with +/// nothing committed: that left the next fault on it unrecognised, so the title died with +/// `0xc0000005` on a reserved page and nothing said why. +unsafe fn commit_with(page: usize, alloc: impl FnOnce(usize) -> bool) -> bool { let mut g = LAZY.write().unwrap_or_else(|e| e.into_inner()); - if !g.remove(&page) { + if !g.contains(&page) { + return false; + } + if !alloc(page) { + let e = std::io::Error::last_os_error(); + drop(g); + crate::xlog_warn!( + "lazy page {page:#x}: commit failed ({e}); the title's next access to it faults here" + ); return false; } - !VirtualAlloc(page as *const c_void, PAGE, MEM_COMMIT, PAGE_READWRITE).is_null() + g.remove(&page); + true } unsafe extern "system" fn handler(info: *mut EXCEPTION_POINTERS) -> i32 { @@ -105,6 +128,28 @@ mod tests { use super::*; use windows_sys::Win32::System::Memory::{VirtualFree, MEM_RELEASE, MEM_RESERVE}; + /// A commit that fails (the system's commit limit) leaves the page lazy, so the next access + /// retries and the crash names the page instead of a silent `MEM_RESERVE` fault. + #[test] + fn a_failed_commit_leaves_the_page_lazy() { + unsafe { + let r = VirtualAlloc(std::ptr::null(), PAGE, MEM_RESERVE, PAGE_READWRITE) as usize; + assert_ne!(r, 0); + mark(r); + assert!(!commit_with(r, |_| false), "a failed commit is not a commit"); + assert!( + LAZY.read().unwrap_or_else(|e| e.into_inner()).contains(&r), + "the page must stay lazy after a failed commit" + ); + // The retry that succeeds takes it out of the set. + assert!(commit_with(r, |_| true)); + assert!(!LAZY.read().unwrap_or_else(|e| e.into_inner()).contains(&r)); + // A page that was never lazy is not committed either. + assert!(!commit_with(r, |_| true)); + VirtualFree(r as *mut c_void, 0, MEM_RELEASE); + } + } + /// A lazy page reads as zeros on first touch, keeps what is written; `ensure` commits it /// without a fault. #[test] From a01193bbb8141f542be6a0f0c7f135b5bab3c55b Mon Sep 17 00:00:00 2001 From: LUIS LARIOS Date: Mon, 23 Nov 2026 19:07:41 -0600 Subject: [PATCH 2/2] tests: when the test title does not finish, print why The assertion in the debug-layer end-to-end test only printed the launcher's opening lines, so a failure looked like "the test title did not run to the end" with no exit code and no log. I hit exactly that once here (it did not reproduce in five later runs, issue #500) and could not tell whether the title crashed, was refused, or never started. Now the failure carries the process exit code, the launcher's stdout and stderr, and the last 40 lines of xcl.log, which is where the debug layer's own objections end up: the test title did not run to the end (exit Some(0)): --- xcl stdout --- ... --- xcl stderr --- --- xcl.log, last 40 lines --- xcl: D3D12 debug layer (error, id 93): ID3D12Device::CreatePixelShader: ... The log is read before the assertion and with unwrap_or_default, so a missing log no longer panics on its own and hides the reason the title stopped; the "debug layer did not come up" assertion prints the same tail. cargo test -p xcl --test e2e test_title_draws: ok, 1 passed. Test-only, no production code touched. --- crates/xcl/tests/e2e.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/xcl/tests/e2e.rs b/crates/xcl/tests/e2e.rs index 57301087f..cd3ba25f9 100644 --- a/crates/xcl/tests/e2e.rs +++ b/crates/xcl/tests/e2e.rs @@ -6,6 +6,13 @@ use std::collections::BTreeMap; use std::path::{Path, PathBuf}; +/// The last `n` lines of `s`, for an assertion message. +const TAIL: usize = 40; + +fn tail(s: &str, n: usize) -> String { + let lines: Vec<&str> = s.lines().collect(); + lines[lines.len().saturating_sub(n)..].join("\n") +} use std::process::Command; fn fnv(bytes: &[u8]) -> u64 { @@ -264,9 +271,18 @@ fn test_title_draws_with_a_root_signature_under_the_debug_layer() { } let out = Command::new(&xcl).arg("run").arg(&game).arg("--").arg("--hello").env("XCL_DATA_ROOT", &data).env("XCL_D3D12_DEBUG", "1").output().expect("spawn xcl"); let stdout = String::from_utf8_lossy(&out.stdout); - assert!(stdout.contains("xcl_testtitle: done, failures: 0"), "the test title did not run to the end:\n{stdout}"); - let log = std::fs::read_to_string(data.join("XCL.TestTitle").join("logs").join("xcl.log")).unwrap(); - assert!(log.contains("D3D12: debug layer enabled"), "the debug layer did not come up"); + let stderr = String::from_utf8_lossy(&out.stderr); + let log = std::fs::read_to_string(data.join("XCL.TestTitle").join("logs").join("xcl.log")).unwrap_or_default(); + assert!( + stdout.contains("xcl_testtitle: done, failures: 0"), + "the test title did not run to the end (exit {:?}):\n\ + --- xcl stdout ---\n{stdout}\n\ + --- xcl stderr ---\n{stderr}\n\ + --- xcl.log, last {TAIL} lines ---\n{}", + out.status.code(), + tail(&log, TAIL) + ); + assert!(log.contains("D3D12: debug layer enabled"), "the debug layer did not come up:\n{}", tail(&log, TAIL)); let bad: Vec<&str> = log.lines().filter(|l| l.contains("No root signature has been set")).collect(); assert!(bad.is_empty(), "{} draws without a root signature, first: {}", bad.len(), bad[0]); }