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
13 func TestASAN(t *testing.T) {
14 goos, err := goEnv("GOOS")
18 goarch, err := goEnv("GOARCH")
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)
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)
37 config := configure("address")
38 config.skipIfCSanitizerBroken(t)
40 mustRun(t, config.goCmd("build", "std"))
44 memoryAccessError string
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"}},
64 for _, tc := range cases {
66 name := strings.TrimSuffix(tc.src, ".go")
67 t.Run(name, func(t *testing.T) {
71 defer dir.RemoveAll(t)
73 outPath := dir.Join(name)
74 mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
76 cmd := hangProneCmd(outPath)
77 if tc.memoryAccessError != "" {
78 outb, err := cmd.CombinedOutput()
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() {
91 t.Errorf("%#q exited without expected location of the error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.errorLocation, out)
95 t.Fatalf("%#q exited without expected memory access error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.memoryAccessError, out)
102 func TestASANLinkerX(t *testing.T) {
103 // Test ASAN with linker's -X flag (see issue 56175).
104 goos, err := goEnv("GOOS")
108 goarch, err := goEnv("GOARCH")
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)
116 if !compilerRequiredAsanVersion(goos, goarch) {
117 t.Skipf("skipping on %s/%s: too old version of compiler", goos, goarch)
122 config := configure("address")
123 config.skipIfCSanitizerBroken(t)
126 defer dir.RemoveAll(t)
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)
134 outPath := dir.Join("main.exe")
135 cmd := config.goCmd("build", "-ldflags="+ldflags, "-o", outPath)
136 cmd.Dir = srcPath("asan_linkerx")
140 mustRun(t, hangProneCmd(outPath))