xref: /aosp_15_r20/external/AFLplusplus/utils/analysis_scripts/queue2csv.sh (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1#!/bin/bash
2
3test -z "$1" -o -z "$2" -o "$1" = "-h" -o "$1" = "-hh" -o "$1" = "--help" -o '!' -d "$1" && {
4  echo "Syntax: [-n]  $0 out-directory file.csv [\"tools/target --opt @@\"]"
5  echo Option -n will suppress the CSV header.
6  echo If the target execution command is supplied then also edge coverage is gathered.
7  exit 1
8}
9
10function getval() {
11  VAL=""
12  if [ "$file" != "${file/$1/}" ]; then
13    TMP="${file/*$1:/}"
14    VAL="${TMP/,*/}"
15  fi
16}
17
18SKIP=
19if [ "$1" = "-n" ]; then
20  SKIP=1
21  shift
22fi
23
24test -n "$4" && { echo "Error: too many commandline options. Target command and options including @@ have to be passed within \"\"!"; exit 1; }
25
26test -d "$1"/queue && OUT="$1/queue" || OUT="$1"
27
28OK=`ls $OUT/id:000000,time:0,orig:* 2> /dev/null`
29if [ -n "$OK" ]; then
30  LISTCMD="ls $OUT/id:"*
31else
32  LISTCMD="ls -tr $OUT/"
33fi
34
35ID=;SRC=;TIME=;OP=;POS=;REP=;EDGES=;EDGES_TOTAL=;
36DIR="$OUT/../stats"
37rm -rf "$DIR"
38> "$2" || exit 1
39mkdir "$DIR" || exit 1
40> "$DIR/../edges.txt" || exit 1
41
42{
43
44  if [ -z "$SKIP" ]; then
45    echo "time;\"filename\";id;src;new_cov;edges;total_edges;\"op\";pos;rep;unique_edges"
46  fi
47
48  $LISTCMD | grep -v ,sync: | sed 's/.*id:/id:/g' | while read file; do
49
50    if [ -n "$3" ]; then
51
52      TMP=${3/@@/$OUT/$file}
53
54      if [ "$TMP" = "$3" ]; then
55
56        cat "$OUT/$file" | afl-showmap -o "$DIR/$file" -q -- $3 >/dev/null 2>&1
57
58      else
59
60        afl-showmap -o "$DIR/$file" -q -- $TMP >/dev/null 2>&1
61
62      fi
63
64      { cat "$DIR/$file" | sed 's/:.*//' ; cat "$DIR/../edges.txt" ; } | sort -nu > $DIR/../edges.txt.tmp
65      mv $DIR/../edges.txt.tmp $DIR/../edges.txt
66      EDGES=$(cat "$DIR/$file" | wc -l)
67      EDGES_TOTAL=$(cat "$DIR/../edges.txt" | wc -l)
68
69    fi
70
71    getval id; ID="$VAL"
72    getval src; SRC="$VAL"
73    getval time; TIME="$VAL"
74    getval op; OP="$VAL"
75    getval pos; POS="$VAL"
76    getval rep; REP="$VAL"
77    if [ "$file" != "${file/+cov/}" ]; then
78      COV=1
79    else
80      COV=""
81    fi
82
83    if [ -n "$3" -a -s "$DIR/../edges.txt" ]; then
84      echo "$TIME;\"$file\";$ID;$SRC;$COV;$EDGES;$EDGES_TOTAL;\"$OP\";$POS;$REP;UNIQUE$file"
85    else
86      echo "$TIME;\"$file\";$ID;$SRC;$COV;;;\"$OP\";$POS;$REP;"
87    fi
88
89  done
90
91} | tee "$DIR/../queue.csv" > "$2" || exit 1
92
93if [ -n "$3" -a -s "$DIR/../edges.txt" ]; then
94
95  cat "$DIR/"* | sed 's/:.*//' | sort -n | uniq -c | grep -E '^[ \t]*1 ' | awk '{print$2}' > $DIR/../unique.txt
96
97  if [ -s "$DIR/../unique.txt" ]; then
98
99    ls "$DIR/id:"* | grep -v ",sync:" |sed 's/.*\/id:/id:/g' | while read file; do
100
101      CNT=$(sed 's/:.*//' "$DIR/$file" | tee "$DIR/../tmp.txt" | wc -l)
102      DIFF=$(diff -u "$DIR/../tmp.txt" "$DIR/../unique.txt" | grep -E '^-[0-9]' | wc -l)
103      UNIQUE=$(($CNT - $DIFF))
104      sed -i "s/;UNIQUE$file/;$UNIQUE/" "$DIR/../queue.csv" "$2"
105
106    done
107
108    rm -f "$DIR/../tmp.txt"
109
110  else
111
112    sed -i 's/;UNIQUE.*/;/' "$DIR/../queue.csv" "$2"
113
114  fi
115
116fi
117
118mv "$DIR/../queue.csv" "$DIR/queue.csv"
119if [ -e "$DIR/../edges.txt" ]; then mv "$DIR/../edges.txt" "$DIR/edges.txt"; fi
120if [ -e "$DIR/../unique.txt" ]; then mv "$DIR/../unique.txt" "$DIR/unique.txt"; fi
121
122echo "Created $2"
123