7
7
package sanitizers_test
8
8
9
9
import (
10
+ "bytes"
10
11
"fmt"
11
12
"internal/platform"
12
13
"internal/testenv"
@@ -15,34 +16,9 @@ import (
15
16
)
16
17
17
18
func TestASAN (t * testing.T ) {
18
- testenv .MustHaveGoBuild (t )
19
- testenv .MustHaveCGO (t )
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 asan tests require support for the -asan option.
29
- if ! platform .ASanSupported (goos , goarch ) {
30
- t .Skipf ("skipping on %s/%s; -asan option is not supported." , goos , goarch )
31
- }
32
- // The current implementation is only compatible with the ASan library from version
33
- // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
34
- // -asan option must use a compatible version of ASan library, which requires that
35
- // the gcc version is not less than 7 and the clang version is not less than 9,
36
- // otherwise a segmentation fault will occur.
37
- if ! compilerRequiredAsanVersion (goos , goarch ) {
38
- t .Skipf ("skipping on %s/%s: too old version of compiler" , goos , goarch )
39
- }
19
+ config := mustHaveASAN (t )
40
20
41
21
t .Parallel ()
42
- requireOvercommit (t )
43
- config := configure ("address" )
44
- config .skipIfCSanitizerBroken (t )
45
-
46
22
mustRun (t , config .goCmd ("build" , "std" ))
47
23
48
24
cases := []struct {
@@ -106,9 +82,53 @@ func TestASAN(t *testing.T) {
106
82
}
107
83
108
84
func TestASANLinkerX (t * testing.T ) {
85
+ // Test ASAN with linker's -X flag (see issue 56175).
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
+ // build the binary
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
+ // run the binary
105
+ mustRun (t , hangProneCmd (outPath ))
106
+ }
107
+
108
+ // Issue 66966.
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 {
109
130
testenv .MustHaveGoBuild (t )
110
131
testenv .MustHaveCGO (t )
111
- // Test ASAN with linker's -X flag (see issue 56175).
112
132
goos , err := goEnv ("GOOS" )
113
133
if err != nil {
114
134
t .Fatal (err )
@@ -117,33 +137,23 @@ func TestASANLinkerX(t *testing.T) {
117
137
if err != nil {
118
138
t .Fatal (err )
119
139
}
120
- // The asan tests require support for the -asan option.
121
140
if ! platform .ASanSupported (goos , goarch ) {
122
141
t .Skipf ("skipping on %s/%s; -asan option is not supported." , goos , goarch )
123
142
}
143
+
144
+ // The current implementation is only compatible with the ASan library from version
145
+ // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
146
+ // -asan option must use a compatible version of ASan library, which requires that
147
+ // the gcc version is not less than 7 and the clang version is not less than 9,
148
+ // otherwise a segmentation fault will occur.
124
149
if ! compilerRequiredAsanVersion (goos , goarch ) {
125
150
t .Skipf ("skipping on %s/%s: too old version of compiler" , goos , goarch )
126
151
}
127
152
128
- t .Parallel ()
129
153
requireOvercommit (t )
154
+
130
155
config := configure ("address" )
131
156
config .skipIfCSanitizerBroken (t )
132
157
133
- dir := newTempDir (t )
134
- defer dir .RemoveAll (t )
135
-
136
- var ldflags string
137
- for i := 1 ; i <= 10 ; i ++ {
138
- ldflags += fmt .Sprintf ("-X=main.S%d=%d -X=cmd/cgo/internal/testsanitizers/testdata/asan_linkerx/p.S%d=%d " , i , i , i , i )
139
- }
140
-
141
- // build the binary
142
- outPath := dir .Join ("main.exe" )
143
- cmd := config .goCmd ("build" , "-ldflags=" + ldflags , "-o" , outPath )
144
- cmd .Dir = srcPath ("asan_linkerx" )
145
- mustRun (t , cmd )
146
-
147
- // run the binary
148
- mustRun (t , hangProneCmd (outPath ))
158
+ return config
149
159
}
0 commit comments