Add bounded guest Winsock startup and UDP socket lifecycle

Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
Adeeb Rahman committed 2026-11-24 08:30:55 -05:00
1 parent 8343e0cdc6
commit aacd160626
3 files changed
+160

No files matched your search

+4
View File
@@ -14,6 +14,7 @@ mod events;
mod files;
mod heap;
mod modules;
mod winsock;
use crate::runtime::HResult;
@@ -170,6 +171,7 @@ pub struct ServicesState {
pub thread_id: u32,
objects: Mutex<ObjectState>,
last_errors: Mutex<BTreeMap<u64, u32>>,
winsock: Mutex<winsock::Winsock>,
pub next_alloc_addr: AtomicU64,
pub graphics: Arc<Mutex<crate::graphics::GraphicsContext>>,
}
@@ -184,6 +186,7 @@ impl ServicesState {
thread_id: 0x4104, // Synthetic main thread ID
objects: Mutex::new(ObjectState::default()),
last_errors: Mutex::default(),
winsock: Mutex::default(),
next_alloc_addr: AtomicU64::new(0x0000_0002_0000_0000), // 8GB guest heap
graphics: Arc::new(Mutex::new(crate::graphics::GraphicsContext::new())),
}
@@ -539,6 +542,7 @@ pub fn register_standard_services(registry: &mut ImportRegistry, state: Arc<Serv
"SubmitGuestDraw",
ServiceStatus::Implemented,
);
winsock::register(registry, &state);
modules::register(registry, &state);
}
+122
View File
@@ -0,0 +1,122 @@
// SPDX-License-Identifier: GPL-3.0-or-later
//! Winsock lifecycle for the synthetic guest. Descriptors are guest-owned.
use super::ServicesState;
use crate::imports::{ImportRegistry, ServiceStatus};
use std::collections::BTreeSet;
use std::sync::Arc;
const MODULE: &str = "ws2_32.dll";
const VERSION: u16 = 0x0202;
const WSADATA_SIZE: usize = 408;
const MAX_SOCKETS: usize = 64;
const SOCKET_ERROR: u64 = u32::MAX as u64;
const INVALID_SOCKET: u64 = u64::MAX;
const WSAEFAULT: u32 = 10014;
const WSAEMFILE: u32 = 10024;
const WSAENOTSOCK: u32 = 10038;
const WSAEPROTONOSUPPORT: u32 = 10043;
const WSAESOCKTNOSUPPORT: u32 = 10044;
const WSAEAFNOSUPPORT: u32 = 10047;
const WSASYSNOTREADY: u32 = 10091;
const WSAVERNOTSUPPORTED: u32 = 10092;
const WSANOTINITIALISED: u32 = 10093;
#[derive(Default)]
pub(super) struct Winsock {
refs: u32,
serial: u32,
sockets: BTreeSet<u64>,
}
pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
let startup = Arc::clone(state);
registry.register(MODULE, "WSAStartup", move |args, _, memory| {
let requested = args.rcx as u16;
let negotiated = match requested.to_le_bytes() {
[2, minor] => u16::from_le_bytes([2, minor.min(2)]),
_ => return Ok(u64::from(WSAVERNOTSUPPORTED)),
};
let mut net = startup.winsock.lock().expect("guest Winsock state");
let Some(next) = net.refs.checked_add(1) else {
return Ok(u64::from(WSASYSNOTREADY));
};
let mut data = [0; WSADATA_SIZE];
data[..2].copy_from_slice(&negotiated.to_le_bytes());
data[2..4].copy_from_slice(&VERSION.to_le_bytes());
let description = b"Xenora synthetic Winsock";
data[16..16 + description.len()].copy_from_slice(description);
if args.rdx == 0 || memory.write_bytes(args.rdx, &data).is_err() {
return Ok(u64::from(WSAEFAULT));
}
net.refs = next;
Ok(0)
});
let cleanup = Arc::clone(state);
registry.register(MODULE, "WSACleanup", move |_, cpu, _| {
let mut net = cleanup.winsock.lock().expect("guest Winsock state");
if net.refs == 0 {
return cleanup.fail(cpu.gs_base, WSANOTINITIALISED, SOCKET_ERROR);
}
net.refs -= 1;
if net.refs == 0 {
net.sockets.clear();
}
Ok(0)
});
let open = Arc::clone(state);
registry.register(MODULE, "socket", move |args, cpu, _| {
let mut net = open.winsock.lock().expect("guest Winsock state");
let fail = |error| open.fail(cpu.gs_base, error, INVALID_SOCKET);
if net.refs == 0 {
return fail(WSANOTINITIALISED);
}
if args.rcx as u32 != 2 {
return fail(WSAEAFNOSUPPORT);
}
if args.rdx as u32 != 2 {
return fail(WSAESOCKTNOSUPPORT);
}
if !matches!(args.r8 as u32, 0 | 17) {
return fail(WSAEPROTONOSUPPORT);
}
if net.sockets.len() >= MAX_SOCKETS {
return fail(WSAEMFILE);
}
let Some(next) = net.serial.checked_add(1) else {
return fail(WSAEMFILE);
};
net.serial = next;
let socket = 0x5300_0000_0000_0000 | u64::from(next);
net.sockets.insert(socket);
Ok(socket)
});
let close = Arc::clone(state);
registry.register(MODULE, "closesocket", move |args, cpu, _| {
let mut net = close.winsock.lock().expect("guest Winsock state");
if net.refs == 0 {
return close.fail(cpu.gs_base, WSANOTINITIALISED, SOCKET_ERROR);
}
if !net.sockets.remove(&args.rcx) {
return close.fail(cpu.gs_base, WSAENOTSOCK, SOCKET_ERROR);
}
Ok(0)
});
let get = Arc::clone(state);
registry.register(MODULE, "WSAGetLastError", move |_, cpu, _| {
Ok(u64::from(get.last_error(cpu.gs_base)))
});
let set = Arc::clone(state);
registry.register(MODULE, "WSASetLastError", move |args, cpu, _| {
set.set_last_error(cpu.gs_base, args.rcx as u32);
Ok(0)
});
for symbol in ["WSAStartup", "WSACleanup", "socket", "closesocket"] {
registry.set_status(MODULE, symbol, ServiceStatus::Partial);
}
}
+34
View File
@@ -1121,6 +1121,40 @@ fn test_guest_crt_string_length_through_iat() {
}
}
#[test]
fn guest_winsock_lifecycle_balances_startup_and_invalidates_sockets() {
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));
let call = |symbol, args: &[u64]| {
let result = call_service_fixture(&state, "ws2_32.dll", symbol, args);
assert_eq!(result.execution.exit_reason, ExitReason::Halt);
result
};
let value = |symbol, args: &[u64]| call(symbol, args).execution.final_state.get(Register::Rax);
let datagram = [2, 2, 17];
let output = PROCESS_FIXTURE_STACK_TOP - 0x400;
assert_eq!(value("socket", &datagram), u64::MAX);
assert_eq!(value("WSAGetLastError", &[]), 10093);
assert_eq!(value("WSAStartup", &[0x0202, 0]), 10014);
assert_eq!(value("WSAStartup", &[0x0101, output]), 10092);
assert_eq!(value("WSAGetLastError", &[]), 10093);
let first = call("WSAStartup", &[0x0102, output]);
assert_eq!(first.execution.final_state.get(Register::Rax), 0);
assert_eq!(first.image.memory.read_u32(output).unwrap(), 0x0202_0102);
assert_eq!(value("WSAStartup", &[0x0202, output]), 0);
let socket = value("socket", &datagram);
assert_ne!(socket, u64::MAX);
assert_eq!(value("WSACleanup", &[]), 0);
assert_eq!(value("closesocket", &[socket]), 0);
let stale = value("socket", &datagram);
assert_ne!(stale, socket);
assert_eq!(value("WSACleanup", &[]), 0);
assert_eq!(value("WSACleanup", &[]), u32::MAX as u64);
assert_eq!(value("WSAStartup", &[0x0202, output]), 0);
assert_eq!(value("closesocket", &[stale]), u32::MAX as u64);
assert_eq!(value("WSAGetLastError", &[]), 10038);
assert_ne!(value("socket", &datagram), stale);
}
#[test]
fn guest_hresult_failures_preserve_last_error() {
let state = Arc::new(ServicesState::new(Arc::new(MockClock::new(0))));