Switch branches/tags
v1.0 v-ffddabfa41becfe4a2984b895551f931d68df17c v-fe91303a9ec155314351099030ce37ff930e477f v-fd50964c42a6fa4c08fe23b2d8b422e4a4592911 v-fab39bac6661ed35c1e79cd7e8049663a7fccf8e v-f645af136d86d42b78dea70c1f82bc061a99efe1 v-f3c0a7655d97ebc941018091ff2d937bf98c9ab6 v-e5822cd673f9972283a2d1b9b5b27a0261d6dda6 v-e695a8425d588bfc2de34d232078c26e0c0638e9 v-e43c4ef9002082730f1bdc1d4c6440a5a80e990c v-df699a5d36569b44cdb9b51558ff96087f699c2c v-debfea1a29bca81f54793c1a3518e61dc56a47a0 v-d33168884b3b5c91c5e4af5cce68f8da79579da4 v-d99d6187cab5be425c0a74f55d2c83f2cf0ef4e7 v-d92b6ed85f532ff6a50c9a2413db29d21e39e64f v-cee68e544944c82c71159a9f156e82893c1266d4 v-ce30f407dcbacaf08413b1548fb684fe378e7ac4 v-cd9dc7334bbdbfd823b6c1c0d6a299f6564b9685 v-cc762c78da96ad005e843e728c2440c2e9843e60 v-cb07c7bfe8cdfda2163f80b87eb0bec780247ec6 v-c03f81d02bfd081e61183a2a0355025e28f42385 v-c1afc425102effc3cbbcf695ef23b16824fd030a v-c69704203431978a525fcfc9172cb5dd7dbe9f72 v-bb8a863508ae342e499614458e1f6e39cc3feabd v-ba4e3810d0088a56381f01484979eba56df61496 v-b30fea92b4e98f5d02fd048bce4a6efd91515c5a v-afc6ff1c065092113c2730184821bd3a90481c4b v-af1d13122cbc2b006434d910f494457988927d35 v-abbdeee5589c3c9dc90d4059e567fd5a80dd6481 v-aa3c7c96ad0c94f1bf46d69a88fb69023d7cb897 v-a914a40825123194638f3f27e10898ec9ea530f2 v-a5b4356c981416e7a55d43d585921266c64331a1 v-a1b1c9918c1c71c075a9b5c0e548a63b88fceef5 v-537955550d8b19921b6f431db21a9064296ecc57 v-213485282e0796588b78875571099de239d7ffb0 v-3141663aa21adcab528a6a2c5765bf08cc3e76cc v-800127c434bad7372bd8102bd9f94b1b1d3b8bbe v-486298ed773fd21159e53c08b9999a9a5f3aa6ca v-230203ed4c07a82fa4bb01ca87a8e0f621fc820e v-27082d931d8aec38c70b8a26d594f65cc513013f v-23302d3b124f416509b4968f572a9a2e6cd59db3 v-9224f9446f434a7581a8060c099ab3d8e7497068 v-6143a8805cd6bbcc04d351639c97f8b9f099e3ef v-4294d6972feb8b6cf7d97d42ceeb51cc6b7768bf v-03803c906975ddb74f7202550b637b3d9e1f961e v-3778e5feb659597f8845006be9606adcd3447634 v-831eb89260aad4cff131218de7174bd49d4df36a v-636fa43606419c05fecec50fef5bd2ea60678ce5 v-622e90875910cca608c977d86dd0e3fe05c498fa v-622e2b82223777c28037680db76457f4554f9809 v-617ecd45217c901790ad0b169610774198dc7e29 v-279ad9d1710adc9e5a2e81a0f14c9b1e816e631c v-97ff09a46c881f77d1bb2e94398bc40da6f7f926 v-95b4fb5be906f8a5b8ab03c029fc19c9b0ed3167 v-86af3306f967b1cebe4c8e9ff8a491c926b25f09 v-70a8eddd7f0a672c79c37bcbdb910eabbca7ceb4 v-066b518b8f16be2853d9927116d0d6f85c88c2ca v-60a2b7defac79acd3cb1d4e6d92a51b26b2b2317 v-48caa8864525d8fb88abd00b1ae2df58d3cb5a53 v-045e96f8777a127cdfbdf12691b8c3b5fc070889 v-43a0dac192101688dba4e6ceacdf4c51f25ae2fe v-38f2945d588bf739e00f14f32ece319e749d2e18 v-33b4180778da068f58e67706d38c18d48d5b104d v-11ced28337059eb2a9c407ffbc79f82735635bde v-11b164a9310207e9cab5fd05c8501dea2a7b9ba0 v-9e12a140346c9b7eb663fe4a37ad00bfdfd031a3 v-9c95a7792bf03e7e56730a9ba7e412d41bf4c8a7 v-8f7f621ce259863bbfa8a34f10c30d30f7cbeff5 v-8bfabc6fd3314c008a2c630d6c1fbac28586bea5 v-8be19987e17520b1a44b97d1383da9b32a0747cb v-7fa7d3a1b3a480bed52f3287fef9a6a9d8fb082a v-5f0d136d979f2050def3725f4a6c84ba3323ba29 v-5d6efc9cb4b0957285a7702a46d57c87a376e434 v-5a245ff0a1ec85ceff0250a4495b821eef2692bf v-4eb8193d23c56f66faeaff4c0bf38ef4356ea496 v-4eb3cd7eaa725f9ba1c22b193c3c5fab3b8bfdfa v-4cd585359f82fe9bbfa40a3322d0d9156abf59e9 v-4ae863058386641d7c58b816dbe9251f83c2f519 v-3eb8619929f4060c368e186eedd571162a8fc5f1 v-3db3fa37614fe9d991966f46d7ae2eae7c8b0e2d v-3ccb0523137ef91f956996eb915267f77ab79aa1 v-3c841aee3c8c9d54e122c9188923b17e2503a6ab v-2ed43c3bb1bb66641daf0749339252bbc9c6134b v-02ed0b3cc0f5a53a426e79dec8dc7a2ea052a883 v-2a8d721fa76138a256762bc8cf9818bf026a87f6 v-1e794e1b6dbfdf4ec68cf65dc5abadab73d87dd4 v-1d1ad1bdbaf6fa24269484916a8bfd5bd9e7d27e v-0b165f3b846764721d0c51f238caa35aa56ee188
Nothing to show
Find file Copy path
07e1754 Nov 30, 2018
1 contributor

