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

Free lesson

Triage: from crash files to fixes

After this lesson: I can sort a pile of crash files into distinct bugs and fix each one.

60 min lesson11 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

Eleven crash files on Monday morning

The weekend fuzz job left eleven crash files. Is that eleven bugs, or one bug found eleven times?

Before anybody fixes anything, somebody has to sort them: which are the same, which is worst, and what the smallest input is.

The one rule

Warning

Triage only crashes from your own code, fuzzed on your own lab machine. Crash files from somebody else's product belong to them, under their permission. In the UK, testing without it can break the Computer Misuse Act 1990.

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 sort a pile of crash files into distinct bugs and fix each one.

Key words

WordWhat it meansExample
Triagesorting crashes before fixing them
Bucketcrashes that stop at the same place
Minimiseshrink a crashing input and keep it crashingafl-tmin
Root causethe mistake in the code behind a crash
Severityhow much harm a bug could do

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 small reader with three mistakes

average_mark reads a line such as ann:71,64,80 and returns the average. It assumes at most four marks, small numbers, and at least one mark. Each assumption is a bug.

Write it, build it for AFL++ with both sanitizers, and build a second copy that reports clearly.

Write the marks reader

Command
mkdir -p ~/fuzz/marks && cd ~/fuzz/marks && cat > marks.c <<'END'
#include <stdlib.h>
#include <string.h>

int average_mark(const char *line) {
  int marks[4];
  int count = 0, total = 0;
  const char *p = strchr(line, ':');
  if (p == NULL) return -1;
  for (p = p + 1; *p; p++) {
    if (*p >= '0' && *p <= '9') {
      marks[count++] = atoi(p);
      while (p[1] >= '0' && p[1] <= '9') p++;
    }
  }
  for (int i = 0; i < count; i++) total += marks[i];
  return total / count;
}
END

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

Write the program and try two lines

Command
cd ~/fuzz/marks && cat > main.c <<'END'
#include <stdio.h>
#include <string.h>
int average_mark(const char *line);
int main(void) {
  char line[256];
  while (fgets(line, sizeof line, stdin)) {
    line[strcspn(line, "\n")] = 0;
    printf("%d\n", average_mark(line));
  }
  return 0;
}
END
gcc -Wall -g -o marks marks.c main.c && printf 'ann:71,64,80\nben:50\n' | ./marks

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

Build a fuzzing copy and a checking copy

Command
cd ~/fuzz/marks && AFL_USE_ASAN=1 AFL_USE_UBSAN=1 afl-clang-fast -g -o marks-afl marks.c main.c && clang -g -fsanitize=address,undefined -fno-sanitize-recover=undefined -o marks-check marks.c main.c && ls marks-afl marks-check

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

Fuzz it for a minute and count

Command
cd ~/fuzz/marks && mkdir -p seeds && printf 'ann:71,64,80\n' > seeds/one.txt && rm -rf out && AFL_NO_UI=1 AFL_SKIP_CPUFREQ=1 AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 afl-fuzz -i seeds -o out -V 60 -- ./marks-afl > afl.log 2>&1; ls out/default/crashes | grep -c '^id'

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

Sort each crash by where it stops

AFL++'s UBSan build stops with a bare trap and no words. The checking copy prints a SUMMARY line naming the file and line, and that line is the bucket.

Run every crash file through the checking copy and count the distinct lines. Then ask what each bucket means.

See the fuzzing copy stop without words

Command
cd ~/fuzz/marks && ./marks-afl < out/default/crashes/id:000000* > /dev/null 2>&1; echo "exit $?"

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

Count the crashes in each bucket

Command
cd ~/fuzz/marks && for f in out/default/crashes/id*; do DEBUGINFOD_URLS= ./marks-check < "$f" 2>&1 | grep -m 1 SUMMARY; done | sort | uniq -c

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

Name what each bucket is

Command
cd ~/fuzz/marks && for f in out/default/crashes/id*; do DEBUGINFOD_URLS= ./marks-check < "$f" 2>&1 | grep -m 1 'runtime error' | cut -d: -f2,4,5; done | sort -u

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

