tests/fuzzers: improve the fuzzers (#21829)

* tests/fuzzers, common/bitutil: make fuzzers use correct returnvalues + remove output

* tests/fuzzers/stacktrie: fix duplicate-key insertion in stacktrie (false positive)

* tests/fuzzers/stacktrie: fix compilation error

* tests/fuzzers: linter nits
pull/21860/head
Martin Holst Swende 4 years ago committed by GitHub
parent 9ded4e33c5
commit 0703c91fba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      common/bitutil/compress_fuzz.go
  2. 2
      tests/fuzzers/keystore/keystore-fuzzer.go
  3. 10
      tests/fuzzers/rlp/rlp_fuzzer.go
  4. 7
      tests/fuzzers/stacktrie/trie_fuzzer.go
  5. 14
      tests/fuzzers/txfetcher/txfetcher_fuzzer.go

@ -24,7 +24,7 @@ import "bytes"
// invocations. // invocations.
func Fuzz(data []byte) int { func Fuzz(data []byte) int {
if len(data) == 0 { if len(data) == 0 {
return -1 return 0
} }
if data[0]%2 == 0 { if data[0]%2 == 0 {
return fuzzEncode(data[1:]) return fuzzEncode(data[1:])
@ -39,7 +39,7 @@ func fuzzEncode(data []byte) int {
if !bytes.Equal(data, proc) { if !bytes.Equal(data, proc) {
panic("content mismatch") panic("content mismatch")
} }
return 0 return 1
} }
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
@ -52,5 +52,5 @@ func fuzzDecode(data []byte) int {
if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) { if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
panic("content mismatch") panic("content mismatch")
} }
return 0 return 1
} }

@ -33,5 +33,5 @@ func Fuzz(input []byte) int {
panic(err) panic(err)
} }
os.Remove(a.URL.Path) os.Remove(a.URL.Path)
return 0 return 1
} }

@ -37,19 +37,19 @@ func decodeEncode(input []byte, val interface{}, i int) {
} }
func Fuzz(input []byte) int { func Fuzz(input []byte) int {
if len(input) == 0 {
return 0
}
var i int var i int
{ {
if len(input) > 0 {
rlp.Split(input) rlp.Split(input)
} }
}
{ {
if len(input) > 0 {
if elems, _, err := rlp.SplitList(input); err == nil { if elems, _, err := rlp.SplitList(input); err == nil {
rlp.CountValues(elems) rlp.CountValues(elems)
} }
} }
}
{ {
rlp.NewStream(bytes.NewReader(input), 0).Decode(new(interface{})) rlp.NewStream(bytes.NewReader(input), 0).Decode(new(interface{}))
@ -123,5 +123,5 @@ func Fuzz(input []byte) int {
var rs types.Receipts var rs types.Receipts
decodeEncode(input, &rs, i) decodeEncode(input, &rs, i)
} }
return 0 return 1
} }

@ -148,6 +148,8 @@ func (f *fuzzer) fuzz() int {
vals kvs vals kvs
useful bool useful bool
maxElements = 10000 maxElements = 10000
// operate on unique keys only
keys = make(map[string]struct{})
) )
// Fill the trie with elements // Fill the trie with elements
for i := 0; !f.exhausted && i < maxElements; i++ { for i := 0; !f.exhausted && i < maxElements; i++ {
@ -158,6 +160,11 @@ func (f *fuzzer) fuzz() int {
// thus 'deletion' which is not supported on stacktrie // thus 'deletion' which is not supported on stacktrie
break break
} }
if _, present := keys[string(k)]; present {
// This key is a duplicate, ignore it
continue
}
keys[string(k)] = struct{}{}
vals = append(vals, kv{k: k, v: v}) vals = append(vals, kv{k: k, v: v})
trieA.Update(k, v) trieA.Update(k, v)
useful = true useful = true

@ -51,8 +51,9 @@ func init() {
func Fuzz(input []byte) int { func Fuzz(input []byte) int {
// Don't generate insanely large test cases, not much value in them // Don't generate insanely large test cases, not much value in them
if len(input) > 16*1024 { if len(input) > 16*1024 {
return -1 return 0
} }
verbose := false
r := bytes.NewReader(input) r := bytes.NewReader(input)
// Reduce the problem space for certain fuzz runs. Small tx space is better // Reduce the problem space for certain fuzz runs. Small tx space is better
@ -124,7 +125,9 @@ func Fuzz(input []byte) int {
announceIdxs[i] = (int(annBuf[0])*256 + int(annBuf[1])) % len(txs) announceIdxs[i] = (int(annBuf[0])*256 + int(annBuf[1])) % len(txs)
announces[i] = txs[announceIdxs[i]].Hash() announces[i] = txs[announceIdxs[i]].Hash()
} }
if verbose {
fmt.Println("Notify", peer, announceIdxs) fmt.Println("Notify", peer, announceIdxs)
}
if err := f.Notify(peer, announces); err != nil { if err := f.Notify(peer, announces); err != nil {
panic(err) panic(err)
} }
@ -163,8 +166,9 @@ func Fuzz(input []byte) int {
return 0 return 0
} }
direct := (directFlag % 2) == 0 direct := (directFlag % 2) == 0
if verbose {
fmt.Println("Enqueue", peer, deliverIdxs, direct) fmt.Println("Enqueue", peer, deliverIdxs, direct)
}
if err := f.Enqueue(peer, deliveries, direct); err != nil { if err := f.Enqueue(peer, deliveries, direct); err != nil {
panic(err) panic(err)
} }
@ -177,8 +181,9 @@ func Fuzz(input []byte) int {
return 0 return 0
} }
peer := peers[int(peerIdx)%len(peers)] peer := peers[int(peerIdx)%len(peers)]
if verbose {
fmt.Println("Drop", peer) fmt.Println("Drop", peer)
}
if err := f.Drop(peer); err != nil { if err := f.Drop(peer); err != nil {
panic(err) panic(err)
} }
@ -191,8 +196,9 @@ func Fuzz(input []byte) int {
return 0 return 0
} }
tick := time.Duration(tickCnt) * 100 * time.Millisecond tick := time.Duration(tickCnt) * 100 * time.Millisecond
if verbose {
fmt.Println("Sleep", tick) fmt.Println("Sleep", tick)
}
clock.Run(tick) clock.Run(tick)
} }
} }

Loading…
Cancel
Save