Skip to content
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
139 changes: 139 additions & 0 deletions src/webgpu/shader/execution/mutation/mesa/no_input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
export const description = `
Execution tests that read no input, found by mutation testing Mesa.

Each case is a compute shader generated by the WGSL fuzzer WGSLsmith and then reduced
automatically. They were found by mutation testing a shader compiler: each program produces
different output when a specific change is injected into the compiler, and no existing CTS test
detected that change.

Each shader writes its result to a storage buffer at @group(0) @binding(1).
Expected output is given as bytes, in the form the fuzzer reports it, with a comment showing the
values of each struct member.

Case names are "shader_" followed by the first 8 hex digits of the SHA-256 hash of the
shader text. kTestData holds each case's shader, its expected output, and the location of the
mutation it detects.
`;

import { makeTestGroup } from '../../../../../common/framework/test_group.js';
import { keysOf } from '../../../../../common/util/data_tables.js';
import { AllFeaturesMaxLimitsGPUTest, GPUTest } from '../../../../gpu_test.js';
import { MutationInfo } from '../mutation_info.js';

export const g = makeTestGroup(AllFeaturesMaxLimitsGPUTest);

interface TestData {
/** Where the mutation this test detects was made. */
mutation: MutationInfo;
/** The shader. */
code: string;
/** The expected bytes of the output buffer. */
expectedOutput: number[];
}

/**
* Runs a shader (entry point `main`, one invocation) and checks the contents of its output
* buffer.
*
* @param t The test object
* @param expectedOutput The expected bytes of the storage buffer at @group(0) @binding(1).
* The buffer starts zero-filled, so padding bytes are expected to be 0.
* @param wgsl The shader
*/
function runGeneratedTest(t: GPUTest, expectedOutput: Uint8Array, wgsl: string) {
const pipeline = t.device.createComputePipeline({
layout: 'auto',
compute: {
module: t.device.createShaderModule({ code: wgsl }),
entryPoint: 'main',
},
});

// New buffers are zero-filled.
const outputBuffer = t.createBufferTracked({
size: expectedOutput.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
});
const bindGroup = t.device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [{ binding: 1, resource: { buffer: outputBuffer } }],
});

// Run the shader.
const encoder = t.device.createCommandEncoder();
const pass = encoder.beginComputePass();
pass.setPipeline(pipeline);
pass.setBindGroup(0, bindGroup);
pass.dispatchWorkgroups(1);
pass.end();
t.queue.submit([encoder.finish()]);

t.expectGPUBufferValuesEqual(outputBuffer, expectedOutput);
}

const kTestData: Record<string, TestData> = {
shader_101299f4: {
mutation: {
mutant: {
component: 'mesa',
version: 'ba8eaab4f07e33c0b74fa92c60852cba2518bf2e',
file: 'src/compiler/nir/nir_constant_expressions.c',
line: 48527,
generatedFrom: 'src/compiler/nir/nir_constant_expressions.py',
},
environment: [{ component: 'dawn', version: '9de0fd67086127228e8aa28e69616da37220cfc3' }],
},
code: `
struct StorageBuffer {
a: vec2<i32>,
}

@group(0) @binding(1) var<storage, read_write> s_output: StorageBuffer;

var<private> v: array<i32, 7>;

@compute
@workgroup_size(1i)
fn main() {
s_output = StorageBuffer(max(vec3<i32>(), vec3<i32>(v[select(abs(5i) % 7i, 0i, 5i == i32(-2147483648))])).xy);
}
`,
expectedOutput: [0, 0, 0, 0, 0, 0, 0, 0], // s_output: a = (0, 0)
},
shader_10c2ef9c: {
mutation: {
mutant: {
component: 'mesa',
version: 'ba8eaab4f07e33c0b74fa92c60852cba2518bf2e',
file: 'src/compiler/nir/nir_constant_expressions.c',
line: 50895,
generatedFrom: 'src/compiler/nir/nir_constant_expressions.py',
},
environment: [{ component: 'dawn', version: '9de0fd67086127228e8aa28e69616da37220cfc3' }],
},
code: `
struct StorageBuffer {
c: i32,
}

@group(0) @binding(1) var<storage, read_write> s_output: StorageBuffer;

var<private> v: array<i32, 5> = array<i32, 5>(1i, 8i, 4i, 8i, 0i);

@compute
@workgroup_size(1i)
fn main(@builtin(local_invocation_id) builtin_local_invocation_id: vec3<u32>) {
s_output = StorageBuffer(v[countTrailingZeros(vec2<u32>(builtin_local_invocation_id.x, 6u)).x % 5u]);
}
`,
expectedOutput: [4, 0, 0, 0], // s_output: c = 4
},
};

