about summary refs log tree commit diff
path: root/examples/analysis_scripts/queue2csv.sh
blob: 2528b4386e8659be96ae675d9145d0d4ef924186 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash

test -z "$1" -o -z "$2" -o "$1" = "-h" -o "$1" = "-hh" -o "$1" = "--help" -o '!' -d "$1" && {
  echo "Syntax: [-n]  $0 out-directory file.csv [\"tools/target --opt @@\"]"
  echo Option -n will suppress the CSV header.
  echo If the target execution command is supplied then also edge coverage is gathered.
  exit 1
}

function getval() {
  VAL=""
  if [ "$file" != "${file/$1/}" ]; then
    TMP="${file/*$1:/}"
    VAL="${TMP/,*/}"
  fi
}

SKIP=
if [ "$1" = "-n" ]; then
  SKIP=1
  shift
fi

test -n "$4" && { echo "Error: too many commandline options. Target command and options including @@ have to be passed within \"\"!"; exit 1; }

test -d "$1"/queue && OUT="$1/queue" || OUT="$1"

OK=`ls $OUT/id:000000,time:0,orig:* 2> /dev/null`
if [ -n "$OK" ]; then
  LISTCMD="ls $OUT/id:"*
else
  LISTCMD="ls -tr $OUT/"
fi

ID=;SRC=;TIME=;OP=;POS=;REP=;EDGES=;EDGES_TOTAL=;
DIR="$OUT/../stats"
rm -rf "$DIR"
> "$2" || exit 1
mkdir "$DIR" || exit 1
> "$DIR/../edges.txt" || exit 1

{

  if [ -z "$SKIP" ]; then
    echo "time;\"filename\";id;src;new_cov;edges;total_edges;\"op\";pos;rep;unique_edges"
  fi

  $LISTCMD | grep -v ,sync: | sed 's/.*id:/id:/g' | while read file; do

    if [ -n "$3" ]; then

      TMP=${3/@@/$OUT/$file}
      
      if [ "$TMP" = "$3" ]; then
    
        cat "$OUT/$file" | afl-showmap -o "$DIR/$file" -q -- $3 >/dev/null 2>&1
        
      else
      
        afl-showmap -o "$DIR/$file" -q -- $TMP >/dev/null 2>&1
      
      fi
    
      { cat "$DIR/$file" | sed 's/:.*//' ; cat "$DIR/../edges.txt" ; } | sort -nu > $DIR/../edges.txt.tmp
      mv $DIR/../edges.txt.tmp $DIR/../edges.txt
      EDGES=$(cat "$DIR/$file" | wc -l)
      EDGES_TOTAL=$(cat "$DIR/../edges.txt" | wc -l)

    fi

    getval id; ID="$VAL"
    getval src; SRC="$VAL"
    getval time; TIME="$VAL"
    getval op; OP="$VAL"
    getval pos; POS="$VAL"
    getval rep; REP="$VAL"
    if [ "$file" != "${file/+cov/}" ]; then
      COV=1
    else
      COV=""
    fi

    if [ -n "$3" -a -s "$DIR/../edges.txt" ]; then
      echo "$TIME;\"$file\";$ID;$SRC;$COV;$EDGES;$EDGES_TOTAL;\"$OP\";$POS;$REP;UNIQUE$file"
    else
      echo "$TIME;\"$file\";$ID;$SRC;$COV;;;\"$OP\";$POS;$REP;"
    fi

  done

} | tee "$DIR/../queue.csv" > "$2" || exit 1

if [ -n "$3" -a -s "$DIR/../edges.txt" ]; then

  cat "$DIR/"* | sed 's/:.*//' | sort -n | uniq -c | egrep '^[ \t]*1 ' | awk '{print$2}' > $DIR/../unique.txt

  if [ -s "$DIR/../unique.txt" ]; then

    ls "$DIR/id:"* | grep -v ",sync:" |sed 's/.*\/id:/id:/g' | while read file; do

      CNT=$(sed 's/:.*//' "$DIR/$file" | tee "$DIR/../tmp.txt" | wc -l)
      DIFF=$(diff -u "$DIR/../tmp.txt" "$DIR/../unique.txt" | egrep '^-[0-9]' | wc -l)
      UNIQUE=$(($CNT - $DIFF))
      sed -i "s/;UNIQUE$file/;$UNIQUE/" "$DIR/../queue.csv" "$2"

    done
    
    rm -f "$DIR/../tmp.txt"

  else
    
    sed -i 's/;UNIQUE.*/;/' "$DIR/../queue.csv" "$2"
  
  fi  

fi

mv "$DIR/../queue.csv" "$DIR/queue.csv"
if [ -e "$DIR/../edges.txt" ]; then mv "$DIR/../edges.txt" "$DIR/edges.txt"; fi
if [ -e "$DIR/../unique.txt" ]; then mv "$DIR/../unique.txt" "$DIR/unique.txt"; fi

echo "Created $2"