Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that stores are permitted by the pointer's storage access. #1534

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ impl super::TypeInner {
}
}

impl super::StorageClass {
pub fn access(self) -> crate::StorageAccess {
use crate::StorageAccess as Sa;
match self {
crate::StorageClass::Function
| crate::StorageClass::Private
| crate::StorageClass::WorkGroup => Sa::LOAD | Sa::STORE,
crate::StorageClass::Uniform => Sa::LOAD,
crate::StorageClass::Storage { access } => access,
crate::StorageClass::Handle => Sa::LOAD,
crate::StorageClass::PushConstant => Sa::LOAD,
}
}
}

impl super::MathFunction {
pub fn argument_count(&self) -> usize {
match *self {
Expand Down
18 changes: 15 additions & 3 deletions src/valid/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,12 @@ impl super::Validator {
}
_ => {}
}
let good = match *context

let pointer_ty = context
.resolve_pointer_type(pointer)
.map_err(|e| e.with_span())?
{
.map_err(|e| e.with_span())?;

let good = match *pointer_ty {
Ti::Pointer { base, class: _ } => match context.types[base].inner {
Ti::Atomic { kind, width } => *value_ty == Ti::Scalar { kind, width },
ref other => value_ty == other,
Expand All @@ -634,6 +636,16 @@ impl super::Validator {
.with_handle(pointer, context.expressions)
.with_handle(value, context.expressions));
}

if let Some(class) = pointer_ty.pointer_class() {
if !class.access().contains(crate::StorageAccess::STORE) {
return Err(FunctionError::InvalidStorePointer(pointer)
.with_span_static(
context.expressions.get_span(pointer),
"writing to this location is not permitted",
));
}
}
}
S::ImageStore {
image,
Expand Down
2 changes: 1 addition & 1 deletion tests/in/bounds-check-zero.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Globals {
m: mat3x4<f32>;
};

[[group(0), binding(0)]] var<storage> globals: Globals;
[[group(0), binding(0)]] var<storage, read_write> globals: Globals;

fn index_array(i: i32) -> f32 {
return globals.a[i];
Expand Down
1 change: 0 additions & 1 deletion tests/out/spv/bounds-check-zero.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ OpMemberDecorate %9 1 Offset 48
OpMemberDecorate %9 2 Offset 64
OpMemberDecorate %9 2 ColMajor
OpMemberDecorate %9 2 MatrixStride 16
OpDecorate %10 NonWritable
OpDecorate %10 DescriptorSet 0
OpDecorate %10 Binding 0
%2 = OpTypeVoid
Expand Down
42 changes: 42 additions & 0 deletions tests/wgsl-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,45 @@ fn missing_default_case() {
)
}
}

#[test]
fn wrong_access_mode() {
// The assignments to `global.i` should be forbidden, because they are in
// variables whose access mode is `read`, not `read_write`.
check_validation_error! {
"
[[block]]
struct Globals {
i: i32;
};

[[group(0), binding(0)]]
var<storage> globals: Globals;

fn store(v: i32) {
globals.i = v;
}
",
"
[[block]]
struct Globals {
i: i32;
};

[[group(0), binding(0)]]
var<uniform> globals: Globals;

fn store(v: i32) {
globals.i = v;
}
":
Err(
naga::valid::ValidationError::Function {
name,
error: naga::valid::FunctionError::InvalidStorePointer(_),
..
},
)
if name == "store"
}
}