SPIRVSmith is a differential testing tool that leverages structured fuzzing techniques to find bugs in producers and consumers of SPIRV shaders.
SPIRVSmith attempts to find bugs in the following projects:
- SPIRV-Tools - mostly the validator and reducer components.
- SPIRV-Cross
- Amber
- SwiftShader
- MoltenVK
- ...and more to come!
SPIRVSmith uses poetry
to manage Python dependencies and ships with scripts to install external dependencies.
-
Follow the poetry installation instructions for your platform.
-
Grab a local copy of SPIRVSmith:
$ git clone https://github.com/rayanht/SPIRVSmith.git && cd SPIRVSmith
- Install Python dependencies using
poetry
and start apoetry
shell:
$ poetry install && poetry shell
- Install the external dependencies:
$ mkdir bin
$ sh scripts/get_spirv_tools.sh <platform>
Replace <platform>
by either linux
or macos
(No support for Windows π)
The first step is to run SPIRVSmith
is to head to config.py
. That file contains the various parameters that are used to dictate the behaviour of the fuzzer.
SPIRVSmith
is highly parametrizable, you can choose to disable certain features of the SPIR-V language (e.g. do not emit any control flow operation), limit the number of global constants generated, favour the generation of certain kinds of instructions etc.
Once you are happy with your parametrization, make sure you are in a poetry
virtual environment ($ poetry shell
) and run SPIRVSmith
:
$ sh scripts/run.sh
SPIR-V assembly files will be saved as they are generated to the out/
directory and the fuzzer can be stopped at any time by pressing Ctrl+C
.
The bread and butter of SPIRVSmith
is differential testing (sometimes called differential fuzzing), in which we provide the same SPIRV shader to similar consumers (say three different SPIRV compilers for example), execute the three resulting programs and compare the values contained inside all buffers at the end of exeuction.
In a fully deterministic program (== synchronous && free of undefined behaviour)
, we expect all these buffers to be exactly the same at the end of execution, regardless of what compiler was used to generate said program. If one program ends up with different buffers than the other two, we have a strong reason to believe that the compiler that generated it has a bug.
This concept can be extended further by varying the platform that executes the shader. If we get different results by running the same shader on an Nvidia GPU, an AMD GPU, and an Intel integrated graphics chip then there is a good chance that either the underlying Vulkan engine or the GPU driver has a bug (possibly both).
The constraint on determinism creates an interesting problem, how can we ensure that the randomly generated programs are free of undefined behaviour? Unlike existing differential testing tools, SPIRVSmith
does not perform any static analysis or backtracking at generation-time to enforce this constraint, we rather implement the idea of program reconditioning by Donaldson et al.
Donaldson et al. introduce the idea of program reconditioning as "a method for leveraging off-the-shelf test case reducers to simplify programs that expose miscompilation bugs during randomised differential testing".
This approach solves the issues raised by Yang et al. in the case of CSmith where test case reducers couldn't be used to provide concise, actionable bug reports since they would themselves often introduce undefined behaviour.
Program reconditioning works by decoupling the process of generating a program and the process of ensuring that said program is free of undefined behaviour. This is in contrast to Livinskii et al. in YARPGen where code generation steps and static analysis steps are interleaved.
Donaldson et al. describe a rule-based reconditioning approach where transforms are applied to constructs that could exhibit undefined behaviour. Transforms are applied to all eligible construct in a blanket manner, no static analysis is performed to determine which constructs are in fact worth reconditioning. Here is example of reconditioning a GLSL shader:
float A [3]; // Not initialised
void main () {
int i = int (A[0]);
float f = 2000000.0;
while (i != -42) { // Might not terminate
A[i] = f; // Out of bounds ?
f = f + f; // Roundoff
int j = i ++ + ( i / ( i - 1)); // Order of side effects, divide by zero?
i = j;
}
}
// [ Declarations of SAFE_ABS , MAKE_IN_RANGE and SAFE_DIV ]
uint _loop_count = 0u;
const uint _loop_limit = 100u;
float A [3] = float [3](1.0 , 1.0 , 1.0);
void main () {
int i = int (A[0]);
float f = 2000000.0;
while (i != -42) {
if (_loop_count >= _loop_limit) break;
_loop_count ++;
A[SAFE_ABS(i) % 3] = f ;
f = MAKE_IN_RANGE(f + f);
int _t = SAFE_DIV(i , i - 1);
int j = i ++ + _t;
i = j;
}
}
Expand
Expand
OpCode | Status |
---|---|
OpNop | π΄ |
OpUndef | π΄ |
OpSizeOf | π΄ |
Expand
OpCode | Status |
---|---|
OpSourceContinued | π΄ |
OpSource | π΄ |
OpSourceExtension | π΄ |
OpName | π΄ |
OpMemberName | π΄ |
OpString | π΄ |
OpLine | π΄ |
OpNoLine | π΄ |
OpModuleProcessed | π΄ |
Expand
OpCode | Status |
---|---|
OpDecorate | β |
OpMemberDecorate | β |
OpDecorationGroup | π΄ |
OpGroupDecorate | π΄ |
OpGroupMemberDecorate | π΄ |
OpDecorateId | π΄ |
OpDecorateString | π΄ |
OpMemberDecorateString | π΄ |
Expand
OpCode | Status |
---|---|
OpExtension | β |
OpExtInstImport | β |
OpExtInst | β |
Expand
OpCode | Status |
---|---|
OpMemoryModel | β |
OpEntryPoint | β |
OpExecutionMode | β |
OpCapability | β |
OpExecutionModeId | π΄ |
Expand
OpCode | Status |
---|---|
OpTypeVoid | β |
OpTypeBool | β |
OpTypeInt | β |
OpTypeFloat | β |
OpTypeVector | β |
OpTypeMatrix | β |
OpTypeImage | π΄ |
OpTypeSampler | π΄ |
OpTypeSampledImage | π΄ |
OpTypeArray | β |
OpTypeRuntimeArray | π΄ |
OpTypeStruct | β |
OpTypeOpaque | π΄ |
OpTypePointer | β |
OpTypeFunction | β |
Expand
OpCode | Status |
---|---|
OpConstantTrue | β |
OpConstantFalse | β |
OpConstant | β |
OpConstantComposite | β |
OpConstantSampler | π΄ |
OpConstantNull | π΄ |
OpSpecConstantTrue | π΄ |
OpSpecConstantFalse | π΄ |
OpSpecConstant | π΄ |
OpSpecConstantComposite | π΄ |
OpSpecConstantOp | π΄ |
Expand
OpCode | Status |
---|---|
OpVariable | β |
OpImageTexelPointer | π΄ |
OpLoad | β |
OpStore | β |
OpCopyMemory | π΄ |
OpCopyMemorySized | π΄ |
OpAccessChain | β |
OpInBoundsAccessChain | π΄ |
OpPtrAccessChain | π΄ |
OpPtrEqual | π΄ |
OpPtrNotEqual | π΄ |
OpPtrDiff | π΄ |
Expand
OpCode | Status |
---|---|
OpFunction | β |
OpFunctionParameter | β |
OpFunctionEnd | β |
OpFunctionCall | π΄ |
Expand
OpCode | Status |
---|---|
OpSampledImage | π΄ |
OpImageSampleImplicitLod | π΄ |
OpImageSampleExplicitLod | π΄ |
OpImageSampleDrefImplicitLod | π΄ |
OpImageSampleDrefExplicitLod | π΄ |
OpImageSampleProjImplicitLod | π΄ |
OpImageSampleProjExplicitLod | π΄ |
OpImageSampleProjDrefImplicitLod | π΄ |
OpImageSampleProjDrefExplicitLod | π΄ |
OpImageFetch | π΄ |
OpImageGather | π΄ |
OpImageDrefGather | π΄ |
OpImageRead | π΄ |
OpImageWrite | π΄ |
OpImage | π΄ |
OpImageQueryFormat | π΄ |
OpImageQueryOrder | π΄ |
OpImageQuerySizeLod | π΄ |
OpImageQuerySize | π΄ |
OpImageQueryLod | π΄ |
OpImageQueryLevels | π΄ |
OpImageQuerySamples | π΄ |
OpImageSparseSampleImplicitLod | π΄ |
OpImageSparseSampleExplicitLod | π΄ |
OpImageSparseSampleDrefImplicitLod | π΄ |
OpImageSparseSampleDrefExplicitLod | π΄ |
OpImageSparseFetch | π΄ |
OpImageSparseGather | π΄ |
OpImageSparseDrefGather | π΄ |
OpImageSparseTexelsResident | π΄ |
OpImageSparseRead | π΄ |
Expand
OpCode | Status |
---|---|
OpConvertFToU | β |
OpConvertFToS | β |
OpConvertSToF | β |
OpConvertUToF | β |
OpUConvert | π΄ |
OpSConvert | π΄ |
OpFConvert | π΄ |
OpQuantizeToF16 | π΄ |
OpConvertPtrToU | π΄ |
OpSatConvertSToU | π΄ |
OpSatConvertUToS | π΄ |
OpConvertUToPtr | π΄ |
OpPtrCastToGeneric | π΄ |
OpGenericCastToPtr | π΄ |
OpGenericCastToPtrExplicit | π΄ |
OpBitcast | π΄ |
Expand
OpCode | Status |
---|---|
OpVectorExtractDynamic | β |
OpVectorInsertDynamic | β |
OpVectorShuffle | β |
OpCompositeConstruct | π΄ |
OpCompositeExtract | β |
OpCompositeInsert | β |
OpCopyObject | β |
OpTranspose | β |
OpCopyLogical | π΄ |
Expand
OpCode | Status |
---|---|
OpSNegate | β |
OpFNegate | β |
OpIAdd | β |
OpFAdd | β |
OpISub | β |
OpFSub | β |
OpIMul | β |
OpFMul | β |
OpUDiv | β |
OpSDiv | β |
OpFDiv | β |
OpUMod | β |
OpSRem | β |
OpSMod | β |
OpFRem | β |
OpFMod | β |
OpVectorTimesScalar | β |
OpMatrixTimesScalar | β |
OpVectorTimesMatrix | β |
OpMatrixTimesVector | β |
OpMatrixTimesMatrix | β |
OpOuterProduct | β |
OpDot | β |
OpIAddCarry | π΄ |
OpISubBorrow | π΄ |
OpUMulExtended | π΄ |
OpSMulExtended | π΄ |
Expand
OpCode | Status |
---|---|
OpShiftRightLogical | β |
OpShiftRightArithmetic | β |
OpShiftLeftLogical | β |
OpBitwiseOr | β |
OpBitwiseXor | β |
OpBitwiseAnd | β |
OpNot | β |
OpBitFieldInsert | β |
OpBitFieldSExtract | β |
OpBitFieldUExtract | β |
OpBitReverse | β |
OpBitCount | β |
Expand
OpCode | Status |
---|---|
OpAny | β |
OpAll | β |
OpIsNan | β |
OpIsInf | β |
OpIsFinite | π΄ |
OpIsNormal | π΄ |
OpSignBitSet | π΄ |
OpOrdered | π΄ |
OpUnordered | π΄ |
OpLogicalEqual | β |
OpLogicalNotEqual | β |
OpLogicalOr | β |
OpLogicalAnd | β |
OpLogicalNot | β |
OpSelect | β |
OpIEqual | β |
OpINotEqual | β |
OpUGreaterThan | β |
OpSGreaterThan | β |
OpUGreaterThanEqual | β |
OpSGreaterThanEqual | β |
OpULessThan | β |
OpSLessThan | β |
OpULessThanEqual | β |
OpSLessThanEqual | β |
OpFOrdEqual | β |
OpFUnordEqual | β |
OpFOrdNotEqual | β |
OpFUnordNotEqual | β |
OpFOrdLessThan | β |
OpFUnordLessThan | β |
OpFOrdGreaterThan | β |
OpFUnordGreaterThan | β |
OpFOrdLessThanEqual | β |
OpFUnordLessThanEqual | β |
OpFOrdGreaterThanEqual | β |
OpFUnordGreaterThanEqual | β |
Expand
OpCode | Status |
---|---|
OpPhi | π΄ |
OpLoopMerge | β |
OpSelectionMerge | β |
OpLabel | β |
OpBranch | β |
OpBranchConditional | β |
OpSwitch | π΄ |
OpReturn | π΄ |
OpReturnValue | π΄ |
Encountered a bug? Have an idea for a new feature? This project is open to all
sorts of contribution! Feel free to head to the Issues
tab and describe your
request!
This project is licensed under the Apache 2.0 license.