Skip to content

Commit

Permalink
feat: fuzz on pointer guard
Browse files Browse the repository at this point in the history
  • Loading branch information
IdrisHanafi committed Jun 27, 2023
1 parent 7984e16 commit 582327b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 7 additions & 2 deletions cmd/rpcfuzz/argfuzz/argfuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package argfuzz
import (
"encoding/hex"
"math/rand"
"reflect"
"strconv"

"github.com/google/gofuzz"
Expand Down Expand Up @@ -65,8 +66,12 @@ func MutateRPCArgs(args *[]interface{}, c fuzz.Continue) {
case bool:
(*args)[i] = c.RandBool()
default:
c.Fuzz(d)
(*args)[i] = d
if reflect.TypeOf(d).Kind() == reflect.Ptr {
c.Fuzz(d)
(*args)[i] = d
} else {
(*args)[i] = c.RandString()
}
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/rpcfuzz/rpcfuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var (
testNamespaces *string
testFuzz *bool
testFuzzNum *int
seed *int64
testAccountNonce uint64
testAccountNonceMutex sync.Mutex
currentChainID *big.Int
Expand Down Expand Up @@ -1445,8 +1446,9 @@ func CallRPCWithFuzzAndValidate(ctx context.Context, rpcClient *rpc.Client, curr
NumberOfTestsRan: n,
}

originalArgs := currTest.GetArgs()
for i := 0; i < *testFuzzNum; i++ {
args := currTest.GetArgs()
args := originalArgs
fuzzer.Fuzz(&args)
currTestResult.Args[i] = args

Expand Down Expand Up @@ -1644,18 +1646,19 @@ func shouldRunTest(t RPCTest) bool {
}

func init() {
// TODO: make this flaggable
rand.Seed(time.Now().UnixNano())
zerolog.SetGlobalLevel(zerolog.TraceLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

flagSet := RPCFuzzCmd.PersistentFlags()
fuzzer = fuzz.New()
fuzzer.Funcs(argfuzz.MutateRPCArgs)

testPrivateHexKey = flagSet.String("private-key", codeQualityPrivateKey, "The hex encoded private key that we'll use to sending transactions")
testContractAddress = flagSet.String("contract-address", "0x6fda56c57b0acadb96ed5624ac500c0429d59429", "The address of a contract that can be used for testing")
testNamespaces = flagSet.String("namespaces", "eth,web3,net", "Comma separated list of rpc namespaces to test")
testFuzz = flagSet.Bool("fuzz", false, "Flag to indicate whether to fuzz input or not.")
testFuzzNum = flagSet.Int("fuzzn", 100, "Number of times to run the fuzzer per test.")
seed = flagSet.Int64("seed", 123456, "A seed for generating random values within the fuzzer")

rand.Seed(*seed)
fuzzer = fuzz.New()
fuzzer.Funcs(argfuzz.MutateRPCArgs)
}

0 comments on commit 582327b

Please sign in to comment.