Run Grand Theft Auto VI from first boot into the world

The title now boots through every first-boot and menu screen at its own
30 fps, drawing with translated shaders, with input, sound, saves and
loading served; it reaches the world, where the host GPU path still
fails (see "Not working yet").

Loader and runtime
- Crash reports unwind properly, name C++ throws and host GPU failures,
  carry the title's last debug lines and are appended to crashes.log.
- Thread notices on our own threads, reentrant late DLL loading, exit in
  the console's order, relaunch on a launch-new-game request.
- Offline guard: the title cannot reach the network (sockets, name
  lookups, WinHTTP, the media source resolver); by-name lookups and
  loads follow the import rules.
- Every runtime area the title calls is served (networking hint,
  packages, store, saves, display, UI, speech stubs, user, async pool).
  Saves persist; an update is one rename and survives a kill at any step.
- DirectStorage reads for real: shared worker pool, priorities, zlib and
  BCPack. BCPack is our own decoder; the format's constant tables ship in
  crates/victory-formats/src/bcpack-tables.bin (the asset policy's one
  exception, see docs/asset-policy.md).
- GameInput from XInput pads and the keyboard, with a keyboard stand-in
  pad; console timers (1 ms) for the whole process.
- Audio: XMA2 decoding on the emulated contexts, Opus on the decoder
  queues, the DSP interface as a stub; title memory placed below 2^40.
- Console texture layouts (sizes, tiling, detiling) and the layout
  library's imports.

GPU
- Draws, mesh dispatches and compute dispatches run on the host with
  shaders translated at first use and cached on disk; descriptor heaps,
  views, root signatures, barriers and host texture states; rectangle
  lists; comparison samplers.
- Title memory reaches the host through persistent mirrors that copy
  only pages written since the last copy; shader-written buffers are
  copied back.
- Presentation with frame pacing per event kind; framerate in the window
  caption.

Shaders
- DXIL emission for vertex, pixel, compute and mesh programs from the
  console's native code, with a differential test suite against the PC
  compile. World set: mesh 8,191 of 8,506, vertex/pixel 10,617 of
  12,346, compute 1,643 of 2,539; utility set complete.

Build
- Debugging aids (debug logs, frame capture, stack dumps, scripted keys,
  the host debug layer, the software adapter) exist only with the
  `diagnostics` feature; `[profile.diag]` keeps that build apart.

Not working yet
- In the world the host device is removed within seconds (descriptor
  handling fixes are in, unproven by a surviving run) and frames present
  black.
- 64-bit image atomics, merged geometry stages and the BVH builder's own
  shaders do not translate; reverb is silent.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
MarvisandClaude Fable 5.2 committed 2026-11-23 23:10:01 +01:00
1 parent bc3070991a
commit 2849477415
107 files changed
+42526 -868

No files matched your search

