Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Fuzzing in a CI pipeline

After this lesson: I can add a short fuzz job to a CI pipeline that fails when a fixed bug returns.

60 min lesson9 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

The bug that came back in March

In January the team fixed the unclosed-quote bug. In March somebody tidied the loop and put <= back. Nobody noticed for weeks.

A pipeline that replays the old crash on every change would have refused the edit in minutes.

The one rule

Warning

A pipeline fuzzes only your own code, on machines you are allowed to use. Pointing a fuzz job at somebody else's service needs their written permission. In the UK the Computer Misuse Act 1990 applies.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can add a short fuzz job to a CI pipeline that fails when a fixed bug returns.

Key words

WordWhat it meansExample
Pipelinethe checks that run on every changemake ci
Jobone step of a pipeline
Regression suiteall the saved inputs that once broke the code
Smoke runa short fuzz run to catch obvious failures
Artifacta file a pipeline keeps after it runscrash-*

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A pipeline is a gate every change passes

Write lesson 2's reader, tests and harness again, so this lesson stands on its own. Then fix the loop and keep the unclosed-quote input in regress.

If you did lesson 2 on this copy, these Runs simply write the same files again.

Write the setting reader

Command
mkdir -p ~/fuzz/value && cd ~/fuzz/value && cat > value.c <<'END'
#include <stddef.h>
#include <string.h>
int read_value(const char *line, size_t len, char out[32]) {
  const char *eq = memchr(line, '=', len);
  if (eq == NULL) return -1;
  size_t start = (size_t)(eq - line) + 1;
  size_t n = 0;
  if (start < len && line[start] == '"') {
    start++;
    while (start + n <= len && line[start + n] != '"') n++;
  } else {
    n = len - start;
  }
  if (n > 31) return -1;
  memcpy(out, line + start, n);
  out[n] = 0;
  return (int)n;
}
END

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the tests and run them

Command
cd ~/fuzz/value && cat > test_value.c <<'END'
#include <stdio.h>
#include <string.h>
int read_value(const char *line, size_t len, char out[32]);
int main(void) {
  const char *cases[] = {"name=ann", "motto=\"be kind\"", "no setting here"};
  char out[32];
  for (int i = 0; i < 3; i++) {
    int n = read_value(cases[i], strlen(cases[i]), out);
    printf("%s -> %d %s\n", cases[i], n, n < 0 ? "" : out);
  }
  return 0;
}
END
gcc -Wall -g -o test_value value.c test_value.c && ./test_value

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the harness

Command
cd ~/fuzz/value && cat > fuzz_value.c <<'END'
#include <stddef.h>
#include <stdint.h>
int read_value(const char *line, size_t len, char out[32]);

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  char out[32];
  read_value((const char *)data, size, out);
  return 0;
}
END

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Fix the loop and keep the input

Command
cd ~/fuzz/value && sed -i 's/start + n <= len/start + n < len/' value.c && mkdir -p regress && printf 'motto="be kind' > regress/unclosed-quote && grep -n 'start + n' value.c && ls regress

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Three jobs in one make target

The Makefile has three jobs. unit builds the tests with both sanitizers and runs them. regress replays every saved input. fuzz-smoke fuzzes for 20 seconds from a kept corpus.

make ci runs all three and stops at the first that fails. A real pipeline runs exactly this command.

Write the Makefile

Command
cd ~/fuzz/value && cat > Makefile <<'END'
.RECIPEPREFIX = >
.PHONY: unit regress fuzz-smoke ci
SAN = -g -fsanitize=address,undefined -fno-sanitize-recover=undefined
export DEBUGINFOD_URLS =
unit:
> clang $(SAN) -o test_value value.c test_value.c
> ./test_value

regress:
> clang $(SAN) -fsanitize=fuzzer -o fuzz-value value.c fuzz_value.c
> ./fuzz-value regress/*

fuzz-smoke: regress
> mkdir -p corpus
> ./fuzz-value -seed=1 -max_total_time=20 corpus regress

ci: unit regress fuzz-smoke
> @echo "ci passed: unit tests, regression inputs, a short fuzz run"
END

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run the pipeline

Command
cd ~/fuzz/value && make ci > ci.log 2>&1; echo "make exit $?"; grep -E 'Running|Done|passed' ci.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A teammate puts the bug back

Now play the teammate who tidies the loop in March. The Run puts <= back and runs the pipeline again.

Watch which job fails, and what make says.

Undo the fix and run the pipeline

Command
cd ~/fuzz/value && sed -i 's/start + n < len/start + n <= len/' value.c && make ci > ci.log 2>&1; echo "make exit $?"; grep -E 'SUMMARY|Error' ci.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think a green fuzz job proves the code has no bugs. Twenty seconds proves only that nothing was found in twenty seconds. The short job guards known bugs; a long run each night hunts new ones.

Put the fix back and pass again

The pipeline refused the change at the regression step, before any fuzzing time was spent. The fix is to undo the edit.

Restore < and run the pipeline once more.

Restore the fix and run it again

Command
cd ~/fuzz/value && sed -i 's/start + n <= len/start + n < len/' value.c && make ci > ci.log 2>&1; echo "make exit $?"; tail -n 1 ci.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The same command on a CI service

On GitHub Actions the pipeline is a workflow file that installs clang and runs make ci, keeping any crash file as an artifact when it fails.

The Run writes one so you can read it. Your lab has no CI runner and cannot reach GitHub, so it is not run here.

Write a workflow file to read

Command
cd ~/fuzz/value && mkdir -p .github/workflows && cat > .github/workflows/fuzz.yml <<'END'
name: fuzz
on: [push, pull_request]
jobs:
  fuzz:
    runs-on: ubuntu-24.04
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - run: sudo apt-get install -y clang
      - run: make ci
      - if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: crash-inputs
          path: crash-*
END
grep -c run: .github/workflows/fuzz.yml

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: two kinds of fuzz job

JobWhenHow longWhat it is for
Regression replayEvery changeSecondsKnown bugs stay fixed
Smoke runEvery changeAbout a minuteObvious new failures
Long runEach nightHoursNew bugs deep in the code

The cells students fill in are left blank in this preview.

Assignment: the pipeline's decisions

A pipeline makes two decisions. Write a function for each.

first_failure(steps) takes a list of (name, exit_code) pairs in the order they ran. Return the name of the first step whose exit code is not 0, or "" if every step passed.

split_budget(seconds, targets) shares a whole number of fuzzing seconds between fuzz targets. Return a dictionary from each target to its seconds: share them equally, and give the seconds left over one each to the targets that sort first.

Code: python
def first_failure(steps):
    return ""


def split_budget(seconds, targets):
    return {}

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.