-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
executable file
·109 lines (86 loc) · 2.16 KB
/
run.sh
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
#!/bin/bash
# Tools
OPT=opt-3.9
CLANG=clang-3.9
LINK=llvm-link-3.9
LLC=llc-3.9
CXX=g++
# Common folders
build="build/"
# Pass
rcPass="${build}libStateProtectorPass.so"
# Pass' libraries and files
rcLibrary="${build}libcheck.o"
backtraceLibrary="-L/usr/lib/gcc/x86_64-linux-gnu/5 -lbacktrace"
# Configurations
config=""
resultFile=""
function usage {
echo "usage: run.sh [-f file ]"
echo " -f file containing configuration"
}
function exitIfFail {
if [ $1 != 0 ]; then
exit $1
fi
}
# https://stackoverflow.com/questions/1527049/join-elements-of-an-array
function joinBy {
local IFS="$1"; shift; echo "$*";
}
function RC {
${OPT} -load ${rcPass} \
"${build}${resultFile}.bc" -stateProtect -ff $config -o "${build}${resultFile}-inst.bc"
exitIfFail $?
}
function getBCFiles {
local bcFiles
for element in "$@"
do
file=$(basename "$element")
bcFiles+="${file%.*}.bc "
done
echo "$bcFiles"
}
# Check if enough arguments supplied to program
if [ $# != 2 ]; then
usage
exit 1
fi
# Parce input arguments
while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
config=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# Parse json to get inputs
inputCFiles=($(jq -r '.program[]' $config))
exitIfFail $?
inputCFiles=$(joinBy ' ' "${inputCFiles[@]}")
inputBCFiles=$(getBCFiles $inputCFiles)
resultFile=$(jq -r '.binary' $config)
exitIfFail $?
clangFlags=$(jq -r 'select(.clangFlags != null) .clangFlags' $config)
exitIfFail $?
# Generate bc files
${CLANG} -c -g -emit-llvm ${inputCFiles} $clangFlags -O0
exitIfFail $?
${LINK} $inputBCFiles -o "${build}${resultFile}.bc"
exitIfFail $?
rm $inputBCFiles 2> /dev/null
# Protect
RC
# Generate object
${LLC} -filetype=obj "${build}${resultFile}-inst.bc"
exitIfFail $?
# Link
${CXX} -g -rdynamic "${build}${resultFile}-inst.o" ${rcLibrary} ${backtraceLibrary} -o ${build}${resultFile}
exitIfFail $?