aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/csmith.sh
blob: 42e49be7be4b2f0cb8a16c60b2a8800ca7b27807 (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
#!/bin/sh
csmith_path=/usr/include/csmith-2.3.0/
cd $(dirname "$0")

WORKDIR=csmith
mkdir -p "$WORKDIR"

REFCC=${REFCC:-gcc}
N=${1:-100}
npass=0
nfail=0
ntimeout=0
ncomperr=0
pid=$$

TIMEOUT=${TIMEOUT:-"2"}
CFLAGS=${CFLAGS:-""}

echo "($pid) Running $N tests"
echo "Using REFCC=$REFCC"
echo "antcc CFLAGS=$CFLAGS"
echo "timeout=$TIMEOUT"s

run_test() {
    num=$1
    tmp1="$WORKDIR/$pid-$num-antcc"
    tmp2="$WORKDIR/$pid-$num-ref"
    out1="$WORKDIR/$pid-$num-antcc.out"
    out2="$WORKDIR/$pid-$num-ref.out"
    src="$WORKDIR/$pid-$num.c"
    
    csmith > "$src" 2>/dev/null || return 1
    
    if ! prlimit --as=1073741824 ../antcc $CFLAGS -I"$csmith_path" -w "$src" -o "$tmp1" -lm; then
        ncomperr=$((ncomperr+1))
        echo "FAIL (compile): test $num"
        mv "$src" "$WORKDIR/$pid-fail_compile_$num.c"
        rm -f "$tmp1" "$tmp2" "$out1" "$out2"
        return 1
    fi
    
    if ! $REFCC -I"$csmith_path" -w "$src" -o "$tmp2" -lm ; then
        echo "FAIL (ref compile)"
        rm -f "$tmp1" "$tmp2" "$out1" "$out2" "$src"
        exit 1
    fi
    
    timeout $TIMEOUT "$tmp1" > "$out1" 2>&1
    st1=$?
    
    timeout $TIMEOUT "$tmp2" > "$out2" 2>&1
    st2=$?
    
    if [ $st1 -eq 124 ] || [ $st2 -eq 124 ]; then
        # Timeout - likely infinite loop
        ntimeout=$((ntimeout+1))
    elif [ $st1 -ne $st2 ]; then
        # Different exit codes
        echo ""
        echo "FAIL (exit code): test $num"
        echo "  antcc: $st1, gcc: $st2"
        mv "$src" "$WORKDIR/$pid-fail_exit_$num.c"
        nfail=$((nfail+1))
    elif ! diff -q -w "$out1" "$out2" >/dev/null 2>&1; then
        # Different output
        echo ""
        echo "FAIL (output): test $num"
        diff "$out1" "$out2"
        mv "$src" "$WORKDIR/$pid-fail_output_$num.c"
        nfail=$((nfail+1))
    else
        npass=$((npass+1))
        printf "."
    fi
    
    rm -f "$tmp1" "$tmp2" "$out1" "$out2" "$src"
    return 0
}

set -e

i=0
while [ $i -lt $N ]; do
    run_test $i || true
    i=$((i+1))
done

echo ""
echo "Results: $npass passed, $nfail failed, $ntimeout timeouts, $ncomperr compile errors"
find "$WORKDIR"/ | grep "$pid" && echo