...

Source file src/cmd/cgo/internal/testsanitizers/msan_test.go

Documentation: cmd/cgo/internal/testsanitizers

     1  // Copyright 2017 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  	"internal/platform"
    11  	"internal/testenv"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestMSAN(t *testing.T) {
    17  	testenv.MustHaveGoBuild(t)
    18  	testenv.MustHaveCGO(t)
    19  	goos, err := goEnv("GOOS")
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	goarch, err := goEnv("GOARCH")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	// The msan tests require support for the -msan option.
    28  	if !platform.MSanSupported(goos, goarch) {
    29  		t.Skipf("skipping on %s/%s; -msan option is not supported.", goos, goarch)
    30  	}
    31  
    32  	t.Parallel()
    33  	// Overcommit is enabled by default on FreeBSD (vm.overcommit=0, see tuning(7)).
    34  	// Do not skip tests with stricter overcommit settings unless testing shows that FreeBSD has similar issues.
    35  	if goos == "linux" {
    36  		requireOvercommit(t)
    37  	}
    38  	config := configure("memory")
    39  	config.skipIfCSanitizerBroken(t)
    40  
    41  	mustRun(t, config.goCmd("build", "std"))
    42  
    43  	cases := []struct {
    44  		src         string
    45  		wantErr     bool
    46  		experiments []string
    47  	}{
    48  		{src: "msan.go"},
    49  		{src: "msan2.go"},
    50  		{src: "msan2_cmsan.go"},
    51  		{src: "msan3.go"},
    52  		{src: "msan4.go"},
    53  		{src: "msan5.go"},
    54  		{src: "msan6.go"},
    55  		{src: "msan7.go"},
    56  		{src: "msan8.go"},
    57  		{src: "msan_fail.go", wantErr: true},
    58  		// This may not always fail specifically due to MSAN. It may sometimes
    59  		// fail because of a fault. However, we don't care what kind of error we
    60  		// get here, just that we get an error. This is an MSAN test because without
    61  		// MSAN it would not fail deterministically.
    62  		{src: "arena_fail.go", wantErr: true, experiments: []string{"arenas"}},
    63  	}
    64  	for _, tc := range cases {
    65  		tc := tc
    66  		name := strings.TrimSuffix(tc.src, ".go")
    67  		t.Run(name, func(t *testing.T) {
    68  			t.Parallel()
    69  
    70  			dir := newTempDir(t)
    71  			defer dir.RemoveAll(t)
    72  
    73  			outPath := dir.Join(name)
    74  			mustRun(t, config.goCmdWithExperiments("build", []string{"-o", outPath, srcPath(tc.src)}, tc.experiments))
    75  
    76  			cmd := hangProneCmd(outPath)
    77  			if tc.wantErr {
    78  				out, err := cmd.CombinedOutput()
    79  				if err != nil {
    80  					return
    81  				}
    82  				t.Fatalf("%#q exited without error; want MSAN failure\n%s", strings.Join(cmd.Args, " "), out)
    83  			}
    84  			mustRun(t, cmd)
    85  		})
    86  	}
    87  }
    88  

View as plain text