mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
move snap fuzzers
This commit is contained in:
parent
19c53b0718
commit
ea0a83a1c7
3 changed files with 76 additions and 85 deletions
|
|
@ -21,6 +21,7 @@ import (
|
|||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
|
@ -28,7 +29,6 @@ import (
|
|||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
|
|
@ -36,6 +36,56 @@ import (
|
|||
fuzz "github.com/google/gofuzz"
|
||||
)
|
||||
|
||||
func FuzzARange(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &GetAccountRangePacket{}, GetAccountRangeMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzSRange(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &GetStorageRangesPacket{}, GetStorageRangesMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzByteCodes(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &GetByteCodesPacket{}, GetByteCodesMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzTrieNodes(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &GetTrieNodesPacket{}, GetTrieNodesMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func doFuzz(input []byte, obj interface{}, code int) {
|
||||
bc := getChain()
|
||||
defer bc.Stop()
|
||||
fuzz.NewFromGoFuzz(input).Fuzz(obj)
|
||||
var data []byte
|
||||
switch p := obj.(type) {
|
||||
case *GetTrieNodesPacket:
|
||||
p.Root = trieRoot
|
||||
data, _ = rlp.EncodeToBytes(obj)
|
||||
default:
|
||||
data, _ = rlp.EncodeToBytes(obj)
|
||||
}
|
||||
cli := &dummyRW{
|
||||
code: uint64(code),
|
||||
data: data,
|
||||
}
|
||||
peer := NewFakePeer(65, "gazonk01", cli)
|
||||
err := HandleMessage(&dummyBackend{bc}, peer)
|
||||
switch {
|
||||
case err == nil && cli.writeCount != 1:
|
||||
panic(fmt.Sprintf("Expected 1 response, got %d", cli.writeCount))
|
||||
case err != nil && cli.writeCount != 0:
|
||||
panic(fmt.Sprintf("Expected 0 response, got %d", cli.writeCount))
|
||||
}
|
||||
}
|
||||
|
||||
var trieRoot common.Hash
|
||||
|
||||
func getChain() *core.BlockChain {
|
||||
|
|
@ -86,10 +136,10 @@ type dummyBackend struct {
|
|||
chain *core.BlockChain
|
||||
}
|
||||
|
||||
func (d *dummyBackend) Chain() *core.BlockChain { return d.chain }
|
||||
func (d *dummyBackend) RunPeer(*snap.Peer, snap.Handler) error { return nil }
|
||||
func (d *dummyBackend) PeerInfo(enode.ID) interface{} { return "Foo" }
|
||||
func (d *dummyBackend) Handle(*snap.Peer, snap.Packet) error { return nil }
|
||||
func (d *dummyBackend) Chain() *core.BlockChain { return d.chain }
|
||||
func (d *dummyBackend) RunPeer(*Peer, Handler) error { return nil }
|
||||
func (d *dummyBackend) PeerInfo(enode.ID) interface{} { return "Foo" }
|
||||
func (d *dummyBackend) Handle(*Peer, Packet) error { return nil }
|
||||
|
||||
type dummyRW struct {
|
||||
code uint64
|
||||
|
|
@ -110,34 +160,3 @@ func (d *dummyRW) WriteMsg(msg p2p.Msg) error {
|
|||
d.writeCount++
|
||||
return nil
|
||||
}
|
||||
|
||||
func doFuzz(input []byte, obj interface{}, code int) int {
|
||||
if len(input) > 1024*4 {
|
||||
return -1
|
||||
}
|
||||
bc := getChain()
|
||||
defer bc.Stop()
|
||||
backend := &dummyBackend{bc}
|
||||
fuzz.NewFromGoFuzz(input).Fuzz(obj)
|
||||
var data []byte
|
||||
switch p := obj.(type) {
|
||||
case *snap.GetTrieNodesPacket:
|
||||
p.Root = trieRoot
|
||||
data, _ = rlp.EncodeToBytes(obj)
|
||||
default:
|
||||
data, _ = rlp.EncodeToBytes(obj)
|
||||
}
|
||||
cli := &dummyRW{
|
||||
code: uint64(code),
|
||||
data: data,
|
||||
}
|
||||
peer := snap.NewFakePeer(65, "gazonk01", cli)
|
||||
err := snap.HandleMessage(backend, peer)
|
||||
switch {
|
||||
case err == nil && cli.writeCount != 1:
|
||||
panic(fmt.Sprintf("Expected 1 response, got %d", cli.writeCount))
|
||||
case err != nil && cli.writeCount != 0:
|
||||
panic(fmt.Sprintf("Expected 0 response, got %d", cli.writeCount))
|
||||
}
|
||||
return 1
|
||||
}
|
||||
23
oss-fuzz.sh
23
oss-fuzz.sh
|
|
@ -130,21 +130,40 @@ go install github.com/holiman/gofuzz-shim@latest
|
|||
# fuzzKeystore
|
||||
#
|
||||
#compile_fuzzer github.com/ethereum/go-ethereum/trie FuzzTrie fuzzTrie
|
||||
pkg=$GOPATH/src/github.com/ethereum/go-ethereum/trie/
|
||||
compile_fuzzer github.com/ethereum/go-ethereum/trie \
|
||||
FuzzTrie \
|
||||
$GOPATH/src/github.com/ethereum/go-ethereum/trie/trie_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/database_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/tracer_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/proof_test.go \
|
||||
$pkg/trie_test.go,$pkg/database_test.go,$pkg/tracer_test.go,$pkg/proof_test.go,$pkg/iterator_test.go,$pkg/sync_test.go \
|
||||
fuzzTrie
|
||||
|
||||
#compile_fuzzer github.com/ethereum/go-ethereum/trie FuzzStackTrie fuzzStackTrie
|
||||
compile_fuzzer github.com/ethereum/go-ethereum/trie \
|
||||
FuzzStackTrie \
|
||||
$GOPATH/src/github.com/ethereum/go-ethereum/trie/stacktrie_fuzzer_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/iterator_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/trie_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/database_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/tracer_test.go,$GOPATH/src/github.com/ethereum/go-ethereum/trie/proof_test.go \
|
||||
$pkg/stacktrie_fuzzer_test.go,$pkg/iterator_test.go,$pkg/trie_test.go,$pkg/database_test.go,$pkg/tracer_test.go,$pkg/proof_test.go,$pkg/sync_test.go \
|
||||
fuzzStackTrie
|
||||
|
||||
#compile_fuzzer tests/fuzzers/snap FuzzARange fuzz_account_range
|
||||
compile_fuzzer github.com/ethereum/go-ethereum/eth/protocols/snap \
|
||||
FuzzARange \
|
||||
$GOPATH/src/github.com/ethereum/go-ethereum/eth/protocols/snap/handler_fuzzing_test.go \
|
||||
fuzz_account_range
|
||||
|
||||
#compile_fuzzer tests/fuzzers/snap FuzzSRange fuzz_storage_range
|
||||
compile_fuzzer github.com/ethereum/go-ethereum/eth/protocols/snap \
|
||||
FuzzSRange \
|
||||
$GOPATH/src/github.com/ethereum/go-ethereum/eth/protocols/snap/handler_fuzzing_test.go \
|
||||
fuzz_storage_range
|
||||
|
||||
#compile_fuzzer tests/fuzzers/snap FuzzByteCodes fuzz_byte_codes
|
||||
compile_fuzzer github.com/ethereum/go-ethereum/eth/protocols/snap \
|
||||
FuzzByteCodes \
|
||||
$GOPATH/src/github.com/ethereum/go-ethereum/eth/protocols/snap/handler_fuzzing_test.go \
|
||||
fuzz_byte_codes
|
||||
#compile_fuzzer tests/fuzzers/snap FuzzTrieNodes fuzz_trie_nodes
|
||||
compile_fuzzer github.com/ethereum/go-ethereum/eth/protocols/snap \
|
||||
FuzzTrieNodes \
|
||||
$GOPATH/src/github.com/ethereum/go-ethereum/eth/protocols/snap/handler_fuzzing_test.go \
|
||||
fuzz_trie_nodes
|
||||
|
||||
|
||||
#compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
// Copyright 2023 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package snap
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/eth/protocols/snap"
|
||||
)
|
||||
|
||||
func FuzzARange(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &snap.GetAccountRangePacket{}, snap.GetAccountRangeMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzSRange(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &snap.GetStorageRangesPacket{}, snap.GetStorageRangesMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzByteCodes(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &snap.GetByteCodesPacket{}, snap.GetByteCodesMsg)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzTrieNodes(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
doFuzz(data, &snap.GetTrieNodesPacket{}, snap.GetTrieNodesMsg)
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue