#!/bin/sh 

# Assumptions:
# 	$1 is the full pathname to output file


# For PEASOUP, Required XML fields are
# name - name of the test
# host - name of the host where the test was run
# project - project name
# date_time - date time in specific format date +%FT%R:%S
# key_value pairs, any number
#   may include result, user, host platform, build platform


# Fixed attributes
# ATTRIBUTE ModDep=strata
# ATTRIBUTE ModDep=diablo_toolchain
# ATTRIBUTE ModDep=binutils-2.19
# ATTRIBUTE ModDep=stratafier
# ATTRIBUTE ModDep=idapro51
# ATTRIBUTE ModDep=idapro51_sdk
# ATTRIBUTE TestsWhat=lang_C
# ATTRIBUTE TestsWhat=strata
# ATTRIBUTE TestsWhat=interoverflow
# ATTRIBUTE TestsWhat=@LIBTYPE@
# ATTRIBUTE OS=linux
# ATTRIBUTE BenchmarkSuite=IntegerOverflow
# ATTRIBUTE Compiler=gcc
# ATTRIBUTE Arch=x86_32

# Filled in by test generator
# ATTRIBUTE TestName="@BENCHNAME@.c"
# ATTRIBUTE BenchmarkName=@BENCHNAME@
# ATTRIBUTE CompilerFlags="@COMPFLAGS@"

BENCHNAME=@BENCHNAME@
COMPFLAGS="@COMPFLAGS@"

outfile=$1

cleanup()
{
	exit_code=$1
	shift
	msg=$*

	if [ $exit_code -ne 0 ]; then 
		report_test_failure $outfile "Intermediate step failed, exit code is $exit_code, msg='$msg'"
	fi

	basename_orig=`basename $orig .ncexe`

	rm -f $orig strata.$$ tmp1.$$ stratafier.o.exe $orig.annot $orig.{log,SMPobjdump} $basename_orig.{idb,asm}
	exit $exit_code
}

# suck in utils
. ${TEST_HARNESS_HOME}/test_utils.sh || cleanup 1 "Cannot source utils file"

assert_test_args $*
assert_test_env $outfile STRATAFIER STRATA TOOLCHAIN STRATAFIER_OBJCOPY IDAROOT IDASDK 

# path to source
testloc=`pwd`

orig=$BENCHNAME.$$.exe

# compile
gcc ${testloc}/$BENCHNAME.c  $COMPFLAGS -o $orig || cleanup 2 "gcc failed"

# sanity check compile
if [ ! -f $orig ]; then cleanup 3 "Failed to create $orig"; fi

$PEASOUP_HOME/tools/ps_analyze.sh $orig $orig.protected --step ilr=off --step p1transform=off --step concolic=off

${testloc}/$orig.protected | grep -i "overflow detected"
if [ ! $? -eq 0 ]; then
	report_test_failure $outfile "Did not detect overflow" || cleanup 4 "Reporting failed?"
fi

# cleanup
cleanup 0 "Success!"