xref: /aosp_15_r20/external/AFLplusplus/test/test-gcc-plugin.sh (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1#!/bin/sh
2
3. ./test-pre.sh
4
5$ECHO "$BLUE[*] Testing: gcc_plugin"
6test -e ../afl-gcc-fast -a -e ../afl-compiler-rt.o && {
7  SAVE_AFL_CC=${AFL_CC}
8  export AFL_CC=`command -v gcc`
9  ../afl-gcc-fast -o test-instr.plain.gccpi ../test-instr.c > /dev/null 2>&1
10  AFL_HARDEN=1 ../afl-gcc-fast -o test-compcov.harden.gccpi test-compcov.c > /dev/null 2>&1
11  test -e test-instr.plain.gccpi && {
12    $ECHO "$GREEN[+] gcc_plugin compilation succeeded"
13    echo 0 | AFL_QUIET=1 ../afl-showmap -m ${MEM_LIMIT} -o test-instr.plain.0 -r -- ./test-instr.plain.gccpi > /dev/null 2>&1
14    AFL_QUIET=1 ../afl-showmap -m ${MEM_LIMIT} -o test-instr.plain.1 -r -- ./test-instr.plain.gccpi < /dev/null > /dev/null 2>&1
15    test -e test-instr.plain.0 -a -e test-instr.plain.1 && {
16      diff test-instr.plain.0 test-instr.plain.1 > /dev/null 2>&1 && {
17        $ECHO "$RED[!] gcc_plugin instrumentation should be different on different input but is not"
18        CODE=1
19      } || {
20        $ECHO "$GREEN[+] gcc_plugin instrumentation present and working correctly"
21        TUPLES=`echo 0|AFL_QUIET=1 ../afl-showmap -m ${MEM_LIMIT} -o /dev/null -- ./test-instr.plain.gccpi 2>&1 | grep Captur | awk '{print$3}'`
22        test "$TUPLES" -gt 1 -a "$TUPLES" -lt 9 && {
23          $ECHO "$GREEN[+] gcc_plugin run reported $TUPLES instrumented locations which is fine"
24        } || {
25          $ECHO "$RED[!] gcc_plugin instrumentation produces a weird numbers: $TUPLES"
26          $ECHO "$YELLOW[-] this is a known issue in gcc, not AFL++. It is not flagged as an error because travis builds would all fail otherwise :-("
27          #CODE=1
28        }
29        test "$TUPLES" -lt 2 && SKIP=1
30        true
31      }
32    } || {
33      $ECHO "$RED[!] gcc_plugin instrumentation failed"
34      CODE=1
35    }
36    rm -f test-instr.plain.0 test-instr.plain.1
37  } || {
38    $ECHO "$RED[!] gcc_plugin failed"
39    CODE=1
40  }
41
42  test -e test-compcov.harden.gccpi && test_compcov_binary_functionality ./test-compcov.harden.gccpi && {
43    nm test-compcov.harden.gccpi | grep -Eq 'stack_chk_fail|fstack-protector-all|fortified' > /dev/null 2>&1 && {
44      $ECHO "$GREEN[+] gcc_plugin hardened mode succeeded and is working"
45    } || {
46      $ECHO "$RED[!] gcc_plugin hardened mode is not hardened"
47      CODE=1
48    }
49    rm -f test-compcov.harden.gccpi
50  } || {
51    $ECHO "$RED[!] gcc_plugin hardened mode compilation failed"
52    CODE=1
53  }
54  # now we want to be sure that afl-fuzz is working
55  # make sure crash reporter is disabled on Mac OS X
56  (test "$(uname -s)" = "Darwin" && test $(launchctl list 2>/dev/null | grep -q '\.ReportCrash$') && {
57    $ECHO "$RED[!] we cannot run afl-fuzz with enabled crash reporter. Run 'sudo sh afl-system-config'.$RESET"
58    CODE=1
59    true
60  }) || {
61    test -z "$SKIP" && {
62      mkdir -p in
63      echo 0 > in/in
64      $ECHO "$GREY[*] running afl-fuzz for gcc_plugin, this will take approx 10 seconds"
65      {
66        ../afl-fuzz -V07 -m ${MEM_LIMIT} -i in -o out -- ./test-instr.plain.gccpi >>errors 2>&1
67      } >>errors 2>&1
68      test -n "$( ls out/default/queue/id:000002* 2>/dev/null )" && {
69        $ECHO "$GREEN[+] afl-fuzz is working correctly with gcc_plugin"
70      } || {
71        echo CUT------------------------------------------------------------------CUT
72        cat errors
73        echo CUT------------------------------------------------------------------CUT
74        $ECHO "$RED[!] afl-fuzz is not working correctly with gcc_plugin"
75        CODE=1
76      }
77      rm -rf in out errors
78    }
79  }
80  rm -f test-instr.plain.gccpi
81
82  # now for the special gcc_plugin things
83  echo foobar.c > instrumentlist.txt
84  AFL_GCC_INSTRUMENT_FILE=instrumentlist.txt ../afl-gcc-fast -o test-compcov test-compcov.c > /dev/null 2>&1
85  test -x test-compcov && test_compcov_binary_functionality ./test-compcov && {
86    echo 1 | AFL_QUIET=1 ../afl-showmap -m ${MEM_LIMIT} -o - -r -- ./test-compcov 2>&1 | grep -q "Captured 0 tuples" && {
87      $ECHO "$GREEN[+] gcc_plugin instrumentlist feature works correctly"
88    } || {
89      $ECHO "$RED[!] gcc_plugin instrumentlist feature failed"
90      CODE=1
91    }
92  } || {
93    $ECHO "$RED[!] gcc_plugin instrumentlist feature compilation failed."
94    CODE=1
95  }
96  rm -f test-compcov test.out instrumentlist.txt
97  ../afl-gcc-fast -o test-persistent ../utils/persistent_mode/persistent_demo.c > /dev/null 2>&1
98  test -e test-persistent && {
99    echo foo | AFL_QUIET=1 ../afl-showmap -m ${MEM_LIMIT} -o /dev/null -q -r ./test-persistent && {
100      $ECHO "$GREEN[+] gcc_plugin persistent mode feature works correctly"
101    } || {
102      $ECHO "$RED[!] gcc_plugin persistent mode feature failed to work"
103      CODE=1
104    }
105  } || {
106    $ECHO "$RED[!] gcc_plugin persistent mode feature compilation failed"
107    CODE=1
108  }
109  rm -f test-persistent
110  export AFL_CC=${SAVE_AFL_CC}
111} || {
112  $ECHO "$YELLOW[-] gcc_plugin not compiled, cannot test"
113  INCOMPLETE=1
114}
115
116. ./test-post.sh
117