2015-09-29 22:24:13 -06:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style
|
|
|
|
# license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
# This directory is intended to test the use of Go with sanitizers
|
|
|
|
# like msan, asan, etc. See https://github.com/google/sanitizers .
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# The sanitizers were originally developed with clang, so prefer it.
|
|
|
|
CC=cc
|
2015-11-09 13:22:52 -07:00
|
|
|
if test -x "$(type -p clang)"; then
|
2015-09-29 22:24:13 -06:00
|
|
|
CC=clang
|
|
|
|
fi
|
|
|
|
export CC
|
|
|
|
|
2016-11-02 14:33:33 -06:00
|
|
|
if [ "$(sysctl -n vm.overcommit_memory)" = 2 ]; then
|
|
|
|
echo "skipping msan/tsan tests: vm.overcommit_memory=2" >&2
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
msan=yes
|
|
|
|
|
2015-11-03 08:14:22 -07:00
|
|
|
TMPDIR=${TMPDIR:-/tmp}
|
2016-02-29 10:31:12 -07:00
|
|
|
echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
|
2016-12-15 17:57:59 -07:00
|
|
|
if $CC -fsanitize=memory -o ${TMPDIR}/testsanitizers$$ ${TMPDIR}/testsanitizers$$.c 2>&1 | grep "unrecognized" >& /dev/null; then
|
|
|
|
echo "skipping msan tests: $CC -fsanitize=memory not supported"
|
|
|
|
msan=no
|
|
|
|
elif ! test -x ${TMPDIR}/testsanitizers$$; then
|
|
|
|
echo "skipping msan tests: $CC -fsanitize-memory did not generate an executable"
|
|
|
|
msan=no
|
|
|
|
elif ! ${TMPDIR}/testsanitizers$$ >/dev/null 2>&1; then
|
|
|
|
echo "skipping msan tests: $CC -fsanitize-memory generates broken executable"
|
2015-12-03 20:17:21 -07:00
|
|
|
msan=no
|
2015-09-29 22:24:13 -06:00
|
|
|
fi
|
2015-11-03 08:14:22 -07:00
|
|
|
rm -f ${TMPDIR}/testsanitizers$$.*
|
2015-10-02 08:04:34 -06:00
|
|
|
|
2016-02-29 11:05:46 -07:00
|
|
|
tsan=yes
|
|
|
|
|
|
|
|
# The memory and thread sanitizers in versions of clang before 3.6
|
|
|
|
# don't work with Go.
|
2015-12-03 20:17:21 -07:00
|
|
|
if test "$msan" = "yes" && $CC --version | grep clang >& /dev/null; then
|
2015-10-02 08:04:34 -06:00
|
|
|
ver=$($CC --version | sed -e 's/.* version \([0-9.-]*\).*/\1/')
|
|
|
|
major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/')
|
|
|
|
minor=$(echo $ver | sed -e 's/[0-9]*\.\([0-9]*\).*/\1/')
|
2015-11-30 12:50:21 -07:00
|
|
|
if test "$major" -lt 3 || test "$major" -eq 3 -a "$minor" -lt 6; then
|
2016-02-29 11:05:46 -07:00
|
|
|
echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.6)"
|
2015-12-03 20:17:21 -07:00
|
|
|
msan=no
|
2016-02-29 11:05:46 -07:00
|
|
|
tsan=no
|
2015-10-02 08:04:34 -06:00
|
|
|
fi
|
2015-12-03 12:11:44 -07:00
|
|
|
|
2015-11-30 12:50:21 -07:00
|
|
|
# Clang before 3.8 does not work with Linux at or after 4.1.
|
|
|
|
# golang.org/issue/12898.
|
2015-12-03 20:17:21 -07:00
|
|
|
if test "$msan" = "yes" -a "$major" -lt 3 || test "$major" -eq 3 -a "$minor" -lt 8; then
|
2015-11-30 12:50:21 -07:00
|
|
|
if test "$(uname)" = Linux; then
|
|
|
|
linuxver=$(uname -r)
|
2015-12-03 12:11:44 -07:00
|
|
|
linuxmajor=$(echo $linuxver | sed -e 's/\([0-9]*\).*/\1/')
|
|
|
|
linuxminor=$(echo $linuxver | sed -e 's/[0-9]*\.\([0-9]*\).*/\1/')
|
2015-11-30 12:50:21 -07:00
|
|
|
if test "$linuxmajor" -gt 4 || test "$linuxmajor" -eq 4 -a "$linuxminor" -ge 1; then
|
2016-02-29 11:05:46 -07:00
|
|
|
echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.8) incompatible with linux version $linuxmajor.$linuxminor (4.1 or newer)"
|
2015-12-03 20:17:21 -07:00
|
|
|
msan=no
|
2016-02-29 11:05:46 -07:00
|
|
|
tsan=no
|
2015-11-30 12:50:21 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2015-10-02 08:04:34 -06:00
|
|
|
fi
|
|
|
|
|
2015-10-21 13:33:56 -06:00
|
|
|
status=0
|
|
|
|
|
2017-01-18 13:12:18 -07:00
|
|
|
testmsanshared() {
|
|
|
|
goos=$(go env GOOS)
|
|
|
|
suffix="-installsuffix testsanitizers"
|
|
|
|
libext="so"
|
2017-03-10 10:15:46 -07:00
|
|
|
if [ "$goos" = "darwin" ]; then
|
2017-01-18 13:12:18 -07:00
|
|
|
libext="dylib"
|
|
|
|
fi
|
|
|
|
go build -msan -buildmode=c-shared $suffix -o ${TMPDIR}/libmsanshared.$libext msan_shared.go
|
|
|
|
|
2017-03-07 16:50:52 -07:00
|
|
|
echo 'int main() { return 0; }' > ${TMPDIR}/testmsanshared.c
|
2017-01-18 13:12:18 -07:00
|
|
|
$CC $(go env GOGCCFLAGS) -fsanitize=memory -o ${TMPDIR}/testmsanshared ${TMPDIR}/testmsanshared.c ${TMPDIR}/libmsanshared.$libext
|
|
|
|
|
|
|
|
if ! LD_LIBRARY_PATH=. ${TMPDIR}/testmsanshared; then
|
|
|
|
echo "FAIL: msan_shared"
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
rm -f ${TMPDIR}/{testmsanshared,testmsanshared.c,libmsanshared.$libext}
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
if test "$msan" = "yes"; then
|
|
|
|
if ! go build -msan std; then
|
|
|
|
echo "FAIL: build -msan std"
|
|
|
|
status=1
|
|
|
|
fi
|
2015-10-26 14:58:23 -06:00
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
if ! go run -msan msan.go; then
|
|
|
|
echo "FAIL: msan"
|
|
|
|
status=1
|
|
|
|
fi
|
2015-10-21 13:33:56 -06:00
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
if ! CGO_LDFLAGS="-fsanitize=memory" CGO_CPPFLAGS="-fsanitize=memory" go run -msan -a msan2.go; then
|
|
|
|
echo "FAIL: msan2 with -fsanitize=memory"
|
|
|
|
status=1
|
|
|
|
fi
|
2016-01-05 15:06:58 -07:00
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
if ! go run -msan -a msan2.go; then
|
|
|
|
echo "FAIL: msan2"
|
|
|
|
status=1
|
|
|
|
fi
|
2015-10-21 13:33:56 -06:00
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
if ! go run -msan msan3.go; then
|
|
|
|
echo "FAIL: msan3"
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! go run -msan msan4.go; then
|
|
|
|
echo "FAIL: msan4"
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
|
2016-07-06 16:30:33 -06:00
|
|
|
if ! go run -msan msan5.go; then
|
|
|
|
echo "FAIL: msan5"
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
if go run -msan msan_fail.go 2>/dev/null; then
|
|
|
|
echo "FAIL: msan_fail"
|
|
|
|
status=1
|
|
|
|
fi
|
2017-01-18 13:12:18 -07:00
|
|
|
|
|
|
|
testmsanshared
|
2015-11-03 18:15:30 -07:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:50:52 -07:00
|
|
|
testtsanshared() {
|
|
|
|
goos=$(go env GOOS)
|
|
|
|
suffix="-installsuffix tsan"
|
|
|
|
libext="so"
|
2017-03-10 10:15:46 -07:00
|
|
|
if [ "$goos" = "darwin" ]; then
|
2017-03-07 16:50:52 -07:00
|
|
|
libext="dylib"
|
|
|
|
fi
|
|
|
|
go build -buildmode=c-shared $suffix -o ${TMPDIR}/libtsanshared.$libext tsan_shared.go
|
|
|
|
|
|
|
|
echo 'int main() { return 0; }' > ${TMPDIR}/testtsanshared.c
|
|
|
|
$CC $(go env GOGCCFLAGS) -fsanitize=thread -o ${TMPDIR}/testtsanshared ${TMPDIR}/testtsanshared.c ${TMPDIR}/libtsanshared.$libext
|
|
|
|
|
|
|
|
if ! LD_LIBRARY_PATH=. ${TMPDIR}/testtsanshared; then
|
|
|
|
echo "FAIL: tsan_shared"
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
rm -f ${TMPDIR}/{testtsanshared,testtsanshared.c,libtsanshared.$libext}
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:05:46 -07:00
|
|
|
if test "$tsan" = "yes"; then
|
|
|
|
echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
|
|
|
|
ok=yes
|
|
|
|
if ! $CC -fsanitize=thread ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$ &> ${TMPDIR}/testsanitizers$$.err; then
|
|
|
|
ok=no
|
|
|
|
fi
|
2017-08-02 11:49:20 -06:00
|
|
|
if grep "unrecognized" ${TMPDIR}/testsanitizers$$.err >& /dev/null; then
|
2016-02-29 11:05:46 -07:00
|
|
|
echo "skipping tsan tests: -fsanitize=thread not supported"
|
|
|
|
tsan=no
|
2017-08-02 11:49:20 -06:00
|
|
|
elif test "$ok" != "yes"; then
|
|
|
|
cat ${TMPDIR}/testsanitizers$$.err
|
|
|
|
echo "skipping tsan tests: -fsanitizer=thread build failed"
|
|
|
|
tsan=no
|
|
|
|
elif ! ${TMPDIR}/testsanitizers$$ 2>&1; then
|
|
|
|
echo "skipping tsan tests: running tsan program failed"
|
|
|
|
tsan=no
|
|
|
|
fi
|
|
|
|
rm -f ${TMPDIR}/testsanitizers$$*
|
2015-11-04 13:30:30 -07:00
|
|
|
fi
|
2015-12-03 20:17:21 -07:00
|
|
|
|
2016-06-03 11:49:24 -06:00
|
|
|
# Run a TSAN test.
|
|
|
|
# $1 test name
|
|
|
|
# $2 environment variables
|
|
|
|
# $3 go run args
|
|
|
|
testtsan() {
|
2015-12-03 20:17:21 -07:00
|
|
|
err=${TMPDIR}/tsanerr$$.out
|
2016-06-03 11:49:24 -06:00
|
|
|
if ! env $2 go run $3 $1 2>$err; then
|
2016-05-19 11:07:41 -06:00
|
|
|
cat $err
|
2016-06-03 11:49:24 -06:00
|
|
|
echo "FAIL: $1"
|
2016-05-19 11:07:41 -06:00
|
|
|
status=1
|
|
|
|
elif grep -i warning $err >/dev/null 2>&1; then
|
|
|
|
cat $err
|
2016-06-03 11:49:24 -06:00
|
|
|
echo "FAIL: $1"
|
2016-05-19 11:07:41 -06:00
|
|
|
status=1
|
|
|
|
fi
|
2016-06-03 11:49:24 -06:00
|
|
|
rm -f $err
|
|
|
|
}
|
2016-05-19 11:07:41 -06:00
|
|
|
|
2016-06-03 11:49:24 -06:00
|
|
|
if test "$tsan" = "yes"; then
|
|
|
|
testtsan tsan.go
|
|
|
|
testtsan tsan2.go
|
|
|
|
testtsan tsan3.go
|
|
|
|
testtsan tsan4.go
|
2016-11-09 13:28:24 -07:00
|
|
|
testtsan tsan8.go
|
2016-12-08 18:39:00 -07:00
|
|
|
testtsan tsan9.go
|
2016-05-19 17:27:23 -06:00
|
|
|
|
2016-06-06 22:44:24 -06:00
|
|
|
# These tests are only reliable using clang or GCC version 7 or later.
|
|
|
|
# Otherwise runtime/cgo/libcgo.h can't tell whether TSAN is in use.
|
|
|
|
ok=false
|
2017-08-02 11:49:20 -06:00
|
|
|
clang=false
|
2016-06-06 22:44:24 -06:00
|
|
|
if ${CC} --version | grep clang >/dev/null 2>&1; then
|
|
|
|
ok=true
|
2017-08-02 11:49:20 -06:00
|
|
|
clang=true
|
2016-06-06 22:44:24 -06:00
|
|
|
else
|
|
|
|
ver=$($CC -dumpversion)
|
|
|
|
major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/')
|
|
|
|
if test "$major" -lt 7; then
|
|
|
|
echo "skipping remaining TSAN tests: GCC version $major (older than 7)"
|
|
|
|
else
|
|
|
|
ok=true
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test "$ok" = "true"; then
|
2017-03-08 13:40:28 -07:00
|
|
|
# These tests require rebuilding os/user with -fsanitize=thread.
|
2016-06-06 22:44:24 -06:00
|
|
|
testtsan tsan5.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
|
|
|
testtsan tsan6.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
2016-08-02 12:42:53 -06:00
|
|
|
testtsan tsan7.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
2017-08-02 11:49:20 -06:00
|
|
|
|
2017-08-04 16:33:48 -06:00
|
|
|
# The remaining tests reportedly hang when built with GCC; issue #21196.
|
2017-08-02 11:49:20 -06:00
|
|
|
if test "$clang" = "true"; then
|
|
|
|
testtsan tsan10.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
2017-08-04 16:33:48 -06:00
|
|
|
testtsan tsan11.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
|
|
|
testtsan tsan12.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
|
2017-08-02 11:49:20 -06:00
|
|
|
fi
|
|
|
|
|
2017-03-07 16:50:52 -07:00
|
|
|
testtsanshared
|
2016-06-06 22:44:24 -06:00
|
|
|
fi
|
2015-10-21 13:33:56 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit $status
|