Files
xenia-rs/crates/xenia-kernel/src/path.rs
MechaCat02 ad45873a1b ITERATE-2.V: scheduler priority aging closes 18-day AUDIT-049 wedge
Priority aging in xenia-cpu/scheduler.rs:pick_runnable
(effective_priority = base + age_bonus(now_round - last_run_round),
capped at +31, AGING_ROUNDS_PER_BONUS=1). Strict-priority was parking
priority=0 threads behind CPU-bound priority=15 audio mixer
(sub_824D1328 guest spinwait at PC=0x824d1404 on CPU5). Aging
eventually picks the starved thread, breaking the producer-consumer
cycle that caused 5-tid wedge at PC=0x824ac578 since AUDIT-049 (10 May).

Cascade observed: tid=13 clean exit; events 121K -> 13M (107x); last
host_ns 767ms -> 51,011ms (66x); 8 new threads spawn; VdSwap 1 -> 2.

Complete two-day iterate sequence (2026-05-27 -> 2026-05-28):
- 2.F: VdSwap drain timeout 900ms -> 1ms (xenia-gpu/handle.rs); 876x
       perf win on VdSwap kernel callback
- 2.H: vA0000000 physical heap bucket added (state.rs, exports.rs);
       ctx_ptrs now in 0xA0000000-0xBFFFFFFF range matching canary
- 2.L: Phase-A diff harness categorized [return_value mismatch],
       [status mismatch], [args_resolved.path mismatch] tags
       (tools/diff-events/diff_events.py); closes reading-error #41
       (silent test-harness state leak invalidating trace diffs)
- 2.M: always-on exit-thread-state.json sibling to Phase-A JSONL
       (event_log.rs + xenia-app/main.rs); closes reading-error #42
       (Phase-A blind to blocked-forever waits)
- 2.Q: signal.match kernel instrumentation in NtSetEvent /
       NtReleaseSemaphore / KeSetEvent / KeReleaseSemaphore
       (exports.rs); emits target_handle + waiter_count + waiter_tids
- 2.T: wake.requested kernel instrumentation in wake_eligible_waiters
       (exports.rs); emits target_tid + transition + new_state
- 2.V: scheduler priority aging (xenia-cpu/scheduler.rs) [keystone]

Plus accumulated WIP from earlier May (contention_manifest,
phase_b_snapshot, xam/xaudio enhancements, analysis db, xex loader,
xenia-app main loop, etc.). Audit-runs/ artifacts remain untracked
per project convention.

Tests: 300 xenia-cpu / 227 xenia-kernel / 5 xenia-app / 19 xenia-path
/ 30+ smaller suites -- all PASS, 0 regressions. Determinism preserved
(2x cold runs bit-identical at 13,003,881 events post-2.V).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 07:27:26 +02:00

182 lines
5.7 KiB
Rust

