Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions crates/xcl/src/hle/d3d12x/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,11 @@ unsafe extern "system" fn Heap_GetDesc16(this: *mut NullObj, ret: *mut u8) -> *m
}
ret
}
unsafe extern "system" fn Heap_GetHandleStart(this: *mut NullObj, ret: *mut u64) -> *mut u64 {
if !ret.is_null() {
*ret = (*this).value.load(Ordering::Relaxed);
}
ret
/// #9 / #10 GetCPU / GetGPUDescriptorHandleForHeapStart: console code takes the handle in rax
/// and passes only `this` (as `real::heap_cpu_start_x`); rdx holds whatever the caller left
/// there, so it must not be written through as the PC's hidden return pointer.
unsafe extern "system" fn Heap_GetHandleStart(this: *mut NullObj) -> u64 {
(*this).value.load(Ordering::Relaxed)
}
unsafe extern "system" fn Resource_Map(this: *mut NullObj, _sub: u32, _range: *const c_void, data: *mut *mut c_void) -> HRESULT {
let mut g = (*this).mem.lock().unwrap();
Expand Down Expand Up @@ -792,4 +792,56 @@ mod tests {
assert!(crate::stubs::all_stubs().iter().any(|s| s.name == "ID3D12Device::#200" && s.calls.load(Ordering::Relaxed) > 0));
}
}

/// Console code calls descriptor heap #9 / #10 with only `this` and takes the start in rax;
/// rdx is left over from the caller and must not be written (the test title's rdx pointed
/// into its read-only .rdata: an access violation under Wine on the null device).
#[test]
fn heap_start_handles_come_back_in_rax() {
unsafe {
let mut dev = std::ptr::null_mut();
assert_eq!(D3D12CreateDevice(std::ptr::null_mut(), 0xC100, std::ptr::null(), &mut dev), S_OK);
let desc = [0u32, 16, 1, 0]; // CBV_SRV_UAV, 16 slots, shader visible
let create_heap: Fn4 = std::mem::transmute(slot(dev, 14));
let mut heaps = [std::ptr::null_mut(); 2];
for h in &mut heaps {
assert_eq!(create_heap(dev, desc.as_ptr() as *const c_void, std::ptr::null(), h), S_OK);
}
// the start is the heap's own handle range (CreateDescriptorHeap): not 0, one per heap
let base = heaps.map(|h| (*(h as *mut NullObj)).value.load(Ordering::Relaxed));
assert_ne!(base[0], 0);
assert_ne!(base[0], base[1]);
for (h, base) in heaps.into_iter().zip(base) {
for i in [9, 10] {
#[cfg(all(target_arch = "x86_64", windows))]
{
let mut sentinel = 0x5A5A_5A5A_5A5A_5A5Au64;
let p = call_with_rdx(slot(h, i), h, &mut sentinel);
assert_eq!(sentinel, 0x5A5A_5A5A_5A5A_5A5A, "slot {i} wrote through rdx");
assert_eq!(p, base, "slot {i} with rdx set");
}
let start: unsafe extern "system" fn(*mut c_void) -> u64 = std::mem::transmute(slot(h, i));
assert_eq!(start(h), base, "slot {i}");
}
}
}
}

/// Calls `f(this)` (Win64) the way console code does, with a writable pointer left in rdx.
#[cfg(all(target_arch = "x86_64", windows))]
unsafe fn call_with_rdx(f: usize, this: *mut c_void, rdx: *mut u64) -> u64 {
let ret: u64;
// rsp is 16-aligned on entry to the asm block: 32 B of shadow space keep it so at the call
core::arch::asm!(
"sub rsp, 32",
"call {f}",
"add rsp, 32",
f = in(reg) f,
in("rcx") this,
in("rdx") rdx,
lateout("rax") ret,
clobber_abi("system"),
);
ret
}
}
6 changes: 5 additions & 1 deletion crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ fn process_private_bytes() -> u64 {
let mut c: PROCESS_MEMORY_COUNTERS_EX = std::mem::zeroed();
c.cb = std::mem::size_of::<PROCESS_MEMORY_COUNTERS_EX>() as u32;
if K32GetProcessMemoryInfo(thr::GetCurrentProcess(), &mut c as *mut _ as *mut _, c.cb) != 0 {
c.PrivateUsage as u64
// Wine fills the fields before PrivateUsage only; PagefileUsage is the same number
// on Windows (the commit charge)
if c.PrivateUsage != 0 { c.PrivateUsage as u64 } else { c.PagefileUsage as u64 }
} else {
0
}
Expand Down Expand Up @@ -1871,6 +1873,8 @@ mod tests {
assert_eq!(unsafe { TitleMemoryStatus(&mut s) }, 1);
assert_eq!(s.total, 13_824 * 1024 * 1024);
assert!(s.avail <= s.total);
// the process's own memory counts as used (Wine leaves PrivateUsage 0)
assert!(process_private_bytes() > 0 && s.avail < s.total);
s.dw_length = 8;
assert_eq!(unsafe { TitleMemoryStatus(&mut s) }, 0);
}
Expand Down
9 changes: 5 additions & 4 deletions crates/xcl/src/hle/xgameruntime/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
//! dropped), the XGameUi dialogs that do not take a user –, refuses XStore, app broadcast and
//! app capture (the thunk then returns its "API not available" error), and passes everything
//! else to the PC's own XGameRuntime (`System32\xgameruntime.dll`: the same classes under the
//! same GUIDs) – XTaskQueue / XAsync, XThread, XNetworking, XGameInvite… Once the PC runtime is
//! in use, the queues and async blocks are its own, so XCL's async APIs run on its XAsync
//! (`xasync::use_pc`). Nothing that takes an XCL user handle goes to the PC runtime: it does not
//! same GUIDs) – XTaskQueue / XAsync, XThread, XGameInvite… (XNetworking is always XCL's). Once
//! the PC runtime is in use, the queues and async blocks are its own, so XCL's async APIs run on
//! its XAsync (`xasync::use_pc`); without `System32\xgameruntime.dll`, XAsync / XTaskQueue /
//! XThread fall back to XCL's own. Nothing that takes an XCL user handle goes to the PC runtime: it does not
//! validate foreign handles.
//!
//! Layout: `gdk_table` (extracted from the public PC GDK's thunk library) gives every
Expand Down Expand Up @@ -138,7 +139,7 @@ fn pc() -> Option<&'static Pc> {
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryExW, LOAD_LIBRARY_SEARCH_SYSTEM32};
let h = LoadLibraryExW(crate::util::wide("xgameruntime.dll").as_ptr(), std::ptr::null_mut(), LOAD_LIBRARY_SEARCH_SYSTEM32);
if h.is_null() {
crate::xlog_warn!("XGameRuntime: no System32\\xgameruntime.dll (Gaming Services) – XTaskQueue/XAsync and the other APIs XCL does not implement are unavailable");
crate::xlog_warn!("XGameRuntime: no System32\\xgameruntime.dll (Gaming Services) – XCL's own XTaskQueue/XAsync are used; dialogs, text entry, game invites, game streaming and the other APIs XCL does not implement are unavailable");
return None;
}
let f = |n: &str| GetProcAddress(h, format!("{n}\0").as_ptr()).map(|p| p as usize);
Expand Down
5 changes: 4 additions & 1 deletion crates/xcl/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn checks(f: &Found) -> Vec<Check> {
Err(_) => v.push(check(Level::Error, "no DXC (dxcompiler.dll + dxil.dll): no shader can be compiled, the game draws nothing", Some("install the Windows SDK, or put dxcompiler.dll and dxil.dll next to xcl.exe (or in %XCL_DXC_DIR%)"))),
}
if !f.xgameruntime {
v.push(check(Level::Warning, "no System32\\xgameruntime.dll: XCL's own XGameRuntime parts work, the rest (task queues, networking, dialogs) is missing", Some("install Gaming Services (Microsoft Store)")));
v.push(check(Level::Warning, "no System32\\xgameruntime.dll: dialogs, text entry, game invites and game streaming are missing (XCL's own task queues, async and networking are used)", Some("install Gaming Services (Microsoft Store)")));
}
v
}
Expand Down Expand Up @@ -215,6 +215,9 @@ mod tests {
let c = checks(&f);
assert_eq!(c.iter().map(|c| c.level).collect::<Vec<_>>(), [Level::Ok, Level::Error, Level::Warning, Level::Error, Level::Warning]);
assert!(c[1].what.contains("has 6.5"), "{:?}", c[1]);
// XCL's own task queues and networking stand in for the PC runtime's: not missing
let missing = c[4].what.split(" missing").next().unwrap();
assert!(missing.contains("dialogs") && !missing.contains("task queues") && !missing.contains("networking"), "{:?}", c[4]);
assert!(c.iter().filter(|c| c.level != Level::Ok).all(|c| c.fix.is_some()), "{c:?}");
}

Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn test_title_runs_and_game_folder_is_untouched() {
dll.display()
);
let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
let root = manifest.join("..").join("..").join("target").join("e2e");
let root = Path::new(env!("CARGO_TARGET_TMPDIR")).join("e2e");
let _ = std::fs::remove_dir_all(&root);
let game = root.join("game");
let data = root.join("data");
Expand Down
3 changes: 3 additions & 0 deletions tests/testgamedll/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ publish = false
name = "testgame_mw"
crate-type = ["cdylib"]
path = "src/lib.rs"
# no unit tests: the GNU linker keeps the kernelx.dll import in the test harness's exe,
# which then does not start (kernelx.dll exists only inside XCL)
test = false
Loading