| 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 | package mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 10 | "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/" |
| 11 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 12 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 15 | |
| 16 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 17 | ) |
| 18 | |
| 19 | func canRacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/canRace().() bool { |
| 20 | _, err := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("go", "run", "-race", "./testdata/himom.go").CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().() |
| 21 | return err == nil |
| 22 | } |
| 23 | |
| 24 | // buildRunner builds the fuzz-runner executable, returning its path. |
| 25 | func buildRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/buildRunner().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) string { |
| 26 | bindir := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().(), "bin") |
| 27 | err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Mkdirscip-go gomod github.com/golang/go/src go1.25.0 os/Mkdir().(bindir, osscip-go gomod github.com/golang/go/src go1.25.0 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.0 os/ModePerm.) |
| 28 | if err != nil { |
| 29 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 30 | } |
| 31 | binary := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(bindir, "runner") |
| 32 | if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. == "windows" { |
| 33 | binary += ".exe" |
| 34 | } |
| 35 | cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("go", "build", "-o", binary) |
| 36 | if err := cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().(); err != nil { |
| 37 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Building fuzz-runner: %v", err) |
| 38 | } |
| 39 | return binary |
| 40 | } |
| 41 | |
| 42 | // TestRunner builds the binary, then kicks off a collection of sub-tests that invoke it. |
| 43 | func TestRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/TestRunner().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 44 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go") |
| 45 | if runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. == "android" { |
| 46 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("the dependencies are not available on android") |
| 47 | } |
| 48 | binaryPath := buildRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/buildRunner().(t) |
| 49 | |
| 50 | // Sub-tests using the binary built above. |
| 51 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().("Basic", func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { testBasicscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testBasic().(t, binaryPath) }) |
| 52 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().("Race", func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { testRacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testRace().(t, binaryPath) }) |
| 53 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().("Minimization1", func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { testMinimization1scip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testMinimization1().(t, binaryPath) }) |
| 54 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().("Minimization2", func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { testMinimization2scip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testMinimization2().(t, binaryPath) }) |
| 55 | } |
| 56 | |
| 57 | func testBasicscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testBasic().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, binaryPath string) { |
| 58 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 59 | args := []string{"-numit=1", "-numfcns=1", "-numpkgs=1", "-seed=103", "-cleancache=0"} |
| 60 | c := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(binaryPath, args...) |
| 61 | b, err := c.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().() |
| 62 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s\n", b) |
| 63 | if err != nil { |
| 64 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("error invoking fuzz-runner: %v", err) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func testRacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testRace().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, binaryPath string) { |
| 69 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 70 | // For this test to work, the current test platform has to support the |
| 71 | // race detector. Check to see if that is the case by running a very |
| 72 | // simple Go program through it. |
| 73 | if !canRacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/canRace().() { |
| 74 | t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("current platform does not appear to support the race detector") |
| 75 | } |
| 76 | |
| 77 | args := []string{"-v=1", "-numit=1", "-race", "-numfcns=3", "-numpkgs=3", "-seed=987", "-cleancache=0"} |
| 78 | c := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(binaryPath, args...) |
| 79 | b, err := c.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().() |
| 80 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s\n", b) |
| 81 | if err != nil { |
| 82 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("error invoking fuzz-runner: %v", err) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func testMinimization1scip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testMinimization1().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, binaryPath string) { |
| 87 | if binaryPath == "" { |
| 88 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("No runner binary") |
| 89 | } |
| 90 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 91 | // Fire off the runner passing it -emitbad=1, so that the generated code |
| 92 | // contains illegal Go code (which will force the build to fail). Verify that |
| 93 | // it does fail, that the error reflects the nature of the failure, and that |
| 94 | // we can minimize the error down to a single package. |
| 95 | args := []string{"-emitbad=1", "-badfcnidx=2", "-badpkgidx=2", |
| 96 | "-forcetmpclean", "-cleancache=0", |
| 97 | "-numit=1", "-numfcns=3", "-numpkgs=3", "-seed=909"} |
| 98 | invocation := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s %v", binaryPath, args) |
| 99 | c := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(binaryPath, args...) |
| 100 | b, err := c.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().() |
| 101 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s\n", b) |
| 102 | if err == nil { |
| 103 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unexpected pass of fuzz-runner (invocation %q): %v", invocation, err) |
| 104 | } |
| 105 | result := string(b) |
| 106 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "syntax error") { |
| 107 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("-emitbad=1 did not trigger syntax error (invocation %q): output: %s", invocation, result) |
| 108 | } |
| 109 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "package minimization succeeded: found bad pkg 2") { |
| 110 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to minimize package (invocation %q): output: %s", invocation, result) |
| 111 | } |
| 112 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "function minimization succeeded: found bad fcn 2") { |
| 113 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to minimize package (invocation %q): output: %s", invocation, result) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func testMinimization2scip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/signature-fuzzer/fuzz-runner`/testMinimization2().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, binaryPath string) { |
| 118 | if binaryPath == "" { |
| 119 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("No runner binary") |
| 120 | } |
| 121 | t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().() |
| 122 | // Fire off the runner passing it -emitbad=2, so that the |
| 123 | // generated code forces a runtime error. Verify that it does |
| 124 | // fail, and that the error is reflective. |
| 125 | args := []string{"-emitbad=2", "-badfcnidx=1", "-badpkgidx=1", |
| 126 | "-forcetmpclean", "-cleancache=0", |
| 127 | "-numit=1", "-numfcns=3", "-numpkgs=3", "-seed=55909"} |
| 128 | invocation := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s %v", binaryPath, args) |
| 129 | c := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(binaryPath, args...) |
| 130 | b, err := c.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().() |
| 131 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s\n", b) |
| 132 | if err == nil { |
| 133 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("unexpected pass of fuzz-runner (invocation %q): %v", invocation, err) |
| 134 | } |
| 135 | result := string(b) |
| 136 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "Error: fail") || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "Checker1.Test1") { |
| 137 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("-emitbad=2 did not trigger runtime error (invocation %q): output: %s", invocation, result) |
| 138 | } |
| 139 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "package minimization succeeded: found bad pkg 1") { |
| 140 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to minimize package (invocation %q): output: %s", invocation, result) |
| 141 | } |
| 142 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(result, "function minimization succeeded: found bad fcn 1") { |
| 143 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("failed to minimize package (invocation %q): output: %s", invocation, result) |
| 144 | } |
| 145 | } |