From 3ab03545a0b7edbddc79fdbdfeb933aefa596bdb Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 3 Dec 2026 01:55:19 -0800 Subject: [PATCH] :bug: Correctness fix: a widened shape keeps the fill it owes A shape in place over a surface's pages owes a fill from the cache's buffer once a write through another shape of its plane reaches the buffer, which then holds bytes the pages lack. A texture made over such a shape with a usage its image in place lacks moves it onto an image of the platform's own (a widen), and the widen copied the image in place into the new image and promoted it as copied, which cleared the fill the shape owed: the new image held the pages' older bytes, and the shape's textures read texels without that write until another write covered them, with nothing refused or counted. A widen of a shape owing a fill now takes no copy of its image in place and leaves the new image owing the fill, which the shape's next use makes from the buffer. For the guest: after a write through one texture of a surface's plane, a texture of another shape of that plane, in place and then widened by a texture of a new usage, reads the write. Shapes lie in place on hosts of one memory (the M3, Iris). Found by reading (audit Z1); the fake platform's test writes through the plane's storage shape, widens its in-place shape, and finds the next use filling the moved image from the buffer, with no copy of the image in place. --- .../src/graphics/decode/surface_cache.rs | 25 +++++---- .../graphics/decode/surface_cache/shapes.rs | 12 +++-- .../src/graphics/decode/surface_shapes.rs | 52 +++++++++++++++++++ 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs index 6526b0ac1..e0f13ab74 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache.rs @@ -6003,10 +6003,12 @@ impl SurfaceCaches

