From 8fde77a63a808ac1f125aa105967ac6204c9aec1 Mon Sep 17 00:00:00 2001 From: BlestTerror <360342878+BlestTerror@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:06:28 +0100 Subject: [PATCH] Tests under Wine: Windows' own answers are checked only on Windows Three unit tests compared XCL with the host's own Win32 behaviour, which Wine does not reproduce; XCL's answers are the Windows ones in all three: - GetProcAddress(module, NULL): Windows sets ERROR_INVALID_PARAMETER, Wine ERROR_PROC_NOT_FOUND. XCL's code is now checked against ERROR_INVALID_PARAMETER; the host's only on Windows. - CreateFileW on a directory with FILE_FLAG_BACKUP_SEMANTICS and TRUNCATE_EXISTING: Wine answers ERROR_FILE_EXISTS. The VFS decision is still checked against the Windows codes. - A case-only MoveFileExW inside the overlay: Wine succeeds but keeps the old name, so the overlay entry is compared without case there. The Wine check from the loader (ntdll's wine_get_version) is now util::under_wine(). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011Qu7u1UaLUhn6MfAcvkdjG --- crates/xcl/src/hle/kernelx.rs | 6 +++++- crates/xcl/src/loader.rs | 4 +--- crates/xcl/src/util.rs | 10 ++++++++++ crates/xcl/src/vfs.rs | 6 +++++- crates/xcl/src/vfs_hooks.rs | 8 +++++++- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 690a5f0..33affa3 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1765,7 +1765,11 @@ mod tests { fnd::SetLastError(0); assert_eq!(GetProcAddress(k32, std::ptr::null()), 0); - assert_eq!(fnd::GetLastError(), win32_err); + assert_eq!(fnd::GetLastError(), fnd::ERROR_INVALID_PARAMETER); + // Windows' own answer; Wine's GetProcAddress says ERROR_PROC_NOT_FOUND instead + if !crate::util::under_wine() { + assert_eq!(win32_err, fnd::ERROR_INVALID_PARAMETER); + } // Non-zero ordinal lookup still resolves when valid (ordinal #1 on kernel32). assert_ne!(GetProcAddress(k32, 1usize as *const u8), 0); diff --git a/crates/xcl/src/loader.rs b/crates/xcl/src/loader.rs index 016b5e3..59dfd37 100644 --- a/crates/xcl/src/loader.rs +++ b/crates/xcl/src/loader.rs @@ -328,9 +328,7 @@ pub fn run_main(m: &Arc) -> u32 { }; unsafe { // PEB.ImageBaseAddress → GetModuleHandle(NULL), resources with NULL module, … - use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress}; - let wine = GetProcAddress(GetModuleHandleA(c"ntdll.dll".as_ptr() as *const u8), c"wine_get_version".as_ptr() as *const u8).is_some(); - if wine { + if crate::util::under_wine() { crate::xlog_info!("Wine: PEB.ImageBaseAddress left alone (its loader needs the real main image there)"); } else { *(peb().add(0x10) as *mut usize) = m.base; diff --git a/crates/xcl/src/util.rs b/crates/xcl/src/util.rs index 6112d93..e3d6530 100644 --- a/crates/xcl/src/util.rs +++ b/crates/xcl/src/util.rs @@ -104,6 +104,16 @@ pub unsafe fn from_ansi_ptr(p: *const u8) -> Option { Some(String::from_utf16_lossy(&buf)) } +/// The process runs under Wine (its ntdll exports `wine_get_version`). +pub fn under_wine() -> bool { + use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress}; + static WINE: std::sync::OnceLock = std::sync::OnceLock::new(); + // SAFETY: ntdll is always loaded; both names are NUL-terminated. + *WINE.get_or_init(|| unsafe { + GetProcAddress(GetModuleHandleA(c"ntdll.dll".as_ptr() as *const u8), c"wine_get_version".as_ptr() as *const u8).is_some() + }) +} + /// Normalised DLL key: lower-case file name, ".dll" appended when no extension. pub fn dll_key(name: &str) -> String { let base = name.rsplit(['\\', '/']).next().unwrap_or(name); diff --git a/crates/xcl/src/vfs.rs b/crates/xcl/src/vfs.rs index 464fa49..541669b 100644 --- a/crates/xcl/src/vfs.rs +++ b/crates/xcl/src/vfs.rs @@ -897,7 +897,11 @@ mod tests { ) }; assert_eq!(h, windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE); - assert_eq!(unsafe { windows_sys::Win32::Foundation::GetLastError() }, expected); + // Windows' own codes are the reference; Wine answers a directory opened with + // FILE_FLAG_BACKUP_SEMANTICS and TRUNCATE_EXISTING with ERROR_FILE_EXISTS + if !crate::util::under_wine() { + assert_eq!(unsafe { windows_sys::Win32::Foundation::GetLastError() }, expected); + } assert_eq!(v.open_decision("G:\\data", GENERIC_WRITE, TRUNCATE_EXISTING, flags), Decision::Fail(expected)); } assert!(!v.visible_in_overlay("data")); diff --git a/crates/xcl/src/vfs_hooks.rs b/crates/xcl/src/vfs_hooks.rs index 7a75230..e71567f 100644 --- a/crates/xcl/src/vfs_hooks.rs +++ b/crates/xcl/src/vfs_hooks.rs @@ -1218,7 +1218,13 @@ mod tests { .unwrap() .map(|e| e.unwrap().file_name().to_string_lossy().into_owned()) .collect(); - assert_eq!(ov_names, vec!["Hello.txt".to_string()]); + if crate::util::under_wine() { + // Wine's MoveFileExW succeeds on a case-only rename but keeps the old name + assert_eq!(ov_names.len(), 1); + assert!(ov_names[0].eq_ignore_ascii_case("Hello.txt"), "{ov_names:?}"); + } else { + assert_eq!(ov_names, vec!["Hello.txt".to_string()]); + } // Exact self-rename is a safe no-op. let ok = unsafe { move_file_vfs(v, "G:\\data\\gone.txt", "G:\\data\\gone.txt", 0, real_move) };