github.com/euank/go@v0.0.0-20160829210321-495514729181/misc/cgo/testsanitizers/test.bash (about) 1 #!/usr/bin/env bash 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. 5 6 # This directory is intended to test the use of Go with sanitizers 7 # like msan, asan, etc. See https://github.com/google/sanitizers . 8 9 set -e 10 11 # The sanitizers were originally developed with clang, so prefer it. 12 CC=cc 13 if test -x "$(type -p clang)"; then 14 CC=clang 15 fi 16 export CC 17 18 msan=yes 19 20 TMPDIR=${TMPDIR:-/tmp} 21 echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c 22 if $CC -fsanitize=memory -c ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$.o 2>&1 | grep "unrecognized" >& /dev/null; then 23 echo "skipping msan tests: -fsanitize=memory not supported" 24 msan=no 25 fi 26 rm -f ${TMPDIR}/testsanitizers$$.* 27 28 tsan=yes 29 30 # The memory and thread sanitizers in versions of clang before 3.6 31 # don't work with Go. 32 if test "$msan" = "yes" && $CC --version | grep clang >& /dev/null; then 33 ver=$($CC --version | sed -e 's/.* version \([0-9.-]*\).*/\1/') 34 major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/') 35 minor=$(echo $ver | sed -e 's/[0-9]*\.\([0-9]*\).*/\1/') 36 if test "$major" -lt 3 || test "$major" -eq 3 -a "$minor" -lt 6; then 37 echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.6)" 38 msan=no 39 tsan=no 40 fi 41 42 # Clang before 3.8 does not work with Linux at or after 4.1. 43 # golang.org/issue/12898. 44 if test "$msan" = "yes" -a "$major" -lt 3 || test "$major" -eq 3 -a "$minor" -lt 8; then 45 if test "$(uname)" = Linux; then 46 linuxver=$(uname -r) 47 linuxmajor=$(echo $linuxver | sed -e 's/\([0-9]*\).*/\1/') 48 linuxminor=$(echo $linuxver | sed -e 's/[0-9]*\.\([0-9]*\).*/\1/') 49 if test "$linuxmajor" -gt 4 || test "$linuxmajor" -eq 4 -a "$linuxminor" -ge 1; then 50 echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.8) incompatible with linux version $linuxmajor.$linuxminor (4.1 or newer)" 51 msan=no 52 tsan=no 53 fi 54 fi 55 fi 56 fi 57 58 status=0 59 60 if test "$msan" = "yes"; then 61 if ! go build -msan std; then 62 echo "FAIL: build -msan std" 63 status=1 64 fi 65 66 if ! go run -msan msan.go; then 67 echo "FAIL: msan" 68 status=1 69 fi 70 71 if ! CGO_LDFLAGS="-fsanitize=memory" CGO_CPPFLAGS="-fsanitize=memory" go run -msan -a msan2.go; then 72 echo "FAIL: msan2 with -fsanitize=memory" 73 status=1 74 fi 75 76 if ! go run -msan -a msan2.go; then 77 echo "FAIL: msan2" 78 status=1 79 fi 80 81 if ! go run -msan msan3.go; then 82 echo "FAIL: msan3" 83 status=1 84 fi 85 86 if ! go run -msan msan4.go; then 87 echo "FAIL: msan4" 88 status=1 89 fi 90 91 if ! go run -msan msan5.go; then 92 echo "FAIL: msan5" 93 status=1 94 fi 95 96 if go run -msan msan_fail.go 2>/dev/null; then 97 echo "FAIL: msan_fail" 98 status=1 99 fi 100 fi 101 102 if test "$tsan" = "yes"; then 103 echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c 104 ok=yes 105 if ! $CC -fsanitize=thread ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$ &> ${TMPDIR}/testsanitizers$$.err; then 106 ok=no 107 fi 108 if grep "unrecognized" ${TMPDIR}/testsanitizers$$.err >& /dev/null; then 109 echo "skipping tsan tests: -fsanitize=thread not supported" 110 tsan=no 111 elif test "$ok" != "yes"; then 112 cat ${TMPDIR}/testsanitizers$$.err 113 echo "skipping tsan tests: -fsanitizer=thread build failed" 114 tsan=no 115 fi 116 rm -f ${TMPDIR}/testsanitizers$$* 117 fi 118 119 # Run a TSAN test. 120 # $1 test name 121 # $2 environment variables 122 # $3 go run args 123 testtsan() { 124 err=${TMPDIR}/tsanerr$$.out 125 if ! env $2 go run $3 $1 2>$err; then 126 cat $err 127 echo "FAIL: $1" 128 status=1 129 elif grep -i warning $err >/dev/null 2>&1; then 130 cat $err 131 echo "FAIL: $1" 132 status=1 133 fi 134 rm -f $err 135 } 136 137 if test "$tsan" = "yes"; then 138 testtsan tsan.go 139 testtsan tsan2.go 140 testtsan tsan3.go 141 testtsan tsan4.go 142 143 # These tests are only reliable using clang or GCC version 7 or later. 144 # Otherwise runtime/cgo/libcgo.h can't tell whether TSAN is in use. 145 ok=false 146 if ${CC} --version | grep clang >/dev/null 2>&1; then 147 ok=true 148 else 149 ver=$($CC -dumpversion) 150 major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/') 151 if test "$major" -lt 7; then 152 echo "skipping remaining TSAN tests: GCC version $major (older than 7)" 153 else 154 ok=true 155 fi 156 fi 157 158 if test "$ok" = "true"; then 159 # This test requires rebuilding os/user with -fsanitize=thread. 160 testtsan tsan5.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan" 161 162 # This test requires rebuilding runtime/cgo with -fsanitize=thread. 163 testtsan tsan6.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan" 164 165 # This test requires rebuilding runtime/cgo with -fsanitize=thread. 166 testtsan tsan7.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan" 167 fi 168 fi 169 170 exit $status