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

View as plain text