github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/tests/fuzzers/rangeproof/rangeproof-fuzzer.go (about)

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