mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-27 00:50:43 +00:00
Add manual GDK task queue handle ownership
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
0b6ab43ad4
commit
f6c87de433
3 files changed
+142
No files matched your search
@@ -12,6 +12,7 @@
|
||||
mod crt;
|
||||
mod events;
|
||||
mod files;
|
||||
mod gdk_queue;
|
||||
mod heap;
|
||||
mod modules;
|
||||
mod task_fixture;
|
||||
@@ -174,6 +175,7 @@ pub struct ServicesState {
|
||||
last_errors: Mutex<BTreeMap<u64, u32>>,
|
||||
winsock: Mutex<winsock::Winsock>,
|
||||
task_fixture: Mutex<task_fixture::TaskFixture>,
|
||||
gdk_queues: Mutex<gdk_queue::QueueHandles>,
|
||||
winsock_changed: Condvar,
|
||||
pub next_alloc_addr: AtomicU64,
|
||||
pub graphics: Arc<Mutex<crate::graphics::GraphicsContext>>,
|
||||
@@ -191,6 +193,7 @@ impl ServicesState {
|
||||
last_errors: Mutex::default(),
|
||||
winsock: Mutex::default(),
|
||||
task_fixture: Mutex::default(),
|
||||
gdk_queues: Mutex::default(),
|
||||
winsock_changed: Condvar::new(),
|
||||
next_alloc_addr: AtomicU64::new(0x0000_0002_0000_0000), // 8GB guest heap
|
||||
graphics: Arc::new(Mutex::new(crate::graphics::GraphicsContext::new())),
|
||||
@@ -549,6 +552,7 @@ pub fn register_standard_services(registry: &mut ImportRegistry, state: Arc<Serv
|
||||
);
|
||||
winsock::register(registry, &state);
|
||||
task_fixture::register(registry, &state);
|
||||
gdk_queue::register(registry, &state);
|
||||
modules::register(registry, &state);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
//! Handle ownership for the documented manual GDK task queue subset.
|
||||
|
||||
use super::ServicesState;
|
||||
use super::{ERROR_INVALID_HANDLE, ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED};
|
||||
use crate::imports::{ImportRegistry, ServiceStatus};
|
||||
use crate::memory::GuestMemory;
|
||||
use crate::runtime::HResult;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
const MODULE: &str = "xgameruntime.dll";
|
||||
const MAX_HANDLES: usize = 64;
|
||||
const ERROR_OUTOFMEMORY: u32 = 14;
|
||||
const MANUAL: u32 = 0;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct QueueHandles {
|
||||
last_id: u64,
|
||||
queues: BTreeMap<u64, u64>,
|
||||
}
|
||||
|
||||
impl QueueHandles {
|
||||
fn open(
|
||||
&mut self,
|
||||
object: Option<u64>,
|
||||
output: u64,
|
||||
memory: &mut GuestMemory,
|
||||
) -> Result<(), u32> {
|
||||
if output == 0 {
|
||||
return Err(ERROR_INVALID_PARAMETER);
|
||||
}
|
||||
if self.queues.len() == MAX_HANDLES {
|
||||
return Err(ERROR_OUTOFMEMORY);
|
||||
}
|
||||
let next = self
|
||||
.last_id
|
||||
.checked_add(1)
|
||||
.filter(|id| *id < (1 << 48))
|
||||
.ok_or(ERROR_OUTOFMEMORY)?;
|
||||
let handle = 0x5144_0000_0000_0000 | next;
|
||||
memory
|
||||
.write_u64(output, handle)
|
||||
.map_err(|_| ERROR_INVALID_PARAMETER)?;
|
||||
self.last_id = next;
|
||||
self.queues.insert(handle, object.unwrap_or(handle));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn guest_hresult(result: Result<(), u32>) -> u64 {
|
||||
match result {
|
||||
Ok(()) => HResult::OK.guest_return(),
|
||||
Err(error) => HResult::from_win32(error).guest_return(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
|
||||
let create = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XTaskQueueCreate",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
if args.rcx as u32 != MANUAL || args.rdx as u32 != MANUAL {
|
||||
return Ok(guest_hresult(Err(ERROR_NOT_SUPPORTED)));
|
||||
}
|
||||
let mut queues = create.gdk_queues.lock().expect("GDK queue handle table");
|
||||
Ok(guest_hresult(queues.open(None, args.r8, memory)))
|
||||
},
|
||||
);
|
||||
|
||||
let duplicate = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XTaskQueueDuplicateHandle",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, memory| {
|
||||
let mut queues = duplicate.gdk_queues.lock().expect("GDK queue handle table");
|
||||
let Some(&object) = queues.queues.get(&args.rcx) else {
|
||||
return Ok(guest_hresult(Err(ERROR_INVALID_HANDLE)));
|
||||
};
|
||||
Ok(guest_hresult(queues.open(Some(object), args.rdx, memory)))
|
||||
},
|
||||
);
|
||||
|
||||
let close = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"XTaskQueueCloseHandle",
|
||||
ServiceStatus::Partial,
|
||||
move |args, _, _| {
|
||||
close
|
||||
.gdk_queues
|
||||
.lock()
|
||||
.expect("GDK queue handle table")
|
||||
.queues
|
||||
.remove(&args.rcx);
|
||||
Ok(0)
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1121,6 +1121,41 @@ fn test_guest_crt_string_length_through_iat() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guest_gdk_manual_queue_handles_have_independent_lifetimes() {
|
||||
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
|
||||
let call = |symbol, args: &[u64]| {
|
||||
let result = call_service_fixture(&state, "xgameruntime.dll", symbol, args);
|
||||
assert_eq!(result.execution.exit_reason, ExitReason::Halt);
|
||||
result
|
||||
};
|
||||
let output = PROCESS_FIXTURE_STACK_TOP - 0x400;
|
||||
let rejected = call("XTaskQueueCreate", &[1, 0, output]);
|
||||
assert_eq!(
|
||||
rejected.execution.final_state.get(Register::Rax),
|
||||
0x8007_0032
|
||||
);
|
||||
assert_eq!(rejected.image.memory.read_u64(output).unwrap(), 0);
|
||||
let open = |symbol, args: &[u64]| {
|
||||
let result = call(symbol, args);
|
||||
assert_eq!(result.execution.final_state.get(Register::Rax), 0);
|
||||
result.image.memory.read_u64(output).unwrap()
|
||||
};
|
||||
let first = open("XTaskQueueCreate", &[0, 0, output]);
|
||||
let second = open("XTaskQueueDuplicateHandle", &[first, output]);
|
||||
assert_ne!(first, second);
|
||||
call("XTaskQueueCloseHandle", &[first]);
|
||||
let stale = call("XTaskQueueDuplicateHandle", &[first, output]);
|
||||
assert_eq!(stale.execution.final_state.get(Register::Rax), 0x8007_0006);
|
||||
assert_eq!(stale.image.memory.read_u64(output).unwrap(), 0);
|
||||
let third = open("XTaskQueueDuplicateHandle", &[second, output]);
|
||||
assert!(third > second);
|
||||
call("XTaskQueueCloseHandle", &[second]);
|
||||
call("XTaskQueueCloseHandle", &[third]);
|
||||
let next = open("XTaskQueueCreate", &[0, 0, output]);
|
||||
assert!(next > third);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn guest_manual_tasks_deliver_results_once_through_imports() {
|
||||
let clock = Arc::new(MockClock::new(0));
|
||||
|
||||
Reference in new issue
Block a user