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