trie-fuzzer.go 4.5 KB

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