]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/testsanitizers/asan_test.go
misc/cgo/test: add asan and msan arena tests
[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         "strings"
9         "testing"
10 )
11
12 func TestASAN(t *testing.T) {
13         goos, err := goEnv("GOOS")
14         if err != nil {
15                 t.Fatal(err)
16         }
17         goarch, err := goEnv("GOARCH")
18         if err != nil {
19                 t.Fatal(err)
20         }
21         // The asan tests require support for the -asan option.
22         if !aSanSupported(goos, goarch) {
23                 t.Skipf("skipping on %s/%s; -asan option is not supported.", goos, goarch)
24         }
25         // The current implementation is only compatible with the ASan library from version
26         // v7 to v9 (See the description in src/runtime/asan/asan.go). Therefore, using the
27         // -asan option must use a compatible version of ASan library, which requires that
28         // the gcc version is not less than 7 and the clang version is not less than 9,
29         // otherwise a segmentation fault will occur.
30         if !compilerRequiredAsanVersion(goos, goarch) {
31                 t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
32         }
33
34         t.Parallel()
35         requireOvercommit(t)
36         config := configure("address")
37         config.skipIfCSanitizerBroken(t)
38
39         mustRun(t, config.goCmd("build", "std"))
40
41         cases := []struct {
42                 src               string
43                 memoryAccessError string
44                 errorLocation     string
45                 experiments       []string
46         }{
47                 {src: "asan1_fail.go", memoryAccessError: "heap-use-after-free", errorLocation: "asan1_fail.go:25"},
48                 {src: "asan2_fail.go", memoryAccessError: "heap-buffer-overflow", errorLocation: "asan2_fail.go:31"},
49                 {src: "asan3_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan3_fail.go:13"},
50                 {src: "asan4_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan4_fail.go:13"},
51                 {src: "asan5_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan5_fail.go:18"},
52                 {src: "asan_useAfterReturn.go"},
53                 {src: "asan_unsafe_fail1.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail1.go:25"},
54                 {src: "asan_unsafe_fail2.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail2.go:25"},
55                 {src: "asan_unsafe_fail3.go", memoryAccessError: "use-after-poison", errorLocation: "asan_unsafe_fail3.go:18"},
56                 {src: "asan_global1_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global1_fail.go:12"},
57                 {src: "asan_global2_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global2_fail.go:19"},
58                 {src: "asan_global3_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global3_fail.go:13"},
59                 {src: "asan_global4_fail.go", memoryAccessError: "global-buffer-overflow", errorLocation: "asan_global4_fail.go:21"},
60                 {src: "asan_global5.go"},
61                 {src: "arena_fail.go", memoryAccessError: "use-after-poison", errorLocation: "arena_fail.go:26", experiments: []string{"arenas"}},
62         }
63         for _, tc := range cases {
64                 tc := tc
65                 name := strings.TrimSuffix(tc.src, ".go")
66                 t.Run(name, func(t *testing.T) {
67                         t.Parallel()
68
69                         dir := newTempDir(t)
70                         defer dir.RemoveAll(t)
71
72                         outPath := dir.Join(name)
73                         mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
74
75                         cmd := hangProneCmd(outPath)
76                         if tc.memoryAccessError != "" {
77                                 outb, err := cmd.CombinedOutput()
78                                 out := string(outb)
79                                 if err != nil && strings.Contains(out, tc.memoryAccessError) {
80                                         // This string is output if the
81                                         // sanitizer library needs a
82                                         // symbolizer program and can't find it.
83                                         const noSymbolizer = "external symbolizer"
84                                         // Check if -asan option can correctly print where the error occurred.
85                                         if tc.errorLocation != "" &&
86                                                 !strings.Contains(out, tc.errorLocation) &&
87                                                 !strings.Contains(out, noSymbolizer) &&
88                                                 compilerSupportsLocation() {
89
90                                                 t.Errorf("%#q exited without expected location of the error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.errorLocation, out)
91                                         }
92                                         return
93                                 }
94                                 t.Fatalf("%#q exited without expected memory access error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.memoryAccessError, out)
95                         }
96                         mustRun(t, cmd)
97                 })
98         }
99 }