github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/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 msan_fail.go 2>/dev/null; then
    92  	echo "FAIL: msan_fail"
    93  	status=1
    94      fi
    95  fi
    96  
    97  if test "$tsan" = "yes"; then
    98      echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
    99      ok=yes
   100      if ! $CC -fsanitize=thread ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$ &> ${TMPDIR}/testsanitizers$$.err; then
   101  	ok=no
   102      fi
   103       if grep "unrecognized" ${TMPDIR}/testsanitizers$$.err >& /dev/null; then
   104  	echo "skipping tsan tests: -fsanitize=thread not supported"
   105  	tsan=no
   106       elif test "$ok" != "yes"; then
   107  	 cat ${TMPDIR}/testsanitizers$$.err
   108  	 echo "skipping tsan tests: -fsanitizer=thread build failed"
   109  	 tsan=no
   110       fi
   111       rm -f ${TMPDIR}/testsanitizers$$*
   112  fi
   113  
   114  # Run a TSAN test.
   115  # $1 test name
   116  # $2 environment variables
   117  # $3 go run args
   118  testtsan() {
   119      err=${TMPDIR}/tsanerr$$.out
   120      if ! env $2 go run $3 $1 2>$err; then
   121  	cat $err
   122  	echo "FAIL: $1"
   123  	status=1
   124      elif grep -i warning $err >/dev/null 2>&1; then
   125  	cat $err
   126  	echo "FAIL: $1"
   127  	status=1
   128      fi
   129      rm -f $err
   130  }
   131  
   132  if test "$tsan" = "yes"; then
   133      testtsan tsan.go
   134      testtsan tsan2.go
   135      testtsan tsan3.go
   136      testtsan tsan4.go
   137  
   138      # These tests are only reliable using clang or GCC version 7 or later.
   139      # Otherwise runtime/cgo/libcgo.h can't tell whether TSAN is in use.
   140      ok=false
   141      if ${CC} --version | grep clang >/dev/null 2>&1; then
   142  	ok=true
   143      else
   144  	ver=$($CC -dumpversion)
   145  	major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/')
   146  	if test "$major" -lt 7; then
   147  	    echo "skipping remaining TSAN tests: GCC version $major (older than 7)"
   148  	else
   149  	    ok=true
   150  	fi
   151      fi
   152  
   153      if test "$ok" = "true"; then
   154  	# This test requires rebuilding os/user with -fsanitize=thread.
   155  	testtsan tsan5.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
   156  
   157  	# This test requires rebuilding runtime/cgo with -fsanitize=thread.
   158  	testtsan tsan6.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
   159      fi
   160  fi
   161  
   162  exit $status