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 questionsBy 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
| Word | What it means | Example |
|---|---|---|
| Pipeline | the checks that run on every change | make ci |
| Job | one step of a pipeline | |
| Regression suite | all the saved inputs that once broke the code | |
| Smoke run | a short fuzz run to catch obvious failures | |
| Artifact | a file a pipeline keeps after it runs | crash-* |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA 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
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;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the tests and run them
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_valueShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the harness
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;
}
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Fix the loop and keep the input
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 regressShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsThree 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
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"
ENDShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the pipeline
cd ~/fuzz/value && make ci > ci.log 2>&1; echo "make exit $?"; grep -E 'Running|Done|passed' ci.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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.logShown 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
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.logShown 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
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.ymlShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: two kinds of fuzz job
| Job | When | How long | What it is for |
|---|---|---|---|
| Regression replay | Every change | Seconds | Known bugs stay fixed |
| Smoke run | Every change | About a minute | Obvious new failures |
| Long run | Each night | Hours | New 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.
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