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

View as plain text