+324 -42
View File
@@ -1,77 +1,330 @@
//! Names the faulting code when title code crashes.
//!
//! A vectored handler sees every exception first, so it only logs the kinds
//! that end a process (access violations and the like) and leaves handling
//! to the title's own handlers.
//! A vectored handler sees every exception first, so it only prints the
//! kinds that end a process (access violations and the like, and
//! breakpoints: titles assert with `int3`, fatal without a debugger) as
//! first-chance, since a handler may still take them (ntdll's loader does
//! with its own probes). What nobody handles, of any kind (an uncaught C++
//! exception too), is reported from the unhandled-exception filter
//! (`report_unhandled`, which the runtime calls ahead of the title's own)
//! and appended to the crash log, with the title's last debug output, which
//! often states the reason. Panics of ours go to the crash log too.
use std::ptr::null_mut;
use windows_sys::Win32::Foundation::{
EXCEPTION_ACCESS_VIOLATION, EXCEPTION_ILLEGAL_INSTRUCTION, EXCEPTION_INT_DIVIDE_BY_ZERO,
EXCEPTION_PRIV_INSTRUCTION, EXCEPTION_STACK_OVERFLOW, HMODULE,
EXCEPTION_ACCESS_VIOLATION, EXCEPTION_BREAKPOINT, EXCEPTION_ILLEGAL_INSTRUCTION,
EXCEPTION_INT_DIVIDE_BY_ZERO, EXCEPTION_PRIV_INSTRUCTION, EXCEPTION_STACK_OVERFLOW, HMODULE,
};
use windows_sys::Win32::System::Diagnostics::Debug::{
AddVectoredExceptionHandler, EXCEPTION_POINTERS,
AddVectoredExceptionHandler, CONTEXT, EXCEPTION_POINTERS, RtlLookupFunctionEntry,
RtlVirtualUnwind, SetUnhandledExceptionFilter, UNW_FLAG_NHANDLER,
};
use windows_sys::Win32::System::LibraryLoader::{
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
GetModuleFileNameW, GetModuleHandleExW,
};
use windows_sys::Win32::System::Threading::GetCurrentThreadStackLimits;
const EXCEPTION_CONTINUE_SEARCH: i32 = 0;
/// Stack slots scanned for return addresses into a module.
const STACK_SCAN: usize = 64;
/// Frames listed per report.
pub(crate) const MAX_FRAMES: usize = 40;
/// Installs the handler; call once before running title code.
pub fn report_crashes() {
// SAFETY: registers a handler that only reads the exception it is given.
unsafe { AddVectoredExceptionHandler(0, Some(handler)) };
/// Memory saved to files when a crash is reported: (address, length, file).
#[cfg(feature = "diagnostics")]
static SAVE: std::sync::OnceLock<Vec<(usize, usize, std::path::PathBuf)>> =
std::sync::OnceLock::new();
/// Debugging aid: on a reported crash, also write each `[address, address
/// + len)` (readable title memory, e.g. an engine table) to its file.
#[cfg(feature = "diagnostics")]
pub fn save_on_crash(ranges: Vec<(usize, usize, std::path::PathBuf)>) {
let _ = SAVE.set(ranges);
}
/// The file crash reports are appended to, besides being printed.
static LOG: std::sync::OnceLock<std::path::PathBuf> = std::sync::OnceLock::new();
/// Also appends every unhandled-exception report and panic to `file`, so a
/// crash can be read after the console is gone.
pub fn log_crashes_to(file: std::path::PathBuf) {
if LOG.set(file).is_ok() {
let print = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
print(info);
append_to_log(&format!(
"VIctory: panic on thread {:?}: {info}\n{}",
std::thread::current().name().unwrap_or("?"),
title_output()
));
}));
}
}
/// Appends a failure that is not an exception (the host GPU's) to the crash
/// log, with the title's last debug output, and prints it.
pub fn log_crash(text: &str) {
let text = format!("{text}
{}", title_output());
eprint!("{text}");
append_to_log(&text);
}
fn append_to_log(text: &str) {
use std::io::Write;
let Some(file) = LOG.get() else { return };
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs());
let opened = std::fs::OpenOptions::new().create(true).append(true).open(file);
if let Ok(mut f) = opened {
let _ = write!(f, "unix time {now}, process {}\n{text}\n", std::process::id());
}
}
/// The title's last lines of debug output, kept for crash reports.
static SAID: std::sync::Mutex<std::collections::VecDeque<String>> =
std::sync::Mutex::new(std::collections::VecDeque::new());
const SAID_LINES: usize = 8;
/// Keeps a line of the title's debug output for the next crash report.
pub fn note_title_output(line: &str) {
if line.is_empty() {
return;
}
let mut said = SAID.lock().unwrap_or_else(|e| e.into_inner());
if said.len() == SAID_LINES {
said.pop_front();
}
said.push_back(line.to_owned());
}
/// The kept lines, one per report line; nothing if another thread holds
/// them (it may be the one that faulted).
fn title_output() -> String {
let Ok(said) = SAID.try_lock() else {
return String::new();
};
said.iter().map(|l| format!(" title said: {l}\n")).collect()
}
/// Installs the handlers; call once before running title code.
pub fn report_crashes() {
unsafe extern "system" fn filter(info: *const EXCEPTION_POINTERS) -> i32 {
// SAFETY: the system passes the exception being dispatched.
unsafe { report_unhandled(info) };
EXCEPTION_CONTINUE_SEARCH
}
// SAFETY: registers handlers that only read the exception they are
// given; the title's own filter, once it sets one, takes the place of
// ours and calls `report_unhandled` first.
unsafe {
AddVectoredExceptionHandler(0, Some(handler));
SetUnhandledExceptionFilter(Some(filter));
}
}
/// What the vectored handler reports as it happens.
const FATAL: [i32; 6] = [
EXCEPTION_ACCESS_VIOLATION,
EXCEPTION_ILLEGAL_INSTRUCTION,
EXCEPTION_PRIV_INSTRUCTION,
EXCEPTION_INT_DIVIDE_BY_ZERO,
EXCEPTION_STACK_OVERFLOW,
EXCEPTION_BREAKPOINT,
];
unsafe extern "system" fn handler(info: *mut EXCEPTION_POINTERS) -> i32 {
// SAFETY: the system passes valid exception pointers.
if FATAL.contains(&unsafe { (*(*info).ExceptionRecord).ExceptionCode }) {
// SAFETY: as above.
unsafe { report("first-chance exception", info, false) };
}
EXCEPTION_CONTINUE_SEARCH
}
/// For an unhandled-exception filter: reports the exception that is about
/// to end the process, and appends it to the crash log.
///
/// # Safety
/// `info` comes from the system, for an exception being dispatched.
pub unsafe fn report_unhandled(info: *const EXCEPTION_POINTERS) {
// SAFETY: forwarded from the caller.
unsafe { report("unhandled exception", info, true) };
}
/// MSVC's code for a C++ `throw`, and its magic first parameter.
const CPP_EXCEPTION: i32 = 0xE06D_7363_u32 as i32;
const CPP_MAGIC: usize = 0x1993_0520;
/// What a C++ throw threw: the decorated name of its type, then its value
/// where that names the cause (an integer, e.g. an HRESULT; a standard
/// exception's message).
///
/// # Safety
/// `p` are the parameters of a live MSVC throw (`p[0]` the magic).
unsafe fn thrown(p: &[usize]) -> String {
// SAFETY: the throw's ThrowInfo, its RVAs relative to `p[3]`: catchable
// type array at +12 (count, then type RVAs), each type's descriptor at
// +4, whose decorated name starts at +16; the object is at `p[1]`.
unsafe {
let rva = |at: usize| (at as *const u32).read_unaligned() as usize;
let types = p[3] + rva(p[2] + 12);
let name = |i: usize| {
let descriptor = p[3] + rva(p[3] + rva(types + 4 + 4 * i) + 4);
std::ffi::CStr::from_ptr((descriptor + 16) as *const std::ffi::c_char)
.to_string_lossy()
};
let first = name(0);
let value = match &*first {
// int, unsigned, long (HRESULT), unsigned long
".H" | ".I" | ".J" | ".K" if p[1] != 0 => {
format!(" = {:#010x}", (p[1] as *const u32).read_unaligned())
}
// std::exception keeps its message after the table pointer.
_ if (0..rva(types)).any(|i| name(i) == ".?AVexception@std@@") => {
let message = (p[1] as *const *const std::ffi::c_char).add(1).read();
if message.is_null() {
String::new()
} else {
format!(": {}", std::ffi::CStr::from_ptr(message).to_string_lossy())
}
}
_ => String::new(),
};
format!("{first}{value}")
}
}
/// # Safety
/// See [`report_unhandled`].
unsafe fn report(what: &str, info: *const EXCEPTION_POINTERS, to_file: bool) {
// SAFETY: the system passes valid exception pointers.
let (record, context) = unsafe { (&*(*info).ExceptionRecord, &*(*info).ContextRecord) };
let code = record.ExceptionCode;
if ![
EXCEPTION_ACCESS_VIOLATION,
EXCEPTION_ILLEGAL_INSTRUCTION,
EXCEPTION_PRIV_INSTRUCTION,
EXCEPTION_INT_DIVIDE_BY_ZERO,
EXCEPTION_STACK_OVERFLOW,
]
.contains(&code)
{
return EXCEPTION_CONTINUE_SEARCH;
}
let at = record.ExceptionAddress as usize;
let access = if code == EXCEPTION_ACCESS_VIOLATION {
let kind = match record.ExceptionInformation[0] {
let p = record.ExceptionInformation;
let detail = if code == EXCEPTION_ACCESS_VIOLATION {
let kind = match p[0] {
0 => "read",
1 => "write",
_ => "execute",
};
format!(" ({kind} {:#x})", record.ExceptionInformation[1])
format!(" ({kind} {:#x})", p[1])
} else if code == CPP_EXCEPTION && record.NumberParameters >= 4 && p[0] == CPP_MAGIC {
// SAFETY: a live MSVC throw.
format!(" (C++ {} thrown)", unsafe { thrown(&p) })
} else {
String::new()
};
eprintln!(
"VIctory: exception {:#010x} at {}{access}",
// Printed line by line (a fault while unwinding must not lose the
// lines before it), and kept for the file.
let mut text = String::new();
let mut line = |l: String| {
eprintln!("{l}");
text.push_str(&l);
text.push('\n');
};
line(format!(
"VIctory: {what} {:#010x} at {}{detail}",
code as u32,
locate(at)
);
eprintln!(
" rax {:#x} rbx {:#x} rcx {:#x} rdx {:#x} rsi {:#x} rdi {:#x} rsp {:#x}",
context.Rax, context.Rbx, context.Rcx, context.Rdx, context.Rsi, context.Rdi, context.Rsp
);
// shortcut: a stack scan, not an unwind; it lists stale return
// addresses too. Use RtlVirtualUnwind if this misleads.
let stack = context.Rsp as *const usize;
for i in 0..STACK_SCAN {
// SAFETY: slots just above the faulting RSP are committed stack.
let value = unsafe { stack.add(i).read_unaligned() };
if let Some(name) = module_of(value) {
eprintln!(" [rsp+{:#x}] {name}", i * 8);
));
line(format!(
" rax {:#x} rbx {:#x} rcx {:#x} rdx {:#x} rsi {:#x} rdi {:#x} rsp {:#x} rbp {:#x}",
context.Rax,
context.Rbx,
context.Rcx,
context.Rdx,
context.Rsi,
context.Rdi,
context.Rsp,
context.Rbp
));
line(format!(
" r8 {:#x} r9 {:#x} r10 {:#x} r11 {:#x} r12 {:#x} r13 {:#x} r14 {:#x} r15 {:#x}",
context.R8,
context.R9,
context.R10,
context.R11,
context.R12,
context.R13,
context.R14,
context.R15
));
for (i, pc) in unwind(*context).into_iter().enumerate() {
line(format!(" #{i:<2} {}", locate(pc as usize)));
}
if to_file {
let said = title_output();
eprint!("{said}");
text.push_str(&said);
append_to_log(&text);
}
#[cfg(feature = "diagnostics")]
for (address, len, file) in SAVE.get().into_iter().flatten() {
// SAFETY: the caller of `save_on_crash` named readable memory.
let bytes = unsafe { std::slice::from_raw_parts(*address as *const u8, *len) };
let saved = std::fs::write(file, bytes);
eprintln!(
" saved {address:#x}+{len:#x} to {} ({saved:?})",
file.display()
);
}
}
/// Return addresses of the faulting thread's frames, from the faulting
/// instruction up, by the unwind data of each image (the Windows loader
/// registered it for title images too). Code without unwind data (our
/// thunks, leaf functions) is taken to have its return address at `rsp`.
fn unwind(context: CONTEXT) -> Vec<u64> {
let (mut low, mut high) = (0, 0);
// SAFETY: writes the current thread's stack bounds.
unsafe { GetCurrentThreadStackLimits(&mut low, &mut high) };
let mut frames = Vec::with_capacity(MAX_FRAMES);
unwind_into(context, low, high, &mut frames);
frames
}
/// [`unwind`] for any thread of the process, its stack being `low..high`;
/// pushes no more than `MAX_FRAMES`, so it does not allocate when `frames`
/// has that capacity (another thread may be suspended holding the heap).
pub(crate) fn unwind_into(mut context: CONTEXT, low: usize, high: usize, frames: &mut Vec<u64>) {
frames.push(context.Rip);
while frames.len() < MAX_FRAMES {
// Stop where reading the stack would fault inside this handler.
let rsp = context.Rsp as usize;
if rsp < low || rsp + 8 > high {
break;
}
let mut image = 0;
// SAFETY: looks up the function table entry covering `Rip`, if any.
let entry = unsafe { RtlLookupFunctionEntry(context.Rip, &mut image, null_mut()) };
if entry.is_null() {
// SAFETY: `rsp` is inside this thread's stack.
context.Rip = unsafe { (rsp as *const u64).read() };
context.Rsp += 8;
} else {
let (mut data, mut frame) = (null_mut(), 0);
// SAFETY: `entry` belongs to `image`; the context is our copy.
unsafe {
RtlVirtualUnwind(
UNW_FLAG_NHANDLER,
image,
context.Rip,
entry,
&mut context,
&mut data,
&mut frame,
null_mut(),
)
};
}
if context.Rip == 0 {
break;
}
frames.push(context.Rip);
}
EXCEPTION_CONTINUE_SEARCH
}
/// `module+rva`, or the bare address.
@@ -99,3 +352,32 @@ fn module_of(address: usize) -> Option<String> {
let name = path.rsplit('\\').next().unwrap_or(&path);
Some(format!("{name}+{:#x}", address - module as usize))
}
#[cfg(test)]
mod tests {
use super::*;
use windows_sys::Win32::System::Diagnostics::Debug::{
RtlCaptureContext, RtlCaptureStackBackTrace,
};
#[test]
#[inline(never)]
fn unwinds_like_the_system_walk() {
// SAFETY: zeroed CONTEXT is valid; both calls write our buffers.
let (frames, walked) = unsafe {
let mut context: CONTEXT = std::mem::zeroed();
RtlCaptureContext(&mut context);
let mut walked = [null_mut(); 8];
let n = RtlCaptureStackBackTrace(0, 8, walked.as_mut_ptr(), null_mut());
(unwind(context), walked[..usize::from(n)].to_vec())
};
// Frame 0 differs (two call sites in this function); callers agree.
assert!(walked.len() >= 4, "{walked:?}");
let ours: Vec<_> = frames[1..walked.len()]
.iter()
.map(|&f| f as usize)
.collect();
let system: Vec<_> = walked[1..].iter().map(|&f| f as usize).collect();
assert_eq!(ours, system);
}
}