...

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

Documentation: cmd/cgo/internal/testsanitizers

     1  // Copyright 2022 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/testenv"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestLibFuzzer(t *testing.T) {
    16  	testenv.MustHaveGoBuild(t)
    17  	testenv.MustHaveCGO(t)
    18  	goos, err := goEnv("GOOS")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	goarch, err := goEnv("GOARCH")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	if !libFuzzerSupported(goos, goarch) {
    27  		t.Skipf("skipping on %s/%s; libfuzzer option is not supported.", goos, goarch)
    28  	}
    29  	config := configure("fuzzer")
    30  	config.skipIfCSanitizerBroken(t)
    31  
    32  	cases := []struct {
    33  		goSrc         string
    34  		cSrc          string
    35  		expectedError string
    36  	}{
    37  		{goSrc: "libfuzzer1.go", expectedError: "panic: found it"},
    38  		{goSrc: "libfuzzer2.go", cSrc: "libfuzzer2.c", expectedError: "panic: found it"},
    39  	}
    40  	for _, tc := range cases {
    41  		tc := tc
    42  		name := strings.TrimSuffix(tc.goSrc, ".go")
    43  		t.Run(name, func(t *testing.T) {
    44  			t.Parallel()
    45  
    46  			dir := newTempDir(t)
    47  			defer dir.RemoveAll(t)
    48  
    49  			// build Go code in libfuzzer mode to a c-archive
    50  			outPath := dir.Join(name)
    51  			archivePath := dir.Join(name + ".a")
    52  			mustRun(t, config.goCmd("build", "-buildmode=c-archive", "-o", archivePath, srcPath(tc.goSrc)))
    53  
    54  			// build C code (if any) and link with Go code
    55  			cmd, err := cc(config.cFlags...)
    56  			if err != nil {
    57  				t.Fatalf("error running cc: %v", err)
    58  			}
    59  			cmd.Args = append(cmd.Args, config.ldFlags...)
    60  			cmd.Args = append(cmd.Args, "-o", outPath, "-I", dir.Base())
    61  			if tc.cSrc != "" {
    62  				cmd.Args = append(cmd.Args, srcPath(tc.cSrc))
    63  			}
    64  			cmd.Args = append(cmd.Args, archivePath)
    65  			mustRun(t, cmd)
    66  
    67  			cmd = hangProneCmd(outPath)
    68  			cmd.Dir = dir.Base()
    69  			outb, err := cmd.CombinedOutput()
    70  			out := string(outb)
    71  			if err == nil {
    72  				t.Fatalf("fuzzing succeeded unexpectedly; output:\n%s", out)
    73  			}
    74  			if !strings.Contains(out, tc.expectedError) {
    75  				t.Errorf("exited without expected error %q; got\n%s", tc.expectedError, out)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  // libFuzzerSupported is a copy of the function internal/platform.FuzzInstrumented,
    82  // because the internal package can't be used here.
    83  func libFuzzerSupported(goos, goarch string) bool {
    84  	switch goarch {
    85  	case "amd64", "arm64":
    86  		// TODO(#14565): support more architectures.
    87  		switch goos {
    88  		case "darwin", "freebsd", "linux", "windows":
    89  			return true
    90  		default:
    91  			return false
    92  		}
    93  	default:
    94  		return false
    95  	}
    96  }
    97  

View as plain text