4 echo "Script for enabling CLang sanitizers for debug builds."
5 echo "*Only tested on Ubuntu x64."
6 echo "*This script must be 'sourced' (via dot+space) so that changes to environment variables are preserved. Run like this:"
7 if [ "$(dirname $0)" = "." ]; then
8 echo " . enablesanitizers.sh [options]"
10 echo " cd $(dirname $0);. enablesanitizers.sh [options]; cd -"
12 echo "Usage: [asan] [ubsan] [lsan] [all] [off] [clangx.y]"
13 echo " asan: optional argument to enable Address Sanitizer."
14 echo " ubsan: optional argument to enable Undefined Behavior Sanitizer."
15 echo " lsan - optional argument to enable memory Leak Sanitizer."
16 echo " all - optional argument to enable asan, ubsan and lsan."
17 echo " off - optional argument to turn off all sanitizers."
18 echo " clangx.y - optional argument to specify clang version x.y. which is used to resolve stack traces. Default is 3.6"
20 # default to clang 3.6 instead of 3.5 because it supports print_stacktrace (otherwise only one stack frame)
29 __ExportSymbolizerPath=1
33 lowerI="$(echo $i | awk '{print tolower($0)}')"
64 __ExportSymbolizerPath=0
69 __ExportSymbolizerPath=0
74 __ExportSymbolizerPath=0
77 echo "Unknown arg: $i"
82 if [ $__TurnOff == 1 ]; then
83 unset DEBUG_SANITIZERS
84 echo "Setting DEBUG_SANITIZERS="
86 # for now, specify alloc_dealloc_mismatch=0 as there are too many error reports that are not an issue.
87 # Also specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers
88 ASAN_OPTIONS="symbolize=1 alloc_dealloc_mismatch=0 use_sigaltstack=0"
89 # when Clang 3.8 available, add: suppressions=$(readlink -f sanitizersuppressions.txt)
90 UBSAN_OPTIONS="print_stacktrace=1"
92 if [ $__EnableASan == 1 ]; then
93 __Options="$__Options asan"
95 if [ $__EnableUBSan == 1 ]; then
96 __Options="$__Options ubsan"
98 if [ $__EnableLSan == 1 ]; then
99 ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=1"
101 ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=0"
105 DEBUG_SANITIZERS="$__Options"
106 export DEBUG_SANITIZERS
107 echo "Setting DEBUG_SANITIZERS=$DEBUG_SANITIZERS"
109 # used by ASan at run-time
111 echo "Setting ASAN_OPTIONS=\"$ASAN_OPTIONS\""
114 echo "Setting UBSAN_OPTIONS=\"$UBSAN_OPTIONS\""
116 # for compiler-rt > 3.6 Asan check that binary name is 'llvm-symbolizer', 'addr2line' or
117 # 'atos' (for Darwin) otherwise it returns error
118 if [ $__ExportSymbolizerPath == 1 ]; then
119 # used by ASan at run-time
120 ASAN_SYMBOLIZER_PATH="/usr/bin/llvm-symbolizer-$__ClangMajorVersion.$__ClangMinorVersion"
121 export ASAN_SYMBOLIZER_PATH
122 echo "Setting ASAN_SYMBOLIZER_PATH=$ASAN_SYMBOLIZER_PATH"
124 unset ASAN_SYMBOLIZER_PATH
126 echo "Done. You can now run: build.sh Debug clang$__ClangMajorVersion.$__ClangMinorVersion"
129 unset __ClangMajorVersion
130 unset __ClangMinorVersion