| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build linux || (freebsd && amd64) |
| 6 |
|
| 7 |
package sanitizers_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/testenv" |
| 11 |
"os/exec" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestTSAN(t *testing.T) { |
| 17 |
testenv.MustHaveGoBuild(t) |
| 18 |
testenv.MustHaveCGO(t) |
| 19 |
|
| 20 |
goos, err := goEnv("GOOS") |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
goarch, err := goEnv("GOARCH") |
| 25 |
if err != nil { |
| 26 |
t.Fatal(err) |
| 27 |
} |
| 28 |
// The tsan tests require support for the -tsan option. |
| 29 |
if !compilerRequiredTsanVersion(goos, goarch) { |
| 30 |
t.Skipf("skipping on %s/%s; compiler version for -tsan option is too old.", goos, goarch) |
| 31 |
} |
| •32 |
if goarch == "riscv64" { |
| 33 |
t.Skipf("skipping TestTSAN test on %s (see https://github.com/golang/go/issues/75553)", goarch) |
| 34 |
} |
| •35 |
if goarch == "s390x" { |
| 36 |
t.Skipf("skipping TestTSAN test on %s (see https://github.com/golang/go/issues/77289)", goarch) |
| 37 |
} |
| 38 |
|
| 39 |
t.Parallel() |
| 40 |
requireOvercommit(t) |
| 41 |
config := configure("thread") |
| 42 |
config.skipIfCSanitizerBroken(t) |
| 43 |
|
| 44 |
mustRun(t, config.goCmd("build", "std")) |
| 45 |
|
| 46 |
cases := []struct { |
| 47 |
src string |
| 48 |
needsRuntime bool |
| 49 |
}{ |
| 50 |
{src: "tsan.go"}, |
| 51 |
{src: "tsan2.go"}, |
| 52 |
{src: "tsan3.go"}, |
| 53 |
{src: "tsan4.go"}, |
| 54 |
{src: "tsan5.go", needsRuntime: true}, |
| 55 |
{src: "tsan6.go", needsRuntime: true}, |
| 56 |
{src: "tsan7.go", needsRuntime: true}, |
| 57 |
{src: "tsan8.go"}, |
| 58 |
{src: "tsan9.go"}, |
| 59 |
{src: "tsan10.go", needsRuntime: true}, |
| 60 |
{src: "tsan11.go", needsRuntime: true}, |
| 61 |
{src: "tsan12.go", needsRuntime: true}, |
| 62 |
{src: "tsan13.go", needsRuntime: true}, |
| 63 |
{src: "tsan14.go", needsRuntime: true}, |
| 64 |
{src: "tsan15.go", needsRuntime: true}, |
| 65 |
} |
| 66 |
for _, tc := range cases { |
| 67 |
tc := tc |
| 68 |
name := strings.TrimSuffix(tc.src, ".go") |
| 69 |
t.Run(name, func(t *testing.T) { |
| 70 |
t.Parallel() |
| 71 |
|
| 72 |
dir := newTempDir(t) |
| 73 |
defer dir.RemoveAll(t) |
| 74 |
|
| 75 |
outPath := dir.Join(name) |
| 76 |
mustRun(t, config.goCmd("build", "-o", outPath, srcPath(tc.src))) |
| 77 |
|
| 78 |
cmdArgs := []string{outPath} |
| 79 |
if goos == "linux" { |
| 80 |
// Disable ASLR for TSAN. See https://go.dev/issue/59418. |
| 81 |
out, err := exec.Command("uname", "-m").Output() |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("failed to run `uname -m`: %v", err) |
| 84 |
} |
| 85 |
arch := strings.TrimSpace(string(out)) |
| 86 |
if _, err := exec.Command("setarch", arch, "-R", "true").Output(); err != nil { |
| 87 |
// Some systems don't have permission to run `setarch`. |
| 88 |
// See https://go.dev/issue/70463. |
| 89 |
t.Logf("failed to run `setarch %s -R true`: %v", arch, err) |
| 90 |
} else { |
| 91 |
cmdArgs = []string{"setarch", arch, "-R", outPath} |
| 92 |
} |
| 93 |
} |
| 94 |
cmd := hangProneCmd(cmdArgs[0], cmdArgs[1:]...) |
| 95 |
if tc.needsRuntime { |
| 96 |
config.skipIfRuntimeIncompatible(t) |
| 97 |
} |
| 98 |
// If we don't see halt_on_error, the program |
| 99 |
// will only exit non-zero if we call C.exit. |
| 100 |
cmd.Env = append(cmd.Environ(), "TSAN_OPTIONS=halt_on_error=1") |
| 101 |
mustRun(t, cmd) |
| 102 |
}) |
| 103 |
} |
| 104 |
} |