Skip to content

Commit

Permalink
Add NewFromGoFuzz
Browse files Browse the repository at this point in the history
Add a helper function that enables using gofuzz (this
project) with go-fuzz (https://github.com/dvyukov/go-fuzz) for continuose
fuzzing. Essentially, it enables translating the fuzzing bytes from
go-fuzz to any Go object using this library.

This change will enable using this project with fuzzing websites such as fuzzit.dev
or fuzzbuzz.io.

I've created an example in: https://github.com/posener/fuzzme

Fixes google#33
  • Loading branch information
posener committed Jan 23, 2020
1 parent db92cf7 commit 4224d54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package fuzz

import (
"fmt"
"math"
"math/big"
"math/rand"
"reflect"
"regexp"
Expand Down Expand Up @@ -61,6 +63,33 @@ func NewWithSeed(seed int64) *Fuzzer {
return f
}

// NewFromGoFuzz is a helper function that enables using gofuzz (this
// project) with go-fuzz (https://github.com/dvyukov/go-fuzz) for continuous
// fuzzing. Essentially, it enables translating the fuzzing bytes from
// go-fuzz to any Go object using this library.
//
// Example: use go-fuzz to test the function `MyFunc(int)` in the package
// `mypackage`. Add the file: "mypacakge_fuzz.go" with the content:
//
// // +build gofuzz
// package mypacakge
// import "github.com/google/go-fuzz"
// func Fuzz(data []byte) int {
// var i int
// fuzz.NewFromGoFuzz(data).Fuzz(&i)
// MyFunc(i)
// return 0
// }
func NewFromGoFuzz(data []byte) *Fuzzer {
maxint := big.NewInt(math.MaxInt64)
seed := big.NewInt(1)
for _, b := range data {
seed.Mul(seed, big.NewInt(int64(b)))
seed.Mod(seed, maxint)
}
return NewWithSeed(seed.Int64())
}

// Funcs adds each entry in fuzzFuncs as a custom fuzzing function.
//
// Each entry in fuzzFuncs must be a function taking two parameters.
Expand Down
25 changes: 25 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,28 @@ func TestFuzz_SkipPattern(t *testing.T) {
return 5, true
})
}

func TestNewFromGoFuzz(t *testing.T) {
tests := []struct {
data []byte
want int
}{
// Tests consistant fuzzing for the same input.
{data: []byte("a"), want: 943890139707072619},
// Tests that different input corresponds in different output.
{data: []byte("b"), want: -7308824908432364150},
// Tests long input.
{
data: []byte("123jhasd0128931`3kljahsdlkasb,mxznbcqlsjerqwporu108378091237ksdjhlakshz"),
want: 3588769380299097370,
},
}

for _, tt := range tests {
var got int
NewFromGoFuzz(tt.data).Fuzz(&got)
if tt.want != got {
t.Errorf("Fuzz(%q) = %d, want: %d", tt.data, got, tt.want)
}
}
}

0 comments on commit 4224d54

Please sign in to comment.