Add process-local platform lifecycle identity and settings

Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
Adeeb Rahman committed 2026-11-24 08:55:17 -05:00
1 parent f9a7d99638
commit 542684612d
3 files changed
+116

No files matched your search

+4
View File
@@ -15,6 +15,7 @@ mod files;
mod gdk_queue;
mod heap;
mod modules;
mod platform_fixture;
mod task_fixture;
mod winsock;
@@ -175,6 +176,7 @@ pub struct ServicesState {
last_errors: Mutex<BTreeMap<u64, u32>>,
winsock: Mutex<winsock::Winsock>,
task_fixture: Mutex<task_fixture::TaskFixture>,
platform_fixture: Mutex<platform_fixture::PlatformFixture>,
gdk_queues: Mutex<gdk_queue::QueueHandles>,
gdk_queue_changed: Condvar,
winsock_changed: Condvar,
@@ -194,6 +196,7 @@ impl ServicesState {
last_errors: Mutex::default(),
winsock: Mutex::default(),
task_fixture: Mutex::default(),
platform_fixture: Mutex::default(),
gdk_queues: Mutex::default(),
gdk_queue_changed: Condvar::new(),
winsock_changed: Condvar::new(),
@@ -554,6 +557,7 @@ pub fn register_standard_services(registry: &mut ImportRegistry, state: Arc<Serv
);
winsock::register(registry, &state);
task_fixture::register(registry, &state);
platform_fixture::register(registry, &state);
gdk_queue::register(registry, &state);
modules::register(registry, &state);
}
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! Process-local platform services for original guest fixtures.
use super::{ERROR_INVALID_PARAMETER, ERROR_NOACCESS, ERROR_NOT_ENOUGH_MEMORY, ServicesState};
use crate::imports::ImportRegistry;
use std::collections::BTreeMap;
use std::sync::Arc;
const MODULE: &str = "xenora_fixture.dll";
const FIXTURE_APP_ID: u64 = 0x5845_4e4f_5241_0001;
const ERROR_NOT_FOUND: u32 = 1168;
const ERROR_INVALID_STATE: u32 = 5023;
const MAX_SETTINGS: usize = 32;
#[derive(Default)]
pub(super) struct PlatformFixture {
active: bool,
settings: BTreeMap<u32, u64>,
}
pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
for (symbol, active) in [
("OpenFixturePlatform", true),
("CloseFixturePlatform", false),
] {
let state = Arc::clone(state);
registry.register(MODULE, symbol, move |_, cpu, _| {
let mut platform = state.platform_fixture.lock().expect("fixture platform");
if platform.active == active {
return state.fail(cpu.gs_base, ERROR_INVALID_STATE, 0);
}
platform.active = active;
Ok(1)
});
}
let identity = Arc::clone(state);
registry.register(MODULE, "GetFixtureApplicationId", move |_, cpu, _| {
let platform = identity.platform_fixture.lock().expect("fixture platform");
if !platform.active {
return identity.fail(cpu.gs_base, ERROR_INVALID_STATE, 0);
}
Ok(FIXTURE_APP_ID)
});
let set = Arc::clone(state);
registry.register(MODULE, "SetFixtureSetting", move |args, cpu, _| {
let mut platform = set.platform_fixture.lock().expect("fixture platform");
if !platform.active {
return set.fail(cpu.gs_base, ERROR_INVALID_STATE, 0);
}
let key = args.rcx as u32;
if key == 0 {
return set.fail(cpu.gs_base, ERROR_INVALID_PARAMETER, 0);
}
if platform.settings.len() >= MAX_SETTINGS && !platform.settings.contains_key(&key) {
return set.fail(cpu.gs_base, ERROR_NOT_ENOUGH_MEMORY, 0);
}
platform.settings.insert(key, args.rdx);
Ok(1)
});
let get = Arc::clone(state);
registry.register(MODULE, "GetFixtureSetting", move |args, cpu, memory| {
let platform = get.platform_fixture.lock().expect("fixture platform");
if !platform.active {
return get.fail(cpu.gs_base, ERROR_INVALID_STATE, 0);
}
let key = args.rcx as u32;
if key == 0 {
return get.fail(cpu.gs_base, ERROR_INVALID_PARAMETER, 0);
}
let Some(&value) = platform.settings.get(&key) else {
return get.fail(cpu.gs_base, ERROR_NOT_FOUND, 0);
};
if args.rdx == 0 || memory.write_u64(args.rdx, value).is_err() {
return get.fail(cpu.gs_base, ERROR_NOACCESS, 0);
}
Ok(1)
});
}
+30
View File
@@ -1121,6 +1121,36 @@ fn test_guest_crt_string_length_through_iat() {
}
}
#[test]
fn guest_platform_settings_follow_lifecycle_and_process_boundaries() {
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
let call =
|symbol, args: &[u64]| call_service_fixture(&state, "xenora_fixture.dll", symbol, args);
let value = |symbol, args: &[u64]| call(symbol, args).execution.final_state.get(Register::Rax);
let output = PROCESS_FIXTURE_STACK_TOP - 0x400;
assert_eq!(value("GetFixtureApplicationId", &[]), 0);
assert_eq!(value("OpenFixturePlatform", &[]), 1);
assert_eq!(value("OpenFixturePlatform", &[]), 0);
assert_eq!(value("GetFixtureApplicationId", &[]), 0x5845_4e4f_5241_0001);
for key in 1..=32 {
assert_eq!(value("SetFixtureSetting", &[key, key * 10]), 1);
}
assert_eq!(value("SetFixtureSetting", &[33, 1]), 0);
assert_eq!(value("SetFixtureSetting", &[1, 99]), 1);
assert_eq!(value("CloseFixturePlatform", &[]), 1);
assert_eq!(value("GetFixtureSetting", &[1, output]), 0);
assert_eq!(value("OpenFixturePlatform", &[]), 1);
let read = call("GetFixtureSetting", &[1, output]);
assert_eq!(read.execution.final_state.get(Register::Rax), 1);
assert_eq!(read.image.memory.read_u64(output).unwrap(), 99);
let other = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
let module = "xenora_fixture.dll";
call_service_fixture(&other, module, "OpenFixturePlatform", &[]);
let missing = call_service_fixture(&other, module, "GetFixtureSetting", &[1, output]);
assert_eq!(missing.execution.final_state.get(Register::Rax), 0);
assert_eq!(missing.image.memory.read_u64(output).unwrap(), 0);
}
#[test]
fn guest_gdk_dispatch_runs_callback_and_resumes_caller() {
const IMAGE: u64 = 0x140000000;