#!/bin/bash

# Path to ESBMC binary
ESBMC_PATH=./esbmc
# A temporary file to pipe output into for later examination
TMPFILE=`mktemp`

# Options we might run with.
NORMALOPTS=' --unwind 6 --no-unwinding-assertions --partial-loops --64 --no-assertions -D_Bool=int --no-bounds-check --no-pointer-check --no-div-by-zero-check --error-label ERROR'
MEMSAFETYOPTS=' --unwind 6 --no-unwinding-assertions --partial-loops --64 --no-assertions -D_Bool=int --memory-leak-check'

# Memsafety cmdline picked
do_memsafety=0

# Use parallel k-induction
parallel=0

while getopts "c:mhvp" arg; do
    case $arg in
        h)
            echo "Usage: $0 [options] path_to_benchmark
Options:
-h             Print this message
-c propfile    Specifythe given property file
-m             Use the 'simple' memory model (i.e., the memsafety tests)
-k             Use k-induction
-p             If using k-induction, use a parallel algorithm"
            ;;
        c)
            # Given the lack of variation in the property file... we don't
            # actually interpret it. Instead we have the same options to all
            # tests (except for the memory checking ones), and define all the
            # error labels from other directories to be ERROR.
            ;;
        m)
            do_memsafety=1
            ;;
        v) echo `$path_to_esbmc --version`
            ;;
        p) parallel=1
            ;;
    esac
done

# Store the path to the file we're going to be checking.
filename=${BASH_ARGV[0]}

if test "${filename}" = ""; then
    echo "No benchmark given" >&2
    exit 1
fi

# There are two modes we might run in: Normal, and memsafety. The first is just
# a general set of options that work well for all testcases, the second is
# options specific to checking the memory safety category. The drivers64
# catagory receives no special options.

# Decide what options to run with.
if test ${do_memsafety} = 0; then
        THEOPTS=$NORMALOPTS
else
        THEOPTS=$MEMSAFETYOPTS
fi

inductionType="--k-induction"

if test ${parallel} = 1; then
    inductionType="--k-induction-parallel"
fi

run_bmc()
{
	$ESBMC_PATH $THEOPTS $filename > ${TMPFILE} 2>&1
}

# Run ESBMC in k-induction mode for half the time
#echo "$ESBMC_PATH $THEOPTS $filename --timeout 450s $inductionType --k-step 100 --memlimit 15g > ${TMPFILE} 2>&1"
$ESBMC_PATH $THEOPTS $filename --timeout 450s $inductionType --k-step 100 --memlimit 15g > ${TMPFILE} 2>&1

# Did we produce a concrete answer?
grep "VERIFICATION SUCCESSFUL" $TMPFILE > /dev/null
UNREACH=$?
grep "VERIFICATION FAILED" $TMPFILE > /dev/null
REACH=$?
grep "VERIFICATION UNKNOWN" $TMPFILE > /dev/null
UNKNOWN=$?

loopIts=`grep -i '*** K-Induction Loop Iteration' $TMPFILE`
echo "$loopIts"
loopIts=`grep -i 'Unwinding loop' $TMPFILE`
echo "$loopIts"

# If we produced a concrete answer, use that. Otherwise, run in BMC mode for the
# rest of the allocated time. All pthread tests will be rejected by
# ESBMC in k-induction mode.
if test $UNKNOWN -eq 0; then
	# No answer could be produced; run in BMC mode.
	run_bmc
elif test $UNREACH -eq 0; then
	# We've decided that the property is unreachable. Report and exit.
	echo "true"
	exit 0
elif test $REACH -eq 0; then
	# We've decided that the property is reachable. Report and exit.
	echo "false"
	exit 0
else
	# Something bad happened; default to running BMC mode.
	run_bmc
fi

# Examine the output from BMC mode. Note that order is important; multithreaded
# output will print SUCCESS repeatedly, and will possibly print FAILED if a
# counterexample interleaving is found. So check for presence of FAILED before
# SUCCESS.

grep "VERIFICATION SUCCESSFUL" $TMPFILE > /dev/null
UNREACH=$?
grep "VERIFICATION FAILED" $TMPFILE > /dev/null
REACH=$?

rm $TMPFILE

if test $REACH -eq 0; then
	# We've decided that the property is reachable
	echo "false"
	exit 0
elif test $UNREACH -eq 0; then
	# We've decided that the property is unreachable
	echo "true"
	exit 0
else
	echo "error"
	exit 0
fi
