2026-11-23 01:15:26 +01:00
|
|
|
//! Overrides of shared Windows APIs whose console meaning differs.
|
|
|
|
|
//!
|
|
|
|
|
//! Only the differences live here; everything else passes through to the
|
|
|
|
|
//! host (see `HostPolicy`).
|
|
|
|
|
#![allow(non_snake_case)]
|
2026-11-23 14:42:21 +01:00
|
|
|
// Title code calls these through its import table, passing pointers the real
|
|
|
|
|
// API would trust.
|
|
|
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
2026-11-23 01:15:26 +01:00
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
use std::ffi::{CStr, c_void};
|
2026-11-23 01:15:26 +01:00
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
use crate::{HleModule, HostPolicy, hle_export, registry, runtime};
|
2026-11-23 01:15:26 +01:00
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
/// `GetModuleHandleW(NULL)` must return the title's image, not our exe;
|
|
|
|
|
/// title images and our console modules are found by name.
|
2026-11-23 01:15:26 +01:00
|
|
|
pub extern "system" fn GetModuleHandleW(name: *const u16) -> *mut c_void {
|
|
|
|
|
if name.is_null() {
|
|
|
|
|
return runtime::main_image().unwrap_or(0) as *mut c_void;
|
|
|
|
|
}
|
2026-11-23 14:59:03 +01:00
|
|
|
find_module(&wide(name), false).unwrap_or_else(|| host::get_module_handle_w(name))
|
2026-11-23 01:15:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
/// See [`GetModuleHandleW`].
|
2026-11-23 01:15:26 +01:00
|
|
|
pub extern "system" fn GetModuleHandleA(name: *const u8) -> *mut c_void {
|
|
|
|
|
if name.is_null() {
|
|
|
|
|
return runtime::main_image().unwrap_or(0) as *mut c_void;
|
|
|
|
|
}
|
2026-11-23 14:59:03 +01:00
|
|
|
find_module(&narrow(name), false).unwrap_or_else(|| host::get_module_handle_a(name))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A NUL-terminated string from the title.
|
|
|
|
|
fn narrow(text: *const u8) -> String {
|
|
|
|
|
// SAFETY: the title passes a NUL-terminated string.
|
|
|
|
|
unsafe { CStr::from_ptr(text.cast()) }
|
|
|
|
|
.to_string_lossy()
|
|
|
|
|
.into_owned()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// See [`narrow`].
|
|
|
|
|
fn wide(text: *const u16) -> String {
|
|
|
|
|
// SAFETY: the title passes a NUL-terminated string.
|
|
|
|
|
unsafe {
|
|
|
|
|
let len = (0..).take_while(|&i| *text.add(i) != 0).count();
|
|
|
|
|
String::from_utf16_lossy(std::slice::from_raw_parts(text, len))
|
|
|
|
|
}
|
2026-11-23 01:15:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
/// A title image's path is its original file, not our cache copy;
|
|
|
|
|
/// `NULL` means the title's exe.
|
|
|
|
|
pub extern "system" fn GetModuleFileNameW(module: *mut c_void, out: *mut u16, size: u32) -> u32 {
|
|
|
|
|
let Some(path) = title_path(module) else {
|
|
|
|
|
return host::get_module_file_name_w(module, out, size);
|
|
|
|
|
};
|
|
|
|
|
let wide: Vec<u16> = path.encode_utf16().collect();
|
|
|
|
|
// SAFETY: the title passes a buffer of `size` characters.
|
|
|
|
|
copy_truncated(&wide, unsafe {
|
|
|
|
|
std::slice::from_raw_parts_mut(out, size as usize)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// See [`GetModuleFileNameW`].
|
|
|
|
|
pub extern "system" fn GetModuleFileNameA(module: *mut c_void, out: *mut u8, size: u32) -> u32 {
|
|
|
|
|
let Some(path) = title_path(module) else {
|
|
|
|
|
return host::get_module_file_name_a(module, out, size);
|
|
|
|
|
};
|
|
|
|
|
// SAFETY: the title passes a buffer of `size` bytes.
|
|
|
|
|
copy_truncated(path.as_bytes(), unsafe {
|
|
|
|
|
std::slice::from_raw_parts_mut(out, size as usize)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn title_path(module: *mut c_void) -> Option<String> {
|
|
|
|
|
let base = if module.is_null() {
|
|
|
|
|
runtime::main_image()?
|
|
|
|
|
} else {
|
|
|
|
|
module as usize
|
|
|
|
|
};
|
|
|
|
|
Some(runtime::image_path(base)?.to_string_lossy().into_owned())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `GetModuleFileName`'s contract: copy with a NUL; if it does not fit,
|
|
|
|
|
/// truncate to the buffer, report `ERROR_INSUFFICIENT_BUFFER` and return
|
|
|
|
|
/// the buffer size.
|
|
|
|
|
fn copy_truncated<T: Copy + Default>(text: &[T], out: &mut [T]) -> u32 {
|
|
|
|
|
let Some(room) = out.len().checked_sub(1) else {
|
|
|
|
|
host::set_last_error(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
let n = text.len().min(room);
|
|
|
|
|
out[..n].copy_from_slice(&text[..n]);
|
|
|
|
|
out[n] = T::default();
|
|
|
|
|
if n < text.len() {
|
|
|
|
|
host::set_last_error(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
|
return out.len() as u32;
|
|
|
|
|
}
|
|
|
|
|
n as u32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ERROR_INSUFFICIENT_BUFFER: u32 = 122;
|
|
|
|
|
|
|
|
|
|
/// Console-only DLLs we implement load as a pseudo handle (titles load
|
2026-11-23 14:59:03 +01:00
|
|
|
/// `xgameruntime.dll` this way), the title's DLLs as the images we map
|
|
|
|
|
/// (on first use if nothing imports them); anything else loads on the host.
|
2026-11-23 14:42:21 +01:00
|
|
|
pub extern "system" fn LoadLibraryExA(
|
|
|
|
|
name: *const u8,
|
|
|
|
|
file: *mut c_void,
|
|
|
|
|
flags: u32,
|
|
|
|
|
) -> *mut c_void {
|
2026-11-23 14:59:03 +01:00
|
|
|
if !name.is_null()
|
|
|
|
|
&& let Some(handle) = find_module(&narrow(name), runs_code(flags))
|
|
|
|
|
{
|
|
|
|
|
return handle;
|
2026-11-23 14:42:21 +01:00
|
|
|
}
|
|
|
|
|
host::load_library_ex_a(name, file, flags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// See [`LoadLibraryExA`].
|
|
|
|
|
pub extern "system" fn LoadLibraryExW(
|
|
|
|
|
name: *const u16,
|
|
|
|
|
file: *mut c_void,
|
|
|
|
|
flags: u32,
|
|
|
|
|
) -> *mut c_void {
|
2026-11-23 14:59:03 +01:00
|
|
|
if !name.is_null()
|
|
|
|
|
&& let Some(handle) = find_module(&wide(name), runs_code(flags))
|
|
|
|
|
{
|
|
|
|
|
return handle;
|
2026-11-23 14:42:21 +01:00
|
|
|
}
|
|
|
|
|
host::load_library_ex_w(name, file, flags)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
/// False for loads as data or resources (`DONT_RESOLVE_DLL_REFERENCES`,
|
|
|
|
|
/// `LOAD_LIBRARY_AS_DATAFILE(_EXCLUSIVE)`, `AS_IMAGE_RESOURCE`): those run
|
|
|
|
|
/// no code, so a title DLL we have not mapped can go to the host.
|
|
|
|
|
fn runs_code(flags: u32) -> bool {
|
|
|
|
|
flags & (0x1 | 0x2 | 0x20 | 0x40) == 0
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
pub extern "system" fn LoadLibraryA(name: *const u8) -> *mut c_void {
|
|
|
|
|
LoadLibraryExA(name, std::ptr::null_mut(), 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub extern "system" fn LoadLibraryW(name: *const u16) -> *mut c_void {
|
|
|
|
|
LoadLibraryExW(name, std::ptr::null_mut(), 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Looks up our implementations behind a pseudo handle; real modules go to
|
|
|
|
|
/// the host.
|
|
|
|
|
pub extern "system" fn GetProcAddress(module: *mut c_void, name: *const u8) -> *mut c_void {
|
|
|
|
|
let Some(m) = registry().iter().find(|m| handle(m) == module) else {
|
|
|
|
|
return host::get_proc_address(module, name);
|
|
|
|
|
};
|
|
|
|
|
// Values below 0x10000 are ordinals; we export by name only.
|
|
|
|
|
if (name as usize) < 0x1_0000 {
|
|
|
|
|
tracing::warn!(
|
|
|
|
|
module = m.name,
|
|
|
|
|
ordinal = name as usize,
|
|
|
|
|
"GetProcAddress by ordinal"
|
|
|
|
|
);
|
|
|
|
|
return std::ptr::null_mut();
|
|
|
|
|
}
|
|
|
|
|
// SAFETY: not an ordinal, so the title passes a NUL-terminated name.
|
|
|
|
|
let name = unsafe { CStr::from_ptr(name.cast()) }.to_string_lossy();
|
|
|
|
|
match m.exports.iter().find(|e| e.name == name) {
|
|
|
|
|
Some(e) => e.address as *mut c_void,
|
|
|
|
|
None => {
|
|
|
|
|
tracing::warn!(module = m.name, %name, "GetProcAddress: not implemented");
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
/// Our pseudo modules and the title's images stay loaded: `LoadLibrary`
|
|
|
|
|
/// took no reference on them, so the host's count must not drop.
|
2026-11-23 14:42:21 +01:00
|
|
|
pub extern "system" fn FreeLibrary(module: *mut c_void) -> i32 {
|
2026-11-23 14:59:03 +01:00
|
|
|
if registry().iter().any(|m| handle(m) == module)
|
|
|
|
|
|| runtime::image_path(module as usize).is_some()
|
|
|
|
|
{
|
2026-11-23 14:42:21 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
host::free_library(module)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Title debug output goes to our log (no debugger needs to be attached).
|
|
|
|
|
pub extern "system" fn OutputDebugStringA(text: *const u8) {
|
|
|
|
|
if !text.is_null() {
|
2026-11-23 14:59:03 +01:00
|
|
|
tracing::info!(target: "title", "{}", narrow(text).trim_end());
|
2026-11-23 14:42:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// See [`OutputDebugStringA`].
|
|
|
|
|
pub extern "system" fn OutputDebugStringW(text: *const u16) {
|
|
|
|
|
if !text.is_null() {
|
2026-11-23 14:59:03 +01:00
|
|
|
tracing::info!(target: "title", "{}", wide(text).trim_end());
|
2026-11-23 14:42:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The CRT's `exit`, through which the title's entry point returns: logs
|
|
|
|
|
/// the code and the caller, then exits on the host.
|
|
|
|
|
pub extern "C" fn exit(code: i32) -> ! {
|
|
|
|
|
tracing::warn!(code, from = %host::callers(), "title exits");
|
|
|
|
|
host::exit(code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The CRT's `abort` (also reached from `terminate`): logged like [`exit`].
|
|
|
|
|
pub extern "C" fn abort() -> ! {
|
|
|
|
|
tracing::error!(from = %host::callers(), "title aborts");
|
|
|
|
|
host::abort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The CRT's `terminate` and the STL's invalid-argument exit: logged, then
|
|
|
|
|
/// the host's `abort`.
|
|
|
|
|
pub extern "C" fn terminate() -> ! {
|
|
|
|
|
tracing::error!(from = %host::callers(), "title calls terminate");
|
|
|
|
|
host::abort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// See [`terminate`].
|
|
|
|
|
pub extern "C" fn _invalid_parameter_noinfo_noreturn() -> ! {
|
|
|
|
|
tracing::error!(from = %host::callers(), "title passed an invalid parameter");
|
|
|
|
|
host::abort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// See [`exit`]; only the current process is logged.
|
|
|
|
|
pub extern "system" fn TerminateProcess(process: *mut c_void, code: u32) -> i32 {
|
|
|
|
|
if process as isize == -1 {
|
|
|
|
|
tracing::warn!(code, from = %host::callers(), "title terminates the process");
|
|
|
|
|
}
|
|
|
|
|
host::terminate_process(process, code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The pseudo handle of one of our modules: its address, never a real image.
|
|
|
|
|
fn handle(m: &HleModule) -> *mut c_void {
|
|
|
|
|
m as *const HleModule as *mut c_void
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
/// The title's images and our console modules by name; `None` leaves the
|
|
|
|
|
/// name to the host. With `load`, a title DLL not mapped yet is mapped now.
|
|
|
|
|
fn find_module(path: &str, load: bool) -> Option<*mut c_void> {
|
2026-11-23 14:42:21 +01:00
|
|
|
let file = path.rsplit(['\\', '/']).next().unwrap_or(path);
|
2026-11-23 14:59:03 +01:00
|
|
|
// As the Windows loader does, a name without extension means a DLL.
|
|
|
|
|
let file = if file.contains('.') {
|
|
|
|
|
file.to_owned()
|
|
|
|
|
} else {
|
|
|
|
|
format!("{file}.dll")
|
|
|
|
|
};
|
|
|
|
|
if let Some(base) = runtime::image_named(&file) {
|
|
|
|
|
return Some(base as *mut c_void);
|
|
|
|
|
}
|
2026-11-23 14:42:21 +01:00
|
|
|
// DLLs the host has too are real host modules, whatever `--strict` says.
|
2026-11-23 14:59:03 +01:00
|
|
|
if !HostPolicy::default().allows(&file)
|
|
|
|
|
&& let Some(m) = registry().iter().find(|m| m.serves(&file))
|
|
|
|
|
{
|
|
|
|
|
if load {
|
|
|
|
|
tracing::info!(dll = file, "loaded as a pseudo module");
|
|
|
|
|
}
|
|
|
|
|
return Some(handle(m));
|
|
|
|
|
}
|
|
|
|
|
if !load {
|
2026-11-23 14:42:21 +01:00
|
|
|
return None;
|
|
|
|
|
}
|
2026-11-23 14:59:03 +01:00
|
|
|
match runtime::load_title_dll(&file) {
|
|
|
|
|
Ok(base) => base.map(|b| b as *mut c_void),
|
|
|
|
|
// Not the host: it would load the unpatched DLL from the title's folder.
|
|
|
|
|
Err(error) => {
|
|
|
|
|
tracing::error!(dll = file, %error, "mapping a title DLL failed");
|
|
|
|
|
host::set_last_error(ERROR_MOD_NOT_FOUND);
|
|
|
|
|
Some(std::ptr::null_mut())
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-23 14:42:21 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-23 14:59:03 +01:00
|
|
|
const ERROR_MOD_NOT_FOUND: u32 = 126;
|
|
|
|
|
|
2026-11-23 01:15:26 +01:00
|
|
|
pub(crate) fn module() -> HleModule {
|
|
|
|
|
HleModule {
|
|
|
|
|
name: "kernel",
|
|
|
|
|
dlls: &[
|
|
|
|
|
"kernel32.dll",
|
|
|
|
|
"kernelbase.dll",
|
|
|
|
|
"api-ms-win-core-libraryloader-*",
|
2026-11-23 14:42:21 +01:00
|
|
|
"api-ms-win-core-processthreads-*",
|
|
|
|
|
"api-ms-win-core-debug-*",
|
|
|
|
|
"api-ms-win-crt-runtime-*",
|
|
|
|
|
],
|
|
|
|
|
exports: vec![
|
|
|
|
|
hle_export!(GetModuleHandleW),
|
|
|
|
|
hle_export!(GetModuleHandleA),
|
|
|
|
|
hle_export!(GetModuleFileNameW),
|
|
|
|
|
hle_export!(GetModuleFileNameA),
|
|
|
|
|
hle_export!(LoadLibraryExA),
|
|
|
|
|
hle_export!(LoadLibraryExW),
|
|
|
|
|
hle_export!(LoadLibraryA),
|
|
|
|
|
hle_export!(LoadLibraryW),
|
|
|
|
|
hle_export!(GetProcAddress),
|
|
|
|
|
hle_export!(FreeLibrary),
|
|
|
|
|
hle_export!(OutputDebugStringA),
|
|
|
|
|
hle_export!(OutputDebugStringW),
|
|
|
|
|
hle_export!(exit),
|
|
|
|
|
hle_export!(abort),
|
|
|
|
|
hle_export!(terminate),
|
|
|
|
|
hle_export!(_invalid_parameter_noinfo_noreturn),
|
|
|
|
|
hle_export!(TerminateProcess),
|
2026-11-23 01:15:26 +01:00
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
mod host {
|
|
|
|
|
use std::ffi::c_void;
|
|
|
|
|
|
2026-11-23 14:42:21 +01:00
|
|
|
use windows_sys::Win32::Foundation::{FreeLibrary, SetLastError};
|
|
|
|
|
use windows_sys::Win32::System::Diagnostics::Debug::RtlCaptureStackBackTrace;
|
|
|
|
|
use windows_sys::Win32::System::LibraryLoader::{
|
|
|
|
|
GetModuleFileNameA, GetModuleFileNameW, GetModuleHandleA, GetModuleHandleW, GetProcAddress,
|
|
|
|
|
LoadLibraryExA, LoadLibraryExW,
|
|
|
|
|
};
|
|
|
|
|
use windows_sys::Win32::System::Threading::TerminateProcess;
|
|
|
|
|
|
|
|
|
|
unsafe extern "C" {
|
|
|
|
|
#[link_name = "exit"]
|
|
|
|
|
fn crt_exit(code: i32) -> !;
|
|
|
|
|
#[link_name = "abort"]
|
|
|
|
|
fn crt_abort() -> !;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SAFETY (every function): forwarding the title's own arguments to the
|
|
|
|
|
// host API of the same name.
|
|
|
|
|
|
2026-11-23 01:15:26 +01:00
|
|
|
pub(super) fn get_module_handle_w(name: *const u16) -> *mut c_void {
|
2026-11-23 14:42:21 +01:00
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { GetModuleHandleW(name) }
|
2026-11-23 01:15:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_handle_a(name: *const u8) -> *mut c_void {
|
2026-11-23 14:42:21 +01:00
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { GetModuleHandleA(name) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_file_name_w(module: *mut c_void, out: *mut u16, size: u32) -> u32 {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { GetModuleFileNameW(module, out, size) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_file_name_a(module: *mut c_void, out: *mut u8, size: u32) -> u32 {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { GetModuleFileNameA(module, out, size) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn set_last_error(code: u32) {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { SetLastError(code) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn load_library_ex_a(name: *const u8, file: *mut c_void, flags: u32) -> *mut c_void {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { LoadLibraryExA(name, file, flags) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn load_library_ex_w(
|
|
|
|
|
name: *const u16,
|
|
|
|
|
file: *mut c_void,
|
|
|
|
|
flags: u32,
|
|
|
|
|
) -> *mut c_void {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { LoadLibraryExW(name, file, flags) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_proc_address(module: *mut c_void, name: *const u8) -> *mut c_void {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { GetProcAddress(module, name) }.map_or(std::ptr::null_mut(), |f| f as *mut c_void)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn free_library(module: *mut c_void) -> i32 {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { FreeLibrary(module) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn exit(code: i32) -> ! {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { crt_exit(code) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn abort() -> ! {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { crt_abort() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn terminate_process(process: *mut c_void, code: u32) -> i32 {
|
|
|
|
|
// SAFETY: see above.
|
|
|
|
|
unsafe { TerminateProcess(process, code) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Our caller's callers, as `module+rva`.
|
|
|
|
|
pub(super) fn callers() -> String {
|
|
|
|
|
let mut frames = [std::ptr::null_mut(); 12];
|
|
|
|
|
// SAFETY: fills at most `frames.len()` return addresses.
|
|
|
|
|
let n = unsafe {
|
|
|
|
|
RtlCaptureStackBackTrace(
|
|
|
|
|
2,
|
|
|
|
|
frames.len() as u32,
|
|
|
|
|
frames.as_mut_ptr(),
|
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
let names: Vec<_> = frames[..usize::from(n)]
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|&f| victory_loader::locate(f as usize))
|
|
|
|
|
.collect();
|
|
|
|
|
names.join(" < ")
|
2026-11-23 01:15:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
mod host {
|
|
|
|
|
use std::ffi::c_void;
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_handle_w(_: *const u16) -> *mut c_void {
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_handle_a(_: *const u8) -> *mut c_void {
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
|
}
|
2026-11-23 14:42:21 +01:00
|
|
|
|
|
|
|
|
pub(super) fn load_library_ex_a(_: *const u8, _: *mut c_void, _: u32) -> *mut c_void {
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_file_name_w(_: *mut c_void, _: *mut u16, _: u32) -> u32 {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_module_file_name_a(_: *mut c_void, _: *mut u8, _: u32) -> u32 {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn set_last_error(_: u32) {}
|
|
|
|
|
|
|
|
|
|
pub(super) fn load_library_ex_w(_: *const u16, _: *mut c_void, _: u32) -> *mut c_void {
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_proc_address(_: *mut c_void, _: *const u8) -> *mut c_void {
|
|
|
|
|
std::ptr::null_mut()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn free_library(_: *mut c_void) -> i32 {
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn exit(code: i32) -> ! {
|
|
|
|
|
std::process::exit(code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn abort() -> ! {
|
|
|
|
|
std::process::abort()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn terminate_process(_: *mut c_void, code: u32) -> i32 {
|
|
|
|
|
std::process::exit(code as i32)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn callers() -> String {
|
|
|
|
|
String::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn title_images_report_their_original_path() {
|
|
|
|
|
let base = 0x7000_0000_0000;
|
|
|
|
|
runtime::set_image_path(base, std::path::Path::new(r"C:\title\game.exe"));
|
|
|
|
|
let module = base as *mut c_void;
|
|
|
|
|
let mut buf = [0xFFFFu16; 32];
|
|
|
|
|
let n = GetModuleFileNameW(module, buf.as_mut_ptr(), buf.len() as u32);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
String::from_utf16_lossy(&buf[..n as usize]),
|
|
|
|
|
r"C:\title\game.exe"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(buf[n as usize], 0);
|
|
|
|
|
|
|
|
|
|
// Too small: truncated, NUL-terminated, the buffer size returned.
|
|
|
|
|
let mut small = [0u8; 4];
|
|
|
|
|
assert_eq!(GetModuleFileNameA(module, small.as_mut_ptr(), 4), 4);
|
|
|
|
|
assert_eq!(&small, &[b'C', b':', b'\\', 0]);
|
|
|
|
|
}
|
2026-11-23 14:59:03 +01:00
|
|
|
|
|
|
|
|
fn w(text: &str) -> Vec<u16> {
|
|
|
|
|
text.encode_utf16().chain(Some(0)).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn title_dlls_and_pseudo_modules_are_found_by_name() {
|
|
|
|
|
let base = 0x7100_0000_0000usize;
|
|
|
|
|
runtime::set_image_path(base, std::path::Path::new(r"C:\title\Mapped.dll"));
|
|
|
|
|
runtime::set_title_dll_loader(Box::new(|file| match file {
|
|
|
|
|
"late.dll" => Ok(Some(0x7200_0000_0000)),
|
|
|
|
|
"broken.dll" => Err("bad image".into()),
|
|
|
|
|
_ => Ok(None),
|
|
|
|
|
}));
|
|
|
|
|
let mapped = base as *mut c_void;
|
|
|
|
|
assert_eq!(GetModuleHandleW(w("MAPPED").as_ptr()), mapped);
|
|
|
|
|
assert_eq!(GetModuleHandleA(c"sub\\mapped.dll".as_ptr().cast()), mapped);
|
|
|
|
|
assert_eq!(LoadLibraryW(w("mapped.dll").as_ptr()), mapped);
|
|
|
|
|
assert_eq!(FreeLibrary(mapped), 1, "title images stay loaded");
|
|
|
|
|
|
|
|
|
|
// Not mapped yet: `LoadLibrary` maps it, `GetModuleHandle` does not.
|
|
|
|
|
assert!(GetModuleHandleW(w("late.dll").as_ptr()).is_null());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
LoadLibraryA(c"late".as_ptr().cast()) as usize,
|
|
|
|
|
0x7200_0000_0000
|
|
|
|
|
);
|
|
|
|
|
assert!(LoadLibraryA(c"broken.dll".as_ptr().cast()).is_null());
|
|
|
|
|
// As data it runs no code, so the host loads it (and finds nothing).
|
|
|
|
|
let data = LoadLibraryExW(w("late.dll").as_ptr(), std::ptr::null_mut(), 0x2);
|
|
|
|
|
assert_ne!(data as usize, 0x7200_0000_0000);
|
|
|
|
|
|
|
|
|
|
let runtime = LoadLibraryA(c"xgameruntime.dll".as_ptr().cast());
|
|
|
|
|
assert!(!runtime.is_null());
|
|
|
|
|
assert_eq!(GetModuleHandleW(w("XGameRuntime.dll").as_ptr()), runtime);
|
|
|
|
|
}
|
2026-11-23 01:15:26 +01:00
|
|
|
}
|