From 00e254ac292b32057599fafea36d1dc2a2cf8efb Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 20 Nov 2026 19:55:49 -0800 Subject: [PATCH] validation: an out-of-bounds target is contained as its module's domains decide containment read a module's facts as if every device took the resident table and the handle table. The runtime's device takes the identity domains (DevicePolicy), so a kernel case through the runtime whose target is a buffer an argument buffer points to, or an argument buffer's texture array, failed: the module has no resident roots and no handle table. ModuleFacts now carries the policy of the device the module was compiled for (Device::policy: the runtime's translator, the executor's own device, and DevicePolicy::default() for the committed tier profiles, which are made with it). - Under the identity pointer domain a pointer loaded from memory is the address it holds, used as given: the runtime's README states that an access through a pointer loaded from memory is not checked. Such a target is contained as device-address with the new Check::Unchecked, and out_of_bounds_run refuses every case with an unchecked target before the make, as NotSupported::Uncontained with the new `unchecked` forms (serde default): not_supported{uncontained:unchecked loaded_pointer}. - Under the identity handle domain an argument buffer's texture-array index is the resource id's index masked onto the descriptor arrays (the new Medium::ResourceId, resource-id:shader), whose elements no live texture owns are null descriptors: its read rule is ResidentTexels, as the handle table's. ENGINE_EXECUTOR_ABI to vulkan-engine-v193. Rows: the family D proof at 846f7c1428 (m3-max-61319) failed 5 cases through the runtime on these facts. Expected at this commit: the 4 loaded-pointer cases 22f5d8ab9c, 570c5673f8, 85c504e02e and ad528203ee move from failed to not_supported{uncontained:unchecked loaded_pointer}; the texture-array case a937d43a67 runs, contained as resource-id:shader. The family D proof at this commit reads them. Verified: unit tests the_identity_domains_leave_a_loaded_pointer_unchecked_ and_mask_a_resource_id (containment under both policies, the refusal's label and text, the masked target's run), the in-layout read-rule test over the resource-id medium, and the bless round trip of the new refusal; the gate job is named on landing. --- .../src/authored_compile/tests.rs | 7 + .../src/candidate/engine.rs | 28 ++-- .../src/candidate/platform/compute.rs | 1 + .../src/candidate/platform/render.rs | 3 +- .../src/candidate/platform/runtime_bounds.rs | 2 + .../src/check/admission.rs | 33 +++- .../src/coverage/tier_table.rs | 2 + .../src/executor_contract/out_of_bounds.rs | 100 +++++++++--- .../executor_contract/out_of_bounds/tests.rs | 147 +++++++++++++++++- .../src/refusal/tests.rs | 1 + docs/VALIDATION.md | 7 +- 11 files changed, 285 insertions(+), 46 deletions(-) diff --git a/crates/metal2vulkan-validation/src/authored_compile/tests.rs b/crates/metal2vulkan-validation/src/authored_compile/tests.rs index f2ebea374..b64183d8a 100644 --- a/crates/metal2vulkan-validation/src/authored_compile/tests.rs +++ b/crates/metal2vulkan-validation/src/authored_compile/tests.rs @@ -886,6 +886,7 @@ fn a_stopped_case_worker_fails_every_cell_of_its_case() { /// failure, and the writer refuses one. #[test] fn every_outcome_kind_round_trips_through_a_bless() { + use crate::case::out_of_bounds::ContainmentForm; use crate::check::admission::ExecutionRefused; use crate::coverage::RobustBufferForm; use crate::engine::{LimitKind, UnsupportedReason}; @@ -957,6 +958,12 @@ fn every_outcome_kind_round_trips_through_a_bless() { ]))), executor(NotSupported::Uncontained { forms: BTreeSet::from([RobustBufferForm::LoadStore, RobustBufferForm::Atomic]), + unchecked: BTreeSet::new(), + variant: None, + }), + executor(NotSupported::Uncontained { + forms: BTreeSet::new(), + unchecked: BTreeSet::from([ContainmentForm::LoadedPointer]), variant: None, }), executor_outcome( diff --git a/crates/metal2vulkan-validation/src/candidate/engine.rs b/crates/metal2vulkan-validation/src/candidate/engine.rs index 02882698b..1d15e869d 100644 --- a/crates/metal2vulkan-validation/src/candidate/engine.rs +++ b/crates/metal2vulkan-validation/src/candidate/engine.rs @@ -96,7 +96,7 @@ use metal2vulkan::pipeline::StageInputDescriptor; use metal2vulkan::pipeline::{ComputePipeline, RenderPipeline}; use std::borrow::Cow; use std::cell::RefCell; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::path::Path; use std::time::Duration; @@ -123,7 +123,7 @@ use tables::{function_tables, table_buffers, FunctionTable, TableBuffers}; /// how it decides a match. A change no recorded row can observe (a case kind no earlier executor /// ran, or one it refused) needs no bump; its commit says why. The commit that bumps it says what /// changed; this comment keeps no history. -pub const ENGINE_EXECUTOR_ABI: &str = "vulkan-engine-v192"; +pub const ENGINE_EXECUTOR_ABI: &str = "vulkan-engine-v193"; /// One kernel case in the terms the binder takes: what this executor supports of an /// [`AuthoredCase`] (buffers, device buffer arrays, argument-buffer buffers and textures, command @@ -1581,8 +1581,9 @@ pub fn is_robustness_off_slot(environment_id: &str) -> bool { /// form and `profile`'s encodings) on a device that executes as `class`, or `None` for a case /// without targets: the run's out-of-bounds facts, before what its guards count /// (`TargetRun::escaped`, `TargetRun::past_end`). A target whose form the device leaves -/// uncontained under that encoding refuses the run, so it is decided before anything is submitted, -/// before the executor's context executes. +/// uncontained under that encoding, or whose accesses the module checks nowhere +/// (`out_of_bounds::Check::Unchecked`), refuses the run, so it is decided before anything is +/// submitted, before the executor's context executes. pub(crate) fn out_of_bounds_run( class: Option, profile: &DeviceProfile, @@ -1608,9 +1609,15 @@ pub(crate) fn out_of_bounds_run( }) .collect::, ExecutorError>>()?; let uncontained = out_of_bounds::uncontained(&targets, class); - if !uncontained.is_empty() { + let unchecked = targets + .iter() + .filter(|target| target.mechanism.check == out_of_bounds::Check::Unchecked) + .map(|target| target.form) + .collect::>(); + if !uncontained.is_empty() || !unchecked.is_empty() { return Err(NotSupported::Uncontained { forms: uncontained, + unchecked, variant: None, } .into()); @@ -1618,18 +1625,18 @@ pub(crate) fn out_of_bounds_run( Ok(Some(OutOfBoundsRun { targets })) } -/// [`out_of_bounds_run`] for a render case's draw: how `pipeline`'s module that runs the Metal -/// vertex function (`graphics::vertex_module`) contains each of `case`'s targets, its attributes -/// fetched from `case`'s vertex buffers. +/// [`out_of_bounds_run`] for a render case's draw: how `pipeline`'s module, compiled for +/// `device`, that runs the Metal vertex function (`graphics::vertex_module`) contains each of +/// `case`'s targets, its attributes fetched from `case`'s vertex buffers. pub(crate) fn draw_out_of_bounds_run( class: Option, - profile: &DeviceProfile, + device: &metal2vulkan::device::Device, pipeline: &RenderPipeline, case: &graphics::RenderCase, ) -> Result, ExecutorError> { out_of_bounds_run( class, - profile, + device.profile(), &out_of_bounds::ModuleFacts { stage: ShaderStage::Vertex, slots: &pipeline.interface.slots, @@ -1641,6 +1648,7 @@ pub(crate) fn draw_out_of_bounds_run( ), resident_roots: pipeline.interface.needs_pointer_translation, handle_table: pipeline.interface.needs_handle_translation, + policy: *device.policy(), strategies: &graphics::vertex_module(pipeline)?.strategies, }, &case.out_of_bounds, diff --git a/crates/metal2vulkan-validation/src/candidate/platform/compute.rs b/crates/metal2vulkan-validation/src/candidate/platform/compute.rs index 0308dd54f..3efbd01de 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/compute.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/compute.rs @@ -300,6 +300,7 @@ fn run( attributes: &attributes, resident_roots: pipeline.interface.needs_pointer_translation, handle_table: pipeline.interface.needs_handle_translation, + policy: *device.policy(), strategies: &pipeline.stage.strategies, }, &case.out_of_bounds, diff --git a/crates/metal2vulkan-validation/src/candidate/platform/render.rs b/crates/metal2vulkan-validation/src/candidate/platform/render.rs index 11c33e347..7d3b6e6c5 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/render.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/render.rs @@ -187,8 +187,7 @@ fn run( .transpose()?; // How the Metal vertex function's module contains each out-of-bounds target on this device, // decided before anything runs; a form the device leaves uncontained is refused, never run. - let out_of_bounds = - draw_out_of_bounds_run(context.execution_class, device.profile(), pipeline, case)?; + let out_of_bounds = draw_out_of_bounds_run(context.execution_class, device, pipeline, case)?; let spv_sha256 = sha256_bytes( &pipeline .stages diff --git a/crates/metal2vulkan-validation/src/candidate/platform/runtime_bounds.rs b/crates/metal2vulkan-validation/src/candidate/platform/runtime_bounds.rs index 45e311edf..44a97e1ec 100644 --- a/crates/metal2vulkan-validation/src/candidate/platform/runtime_bounds.rs +++ b/crates/metal2vulkan-validation/src/candidate/platform/runtime_bounds.rs @@ -70,6 +70,7 @@ pub(super) fn contained( attributes: &attributes, resident_roots: lowered.reads_resident_roots(), handle_table: lowered.resolves_handles(), + policy: *translator.policy(), strategies: lowered.strategies(), }; let run = out_of_bounds_run(class, translator.profile(), &module, &case.out_of_bounds); @@ -84,6 +85,7 @@ pub(super) fn contained( if let Some(&(variant, _)) = rest.iter().find(|(_, run)| run != first) { return Err(NotSupported::Uncontained { forms: Default::default(), + unchecked: Default::default(), variant: Some(variant), } .into()); diff --git a/crates/metal2vulkan-validation/src/check/admission.rs b/crates/metal2vulkan-validation/src/check/admission.rs index dd22bd065..917a51c8a 100644 --- a/crates/metal2vulkan-validation/src/check/admission.rs +++ b/crates/metal2vulkan-validation/src/check/admission.rs @@ -94,13 +94,16 @@ pub enum NotSupported { Requirements(BTreeSet), /// An out-of-bounds case whose run would reach memory no binding gave it, so it is not run /// there: the opened device leaves accesses of `forms` uncontained under the robust buffer - /// encoding the case's module compiled to (`HostClass::robust_buffers_uncontained`), or, for a - /// run through the runtime, the kernel's `variant` contains one of its targets otherwise than - /// the first variant the translator lowered of it (`platform::runtime_bounds`), so which module - /// a dispatch runs would decide it. + /// encoding the case's module compiled to (`HostClass::robust_buffers_uncontained`), the + /// module checks the accesses of its targets of the `unchecked` forms nowhere + /// (`out_of_bounds::Check::Unchecked`), or, for a run through the runtime, the kernel's + /// `variant` contains one of its targets otherwise than the first variant the translator + /// lowered of it (`platform::runtime_bounds`), so which module a dispatch runs would decide it. Uncontained { forms: BTreeSet, #[serde(default)] + unchecked: BTreeSet, + #[serde(default)] variant: Option, }, /// The run was restricted to a committed profile (`--profiles-layer`) that states capabilities @@ -123,11 +126,20 @@ impl NotSupported { "not_supported{{tooling_requirement:{}}}", requirement_names(requirements) ), - Self::Uncontained { forms, variant } => format!( + Self::Uncontained { + forms, + unchecked, + variant, + } => format!( "not_supported{{uncontained:{}}}", forms .iter() .map(|form| form.label().to_owned()) + .chain( + unchecked + .iter() + .map(|form| format!("unchecked {}", form.as_str())) + ) .chain(variant.map(|variant| format!("variant {variant:?}"))) .collect::>() .join(",") @@ -160,6 +172,17 @@ impl fmt::Display for NotSupported { "{}: the executor does not run these families", self.outcome() ), + Self::Uncontained { + unchecked, + variant: None, + .. + } if !unchecked.is_empty() => write!( + formatter, + "{}: the compiled module checks the accesses of these forms nowhere, neither in \ + the shader nor by the device's robustness, so the case's out-of-bounds accesses \ + are not run", + self.outcome() + ), Self::Uncontained { variant: None, .. } => write!( formatter, "{}: the device leaves these forms uncontained under its robust buffer access, so \ diff --git a/crates/metal2vulkan-validation/src/coverage/tier_table.rs b/crates/metal2vulkan-validation/src/coverage/tier_table.rs index 18b62caa5..b3008194a 100644 --- a/crates/metal2vulkan-validation/src/coverage/tier_table.rs +++ b/crates/metal2vulkan-validation/src/coverage/tier_table.rs @@ -262,6 +262,8 @@ pub fn compile_module( attributes: &attributes, resident_roots: function.reads_resident_roots(), handle_table: function.resolves_handles(), + // Every committed profile's device takes the default domains (`committed_profiles`). + policy: DevicePolicy::default(), strategies: function.strategies(), }; let out_of_bounds = inputs diff --git a/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds.rs b/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds.rs index 13722e47d..7188a9d30 100644 --- a/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds.rs +++ b/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds.rs @@ -17,19 +17,23 @@ //! checked in the shader. A buffer reached through a pointer loaded from an argument buffer is a //! device address the resident set resolves, a thread-local array a typed object whose index is //! checked against its length, and an argument buffer's texture array a handle table the loaded -//! resource id is looked up in: each checked in the shader in every leg. An attribute buffer is -//! fetched by fixed-function vertex input, left to the device's buffer robustness, or pulled in -//! the shader through the slot its fetch names, contained as that slot is ([`Fetch`]); a direct -//! indexed draw's index buffer is bounded by `robustBufferAccess2` or the binder's clamp; and a -//! top-level texture array's dynamic index is clamped onto its elements in the shader. +//! resource id is looked up in: each checked in the shader in every leg. Under the identity +//! domains (`DevicePolicy`) a loaded pointer is the address it holds, which nothing checks, so +//! the candidate does not run the case ([`Check::Unchecked`]), and a loaded resource id's index +//! is masked onto the descriptor arrays, whose elements no live texture owns are null +//! descriptors. An attribute buffer is fetched by fixed-function vertex input, left to the +//! device's buffer robustness, or pulled in the shader through the slot its fetch names, +//! contained as that slot is ([`Fetch`]); a direct indexed draw's index buffer is bounded by +//! `robustBufferAccess2` or the binder's clamp; and a top-level texture array's dynamic index is +//! clamped onto its elements in the shader. //! - **The read rule** ([`read_rule`]) says what an out-of-bounds read of the target may return: //! zero under an in-shader check and under `robustBufferAccess2` (Vulkan's "Robust Buffer //! Access 2": "reads ... outside of the checked range will return zero values"), anything for //! an atomic's return under robust buffer access ("will return an undefined value"), and under //! `robustBufferAccess` alone zero, `(0, 0, 0, x)`, or a value of the buffer's own memory. A //! read through a handle past a handle array returns a texel of a texture the case binds, or -//! zero (the null handle). An index read past an index buffer is index 0 or none, so the -//! vertex ids only a read past it gives stay unshaded. +//! zero (the null handle or the null descriptor). An index read past an index buffer is index 0 +//! or none, so the vertex ids only a read past it gives stay unshaded. //! - **Nothing escapes**: every byte of the guard the candidate allocates around a buffer (an //! argument buffer's included) or texture buffer target is unchanged ([`TargetRun::escaped`]), //! and so is the memory bound to it past its end ([`TargetRun::past_end`]) except under @@ -48,7 +52,7 @@ use crate::case::{AuthoredCase, OutputSelection}; use crate::coverage::{ExecutionClass, RobustBufferForm}; use crate::observation_contract::{ComparisonClass, ObservationLanes}; use base64::Engine as _; -use metal2vulkan::device::{DeviceProfile, Robustness}; +use metal2vulkan::device::{DevicePolicy, DeviceProfile, HandleDomain, PointerDomain, Robustness}; use metal2vulkan::library::CompiledFunction; use metal2vulkan::pipeline::{AttributeFetch, StrategyCensus}; use metal2vulkan::pipeline::{BufferForm, DescriptorView, SlotInterface}; @@ -77,6 +81,10 @@ pub enum Medium { /// The handle table a texture's resource id, loaded from an argument buffer, is looked up in: /// an id of no resident texture is the null handle. HandleTable, + /// The descriptor arrays a texture's resource id, loaded from an argument buffer, indexes + /// under the identity handle domain (`HandleDomain::Identity`), its index masked onto the + /// arrays' elements: an element no live texture owns is the null descriptor. + ResourceId, /// Vulkan's fixed-function vertex input, fetching a vertex buffer binding (only under buffer /// robustness). VertexInput, @@ -109,6 +117,12 @@ pub enum Check { /// reads, so no index fetch lies past the bound range, where Vulkan states no index: each /// position past where Metal's fetch stops reads index 0. Padded, + /// Nothing checks the access: neither the shader nor the device's robustness bounds it (a + /// pointer loaded from memory under the identity pointer domain, `PointerDomain::Identity`, + /// is the device address it holds, used as given). A run would reach memory no binding gave + /// the case, so the candidate refuses the case before it makes the pipeline + /// (`NotSupported::Uncontained`). + Unchecked, } /// How a compiled module contains one out-of-bounds target. @@ -130,6 +144,7 @@ impl Mechanism { Medium::Image => "image", Medium::TypedObject => "typed-object", Medium::HandleTable => "handle-table", + Medium::ResourceId => "resource-id", Medium::VertexInput => "vertex-input", Medium::IndexBuffer => "index-buffer", Medium::HandleArray => "handle-array", @@ -141,6 +156,7 @@ impl Mechanism { Check::Robust2 => "robust2", Check::Binder => "binder", Check::Padded => "padded", + Check::Unchecked => "unchecked", }; format!("{medium}:{check}") } @@ -148,11 +164,18 @@ impl Mechanism { /// The form of the robust buffer encoding this mechanism leaves to the device /// ([`RobustBufferForm`]): a descriptor view's loads and stores or atomics, a texel buffer, and /// fixed-function vertex input, under buffer robustness; `None` for every in-shader check, for - /// the binder's and the padded copy's, for images, and for an index fetch, which only - /// `robustBufferAccess2` bounds. + /// the binder's and the padded copy's, for an unchecked access, for images, and for an index + /// fetch, which only `robustBufferAccess2` bounds. pub fn robust_buffer_form(self, form: ContainmentForm) -> Option { match (self.medium, self.check) { - (_, Check::Shader | Check::ShaderReads | Check::Binder | Check::Padded) => None, + ( + _, + Check::Shader + | Check::ShaderReads + | Check::Binder + | Check::Padded + | Check::Unchecked, + ) => None, (Medium::DescriptorView, _) if form == ContainmentForm::BufferAtomic => { Some(RobustBufferForm::Atomic) } @@ -165,6 +188,7 @@ impl Mechanism { | Medium::Image | Medium::TypedObject | Medium::HandleTable + | Medium::ResourceId | Medium::IndexBuffer | Medium::HandleArray, _, @@ -259,10 +283,16 @@ pub enum ReadRule { /// ([`TargetRun::read_rule`]). pub fn read_rule(form: ContainmentForm, mechanism: Mechanism, written: bool) -> ReadRule { match (mechanism.medium, mechanism.check) { + // An unchecked access reads whatever the device holds there; the candidate never runs one. + (_, Check::Unchecked) => ReadRule::Any, // An id past the handle array is another handle of the argument buffer, which the table - // resolves to a resident texture or the null handle. An index past a top-level array is - // clamped onto its last element, a texture the case binds. - (Medium::HandleTable | Medium::HandleArray, _) => ReadRule::ResidentTexels, + // resolves to a resident texture or the null handle, and which the identity handle domain + // masks onto the descriptor arrays, whose elements no live texture owns are null + // descriptors. An index past a top-level array is clamped onto its last element, a texture + // the case binds. + (Medium::HandleTable | Medium::ResourceId | Medium::HandleArray, _) => { + ReadRule::ResidentTexels + } // An index read past the buffer is index 0 from the padded copy (and was under robust // buffer access 2, and the binder drew none); the case's in-range indices include index 0 // (its checker rule), so none reaches a byte the in-range indices do not, and the declared @@ -373,7 +403,8 @@ impl Fetch { /// What [`containment`] reads of one compiled module: the stage it was compiled for, its Metal /// argument slots, how it fetches each attribute, whether it reads the resident root table and -/// resolves handles through the handle table, and the census of the strategies its lowering used. +/// resolves handles through the handle table, the domains it was compiled under, and the census of +/// the strategies its lowering used. #[derive(Clone, Copy, Debug)] pub struct ModuleFacts<'a> { pub stage: SymStage, @@ -387,6 +418,9 @@ pub struct ModuleFacts<'a> { /// The module resolves texture or sampler handles through the handle table /// (`needs_handle_translation`). pub handle_table: bool, + /// The pointer and handle domains of the device the module was compiled for + /// (`Device::policy`). + pub policy: DevicePolicy, pub strategies: &'a StrategyCensus, } @@ -434,9 +468,11 @@ mod census { /// buffer, and a `device_address` when it reaches it by its address, except that a /// `buffer_atomic` target (one an AIR atomic accesses, which the case checker establishes) stays /// one in every medium. A buffer an argument buffer points to is a `loaded_pointer` when the -/// module loads a pointer and accesses memory through it, thread memory a `sized_array` when the -/// module indexes a typed thread-local array at a dynamic index, and an argument buffer's texture -/// array a `handle_array_index` when the module looks its handles up in the handle table. `Err` +/// module loads a pointer and accesses memory through it, which the resident table checks and the +/// identity pointer domain leaves unchecked ([`Check::Unchecked`]), thread memory a `sized_array` +/// when the module indexes a typed thread-local array at a dynamic index, and an argument buffer's +/// texture array a `handle_array_index` when the module looks its handles up in the handle table, +/// or, under the identity handle domain, masks their ids onto the descriptor arrays. `Err` /// when the module does not access the target, reaches it in a way validation does not know (thread /// memory in the private arena, whose check is the arena's bounds, not the array's). pub fn containment( @@ -518,18 +554,25 @@ pub fn containment( OutOfBoundsResource::ArgumentBufferBuffer { buffer_binding, .. } => { slot(SlotClass::Buffer, buffer_binding)?; let accessed = module.uses(census::DYNAMIC_LOAD) || module.uses(census::DYNAMIC_STORE); - if !module.resident_roots || !module.uses_family(census::LOAD_POINTER) || !accessed { + // Under the resident table the loaded pointer resolves to the resident allocation + // holding it, and each access is checked against that allocation's size in the shader, + // in every leg. Under the identity pointer domain it is the device address it holds, + // used as given: nothing checks the access. + let (resolved, check) = match module.policy.pointer_domain { + PointerDomain::ResidentTable => (module.resident_roots, Check::Shader), + PointerDomain::Identity => (true, Check::Unchecked), + _ => return Err("a pointer domain validation does not know".into()), + }; + if !resolved || !module.uses_family(census::LOAD_POINTER) || !accessed { return Err(format!( "the compiled module accesses no memory through a pointer loaded from {}", target.resource.label() )); } - // The loaded pointer resolves to the resident allocation holding it, and each access is - // checked against that allocation's size in the shader, in every leg. made( ContainmentForm::LoadedPointer, Medium::DeviceAddress, - Check::Shader, + check, module.uses(census::DYNAMIC_STORE), ) } @@ -557,15 +600,22 @@ pub fn containment( } OutOfBoundsResource::ArgumentBufferTextureArray { buffer_binding, .. } => { slot(SlotClass::Buffer, buffer_binding)?; - if !module.handle_table || !module.uses_family(census::HANDLE_LOOKUP) { + // The handle table resolves a loaded id to a resident texture or the null handle; the + // identity handle domain masks the id's index onto the descriptor arrays. + let (resolved, medium) = match module.policy.handle_domain { + HandleDomain::Table => (module.handle_table, Medium::HandleTable), + HandleDomain::Identity => (true, Medium::ResourceId), + _ => return Err("a handle domain validation does not know".into()), + }; + if !resolved || !module.uses_family(census::HANDLE_LOOKUP) { return Err(format!( - "the compiled module looks no handle of {} up in the handle table", + "the compiled module resolves no handle of {}", target.resource.label() )); } made( ContainmentForm::HandleArrayIndex, - Medium::HandleTable, + medium, Check::Shader, false, ) diff --git a/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds/tests.rs b/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds/tests.rs index 23f188674..b91308ea2 100644 --- a/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds/tests.rs +++ b/crates/metal2vulkan-validation/src/executor_contract/out_of_bounds/tests.rs @@ -511,8 +511,8 @@ fn a_loaded_pointers_buffer_reads_zero_and_is_guarded() { /// A thread-local array is a typed object and a handle array lies in its argument buffer: no /// host memory holds either beside the case's own, so neither has a guard. A read past the array is -/// zero; a read past the handle array is a texel of a texture the case binds, or zero (the null -/// handle), never another value. +/// zero; a read past the handle array, through the handle table or a masked resource id, is a texel +/// of a texture the case binds, or zero (the null handle or descriptor), never another value. #[test] fn an_in_layout_target_has_no_guard_and_its_own_read_rule() { let class = ComparisonClass::Exact; @@ -531,6 +531,14 @@ fn an_in_layout_target_has_no_guard_and_its_own_read_rule() { ContainmentForm::HandleArrayIndex, Medium::HandleTable, ), + ( + OutOfBoundsResource::ArgumentBufferTextureArray { + buffer_binding: 0, + field_offset: 0, + }, + ContainmentForm::HandleArrayIndex, + Medium::ResourceId, + ), ] { let case = embedded_case(resource, form, texel); let ran = |guard| embedded_run(resource, form, medium, guard); @@ -551,7 +559,7 @@ fn an_in_layout_target_has_no_guard_and_its_own_read_rule() { let resident = with_reads([texel, 0, texel, 0]); assert_eq!( matches(&case, &class, &golden(), &resident, Some(&ran(None))), - medium == Medium::HandleTable, + medium != Medium::TypedObject, "{form}" ); let foreign = with_reads([0xA000_0005, 0, 0, 0]); @@ -784,6 +792,7 @@ fn thread_memory_is_contained_by_the_typed_arrays_check_only() { attributes: &BTreeMap::new(), resident_roots: true, handle_table: true, + policy: DevicePolicy::default(), strategies: &strategies, }; let target = OutOfBoundsTarget { @@ -847,6 +856,137 @@ fn thread_memory_is_contained_by_the_typed_arrays_check_only() { } } +/// Under the identity domains a pointer loaded from an argument buffer is the device address it +/// holds, which nothing checks, so the run is refused before anything is made; an argument +/// buffer's texture array is the descriptor arrays the loaded id's masked index reads, whose read +/// past the array is a texel of a texture the case binds or zero. Under the resident table and the +/// handle table, the same module facts without the table's reads access neither target. +#[test] +fn the_identity_domains_leave_a_loaded_pointer_unchecked_and_mask_a_resource_id() { + use crate::candidate::engine::out_of_bounds_run; + use crate::check::admission::{ExecutorError, NotSupported}; + use metal2vulkan::pipeline::{StrategyCensus, StrategyUse}; + let profiles = crate::coverage::tier_table::committed_profiles().unwrap(); + let profile = &profiles + .iter() + .find(|entry| entry.tier.stem == "lavapipe") + .unwrap() + .profile; + let mut identity = DevicePolicy::default(); + identity.pointer_domain = PointerDomain::Identity; + identity.handle_domain = HandleDomain::Identity; + let slots = BTreeMap::from([( + SlotRef { + stage: SymStage::Kernel, + class: SlotClass::Buffer, + index: 1, + }, + // The argument buffer's slot, read through its device address (the interface has no + // constructor outside the product). + serde_json::from_value::(serde_json::json!({"Buffer": { + "access": {"read": true, "write": false}, + "window": "Whole", + "form": "Psb", + "kind": "Buffer", + }})) + .unwrap(), + )]); + let mut strategies = StrategyCensus::default(); + for name in [ + "access.dynamic.load_pointer", + "access.dynamic.load", + "access.handle.lookup", + ] { + strategies + .entries + .insert(name.into(), StrategyUse::default()); + } + let module = |policy| ModuleFacts { + stage: SymStage::Kernel, + slots: &slots, + attributes: &BTreeMap::new(), + resident_roots: false, + handle_table: false, + policy, + strategies: &strategies, + }; + let target = |resource, form| OutOfBoundsTarget { + resource, + form, + reach_before: 0, + reach_after: 16, + }; + let pointer = target( + OutOfBoundsResource::ArgumentBufferBuffer { + buffer_binding: 1, + field_offset: 0, + }, + ContainmentForm::LoadedPointer, + ); + let handles = target( + OutOfBoundsResource::ArgumentBufferTextureArray { + buffer_binding: 1, + field_offset: 0, + }, + ContainmentForm::HandleArrayIndex, + ); + let loaded = containment(&pointer, &module(identity), profile).unwrap(); + assert_eq!( + (loaded.form, loaded.mechanism.label(), loaded.written), + ( + ContainmentForm::LoadedPointer, + "device-address:unchecked".into(), + false + ) + ); + assert_eq!( + loaded + .mechanism + .robust_buffer_form(ContainmentForm::LoadedPointer), + None + ); + let masked = containment(&handles, &module(identity), profile).unwrap(); + assert_eq!( + (masked.form, masked.mechanism.label()), + ( + ContainmentForm::HandleArrayIndex, + "resource-id:shader".into() + ) + ); + assert_eq!( + read_rule(ContainmentForm::HandleArrayIndex, masked.mechanism, false), + ReadRule::ResidentTexels + ); + assert!( + containment(&pointer, &module(DevicePolicy::default()), profile) + .unwrap_err() + .contains("no memory through a pointer loaded") + ); + assert!( + containment(&handles, &module(DevicePolicy::default()), profile) + .unwrap_err() + .contains("resolves no handle") + ); + // The unchecked target refuses the run, naming its form; the masked one runs. + match out_of_bounds_run(None, profile, &module(identity), &[pointer, handles]) { + Err(ExecutorError::NotSupported(refused @ NotSupported::Uncontained { .. })) => { + assert_eq!( + refused.outcome(), + "not_supported{uncontained:unchecked loaded_pointer}" + ); + assert!( + refused.to_string().contains("checks the accesses"), + "{refused}" + ); + } + other => panic!("{other:?}"), + } + let run = out_of_bounds_run(None, profile, &module(identity), &[handles]) + .unwrap() + .unwrap(); + assert_eq!(run.targets[0].mechanism, masked.mechanism); +} + /// A draw's targets: a fixed-function attribute is contained by the device's buffer robustness, and /// only there; a direct indexed draw's index buffer by the padded copy of its indices on every /// device; a top-level texture array by the clamped handle-array load. A pulled attribute is @@ -869,6 +1009,7 @@ fn a_draws_targets_are_contained_by_their_fetch() { attributes, resident_roots: false, handle_table: false, + policy: DevicePolicy::default(), strategies, }; let target = OutOfBoundsTarget { diff --git a/crates/metal2vulkan-validation/src/refusal/tests.rs b/crates/metal2vulkan-validation/src/refusal/tests.rs index 4d97e4b80..3838d7a0b 100644 --- a/crates/metal2vulkan-validation/src/refusal/tests.rs +++ b/crates/metal2vulkan-validation/src/refusal/tests.rs @@ -38,6 +38,7 @@ fn a_refusal_is_classed_by_its_type() { ( NotSupported::Uncontained { forms: [RobustBufferForm::LoadStore].into(), + unchecked: Default::default(), variant: None, }, RefusalClass::Device, diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index e7fc2e604..51029c91b 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -471,7 +471,12 @@ cargo run -p metal2vulkan-validation --release --bin corpus-moltenvk -- \ out-of-bounds case's facts are read before the state is made, from each variant the translator lowers of the kernel on the runtime device's own translator (`Device::translator`, under `LoweringInputs::kernel_variant`); variants that contain a target differently refuse the case, - `not_supported{uncontained:variant ...}` naming the one that differs. Each buffer target sits + `not_supported{uncontained:variant ...}` naming the one that differs. The facts are the identity + domains' (`DevicePolicy`): a buffer reached through a pointer loaded from an argument buffer is + the address the pointer holds, which nothing checks (the runtime's README states it), so such a + case is refused as `not_supported{uncontained:unchecked loaded_pointer}`, and an argument + buffer's texture array is the descriptor arrays its resource ids' masked indices read + (`resource-id:shader`), a read past it a texel of a bound texture or zero. Each buffer target sits between guards of its declared reach in a Shared block of its own, read back after the dispatch. A multisampled, sparse, texture-buffer or cube-array texture keeps the case on the executor's own context. A pixel format the runtime makes no image of on the device records `not_enabled`, as