2 # Copyright 2015 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # This directory is intended to test the use of Go with sanitizers
7 # like msan, asan, etc. See https://github.com/google/sanitizers .
11 # The sanitizers were originally developed with clang, so prefer it.
13 if test -x "$(type -p clang)"; then
18 if [ "$(sysctl -n vm.overcommit_memory)" = 2 ]; then
19 echo "skipping msan/tsan tests: vm.overcommit_memory=2" >&2
25 TMPDIR
=${TMPDIR:-/tmp}
26 echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
27 if $CC -fsanitize=memory
-o ${TMPDIR}/testsanitizers$$
${TMPDIR}/testsanitizers$$.c
2>&1 |
grep "unrecognized" >& /dev
/null
; then
28 echo "skipping msan tests: $CC -fsanitize=memory not supported"
30 elif ! test -x ${TMPDIR}/testsanitizers$$
; then
31 echo "skipping msan tests: $CC -fsanitize-memory did not generate an executable"
33 elif ! ${TMPDIR}/testsanitizers$$
>/dev
/null
2>&1; then
34 echo "skipping msan tests: $CC -fsanitize-memory generates broken executable"
37 rm -f ${TMPDIR}/testsanitizers$$.
*
41 # The memory and thread sanitizers in versions of clang before 3.6
43 if test "$msan" = "yes" && $CC --version |
grep clang
>& /dev
/null
; then
44 ver
=$
($CC --version |
sed -e 's/.* version \([0-9.-]*\).*/\1/')
45 major
=$
(echo $ver |
sed -e 's/\([0-9]*\).*/\1/')
46 minor
=$
(echo $ver |
sed -e 's/[0-9]*\.\([0-9]*\).*/\1/')
47 if test "$major" -lt 3 ||
test "$major" -eq 3 -a "$minor" -lt 6; then
48 echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.6)"
53 # Clang before 3.8 does not work with Linux at or after 4.1.
54 # golang.org/issue/12898.
55 if test "$msan" = "yes" -a "$major" -lt 3 ||
test "$major" -eq 3 -a "$minor" -lt 8; then
56 if test "$(uname)" = Linux
; then
58 linuxmajor
=$
(echo $linuxver |
sed -e 's/\([0-9]*\).*/\1/')
59 linuxminor
=$
(echo $linuxver |
sed -e 's/[0-9]*\.\([0-9]*\).*/\1/')
60 if test "$linuxmajor" -gt 4 ||
test "$linuxmajor" -eq 4 -a "$linuxminor" -ge 1; then
61 echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.8) incompatible with linux version $linuxmajor.$linuxminor (4.1 or newer)"
73 suffix
="-installsuffix testsanitizers"
75 if [ "$goos" = "darwin" ]; then
78 go build
-msan -buildmode=c-shared
$suffix -o ${TMPDIR}/libmsanshared.
$libext msan_shared.go
80 echo 'int main() { return 0; }' > ${TMPDIR}/testmsanshared.c
81 $CC $
(go env GOGCCFLAGS
) -fsanitize=memory
-o ${TMPDIR}/testmsanshared ${TMPDIR}/testmsanshared.c ${TMPDIR}/libmsanshared.
$libext
83 if ! LD_LIBRARY_PATH
=.
${TMPDIR}/testmsanshared
; then
84 echo "FAIL: msan_shared"
87 rm -f ${TMPDIR}/{testmsanshared
,testmsanshared.c
,libmsanshared.
$libext}
90 if test "$msan" = "yes"; then
91 if ! go build
-msan std
; then
92 echo "FAIL: build -msan std"
96 if ! go run
-msan msan.go
; then
101 if ! CGO_LDFLAGS
="-fsanitize=memory" CGO_CPPFLAGS
="-fsanitize=memory" go run
-msan -a msan2.go
; then
102 echo "FAIL: msan2 with -fsanitize=memory"
106 if ! go run
-msan -a msan2.go
; then
111 if ! go run
-msan msan3.go
; then
116 if ! go run
-msan msan4.go
; then
121 if ! go run
-msan msan5.go
; then
126 if go run
-msan msan_fail.go
2>/dev
/null
; then
127 echo "FAIL: msan_fail"
136 suffix
="-installsuffix tsan"
138 if [ "$goos" = "darwin" ]; then
141 go build
-buildmode=c-shared
$suffix -o ${TMPDIR}/libtsanshared.
$libext tsan_shared.go
143 echo 'int main() { return 0; }' > ${TMPDIR}/testtsanshared.c
144 $CC $
(go env GOGCCFLAGS
) -fsanitize=thread
-o ${TMPDIR}/testtsanshared ${TMPDIR}/testtsanshared.c ${TMPDIR}/libtsanshared.
$libext
146 if ! LD_LIBRARY_PATH
=.
${TMPDIR}/testtsanshared
; then
147 echo "FAIL: tsan_shared"
150 rm -f ${TMPDIR}/{testtsanshared
,testtsanshared.c
,libtsanshared.
$libext}
153 if test "$tsan" = "yes"; then
154 echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
156 if ! $CC -fsanitize=thread
${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$ &> ${TMPDIR}/testsanitizers$$.err
; then
159 if grep "unrecognized" ${TMPDIR}/testsanitizers$$.err
>& /dev
/null
; then
160 echo "skipping tsan tests: -fsanitize=thread not supported"
162 elif test "$ok" != "yes"; then
163 cat ${TMPDIR}/testsanitizers$$.err
164 echo "skipping tsan tests: -fsanitizer=thread build failed"
166 elif ! ${TMPDIR}/testsanitizers$$
2>&1; then
167 echo "skipping tsan tests: running tsan program failed"
170 rm -f ${TMPDIR}/testsanitizers$$
*
175 # $2 environment variables
178 err
=${TMPDIR}/tsanerr$$.out
179 if ! env
$2 go run
$3 $1 2>$err; then
183 elif grep -i warning
$err >/dev
/null
2>&1; then
191 if test "$tsan" = "yes"; then
199 # These tests are only reliable using clang or GCC version 7 or later.
200 # Otherwise runtime/cgo/libcgo.h can't tell whether TSAN is in use.
203 if ${CC} --version |
grep clang
>/dev
/null
2>&1; then
207 ver
=$
($CC -dumpversion)
208 major
=$
(echo $ver |
sed -e 's/\([0-9]*\).*/\1/')
209 if test "$major" -lt 7; then
210 echo "skipping remaining TSAN tests: GCC version $major (older than 7)"
216 if test "$ok" = "true"; then
217 # These tests require rebuilding os/user with -fsanitize=thread.
218 testtsan tsan5.go
"CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
219 testtsan tsan6.go
"CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
220 testtsan tsan7.go
"CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
222 # The remaining tests reportedly hang when built with GCC; issue #21196.
223 if test "$clang" = "true"; then
224 testtsan tsan10.go
"CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
225 testtsan tsan11.go
"CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
226 testtsan tsan12.go
"CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"