libgo: update to go1.9
[official-gcc.git] / libgo / misc / cgo / testsanitizers / test.bash
blob9f80af6c507557be2eefecb0edb28a343311b2bb
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.
6 # This directory is intended to test the use of Go with sanitizers
7 # like msan, asan, etc. See https://github.com/google/sanitizers .
9 set -e
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
16 export CC
18 if [ "$(sysctl -n vm.overcommit_memory)" = 2 ]; then
19 echo "skipping msan/tsan tests: vm.overcommit_memory=2" >&2
20 exit 0
23 msan=yes
25 TMPDIR=${TMPDIR:-/tmp}
26 echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
27 if $CC -fsanitize=memory -o ${TMPDIR}/testsanitizers$$ ${TMPDIR}/testsanitizers$$.c 2>&1 | grep "unrecognized" >& /dev/null; then
28 echo "skipping msan tests: $CC -fsanitize=memory not supported"
29 msan=no
30 elif ! test -x ${TMPDIR}/testsanitizers$$; then
31 echo "skipping msan tests: $CC -fsanitize-memory did not generate an executable"
32 msan=no
33 elif ! ${TMPDIR}/testsanitizers$$ >/dev/null 2>&1; then
34 echo "skipping msan tests: $CC -fsanitize-memory generates broken executable"
35 msan=no
37 rm -f ${TMPDIR}/testsanitizers$$.*
39 tsan=yes
41 # The memory and thread sanitizers in versions of clang before 3.6
42 # don't work with Go.
43 if test "$msan" = "yes" && $CC --version | grep clang >& /dev/null; then
44 ver=$($CC --version | sed -e 's/.* version \([0-9.-]*\).*/\1/')
45 major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/')
46 minor=$(echo $ver | sed -e 's/[0-9]*\.\([0-9]*\).*/\1/')
47 if test "$major" -lt 3 || test "$major" -eq 3 -a "$minor" -lt 6; then
48 echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.6)"
49 msan=no
50 tsan=no
53 # Clang before 3.8 does not work with Linux at or after 4.1.
54 # golang.org/issue/12898.
55 if test "$msan" = "yes" -a "$major" -lt 3 || test "$major" -eq 3 -a "$minor" -lt 8; then
56 if test "$(uname)" = Linux; then
57 linuxver=$(uname -r)
58 linuxmajor=$(echo $linuxver | sed -e 's/\([0-9]*\).*/\1/')
59 linuxminor=$(echo $linuxver | sed -e 's/[0-9]*\.\([0-9]*\).*/\1/')
60 if test "$linuxmajor" -gt 4 || test "$linuxmajor" -eq 4 -a "$linuxminor" -ge 1; then
61 echo "skipping msan/tsan tests: clang version $major.$minor (older than 3.8) incompatible with linux version $linuxmajor.$linuxminor (4.1 or newer)"
62 msan=no
63 tsan=no
69 status=0
71 testmsanshared() {
72 goos=$(go env GOOS)
73 suffix="-installsuffix testsanitizers"
74 libext="so"
75 if [ "$goos" = "darwin" ]; then
76 libext="dylib"
78 go build -msan -buildmode=c-shared $suffix -o ${TMPDIR}/libmsanshared.$libext msan_shared.go
80 echo 'int main() { return 0; }' > ${TMPDIR}/testmsanshared.c
81 $CC $(go env GOGCCFLAGS) -fsanitize=memory -o ${TMPDIR}/testmsanshared ${TMPDIR}/testmsanshared.c ${TMPDIR}/libmsanshared.$libext
83 if ! LD_LIBRARY_PATH=. ${TMPDIR}/testmsanshared; then
84 echo "FAIL: msan_shared"
85 status=1
87 rm -f ${TMPDIR}/{testmsanshared,testmsanshared.c,libmsanshared.$libext}
90 if test "$msan" = "yes"; then
91 if ! go build -msan std; then
92 echo "FAIL: build -msan std"
93 status=1
96 if ! go run -msan msan.go; then
97 echo "FAIL: msan"
98 status=1
101 if ! CGO_LDFLAGS="-fsanitize=memory" CGO_CPPFLAGS="-fsanitize=memory" go run -msan -a msan2.go; then
102 echo "FAIL: msan2 with -fsanitize=memory"
103 status=1
106 if ! go run -msan -a msan2.go; then
107 echo "FAIL: msan2"
108 status=1
111 if ! go run -msan msan3.go; then
112 echo "FAIL: msan3"
113 status=1
116 if ! go run -msan msan4.go; then
117 echo "FAIL: msan4"
118 status=1
121 if ! go run -msan msan5.go; then
122 echo "FAIL: msan5"
123 status=1
126 if go run -msan msan_fail.go 2>/dev/null; then
127 echo "FAIL: msan_fail"
128 status=1
131 testmsanshared
134 testtsanshared() {
135 goos=$(go env GOOS)
136 suffix="-installsuffix tsan"
137 libext="so"
138 if [ "$goos" = "darwin" ]; then
139 libext="dylib"
141 go build -buildmode=c-shared $suffix -o ${TMPDIR}/libtsanshared.$libext tsan_shared.go
143 echo 'int main() { return 0; }' > ${TMPDIR}/testtsanshared.c
144 $CC $(go env GOGCCFLAGS) -fsanitize=thread -o ${TMPDIR}/testtsanshared ${TMPDIR}/testtsanshared.c ${TMPDIR}/libtsanshared.$libext
146 if ! LD_LIBRARY_PATH=. ${TMPDIR}/testtsanshared; then
147 echo "FAIL: tsan_shared"
148 status=1
150 rm -f ${TMPDIR}/{testtsanshared,testtsanshared.c,libtsanshared.$libext}
153 if test "$tsan" = "yes"; then
154 echo 'int main() { return 0; }' > ${TMPDIR}/testsanitizers$$.c
155 ok=yes
156 if ! $CC -fsanitize=thread ${TMPDIR}/testsanitizers$$.c -o ${TMPDIR}/testsanitizers$$ &> ${TMPDIR}/testsanitizers$$.err; then
157 ok=no
159 if grep "unrecognized" ${TMPDIR}/testsanitizers$$.err >& /dev/null; then
160 echo "skipping tsan tests: -fsanitize=thread not supported"
161 tsan=no
162 elif test "$ok" != "yes"; then
163 cat ${TMPDIR}/testsanitizers$$.err
164 echo "skipping tsan tests: -fsanitizer=thread build failed"
165 tsan=no
166 elif ! ${TMPDIR}/testsanitizers$$ 2>&1; then
167 echo "skipping tsan tests: running tsan program failed"
168 tsan=no
170 rm -f ${TMPDIR}/testsanitizers$$*
173 # Run a TSAN test.
174 # $1 test name
175 # $2 environment variables
176 # $3 go run args
177 testtsan() {
178 err=${TMPDIR}/tsanerr$$.out
179 if ! env $2 go run $3 $1 2>$err; then
180 cat $err
181 echo "FAIL: $1"
182 status=1
183 elif grep -i warning $err >/dev/null 2>&1; then
184 cat $err
185 echo "FAIL: $1"
186 status=1
188 rm -f $err
191 if test "$tsan" = "yes"; then
192 testtsan tsan.go
193 testtsan tsan2.go
194 testtsan tsan3.go
195 testtsan tsan4.go
196 testtsan tsan8.go
197 testtsan tsan9.go
199 # These tests are only reliable using clang or GCC version 7 or later.
200 # Otherwise runtime/cgo/libcgo.h can't tell whether TSAN is in use.
201 ok=false
202 clang=false
203 if ${CC} --version | grep clang >/dev/null 2>&1; then
204 ok=true
205 clang=true
206 else
207 ver=$($CC -dumpversion)
208 major=$(echo $ver | sed -e 's/\([0-9]*\).*/\1/')
209 if test "$major" -lt 7; then
210 echo "skipping remaining TSAN tests: GCC version $major (older than 7)"
211 else
212 ok=true
216 if test "$ok" = "true"; then
217 # These tests require rebuilding os/user with -fsanitize=thread.
218 testtsan tsan5.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
219 testtsan tsan6.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
220 testtsan tsan7.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
222 # The remaining tests reportedly hang when built with GCC; issue #21196.
223 if test "$clang" = "true"; then
224 testtsan tsan10.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
225 testtsan tsan11.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
226 testtsan tsan12.go "CGO_CFLAGS=-fsanitize=thread CGO_LDFLAGS=-fsanitize=thread" "-installsuffix=tsan"
229 testtsanshared
233 exit $status