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.
5 package sanitizers_test
12 func TestLibFuzzer(t *testing.T) {
13 goos, err := goEnv("GOOS")
17 goarch, err := goEnv("GOARCH")
21 if !libFuzzerSupported(goos, goarch) {
22 t.Skipf("skipping on %s/%s; libfuzzer option is not supported.", goos, goarch)
24 config := configure("fuzzer")
25 config.skipIfCSanitizerBroken(t)
32 {goSrc: "libfuzzer1.go", expectedError: "panic: found it"},
33 {goSrc: "libfuzzer2.go", cSrc: "libfuzzer2.c", expectedError: "panic: found it"},
35 for _, tc := range cases {
37 name := strings.TrimSuffix(tc.goSrc, ".go")
38 t.Run(name, func(t *testing.T) {
42 defer dir.RemoveAll(t)
44 // build Go code in libfuzzer mode to a c-archive
45 outPath := dir.Join(name)
46 archivePath := dir.Join(name + ".a")
47 mustRun(t, config.goCmd("build", "-buildmode=c-archive", "-o", archivePath, srcPath(tc.goSrc)))
49 // build C code (if any) and link with Go code
50 cmd, err := cc(config.cFlags...)
52 t.Fatalf("error running cc: %v", err)
54 cmd.Args = append(cmd.Args, config.ldFlags...)
55 cmd.Args = append(cmd.Args, "-o", outPath, "-I", dir.Base())
57 cmd.Args = append(cmd.Args, srcPath(tc.cSrc))
59 cmd.Args = append(cmd.Args, archivePath)
62 cmd = hangProneCmd(outPath)
63 outb, err := cmd.CombinedOutput()
66 t.Fatalf("fuzzing succeeded unexpectedly; output:\n%s", out)
68 if !strings.Contains(out, tc.expectedError) {
69 t.Errorf("exited without expected error %q; got\n%s", tc.expectedError, out)
75 // libFuzzerSupported is a copy of the function internal/platform.FuzzInstrumented,
76 // because the internal package can't be used here.
77 func libFuzzerSupported(goos, goarch string) bool {
79 case "amd64", "arm64":
80 // TODO(#14565): support more architectures.
82 case "darwin", "freebsd", "linux", "windows":