forked from GJDuck/LowFat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·342 lines (311 loc) · 10.7 KB
/
build.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash
#
# _| _|_|_|_| _|
# _| _|_| _| _| _| _| _|_|_| _|_|_|_|
# _| _| _| _| _| _| _|_|_| _| _| _|
# _| _| _| _| _| _| _| _| _| _| _|
# _|_|_|_| _|_| _| _| _| _|_|_| _|_|
#
# Gregory J. Duck
#
LEGACY=no
BUILD_PLUGIN=no
VERSION=`cat VERSION`
if [ $LEGACY = yes ]
then
VERSION=${VERSION}-LEGACY
fi
if [ $# != 0 ]
then
VERSION=${VERSION}-CUSTOM
fi
RELEASE_NAME=lowfat-$VERSION
build_llvm()
{
echo -e "${GREEN}$0${OFF}: copying the LowFat config files..."
RUNTIME_PATH=llvm-4.0.0.src/projects/compiler-rt/lib/lowfat/
INSTRUMENTATION_PATH=llvm-4.0.0.src/lib/Transforms/Instrumentation/
CLANGLIB_PATH=llvm-4.0.0.src/tools/clang/lib/Basic/
(cd config; cp lowfat_config.h lowfat_config.c ../${RUNTIME_PATH}/.)
ln -fs "$PWD/${RUNTIME_PATH}/lowfat_config.c" \
"$PWD/$INSTRUMENTATION_PATH/lowfat_config.inc"
ln -fs "$PWD/${RUNTIME_PATH}/lowfat_config.h" \
"$PWD/$INSTRUMENTATION_PATH/lowfat_config.h"
ln -fs "$PWD/${RUNTIME_PATH}/lowfat_config.h" \
"$PWD/$CLANGLIB_PATH/lowfat_config.h"
ln -fs "$PWD/${RUNTIME_PATH}/lowfat.h" \
"$PWD/$INSTRUMENTATION_PATH/lowfat.h"
if [ ! -f "$PWD/${RUNTIME_PATH}/CMakeLists.txt" ]
then
if [ x$LEGACY = xno ]
then
ln -fs "$PWD/${RUNTIME_PATH}/CMakeLists.txt.modern" \
"$PWD/${RUNTIME_PATH}/CMakeLists.txt"
else
ln -fs "$PWD/${RUNTIME_PATH}/CMakeLists.txt.legacy" \
"$PWD/${RUNTIME_PATH}/CMakeLists.txt"
fi
fi
BUILD_PATH=$1
if [ -e $BUILD_PATH ]
then
CONFIGURE=false
echo -e \
"${GREEN}$0${OFF}: using existing LLVM build directory ($BUILD_PATH)..."
else
CONFIGURE=true
echo -e \
"${GREEN}$0${OFF}: creating LLVM build directory ($BUILD_PATH)..."
mkdir -p $BUILD_PATH
fi
echo -e \
"${GREEN}$0${OFF}: installing the LowFat ld script file..."
mkdir -p $BUILD_PATH/lib/LowFat/
cp config/lowfat.ld $BUILD_PATH/lib/LowFat/
echo -e "${GREEN}$0${OFF}: will now build LLVM..."
cd $BUILD_PATH
if [ x$CONFIGURE = xtrue ]
then
CC=$CLANG CXX=$CLANGXX cmake ../llvm-4.0.0.src/ \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=install \
-DBUILD_SHARED_LIBS=ON \
-DLLVM_TARGETS_TO_BUILD="X86" \
-DLLVM_BUILD_TOOLS=OFF
fi
make -j `nproc` install install-clang
rm -rf "../$RELEASE_NAME"
mv install "../$RELEASE_NAME"
cd ..
echo
if [ x$BUILD_PLUGIN = xyes ]
then
echo -e "${GREEN}$0${OFF}: creating LowFat.so plugin..."
# These should not fail if we got this far:
$CLANGXX -DLOWFAT_PLUGIN "$PWD/$INSTRUMENTATION_PATH/LowFat.cpp" \
-c -Wall -O2 -I "$PWD/$INSTRUMENTATION_PATH/" -o LowFat.o \
`$LLVM_CONFIG --cxxflags` \
`$LLVM_CONFIG --includedir` >/dev/null 2>&1
$CLANGXX -shared -rdynamic -o LowFat.so LowFat.o \
`$LLVM_CONFIG --ldflags` >/dev/null 2>&1
rm -f LowFat.o LowFat.dwo
fi
if [ x$BUILD_STANDALONE = xyes ]
then
echo -e "${GREEN}$0${OFF}: creating liblowfat.preload.so standalone..."
if [ $LEGACY = no ]
then
STANDALONE_OPTS="-mbmi -mbmi2 -mlzcnt"
else
STANDALONE_OPTS="-mno-bmi -mno-bmi2 -mno-lzcnt"
fi
$CLANG -D_GNU_SOURCE -DLOWFAT_STANDALONE -fPIC -shared \
-o liblowfat.preload.so -std=gnu99 -m64 "-I$PWD/${RUNTIME_PATH}/" \
-DLOWFAT_LINUX -O2 $STANDALONE_OPTS "$PWD/${RUNTIME_PATH}/lowfat.c"
echo -e "${GREEN}$0${OFF}: creating liblowfat.so standalone..."
$CLANG -D_GNU_SOURCE -DLOWFAT_STANDALONE \
-DLOWFAT_NO_REPLACE_STD_MALLOC -fPIC -shared \
-o liblowfat.so -std=gnu99 -m64 "-I$PWD/${RUNTIME_PATH}/" \
-DLOWFAT_LINUX -O2 $STANDALONE_OPTS "$PWD/${RUNTIME_PATH}/lowfat.c"
echo -e "${GREEN}$0${OFF}: creating lowfat.o standalone..."
$CLANG -D_GNU_SOURCE -DLOWFAT_STANDALONE \
-DLOWFAT_NO_REPLACE_STD_MALLOC -c -fPIC \
-o lowfat.o -std=gnu99 -m64 "-I$PWD/${RUNTIME_PATH}/" \
-DLOWFAT_LINUX -O2 $STANDALONE_OPTS "$PWD/${RUNTIME_PATH}/lowfat.c"
fi
echo -e "${GREEN}$0${OFF}: cleaning up the LowFat config files..."
rm -f "$PWD/${RUNTIME_PATH}/lowfat_config.h" \
"$PWD/${RUNTIME_PATH}/lowfat_config.c" \
"$PWD/$INSTRUMENTATION_PATH/lowfat_config.inc" \
"$PWD/$INSTRUMENTATION_PATH/lowfat_config.h" \
"$PWD/$INSTRUMENTATION_PATH/lowfat.h" \
"$PWD/$CLANGLIB_PATH/lowfat_config.h"
}
if [ -t 1 ]
then
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BOLD="\033[1m"
OFF="\033[0m"
else
RED=
GREEN=
YELLOW=
BOLD=
OFF=
fi
if [ $# = 0 ]
then
CONFIG="sizes.cfg 32"
echo -e \
"${GREEN}$0${OFF}: using the default LowFat configuration ($CONFIG)..."
else
CONFIG=$@
echo -e "${GREEN}$0${OFF}: using custom LowFat configuration ($CONFIG)..."
fi
CMAKE=`which cmake`
if [ -z "$CMAKE" ]
then
echo -e "${GREEN}$0${OFF}: ${RED}ERROR${OFF}: cmake is not installed!"
exit 1
fi
CLANG=`which clang-4.0`
CLANGXX=`which clang++-4.0`
LLVM_CONFIG=`which llvm-config-4.0`
HAVE_CLANG_4=false
if [ -z "$CLANG" -o -z "$CLANGXX" -o -z "$LLVM_CONFIG" ]
then
echo -e \
"${GREEN}$0${OFF}: ${YELLOW}warning${OFF}: clang-4.0 is not installed!"
echo -e "${GREEN}$0${OFF}: will try the default clang."
CLANG=`which gcc`
CLANGXX=`which g++`
if [ -z "$CLANG" -o -z "$CLANGXX" ]
then
echo -e "${GREEN}$0${OFF}: ${RED}ERROR${OFF}: gcc is not installed!"
exit 1
fi
else
HAVE_CLANG_4=true
fi
set -e
echo -n -e "${GREEN}$0${OFF}: checking the CPU..."
if grep ' bmi1' /proc/cpuinfo > /dev/null
then
echo -n "[bmi]"
else
echo
echo -e "${GREEN}$0${OFF}: ${YELLOW}warning${OFF}: CPU does not support BMI"
LEGACY=yes
fi
if grep ' bmi2' /proc/cpuinfo > /dev/null
then
echo -n "[bmi2]"
else
echo
echo -e \
"${GREEN}$0${OFF}: ${YELLOW}warning${OFF}: CPU does not support BMI2"
LEGACY=yes
fi
if grep ' abm' /proc/cpuinfo > /dev/null
then
echo "[lzcnt]"
else
echo
echo -e \
"${GREEN}$0${OFF}: ${YELLOW}warning${OFF}: CPU does not support LZCNT"
LEGACY=yes
fi
echo -e "${GREEN}$0${OFF}: building the LowFat config builder..."
(cd config; CC=$CLANG CFLAGS="-std=gnu99" CXX=$CLANGXX make >/dev/null)
echo -e "${GREEN}$0${OFF}: building the LowFat config..."
(cd config; ./lowfat-config $CONFIG > lowfat-config.log)
echo -e "${GREEN}$0${OFF}: building the LowFat config check..."
(cd config; CC=$CLANG CLFAGS="-std=gnu99" CXX=$CLANGXX make lowfat-check-config >/dev/null)
echo -e "${GREEN}$0${OFF}: checking the LowFat config..."
if config/lowfat-check-config >/dev/null 2>&1
then
CHECK=true
else
CHECK=false
fi
if [ x$CHECK != xtrue ]
then
echo -e "${GREEN}$0${OFF}: ${RED}ERROR${OFF}: configuration check failed!"
config/lowfat-check-config
exit 1
fi
echo -e "${GREEN}$0${OFF}: building the LowFat pointer info tool..."
(cd config; CC=$CLANG CFLAGS="-std=gnu99" CXX=$CLANGXX make lowfat-ptr-info >/dev/null)
if [ x$HAVE_CLANG_4 = xfalse ]
then
BOOTSTRAP_PATH=bootstrap
CLANG_TMP="$PWD/$BOOTSTRAP_PATH/bin/clang"
CLANGXX_TMP="$PWD/$BOOTSTRAP_PATH/bin/clang++"
LLVM_CONFIG_TMP="$PWD/$BOOTSTRAP_PATH/bin/llvm-config"
if [ ! -x "$CLANG_TMP" -o ! -x "$CLANGXX_TMP" -o ! -x "$LLVM_CONFIG_TMP" ]
then
echo -e \
"${GREEN}$0${OFF}: clang-4.0 is not installed; bootstrapping LLVM..."
build_llvm $BOOTSTRAP_PATH
fi
CLANG=$CLANG_TMP
CLANGXX=$CLANGXX_TMP
LLVM_CONFIG=$LLVM_CONFIG_TMP
HAVE_CLANG_4=true
fi
BUILD_PATH=build
BUILD_PLUGIN=yes
BUILD_STANDALONE=yes
build_llvm $BUILD_PATH
echo -e "${GREEN}$0${OFF}: installing the LowFat pointer info tool..."
cp config/lowfat-ptr-info $BUILD_PATH/bin/
echo -e "${GREEN}$0${OFF}: building test program..."
(cd test; make clean >/dev/null 2>&1; make >/dev/null 2>&1)
echo -n -e "${GREEN}$0${OFF}: testing LowFat build..."
if test/Test >test.tmp 2>&1
then
TEST_PASSED=true
else
TEST_PASSED=false
fi
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" < test.tmp > test.log
rm -f test.tmp
(cd test; make clean >/dev/null 2>&1)
if [ x$TEST_PASSED = xtrue ]
then
echo "ok"
else
echo "failed!"
echo -n -e \
"${GREEN}$0${OFF}: ${RED}ERROR${OFF}: LowFat failed to build correctly"
echo " (see test.log for more information)"
exit 1
fi
echo -e "${GREEN}$0${OFF}: building release package..."
rm -f "$RELEASE_NAME.tar.xz"
tar cvJ --owner root --group root -f "$RELEASE_NAME.tar.xz" "$RELEASE_NAME"
echo -e "${GREEN}$0${OFF}: build is complete!"
echo -e \
"${GREEN}$0${OFF}: clang with LowFat is available here: $PWD/bin/clang"
echo -e \
"${GREEN}$0${OFF}: clang++ with LowFat is available here: $PWD/bin/clang++"
echo -e "${YELLOW}"
echo "_| _|_|_|_| _|"
echo "_| _|_| _| _| _| _| _|_|_| _|_|_|_|"
echo "_| _| _| _| _| _| _|_|_| _| _| _|"
echo "_| _| _| _| _| _| _| _| _| _| _|"
echo "_|_|_|_| _|_| _| _| _| _|_|_| _|_|"
echo -e "${OFF}"
echo "USAGE:"
echo -e "${BOLD} $PWD/$BUILD_PATH/bin/clang -fsanitize=lowfat program.c${OFF}"
echo -e "${BOLD} $PWD/$BUILD_PATH/bin/clang++ -fsanitize=lowfat program.cpp${OFF}"
echo -e "${BOLD} $PWD/$BUILD_PATH/bin/lowfat-ptr-info <pointer-hex-value>${OFF}"
echo
echo "EXAMPLE:"
echo -e "${BOLD} \$ cd test${OFF}"
echo -e "${BOLD} \$ $PWD/$BUILD_PATH/bin/clang -fsanitize=lowfat -O2 test_input_heap.c${OFF}"
echo -e "${BOLD} \$ ./a.out 15${OFF}"
echo " Enter a string: A short string"
echo " String = \"a short string\""
echo -e "${BOLD} \$ ./a.out 15${OFF}"
echo " Enter a string: A loooooooooooooong string"
echo " LOWFAT ERROR: out-of-bounds error detected!"
echo " operation = write"
echo " pointer = 0x8dcb3a430 (heap)"
echo " base = 0x8dcb3a420"
echo " size = 16"
echo " overflow = +0"
echo
if [ $LEGACY = yes ]
then
echo "------------------------------------------------------------------"
echo -e "${YELLOW}*** LEGACY MODE WARNING ***${OFF}"
echo
echo "LowFat has been compiled in LEGACY mode for older CPUs. This mode:"
echo " (1) is not officially supported; and"
echo " (2) has some features disabled."
echo
echo "------------------------------------------------------------------"
fi