From 23da2c327afa20128ac429c31be00a1111fb731d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 2 Dec 2026 20:45:28 -0800 Subject: [PATCH] :white_check_mark: Refuse blits past what they name in the fake The fake recorded every copy and fill, where the Vulkan platform's runtime refuses one that reaches past what it names (metal2vulkan-runtime blit.rs: buffer_to_buffer, fill_range, buffer_copy; texture.rs: place; at pin 208360ab6d): a range past a buffer or meeting its own source, a fill of no bytes or past its buffer, a level, slice or region outside a texture or off its block grid, and a buffer layout whose rows, images or bytes do not fit. The fake now refuses each, after logging the call and answering its script, its texels sized from metal2vulkan's format tables as rt_platform/blit.rs sizes them. A census of the fake's tests found four tests whose blits it now refuses (every_blit_command_drives_its_selector and three of surface_cache), each of which reads only the call log and passes as before, and one fill of the command-buffer fuzz. Nothing changes for the guest. --- .../src/core/testkit/impls/blit.rs | 308 +++++++++++++++++- .../src/core/testkit/impls/mod.rs | 103 ++++++ .../src/core/testkit/impls/resource.rs | 33 ++ 3 files changed, 439 insertions(+), 5 deletions(-) diff --git a/crates/reims-vgpu-vulkan/src/core/testkit/impls/blit.rs b/crates/reims-vgpu-vulkan/src/core/testkit/impls/blit.rs index 9c7422b19..d6900dd61 100644 --- a/crates/reims-vgpu-vulkan/src/core/testkit/impls/blit.rs +++ b/crates/reims-vgpu-vulkan/src/core/testkit/impls/blit.rs @@ -1,16 +1,20 @@ //! Fake: the `platform/blit.rs` traits over the fake models (architecture 13.2). //! //! Every blit command is ordered: each logs its selector and arguments (`fake::calllog`) and -//! consults the script (`fake::script`); see `fake::impls` for the outcome channels. +//! consults the script (`fake::script`); see `fake::impls` for the outcome channels. A copy or +//! fill the Vulkan platform refuses for where it reaches (`outside`) is refused, recording +//! nothing, as the platform refuses it. use std::ops::Range; use super::super::calllog::Arg; -use super::super::objects::{Fk, kind}; +use super::super::objects::{Fk, TexDesc, kind}; use super::super::platform::FakePlatform; use super::res_id; -use crate::core::platform::Encoded; +use super::resource::{level_extent, slice_count, texel_unit}; use crate::core::platform::blit::*; +use crate::core::platform::types::{MtlOrigin, MtlSize}; +use crate::core::platform::{Encoded, Handle as _}; use crate::core::raise::{OnFault, RaiseResult}; use crate::core::testkit::script::{Policy, Step}; @@ -96,7 +100,10 @@ impl BlitEncoder for Fk { /// Each command is logged, then answered as the script says ([`Self::blit_act`]). fn encode(&self, on: OnFault, cmd: BlitCmd<'_, FakePlatform>) -> RaiseResult { let sel = cmd.selector(); - match cmd { + // A copy or fill the Vulkan platform refuses for where it reaches is refused, once the + // call is logged and its script answered. + let outside = outside(&cmd); + let encoded = match cmd { BlitCmd::CopyBufferToTexture { source_buffer, source_offset, @@ -445,6 +452,297 @@ impl BlitEncoder for Fk { BlitCmd::WaitForFence { fence } => { self.blit_act(Policy::Caught, "waitForFence:", vec![Arg::obj(fence)]) } - } + }?; + Ok(if outside { Encoded::REFUSED } else { encoded }) } } + +/// Whether the Vulkan platform's runtime refuses `cmd` for where it reaches, by its checks of a +/// copy and a fill (metal2vulkan-runtime's `blit.rs`: `buffer_to_buffer`, `fill_range` and +/// `buffer_copy`; `texture.rs`: `place`), its texels sized as the runtime sizes them +/// ([`texel_unit`]): a range past a buffer, two ranges of one buffer whose bytes meet, a fill of +/// no bytes, a level, slice or region outside a texture or off its block grid, rows or an offset +/// in a buffer off its texels, rows or images closer than the region's, and bytes past the +/// buffer. False where it reaches only what it names, and where a buffer or texture has no +/// model or a format the fake does not size. +fn outside(cmd: &BlitCmd<'_, FakePlatform>) -> bool { + match *cmd { + BlitCmd::CopyBufferToBuffer { + source_buffer: Some(source), + source_offset, + destination_buffer: Some(destination), + destination_offset, + size, + } => { + let past = |b: &Fk, at: u64| { + (b.modelled_length()) + .is_some_and(|len| at.checked_add(size).is_none_or(|end| end > len)) + }; + let meets = size != 0 + && source.id() == destination.id() + && source_offset < destination_offset.saturating_add(size) + && destination_offset < source_offset.saturating_add(size); + past(source, source_offset) || past(destination, destination_offset) || meets + } + BlitCmd::FillBuffer { buffer, range, .. } + | BlitCmd::FillBufferPattern4 { buffer, range, .. } => { + buffer.modelled_length().is_some_and(|len| { + range.length == 0 + || (range.location.checked_add(range.length)).is_none_or(|end| end > len) + }) + } + BlitCmd::CopyBufferToTexture { + source_buffer, + source_offset, + source_bytes_per_row, + source_bytes_per_image, + source_size, + destination_texture, + destination_slice, + destination_level, + destination_origin, + } => bytes_copy( + (destination_texture, destination_slice, destination_level), + destination_origin, + source_size, + source_buffer, + Some((source_offset, source_bytes_per_row, source_bytes_per_image)), + ), + BlitCmd::CopyBufferToTextureOptions { + source_buffer: Some(buffer), + source_offset, + source_bytes_per_row, + source_bytes_per_image, + source_size, + destination_texture: Some(texture), + destination_slice, + destination_level, + destination_origin, + options, + } => bytes_copy( + (texture, destination_slice, destination_level), + destination_origin, + source_size, + buffer, + (options == 0).then_some((source_offset, source_bytes_per_row, source_bytes_per_image)), + ), + BlitCmd::CopyTextureToBuffer { + source_texture, + source_slice, + source_level, + source_origin, + source_size, + destination_buffer, + destination_offset, + destination_bytes_per_row, + destination_bytes_per_image, + } => bytes_copy( + (source_texture, source_slice, source_level), + source_origin, + source_size, + destination_buffer, + Some(( + destination_offset, + destination_bytes_per_row, + destination_bytes_per_image, + )), + ), + BlitCmd::CopyTextureToBufferOptions { + source_texture: Some(texture), + source_slice, + source_level, + source_origin, + source_size, + destination_buffer: Some(buffer), + destination_offset, + destination_bytes_per_row, + destination_bytes_per_image, + options, + } => bytes_copy( + (texture, source_slice, source_level), + source_origin, + source_size, + buffer, + (options == 0).then_some(( + destination_offset, + destination_bytes_per_row, + destination_bytes_per_image, + )), + ), + BlitCmd::CopyTextureRegion { + source_texture, + source_slice, + source_level, + source_origin, + source_size, + destination_texture, + destination_slice, + destination_level, + destination_origin, + } + | BlitCmd::CopyTextureRegionOptions { + source_texture, + source_slice, + source_level, + source_origin, + source_size, + destination_texture, + destination_slice, + destination_level, + destination_origin, + options: 0, + } => { + let size = [source_size.width, source_size.height, source_size.depth]; + let off = |t: &Fk, slice: u64, level: u64, origin: MtlOrigin| { + let origin = [origin.x, origin.y, origin.z]; + (t.modelled_desc()).is_some_and(|d| place(&d, slice, level, origin, size).is_err()) + }; + off(source_texture, source_slice, source_level, source_origin) + || off( + destination_texture, + destination_slice, + destination_level, + destination_origin, + ) + } + BlitCmd::CopyTextureSlices { + source_texture, + source_slice, + source_level, + destination_texture, + destination_slice, + destination_level, + slice_count: slices, + level_count: levels, + } => { + let (Some(s), Some(d)) = ( + source_texture.modelled_desc(), + destination_texture.modelled_desc(), + ) else { + return false; + }; + if slices == 0 || levels == 0 { + return false; + } + let fits = |t: &TexDesc, slice: u64, level: u64| { + slice + .checked_add(slices) + .is_some_and(|end| end <= slice_count(t)) + && level + .checked_add(levels) + .is_some_and(|end| end <= t.mipmap_level_count) + }; + if !fits(&s, source_slice, source_level) + || !fits(&d, destination_slice, destination_level) + { + return true; + } + // Each level of each slice is copied whole: the source level's extent, from its + // origin into the destination's level at its origin. + (0..levels).any(|l| { + let [width, height, depth] = level_extent(&s, source_level + l); + let size = [width, height, if s.texture_type == 7 { depth } else { 1 }]; + let off = |t: &TexDesc, slice, level| place(t, slice, level, [0; 3], size).is_err(); + off(&s, source_slice, source_level + l) + || off(&d, destination_slice, destination_level + l) + }) + } + _ => false, + } +} + +/// Whether the Vulkan platform's runtime refuses a copy of `size` texels at `origin` of a level +/// and slice of a texture and a buffer's bytes laid out as `layout` (offset, bytes a row, bytes +/// an image; `None`, a plane's copy, whose texels the fake does not size), as its `buffer_copy` +/// finds it: the region outside the texture or off its block grid ([`place`]), rows or an offset +/// off a format of single texels' texels, rows or images closer than the region's, or bytes past +/// the buffer. A copy of no texels reaches no bytes. +fn bytes_copy( + (texture, slice, level): (&Fk, u64, u64), + origin: MtlOrigin, + size: MtlSize, + buffer: &Fk, + layout: Option<(u64, u64, u64)>, +) -> bool { + let Some(d) = texture.modelled_desc() else { + return false; + }; + let mut size = [size.width, size.height, size.depth]; + // A 1D texture's region is one row high, and any but a 3D texture's one image deep. + if size[1] == 0 && matches!(d.texture_type, 0 | 1) { + size[1] = 1; + } + if size[2] == 0 && d.texture_type != 7 { + size[2] = 1; + } + let placed = match place(&d, slice, level, [origin.x, origin.y, origin.z], size) { + Err(()) => return true, + Ok(None) => return false, + Ok(Some(placed)) => placed, + }; + let (Some((offset, bytes_per_row, bytes_per_image)), Some(len), Some((bytes, [bw, bh], _))) = + (layout, buffer.modelled_length(), texel_unit(d.pixel_format)) + else { + return false; + }; + let row = placed[0].div_ceil(bw).saturating_mul(bytes); + let (rows, images) = (placed[1].div_ceil(bh), placed[2]); + let single = [bw, bh] == [1, 1]; + if single + && (!offset.is_multiple_of(bytes) || (rows > 1 && !bytes_per_row.is_multiple_of(bytes))) + { + return true; + } + let image = match rows { + 1 => Some(row), + _ if bytes_per_row < row => return true, + _ => (rows - 1) + .checked_mul(bytes_per_row) + .and_then(|span| span.checked_add(row)), + }; + let span = match (images, image) { + (_, None) => None, + (1, image) => image, + (_, Some(image)) if bytes_per_image < image => return true, + (_, Some(image)) => (images - 1) + .checked_mul(bytes_per_image) + .and_then(|span| span.checked_add(image)), + }; + let end = span.and_then(|span| span.checked_add(offset)); + end.is_none_or(|end| end > len) +} + +/// Where a region of `size` texels at `origin` of level `level` and slice `slice` lies in a +/// texture of `d`, as the runtime's `place` finds it, its blocks sized as [`texel_unit`] sizes +/// them (one texel where the fake does not size its format): `Err` (`Refusal::TextureRegion`) +/// for a level or slice past the texture's, or a region past the level's extent or off its +/// block grid (whole blocks may end in a last block the edge cuts); else its size, clamped to +/// the level's edge, and `None` where it holds no texel. +fn place( + d: &TexDesc, + slice: u64, + level: u64, + origin: [u64; 3], + size: [u64; 3], +) -> Result, ()> { + if level >= d.mipmap_level_count || slice >= slice_count(d) { + return Err(()); + } + let [bw, bh] = texel_unit(d.pixel_format).map_or([1, 1], |(_, block, _)| block); + let axes = (origin.into_iter().zip(size)) + .zip(level_extent(d, level)) + .zip([bw, bh, 1]); + let mut placed = [0; 3]; + for (out, (((at, length), edge), grid)) in placed.iter_mut().zip(axes) { + let end = at.checked_add(length).ok_or(())?; + let whole = length.is_multiple_of(grid); + let on_grid = at.is_multiple_of(grid) && (whole || end == edge); + let rounded = + whole && at < edge && (edge.checked_next_multiple_of(grid)).is_some_and(|r| end <= r); + if (end > edge && !rounded) || !on_grid { + return Err(()); + } + *out = end.min(edge).saturating_sub(at); + } + Ok((!size.contains(&0)).then_some(placed)) +} diff --git a/crates/reims-vgpu-vulkan/src/core/testkit/impls/mod.rs b/crates/reims-vgpu-vulkan/src/core/testkit/impls/mod.rs index f02a377c1..5c7cac162 100644 --- a/crates/reims-vgpu-vulkan/src/core/testkit/impls/mod.rs +++ b/crates/reims-vgpu-vulkan/src/core/testkit/impls/mod.rs @@ -809,4 +809,107 @@ mod tests { "every make sent" ); } + + /// A copy or fill is refused, recording nothing, where the Vulkan platform refuses it for + /// where it reaches: a range past a buffer or meeting its own source, a fill of no bytes or + /// past its buffer, a region outside a texture, rows closer than the region's or bytes past + /// the buffer (each texel of RGBA8Unorm 4 bytes), and slices past a texture's; the same + /// commands inside are recorded + #[test] + fn a_blit_past_what_it_names_is_refused() { + use crate::core::platform::Encoded; + use crate::core::platform::blit::{BlitCmd, BlitEncoder}; + use crate::core::platform::types::{MtlOrigin, NsRange}; + const FILL: &str = "fillBuffer:range:value:"; + let p = FakePlatform::new(); + let d = p.device(); + let q = d.new_command_queue().unwrap(); + let cb = q.command_buffer(OnFault::Die).unwrap().unwrap(); + let e = cb.blit_encoder(OnFault::Die).unwrap().unwrap(); + let a = d + .new_buffer_of_length(OnFault::Die, 0x1000, 0) + .unwrap() + .unwrap(); + let b = d + .new_buffer_of_length(OnFault::Die, 0x1000, 0) + .unwrap() + .unwrap(); + let td = p.texture_descriptor(&TextureDesc { + width: 16, + height: 16, + ..TextureDesc::default() + }); + let t = d.new_texture(OnFault::Die, &td).unwrap().unwrap(); + let u = d.new_texture(OnFault::Die, &td).unwrap().unwrap(); + let copy = |from: u64, to: u64, size: u64| BlitCmd::CopyBufferToBuffer { + source_buffer: Some(&a), + source_offset: from, + destination_buffer: Some(&b), + destination_offset: to, + size, + }; + let fill = |location: u64, length: u64| BlitCmd::FillBuffer { + buffer: &a, + range: NsRange::new(location, length), + value: 0, + }; + let upload = |offset: u64, bytes_per_row: u64, size: MtlSize, origin: MtlOrigin| { + BlitCmd::CopyBufferToTexture { + source_buffer: &a, + source_offset: offset, + source_bytes_per_row: bytes_per_row, + source_bytes_per_image: 0x400, + source_size: size, + destination_texture: &t, + destination_slice: 0, + destination_level: 0, + destination_origin: origin, + } + }; + let slices = |level: u64, levels: u64| BlitCmd::CopyTextureSlices { + source_texture: &t, + source_slice: 0, + source_level: level, + destination_texture: &u, + destination_slice: 0, + destination_level: level, + slice_count: 1, + level_count: levels, + }; + let (at, whole) = (MtlOrigin::new(0, 0, 0), MtlSize::new(16, 16, 1)); + let recorded = [ + copy(0, 0x800, 0x800), + fill(0, 0x1000), + upload(0, 64, whole, at), + upload(0x400, 64, MtlSize::new(8, 8, 1), MtlOrigin::new(8, 8, 0)), + slices(0, 1), + ]; + for cmd in recorded { + assert_eq!(e.encode(OnFault::Die, cmd), Ok(Encoded::RECORDED)); + } + let same = BlitCmd::CopyBufferToBuffer { + source_buffer: Some(&a), + source_offset: 0, + destination_buffer: Some(&a), + destination_offset: 0x10, + size: 0x20, + }; + let refused = [ + copy(0x800, 0, 0x801), + copy(0, u64::MAX, 2), + same, + fill(0, 0), + fill(0xf00, 0x101), + upload(0, 60, whole, at), + upload(2, 64, whole, at), + upload(0xc04, 64, whole, at), + upload(0, 64, whole, MtlOrigin::new(1, 0, 0)), + slices(0, 2), + slices(1, 1), + ]; + for cmd in refused { + assert_eq!(e.encode(OnFault::Die, cmd), Ok(Encoded::REFUSED)); + } + assert_eq!(p.call_log().count(FILL), 3, "every fill sent"); + } } diff --git a/crates/reims-vgpu-vulkan/src/core/testkit/impls/resource.rs b/crates/reims-vgpu-vulkan/src/core/testkit/impls/resource.rs index d7b991767..f7e544136 100644 --- a/crates/reims-vgpu-vulkan/src/core/testkit/impls/resource.rs +++ b/crates/reims-vgpu-vulkan/src/core/testkit/impls/resource.rs @@ -46,6 +46,11 @@ impl Fk { _ => T::default(), } } + + /// Its length; `None` for a buffer a test made with no model, whose length nothing checks. + pub(super) fn modelled_length(&self) -> Option { + self.buffer(|m| Some(m.length)) + } } impl Fk { @@ -92,6 +97,34 @@ impl Fk { } } +/// The extent of level `level` of a texture of `d`: its width, height and depth halved `level` +/// times, each at least 1. +pub(super) fn level_extent(d: &TexDesc, level: u64) -> [u64; 3] { + let shift = u32::try_from(level).unwrap_or(u32::MAX); + [d.width, d.height, d.depth].map(|e| e.checked_shr(shift).unwrap_or(0).max(1)) +} + +/// The Vulkan platform's texel block of `pixel_format` for copies and textures over a buffer, +/// from metal2vulkan's format tables as `rt_platform::blit` sizes one: its bytes (a colour +/// format's Vulkan format's block, 1 for `Stencil8`, 2 for `Depth16Unorm`, 4 for +/// `Depth32Float`), its extent in texels, and whether it is a colour format's. `None` for any +/// other format, whose bytes the fake does not size, and which nothing checks. +pub(super) fn texel_unit(pixel_format: u64) -> Option<(u64, [u64; 2], bool)> { + use metal2vulkan::metal::{PixelFormat as P, format}; + let pf = P::try_from(u32::try_from(pixel_format).ok()?).ok()?; + let color = format::aspects(pf) == Some(metal2vulkan::vk::ImageAspectFlags::COLOR); + let bytes = match pf { + P::Stencil8 => 1, + P::Depth16Unorm => 2, + P::Depth32Float => 4, + _ if color => format::byte_format(pf)?.block()?.size, + _ => return None, + }; + let [width, height] = format::block_extent(pf).unwrap_or([1, 1]); + let block = [u64::from(width.max(1)), u64::from(height.max(1))]; + Some((u64::from(bytes), block, color)) +} + /// The slices of a texture of `d`, as the Vulkan platform's runtime counts them: each face of a /// cube (`MTLTextureTypeCube`, `CubeArray`) one, a 3D texture's one, an array's each element. pub(super) fn slice_count(d: &TexDesc) -> u64 {