From e6a2c5471aa80e5669c78e424cadc9cf428208df Mon Sep 17 00:00:00 2001 From: 9hvn9yt2vk-netizen <360448330+9hvn9yt2vk-netizen@users.noreply.github.com> Date: Tue, 24 Nov 2026 00:01:09 +0100 Subject: [PATCH] texture: refuse extents past 16384 texels instead of overflowing Layout::new checked that width, height and depth are non-zero but not that they are bounded, so values a title passes unchecked (up to u32::MAX) overflowed the size arithmetic: `offset * depth` in the linear layout, `slice_size * slices` and the per-mip sizes in the tiled one, and a u32 next_multiple_of in Layout::tiled. Debug builds panicked; release builds wrapped silently and returned a wrong size. Refuse anything past 16384 texels per side, the most a GFX10 image descriptor holds, so no texture the GPU can use is affected. Found with a random-input harness (four distinct overflow sites, none left after this change). --- crates/victory-formats/src/texture.rs | 34 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/victory-formats/src/texture.rs b/crates/victory-formats/src/texture.rs index 8752447..73ef98d 100644 --- a/crates/victory-formats/src/texture.rs +++ b/crates/victory-formats/src/texture.rs @@ -249,6 +249,13 @@ fn spread(bits: &[u32]) -> Vec { const MAX_MIPS: u32 = 16; +/// The largest width, height and depth (or array size) accepted, in texels. +/// GFX10's image descriptors hold at most 16384 texels per side, so no +/// texture the GPU can use is refused. The bound also keeps every size +/// computed below within `u64` (and its `u32` steps), which unchecked +/// values from a title would otherwise overflow. +const MAX_EXTENT: u32 = 1 << 14; + /// 256-byte micro tile dimensions by log2 element size, 2D and 3D. const BLOCK_256_2D: [[u32; 2]; 5] = [[16, 16], [16, 8], [8, 8], [8, 4], [4, 4]]; const BLOCK_256_3D: [[u32; 3]; 5] = [[8, 4, 8], [4, 4, 8], [4, 4, 4], [4, 2, 4], [2, 2, 4]]; @@ -258,9 +265,9 @@ impl Layout { pub fn new(desc: &Desc) -> Result { let d = *desc; let block_log2 = d.swizzle.block_log2().ok_or(Error::Invalid)?; - let valid = d.width > 0 - && d.height > 0 - && d.depth > 0 + let valid = (1..=MAX_EXTENT).contains(&d.width) + && (1..=MAX_EXTENT).contains(&d.height) + && (1..=MAX_EXTENT).contains(&d.depth) && (1..=MAX_MIPS).contains(&d.mips) && d.element_bytes.is_power_of_two() && d.element_bytes <= 16 @@ -838,6 +845,27 @@ mod tests { } } + /// Sizes past the hardware's 16384 texels per side are refused instead + /// of overflowing the size arithmetic (found by random-input testing). + #[test] + fn extents_past_the_hardware_limit_are_invalid() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_64KB_D_X] { + let largest = Layout::new(&desc(MAX_EXTENT, MAX_EXTENT, 15, 16, swizzle)).unwrap(); + assert!(largest.size >= u64::from(MAX_EXTENT) * u64::from(MAX_EXTENT) * 16); + for (w, h) in [ + (MAX_EXTENT + 1, 1), + (1, MAX_EXTENT + 1), + (u32::MAX, u32::MAX), + ] { + let result = Layout::new(&desc(w, h, 1, 16, swizzle)); + assert_eq!(result, Err(Error::Invalid), "{w}x{h}"); + } + let mut deep = desc(1, 1, 1, 16, swizzle); + deep.depth = u32::MAX; + assert_eq!(Layout::new(&deep), Err(Error::Invalid)); + } + } + #[test] fn blocks_hold_their_size_in_elements() { for log2 in [8u32, 12, 16] {