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.
5 package sanitizers_test
12 func TestASAN(t *testing.T) {
13 goos, err := goEnv("GOOS")
17 goarch, err := goEnv("GOARCH")
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)
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)
36 config := configure("address")
37 config.skipIfCSanitizerBroken(t)
39 mustRun(t, config.goCmd("build", "std"))
43 memoryAccessError string
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"}},
63 for _, tc := range cases {
65 name := strings.TrimSuffix(tc.src, ".go")
66 t.Run(name, func(t *testing.T) {
70 defer dir.RemoveAll(t)
72 outPath := dir.Join(name)
73 mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
75 cmd := hangProneCmd(outPath)
76 if tc.memoryAccessError != "" {
77 outb, err := cmd.CombinedOutput()
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() {
90 t.Errorf("%#q exited without expected location of the error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.errorLocation, out)
94 t.Fatalf("%#q exited without expected memory access error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.memoryAccessError, out)