🐛 A compute kernel that samples a texture the guest never bound left a hole in the layout, and one driver divides by it
Pathway: x86 macOS / Linux Vulkan, compute dispatch. Reported from a user's host: QEMU killed by SIGFPE (rc=136) on an Intel Iris Xe / Mesa 26.1.5 guest session, the fault inside vkCreateComputePipelines on the drain thread, under `ObjectCaches::get_or_create_compute_pipeline`. WHAT IS WRONG Vulkan requires the pipeline layout to contain a descriptor for every resource the module *statically uses*. The descriptor set layout this device builds is assembled from what the guest bound (`exec_compute.rs`'s `LayoutKey`), so a texture the kernel samples and the guest left empty is absent from the layout entirely — not an unwritten slot in it. `kb/frag-descriptor-unbound-is-a-real- violation.md` established that population on the *draw* path and named the repair; this is the same defect on the compute path, and it is not merely undefined. Mesa's ANV sizes its own binding array to `max_binding + 1`, zero-fills every number the layout did not declare, and then scores each used binding as `(use_count << 7) / binding->array_size` when it picks binding-table slots (`anv_nir_apply_pipeline_layout.c`, `anv_CreateDescriptorSetLayout`). A hole under a used binding divides by zero. `vkCreateComputePipelines` therefore does not return an error — the host process dies inside it and takes the VM with it. There is no validation layer in this tree and no status to inspect, so nothing downstream could have reported this. WHAT CHANGED - `spirv_bind::sampled_image_bindings` — the texture analogue of the sampler walk that already existed. Class from the SPIR-V type, never the binding number: `OpTypeImage` with `Sampled` 1 and `Dim` not `SubpassData`. - `spirv_bind::declared_binding_numbers` — the class-blind candidate sweep the backstop asks `descriptor_static_use` about. - `runtime::compute_exec` provisions a neutral 1x1 transparent texture for every sampled-image binding the module statically uses and the guest left empty, mirroring the neutral-sampler pass a few lines below it. Reported on the fail channel as `compute_neutral_sampled_image_unbound`, because it is a repair that succeeded and the reliance has to stay measurable. - `exec_compute::used_binding_absent_from_layout` refuses the dispatch if any used binding is still absent when the layout is built, as `ComputeExecutionDecline::UsedBindingAbsentFromLayout`. Expected to stay at zero; it is the backstop for a class the repair above does not cover, and losing one dispatch is the only outcome left that keeps the VM alive. Only `Used` is provisioned. `DeclaredUnused` is legal to omit and must stay omitted or the census that separated the two populations cannot tell them apart; `Ambiguous` is two variables on one binding, which is its own defect and is not repaired by picking one. `descriptor_static_use` answers `NotDeclared` for anything that is not a `UniformConstant` descriptor, so a storage buffer — whose root that walk cannot resolve — is never refused on a guess. VERIFIED - 1878 tests pass, `--test-threads=1`, backend-vulkan,host-window. Three new: the two walks partition set 0; the class-blind sweep vs the use test; the neutral pass fills the used binding and not the declared-unused one; the backstop refuses by number and admits the legal layout. - Probed: stubbing the `is_violation` filter out of the neutral pass fails the test, so it discriminates rather than merely running. - Clippy `-D warnings` clean on all three arms (backend-vulkan+host-window, the same with an explicit x86_64-unknown-linux-gnu target, and backend-metal under --target aarch64-apple-darwin). The three new items are `backend-vulkan`-gated; the Metal arm found that and it is now honest. - The reflection was checked against the actual reporting shader by an independent parser, not against itself: it declares set 0 bindings 1-10 (storage buffers), 32 (storage image), 33-38 (sampled images) and 160 (sampler), and every one of them is statically used. The reporting host's own log shows the guest wrote 3 of the 6 sampled images, and the fault operand resolves to `binding[33].array_size` — the first used binding with no layout entry, which is exactly this defect. NOT VERIFIED - No live boot. This is a Linux/Vulkan host-side layout fix; the machine that reproduces it is an Intel iGPU and this dev host has no Intel GPU, so the SIGFPE itself cannot be re-triggered here. The repair is verified by test and by the driver's own published source, not by a boot. - Whether the guest's kernel depended on the contents of the textures it did not bind. The neutral texture is transparent and 1x1; a kernel that queries its size gets 1x1. The fail line is what makes that reliance findable later. - The draw path carries the same defect (2-3 firings a boot on rails 14, 15 and 26 per the kb note) and is NOT fixed here. - No throughput measurement. The neutral images are 4 bytes each and only materialize when the guest under-binds. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
4d4e79b2b9
commit
25051457a3
5 files changed
+517
No files matched your search
@@ -74,6 +74,23 @@ pub enum ComputeExecutionDecline {
|
||||
wanted_height: u32,
|
||||
wanted_format: StorageImageFormat,
|
||||
},
|
||||
/// The module statically uses a `set = 0` binding the descriptor set layout
|
||||
/// this dispatch built does not contain.
|
||||
///
|
||||
/// A specification violation — Vulkan requires the pipeline layout to
|
||||
/// describe every resource the shader statically uses — and the one layout
|
||||
/// defect that cannot be reported from any later point. Mesa's Intel driver
|
||||
/// scores each used binding as `(use_count << 7) / array_size` over an array
|
||||
/// it sized to `max_binding + 1` and zero-filled, so the absent binding
|
||||
/// divides by zero and `vkCreateComputePipelines` kills the process with
|
||||
/// `SIGFPE` instead of returning. Refusing one dispatch is the only outcome
|
||||
/// left that keeps the VM alive and says why.
|
||||
///
|
||||
/// Expected to stay at zero: `runtime::compute_exec` provisions a neutral
|
||||
/// sampler and a neutral sampled image for every binding of those classes
|
||||
/// the guest left empty, so a firing is a class those passes do not cover
|
||||
/// and is worth reading as a real gap.
|
||||
UsedBindingAbsentFromLayout { binding: u32 },
|
||||
}
|
||||
|
||||
impl Decline for ComputeExecutionDecline {
|
||||
@@ -98,6 +115,9 @@ impl Decline for ComputeExecutionDecline {
|
||||
Self::ResidentRekeyWouldDropPinned { .. } => {
|
||||
"vk_compute_exec_resident_rekey_would_drop_pinned"
|
||||
}
|
||||
Self::UsedBindingAbsentFromLayout { .. } => {
|
||||
"vk_compute_exec_used_binding_absent_from_layout"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,6 +226,9 @@ impl Decline for ComputeExecutionDecline {
|
||||
]);
|
||||
fields
|
||||
}
|
||||
Self::UsedBindingAbsentFromLayout { binding } => {
|
||||
vec![("binding", binding.to_string())]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,6 +209,41 @@ fn resident_sample_exact(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The lowest `set = 0` binding the module statically uses and `layout` does not
|
||||
/// describe, if there is one.
|
||||
///
|
||||
/// This is the one descriptor-layout defect that cannot be reported from any
|
||||
/// later point, which is why it is checked before the layout is built rather
|
||||
/// than inferred from a result code afterwards. Vulkan requires the pipeline
|
||||
/// layout to contain a descriptor for every resource the shader statically uses.
|
||||
/// Mesa's Intel driver sizes its own binding array to `max_binding + 1`,
|
||||
/// zero-fills every number the layout did not declare, and then scores each used
|
||||
/// binding as `(use_count << 7) / array_size` when it picks binding-table slots.
|
||||
/// A binding the module uses and the layout omits therefore divides by zero:
|
||||
/// `vkCreateComputePipelines` does not return an error, the host process dies of
|
||||
/// `SIGFPE` inside it, and the VM goes with it. There is no validation layer in
|
||||
/// this tree and no status to inspect, so refusing the dispatch here is the only
|
||||
/// outcome left that keeps the device alive and says why.
|
||||
///
|
||||
/// **Expected to return `None` always.** The two repairable classes are filled
|
||||
/// before the request is built — `runtime::compute_exec` provisions a neutral
|
||||
/// sampler and a neutral sampled image for every binding of those classes the
|
||||
/// guest left empty — so this is the backstop for a class those passes do not
|
||||
/// cover, and a firing names a real gap rather than noise.
|
||||
///
|
||||
/// [`crate::runtime::spirv_bind::descriptor_static_use`] answers `NotDeclared`
|
||||
/// for anything that is not a `UniformConstant` descriptor, so a storage buffer,
|
||||
/// whose root this walk cannot resolve, is never refused on a guess.
|
||||
fn used_binding_absent_from_layout(spirv: &[u32], layout: &[BindingSig]) -> Option<u32> {
|
||||
crate::runtime::spirv_bind::declared_binding_numbers(spirv)
|
||||
.into_iter()
|
||||
.find(|binding| {
|
||||
!layout.iter().any(|b| b.binding == *binding)
|
||||
&& crate::runtime::spirv_bind::descriptor_static_use(spirv, *binding)
|
||||
.is_violation()
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn execute_compute_inner(
|
||||
owner: &mut ContextOwner,
|
||||
caches: &mut ObjectCaches,
|
||||
@@ -264,6 +299,13 @@ pub(crate) unsafe fn execute_compute_inner(
|
||||
});
|
||||
}
|
||||
layout_bindings.sort_by_key(|b| b.binding);
|
||||
|
||||
if let Some(binding) = used_binding_absent_from_layout(&req.spirv, &layout_bindings) {
|
||||
return Err(DrawError::ComputeExecution(
|
||||
ComputeExecutionDecline::UsedBindingAbsentFromLayout { binding },
|
||||
));
|
||||
}
|
||||
|
||||
let layout_key = LayoutKey {
|
||||
bindings: layout_bindings,
|
||||
};
|
||||
@@ -985,6 +1027,35 @@ mod tests {
|
||||
use crate::model::ComputeStorageResidencyKey;
|
||||
use crate::observe::Decline;
|
||||
|
||||
/// A dispatch whose layout omits a binding its module samples is refused by
|
||||
/// number, and one that only omits an unreferenced binding is not.
|
||||
///
|
||||
/// This is the shape that killed a host: Mesa's Intel driver divides
|
||||
/// `(use_count << 7)` by the absent binding's `array_size`, which its own
|
||||
/// zero-fill made zero, so `vkCreateComputePipelines` raises `SIGFPE`
|
||||
/// instead of returning. Refusing one dispatch is the only outcome that
|
||||
/// keeps the VM alive, and the second half of this test is what keeps the
|
||||
/// refusal from swallowing legal dispatches with it.
|
||||
#[test]
|
||||
fn a_used_binding_the_layout_omits_is_refused_and_a_declared_unused_one_is_not() {
|
||||
let spirv = crate::runtime::spirv_bind::test_module_with_two_sampled_images(33, 34);
|
||||
let sig = |binding| BindingSig {
|
||||
binding,
|
||||
ty: vk::DescriptorType::SAMPLED_IMAGE.as_raw() as u32,
|
||||
stages: vk::ShaderStageFlags::COMPUTE.as_raw(),
|
||||
};
|
||||
|
||||
assert_eq!(used_binding_absent_from_layout(&spirv, &[]), Some(33));
|
||||
assert_eq!(used_binding_absent_from_layout(&spirv, &[sig(34)]), Some(33));
|
||||
// Covering the used binding is sufficient: 34 is declared and never
|
||||
// referenced, so leaving it out is legal and must not be reported.
|
||||
assert_eq!(used_binding_absent_from_layout(&spirv, &[sig(33)]), None);
|
||||
assert_eq!(
|
||||
used_binding_absent_from_layout(&spirv, &[sig(33), sig(34)]),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
fn residency_identity() -> ComputeStorageResidencyKey {
|
||||
ComputeStorageResidencyKey {
|
||||
mapping_id: 7,
|
||||
|
||||
@@ -185,6 +185,79 @@ impl crate::observe::Decline for ComputeBindOverflow {
|
||||
}
|
||||
}
|
||||
|
||||
/// The sampled-image bindings that need a neutral texture: those the module
|
||||
/// statically uses and `bound` does not cover.
|
||||
///
|
||||
/// Vulkan requires the pipeline layout to contain a descriptor for every
|
||||
/// resource the module statically uses, and the layout this device builds is
|
||||
/// assembled from what the guest bound — so a texture the kernel samples and the
|
||||
/// guest left empty is absent from the layout entirely, not an unwritten slot in
|
||||
/// it. Besides being undefined by the specification, that hole is fatal on one
|
||||
/// of the two iGPU vendors this device supports: Mesa's Intel driver scores each
|
||||
/// used binding as `(use_count << 7) / array_size` over an array it sized to
|
||||
/// `max_binding + 1` and zero-filled, so it divides by zero and the host process
|
||||
/// dies of `SIGFPE` inside `vkCreateComputePipelines`.
|
||||
///
|
||||
/// Only [`DescriptorUse::Used`] is returned, which is the bar the specification
|
||||
/// actually sets. A declared-and-never-referenced variable is legal to omit and
|
||||
/// must stay omitted, or the census that separated those two populations cannot
|
||||
/// tell them apart any more. `Ambiguous` — two variables on one binding — is its
|
||||
/// own defect and is not repaired by picking one of them.
|
||||
#[cfg(feature = "backend-vulkan")]
|
||||
fn neutral_sampled_image_bindings(spirv: &[u32], bound: &[u32]) -> Vec<u32> {
|
||||
crate::runtime::spirv_bind::sampled_image_bindings(spirv)
|
||||
.into_iter()
|
||||
.filter(|binding| {
|
||||
!bound.contains(binding)
|
||||
&& crate::runtime::spirv_bind::descriptor_static_use(spirv, *binding).is_violation()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Side length of the texture substituted for a sampled image the kernel
|
||||
/// samples and the guest never bound.
|
||||
///
|
||||
/// One texel, because there is nothing to derive a size from: the guest supplied
|
||||
/// no texture, and any larger extent would be a number chosen to look plausible.
|
||||
/// A kernel that asks this image its size gets 1×1 and that is reported, rather
|
||||
/// than a guess that reads as data.
|
||||
#[cfg(feature = "backend-vulkan")]
|
||||
const NEUTRAL_SAMPLED_IMAGE_EXTENT: u32 = 1;
|
||||
|
||||
/// A sampled image the kernel statically uses and the guest never bound, given a
|
||||
/// neutral transparent texture so the pipeline layout can describe it.
|
||||
///
|
||||
/// **A repair that succeeded, not a success**, which is why it goes to the fail
|
||||
/// channel: the kernel samples a texture whose contents this device invented, and
|
||||
/// the reliance has to stay measurable so a later session can find out whether
|
||||
/// the guest ever depended on what was in it. Nothing here claims the read did
|
||||
/// not matter.
|
||||
///
|
||||
/// Omitting the binding instead is not the cheaper option. It is a specification
|
||||
/// violation, and on Mesa's Intel driver it is a `SIGFPE` that kills the host
|
||||
/// process — see the walk in [`crate::runtime::spirv_bind::sampled_image_bindings`].
|
||||
#[cfg(feature = "backend-vulkan")]
|
||||
struct NeutralSampledImage {
|
||||
binding: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
#[cfg(feature = "backend-vulkan")]
|
||||
impl crate::observe::Decline for NeutralSampledImage {
|
||||
fn slug(&self) -> &'static str {
|
||||
"compute_neutral_sampled_image_unbound"
|
||||
}
|
||||
|
||||
fn fields(&self) -> Vec<(&'static str, String)> {
|
||||
vec![
|
||||
("binding", self.binding.to_string()),
|
||||
("width", self.width.to_string()),
|
||||
("height", self.height.to_string()),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ComputeBufferBind {
|
||||
pub index: u32,
|
||||
@@ -3768,6 +3841,49 @@ fn execute_dispatch_linux<M: HostMemory + HostOps>(
|
||||
}
|
||||
}
|
||||
|
||||
// Vulkan requires the pipeline layout to contain a descriptor for every
|
||||
// resource the module *statically uses*. The layout this device builds is
|
||||
// assembled from what the guest bound, so a texture the kernel samples and
|
||||
// the guest left empty is absent from the layout entirely — not an unwritten
|
||||
// slot in it. That is undefined behaviour by the specification and it is
|
||||
// worse than that in practice: Mesa's Intel driver sizes its binding array
|
||||
// to `max_binding + 1`, zero-fills every number nothing declared, and scores
|
||||
// each used binding as `(use_count << 7) / array_size` when it picks
|
||||
// binding-table slots. A hole under a used binding divides by zero, so the
|
||||
// whole process dies of `SIGFPE` inside `vkCreateComputePipelines` with no
|
||||
// error for this device to decline on. Fill it the way the sampler class
|
||||
// below already fills its own.
|
||||
//
|
||||
// Only `Used` is filled. A declared-and-unused variable is legal to omit and
|
||||
// must stay omitted, or the census that separated those two populations
|
||||
// cannot tell them apart any more; `Ambiguous` is two variables on one
|
||||
// binding, which is its own defect and is not repaired by picking one.
|
||||
let bound: Vec<u32> = sampled_images.iter().map(|img| img.binding).collect();
|
||||
for binding in neutral_sampled_image_bindings(&spirv, &bound) {
|
||||
crate::observe::Emit::decline(
|
||||
"compute_linux_sampled",
|
||||
&NeutralSampledImage {
|
||||
binding,
|
||||
width: NEUTRAL_SAMPLED_IMAGE_EXTENT,
|
||||
height: NEUTRAL_SAMPLED_IMAGE_EXTENT,
|
||||
},
|
||||
)
|
||||
.field("pipe", acc.pipeline_ref)
|
||||
.fail_once((u64::from(acc.pipeline_ref) << 32) | u64::from(binding));
|
||||
sampled_images.push(ComputeSampledImageResource {
|
||||
binding,
|
||||
format: crate::backend::vulkan::engine::StorageImageFormat::Rgba8Unorm,
|
||||
width: NEUTRAL_SAMPLED_IMAGE_EXTENT,
|
||||
height: NEUTRAL_SAMPLED_IMAGE_EXTENT,
|
||||
bytes: pixel_format::solid_rgba8(
|
||||
NEUTRAL_SAMPLED_IMAGE_EXTENT,
|
||||
NEUTRAL_SAMPLED_IMAGE_EXTENT,
|
||||
&[0.0; 4],
|
||||
),
|
||||
resident_bind: None,
|
||||
});
|
||||
}
|
||||
|
||||
let mut samplers = Vec::new();
|
||||
for s in &acc.samplers {
|
||||
let binding = crate::runtime::spirv_bind::SAMPLER_BINDING_BASE + s.index;
|
||||
|
||||
@@ -2456,3 +2456,38 @@ fn a_bind_past_the_argument_table_refuses_the_dispatch() {
|
||||
"clearing an unrelated slot leaves the refused one refused"
|
||||
);
|
||||
}
|
||||
|
||||
/// A texture the kernel samples and the guest never bound gets a neutral image,
|
||||
/// and one the kernel merely declares does not.
|
||||
///
|
||||
/// This is the repair for the hole that killed a host. The descriptor set layout
|
||||
/// this device builds is assembled from what the guest bound, so a sampled image
|
||||
/// the guest left empty is absent from the layout entirely — and Mesa's Intel
|
||||
/// driver scores each *used* binding as `(use_count << 7) / array_size` over an
|
||||
/// array it sized to `max_binding + 1` and zero-filled, so the hole divides by
|
||||
/// zero and `vkCreateComputePipelines` raises `SIGFPE` rather than returning an
|
||||
/// error this device could decline on.
|
||||
///
|
||||
/// The negative half is not decoration. Provisioning for a declared-and-unused
|
||||
/// variable is legal but pays a descriptor for nothing, and it would destroy the
|
||||
/// census that separated the two populations in the first place — so a pass that
|
||||
/// filled both would look identical to this one on a boot and be wrong.
|
||||
#[cfg(feature = "backend-vulkan")]
|
||||
#[test]
|
||||
fn a_sampled_image_the_kernel_uses_and_the_guest_left_empty_gets_a_neutral_texture() {
|
||||
let spirv = crate::runtime::spirv_bind::test_module_with_two_sampled_images(33, 34);
|
||||
|
||||
// Nothing bound: 33 is sampled and needs the neutral texture; 34 is declared
|
||||
// and never referenced, so it stays out of the layout.
|
||||
assert_eq!(neutral_sampled_image_bindings(&spirv, &[]), vec![33]);
|
||||
|
||||
// The guest bound it after all — there is nothing to substitute.
|
||||
assert_eq!(
|
||||
neutral_sampled_image_bindings(&spirv, &[33]),
|
||||
Vec::<u32>::new()
|
||||
);
|
||||
|
||||
// A binding the guest supplied that the module does not carry is not
|
||||
// invented back into the list.
|
||||
assert_eq!(neutral_sampled_image_bindings(&spirv, &[99]), vec![33]);
|
||||
}
|
||||
@@ -1675,6 +1675,191 @@ pub fn sampler_bindings(words: &[u32]) -> Vec<u32> {
|
||||
bindings
|
||||
}
|
||||
|
||||
/// A module carrying two set-0 sampled images: one referenced by an `OpLoad`,
|
||||
/// one declared and never touched.
|
||||
///
|
||||
/// Lives here, out of any one `mod tests`, because three modules need the same
|
||||
/// fixture and because the opcode numbers it is built from are already constants
|
||||
/// in this file — a copy elsewhere would be those numbers spelled a second time,
|
||||
/// which is the duplication that goes stale silently.
|
||||
///
|
||||
/// Both halves are the point. Only the referenced variable is what Vulkan calls
|
||||
/// *statically used*, so a caller that cannot tell the two apart either refuses
|
||||
/// dispatches that are legal or admits the one that makes a driver divide by
|
||||
/// zero.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn test_module_with_two_sampled_images(used: u32, declared_unused: u32) -> Vec<u32> {
|
||||
const IMAGE_TY: u32 = 10;
|
||||
const POINTER_TY: u32 = 11;
|
||||
const USED_VAR: u32 = 12;
|
||||
const UNUSED_VAR: u32 = 13;
|
||||
|
||||
let mut w = vec![
|
||||
0x0723_0203, // magic
|
||||
0x0001_0600, // version
|
||||
0, // generator
|
||||
32, // bound
|
||||
0, // schema
|
||||
(2u32 << 16) | 17, // OpCapability
|
||||
1, // Shader
|
||||
(3u32 << 16) | 14, // OpMemoryModel
|
||||
0, // Logical
|
||||
1, // GLSL450
|
||||
];
|
||||
for (var, binding) in [(USED_VAR, used), (UNUSED_VAR, declared_unused)] {
|
||||
w.extend_from_slice(&[
|
||||
(4u32 << 16) | OP_DECORATE as u32,
|
||||
var,
|
||||
DECORATION_BINDING,
|
||||
binding,
|
||||
]);
|
||||
}
|
||||
// OpTypeImage %IMAGE_TY %2 2D 0 0 0 1 Unknown — `Sampled` 1 is the
|
||||
// separate-image form, which is what makes these SAMPLED_IMAGE rather than
|
||||
// the storage class that shares the opcode.
|
||||
w.extend_from_slice(&[
|
||||
(9u32 << 16) | OP_TYPE_IMAGE as u32,
|
||||
IMAGE_TY,
|
||||
2, // sampled type id
|
||||
1, // Dim2D
|
||||
0, // depth
|
||||
0, // arrayed
|
||||
0, // MS
|
||||
IMAGE_SAMPLED_WITH_SAMPLER,
|
||||
0, // Unknown format
|
||||
]);
|
||||
w.extend_from_slice(&[
|
||||
(4u32 << 16) | OP_TYPE_POINTER as u32,
|
||||
POINTER_TY,
|
||||
STORAGE_CLASS_UNIFORM_CONSTANT,
|
||||
IMAGE_TY,
|
||||
]);
|
||||
for var in [USED_VAR, UNUSED_VAR] {
|
||||
w.extend_from_slice(&[
|
||||
(4u32 << 16) | OP_VARIABLE as u32,
|
||||
POINTER_TY,
|
||||
var,
|
||||
STORAGE_CLASS_UNIFORM_CONSTANT,
|
||||
]);
|
||||
}
|
||||
// The reference that makes `USED_VAR` statically used, and the only
|
||||
// difference between the two variables.
|
||||
w.extend_from_slice(&[(4u32 << 16) | OP_LOAD as u32, IMAGE_TY, 20, USED_VAR]);
|
||||
w
|
||||
}
|
||||
|
||||
/// Every distinct `Binding` decoration in the module, whatever it decorates.
|
||||
///
|
||||
/// Deliberately class-blind and deliberately cheap: this is the candidate list
|
||||
/// for "does the pipeline layout describe everything the module uses", and the
|
||||
/// question of *what* each binding is has already been answered by the walks
|
||||
/// that build the layout. Pair it with [`descriptor_static_use`], which answers
|
||||
/// `NotDeclared` for anything that is not a `UniformConstant` descriptor and so
|
||||
/// narrows this to the population that walk can reason about exactly.
|
||||
pub fn declared_binding_numbers(words: &[u32]) -> Vec<u32> {
|
||||
let mut bindings = Vec::new();
|
||||
let mut i = HEADER_WORDS;
|
||||
while i < words.len() {
|
||||
let word0 = words[i];
|
||||
let word_count = (word0 >> 16) as usize;
|
||||
let opcode = (word0 & 0xffff) as u16;
|
||||
if word_count == 0 || i + word_count > words.len() {
|
||||
break;
|
||||
}
|
||||
if opcode == OP_DECORATE && word_count >= 4 && words[i + 2] == DECORATION_BINDING {
|
||||
bindings.push(words[i + 3]);
|
||||
}
|
||||
i += word_count;
|
||||
}
|
||||
bindings.sort_unstable();
|
||||
bindings.dedup();
|
||||
bindings
|
||||
}
|
||||
|
||||
/// SPIR-V `OpTypeImage` `Sampled` operand value 1 — "will be used with a
|
||||
/// sampler". 2 is the storage-image form, which is a different descriptor type,
|
||||
/// and 0 means "either", which cannot be given a descriptor type without a guess.
|
||||
const IMAGE_SAMPLED_WITH_SAMPLER: u32 = 1;
|
||||
|
||||
/// Set-0 `Binding` decorations carried by a *sampled image* variable.
|
||||
///
|
||||
/// The texture analogue of [`sampler_bindings`], and it exists for the same
|
||||
/// reason. The descriptor set layout this device builds is assembled from what
|
||||
/// the guest bound, so a binding the module carries and the guest left empty is
|
||||
/// absent from the layout entirely — not an unwritten slot in it.
|
||||
///
|
||||
/// Vulkan requires the pipeline layout to contain a descriptor for every
|
||||
/// resource the module *statically uses*, and a driver is entitled to assume it.
|
||||
/// Mesa's Intel driver indexes its own binding array by binding number, sizes it
|
||||
/// to `max_binding + 1`, zero-fills every number nothing declared, and then
|
||||
/// scores each used binding as `(use_count << 7) / array_size` when it decides
|
||||
/// which descriptors get a binding-table slot. A hole under a *used* binding
|
||||
/// therefore divides by zero: the process dies of `SIGFPE` inside
|
||||
/// `vkCreateComputePipelines`, with no error for the caller to see and no
|
||||
/// validation layer involved. That is why this is a hard requirement here and
|
||||
/// not a tidiness rule — see [`descriptor_static_use`] for the "used" bar, which
|
||||
/// is the only population that must be filled.
|
||||
///
|
||||
/// The class is read from the SPIR-V type, never from the binding number, for
|
||||
/// the reason [`BindingClass`] states. `Sampled` 1 is the separate-image form
|
||||
/// this device's translator emits; the storage form is a different descriptor
|
||||
/// type and is deliberately not returned here, and `SubpassData` is the
|
||||
/// framebuffer-fetch image the engine binds at its un-relocated number.
|
||||
pub fn sampled_image_bindings(words: &[u32]) -> Vec<u32> {
|
||||
use std::collections::HashSet;
|
||||
|
||||
let mut image_types = HashSet::new();
|
||||
let mut image_ptrs = HashSet::new();
|
||||
let mut image_vars = HashSet::new();
|
||||
let mut decorations = Vec::new();
|
||||
let mut i = HEADER_WORDS;
|
||||
while i < words.len() {
|
||||
let word0 = words[i];
|
||||
let word_count = (word0 >> 16) as usize;
|
||||
let opcode = (word0 & 0xffff) as u16;
|
||||
if word_count == 0 || i + word_count > words.len() {
|
||||
break;
|
||||
}
|
||||
match opcode {
|
||||
// OpTypeImage: result, sampled type, Dim, Depth, Arrayed, MS,
|
||||
// Sampled, Format — so `Dim` is operand 3 and `Sampled` operand 7.
|
||||
OP_TYPE_IMAGE
|
||||
if word_count >= 9
|
||||
&& words[i + 3] != DIM_SUBPASS_DATA
|
||||
&& words[i + 7] == IMAGE_SAMPLED_WITH_SAMPLER =>
|
||||
{
|
||||
image_types.insert(words[i + 1]);
|
||||
}
|
||||
OP_TYPE_POINTER if word_count >= 4 => {
|
||||
if words[i + 2] == STORAGE_CLASS_UNIFORM_CONSTANT
|
||||
&& image_types.contains(&words[i + 3])
|
||||
{
|
||||
image_ptrs.insert(words[i + 1]);
|
||||
}
|
||||
}
|
||||
OP_VARIABLE if word_count >= 4 => {
|
||||
if image_ptrs.contains(&words[i + 1])
|
||||
&& words[i + 3] == STORAGE_CLASS_UNIFORM_CONSTANT
|
||||
{
|
||||
image_vars.insert(words[i + 2]);
|
||||
}
|
||||
}
|
||||
OP_DECORATE if word_count >= 4 && words[i + 2] == DECORATION_BINDING => {
|
||||
decorations.push((words[i + 1], words[i + 3]));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
i += word_count;
|
||||
}
|
||||
let mut bindings: Vec<u32> = decorations
|
||||
.into_iter()
|
||||
.filter_map(|(id, binding)| image_vars.contains(&id).then_some(binding))
|
||||
.collect();
|
||||
bindings.sort_unstable();
|
||||
bindings.dedup();
|
||||
bindings
|
||||
}
|
||||
|
||||
/// SPIR-V `Dim` operand value `SubpassData` — the framebuffer-fetch image.
|
||||
const DIM_SUBPASS_DATA: u32 = 6;
|
||||
|
||||
@@ -2761,6 +2946,93 @@ mod tests {
|
||||
assert!(!required_image_capabilities(&words).extended_formats);
|
||||
}
|
||||
|
||||
/// The candidate list is class-blind and the use test is not, which is the
|
||||
/// division the layout backstop is built on.
|
||||
///
|
||||
/// [`declared_binding_numbers`] must return both variables — it is the
|
||||
/// cheap sweep that decides what gets asked about — while
|
||||
/// [`descriptor_static_use`] separates the one Vulkan requires a descriptor
|
||||
/// for from the one it is legal to omit. Asserting them together is what
|
||||
/// says the pair partitions; either alone reads correct while the caller
|
||||
/// built from them refuses legal work or admits the divide-by-zero.
|
||||
#[test]
|
||||
fn the_binding_sweep_is_class_blind_and_the_use_test_narrows_it() {
|
||||
let words = test_module_with_two_sampled_images(33, 34);
|
||||
assert_eq!(declared_binding_numbers(&words), vec![33, 34]);
|
||||
assert_eq!(descriptor_static_use(&words, 33), DescriptorUse::Used);
|
||||
assert_eq!(
|
||||
descriptor_static_use(&words, 34),
|
||||
DescriptorUse::DeclaredUnused
|
||||
);
|
||||
// Both are sampled images all the same — "declared and unused" is a
|
||||
// statement about references, not about class.
|
||||
assert_eq!(sampled_image_bindings(&words), vec![33, 34]);
|
||||
// A binding no variable carries is not invented.
|
||||
assert_eq!(descriptor_static_use(&words, 99), DescriptorUse::NotDeclared);
|
||||
}
|
||||
|
||||
/// A module in the shape of the compositor kernel that killed a host: a
|
||||
/// storage image, several sampled images and a sampler, all on set 0.
|
||||
///
|
||||
/// The three walks must **partition** the bindings, which is why this
|
||||
/// asserts all three rather than only the new one. Each returns a descriptor
|
||||
/// type, and a binding that lands in two of them would be given two
|
||||
/// conflicting layout entries; a binding that lands in none is the hole that
|
||||
/// makes Mesa's Intel driver divide by zero. Reading either walk alone
|
||||
/// cannot see that, because both look correct in isolation.
|
||||
#[test]
|
||||
fn the_sampled_image_walk_and_the_sampler_walk_partition_set_zero() {
|
||||
const STORAGE_IMAGE_TY: u32 = 10;
|
||||
const SAMPLED_IMAGE_TY: u32 = 11;
|
||||
const SAMPLER_TY: u32 = 12;
|
||||
const STORAGE_VAR: u32 = 30;
|
||||
const SAMPLER_VAR: u32 = 33;
|
||||
|
||||
let mut body = Vec::new();
|
||||
// Decorations first, the order a real module carries them in.
|
||||
for (var, binding) in [(STORAGE_VAR, 32u32), (31, 33), (32, 34), (SAMPLER_VAR, 160)] {
|
||||
body.extend_from_slice(&[
|
||||
(4u32 << 16) | OP_DECORATE as u32,
|
||||
var,
|
||||
DECORATION_BINDING,
|
||||
binding,
|
||||
]);
|
||||
}
|
||||
body.extend_from_slice(&op_type_image(STORAGE_IMAGE_TY, IMAGE_SAMPLED_STORAGE, 0));
|
||||
body.extend_from_slice(&op_type_image(
|
||||
SAMPLED_IMAGE_TY,
|
||||
IMAGE_SAMPLED_WITH_SAMPLER,
|
||||
0,
|
||||
));
|
||||
body.extend_from_slice(&[(2u32 << 16) | OP_TYPE_SAMPLER as u32, SAMPLER_TY]);
|
||||
for (ptr, pointee) in [(20, STORAGE_IMAGE_TY), (21, SAMPLED_IMAGE_TY), (22, SAMPLER_TY)] {
|
||||
body.extend_from_slice(&[
|
||||
(4u32 << 16) | OP_TYPE_POINTER as u32,
|
||||
ptr,
|
||||
STORAGE_CLASS_UNIFORM_CONSTANT,
|
||||
pointee,
|
||||
]);
|
||||
}
|
||||
for (ptr, var) in [(20, STORAGE_VAR), (21, 31), (21, 32), (22, SAMPLER_VAR)] {
|
||||
body.extend_from_slice(&[
|
||||
(4u32 << 16) | OP_VARIABLE as u32,
|
||||
ptr,
|
||||
var,
|
||||
STORAGE_CLASS_UNIFORM_CONSTANT,
|
||||
]);
|
||||
}
|
||||
let words = module_with(&body);
|
||||
|
||||
// The two sampled images, and neither the storage image nor the sampler.
|
||||
assert_eq!(sampled_image_bindings(&words), vec![33, 34]);
|
||||
// The sampler alone — the walk that already existed is unchanged.
|
||||
assert_eq!(sampler_bindings(&words), vec![160]);
|
||||
// The storage image belongs to neither walk — it is bound from the
|
||||
// guest's own storage table, as a STORAGE_IMAGE descriptor.
|
||||
assert!(!sampled_image_bindings(&words).contains(&32));
|
||||
assert!(!sampler_bindings(&words).contains(&32));
|
||||
}
|
||||
|
||||
/// An `Unknown`-format storage image plus a write demands
|
||||
/// `StorageImageWriteWithoutFormat`.
|
||||
///
|
||||
|
||||
Reference in new issue
Block a user