1
2
3
4
5
6
7 package sanitizers_test
8
9 import (
10 "bytes"
11 "fmt"
12 "internal/platform"
13 "internal/testenv"
14 "strings"
15 "testing"
16 )
17
18 func TestASAN(t *testing.T) {
19 config := mustHaveASAN(t)
20
21 t.Parallel()
22 mustRun(t, config.goCmd("build", "std"))
23
24 cases := []struct {
25 src string
26 memoryAccessError string
27 errorLocation string
28 experiments []string
29 }{
30 {src: "asan1_fail.go", memoryAccessError: "heap-use-after-free", errorLocation: "asan1_fail.go:25"},
31 {src: "asan2_fail.go", memoryAccessError: "heap-buffer-overflow", errorLocation: "asan2_fail.go:31"},
32 {src: "asan3_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan3_fail.go:13"},
33 {src: "asan4_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan4_fail.go:13"},
34 {src: "asan5_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan5_fail.go:18"},
35 {src: "asan_useAfterReturn.go"},
36 {src: "asan_unsafe_fail1.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail1.go:25"},
37 {src: "asan_unsafe_fail2.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail2.go:25"},
38 {src: "asan_unsafe_fail3.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail3.go:18"},
39 {src: "asan_global1_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global1_fail.go:12"},
40 {src: "asan_global2_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global2_fail.go:19"},
41 {src: "asan_global3_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global3_fail.go:13"},
42 {src: "asan_global4_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global4_fail.go:21"},
43 {src: "asan_global5.go"},
44 {src: "arena_fail.go", memoryAccessError: "use-after-poison", errorLocation: "arena_fail.go:26", experiments: []string{"arenas"}},
45 }
46 for _, tc := range cases {
47 tc := tc
48 name := strings.TrimSuffix(tc.src, ".go")
49 t.Run(name, func(t *testing.T) {
50 t.Parallel()
51
52 dir := newTempDir(t)
53 defer dir.RemoveAll(t)
54
55 outPath := dir.Join(name)
56 mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
57
58 cmd := hangProneCmd(outPath)
59 if tc.memoryAccessError != "" {
60 outb, err := cmd.CombinedOutput()
61 out := string(outb)
62 if err != nil && strings.Contains(out, tc.memoryAccessError) {
63
64
65
66 const noSymbolizer = "external symbolizer"
67
68 if tc.errorLocation != "" &&
69 !strings.Contains(out, tc.errorLocation) &&
70 !strings.Contains(out, noSymbolizer) &&
71 compilerSupportsLocation() {
72
73 t.Errorf("%#q exited without expected location of the error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.errorLocation, out)
74 }
75 return
76 }
77 t.Fatalf("%#q exited without expected memory access error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.memoryAccessError, out)
78 }
79 mustRun(t, cmd)
80 })
81 }
82 }
83
84 func TestASANLinkerX(t *testing.T) {
85
86 config := mustHaveASAN(t)
87
88 t.Parallel()
89
90 dir := newTempDir(t)
91 defer dir.RemoveAll(t)
92
93 var ldflags string
94 for i := 1; i <= 10; i++ {
95 ldflags += fmt.Sprintf("-X=main.S%d=%d -X=cmd/cgo/internal/testsanitizers/testdata/asan_linkerx/p.S%d=%d ", i, i, i, i)
96 }
97
98
99 outPath := dir.Join("main.exe")
100 cmd := config.goCmd("build", "-ldflags="+ldflags, "-o", outPath)
101 cmd.Dir = srcPath("asan_linkerx")
102 mustRun(t, cmd)
103
104
105 mustRun(t, hangProneCmd(outPath))
106 }
107
108
109 func TestASANFuzz(t *testing.T) {
110 config := mustHaveASAN(t)
111
112 t.Parallel()
113
114 dir := newTempDir(t)
115 defer dir.RemoveAll(t)
116
117 cmd := config.goCmd("test", "-fuzz=Fuzz", srcPath("asan_fuzz_test.go"))
118 t.Logf("%v", cmd)
119 out, err := cmd.CombinedOutput()
120 t.Logf("%s", out)
121 if err == nil {
122 t.Error("expected fuzzing failure")
123 }
124 if bytes.Contains(out, []byte("AddressSanitizer")) {
125 t.Error(`output contains "AddressSanitizer", but should not`)
126 }
127 }
128
129 func mustHaveASAN(t *testing.T) *config {
130 testenv.MustHaveGoBuild(t)
131 testenv.MustHaveCGO(t)
132 goos, err := goEnv("GOOS")
133 if err != nil {
134 t.Fatal(err)
135 }
136 goarch, err := goEnv("GOARCH")
137 if err != nil {
138 t.Fatal(err)
139 }
140 if !platform.ASanSupported(goos, goarch) {
141 t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
142 }
143
144
145
146
147
148
149 if !compilerRequiredAsanVersion(goos, goarch) {
150 t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
151 }
152
153 requireOvercommit(t)
154
155 config := configure("address")
156 config.skipIfCSanitizerBroken(t)
157
158 return config
159 }
160
View as plain text