Try it: put reports into buckets

Code: python
# Crash files and the SUMMARY line each one printed.
reports = {
    "id:000000": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:16:16",
    "id:000001": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:16:16",
    "id:000002": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:11:7",
    "id:000003": "SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior marks.c:15:41",
}

buckets = {}
for name, line in sorted(reports.items()):
    place = line.split()[-1]
    buckets.setdefault(place, []).append(name)

for place, names in sorted(buckets.items()):
    print(place, len(names), names)

# Change the place line so marks.c:16:16 becomes marks.c:16, the bucket without its column.

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

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

Shrink each input, then check its bucket

afl-tmin removes and simplifies bytes while the program still crashes. A one-character input tells you more than a screen of noise.

It keeps any crash, not the same crash, so a file can slip into another bucket. Count the buckets again afterwards.

Shrink every crash file

Command
cd ~/fuzz/marks && rm -rf min && mkdir min && for f in out/default/crashes/id*; do afl-tmin -i "$f" -o "min/$(basename "$f" | cut -c1-9)" -- ./marks-afl > /dev/null 2>&1; done; for f in min/*; do echo "$f: $(cat -v "$f")"; done

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

Count the buckets of the small files

Command
cd ~/fuzz/marks && for f in min/*; do DEBUGINFOD_URLS= ./marks-check < "$f" 2>&1 | grep -m 1 SUMMARY; done | sort | uniq -c

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

A common mistake

Warning

Many people think eleven crash files mean eleven bugs. Here they were three. Count buckets, not files, and fix the worst bucket first.

Worked example: the three buckets

BucketWhat happensWorst caseFix
marks.c:11A fifth mark is written past the arrayMemory corruptedRefuse a fifth mark
marks.c:15Huge marks overflow the totalA wrong averageRefuse marks over 100
marks.c:16A line with no marks divides by zeroA crash or a wrong answerRefuse a line with none

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

One rewrite, three refusals

Fix the root causes, not the inputs: refuse a fifth mark, refuse any mark over 100, refuse a line with none. strtol reads a long number without wrapping.

Then keep the small files as regression inputs and run every crash and every one of them again.

Rewrite the marks reader

Command
cd ~/fuzz/marks && cat > marks.c <<'END'
#include <stdlib.h>
#include <string.h>
int average_mark(const char *line) {
  int marks[4], count = 0, total = 0;
  const char *p = strchr(line, ':');
  if (p == NULL) return -1;
  for (p = p + 1; *p; p++) {
    if (*p >= '0' && *p <= '9') {
      long mark = strtol(p, NULL, 10);
      if (count == 4 || mark > 100) return -1;
      marks[count++] = (int)mark;
      while (p[1] >= '0' && p[1] <= '9') p++;
    }
  }
  if (count == 0) return -1;
  for (int i = 0; i < count; i++) total += marks[i];
  return total / count;
}
END

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

Recheck every crash and keep the small ones

Command
cd ~/fuzz/marks && clang -g -fsanitize=address,undefined -fno-sanitize-recover=undefined -o marks-check marks.c main.c && mkdir -p regress && cp min/* regress/ && for f in regress/* out/default/crashes/id*; do DEBUGINFOD_URLS= ./marks-check < "$f" > /dev/null 2>&1 || echo "still crashes: $f"; done; echo "every crash and every regression input checked"

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

Assignment: bucket and shrink crashes

Write the two steps of triage.

buckets(reports) takes a dictionary from a crash file's name to its SUMMARY line. The place is the last word of the line that has two colons in it, such as marks.c:16:16; the bucket is that place without the column, marks.c:16. Return a dictionary from each bucket to the sorted list of names in it.

shrink(data, crashes) makes a crashing string smaller. Starting at position 0, try removing the character there: if crashes is still true for the shorter string, keep it and try the same position again; if not, move on. Return the string when no single removal still crashes.

Code: python
def buckets(reports):
    return {}


def shrink(data, crashes):
    return data

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.