From bb6f91b58e8d9942abe80a3ff4f88f218e447581 Mon Sep 17 00:00:00 2001 From: emirduran707 Date: Tue, 24 Nov 2026 21:54:57 +0300 Subject: [PATCH] kernelx: GetThreadName/SetThreadName set the Win32 error of NT failures SetThreadDescription and GetThreadDescription fail with HRESULT_FROM_NT of the status (0xD0000008 for a bad handle), which hresult_to_win32 did not recognise, so every failure set ERROR_INVALID_PARAMETER. Map the NT case through RtlNtStatusToDosError: a null handle now sets ERROR_INVALID_HANDLE, as measured on Windows 11 26300. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/kernelx.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 8aa3bc2002..8f10cb395c 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1372,9 +1372,12 @@ pub unsafe extern "system" fn UnregisterAppConstrainedChangeNotification(_regist // Threads / system info // --------------------------------------------------------------------------------------- +// (Set/GetThreadDescription fail with HRESULT_FROM_NT of the status: 0xD0000008 for a bad handle) fn hresult_to_win32(hr: i32) -> u32 { if (hr as u32 & 0xFFFF_0000) == 0x8007_0000 { hr as u32 & 0xFFFF + } else if hr as u32 & 0x1000_0000 != 0 { + unsafe { RtlNtStatusToDosError(hr & !0x1000_0000) } } else { fnd::ERROR_INVALID_PARAMETER } @@ -1515,6 +1518,7 @@ extern "system" { fn RtlGetVersion(info: *mut OsVersionInfoW) -> i32; fn NtQueryTimerResolution(coarsest: *mut u32, finest: *mut u32, current: *mut u32) -> i32; fn NtSetTimerResolution(desired: u32, set: u8, current: *mut u32) -> i32; + fn RtlNtStatusToDosError(status: i32) -> u32; } pub fn host_os_version() -> (u32, u32, u32) { @@ -2611,10 +2615,13 @@ mod tests { assert_eq!(GetThreadName(cur, full.as_mut_ptr(), full.len(), std::ptr::null_mut()), 1); assert_eq!(&full[..], &want[..]); - // Invalid thread handle -> FALSE + Win32 error set. + // Invalid thread handle -> FALSE + ERROR_INVALID_HANDLE (the HRESULT is HRESULT_FROM_NT). fnd::SetLastError(0); assert_eq!(GetThreadName(std::ptr::null_mut(), full.as_mut_ptr(), full.len(), &mut ret), 0); - assert_ne!(fnd::GetLastError(), 0); + assert_eq!(fnd::GetLastError(), fnd::ERROR_INVALID_HANDLE); + fnd::SetLastError(0); + assert_eq!(SetThreadName(std::ptr::null_mut(), want.as_ptr()), 0); + assert_eq!(fnd::GetLastError(), fnd::ERROR_INVALID_HANDLE); }) .join() .unwrap();