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
|
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
msan=yes
|
|
|
|
|
2015-11-03 08:14:22 -07:00
|
|
|
TMPDIR=${TMPDIR:-/tmp}
|
|
|
|
echo > ${TMPDIR}/testsanitizers$$.c
|
2015-11-03 18:15:30 -07:00
|
|
|
if $CC -fsanitize=memory -c ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$.o 2>&1 | grep "unrecognized" >& /dev/null; then
|
2015-12-03 20:17:21 -07:00
|
|
|
echo "skipping msan tests: -fsanitize=memory not supported"
|
|
|
|
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
|
|
|
|
|
|
|
# The memory sanitizer 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
|
2015-12-03 20:17:21 -07:00
|
|
|
echo "skipping msan tests: clang version $major.$minor (older than 3.6)"
|
|
|
|
msan=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
|
2015-12-03 20:17:21 -07:00
|
|
|
echo "skipping msan tests: clang version $major.$minor (older than 3.8) incompatible with linux version $linuxmajor.$linuxminor (4.1 or newer)"
|
|
|
|
msan=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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if go run -msan msan_fail.go 2>/dev/null; then
|
|
|
|
echo "FAIL: msan_fail"
|
|
|
|
status=1
|
|
|
|
fi
|
2015-11-03 18:15:30 -07:00
|
|
|
fi
|
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
tsan=yes
|
|
|
|
|
|
|
|
TMPDIR=${TMPDIR:-/tmp}
|
|
|
|
echo > ${TMPDIR}/testsanitizers$$.c
|
|
|
|
if $CC -fsanitize=thread -c ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$.o 2>&1 | grep "unrecognized" >& /dev/null; then
|
|
|
|
echo "skipping tsan tests: -fsanitize=thread not supported"
|
|
|
|
tsan=no
|
2015-11-04 13:30:30 -07:00
|
|
|
fi
|
2015-12-03 20:17:21 -07:00
|
|
|
rm -f ${TMPDIR}/testsanitizers$$.*
|
|
|
|
|
|
|
|
if test "$tsan" = "yes"; then
|
|
|
|
err=${TMPDIR}/tsanerr$$.out
|
|
|
|
|
|
|
|
if ! go run tsan.go 2>$err; then
|
|
|
|
echo "FAIL: tsan"
|
|
|
|
status=1
|
|
|
|
elif grep -i warning $err >/dev/null 2>&1; then
|
|
|
|
cat $err
|
|
|
|
echo "FAIL: tsan"
|
|
|
|
status=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! go run tsan2.go 2>$err; then
|
|
|
|
echo "FAIL: tsan2"
|
|
|
|
status=1
|
|
|
|
elif grep -i warning $err >/dev/null 2>&1; then
|
|
|
|
cat $err
|
|
|
|
echo "FAIL: tsan2"
|
|
|
|
status=1
|
|
|
|
fi
|
2015-11-04 13:30:30 -07:00
|
|
|
|
2015-12-03 20:17:21 -07:00
|
|
|
rm -f $err
|
2015-10-21 13:33:56 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
exit $status
|