Skip to content

Instantly share code, notes, and snippets.

@azat
Last active August 29, 2015 14:27
Show Gist options
  • Save azat/e526028be0ec37c791e7 to your computer and use it in GitHub Desktop.
Save azat/e526028be0ec37c791e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Compile and run under sanitizers, that compiler have (we will use clang).
# Sanitizers will check more then valgrind, because under valgrind we check
# only {db,db-indexes,db-ck} while using sanitizers we can test all utils.
#
set -e
function llvm_latest()
{
(ls -t /usr/bin/$@-?.? 2>/dev/null || echo $(which $@)) | sort -Vr | head -1
}
function printUsage()
{
cat <<EOF
$0 [ OPTS ] [ BINARY ] [ BINARY OPTS ]
-b - binary dir prefix (will be exported via PATH)
-s - sanitizers to run on
-r - root (path with CMakeLists.txt)
-N - don not append sanitizer name (but install env)
EOF
}
self="$(readlink -f "$0")" && self="$(dirname "$self")"
root="$(readlink -f "$self/../")"
pathBak="$PATH"
symbolizer=$(llvm_latest llvm-symbolizer)
clang=$(llvm_latest clang)
clangcxx=$(llvm_latest clang++)
sanitizers=(SANITIZETHREAD SANITIZEADDRESS)
binDirPrefix="build/bin"
appendSanitizer=1
while getopts "b:s:r:N" c; do
case $c in
b) binDirPrefix="$OPTARG";;
s) sanitizers=( $OPTARG );;
r) root="$OPTARG";;
N) appendSanitizer="0";;
*) printUsage; exit 1;;
esac
done
shift $((OPTIND - 1))
function log()
{
echo "# --------------------- " $* " -------------------- #"
}
# https://code.google.com/p/thread-sanitizer/wiki/CppManual
# https://bugzilla.kernel.org/show_bug.cgi?id=66721 (fixed in XXX)
function gdb()
{
command gdb -ex 'set disable-randomization off' "$@"
}
export -f gdb
log "Using symbolizer: $symbolizer"
for name in ${sanitizers[@]}; do
log $name
dir="$root/.test-$name"
mkdir -p "$dir"
cd "$dir"
cmake \
-DCMAKE_CXX_COMPILER=$clangcxx -DCMAKE_C_COMPILER=$clang \
-DCMAKE_BUILD_TYPE=$name \
$root
make -j
export MSAN_SYMBOLIZER_PATH="$symbolizer" \
ASAN_SYMBOLIZER_PATH="$symbolizer" \
TSAN_OPTIONS="external_symbolizer_path=$symbolizer" \
PATH=$PWD/$binDirPrefix:$pathBak
export SANITIZER_NAME=$name
if [ $appendSanitizer -eq 1 ]; then
$@ $name
else
$@
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment