Merge fork main a803c87

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 18:57:47 -06:00
commit fa967c0340
22 files changed
+895 -117

No files matched your search

+131 -10
View File
@@ -847,11 +847,47 @@ fn execute(request: &Request) -> Result<(), HRESULT> {
/// that ends short of `out` passes: the title's chunks fill it exactly, and
/// nothing says that is required.
fn inflate(source: &[u8], out: &mut [u8]) -> Result<usize, HRESULT> {
miniz_oxide::inflate::decompress_slice_iter_to_slice(out, std::iter::once(source), true, false)
.map_err(|status| {
tracing::debug!(?status, "dstorage: zlib stream");
INVALID_DATA
})
thread_local! {
static INFLATE: std::cell::RefCell<zlib_rs::Inflate> =
std::cell::RefCell::new(zlib_rs::Inflate::new(true, 15));
}
INFLATE.with_borrow_mut(|state| {
state.reset(true);
// `Inflate::decompress` cannot distinguish a valid empty stream from
// an insufficient buffer when `avail_out == 0`, so use a 1-byte scratch
// destination and verify `written <= out_len` afterward.
let out_len = out.len();
let mut scratch = [0u8; 1];
let dest = if out_len == 0 { &mut scratch[..] } else { out };
let (mut in_pos, mut out_pos) = (0usize, 0usize);
let status = loop {
match state.decompress(
&source[in_pos..],
&mut dest[out_pos..],
zlib_rs::InflateFlush::NoFlush,
) {
Ok(zlib_rs::Status::StreamEnd) => break Ok(zlib_rs::Status::StreamEnd),
Ok(zlib_rs::Status::Ok) => {
let next_in = state.total_in() as usize;
let next_out = state.total_out() as usize;
if next_in == in_pos && next_out == out_pos {
break Ok(zlib_rs::Status::BufError);
}
in_pos = next_in;
out_pos = next_out;
}
other => break other,
}
};
let written = state.total_out() as usize;
match status {
Ok(zlib_rs::Status::StreamEnd) if written <= out_len => Ok(written),
other => {
tracing::debug!(?other, "dstorage: zlib stream");
Err(INVALID_DATA)
}
}
})
}
/// The BCPack format's constant tables, parsed once for the process. If
@@ -974,10 +1010,30 @@ fn into_destination(
}
let mut data = vec![0; len];
let n = fill(&mut data)?;
if super::xmem::write_physical(pages, offset, &data[..n]) {
// A page the title has just made, or a view the host refuses for a moment (commit
// or address space short while a load maps many at once), can come right a little
// later: tried again before the request fails (`VICTORY_DSTORAGE_RETRIES`, default 12,
// about 1.5 s in all).
static RETRIES: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
let retries = *RETRIES.get_or_init(|| {
std::env::var("VICTORY_DSTORAGE_RETRIES").ok().and_then(|v| v.parse().ok()).unwrap_or(12)
});
let mut written = super::xmem::write_physical(pages, offset, &data[..n]);
let mut tries = 0;
while !written && tries < retries {
std::thread::sleep(std::time::Duration::from_millis((2u64 << tries.min(6)).min(128)));
tries += 1;
written = super::xmem::write_physical(pages, offset, &data[..n]);
}
if written {
super::xmem::pages_written(pages, offset, n);
if tries > 0 {
tracing::info!(offset, len, tries, "dstorage: a destination's physical pages written after a retry");
}
Ok(())
} else {
let known = super::xmem::pages_known(pages, offset, len);
tracing::warn!(known, last_error = std::io::Error::last_os_error().raw_os_error(), "dstorage: retries used up");
// Said aloud: the title never reads a request's result
// (it only polls for completion), so a destination left unwritten
// is a model or texture that silently holds whatever its pages
@@ -1187,10 +1243,18 @@ mod tests {
assert_eq!(std::mem::offset_of!(QueueDesc, unknown), 0x18);
}
fn compress_zlib(plain: &[u8]) -> Vec<u8> {
let mut buf = vec![0u8; zlib_rs::compress_bound(plain.len())];
let (packed, rc) =
zlib_rs::compress_slice(&mut buf, plain, zlib_rs::DeflateConfig::new(6));
assert_eq!(rc, zlib_rs::ReturnCode::Ok);
packed.to_vec()
}
#[test]
fn raw_and_zlib_reads_from_a_file() {
let plain = pattern(300_000);
let packed = miniz_oxide::deflate::compress_to_vec_zlib(&plain, 6);
let packed = compress_zlib(&plain);
let mut contents = plain.clone();
contents.extend_from_slice(&packed);
let (_dir, file) = file_with(&contents);
@@ -1222,7 +1286,7 @@ mod tests {
#[test]
fn memory_sources_inflate_too() {
let plain = pattern(70_000);
let packed = miniz_oxide::deflate::compress_to_vec_zlib(&plain, 6);
let packed = compress_zlib(&plain);
let (queue, array) = (new_queue(1), status_array(1));
let mut out = vec![0u8; plain.len()];
let options = ZLIB | MEMORY_SOURCE;
@@ -1399,7 +1463,7 @@ mod tests {
#[test]
fn zlib_then_bcpack() {
with_test_tables();
let packed = miniz_oxide::deflate::compress_to_vec_zlib(&BC4_STREAM, 6);
let packed = compress_zlib(&BC4_STREAM);
let (_dir, file) = file_with(&packed);
let (queue, array) = (new_queue(0), status_array(1));
let mut out = [0u8; 16];
@@ -1541,7 +1605,7 @@ mod tests {
};
let (a, b) = (allocate(3), allocate(1));
let plain = pattern(2 * PAGE + 100);
let packed = miniz_oxide::deflate::compress_to_vec_zlib(&plain, 6);
let packed = compress_zlib(&plain);
// One allocation's pages in order (one window), then pages of two
// (through a buffer), both from 50 bytes into the first.
let mut destinations = [a.clone(), vec![a[0], a[1], b[0]]];
@@ -1586,4 +1650,61 @@ mod tests {
assert_eq!(xmem::XMemFreePhysicalPages(3, a.as_ptr()), 1);
assert_eq!(xmem::XMemFreePhysicalPages(1, b.as_ptr()), 1);
}
#[test]
fn zlib_inflate_handles_short_exact_empty_and_bad_streams() {
let plain = pattern(65_537);
let packed = compress_zlib(&plain);
// Exact fit.
let mut exact = vec![0u8; plain.len()];
assert_eq!(inflate(&packed, &mut exact), Ok(plain.len()));
assert_eq!(exact, plain);
// Short stream into a larger buffer passes and returns bytes written;
// guard bytes outside the slice passed to `inflate` stay untouched.
let mut larger = vec![0xAA; plain.len() + 64];
assert_eq!(
inflate(&packed, &mut larger[..plain.len() + 32]),
Ok(plain.len())
);
assert_eq!(&larger[..plain.len()], &plain[..]);
assert!(larger[plain.len() + 32..].iter().all(|&b| b == 0xAA));
// Empty valid stream passes for empty or non-empty destinations.
let empty = compress_zlib(&[]);
assert_eq!(inflate(&empty, &mut []), Ok(0));
let mut scratch = [0x55u8; 4];
assert_eq!(inflate(&empty, &mut scratch), Ok(0));
// Oversized stream (destination too small, including empty) fails.
let mut small = vec![0u8; plain.len() - 1];
assert_eq!(inflate(&packed, &mut small), Err(INVALID_DATA));
assert_eq!(inflate(&packed, &mut []), Err(INVALID_DATA));
// Truncated stream (missing trailer or payload bytes) fails.
let mut out = vec![0u8; plain.len()];
assert_eq!(
inflate(&packed[..packed.len() - 1], &mut out),
Err(INVALID_DATA)
);
assert_eq!(
inflate(&packed[..packed.len() / 2], &mut out),
Err(INVALID_DATA)
);
assert_eq!(inflate(&[], &mut out), Err(INVALID_DATA));
// Corrupt Adler-32 trailer fails.
let mut bad_adler = packed.clone();
*bad_adler.last_mut().unwrap() ^= 1;
assert_eq!(inflate(&bad_adler, &mut out), Err(INVALID_DATA));
// Corrupt header/payload fails, and subsequent valid inflate on the
// same thread still succeeds (verifies thread-local state reset).
let mut bad_body = packed.clone();
bad_body[2] ^= 0xFF;
assert_eq!(inflate(&bad_body, &mut out), Err(INVALID_DATA));
assert_eq!(inflate(&packed, &mut out), Ok(plain.len()));
assert_eq!(out, plain);
}
}