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

View as plain text