Skip to content

Commit

Permalink
[WGSL] Incorrect swizzle validation
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=257378
rdar://109886275

Reviewed by Mike Wyrzykowski.

Swizzle characters need to be checked for bounds, e.g. we can't access vec2.z,
but the original patch was incorrectly checking the offset of the swizzle against
the size of the resulting vector, not the vector we are loading from.

* Source/WebGPU/WGSL/TypeCheck.cpp:
(WGSL::TypeChecker::vectorFieldAccess):
* Source/WebGPU/WGSL/tests/invalid/vector.wgsl:
* Source/WebGPU/WGSL/tests/valid/swizzle.wgsl: Added.

Canonical link: https://commits.webkit.org/264650@main
  • Loading branch information
tadeuzagallo committed May 29, 2023
1 parent c7c487a commit cfcd3f2
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Source/WebGPU/WGSL/TypeCheck.cpp
Expand Up @@ -689,6 +689,7 @@ Type* TypeChecker::vectorFieldAccess(const Types::Vector& vector, AST::FieldAcce
{
const auto& fieldName = access.fieldName().id();
auto length = fieldName.length();
auto vectorSize = vector.size;

bool isValid = true;
const auto& isXYZW = [&](char c) {
Expand All @@ -697,10 +698,10 @@ Type* TypeChecker::vectorFieldAccess(const Types::Vector& vector, AST::FieldAcce
case 'y':
return true;
case 'z':
isValid &= length >= 3;
isValid &= vectorSize >= 3;
return true;
case 'w':
isValid &= length == 4;
isValid &= vectorSize == 4;
return true;
default:
return false;
Expand All @@ -712,10 +713,10 @@ Type* TypeChecker::vectorFieldAccess(const Types::Vector& vector, AST::FieldAcce
case 'g':
return true;
case 'b':
isValid &= length >= 3;
isValid &= vectorSize >= 3;
return true;
case 'a':
isValid &= length == 4;
isValid &= vectorSize == 4;
return true;
default:
return false;
Expand Down
6 changes: 6 additions & 0 deletions Source/WebGPU/WGSL/tests/invalid/vector.wgsl
Expand Up @@ -17,4 +17,10 @@ fn testIndexAccess() {
_ = vec3(0).w;
// CHECK-L: invalid vector swizzle member
_ = vec3(0).a;

// CHECK-L: invalid vector swizzle member
_ = vec2(0).rx;

// CHECK-L: invalid vector swizzle character
_ = vec2(0).v;
}
133 changes: 133 additions & 0 deletions Source/WebGPU/WGSL/tests/valid/swizzle.wgsl
@@ -0,0 +1,133 @@
// RUN: %wgslc

fn testi32(x: i32)
{
_ = x;
}

fn testVec2(x: vec2<i32>)
{
_ = x;
}

fn testVec3(x: vec3<i32>)
{
_ = x;
}

fn testVec4(x: vec4<i32>)
{
_ = x;
}

fn testSwizzleVec2()
{
let v = vec2(0i);
_ = v.x;
_ = v.xx;
_ = v.xxx;
_ = v.xxxx;

_ = v.y;
_ = v.yy;
_ = v.yyy;
_ = v.yyyy;

_ = v.xy;

_ = v.r;
_ = v.rr;
_ = v.rrr;
_ = v.rrrr;

_ = v.g;
_ = v.gg;
_ = v.ggg;
_ = v.gggg;

_ = v.rg;
}

fn testSwizzleVec3() {
let v = vec3(0);
_ = v.x;
_ = v.xx;
_ = v.xxx;
_ = v.xxxx;

_ = v.y;
_ = v.yy;
_ = v.yyy;
_ = v.yyyy;

_ = v.z;
_ = v.zz;
_ = v.zzz;
_ = v.zzzz;

_ = v.xyz;

_ = v.r;
_ = v.rr;
_ = v.rrr;
_ = v.rrrr;

_ = v.g;
_ = v.gg;
_ = v.ggg;
_ = v.gggg;

_ = v.b;
_ = v.bb;
_ = v.bbb;
_ = v.bbbb;

_ = v.rgb;
}

fn testSwizzleVec4() {
let v = vec4(0);
_ = v.x;
_ = v.xx;
_ = v.xxx;
_ = v.xxxx;

_ = v.y;
_ = v.yy;
_ = v.yyy;
_ = v.yyyy;

_ = v.z;
_ = v.zz;
_ = v.zzz;
_ = v.zzzz;

_ = v.w;
_ = v.ww;
_ = v.www;
_ = v.wwww;

_ = v.xyzw;

_ = v.r;
_ = v.rr;
_ = v.rrr;
_ = v.rrrr;

_ = v.g;
_ = v.gg;
_ = v.ggg;
_ = v.gggg;

_ = v.b;
_ = v.bb;
_ = v.bbb;
_ = v.bbbb;

_ = v.a;
_ = v.aa;
_ = v.aaa;
_ = v.aaaa;

_ = v.rgba;
}

0 comments on commit cfcd3f2

Please sign in to comment.