rangeproof-fuzzer.go 5.1 KB

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