Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial fuzzing harness #153

Merged
merged 13 commits into from
Apr 24, 2020
Prev Previous commit
Next Next commit
different db's per instance
  • Loading branch information
willscott committed Mar 27, 2020
commit 5da85dd895fded16e46289cbf10aabbb83f1bef4
6 changes: 6 additions & 0 deletions fuzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
IPFS Datastore Fuzzer
====

The fuzzer provides a [go fuzzer](https://github.com/dvyukov/go-fuzz) interface
to the Datastore interface. This can be used for fuzz testing of datastore
implementations.
39 changes: 28 additions & 11 deletions fuzz/fuzzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ package fuzzer
import (
"context"
"io"
"io/ioutil"
"os"
"sync"
"sync/atomic"

ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-ds-badger"
badger "github.com/ipfs/go-ds-badger"
)

type donefunc func() error

// DsOpener is the concrete datastore. that Fuzz will fuzz against.
var DsOpener func() ds.TxnDatastore
var DsOpener func() (ds.TxnDatastore, donefunc)
var dsInst ds.TxnDatastore
var df donefunc

// Threads is a measure of concurrency.
var Threads int
var wg sync.WaitGroup

func init() {
DsOpener = func() ds.TxnDatastore {
d, _ := badger.NewDatastore("tmp", &badger.DefaultOptions)
return d
DsOpener = func() (ds.TxnDatastore, donefunc) {
dir, _ := ioutil.TempDir("", "fuzz*")
d, _ := badger.NewDatastore(dir, &badger.DefaultOptions)
return d, func() error { return os.RemoveAll(dir) }
}
Threads = 1

Expand All @@ -31,11 +39,10 @@ func setup() ([]chan byte, context.CancelFunc) {
// TODO: dynamic thread starting.
ctx, cncl := context.WithCancel(context.Background())

if dsInst != nil {
dsInst.Close()
}
dsInst = DsOpener()
cleanup()
dsInst, df = DsOpener()

wg.Add(Threads)
drivers := make([]chan byte, Threads)
for i := 0; i < Threads; i++ {
drivers[i] = make(chan byte, 15)
Expand All @@ -44,6 +51,13 @@ func setup() ([]chan byte, context.CancelFunc) {
return drivers, cncl
}

func cleanup() {
if dsInst != nil {
dsInst.Close()
df()
}
}

// Fuzz is a go-fuzzer compatible input point for replaying
// data (interpreted as a script of commands)
// to a chosen ipfs datastore implementation
Expand All @@ -54,7 +68,8 @@ func Fuzz(data []byte) int {
close(drivers[i])
}
cncl()
dsInst.Close()
wg.Wait()
cleanup()
return 0
}

Expand All @@ -79,7 +94,8 @@ func FuzzStream(data io.Reader) int {
close(drivers[i])
}
cncl()
dsInst.Close()
wg.Wait()
cleanup()
return 0
}

Expand Down Expand Up @@ -111,6 +127,7 @@ type state struct {
}

func threadDriver(ctx context.Context, cmnds chan byte) error {
defer wg.Done()
s := state{}
s.reader = dsInst
s.writer = dsInst
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/ipfs/go-datastore

require (
github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813 // indirect
github.com/google/uuid v1.1.1
github.com/ipfs/go-ds-badger v0.2.1
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczC
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813 h1:NgO45/5mBLRVfiXerEFzH6ikcZ7DNRPS639xFg3ENzU=
github.com/dvyukov/go-fuzz v0.0.0-20200318091601-be3528f3a813/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down