Skip to content

Commit 331bc9b

Browse files
neuracrjduart
authored andcommitted
Add test that parses fuzzer_stats (google#17)
PR google#15 added new statistics outputed to the out/fuzzer_stats file. Add tests for it. - create a .travis/ directory in which we can put helper scripts for travis - create .travis/check_fuzzer_stats.sh to parse out/fuzzer_stats and check for expected key:value pairs. - run several jobs to test for different environment variables ( AFL_EXIT_WHEN_DONE, AFL_BENCH_JUST_ONE, AFL_BENCH_UNTIL_CRASH, and manual stopping)
1 parent f54b859 commit 331bc9b

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

.travis.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
language: c
22

33
env:
4-
- AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 AFL_NO_UI=1
4+
- AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 AFL_NO_UI=1 AFL_STOP_MANUALLY=1
5+
- AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 AFL_NO_UI=1 AFL_EXIT_WHEN_DONE=1
6+
# TODO: test AFL_BENCH_UNTIL_CRASH once we have a target that crashes
7+
- AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 AFL_NO_UI=1 AFL_BENCH_JUST_ONE=1
58

69
before_install:
710
- sudo apt update
@@ -13,10 +16,18 @@ script:
1316
- ./afl-gcc ./test-instr.c -o test-instr-gcc
1417
- mkdir seeds
1518
- echo "" > seeds/nil_seed
16-
- timeout --preserve-status 5s ./afl-fuzz -i seeds -o out/ -- ./test-instr-gcc
19+
- if [ -z "$AFL_STOP_MANUALLY" ];
20+
then ./afl-fuzz -i seeds -o out/ -- ./test-instr-gcc;
21+
else timeout --preserve-status 5s ./afl-fuzz -i seeds -o out/ -- ./test-instr-gcc;
22+
fi
23+
- .travis/check_fuzzer_stats.sh -o out -k peak_rss_mb -v 1450 -p 500
1724
- rm -r out/*
1825
- ./afl-clang ./test-instr.c -o test-instr-clang
19-
- timeout --preserve-status 5s ./afl-fuzz -i seeds -o out/ -- ./test-instr-clang
26+
- if [ -z "$AFL_STOP_MANUALLY" ];
27+
then ./afl-fuzz -i seeds -o out/ -- ./test-instr-clang;
28+
else timeout --preserve-status 5s ./afl-fuzz -i seeds -o out/ -- ./test-instr-clang;
29+
fi
30+
- .travis/check_fuzzer_stats.sh -o out -k peak_rss_mb -v 1450 -p 500
2031
- make clean
2132
- CC=clang CXX=clang++ make
2233
- cd llvm_mode
@@ -26,9 +37,18 @@ script:
2637
- cd ..
2738
- rm -r out/*
2839
- ./afl-clang-fast ./test-instr.c -o test-instr-clang-fast
29-
- timeout --preserve-status 5s ./afl-fuzz -i seeds -o out/ -- ./test-instr-clang-fast
40+
- if [ -z "$AFL_STOP_MANUALLY" ];
41+
then ./afl-fuzz -i seeds -o out/ -- ./test-instr-clang;
42+
else timeout --preserve-status 5s ./afl-fuzz -i seeds -o out/ -- ./test-instr-clang-fast;
43+
fi
44+
- .travis/check_fuzzer_stats.sh -o out -k peak_rss_mb -v 1450 -p 500
3045
- cd qemu_mode
3146
- ./build_qemu_support.sh
3247
- cd ..
3348
- gcc ./test-instr.c -o test-no-instr
34-
- timeout --preserve-status 5s ./afl-fuzz -Q -i seeds -o out/ -- ./test-no-instr
49+
- if [ -z "$AFL_STOP_MANUALLY" ];
50+
then ./afl-fuzz -Q -i seeds -o out/ -- ./test-no-instr;
51+
else timeout --preserve-status 5s ./afl-fuzz -Q -i seeds -o out/ -- ./test-no-instr;
52+
fi
53+
- .travis/check_fuzzer_stats.sh -o out -k peak_rss_mb -v 12000 -p 9000
54+

.travis/check_fuzzer_stats.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
usage() {
3+
echo "Usage: $0 -o <out_dir> -k <key> -v <value> [-p <precision>]" 1>&2;
4+
echo " " 1>&2;
5+
echo "Checks if a key:value appears in the fuzzer_stats report" 1>&2;
6+
echo " " 1>&2;
7+
echo -n "If \"value\" is numeric and \"precision\" is defined, checks if the stat " 1>&2;
8+
echo "printed by afl is value+/-precision." 1>&2;
9+
exit 1; }
10+
11+
while getopts "o:k:v:p:" opt; do
12+
case "${opt}" in
13+
o)
14+
o=${OPTARG}
15+
;;
16+
k)
17+
k=${OPTARG}
18+
;;
19+
v)
20+
v=${OPTARG}
21+
;;
22+
p)
23+
p=${OPTARG}
24+
;;
25+
*)
26+
usage
27+
;;
28+
esac
29+
done
30+
31+
if [ -z $o ] || [ -z $k ] || [ -z $v ]; then usage; fi
32+
33+
# xargs to trim the surrounding whitespaces
34+
stat_v=$( grep $k "$o"/fuzzer_stats | cut -d ":" -f 2 | xargs )
35+
v=$( echo "$v" | xargs )
36+
37+
if [ -z stat_v ];
38+
then echo "ERROR: key $k not found in fuzzer_stats." 1>&2
39+
exit 1
40+
fi
41+
42+
re_percent='^[0-9]+([.][0-9]+)?\%$'
43+
# if the argument is a number in percentage, get rid of the %
44+
if [[ "$v" =~ $re_percent ]]; then v=${v: :-1}; fi
45+
if [[ "$stat_v" =~ $re_percent ]]; then stat_v=${stat_v: :-1}; fi
46+
47+
re_numeric='^[0-9]+([.][0-9]+)?$'
48+
# if the argument is not a number, we check for strict equality
49+
if (! [[ "$v" =~ $re_numeric ]]) || (! [[ "$stat_v" =~ $re ]]);
50+
then if [ "$v" != "$stat_v" ];
51+
then echo "ERROR: \"$k:$stat_v\" (should be $v)." 1>&2
52+
exit 2;
53+
fi
54+
# checks if the stat reported by afl is in the range
55+
elif [ "$stat_v" -lt $(( v - p )) ] || [ "$stat_v" -gt $(( v + p )) ];
56+
then echo "ERROR: key $k:$stat_v is out of correct range." 1>&2
57+
exit 3;
58+
fi
59+
echo "OK: key $k:$stat_v" 1>&2
60+

0 commit comments

Comments
 (0)