Users who have contributed to this file

169 lines (111 sloc) 8.66 KB


glsl-reduce is a tool for automatically reducing a GLSL shader with respect to a programmable condition.

In this walkthrough, we will use a mock shader compiler bug to demonstrate how to use glsl-reduce in action.

We will be using the latest release zip You can download this from the releases page or build it from source.

Add the following directories to your path:

  • graphicsfuzz-1.0/python/drivers
  • One of:
    • graphicsfuzz-1.0/bin/Linux
    • graphicsfuzz-1.0/bin/Mac
    • graphicsfuzz-1.0/bin/Windows

The graphicsfuzz-1.0/ directory is the unzipped graphicsfuzz release. If building from source, this directory can be found at graphicsfuzz/target/graphicsfuzz-1.0/.

You will also need to install the latest version of the Java 8 Development Kit, either:

# To check that Java 8 is installed and in use:
java -version
# Output: openjdk version "1.8.0_181"

glsl-reduce in action

We'll start by illustrating a mock shader compiler bug. We present commands assuming a Linux/Mac environment, but they should be easy to adapt to a Windows setting.

# Copy the sample shaders into the current directory:
cp -r graphicsfuzz-1.0/examples/glsl-reduce-walkthrough .

# Run the fake shader compiler on the fragment shader file:
python glsl-reduce-walkthrough/ glsl-reduce-walkthrough/colorgrid_modulo.frag

# Output:
# Fatal error: too much indexing.

Our fake compiler fails to compile the valid shader because it cannot handle shaders with a lot of indexing. Thus, we have found a compiler bug.

Take a look at if you want to see why the fake compiler generates this error message for the shader. This is not important for understanding how to use the reducer.

Let's now use glsl-reduce to get a much smaller shader that causes the compiler to fail with this error:

# Observe that the initial shader is reasonably large
cat glsl-reduce-walkthrough/colorgrid_modulo.frag

# Output:
# <contents of the original shader>

# Run glsl-reduce, putting the tool's output in the 'reduction_results' directory (created if it does not exist)
glsl-reduce glsl-reduce-walkthrough/colorgrid_modulo.json ./glsl-reduce-walkthrough/interestingness_test --output reduction_results

# Output:
# <lots of messages about the reducer's progress>

# Confirm that the reduced fragment shader file still reproduces the issue
python glsl-reduce-walkthrough/ reduction_results/colorgrid_modulo_reduced_final.frag

# Output:
# Fatal error: too much indexing.

# Observe that the reduced shader is much smaller
cat reduction_results/colorgrid_modulo_reduced_final.frag

