Skip to content

Commit

Permalink
Backport fuzzing support
Browse files Browse the repository at this point in the history
Summary:
Essentially backport snippets of the following commit:
golang/go@33c89be

Gives us support for Go fuzzing: https://go.dev/doc/security/fuzz/

#build_rule_type[go_library,go_binary,go_unittest,go_test]

Reviewed By: inesusvet

Differential Revision: D68397313

fbshipit-source-id: e94da245be2c1a761206869a99255effcb320fe4
  • Loading branch information
echistyakov authored and facebook-github-bot committed Jan 20, 2025
1 parent 72a115b commit 3902e98
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions prelude/go_bootstrap/tools/go/testmaingen.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ type Package struct {
}

// isTestFunc tells whether fn has the type of a testing function. arg
// specifies the parameter type we look for: B, M or T.
// specifies the parameter type we look for: B, F, M or T.
func isTestFunc(fn *ast.FuncDecl, arg string) bool {
if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
fn.Type.Params.List == nil ||
Expand All @@ -218,7 +218,7 @@ func isTestFunc(fn *ast.FuncDecl, arg string) bool {
// We can't easily check that the type is *testing.M
// because we don't know how testing has been imported,
// but at least check that it's *M or *something.M.
// Same applies for B and T.
// Same applies for B, F and T.
if name, ok := ptr.X.(*ast.Ident); ok && name.Name == arg {
return true
}
Expand Down Expand Up @@ -286,6 +286,7 @@ type coverInfo struct {
type testFuncs struct {
Tests []testFunc
Benchmarks []testFunc
FuzzTargets []testFunc
Examples []testFunc
TestMain *testFunc
Package *Package
Expand Down Expand Up @@ -377,6 +378,13 @@ func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
}
t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, "", false})
*doImport, *seen = true, true
case isTest(name, "Fuzz"):
err := checkTestFunc(n, "F")
if err != nil {
return err
}
t.FuzzTargets = append(t.FuzzTargets, testFunc{pkg, name, "", false})
*doImport, *seen = true, true
}
}
ex := doc.Examples(f)
Expand Down Expand Up @@ -452,6 +460,9 @@ var benchmarks = []testing.InternalBenchmark{
}
{{if .ReleaseTag "go1.18"}}
var fuzzTargets = []testing.InternalFuzzTarget{
{{range .FuzzTargets}}
{"{{.Name}}", {{.Package}}.{{.Name}}},
{{end}}
}
{{end}}
var examples = []testing.InternalExample{
Expand Down

0 comments on commit 3902e98

Please sign in to comment.