g.test('no_input')
.desc(`Tests compute shaders that read no input, found by mutation testing Mesa.`)
.params(u => u.combine('case', keysOf(kTestData)))
.fn(t => {
const c = kTestData[t.params.case];
runGeneratedTest(t, new Uint8Array(c.expectedOutput), c.code);
});
249 changes: 249 additions & 0 deletions src/webgpu/shader/execution/mutation/mesa/uniform_input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
export const description = `
Execution tests that read a uniform buffer, found by mutation testing Mesa.

Each case is a compute shader generated by the WGSL fuzzer WGSLsmith and then reduced
automatically. They were found by mutation testing a shader compiler: each program produces
different output when a specific change is injected into the compiler, and no existing CTS test
detected that change.

Each shader writes its result to a storage buffer at @group(0) @binding(1).
Each also reads a uniform buffer at @group(0) @binding(0), filled with the fuzzer's random
input (cut to the size of the binding).
Input and expected output are given as bytes, in the form the fuzzer reports them,
with a comment showing the values of each struct member.

Case names are "shader_" followed by the first 8 hex digits of the SHA-256 hash of the
shader text. kTestData holds each case's shader, its input and expected output, and the
mutation it detects.
`;

import { makeTestGroup } from '../../../../../common/framework/test_group.js';
import { keysOf } from '../../../../../common/util/data_tables.js';
import { AllFeaturesMaxLimitsGPUTest, GPUTest } from '../../../../gpu_test.js';
import { MutationInfo } from '../mutation_info.js';

export const g = makeTestGroup(AllFeaturesMaxLimitsGPUTest);

interface TestData {
/** Where the mutation this test detects was made. */
mutation: MutationInfo;
/** The shader. */
code: string;
/** The bytes of the uniform buffer. */
input: number[];
/** The expected bytes of the output buffer. */
expectedOutput: number[];
}

/**
* Runs a shader (entry point `main`, one invocation) and checks the contents of its output
* buffer.
*
* @param t The test object
* @param buffers `input`: the bytes of the uniform buffer at @group(0) @binding(0).
* `expectedOutput`: the expected bytes of the storage buffer at @group(0) @binding(1). The buffer
* starts zero-filled, so padding bytes are expected to be 0.
* @param wgsl The shader
*/
function runGeneratedTest(
t: GPUTest,
buffers: { input: Uint8Array; expectedOutput: Uint8Array },
wgsl: string
) {
const pipeline = t.device.createComputePipeline({
layout: 'auto',
compute: {
module: t.device.createShaderModule({ code: wgsl }),
entryPoint: 'main',
},
});

const inputBuffer = t.makeBufferWithContents(buffers.input, GPUBufferUsage.UNIFORM);
// New buffers are zero-filled.
const outputBuffer = t.createBufferTracked({
size: buffers.expectedOutput.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
});
const bindGroup = t.device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: { buffer: inputBuffer } },
{ binding: 1, resource: { buffer: outputBuffer } },
],
});

// Run the shader.
const encoder = t.device.createCommandEncoder();
const pass = encoder.beginComputePass();
pass.setPipeline(pipeline);
pass.setBindGroup(0, bindGroup);
pass.dispatchWorkgroups(1);
pass.end();
t.queue.submit([encoder.finish()]);

t.expectGPUBufferValuesEqual(outputBuffer, buffers.expectedOutput);
}