# Output:
# <contents of the reduced shader>

In reduction_results you will also find many partially-reduced shader jobs named colorgrid_modulo_reduced_X_Y.json, where Y is success or fail depending on whether the shader job was interesting, and X is a counter associated with each shader job, indicating at what point during the reduction process it was considered. These shader jobs are sometimes useful for understanding the steps that were made during a reduction, but can otherwise be ignored.

In more detail

glsl-reduce requires two arguments:

  • a shader job: a path to a .json file representing the shader or shaders of interest
  • an interestingness test: a path to an executable script encoding the properties a shader job must satisfy to be deemed interesting.

It is advisable to also use the --output option to specify a directory for generated results; this will be the current directory by default.

The .json file serves two purposes: (1) it tells glsl-reduce which shaders to operate on -- if it is called foo.json then at least one of foo.frag, foo.vert and foo.comp must be present, and any that are present will be reduced; (2) it allows metadata about these shaders to be stored. At present, glsl-reduce does not allow any metadata when used in stand-alone mode, so the JSON file is required to simply contain {}. (The file is used for meaningful metadata when glsl-reduce is used in conjunction with glsl-fuzz.)

The interestingness test (name inspired by C-Reduce) is a script that codifies what it means for a shader to be interesting. It takes the name of a shader job as an argument, and should return 0 if and only if the shaders associated with the shader job are interesting. What "interesting" means is specific to the context in which you are using glsl-reduce.

In the above example we deemed a shader job foo.json to be interesting if the associated fragment shader foo.frag caused the fake compiler to fail with the "too much indexing" message. Have a look at glsl-reduce-walkthrough/ (which is invoked by glsl-reduce-walkthrough/interestingness_test for Linux/Mac, and the corresponding .bat file for Windows): it checks precisely this.

In a different setting we might be interested in reducing with respect to a completely different property, e.g. we might check whether rendering using a given shader causes a device's temperature to exceed some threshold, whether a shader causes a driver to consume more than a certain amount of memory, or whether a shader sends a shader compiler into a seemingly infinite loop. Writing the interestingness test is entirely in the hands of the user of glsl-reduce. Notice that in each of these examples, the property we care about (temperature, memory or execution time) is non-binary; it needs to be turned into a binary property by using an appropriate limit on the resource in question inside the interestingness test.

Bug slippage: the need for a strong interestingness test

Let's try our reduction again but with a different interestingness test. Have a look at glsl-reduce-walkthrough/ This test just checks whether compilation failed; it does not check the failure string.

Let's use it to perform a reduction:

# Run glsl-reduce
glsl-reduce glsl-reduce-walkthrough/colorgrid_modulo.json ./glsl-reduce-walkthrough/weak_interestingness_test --output slipped_reduction_results

# Output:
# <lots of messages about the reducer's progress>

# Whoah, it's an even smaller result!
cat slipped_reduction_results/colorgrid_modulo_reduced_final.frag

# Output:
# <a shader with an empty main>

# Does it still give us the fatal error?
python glsl-reduce-walkthrough/ reduction_results/colorgrid_modulo_reduced_final.frag

# Output:
# Internal error: something went wrong inlining 'floor'.

The reduced shader does cause a bug to trigger in the fake compiler, but it is a different bug to the one we saw before: we get a different error message! (Look in glsl-reduce-walkthrough/ to see why this message is generated; this is of course just a mock bug made up for this illustrative walkthrough.)

This is known as bug slippage (see Section 3.5 of this paper): in reducing a shader that triggers one bug we have ended up triggering a second bug and switching attention to that second bug. What we end up with is a minimized shader that triggers the second bug but not the original bug.

Slippage can be useful as a method for discovering additional bugs (see this blog post on the topic), and the series of partially-reduced shader jobs produced during reduction sometimes provides a rich source of by-product bugs. However, slippage is annoying if we really care about reducing with respect to the original bug. To avoid slippage, you need to write a sufficiently strong interestingness test.

Summary from walkthrough

The walkthrough has illustrated:

  • How to run glsl-reduce on an example
  • What the arguments to glsl-reduce mean, including the concept of an interestingness test
  • The notion of bug slippage, whereby a weak interestingness test can cause reduction to switch from an original bug to some other bug

We emphasize again that the interestingness test is entirely programmable. In this tutorial it has simply involved invoking a (fake) compiler and checking the compiler's output. In practice it could do something much more complex, such as launching an app that will consume the shader of interest and then checking for some specific app behavior.