Go supports fuzzing in its standard toolchain beginning in Go 1.18.
Fuzzing is a type of automated testing which continuously manipulates inputs to a program to find bugs. Go fuzzing uses coverage guidance to intelligently walk through the code being fuzzed to find and report failures to the user. Since it can reach edge cases which humans often miss, fuzz testing can be particularly valuable for finding security exploits and vulnerabilities.
Below is an example of a fuzz test, highlighting it's main components.
Below are rules that fuzz tests must follow.
FuzzXxx
, which accepts only a *testing.F
, and has no return value.*testing.T
as the first parameter, followed by the fuzzing arguments. There is no return value.Below are suggestions that will help you get the most out of fuzzing.
The default go command settings should work for most use cases of fuzzing. So typically, an execution of fuzzing on the command line should look like this:
$ go test -fuzz={FuzzTestName}
However, the go
command does provide a few settings when running fuzzing. These are documented in the cmd/go
package docs.
To highlight a few:
-fuzztime
: the total time or number of iterations that the fuzz target will be executed before exiting, default indefinitely.-fuzzminimizetime
: the time or number of iterations that the fuzz target will be executed during each minimization attempt, default 60sec. You can completely disable minimization by setting -fuzzminimizetime 0
when fuzzing.-parallel
: the number of fuzzing processes running at once, default $GOMAXPROCS
. Currently, setting -cpu during fuzzing has no effect.corpus entry: An input in the corpus which can be used while fuzzing. This can be a specially-formatted file, or a call to (*testing.F).Add.
coverage guidance: A method of fuzzing which uses expansions in code coverage to determine which corpus entries are worth keeping for future use.
fuzz target: The function of the fuzz test which is executed for corpus entries and generated values while fuzzing. It is provided to the fuzz test by passing the function to (*testing.F).Fuzz.
fuzz test: A function in a test file of the form func FuzzXxx(*testing.F)
which can be used for fuzzing.
fuzzing: A type of automated testing which continuously manipulates inputs to a program to find issues such as bugs or vulnerabilities to which the code may be susceptible.
fuzzing arguments: The types which will be passed to the fuzz target, and mutated by the mutator.
fuzzing engine: A tool that manages fuzzing, including maintaining the corpus, invoking the mutator, identifying new coverage, and reporting failures.
generated corpus: A corpus which is maintained by the fuzzing engine over time while fuzzing to keep track of progress. It is stored in $GOCACHE
/fuzz.
mutator: A tool used while fuzzing which randomly manipulates corpus entries before passing them to a fuzz target.
package: A collection of source files in the same directory that are compiled together. See the Packages section in the Go Language Specification.
seed corpus: A user-provided corpus for a fuzz test which can be used to guide the fuzzing engine. It is composed of the corpus entries provided by f.Add calls within the fuzz test, and the files in the testdata/fuzz/{FuzzTestName} directory within the package.
test file: A file of the format xxx_test.go that may contain tests, benchmarks, examples and fuzz tests.
vulnerability: A security-sensitive weakness in code which can be exploited by an attacker.
If you experience any problems or have an idea for a feature, please file an issue.
For discussion and general feedback about the feature, you can also participate in the #fuzzing channel in Gophers Slack.