trie-fuzzer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2019 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 trie
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  23. "github.com/ethereum/go-ethereum/trie"
  24. )
  25. // randTest performs random trie operations.
  26. // Instances of this test are created by Generate.
  27. type randTest []randTestStep
  28. type randTestStep struct {
  29. op int
  30. key []byte // for opUpdate, opDelete, opGet
  31. value []byte // for opUpdate
  32. err error // for debugging
  33. }
  34. type proofDb struct{}
  35. func (proofDb) Put(key []byte, value []byte) error {
  36. return nil
  37. }
  38. func (proofDb) Delete(key []byte) error {
  39. return nil
  40. }
  41. const (
  42. opUpdate = iota
  43. opDelete
  44. opGet
  45. opCommit
  46. opHash
  47. opReset
  48. opItercheckhash
  49. opProve
  50. opMax // boundary value, not an actual op
  51. )
  52. type dataSource struct {
  53. input []byte
  54. reader *bytes.Reader
  55. }
  56. func newDataSource(input []byte) *dataSource {
  57. return &dataSource{
  58. input, bytes.NewReader(input),
  59. }
  60. }
  61. func (ds *dataSource) ReadByte() (byte, error) {
  62. if b, err := ds.reader.ReadByte(); err != nil {
  63. return 0, err
  64. } else {
  65. return b, nil
  66. }
  67. }
  68. func (ds *dataSource) adaptReadByte() byte {
  69. b, _ := ds.ReadByte()
  70. return b
  71. }
  72. func (ds *dataSource) Read(buf []byte) (int, error) {
  73. return ds.reader.Read(buf)
  74. }
  75. func (ds *dataSource) Ended() bool {
  76. return ds.reader.Len() == 0
  77. }
  78. func Generate(input []byte) randTest {
  79. var allKeys [][]byte
  80. r := newDataSource(input)
  81. genKey := func() []byte {
  82. if len(allKeys) < 2 || r.adaptReadByte() < 0x0f {
  83. // new key
  84. key := make([]byte, r.adaptReadByte()%50)
  85. r.Read(key)
  86. allKeys = append(allKeys, key)
  87. return key
  88. }
  89. // use existing key
  90. return allKeys[int(r.adaptReadByte())%len(allKeys)]
  91. }
  92. var steps randTest
  93. for i := 0; !r.Ended(); i++ {
  94. step := randTestStep{op: int(r.adaptReadByte()) % opMax}
  95. switch step.op {
  96. case opUpdate:
  97. step.key = genKey()
  98. step.value = make([]byte, 8)
  99. binary.BigEndian.PutUint64(step.value, uint64(i))
  100. case opGet, opDelete, opProve:
  101. step.key = genKey()
  102. }
  103. steps = append(steps, step)
  104. if len(steps) > 500 {
  105. break
  106. }
  107. }
  108. return steps
  109. }
  110. // The function must return
  111. // 1 if the fuzzer should increase priority of the
  112. // given input during subsequent fuzzing (for example, the input is lexically
  113. // correct and was parsed successfully);
  114. // -1 if the input must not be added to corpus even if gives new coverage; and
  115. // 0 otherwise
  116. // other values are reserved for future use.
  117. func Fuzz(input []byte) int {
  118. program := Generate(input)
  119. if len(program) == 0 {
  120. return 0
  121. }
  122. if err := runRandTest(program); err != nil {
  123. panic(err)
  124. }
  125. return 1
  126. }
  127. func runRandTest(rt randTest) error {
  128. triedb := trie.NewDatabase(memorydb.New())
  129. tr, _ := trie.New(common.Hash{}, triedb)
  130. values := make(map[string]string) // tracks content of the trie
  131. for i, step := range rt {
  132. switch step.op {
  133. case opUpdate:
  134. tr.Update(step.key, step.value)
  135. values[string(step.key)] = string(step.value)
  136. case opDelete:
  137. tr.Delete(step.key)
  138. delete(values, string(step.key))
  139. case opGet:
  140. v := tr.Get(step.key)
  141. want := values[string(step.key)]
  142. if string(v) != want {
  143. rt[i].err = fmt.Errorf("mismatch for key 0x%x, got 0x%x want 0x%x", step.key, v, want)
  144. }
  145. case opCommit:
  146. _, rt[i].err = tr.Commit(nil)
  147. case opHash:
  148. tr.Hash()
  149. case opReset:
  150. hash, err := tr.Commit(nil)
  151. if err != nil {
  152. return err
  153. }
  154. newtr, err := trie.New(hash, triedb)
  155. if err != nil {
  156. return err
  157. }
  158. tr = newtr
  159. case opItercheckhash:
  160. checktr, _ := trie.New(common.Hash{}, triedb)
  161. it := trie.NewIterator(tr.NodeIterator(nil))
  162. for it.Next() {
  163. checktr.Update(it.Key, it.Value)
  164. }
  165. if tr.Hash() != checktr.Hash() {
  166. return fmt.Errorf("hash mismatch in opItercheckhash")
  167. }
  168. case opProve:
  169. rt[i].err = tr.Prove(step.key, 0, proofDb{})
  170. }
  171. // Abort the test on error.
  172. if rt[i].err != nil {
  173. return rt[i].err
  174. }
  175. }
  176. return nil
  177. }