github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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  if test "$tsan" = "yes"; then
   115      err=${TMPDIR}/tsanerr$$.out
   116  
   117      if ! go run tsan.go 2>$err; then
   118  	cat $err
   119  	echo "FAIL: tsan"
   120  	status=1
   121      elif grep -i warning $err >/dev/null 2>&1; then
   122  	cat $err
   123  	echo "FAIL: tsan"
   124  	status=1
   125      fi
   126  
   127      if ! go run tsan2.go 2>$err; then
   128  	cat $err
   129  	echo "FAIL: tsan2"
   130  	status=1
   131      elif grep -i warning $err >/dev/null 2>&1; then
   132  	cat $err
   133  	echo "FAIL: tsan2"
   134  	status=1
   135      fi
   136  
   137      rm -f $err
   138  fi
   139  
   140  exit $status