fuzz_handler.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2021 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package snap
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math/big"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/consensus/ethash"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/eth/protocols/snap"
  29. "github.com/ethereum/go-ethereum/p2p"
  30. "github.com/ethereum/go-ethereum/p2p/enode"
  31. "github.com/ethereum/go-ethereum/params"
  32. "github.com/ethereum/go-ethereum/rlp"
  33. fuzz "github.com/google/gofuzz"
  34. )
  35. var trieRoot common.Hash
  36. func getChain() *core.BlockChain {
  37. db := rawdb.NewMemoryDatabase()
  38. ga := make(core.GenesisAlloc, 1000)
  39. var a = make([]byte, 20)
  40. var mkStorage = func(k, v int) (common.Hash, common.Hash) {
  41. var kB = make([]byte, 32)
  42. var vB = make([]byte, 32)
  43. binary.LittleEndian.PutUint64(kB, uint64(k))
  44. binary.LittleEndian.PutUint64(vB, uint64(v))
  45. return common.BytesToHash(kB), common.BytesToHash(vB)
  46. }
  47. storage := make(map[common.Hash]common.Hash)
  48. for i := 0; i < 10; i++ {
  49. k, v := mkStorage(i, i)
  50. storage[k] = v
  51. }
  52. for i := 0; i < 1000; i++ {
  53. binary.LittleEndian.PutUint64(a, uint64(i+0xff))
  54. acc := core.GenesisAccount{Balance: big.NewInt(int64(i))}
  55. if i%2 == 1 {
  56. acc.Storage = storage
  57. }
  58. ga[common.BytesToAddress(a)] = acc
  59. }
  60. gspec := core.Genesis{
  61. Config: params.TestChainConfig,
  62. Alloc: ga,
  63. }
  64. genesis := gspec.MustCommit(db)
  65. blocks, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 2,
  66. func(i int, gen *core.BlockGen) {})
  67. cacheConf := &core.CacheConfig{
  68. TrieCleanLimit: 0,
  69. TrieDirtyLimit: 0,
  70. TrieTimeLimit: 5 * time.Minute,
  71. TrieCleanNoPrefetch: true,
  72. TrieCleanRejournal: 0,
  73. SnapshotLimit: 100,
  74. SnapshotWait: true,
  75. }
  76. trieRoot = blocks[len(blocks)-1].Root()
  77. bc, _ := core.NewBlockChain(db, cacheConf, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
  78. if _, err := bc.InsertChain(blocks); err != nil {
  79. panic(err)
  80. }
  81. return bc
  82. }
  83. type dummyBackend struct {
  84. chain *core.BlockChain
  85. }
  86. func (d *dummyBackend) Chain() *core.BlockChain { return d.chain }
  87. func (d *dummyBackend) RunPeer(*snap.Peer, snap.Handler) error { return nil }
  88. func (d *dummyBackend) PeerInfo(enode.ID) interface{} { return "Foo" }
  89. func (d *dummyBackend) Handle(*snap.Peer, snap.Packet) error { return nil }
  90. type dummyRW struct {
  91. code uint64
  92. data []byte
  93. writeCount int
  94. }
  95. func (d *dummyRW) ReadMsg() (p2p.Msg, error) {
  96. return p2p.Msg{
  97. Code: d.code,
  98. Payload: bytes.NewReader(d.data),
  99. ReceivedAt: time.Now(),
  100. Size: uint32(len(d.data)),
  101. }, nil
  102. }
  103. func (d *dummyRW) WriteMsg(msg p2p.Msg) error {
  104. d.writeCount++
  105. return nil
  106. }
  107. func doFuzz(input []byte, obj interface{}, code int) int {
  108. if len(input) > 1024*4 {
  109. return -1
  110. }
  111. bc := getChain()
  112. defer bc.Stop()
  113. backend := &dummyBackend{bc}
  114. fuzz.NewFromGoFuzz(input).Fuzz(obj)
  115. var data []byte
  116. switch p := obj.(type) {
  117. case *snap.GetTrieNodesPacket:
  118. p.Root = trieRoot
  119. data, _ = rlp.EncodeToBytes(obj)
  120. default:
  121. data, _ = rlp.EncodeToBytes(obj)
  122. }
  123. cli := &dummyRW{
  124. code: uint64(code),
  125. data: data,
  126. }
  127. peer := snap.NewFakePeer(65, "gazonk01", cli)
  128. err := snap.HandleMessage(backend, peer)
  129. switch {
  130. case err == nil && cli.writeCount != 1:
  131. panic(fmt.Sprintf("Expected 1 response, got %d", cli.writeCount))
  132. case err != nil && cli.writeCount != 0:
  133. panic(fmt.Sprintf("Expected 0 response, got %d", cli.writeCount))
  134. }
  135. return 1
  136. }
  137. // To run a fuzzer, do
  138. // $ CGO_ENABLED=0 go-fuzz-build -func FuzzTrieNodes
  139. // $ go-fuzz
  140. func FuzzARange(input []byte) int {
  141. return doFuzz(input, &snap.GetAccountRangePacket{}, snap.GetAccountRangeMsg)
  142. }
  143. func FuzzSRange(input []byte) int {
  144. return doFuzz(input, &snap.GetStorageRangesPacket{}, snap.GetStorageRangesMsg)
  145. }
  146. func FuzzByteCodes(input []byte) int {
  147. return doFuzz(input, &snap.GetByteCodesPacket{}, snap.GetByteCodesMsg)
  148. }
  149. func FuzzTrieNodes(input []byte) int {
  150. return doFuzz(input, &snap.GetTrieNodesPacket{}, snap.GetTrieNodesMsg)
  151. }