github.com/golang/gofrontend@v0.0.0-20240429183944-60f985a78526/libgo/misc/cgo/testsanitizers/asan_test.go (about) 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 26 t.Parallel() 27 requireOvercommit(t) 28 config := configure("address") 29 config.skipIfCSanitizerBroken(t) 30 31 mustRun(t, config.goCmd("build", "std")) 32 33 cases := []struct { 34 src string 35 memoryAccessError string 36 errorLocation string 37 }{ 38 {src: "asan1_fail.go", memoryAccessError: "heap-use-after-free", errorLocation: "asan1_fail.go:25"}, 39 {src: "asan2_fail.go", memoryAccessError: "heap-buffer-overflow", errorLocation: "asan2_fail.go:31"}, 40 {src: "asan3_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan3_fail.go:13"}, 41 {src: "asan4_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan4_fail.go:13"}, 42 {src: "asan5_fail.go", memoryAccessError: "use-after-poison", errorLocation: "asan5_fail.go:18"}, 43 {src: "asan_useAfterReturn.go"}, 44 } 45 for _, tc := range cases { 46 tc := tc 47 name := strings.TrimSuffix(tc.src, ".go") 48 t.Run(name, func(t *testing.T) { 49 t.Parallel() 50 51 dir := newTempDir(t) 52 defer dir.RemoveAll(t) 53 54 outPath := dir.Join(name) 55 mustRun(t, config.goCmd("build", "-o", outPath, srcPath(tc.src))) 56 57 cmd := hangProneCmd(outPath) 58 if tc.memoryAccessError != "" { 59 outb, err := cmd.CombinedOutput() 60 out := string(outb) 61 if err != nil && strings.Contains(out, tc.memoryAccessError) { 62 // This string is output if the 63 // sanitizer library needs a 64 // symbolizer program and can't find it. 65 const noSymbolizer = "external symbolizer" 66 // Check if -asan option can correctly print where the error occurred. 67 if tc.errorLocation != "" && 68 !strings.Contains(out, tc.errorLocation) && 69 !strings.Contains(out, noSymbolizer) && 70 compilerSupportsLocation() { 71 72 t.Errorf("%#q exited without expected location of the error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.errorLocation, out) 73 } 74 return 75 } 76 t.Fatalf("%#q exited without expected memory access error\n%s; got failure\n%s", strings.Join(cmd.Args, " "), tc.memoryAccessError, out) 77 } 78 mustRun(t, cmd) 79 }) 80 } 81 }