//! Path normalization for kernel file I/O.
//!
//! Guests pass file paths inside an `OBJECT_ATTRIBUTES` struct that points at
//! an `ANSI_STRING` descriptor. Those paths come in several Xbox-flavored
//! forms — NT device paths (`\Device\Cdrom0\...`), drive letters (`D:\...`,
//! `d:\...`), or symbolic link prefixes (`game:\...`). We strip whichever
//! prefix applies and return a plain slash-separated path relative to the
//! mounted VFS root, so `VfsDevice::read_file` can look it up directly.
use xenia_memory::{GuestMemory, MemoryAccess};
/// Xbox `ANSI_STRING`:
/// u16 Length
/// u16 MaximumLength
/// u32 Buffer (guest pointer)
fn read_ansi_string(mem: &GuestMemory, ptr: u32) -> Option<String> {
if ptr == 0 {
return None;
}
let length = mem.read_u16(ptr) as u32;
let buffer = mem.read_u32(ptr + 4);
if buffer == 0 || length == 0 {
return Some(String::new());
}
let mut out = String::with_capacity(length as usize);
for i in 0..length {
let c = mem.read_u8(buffer + i);
if c == 0 {
break;
}
out.push(c as char);
}
Some(out)
}
/// Xbox `OBJECT_ATTRIBUTES`:
/// u32 RootDirectory (handle)
/// u32 Name (pointer to ANSI_STRING)
/// u32 Attributes
fn read_object_attributes_name(mem: &GuestMemory, obj_attrs_ptr: u32) -> Option<String> {
if obj_attrs_ptr == 0 {
return None;
}
let name_ptr = mem.read_u32(obj_attrs_ptr + 4);
read_ansi_string(mem, name_ptr)
}
/// Known Xbox device prefixes that need to be stripped before looking a path
/// up in the VFS. The list mirrors the symbolic links xenia-canary sets up
/// at boot (see `xboxkrnl_io.cc`). Case-insensitive matching.
const DEVICE_PREFIXES: &[&str] = &[
"\\Device\\Cdrom0\\",
"\\Device\\Harddisk0\\Partition1\\",
"\\Device\\Harddisk0\\Partition0\\",
"\\Device\\Harddisk0\\",
"\\Device\\Mu0\\",
"\\Device\\Mu1\\",
"\\Device\\Mass0\\",
"\\Device\\Mass1\\",
"\\Device\\Mass2\\",
"\\SystemRoot\\",
"\\??\\",
"game:\\",
"d:\\",
"D:\\",
];
/// Strip any Xbox device prefix and normalize backslashes to forward slashes.
/// Returns the path relative to the VFS root.
pub fn normalize_path(raw: &str) -> String {
let mut s = raw.trim().to_string();
// Case-insensitive prefix strip.
let lowered = s.to_ascii_lowercase();
for prefix in DEVICE_PREFIXES {
let pl = prefix.to_ascii_lowercase();
if lowered.starts_with(&pl) {
s = s[pl.len()..].to_string();
break;
}
}
// Drop any leading slash/backslash that survived prefix stripping.
while s.starts_with('\\') || s.starts_with('/') {
s.remove(0);
}
// Canonical form: forward slashes.
s.replace('\\', "/")
}
/// Convenience: read the OBJECT_ATTRIBUTES struct at `obj_attrs_ptr` and
/// return a normalized VFS path. Returns `None` if the struct pointer or its
/// inner name pointer is null.
pub fn object_attributes_to_vfs_path(mem: &GuestMemory, obj_attrs_ptr: u32) -> Option<String> {
let raw = read_object_attributes_name(mem, obj_attrs_ptr)?;
if raw.is_empty() {
return None;
}
Some(normalize_path(&raw))
}
/// Phase C+10 schema-v1 extension helper: read the OBJECT_ATTRIBUTES
/// struct at `obj_attrs_ptr` and return the **raw** path string (trimmed
/// of leading/trailing whitespace, NO prefix-strip / case-fold). The
/// emitter wants the exact bytes the guest passed so the Phase A diff
/// surfaces upstream divergences (e.g. canary calls with one prefix /
/// ours with another) rather than masking them via normalization.
pub fn object_attributes_raw_name(mem: &GuestMemory, obj_attrs_ptr: u32) -> Option<String> {
let raw = read_object_attributes_name(mem, obj_attrs_ptr)?;
if raw.is_empty() {
return None;
}
Some(raw.trim().to_string())
}
/// Phase C+11 schema-v1 extension helper: read the rename target
/// path from a `NtSetInformationFile` class-10 (`XFileRenameInformation`)
/// info buffer. Returns the raw (un-normalized) path string for emitter
/// use; null when the buffer is too small or the inner ANSI_STRING is
/// empty.
///
/// Layout per canary `info/file.h:79-83`:
/// offset 0: be<u32> replace_existing
/// offset 4: be<u32> root_dir_handle
/// offset 8: X_ANSI_STRING (u16 Length, u16 MaximumLength, u32 Buffer)
/// (16 bytes total — caller is expected to check `info_length >= 16`
/// before invoking.)
pub fn file_rename_information_raw_target(
mem: &GuestMemory,
info_ptr: u32,
info_length: u32,
) -> Option<String> {
if info_ptr == 0 || info_length < 16 {
return None;
}
// The ANSI_STRING lives at offset 8 inside the rename-info struct.
let raw = read_ansi_string(mem, info_ptr + 8)?;
if raw.is_empty() {
return None;
}
Some(raw.trim().to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strips_device_cdrom() {
assert_eq!(normalize_path("\\Device\\Cdrom0\\default.xex"), "default.xex");
}
#[test]
fn strips_drive_letter_lowercase() {
assert_eq!(normalize_path("d:\\media\\shared\\foo.pkg"), "media/shared/foo.pkg");
}
#[test]
fn strips_drive_letter_uppercase() {
assert_eq!(normalize_path("D:\\default.xex"), "default.xex");
}
#[test]
fn strips_game_prefix() {
assert_eq!(normalize_path("game:\\data\\whatever.bin"), "data/whatever.bin");
}
#[test]
fn preserves_relative_path() {
assert_eq!(normalize_path("scripts/init.lua"), "scripts/init.lua");
}
#[test]
fn handles_partition1() {
assert_eq!(
normalize_path("\\Device\\Harddisk0\\Partition1\\content\\abc.sav"),
"content/abc.sav"
);
}
}