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.
This commit is contained in:
Anees Iqbal committed 2026-11-18 05:33:12 -08:00
1 parent 980a27b80a
commit dd52afe31e
2 files changed
+59 -3

No files matched your search

+13 -1
View File
@@ -61,6 +61,15 @@ impl<K: Clone + Eq + Hash> SamplerTable<K> {
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<vk::Sampler> {
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<Option<SamplerDesc>> =
+46 -2
View File
@@ -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