blob: 1bd0a82822be23ef069cdd5f496943b8324a9d04 [file] [log] [blame]
#!/bin/bash -e
#
# This script is used to compile and test a single file with SAFECode.
#
expect_error=1
usage()
{
echo 'usage: test.sh [args] file.c/file.ll'
echo 'arguments:'
echo ' -t dir directory to use for testing'
echo ' -p expect no SAFEcode errors from the test case'
echo ' -e expect a SAFEcode error from the test case'
}
# Process the arguments.
while getopts hept:cf option
do
case $option in
e) expect_error=1;;
p) expect_error=0;;
t) testdir=$OPTARG;;
h) usage
exit 1;;
\?) exit 1;;
esac
done
# Get the file argument.
shift $((OPTIND-1))
# If there is no file, print usage information and exit.
if [ $# -lt 1 ]
then
usage
exit 1
fi
sc=@SC@
sc_lib=@SC_LIB@
filename=$1
# Directory to use for temporary files.
testdir=${testdir:-$(dirname $filename)}
filebase=$(basename $filename)
case $filebase in
*.ll) prefix=${filebase%%.ll}
filetype=ll;;
*.c) prefix=${filebase%%.c}
filetype=c;;
*) echo "unknown file type"
exit 1;;
esac
# SAFECode-compiled bitcode
llfile=$testdir/${prefix}.sc.ll
# Compiled executable
scfile=$testdir/${prefix}.sc
# SAFECode compilation log
sclog=$testdir/${prefix}.sc.log
# Executable output
scout=$testdir/${prefix}.sc.output
# Prepare the testing directory.
setupdir()
{
# Add temporary directory.
mkdir -p $testdir
}
# Compile the bitcode of the test.
compile()
{
# Create bitcode file with SAFECode passes.
$sc -g -S -emit-llvm -fmemsafety -o $llfile $filename 2>&1 | tee $sclog
# Compile and link bitcode.
$sc -o $scfile $llfile $sc_lib/libsc_dbg_rt.a $sc_lib/libpoolalloc_bitmap.a $sc_lib/libgdtoa.a -lstdc++
}
# Run test and check results.
runtest()
{
# Don't exit immediately on failure of below commands.
set +e
$scfile >& $scout
retval=$?
error_count=$(grep -c SAFECode $scout)
set -e
if [ $error_count -gt 0 ]
then
if [ $expect_error -eq 1 ]
then
exit 0 # An error was expected and caught.
else
exit 1 # An unexpected error occurred.
fi
else
if [ $retval -eq 0 ]
then
exit 0 # No error was expected, none was caught, program ran as expected.
else
exit 1 # Some non-SAFECode related error occurred.
fi
fi
}
setupdir
compile
runtest