From dd52afe31e93680c1232c22f38c010ecc3a24232 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 18 Nov 2026 04:48:12 -0800 Subject: [PATCH] rt sampler: a constexpr sampler refused past the device's samplers lets go of the table's lock before giving back hold_constexpr matched on `lock(&core.samplers).take(..)`, and the guard, a temporary in the match scrutinee, lived through the arms: the Err arm's give_back locks the same table again, so a refused take (Limit::DeviceSamplers, or a failed vkCreateSampler) locked the thread against itself. The take now has a statement of its own, so the lock is let go before the arms. Found by grepping metal2vulkan-rt for a lock taken in an if-let, while-let or match scrutinee whose body does more than read the guarded value; the other sites' bodies only read or update the guarded value and return. Test: every_device_refuses_a_constexpr_sampler_past_its_samplers caps the device's sampler table at what it holds (a test-only setter), asserts `sample_up`'s constexpr sampler is refused with Limit::DeviceSamplers, then restores the cap and makes the same state. Verification: a negative control with the old match, and the check and device legs with the fix, are the jobs reported with this branch. --- crates/metal2vulkan-rt/src/sampler.rs | 14 +++++- crates/metal2vulkan-rt/src/tests/sampling.rs | 48 +++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/crates/metal2vulkan-rt/src/sampler.rs b/crates/metal2vulkan-rt/src/sampler.rs index c4ed87f4b..8392fedf4 100644 --- a/crates/metal2vulkan-rt/src/sampler.rs +++ b/crates/metal2vulkan-rt/src/sampler.rs @@ -61,6 +61,15 @@ impl SamplerTable { Ok(sampler) } + /// Sets the most samplers the table holds to `cap`, for a test: the samplers it holds, and + /// the cap it had. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) fn set_cap(&mut self, cap: impl FnOnce(u32) -> u32) -> u32 { + let had = self.cap; + self.cap = cap(self.entries.len() as u32); + had + } + /// One holder fewer of `desc`'s sampler: the sampler, for the reaper, where it was the last. pub(crate) fn give(&mut self, desc: &K) -> Option { let (sampler, holders) = self.entries.get_mut(desc)?; @@ -174,7 +183,10 @@ pub(crate) fn hold_constexpr( if present.is_some_and(|present| present[array].is_some()) { continue; } - match lock(&core.samplers).take(desc, || make(core, desc)) { + // Taken in a statement of its own, so the table's lock is let go before the arms: the + // refusal's `give_back` takes it again. + let taken = lock(&core.samplers).take(desc, || make(core, desc)); + match taken { Ok(sampler) => added.push((array, *desc, sampler)), Err(error) => { let taken: Vec> = diff --git a/crates/metal2vulkan-rt/src/tests/sampling.rs b/crates/metal2vulkan-rt/src/tests/sampling.rs index 24248862d..aa7be11ea 100644 --- a/crates/metal2vulkan-rt/src/tests/sampling.rs +++ b/crates/metal2vulkan-rt/src/tests/sampling.rs @@ -8,11 +8,12 @@ #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod on_device { + use crate::device::lock; use crate::tests::command::on_device::{each_device, finished}; use crate::{ ComputePipelineDescriptor, ComputePipelineState, Device, DispatchType, Error, - FunctionDescriptor, IndexClass, Library, NumberedSampler, PixelFormat, Refusal, Region, - SamplerDescriptor, SamplerMinMagFilter, StageSource, Texture, TextureDescriptor, + FunctionDescriptor, IndexClass, Library, Limit, NumberedSampler, PixelFormat, Refusal, + Region, SamplerDescriptor, SamplerMinMagFilter, StageSource, Texture, TextureDescriptor, TextureType, TextureUsage, }; use metal2vulkan_devtools::{fixtures_dir, llvm_assemble, AssembleError}; @@ -300,6 +301,49 @@ mod on_device { }); } + /// A constexpr sampler past the device's samplers is refused, and the refusal lets go of what + /// the state took: the same state is made once the samplers have room. + #[test] + fn every_device_refuses_a_constexpr_sampler_past_its_samplers() { + let Some(library) = library() else { + return; + }; + each_device("sampling", |name, _, device| { + let grant = device + .grant_indices(IndexClass::Sampler, 1, 4) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let cap = lock(&device.core().samplers).set_cap(|held| held); + let constexpr = |s: &[NumberedSampler]| { + matches!(s, [NumberedSampler::Constexpr(_), NumberedSampler::Write]) + }; + let refused = state( + &device, + &library, + "sample_up", + constexpr, + vec![CONSTEXPR, 0], + ) + .err(); + assert!( + matches!(refused, Some(Error::Limit(Limit::DeviceSamplers { .. }))), + "{name}: {refused:?}" + ); + lock(&device.core().samplers).set_cap(|_| cap); + let up = state( + &device, + &library, + "sample_up", + constexpr, + vec![CONSTEXPR, 0], + ) + .unwrap_or_else(|error| panic!("{name}: sample_up: {error}")); + drop(up); + device + .release_indices(grant) + .unwrap_or_else(|error| panic!("{name}: {error}")); + }); + } + /// An image-only kernel that does not observe its threadgroup partition (`sample_down`) runs a /// grid one deep in 16x16x1 workgroups on the one `VkPipeline` its state made, testing its /// edge: over a grid of 20 by 12, ragged in both, a 32x32 destination holds the sample in the