Skip to content

Commit 874de58

Browse files
authored
Merge pull request #157 from ipfs/feat/fuzzflatfs
support flatfs fuzzing
2 parents 09aec49 + e876767 commit 874de58

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

fuzz/cmd/generate/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import prov "{{ .Package }}"
6464
import ds "github.com/ipfs/go-datastore"
6565
6666
func init() {
67-
AddOpener("{{ .PackageName }}", func(loc string) ds.TxnDatastore {
67+
AddOpener("{{ .PackageName }}", func(loc string) ds.Datastore {
6868
d, err := prov.NewDatastore(loc, nil)
6969
if err != nil {
7070
panic("could not create db instance")

fuzz/fuzzer.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616
//go:generate go run ./cmd/generate
1717

1818
// openers contains the known datastore implementations.
19-
var openers map[string]func(string) ds.TxnDatastore
19+
var openers map[string]func(string) ds.Datastore
2020

2121
// AddOpener allows registration of a new driver for fuzzing.
22-
func AddOpener(name string, opener func(loc string) ds.TxnDatastore) {
22+
func AddOpener(name string, opener func(loc string) ds.Datastore) {
2323
if openers == nil {
24-
openers = make(map[string]func(string) ds.TxnDatastore)
24+
openers = make(map[string]func(string) ds.Datastore)
2525
}
2626
openers[name] = opener
2727
}
@@ -32,14 +32,16 @@ var Threads int
3232

3333
func init() {
3434
if openers == nil {
35-
openers = make(map[string]func(string) ds.TxnDatastore)
35+
openers = make(map[string]func(string) ds.Datastore)
3636
}
3737
Threads = 1
3838
}
3939

4040
// RunState encapulates the state of a given fuzzing run
4141
type RunState struct {
42-
inst ds.TxnDatastore
42+
inst ds.Datastore
43+
// opMax signals which operations the instance supports.
44+
opMax op
4345
inputChannels []chan<- byte
4446
wg sync.WaitGroup
4547
Cancel context.CancelFunc
@@ -50,10 +52,18 @@ type RunState struct {
5052
}
5153

5254
// DB returns the datastore being driven by this instance
53-
func (r *RunState) DB() ds.TxnDatastore {
55+
func (r *RunState) DB() ds.Datastore {
5456
return r.inst
5557
}
5658

59+
// TxnDB returns the transaciton database if the store under test supports transactions
60+
func (r *RunState) TxnDB() ds.TxnDatastore {
61+
if txdb, ok := r.inst.(ds.TxnDatastore); ok {
62+
return txdb
63+
}
64+
return nil
65+
}
66+
5767
type threadState struct {
5868
op
5969
keyReady bool
@@ -78,6 +88,11 @@ func Open(driver string, location string, cleanup bool) (*RunState, error) {
7888

7989
state := RunState{}
8090
state.inst = opener(location)
91+
state.opMax = opMax
92+
// don't attempt transaction operations on non-txn datastores.
93+
if state.TxnDB() == nil {
94+
state.opMax = opNewTX
95+
}
8196
state.keyCache[0] = ds.NewKey("/")
8297
state.cachedKeys = 1
8398

@@ -146,7 +161,7 @@ func FuzzDB(driver string, location string, cleanup bool, data []byte) int {
146161

147162
// FuzzStream does the same as fuzz but with streaming input
148163
func FuzzStream(driver string, location string, cleanup bool, data io.Reader) error {
149-
inst, err := Open("badger", "tmp", true)
164+
inst, err := Open(driver, location, cleanup)
150165
if err != nil {
151166
return err
152167
}
@@ -185,10 +200,10 @@ const (
185200
opQuery
186201
opPut
187202
opDelete
203+
opSync
188204
opNewTX
189205
opCommitTX
190206
opDiscardTX
191-
opSync
192207
opMax
193208
)
194209

@@ -214,7 +229,7 @@ func threadDriver(ctx context.Context, runState *RunState, cmnds chan byte) {
214229

215230
func nextState(s *threadState, c byte) error {
216231
if s.op == opNone {
217-
s.op = op(c) % opMax
232+
s.op = op(c) % s.RunState.opMax
218233
return nil
219234
} else if s.op == opGet {
220235
if !s.keyReady {
@@ -267,11 +282,13 @@ func nextState(s *threadState, c byte) error {
267282
return nil
268283
} else if s.op == opNewTX {
269284
if s.txn == nil {
270-
s.txn, _ = s.RunState.inst.NewTransaction(((c & 1) == 1))
271-
if (c & 1) != 1 { // read+write
272-
s.writer = s.txn
285+
if tdb := s.RunState.TxnDB(); tdb != nil {
286+
s.txn, _ = tdb.NewTransaction(((c & 1) == 1))
287+
if (c & 1) != 1 { // read+write
288+
s.writer = s.txn
289+
}
290+
s.reader = s.txn
273291
}
274-
s.reader = s.txn
275292
}
276293
reset(s)
277294
return nil

fuzz/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ replace github.com/ipfs/go-datastore => ../
66

77
require (
88
github.com/ipfs/go-datastore v0.4.4
9-
github.com/ipfs/go-ds-badger v0.2.4
109
github.com/spf13/pflag v1.0.3
1110
)

fuzz/go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9 h1:HD8gA2tkBy
22
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
44
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
5+
github.com/alexbrainman/goissue34681 v0.0.0-20191006012335-3fc7a47baff5/go.mod h1:Y2QMoi1vgtOIfc+6DhrMOGkLoGzqSV2rKp4Sm+opsyA=
56
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
67
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
78
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
@@ -32,9 +33,15 @@ github.com/ipfs/go-datastore v0.4.4 h1:rjvQ9+muFaJ+QZ7dN5B1MSDNQ0JVZKkkES/rMZmA8
3233
github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA=
3334
github.com/ipfs/go-ds-badger v0.2.4 h1:UPGB0y7luFHk+mY/tUZrif/272M8o+hFsW+avLUeWrM=
3435
github.com/ipfs/go-ds-badger v0.2.4/go.mod h1:pEYw0rgg3FIrywKKnL+Snr+w/LjJZVMTBRn4FS6UHUk=
36+
github.com/ipfs/go-ds-flatfs v0.4.4 h1:DmGZ4qOYQLNgu8Mltuz1DtUHpm+BjWMcVN3F3H3VJzQ=
37+
github.com/ipfs/go-ds-flatfs v0.4.4/go.mod h1:e4TesLyZoA8k1gV/yCuBTnt2PJtypn4XUlB5n8KQMZY=
3538
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
3639
github.com/ipfs/go-log v0.0.1 h1:9XTUN/rW64BCG1YhPK9Hoy3q8nr4gOmHHBpgFdfw6Lc=
3740
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=
41+
github.com/ipfs/go-log v1.0.3 h1:Gg7SUYSZ7BrqaKMwM+hRgcAkKv4QLfzP4XPQt5Sx/OI=
42+
github.com/ipfs/go-log v1.0.3/go.mod h1:OsLySYkwIbiSUR/yBTdv1qPtcE4FW3WPWk/ewz9Ru+A=
43+
github.com/ipfs/go-log/v2 v2.0.3 h1:Q2gXcBoCALyLN/pUQlz1qgu0x3uFV6FzP9oXhpfyJpc=
44+
github.com/ipfs/go-log/v2 v2.0.3/go.mod h1:O7P1lJt27vWHhOwQmcFEvlmo49ry2VY2+JfBWFaa9+0=
3845
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
3946
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY=
4047
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
@@ -54,6 +61,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
5461
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
5562
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
5663
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
64+
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
65+
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
5766
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
5867
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
5968
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -77,9 +86,15 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljT
7786
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo=
7887
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM=
7988
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
89+
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
90+
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
8091
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
92+
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
93+
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
8194
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
8295
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
96+
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
97+
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
8398
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
8499
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
85100
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

0 commit comments

Comments
 (0)