• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7package sanitizers_test
8
9import (
10	"internal/testenv"
11	"strings"
12	"testing"
13)
14
15func TestTSAN(t *testing.T) {
16	testenv.MustHaveGoBuild(t)
17	testenv.MustHaveCGO(t)
18
19	goos, err := goEnv("GOOS")
20	if err != nil {
21		t.Fatal(err)
22	}
23	goarch, err := goEnv("GOARCH")
24	if err != nil {
25		t.Fatal(err)
26	}
27	// The msan tests require support for the -msan option.
28	if !compilerRequiredTsanVersion(goos, goarch) {
29		t.Skipf("skipping on %s/%s; compiler version for -tsan option is too old.", goos, goarch)
30	}
31
32	t.Parallel()
33	requireOvercommit(t)
34	config := configure("thread")
35	config.skipIfCSanitizerBroken(t)
36
37	mustRun(t, config.goCmd("build", "std"))
38
39	cases := []struct {
40		src          string
41		needsRuntime bool
42	}{
43		{src: "tsan.go"},
44		{src: "tsan2.go"},
45		{src: "tsan3.go"},
46		{src: "tsan4.go"},
47		{src: "tsan5.go", needsRuntime: true},
48		{src: "tsan6.go", needsRuntime: true},
49		{src: "tsan7.go", needsRuntime: true},
50		{src: "tsan8.go"},
51		{src: "tsan9.go"},
52		{src: "tsan10.go", needsRuntime: true},
53		{src: "tsan11.go", needsRuntime: true},
54		{src: "tsan12.go", needsRuntime: true},
55		{src: "tsan13.go", needsRuntime: true},
56		{src: "tsan14.go", needsRuntime: true},
57		{src: "tsan15.go", needsRuntime: true},
58	}
59	for _, tc := range cases {
60		tc := tc
61		name := strings.TrimSuffix(tc.src, ".go")
62		t.Run(name, func(t *testing.T) {
63			t.Parallel()
64
65			dir := newTempDir(t)
66			defer dir.RemoveAll(t)
67
68			outPath := dir.Join(name)
69			mustRun(t, config.goCmd("build", "-o", outPath, srcPath(tc.src)))
70
71			cmd := hangProneCmd(outPath)
72			if tc.needsRuntime {
73				config.skipIfRuntimeIncompatible(t)
74			}
75			// If we don't see halt_on_error, the program
76			// will only exit non-zero if we call C.exit.
77			cmd.Env = append(cmd.Environ(), "TSAN_OPTIONS=halt_on_error=1")
78			mustRun(t, cmd)
79		})
80	}
81}
82