const kTestData: Record<string, TestData> = {
shader_429a8325: {
mutation: {
mutant: {
component: 'mesa',
version: 'ba8eaab4f07e33c0b74fa92c60852cba2518bf2e',
file: 'src/compiler/nir/nir_constant_expressions.c',
line: 51437,
generatedFrom: 'src/compiler/nir/nir_constant_expressions.py',
},
environment: [{ component: 'dawn', version: '9de0fd67086127228e8aa28e69616da37220cfc3' }],
},
code: `
struct UniformBuffer {
b: vec4<u32>,
c: u32,
}

struct StorageBuffer {
b: f32,
c: vec2<i32>,
}

@group(0) @binding(0) var<uniform> u_input: UniformBuffer;

@group(0) @binding(1) var<storage, read_write> s_output: StorageBuffer;

var<private> LOOP_COUNTERS: array<u32, 1>;

@compute
@workgroup_size(1i)
fn main(@builtin(local_invocation_id) builtin_local_invocation_id: vec3<u32>) {
while ((max(vec2<u32>(), u_input.b.yz).x < u_input.c)) {
if (LOOP_COUNTERS[0u] >= 1u) {
break;
}
(LOOP_COUNTERS)[0u] = LOOP_COUNTERS[0u] + 1u;
}
s_output = StorageBuffer(6f, extractBits(vec3<i32>(1i), builtin_local_invocation_id.x, 5u).zz);
}
`,
// u_input: b = (2558390668, 1882584399, 2302723328, 2974181364), c = 1672505790,
// then 12 bytes of padding
input: [
140, 241, 125, 152, 79, 245, 53, 112, 0, 197, 64, 137, 244, 103, 70, 177, 190, 105, 176, 99,
182, 225, 107, 28, 89, 82, 1, 211, 34, 217, 48, 22,
],
// s_output: b = 6, c = (1, 1) (padding bytes are 0)
expectedOutput: [0, 0, 192, 64, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
},
shader_7be24930: {
mutation: {
mutant: {
component: 'mesa',
version: 'ba8eaab4f07e33c0b74fa92c60852cba2518bf2e',
file: 'src/compiler/nir/nir_opt_if.c',
line: 839,
},
environment: [{ component: 'dawn', version: '9de0fd67086127228e8aa28e69616da37220cfc3' }],
},
code: `
struct UniformBuffer {
b: vec3<i32>,
}

struct StorageBuffer {
a: mat3x2<f32>,
b: vec4<i32>,
}

@group(0) @binding(0) var<uniform> u_input: UniformBuffer;

@group(0) @binding(1) var<storage, read_write> s_output: StorageBuffer;

fn f(v: vec3<f32>) -> vec3<f32> {
return select(v, vec3<f32>(10f), any((abs(v) < vec3<f32>(0.1f))) || any(abs(v) >= vec3<f32>(16777216f)));
}

fn g(v: vec4<f32>) -> vec4<f32> {
return select(v, vec4<f32>(10f), any((abs(v) < vec4<f32>(0.1f))) || any(abs(v) >= vec4<f32>(16777216f)));
}

fn h() -> mat3x2<f32> {
switch (7i) {
case 2i: {
}
case 1i: {
return mat3x2<f32>(vec2<f32>(), f(step(f(-vec3<f32>()), vec3<f32>())).zx, vec2<f32>());
}
default: {
return mat3x2<f32>(vec2<f32>(1f, 1f), vec4<f32>().ww, g(select(vec4<f32>(), vec4<f32>(), u_input.b.x <= u_input.b.x)).zz);
}
}
return mat3x2<f32>();
}

@compute
@workgroup_size(1i)
fn main() {
s_output = StorageBuffer(h(), vec4<i32>());
}
`,
// u_input: b = (-432204898, 845770051, 1736441713), then 4 bytes of padding
input: [158, 19, 61, 230, 67, 109, 105, 50, 113, 255, 127, 103, 120, 80, 165, 3],
// s_output: a = ((1, 1), (0, 0), (10, 10)), b = (0, 0, 0, 0)
// (padding bytes are 0)
expectedOutput: [
0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 65, 0, 0, 32, 65, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
},
shader_7ea91b04: {
mutation: {
mutant: {
component: 'mesa',
version: 'ba8eaab4f07e33c0b74fa92c60852cba2518bf2e',
file: 'src/compiler/nir/nir_range_analysis.c',
line: 1178,
},
environment: [{ component: 'dawn', version: '9de0fd67086127228e8aa28e69616da37220cfc3' }],
},
code: `
struct UniformBuffer {
c: vec2<u32>,
}

struct StorageBuffer {
a: vec3<f32>,
c: f32,
}

@group(0) @binding(0) var<uniform> u_input: UniformBuffer;

@group(0) @binding(1) var<storage, read_write> s_output: StorageBuffer;

fn f(v: vec3<f32>) -> vec3<f32> {
return select(v, vec3<f32>(10f), any((abs(v) < vec3<f32>(0.1f))) || any(abs(v) >= vec3<f32>(16777216f)));
}

@compute
@workgroup_size(1i)
fn main() {
s_output = StorageBuffer(f(round(f(vec3<f32>(-7f) + mat4x4<f32>()[u_input.c.x % 4u].zzy))), vec4<f32>().x);
}
`,
// u_input: c = (1437411845, 2007253413)
input: [5, 42, 173, 85, 165, 65, 164, 119],
// s_output: a = (-7, -7, -7), c = 0
expectedOutput: [0, 0, 224, 192, 0, 0, 224, 192, 0, 0, 224, 192, 0, 0, 0, 0],
},
};

g.test('uniform_input')
.desc(`Tests compute shaders that read a uniform buffer, found by mutation testing Mesa.`)
.params(u => u.combine('case', keysOf(kTestData)))
.fn(t => {
const c = kTestData[t.params.case];
runGeneratedTest(
t,
{ input: new Uint8Array(c.input), expectedOutput: new Uint8Array(c.expectedOutput) },
c.code
);
});
Loading
Loading