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

View as plain text