]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/testsanitizers/asan_test.go
runtime: remove crash_cgo_test CgoRaceSignal timeout
[gostls13.git] / misc / cgo / testsanitizers / asan_test.go
1 // Copyright 2021 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 package sanitizers_test
6
7 import (
8         "fmt"
9         "strings"
10         "testing"
11 )
12
13 func TestASAN(t *testing.T) {
14         goos, err := goEnv("GOOS")
15         if err != nil {
16                 t.Fatal(err)
17         }
18         goarch, err := goEnv("GOARCH")
19         if err != nil {
20                 t.Fatal(err)
21         }
22         // The asan tests require support for the -asan option.
23         if !aSanSupported(goos, goarch) {
24                 t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
25         }
26         // The current implementation is only compatible with the ASan library from version
27         // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
28         // -asan option must use a compatible version of ASan library, which requires that
29         // the gcc version is not less than 7 and the clang version is not less than 9,
30         // otherwise a segmentation fault will occur.
31         if !compilerRequiredAsanVersion(goos, goarch) {
32                 t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
33         }
34
35         t.Parallel()
36         requireOvercommit(t)
37         config := configure("address")
38         config.skipIfCSanitizerBroken(t)
39
40         mustRun(t, config.goCmd("build", "std"))
41
42         cases := []struct {
43                 src               string
44                 memoryAccessError string
45                 errorLocation     string
46                 experiments       []string
47         }{
48                 {src: "asan1_fail.go", memoryAccessError: "heap-use-after-free", errorLocation: "asan1_fail.go:25"},
49                 {src: "asan2_fail.go", memoryAccessError: "heap-buffer-overflow", errorLocation: "asan2_fail.go:31"},
50                 {src: "asan3_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan3_fail.go:13"},
51                 {src: "asan4_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan4_fail.go:13"},
52                 {src: "asan5_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan5_fail.go:18"},
53                 {src: "asan_useAfterReturn.go"},
54                 {src: "asan_unsafe_fail1.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail1.go:25"},
55                 {src: "asan_unsafe_fail2.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail2.go:25"},
56                 {src: "asan_unsafe_fail3.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail3.go:18"},
57                 {src: "asan_global1_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global1_fail.go:12"},
58                 {src: "asan_global2_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global2_fail.go:19"},
59                 {src: "asan_global3_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global3_fail.go:13"},
60                 {src: "asan_global4_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global4_fail.go:21"},
61                 {src: "asan_global5.go"},
62                 {src: "arena_fail.go", memoryAccessError: "use-after-poison", errorLocation: "arena_fail.go:26", experiments: []string{"arenas"}},
63         }
64         for _, tc := range cases {
65                 tc := tc
66                 name := strings.TrimSuffix(tc.src, ".go")
67                 t.Run(name, func(t *testing.T) {
68                         t.Parallel()
69
70                         dir := newTempDir(t)
71                         defer dir.RemoveAll(t)
72
73                         outPath := dir.Join(name)
74                         mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
75
76                         cmd := hangProneCmd(outPath)
77                         if tc.memoryAccessError != "" {
78                                 outb, err := cmd.CombinedOutput()
79                                 out := string(outb)
80                                 if err != nil && strings.Contains(out, tc.memoryAccessError) {
81                                         // This string is output if the
82                                         // sanitizer library needs a
83                                         // symbolizer program and can't find it.
84                                         const noSymbolizer = "external symbolizer"
85                                         // Check if -asan option can correctly print where the error occurred.
86                                         if tc.errorLocation != "" &&
87                                                 !strings.Contains(out, tc.errorLocation) &&
88                                                 !strings.Contains(out, noSymbolizer) &&
89                                                 compilerSupportsLocation() {
90
91                                                 t.Errorf("%#q exited without expected location of the error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.errorLocation, out)
92                                         }
93                                         return
94                                 }
95                                 t.Fatalf("%#q exited without expected memory access error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.memoryAccessError, out)
96                         }
97                         mustRun(t, cmd)
98                 })
99         }
100 }
101
102 func TestASANLinkerX(t *testing.T) {
103         // Test ASAN with linker's -X flag (see issue 56175).
104         goos, err := goEnv("GOOS")
105         if err != nil {
106                 t.Fatal(err)
107         }
108         goarch, err := goEnv("GOARCH")
109         if err != nil {
110                 t.Fatal(err)
111         }
112         // The asan tests require support for the -asan option.
113         if !aSanSupported(goos, goarch) {
114                 t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
115         }
116         if !compilerRequiredAsanVersion(goos, goarch) {
117                 t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
118         }
119
120         t.Parallel()
121         requireOvercommit(t)
122         config := configure("address")
123         config.skipIfCSanitizerBroken(t)
124
125         dir := newTempDir(t)
126         defer dir.RemoveAll(t)
127
128         var ldflags string
129         for i := 1; i <= 10; i++ {
130                 ldflags += fmt.Sprintf("-X=main.S%d=%d -X=misc/cgo/testsanitizers/testdata/asan_linkerx/p.S%d=%d ", i, i, i, i)
131         }
132
133         // build the binary
134         outPath := dir.Join("main.exe")
135         cmd := config.goCmd("build", "-ldflags="+ldflags, "-o", outPath)
136         cmd.Dir = srcPath("asan_linkerx")
137         mustRun(t, cmd)
138
139         // run the binary
140         mustRun(t, hangProneCmd(outPath))
141 }