Update BuildTools, CoreClr, CoreFx, PgoData to rc1-02719-01, rc1-26419-03, rc1-26419...
[platform/upstream/coreclr.git] / enablesanitizers.sh
1 #!/usr/bin/env bash
2
3 if [ $# -eq 0 ]; then
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]"
9     else
10         echo " cd $(dirname $0);. enablesanitizers.sh [options]; cd -"
11     fi
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"
19 else
20     # default to clang 3.6 instead of 3.5 because it supports print_stacktrace (otherwise only one stack frame)
21     __ClangMajorVersion=3
22     __ClangMinorVersion=6
23
24     __EnableASan=0
25     __EnableUBSan=0
26     __EnableLSan=0
27     __TurnOff=0
28     __Options=
29     __ExportSymbolizerPath=1
30
31     for i in "$@"
32         do
33             lowerI="$(echo $i | awk '{print tolower($0)}')"
34             case $lowerI in
35             asan)
36                 __EnableASan=1
37                 ;;
38             ubsan)
39                 __EnableUBSan=1
40                 ;;
41             lsan)
42                 __EnableASan=1
43                 __EnableLSan=1
44                 ;;
45             all)
46                 __EnableASan=1
47                 __EnableUBSan=1
48                 __EnableLSan=1
49                 ;;
50             off)
51                 __TurnOff=1
52                 ;;
53             clang3.5)
54                 __ClangMajorVersion=3
55                 __ClangMinorVersion=5
56                 ;;
57             clang3.6)
58                 __ClangMajorVersion=3
59                 __ClangMinorVersion=6
60                 ;;
61             clang3.7)
62                 __ClangMajorVersion=3
63                 __ClangMinorVersion=7
64                 __ExportSymbolizerPath=0
65                 ;;
66             clang3.8)
67                 __ClangMajorVersion=3
68                 __ClangMinorVersion=8
69                 __ExportSymbolizerPath=0
70                 ;;
71             clang3.9)
72                 __ClangMajorVersion=3
73                 __ClangMinorVersion=9
74                 __ExportSymbolizerPath=0
75                 ;;
76             *)
77                 echo "Unknown arg: $i"
78                 return 1
79         esac
80     done
81
82     if [ $__TurnOff == 1 ]; then
83         unset DEBUG_SANITIZERS
84         echo "Setting DEBUG_SANITIZERS="
85     else
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"
91
92         if [ $__EnableASan == 1 ]; then
93             __Options="$__Options asan"
94         fi
95         if [ $__EnableUBSan == 1 ]; then
96             __Options="$__Options ubsan"
97         fi
98         if [ $__EnableLSan == 1 ]; then
99             ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=1"
100         else
101             ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=0"
102         fi
103
104         # passed to build.sh
105         DEBUG_SANITIZERS="$__Options"
106         export DEBUG_SANITIZERS
107         echo "Setting DEBUG_SANITIZERS=$DEBUG_SANITIZERS"
108
109         # used by ASan at run-time
110         export ASAN_OPTIONS
111         echo "Setting ASAN_OPTIONS=\"$ASAN_OPTIONS\""
112
113         export UBSAN_OPTIONS
114         echo "Setting UBSAN_OPTIONS=\"$UBSAN_OPTIONS\""
115
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"
123         else
124             unset ASAN_SYMBOLIZER_PATH
125         fi
126         echo "Done. You can now run: build.sh Debug clang$__ClangMajorVersion.$__ClangMinorVersion"
127     fi
128
129     unset __ClangMajorVersion
130     unset __ClangMinorVersion
131     unset __EnableASan
132     unset __EnableUBSan
133     unset __EnableLSan
134     unset __TurnOff
135     unset __Options
136 fi