From c871b2f328e23acf10d98a3f8d99defe193021f6 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 21:20:05 -0800 Subject: [PATCH] Name an image shadow's truth as each copy is recorded, so a storage form's swap leaves no shadow on the old image On the M3 (MoltenVK), tests::self_read_storage::a_fetch_after_a_pinned_reader_renders_through_a_stand_in_from_the_fetch_on died with SIGSEGV in its second pass, in MVKCmdCopyImage::validate under vkCmdCopyImage (m3-max-6939, 6950: shadow.rs ShadowCopy::record_gated <- EncoderCore::held_shadow_copies <- render/inline_splits.rs record_kept <- draw). The first pass's reader samples the target while the pass renders it, so its slot binding takes the texture's usage shadow (texture/attached_reads.rs, texture/usage_shadow.rs), whose ShadowSource::Image held the image's VkImage as it was when the shadow was made. That pass's fetch notes the target for its storage form, and its commit swapped the image's VkImage for one with STORAGE and destroyed the first as it completed (texture/storage_form.rs). The second pass's copy into the kept shadow then named the destroyed image. ShadowSource::Image now holds the image itself (shadow::ImageTruth, which texture::Image takes as its own_raw) and each copy names its VkImage as it is recorded. Every image shadow made over a texture's image goes through texture/view_shadow.rs (a usage shadow, a reinterpreted view, a view's render shadow), so none keeps the old image after a swap. The swap happens at the commit of the device's only uncommitted command buffer, so no recording names either image across it; the regions, formats and identity are the same before and after (the storage form differs only in STORAGE usage). The storage form docs say so. Lavapipe and the Iris did not show it: there the reader takes the snapshot path and binds no usage shadow. A device whose reader splits for want of STORAGE (be1f1c141b notes the form there) binds the shadow and then takes the swap, so the same use after free is expected there too (unaudited: only the M3 run shows it). Verified: on the M3 at df733261f6, m3-max-7009: both self_read_storage tests passed 3 of 3, the fetch test through both passes (pass 1 [1, 0, 0, 0, 0]); 53 of 53 tests filtered by shadow, usage and view shadows, storage forms, attached reads, fetch, snapshot, self-read, stand-in and reinterpretation passed. Negative control: 1a5e185801 alone crashed in pass 1 in 4 of 4 runs (m3-max-6934, 6939). Lavapipe and clippy --cfg m2v_cpu_icd: gem10 m3-max-7004, follows. (cherry picked from commit df733261f6) --- crates/metal2vulkan-runtime/src/shadow.rs | 17 ++++++++++++++--- crates/metal2vulkan-runtime/src/tests/shadow.rs | 15 ++++++++++++--- crates/metal2vulkan-runtime/src/texture.rs | 8 ++++++++ .../src/texture/storage_form.rs | 5 ++++- .../src/texture/view_shadow.rs | 4 ++-- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs index 67051f481..7aaa8e623 100644 --- a/crates/metal2vulkan-runtime/src/shadow.rs +++ b/crates/metal2vulkan-runtime/src/shadow.rs @@ -333,6 +333,16 @@ impl Backing { } } +/// The image an image's shadow holds a copy of ([`ShadowSource::Image`]). Its copies name the +/// image's `VkImage` as each is recorded, not as the shadow was made: a texture's storage form +/// swaps its image's `VkImage` for one with `STORAGE` and destroys the first as the swapping +/// command buffer completes (`texture/storage_form.rs`), and a shadow made before the swap (a +/// slot binding's usage shadow, a view's) copies the image the texture holds from then on. +pub(super) trait ImageTruth: Send + Sync { + /// The image's `VkImage` now. + fn raw(&self) -> vk::Image; +} + /// A shadow's truth: what the shadow holds a copy of between an encoder's copies (the module's /// rules, with the truth in the buffer's place). pub(super) enum ShadowSource { @@ -356,7 +366,8 @@ pub(super) enum ShadowSource { /// view presents between the two, the same subresources, texel for texel (formats of one copy /// unit, whose copies keep every bit). Image { - image: vk::Image, + /// The image, whose `VkImage` each copy names as it is recorded ([`ImageTruth`]). + image: Arc, /// The image's identity. key: ResourceKey, /// The image's levels and layers the view presents. @@ -1910,9 +1921,9 @@ impl ShadowCopy { .. } => { let (from, to) = if into_shadow { - (*image, *shadow) + (image.raw(), *shadow) } else { - (*shadow, *image) + (*shadow, image.raw()) }; if let Some(through) = through { let (buffer, _) = through.buffer.at(0); diff --git a/crates/metal2vulkan-runtime/src/tests/shadow.rs b/crates/metal2vulkan-runtime/src/tests/shadow.rs index 5ba5fbbb6..42e52133d 100644 --- a/crates/metal2vulkan-runtime/src/tests/shadow.rs +++ b/crates/metal2vulkan-runtime/src/tests/shadow.rs @@ -5,8 +5,8 @@ use super::super::host_aliases::{self, Alias}; use super::super::key_hash::KeyMap; use super::super::resource::ResourceKey; use super::super::shadow::{ - self, Backing, CopyCause, KeptArea, Lost, ShadowBuffer, ShadowCarry, ShadowCopies, ShadowCopy, - ShadowSource, ShadowSync, ShadowTarget, ShadowUse, Step, Within, + self, Backing, CopyCause, ImageTruth, KeptArea, Lost, ShadowBuffer, ShadowCarry, ShadowCopies, + ShadowCopy, ShadowSource, ShadowSync, ShadowTarget, ShadowUse, Step, Within, }; use super::super::shadow_merge::ScratchSpares; use super::super::tracker::{Access, Use, UseRange}; @@ -1431,6 +1431,15 @@ fn a_refused_scratch_changes_nothing() { ); } +/// An image no copy is recorded with: these tests track uses alone. +struct NoImage; + +impl ImageTruth for NoImage { + fn raw(&self) -> vk::Image { + vk::Image::null() + } +} + /// A shadow of `levels` and `layers` of the image `image`: a texture view of another component /// layout on a device whose Vulkan views reinterpret none (`texture/view_shadow.rs`). fn view_of(image: ResourceKey, levels: Range, layers: Range) -> Arc { @@ -1441,7 +1450,7 @@ fn view_of(image: ResourceKey, levels: Range, layers: Range) -> Arc vk::Image { + self.own_raw() + } +} + impl Drop for Image { fn drop(&mut self) { // Once the process is exiting, the image is left to its end (`exit_guard`). diff --git a/crates/metal2vulkan-runtime/src/texture/storage_form.rs b/crates/metal2vulkan-runtime/src/texture/storage_form.rs index 938960e33..4bc71c169 100644 --- a/crates/metal2vulkan-runtime/src/texture/storage_form.rs +++ b/crates/metal2vulkan-runtime/src/texture/storage_form.rs @@ -84,7 +84,10 @@ //! encoders name the image the texture has then, their uses after the copy in hazard //! tracking. It waits at most [`SWITCH_WAIT`], and not at all on the scheduler's thread (a //! submission check that makes a command buffer): past that, it gives the switch up, so -//! nothing swaps, and goes on with the old image. +//! nothing swaps, and goes on with the old image. A shadow of the image made before the swap +//! (a slot binding's usage shadow, as a draw reading its own pass's color attachment binds, +//! or a view's) names the image's `VkImage` as each of its copies is recorded +//! (`shadow::ImageTruth`), so its later copies take the image with `STORAGE`. //! - **Once per texture,** by construction: the storage form is a `OnceLock` of the image, which //! is noted at most once. A switch the device refuses (the image's limits, memory) leaves the //! texture its scratch, for good; a commit that fails or is given up leaves it waiting. diff --git a/crates/metal2vulkan-runtime/src/texture/view_shadow.rs b/crates/metal2vulkan-runtime/src/texture/view_shadow.rs index 2e174d8fe..94aa48f08 100644 --- a/crates/metal2vulkan-runtime/src/texture/view_shadow.rs +++ b/crates/metal2vulkan-runtime/src/texture/view_shadow.rs @@ -61,7 +61,7 @@ use crate::image_plan::plan_image; use crate::image_plan::{copies_between, takes_views_of}; use crate::memory::Placement; use crate::resource::{Identity, ResourceKey}; -use crate::shadow::{Backing, ImageThrough, ShadowCopies, ShadowSource}; +use crate::shadow::{Backing, ImageThrough, ImageTruth, ShadowCopies, ShadowSource}; use crate::shadow_merge::ScratchSpares; use crate::texture::{copy_units, image_error, texture_aspects}; use crate::texture_descriptor::{TextureDescriptor, TextureLayout}; @@ -469,7 +469,7 @@ impl Device { tracked: base.identity.tracked, }; let (truth, truth_key, truth_range) = ( - base.own_raw(), + Arc::clone(base) as Arc, base.identity.key, range(texture_aspects(base.plan())), );