rangeproof-fuzzer.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2020 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 rangeproof
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "io"
  22. "sort"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  26. "github.com/ethereum/go-ethereum/trie"
  27. )
  28. type kv struct {
  29. k, v []byte
  30. t bool
  31. }
  32. type entrySlice []*kv
  33. func (p entrySlice) Len() int { return len(p) }
  34. func (p entrySlice) Less(i, j int) bool { return bytes.Compare(p[i].k, p[j].k) < 0 }
  35. func (p entrySlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  36. type fuzzer struct {
  37. input io.Reader
  38. exhausted bool
  39. }
  40. func (f *fuzzer) randBytes(n int) []byte {
  41. r := make([]byte, n)
  42. if _, err := f.input.Read(r); err != nil {
  43. f.exhausted = true
  44. }
  45. return r
  46. }
  47. func (f *fuzzer) readInt() uint64 {
  48. var x uint64
  49. if err := binary.Read(f.input, binary.LittleEndian, &x); err != nil {
  50. f.exhausted = true
  51. }
  52. return x
  53. }
  54. func (f *fuzzer) randomTrie(n int) (*trie.Trie, map[string]*kv) {
  55. trie := trie.NewEmpty(trie.NewDatabase(rawdb.NewMemoryDatabase()))
  56. vals := make(map[string]*kv)
  57. size := f.readInt()
  58. // Fill it with some fluff
  59. for i := byte(0); i < byte(size); i++ {
  60. value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
  61. value2 := &kv{common.LeftPadBytes([]byte{i + 10}, 32), []byte{i}, false}
  62. trie.Update(value.k, value.v)
  63. trie.Update(value2.k, value2.v)
  64. vals[string(value.k)] = value
  65. vals[string(value2.k)] = value2
  66. }
  67. if f.exhausted {
  68. return nil, nil
  69. }
  70. // And now fill with some random
  71. for i := 0; i < n; i++ {
  72. k := f.randBytes(32)
  73. v := f.randBytes(20)
  74. value := &kv{k, v, false}
  75. trie.Update(k, v)
  76. vals[string(k)] = value
  77. if f.exhausted {
  78. return nil, nil
  79. }
  80. }
  81. return trie, vals
  82. }
  83. func (f *fuzzer) fuzz() int {
  84. maxSize := 200
  85. tr, vals := f.randomTrie(1 + int(f.readInt())%maxSize)
  86. if f.exhausted {
  87. return 0 // input too short
  88. }
  89. var entries entrySlice
  90. for _, kv := range vals {
  91. entries = append(entries, kv)
  92. }
  93. if len(entries) <= 1 {
  94. return 0
  95. }
  96. sort.Sort(entries)
  97. var ok = 0
  98. for {
  99. start := int(f.readInt() % uint64(len(entries)))
  100. end := 1 + int(f.readInt()%uint64(len(entries)-1))
  101. testcase := int(f.readInt() % uint64(6))
  102. index := int(f.readInt() & 0xFFFFFFFF)
  103. index2 := int(f.readInt() & 0xFFFFFFFF)
  104. if f.exhausted {
  105. break
  106. }
  107. proof := memorydb.New()
  108. if err := tr.Prove(entries[start].k, 0, proof); err != nil {
  109. panic(fmt.Sprintf("Failed to prove the first node %v", err))
  110. }
  111. if err := tr.Prove(entries[end-1].k, 0, proof); err != nil {
  112. panic(fmt.Sprintf("Failed to prove the last node %v", err))
  113. }
  114. var keys [][]byte
  115. var vals [][]byte
  116. for i := start; i < end; i++ {
  117. keys = append(keys, entries[i].k)
  118. vals = append(vals, entries[i].v)
  119. }
  120. if len(keys) == 0 {
  121. return 0
  122. }
  123. var first, last = keys[0], keys[len(keys)-1]
  124. testcase %= 6
  125. switch testcase {
  126. case 0:
  127. // Modified key
  128. keys[index%len(keys)] = f.randBytes(32) // In theory it can't be same
  129. case 1:
  130. // Modified val
  131. vals[index%len(vals)] = f.randBytes(20) // In theory it can't be same
  132. case 2:
  133. // Gapped entry slice
  134. index = index % len(keys)
  135. keys = append(keys[:index], keys[index+1:]...)
  136. vals = append(vals[:index], vals[index+1:]...)
  137. case 3:
  138. // Out of order
  139. index1 := index % len(keys)
  140. index2 := index2 % len(keys)
  141. keys[index1], keys[index2] = keys[index2], keys[index1]
  142. vals[index1], vals[index2] = vals[index2], vals[index1]
  143. case 4:
  144. // Set random key to nil, do nothing
  145. keys[index%len(keys)] = nil
  146. case 5:
  147. // Set random value to nil, deletion
  148. vals[index%len(vals)] = nil
  149. // Other cases:
  150. // Modify something in the proof db
  151. // add stuff to proof db
  152. // drop stuff from proof db
  153. }
  154. if f.exhausted {
  155. break
  156. }
  157. ok = 1
  158. //nodes, subtrie
  159. hasMore, err := trie.VerifyRangeProof(tr.Hash(), first, last, keys, vals, proof)
  160. if err != nil {
  161. if hasMore {
  162. panic("err != nil && hasMore == true")
  163. }
  164. }
  165. }
  166. return ok
  167. }
  168. // The function must return
  169. // 1 if the fuzzer should increase priority of the
  170. // given input during subsequent fuzzing (for example, the input is lexically
  171. // correct and was parsed successfully);
  172. // -1 if the input must not be added to corpus even if gives new coverage; and
  173. // 0 otherwise; other values are reserved for future use.
  174. func Fuzz(input []byte) int {
  175. if len(input) < 100 {
  176. return 0
  177. }
  178. r := bytes.NewReader(input)
  179. f := fuzzer{
  180. input: r,
  181. exhausted: false,
  182. }
  183. return f.fuzz()
  184. }