{ /// Moves the in-place shape `id` of the block at `block`'s cache to `image`, of its kind's /// usage, the platform's own (the module doc of [`shapes`], "In place"), the cache idle /// ([`Self::promotion_idle`]): `image` added as a shape of the same key, the in-place image - /// copied into it on the transfer queue, which every later use waits for, the in-place - /// image's views made `image`'s ([`Texture::retarget_views`]), and the in-place shape - /// promoted into the new one, which takes its references and holds its image until no texture - /// made over it before now lives ([`ShapeTable::promote`]), counted + /// copied into it on the transfer queue, which every later use waits for, where the shape owes + /// no fill from the buffer (one that owes one holds bytes the in-place image lacks, so the new + /// shape owes that fill instead, which the next use makes), the in-place image's views made + /// `image`'s ([`Texture::retarget_views`]), and the in-place shape promoted into the new one, + /// which takes its references and holds its image until no texture made over it before now + /// lives ([`ShapeTable::promote`]), counted /// ([`shapes::MOVED_WIDENED`]). The new shape and its image; `image` back where the cache /// holds no such shape, or is not idle, or the platform refused the copy or the retarget, for /// the caller to let go of with the lock let go of. @@ -6033,14 +6035,17 @@ impl SurfaceCaches

{ else { return Err(image); }; - let (key, from) = (shape.key, shape.image.clone_ref()); + let (key, from, owed) = (shape.key, shape.image.clone_ref(), shape.owed); let onto = image.clone_ref(); let (reference, _) = cache.shapes.insert(key, image, true, now); let new = reference.id(); - let copied = matches!( - self.copy_promoted(device, block, (&from, &onto), &key), - Ok(true) - ); + // A shape that owes a fill from the buffer holds bytes its in-place image lacks: the new + // shape owes that fill too, and takes no copy of the image. + let copied = owed + || matches!( + self.copy_promoted(device, block, (&from, &onto), &key), + Ok(true) + ); if !copied || from.retarget_views(&onto).is_none() { drop(reference); let taken = (self.by_block.get_mut(&block)).and_then(|c| c.shapes.take_id(new)); @@ -6052,7 +6057,7 @@ impl SurfaceCaches

{ let Some(cache) = self.by_block.get_mut(&block) else { return Err(onto); }; - cache.shapes.promote(id, new, true); + cache.shapes.promote(id, new, !owed); self.hold_bytes(key.bytes()); self.counts.shapes.moved_widened += 1; reims_vgpu_vulkan::answer::count_kind(shapes::MOVED_WIDENED); diff --git a/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs b/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs index a98475d00..f5e781d73 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_cache/shapes.rs @@ -207,11 +207,13 @@ //! the platform's own (on KosmicKrisp, no render target): a later texture of the shape whose //! usage, or format outside the image's own and its sRGB sibling, the image does not take moves //! the shape to an image of its kind's usage, the platform's own, its views moved there as at a -//! promotion ("Promotion"), counted ([`MOVED_WIDENED`]); where a command buffer begun and not -//! finished uses the cache, the texture lies over the buffer instead, counted -//! ([`WIDENED_UNSHAPED`]). A shape is made in place only where its image would hold all the cache -//! holds and nothing would race the guest's CPU over the pages: the cache keeps the pages' layout, -//! with no wide home, no carried pages, no texture over its buffer and no video of the host's; no +//! promotion ("Promotion"), the in-place image copied into it where the shape owes no fill from +//! the buffer, and the fill owed by the new image where it does, counted ([`MOVED_WIDENED`]); +//! where a command buffer begun and not finished uses the cache, the texture lies over the +//! buffer instead, counted ([`WIDENED_UNSHAPED`]). A shape is made in place only where its image +//! would hold all the cache holds and nothing would race the guest's CPU over the pages: the +//! cache keeps the pages' layout, with no wide home, no carried pages, no texture over its buffer +//! and no video of the host's; no //! shape holds a write the pages lack and the buffer holds no byte they lack; and the surface's //! CPU generation is settled, either never moved by a read-write lock (0) or seen unlocked by a //! use's `RGFX_SURFACE_USE_REFILL` at that generation (`SurfaceCaches`'s `settle`). Anywhere else, diff --git a/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs b/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs index 8147e8559..1890b77a0 100644 --- a/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs +++ b/crates/reims-vgpu/src/graphics/decode/surface_shapes.rs @@ -6102,6 +6102,58 @@ mod tests { assert_eq!(made.lock().len(), 1, "no second make in place"); } + /// A shape in place that owes a fill from the buffer, a write the buffer takes that its + /// in-place image lacks (here an unseen write through the block's one storage shape, of + /// another extent of the plane), moved to an image of its kind's usage by a texture its + /// in-place image does not take, takes no copy of that image: the new image owes the fill, + /// which the next use makes from the buffer once the write is in it, so a texture over the + /// shape reads the write (`shapes`, "In place") + #[test] + fn a_shape_owing_a_fill_moved_off_its_in_place_image_still_owes_it() { + let mut r = shaped_staged(); + let cache = cache_id(&r); + let made = in_place_of(&r, None, |_, desc| { + (desc.width == 32).then(|| InPlaceRefused::of("pitch")) + }); + let plain = texture_as_image(&mut r, 21, 80, (16, 8), 0x1); + let storage = texture_as_image(&mut r, 25, 80, (32, 8), 0x2); + assert_eq!(made.lock().len(), 2, "both asked"); + assert_eq!( + r.dec.caches().counts().shapes.in_place, + 1, + "the plain shape in place" + ); + use_surfaces(&mut r, 1, 0x100, &[(BLOCK, 0, false)]); + complete_staged(&r); + writes_unseen(&mut r); + assert_eq!( + r.dec.caches().counts().shapes.unseen_taken, + 1, + "the storage shape's" + ); + + let mark = r.p.call_log().mark(); + let moved = texture_as_image(&mut r, 26, 80, (16, 8), 0x5); + let calls = r.p.call_log().since(mark); + assert_ne!(moved, plain, "a new image"); + assert_eq!(r.dec.caches().counts().shapes.moved_widened, 1); + assert!( + texture_copies(&calls).is_empty(), + "no copy of the image that lacks the write" + ); + + let mark = r.p.call_log().mark(); + use_surfaces(&mut r, 1, 0x101, &[(BLOCK, 0, false)]); + let calls = r.p.call_log().since(mark); + assert_eq!(written_back(&calls), [(storage, cache, 0)], "the write"); + let filled = fills(&calls); + assert!( + filled.contains(&(cache, 0, moved)), + "into the moved shape: {filled:?}" + ); + complete_staged(&r); + } + /// [`shaped_staged`]'s rig over a deferred block of two pages (`surface_cache`'s module doc, /// "Deferred blocks"), a surface the CPU has not locked: its pages, whose bytes are set (from /// page 1), are not named until [`attach_pages`].