diff --git a/src/webgpu/shader/execution/mutation/mesa/no_input.spec.ts b/src/webgpu/shader/execution/mutation/mesa/no_input.spec.ts new file mode 100644 index 000000000000..fbe1e97989ce --- /dev/null +++ b/src/webgpu/shader/execution/mutation/mesa/no_input.spec.ts @@ -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 = { + 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, +} + +@group(0) @binding(1) var s_output: StorageBuffer; + +var v: array; + +@compute +@workgroup_size(1i) +fn main() { + s_output = StorageBuffer(max(vec3(), vec3(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 s_output: StorageBuffer; + +var v: array = array(1i, 8i, 4i, 8i, 0i); + +@compute +@workgroup_size(1i) +fn main(@builtin(local_invocation_id) builtin_local_invocation_id: vec3) { + s_output = StorageBuffer(v[countTrailingZeros(vec2(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); + }); diff --git a/src/webgpu/shader/execution/mutation/mesa/uniform_input.spec.ts b/src/webgpu/shader/execution/mutation/mesa/uniform_input.spec.ts new file mode 100644 index 000000000000..14df40b34f79 --- /dev/null +++ b/src/webgpu/shader/execution/mutation/mesa/uniform_input.spec.ts @@ -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 = { + 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, + c: u32, +} + +struct StorageBuffer { + b: f32, + c: vec2, +} + +@group(0) @binding(0) var u_input: UniformBuffer; + +@group(0) @binding(1) var s_output: StorageBuffer; + +var LOOP_COUNTERS: array; + +@compute +@workgroup_size(1i) +fn main(@builtin(local_invocation_id) builtin_local_invocation_id: vec3) { + while ((max(vec2(), 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(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, +} + +struct StorageBuffer { + a: mat3x2, + b: vec4, +} + +@group(0) @binding(0) var u_input: UniformBuffer; + +@group(0) @binding(1) var s_output: StorageBuffer; + +fn f(v: vec3) -> vec3 { + return select(v, vec3(10f), any((abs(v) < vec3(0.1f))) || any(abs(v) >= vec3(16777216f))); +} + +fn g(v: vec4) -> vec4 { + return select(v, vec4(10f), any((abs(v) < vec4(0.1f))) || any(abs(v) >= vec4(16777216f))); +} + +fn h() -> mat3x2 { + switch (7i) { + case 2i: { + } + case 1i: { + return mat3x2(vec2(), f(step(f(-vec3()), vec3())).zx, vec2()); + } + default: { + return mat3x2(vec2(1f, 1f), vec4().ww, g(select(vec4(), vec4(), u_input.b.x <= u_input.b.x)).zz); + } + } + return mat3x2(); +} + +@compute +@workgroup_size(1i) +fn main() { + s_output = StorageBuffer(h(), vec4()); +} +`, + // 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, +} + +struct StorageBuffer { + a: vec3, + c: f32, +} + +@group(0) @binding(0) var u_input: UniformBuffer; + +@group(0) @binding(1) var s_output: StorageBuffer; + +fn f(v: vec3) -> vec3 { + return select(v, vec3(10f), any((abs(v) < vec3(0.1f))) || any(abs(v) >= vec3(16777216f))); +} + +@compute +@workgroup_size(1i) +fn main() { + s_output = StorageBuffer(f(round(f(vec3(-7f) + mat4x4()[u_input.c.x % 4u].zzy))), vec4().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 + ); + }); diff --git a/src/webgpu/shader/execution/mutation/mutation_info.ts b/src/webgpu/shader/execution/mutation/mutation_info.ts new file mode 100644 index 000000000000..cde358fe8fe4 --- /dev/null +++ b/src/webgpu/shader/execution/mutation/mutation_info.ts @@ -0,0 +1,29 @@ +/** + * Provenance for tests found by mutation testing. + * + * Each test was found by deliberately injecting a small change (a "mutant") into one component's + * source (e.g. the Mesa NIR compiler) and searching for a shader whose result differs between the + * mutated and unmutated builds. The rest of the CTS did not detect these mutants, which means that + * the tests check behaviour that existing tests did not. + */ + +/** A component of the WebGPU stack used to run the test, e.g. Dawn or Mesa. */ +export interface ComponentVersion { + component: string; + /** Git commit in the component's repository. */ + version: string; +} + +/** The configuration in which a test detected a mutant. */ +export interface MutationInfo { + /** The mutated component, at the commit the mutation was applied to. */ + mutant: ComponentVersion & { + /** Location of the mutation in the component's source. */ + file: string; + line: number; + /** Set if `file` is generated during the build: the script that generates it. */ + generatedFrom?: string; + }; + /** Other components used unmodified to run the test (e.g. Dawn). */ + environment: ComponentVersion[]; +}