diff --git a/crypto/blake2b/blake2b_f_fuzz.go b/crypto/blake2b/blake2b_f_fuzz.go new file mode 100644 index 000000000000..b2f4057074b7 --- /dev/null +++ b/crypto/blake2b/blake2b_f_fuzz.go @@ -0,0 +1,58 @@ +//go:build gofuzz +// +build gofuzz + +package blake2b + +import ( + "encoding/binary" +) + +func Fuzz(data []byte) int { + // Make sure the data confirms to the input model + if len(data) != 211 { + return 0 + } + // Parse everything and call all the implementations + var ( + rounds = binary.BigEndian.Uint16(data[0:2]) + + h [8]uint64 + m [16]uint64 + t [2]uint64 + f uint64 + ) + for i := 0; i < 8; i++ { + offset := 2 + i*8 + h[i] = binary.LittleEndian.Uint64(data[offset : offset+8]) + } + for i := 0; i < 16; i++ { + offset := 66 + i*8 + m[i] = binary.LittleEndian.Uint64(data[offset : offset+8]) + } + t[0] = binary.LittleEndian.Uint64(data[194:202]) + t[1] = binary.LittleEndian.Uint64(data[202:210]) + + if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1 + f = 0xFFFFFFFFFFFFFFFF + } + // Run the blake2b compression on all instruction sets and cross reference + want := h + fGeneric(&want, &m, t[0], t[1], f, uint64(rounds)) + + have := h + fSSE4(&have, &m, t[0], t[1], f, uint64(rounds)) + if have != want { + panic("SSE4 mismatches generic algo") + } + have = h + fAVX(&have, &m, t[0], t[1], f, uint64(rounds)) + if have != want { + panic("AVX mismatches generic algo") + } + have = h + fAVX2(&have, &m, t[0], t[1], f, uint64(rounds)) + if have != want { + panic("AVX2 mismatches generic algo") + } + return 1 +} diff --git a/crypto/blake2b/blake2b_f_test.go b/crypto/blake2b/blake2b_f_test.go index 983c65a749ac..4e07d131cda6 100644 --- a/crypto/blake2b/blake2b_f_test.go +++ b/crypto/blake2b/blake2b_f_test.go @@ -1,7 +1,6 @@ package blake2b import ( - "encoding/binary" "fmt" "reflect" "testing" @@ -58,60 +57,3 @@ var testVectorsF = []testVector{ }, }, } - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} - -func fuzz(data []byte) { - // Make sure the data confirms to the input model - if len(data) != 211 { - return - } - // Parse everything and call all the implementations - var ( - rounds = binary.BigEndian.Uint16(data[0:2]) - - h [8]uint64 - m [16]uint64 - t [2]uint64 - f uint64 - ) - - for i := 0; i < 8; i++ { - offset := 2 + i*8 - h[i] = binary.LittleEndian.Uint64(data[offset : offset+8]) - } - for i := 0; i < 16; i++ { - offset := 66 + i*8 - m[i] = binary.LittleEndian.Uint64(data[offset : offset+8]) - } - t[0] = binary.LittleEndian.Uint64(data[194:202]) - t[1] = binary.LittleEndian.Uint64(data[202:210]) - - if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1 - f = 0xFFFFFFFFFFFFFFFF - } - - // Run the blake2b compression on all instruction sets and cross reference - want := h - fGeneric(&want, &m, t[0], t[1], f, uint64(rounds)) - - have := h - fSSE4(&have, &m, t[0], t[1], f, uint64(rounds)) - if have != want { - panic("SSE4 mismatches generic algo") - } - have = h - fAVX(&have, &m, t[0], t[1], f, uint64(rounds)) - if have != want { - panic("AVX mismatches generic algo") - } - have = h - fAVX2(&have, &m, t[0], t[1], f, uint64(rounds)) - if have != want { - panic("AVX2 mismatches generic algo") - } -} diff --git a/oss-fuzz.sh b/oss-fuzz.sh index 55660d08e3c3..745a5ba7c7c0 100644 --- a/oss-fuzz.sh +++ b/oss-fuzz.sh @@ -1,5 +1,5 @@ -#!/bin/bash -eu -# Copyright 2022 Google LLC +#/bin/bash -eu +# Copyright 2020 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,6 +15,17 @@ # ################################################################################ +# This file is for integration with Google OSS-Fuzz. +# The following ENV variables are available when executing on OSS-fuzz: +# +# /out/ $OUT Directory to store build artifacts (fuzz targets, dictionaries, options files, seed corpus archives). +# /src/ $SRC Directory to checkout source files. +# /work/ $WORK Directory to store intermediate files. +# +# $CC, $CXX, $CCC The C and C++ compiler binaries. +# $CFLAGS, $CXXFLAGS C and C++ compiler flags. +# $LIB_FUZZING_ENGINE C++ compiler argument to link fuzz target against the prebuilt engine library (e.g. libFuzzer). + # This sets the -coverpgk for the coverage report when the corpus is executed through go test coverpkg="github.com/ethereum/go-ethereum/..." @@ -48,38 +59,25 @@ DOG cd - } -function build_native_go_fuzzer() { - fuzzer=$1 - function=$2 - path=$3 - tags="-tags gofuzz" - - if [[ $SANITIZER == *coverage* ]]; then - coverbuild $path $function $fuzzer $coverpkg - else - go-118-fuzz-build $tags -o $fuzzer.a -func $function $path - $CXX $CXXFLAGS $LIB_FUZZING_ENGINE $fuzzer.a -o $OUT/$fuzzer - fi -} +function compile_fuzzer { + # Inputs: + # $1: The package to fuzz, within go-ethereum + # $2: The name of the fuzzing function + # $3: The name to give to the final fuzzing-binary -function compile_fuzzer() { path=$GOPATH/src/github.com/ethereum/go-ethereum/$1 - function=$2 + func=$2 fuzzer=$3 echo "Building $fuzzer" - cd $path - - # Install build dependencies - go install github.com/AdamKorcz/go-118-fuzz-build@latest - go get github.com/AdamKorcz/go-118-fuzz-build/testing - # Test if file contains a line with "func $function(" and "testing.F". - if [ $(grep -r "func $function(" $path | grep "testing.F" | wc -l) -eq 1 ] - then - build_native_go_fuzzer $fuzzer $function $path + # Do a coverage-build or a regular build + if [[ $SANITIZER = *coverage* ]]; then + coverbuild $path $func $fuzzer $coverpkg else - echo "Could not find the function: func ${function}(f *testing.F)" + (cd $path && \ + go-fuzz -func $func -o $WORK/$fuzzer.a . && \ + $CXX $CXXFLAGS $LIB_FUZZING_ENGINE $WORK/$fuzzer.a -o $OUT/$fuzzer) fi ## Check if there exists a seed corpus file @@ -89,11 +87,9 @@ function compile_fuzzer() { cp $corpusfile $OUT/ echo "Found seed corpus: $corpusfile" fi - cd - } -compile_fuzzer tests/fuzzers/bitutil FuzzEncoder fuzzBitutilEncoder -compile_fuzzer tests/fuzzers/bitutil FuzzDecoder fuzzBitutilDecoder +compile_fuzzer tests/fuzzers/bitutil Fuzz fuzzBitutilCompress compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair diff --git a/tests/fuzzers/abi/abifuzzer.go b/tests/fuzzers/abi/abifuzzer.go new file mode 100644 index 000000000000..60233d158a86 --- /dev/null +++ b/tests/fuzzers/abi/abifuzzer.go @@ -0,0 +1,170 @@ +// Copyright 2020 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package abi + +import ( + "fmt" + "reflect" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" + fuzz "github.com/google/gofuzz" +) + +var ( + names = []string{"_name", "name", "NAME", "name_", "__", "_name_", "n"} + stateMut = []string{"", "pure", "view", "payable"} + stateMutabilites = []*string{&stateMut[0], &stateMut[1], &stateMut[2], &stateMut[3]} + pays = []string{"", "true", "false"} + payables = []*string{&pays[0], &pays[1]} + vNames = []string{"a", "b", "c", "d", "e", "f", "g"} + varNames = append(vNames, names...) + varTypes = []string{"bool", "address", "bytes", "string", + "uint8", "int8", "uint8", "int8", "uint16", "int16", + "uint24", "int24", "uint32", "int32", "uint40", "int40", "uint48", "int48", "uint56", "int56", + "uint64", "int64", "uint72", "int72", "uint80", "int80", "uint88", "int88", "uint96", "int96", + "uint104", "int104", "uint112", "int112", "uint120", "int120", "uint128", "int128", "uint136", "int136", + "uint144", "int144", "uint152", "int152", "uint160", "int160", "uint168", "int168", "uint176", "int176", + "uint184", "int184", "uint192", "int192", "uint200", "int200", "uint208", "int208", "uint216", "int216", + "uint224", "int224", "uint232", "int232", "uint240", "int240", "uint248", "int248", "uint256", "int256", + "bytes1", "bytes2", "bytes3", "bytes4", "bytes5", "bytes6", "bytes7", "bytes8", "bytes9", "bytes10", "bytes11", + "bytes12", "bytes13", "bytes14", "bytes15", "bytes16", "bytes17", "bytes18", "bytes19", "bytes20", "bytes21", + "bytes22", "bytes23", "bytes24", "bytes25", "bytes26", "bytes27", "bytes28", "bytes29", "bytes30", "bytes31", + "bytes32", "bytes"} +) + +func unpackPack(abi abi.ABI, method string, input []byte) ([]interface{}, bool) { + if out, err := abi.Unpack(method, input); err == nil { + _, err := abi.Pack(method, out...) + if err != nil { + // We have some false positives as we can unpack these type successfully, but not pack them + if err.Error() == "abi: cannot use []uint8 as type [0]int8 as argument" || + err.Error() == "abi: cannot use uint8 as type int8 as argument" { + return out, false + } + panic(err) + } + return out, true + } + return nil, false +} + +func packUnpack(abi abi.ABI, method string, input *[]interface{}) bool { + if packed, err := abi.Pack(method, input); err == nil { + outptr := reflect.New(reflect.TypeOf(input)) + err := abi.UnpackIntoInterface(outptr.Interface(), method, packed) + if err != nil { + panic(err) + } + out := outptr.Elem().Interface() + if !reflect.DeepEqual(input, out) { + panic(fmt.Sprintf("unpackPack is not equal, \ninput : %x\noutput: %x", input, out)) + } + return true + } + return false +} + +type args struct { + name string + typ string +} + +func createABI(name string, stateMutability, payable *string, inputs []args) (abi.ABI, error) { + sig := fmt.Sprintf(`[{ "type" : "function", "name" : "%v" `, name) + if stateMutability != nil { + sig += fmt.Sprintf(`, "stateMutability": "%v" `, *stateMutability) + } + if payable != nil { + sig += fmt.Sprintf(`, "payable": %v `, *payable) + } + if len(inputs) > 0 { + sig += `, "inputs" : [ {` + for i, inp := range inputs { + sig += fmt.Sprintf(`"name" : "%v", "type" : "%v" `, inp.name, inp.typ) + if i+1 < len(inputs) { + sig += "," + } + } + sig += "} ]" + sig += `, "outputs" : [ {` + for i, inp := range inputs { + sig += fmt.Sprintf(`"name" : "%v", "type" : "%v" `, inp.name, inp.typ) + if i+1 < len(inputs) { + sig += "," + } + } + sig += "} ]" + } + sig += `}]` + + return abi.JSON(strings.NewReader(sig)) +} + +func runFuzzer(input []byte) int { + good := false + fuzzer := fuzz.NewFromGoFuzz(input) + + name := names[getUInt(fuzzer)%len(names)] + stateM := stateMutabilites[getUInt(fuzzer)%len(stateMutabilites)] + payable := payables[getUInt(fuzzer)%len(payables)] + maxLen := 5 + for k := 1; k < maxLen; k++ { + var arg []args + for i := k; i > 0; i-- { + argName := varNames[i] + argTyp := varTypes[getUInt(fuzzer)%len(varTypes)] + if getUInt(fuzzer)%10 == 0 { + argTyp += "[]" + } else if getUInt(fuzzer)%10 == 0 { + arrayArgs := getUInt(fuzzer)%30 + 1 + argTyp += fmt.Sprintf("[%d]", arrayArgs) + } + arg = append(arg, args{ + name: argName, + typ: argTyp, + }) + } + abi, err := createABI(name, stateM, payable, arg) + if err != nil { + continue + } + structs, b := unpackPack(abi, name, input) + c := packUnpack(abi, name, &structs) + good = good || b || c + } + if good { + return 1 + } + return 0 +} + +func Fuzz(input []byte) int { + return runFuzzer(input) +} + +func getUInt(fuzzer *fuzz.Fuzzer) int { + var i int + fuzzer.Fuzz(&i) + if i < 0 { + i = -i + if i < 0 { + return 0 + } + } + return i +} diff --git a/tests/fuzzers/abi/abifuzzer_test.go b/tests/fuzzers/abi/abifuzzer_test.go index a206beaf919d..c66399e1b784 100644 --- a/tests/fuzzers/abi/abifuzzer_test.go +++ b/tests/fuzzers/abi/abifuzzer_test.go @@ -17,13 +17,7 @@ package abi import ( - "fmt" - "reflect" - "strings" "testing" - - "github.com/ethereum/go-ethereum/accounts/abi" - fuzz "github.com/google/gofuzz" ) // TestReplicate can be used to replicate crashers from the fuzzing tests. @@ -31,151 +25,19 @@ import ( func TestReplicate(t *testing.T) { testString := "\x20\x20\x20\x20\x20\x20\x20\x20\x80\x00\x00\x00\x20\x20\x20\x20\x00" data := []byte(testString) - fuzzAbi(data) -} - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzAbi(data) - }) -} - -var ( - names = []string{"_name", "name", "NAME", "name_", "__", "_name_", "n"} - stateMut = []string{"", "pure", "view", "payable"} - stateMutabilites = []*string{&stateMut[0], &stateMut[1], &stateMut[2], &stateMut[3]} - pays = []string{"", "true", "false"} - payables = []*string{&pays[0], &pays[1]} - vNames = []string{"a", "b", "c", "d", "e", "f", "g"} - varNames = append(vNames, names...) - varTypes = []string{"bool", "address", "bytes", "string", - "uint8", "int8", "uint8", "int8", "uint16", "int16", - "uint24", "int24", "uint32", "int32", "uint40", "int40", "uint48", "int48", "uint56", "int56", - "uint64", "int64", "uint72", "int72", "uint80", "int80", "uint88", "int88", "uint96", "int96", - "uint104", "int104", "uint112", "int112", "uint120", "int120", "uint128", "int128", "uint136", "int136", - "uint144", "int144", "uint152", "int152", "uint160", "int160", "uint168", "int168", "uint176", "int176", - "uint184", "int184", "uint192", "int192", "uint200", "int200", "uint208", "int208", "uint216", "int216", - "uint224", "int224", "uint232", "int232", "uint240", "int240", "uint248", "int248", "uint256", "int256", - "bytes1", "bytes2", "bytes3", "bytes4", "bytes5", "bytes6", "bytes7", "bytes8", "bytes9", "bytes10", "bytes11", - "bytes12", "bytes13", "bytes14", "bytes15", "bytes16", "bytes17", "bytes18", "bytes19", "bytes20", "bytes21", - "bytes22", "bytes23", "bytes24", "bytes25", "bytes26", "bytes27", "bytes28", "bytes29", "bytes30", "bytes31", - "bytes32", "bytes"} -) - -func unpackPack(abi abi.ABI, method string, input []byte) ([]interface{}, bool) { - if out, err := abi.Unpack(method, input); err == nil { - _, err := abi.Pack(method, out...) - if err != nil { - // We have some false positives as we can unpack these type successfully, but not pack them - if err.Error() == "abi: cannot use []uint8 as type [0]int8 as argument" || - err.Error() == "abi: cannot use uint8 as type int8 as argument" { - return out, false - } + runFuzzer(data) +} + +// TestGenerateCorpus can be used to add corpus for the fuzzer. +// Just replace corpusHex with the hexEncoded output you want to add to the fuzzer. +func TestGenerateCorpus(t *testing.T) { + /* + corpusHex := "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + data := common.FromHex(corpusHex) + checksum := sha1.Sum(data) + outf := fmt.Sprintf("corpus/%x", checksum) + if err := os.WriteFile(outf, data, 0777); err != nil { panic(err) } - return out, true - } - return nil, false -} - -func packUnpack(abi abi.ABI, method string, input *[]interface{}) bool { - if packed, err := abi.Pack(method, input); err == nil { - outptr := reflect.New(reflect.TypeOf(input)) - err := abi.UnpackIntoInterface(outptr.Interface(), method, packed) - if err != nil { - panic(err) - } - out := outptr.Elem().Interface() - if !reflect.DeepEqual(input, out) { - panic(fmt.Sprintf("unpackPack is not equal, \ninput : %x\noutput: %x", input, out)) - } - return true - } - return false -} - -type args struct { - name string - typ string -} - -func createABI(name string, stateMutability, payable *string, inputs []args) (abi.ABI, error) { - sig := fmt.Sprintf(`[{ "type" : "function", "name" : "%v" `, name) - if stateMutability != nil { - sig += fmt.Sprintf(`, "stateMutability": "%v" `, *stateMutability) - } - if payable != nil { - sig += fmt.Sprintf(`, "payable": %v `, *payable) - } - if len(inputs) > 0 { - sig += `, "inputs" : [ {` - for i, inp := range inputs { - sig += fmt.Sprintf(`"name" : "%v", "type" : "%v" `, inp.name, inp.typ) - if i+1 < len(inputs) { - sig += "," - } - } - sig += "} ]" - sig += `, "outputs" : [ {` - for i, inp := range inputs { - sig += fmt.Sprintf(`"name" : "%v", "type" : "%v" `, inp.name, inp.typ) - if i+1 < len(inputs) { - sig += "," - } - } - sig += "} ]" - } - sig += `}]` - - return abi.JSON(strings.NewReader(sig)) -} - -func fuzzAbi(input []byte) int { - good := false - fuzzer := fuzz.NewFromGoFuzz(input) - - name := names[getUInt(fuzzer)%len(names)] - stateM := stateMutabilites[getUInt(fuzzer)%len(stateMutabilites)] - payable := payables[getUInt(fuzzer)%len(payables)] - maxLen := 5 - for k := 1; k < maxLen; k++ { - var arg []args - for i := k; i > 0; i-- { - argName := varNames[i] - argTyp := varTypes[getUInt(fuzzer)%len(varTypes)] - if getUInt(fuzzer)%10 == 0 { - argTyp += "[]" - } else if getUInt(fuzzer)%10 == 0 { - arrayArgs := getUInt(fuzzer)%30 + 1 - argTyp += fmt.Sprintf("[%d]", arrayArgs) - } - arg = append(arg, args{ - name: argName, - typ: argTyp, - }) - } - abi, err := createABI(name, stateM, payable, arg) - if err != nil { - continue - } - structs, b := unpackPack(abi, name, input) - c := packUnpack(abi, name, &structs) - good = good || b || c - } - if good { - return 1 - } - return 0 -} - -func getUInt(fuzzer *fuzz.Fuzzer) int { - var i int - fuzzer.Fuzz(&i) - if i < 0 { - i = -i - if i < 0 { - return 0 - } - } - return i + */ } diff --git a/tests/fuzzers/bitutil/compress_test.go b/tests/fuzzers/bitutil/compress_fuzz.go similarity index 82% rename from tests/fuzzers/bitutil/compress_test.go rename to tests/fuzzers/bitutil/compress_fuzz.go index ed9d27eb3074..5903cf2f932c 100644 --- a/tests/fuzzers/bitutil/compress_test.go +++ b/tests/fuzzers/bitutil/compress_fuzz.go @@ -1,4 +1,4 @@ -// Copyright 2023 The go-ethereum Authors +// Copyright 2017 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -18,37 +18,38 @@ package bitutil import ( "bytes" - "testing" "github.com/ethereum/go-ethereum/common/bitutil" ) -func FuzzEncoder(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzEncode(data) - }) -} -func FuzzDecoder(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzDecode(data) - }) +// Fuzz implements a go-fuzz fuzzer method to test various encoding method +// invocations. +func Fuzz(data []byte) int { + if len(data) == 0 { + return 0 + } + if data[0]%2 == 0 { + return fuzzEncode(data[1:]) + } + return fuzzDecode(data[1:]) } // fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and // decoding algorithm. -func fuzzEncode(data []byte) { +func fuzzEncode(data []byte) int { proc, _ := bitutil.DecompressBytes(bitutil.CompressBytes(data), len(data)) if !bytes.Equal(data, proc) { panic("content mismatch") } + return 1 } // fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and // reencoding algorithm. -func fuzzDecode(data []byte) { +func fuzzDecode(data []byte) int { blob, err := bitutil.DecompressBytes(data, 1024) if err != nil { - return + return 0 } // re-compress it (it's OK if the re-compressed differs from the // original - the first input may not have been compressed at all) @@ -65,4 +66,5 @@ func fuzzDecode(data []byte) { if !bytes.Equal(decomp, blob) { panic("content mismatch") } + return 1 } diff --git a/tests/fuzzers/bls12381/bls12381_fuzz.go b/tests/fuzzers/bls12381/bls12381_fuzz.go index f04524f76a9f..ced87dd41a8e 100644 --- a/tests/fuzzers/bls12381/bls12381_fuzz.go +++ b/tests/fuzzers/bls12381/bls12381_fuzz.go @@ -14,6 +14,9 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . +//go:build gofuzz +// +build gofuzz + package bls import ( @@ -32,7 +35,7 @@ import ( blst "github.com/supranational/blst/bindings/go" ) -func fuzzCrossPairing(data []byte) int { +func FuzzCrossPairing(data []byte) int { input := bytes.NewReader(data) // get random G1 points @@ -98,7 +101,7 @@ func massageBLST(in []byte) []byte { return out } -func fuzzCrossG1Add(data []byte) int { +func FuzzCrossG1Add(data []byte) int { input := bytes.NewReader(data) // get random G1 points @@ -136,7 +139,7 @@ func fuzzCrossG1Add(data []byte) int { return 1 } -func fuzzCrossG2Add(data []byte) int { +func FuzzCrossG2Add(data []byte) int { input := bytes.NewReader(data) // get random G2 points @@ -174,7 +177,7 @@ func fuzzCrossG2Add(data []byte) int { return 1 } -func fuzzCrossG1MultiExp(data []byte) int { +func FuzzCrossG1MultiExp(data []byte) int { var ( input = bytes.NewReader(data) gethScalars []*big.Int diff --git a/tests/fuzzers/bls12381/bls12381_test.go b/tests/fuzzers/bls12381/bls12381_test.go deleted file mode 100644 index 59e4db31d5d7..000000000000 --- a/tests/fuzzers/bls12381/bls12381_test.go +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package bls - -import "testing" - -func FuzzCrossPairing(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzCrossPairing(data) - }) -} - -func FuzzCrossG1Add(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzCrossG1Add(data) - }) -} - -func FuzzCrossG2Add(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzCrossG2Add(data) - }) -} - -func FuzzCrossG1MultiExp(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzCrossG1MultiExp(data) - }) -} - -func FuzzG1Add(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsG1Add, data) - }) -} - -func FuzzG1Mul(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsG1Mul, data) - }) -} - -func FuzzG1MultiExp(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsG1MultiExp, data) - }) -} - -func FuzzG2Add(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsG2Add, data) - }) -} - -func FuzzG2Mul(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsG2Mul, data) - }) -} - -func FuzzG2MultiExp(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsG2MultiExp, data) - }) -} - -func FuzzPairing(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsPairing, data) - }) -} - -func FuzzMapG1(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsMapG1, data) - }) -} - -func FuzzMapG2(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(blsMapG2, data) - }) -} diff --git a/tests/fuzzers/bls12381/precompile_fuzzer.go b/tests/fuzzers/bls12381/precompile_fuzzer.go index 763ed56e9f7a..cab2bcba3863 100644 --- a/tests/fuzzers/bls12381/precompile_fuzzer.go +++ b/tests/fuzzers/bls12381/precompile_fuzzer.go @@ -36,6 +36,16 @@ const ( blsMapG2 = byte(18) ) +func FuzzG1Add(data []byte) int { return fuzz(blsG1Add, data) } +func FuzzG1Mul(data []byte) int { return fuzz(blsG1Mul, data) } +func FuzzG1MultiExp(data []byte) int { return fuzz(blsG1MultiExp, data) } +func FuzzG2Add(data []byte) int { return fuzz(blsG2Add, data) } +func FuzzG2Mul(data []byte) int { return fuzz(blsG2Mul, data) } +func FuzzG2MultiExp(data []byte) int { return fuzz(blsG2MultiExp, data) } +func FuzzPairing(data []byte) int { return fuzz(blsPairing, data) } +func FuzzMapG1(data []byte) int { return fuzz(blsMapG1, data) } +func FuzzMapG2(data []byte) int { return fuzz(blsMapG2, data) } + func checkInput(id byte, inputLen int) bool { switch id { case blsG1Add: diff --git a/tests/fuzzers/bn256/bn256_fuzz.go b/tests/fuzzers/bn256/bn256_fuzz.go index 75f7d59deeef..abf1b88615fe 100644 --- a/tests/fuzzers/bn256/bn256_fuzz.go +++ b/tests/fuzzers/bn256/bn256_fuzz.go @@ -14,6 +14,9 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . +//go:build gofuzz +// +build gofuzz + package bn256 import ( @@ -61,8 +64,8 @@ func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2, *bn254.G2Affine) return xc, xg, xs } -// fuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries. -func fuzzAdd(data []byte) int { +// FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries. +func FuzzAdd(data []byte) int { input := bytes.NewReader(data) xc, xg, xs := getG1Points(input) if xc == nil { @@ -94,9 +97,9 @@ func fuzzAdd(data []byte) int { return 1 } -// fuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare +// FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare // libraries. -func fuzzMul(data []byte) int { +func FuzzMul(data []byte) int { input := bytes.NewReader(data) pc, pg, ps := getG1Points(input) if pc == nil { @@ -136,7 +139,7 @@ func fuzzMul(data []byte) int { return 1 } -func fuzzPair(data []byte) int { +func FuzzPair(data []byte) int { input := bytes.NewReader(data) pc, pg, ps := getG1Points(input) if pc == nil { diff --git a/tests/fuzzers/difficulty/difficulty_test.go b/tests/fuzzers/difficulty/debug/main.go similarity index 63% rename from tests/fuzzers/difficulty/difficulty_test.go rename to tests/fuzzers/difficulty/debug/main.go index 49beedb486db..70cf092568e0 100644 --- a/tests/fuzzers/difficulty/difficulty_test.go +++ b/tests/fuzzers/difficulty/debug/main.go @@ -1,4 +1,4 @@ -// Copyright 2023 The go-ethereum Authors +// Copyright 2020 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -14,12 +14,25 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package difficulty +package main -import "testing" +import ( + "fmt" + "os" -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) + "github.com/ethereum/go-ethereum/tests/fuzzers/difficulty" +) + +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "Usage: debug ") + os.Exit(1) + } + crasher := os.Args[1] + data, err := os.ReadFile(crasher) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) + os.Exit(1) + } + difficulty.Fuzz(data) } diff --git a/tests/fuzzers/difficulty/difficulty-fuzz.go b/tests/fuzzers/difficulty/difficulty-fuzz.go index fbbd7f6876ba..e8753bb62349 100644 --- a/tests/fuzzers/difficulty/difficulty-fuzz.go +++ b/tests/fuzzers/difficulty/difficulty-fuzz.go @@ -75,7 +75,7 @@ func (f *fuzzer) readBool() bool { // - 0 otherwise // // other values are reserved for future use. -func fuzz(data []byte) int { +func Fuzz(data []byte) int { f := fuzzer{ input: bytes.NewReader(data), exhausted: false, diff --git a/tests/fuzzers/keystore/corpus/0176eaf52ed014ec5c91cf4afa070dd3fd469077-1 b/tests/fuzzers/keystore/corpus/0176eaf52ed014ec5c91cf4afa070dd3fd469077-1 new file mode 100644 index 000000000000..1c0ecf5250ba --- /dev/null +++ b/tests/fuzzers/keystore/corpus/0176eaf52ed014ec5c91cf4afa070dd3fd469077-1 @@ -0,0 +1 @@ +ns©›,²Ô \ No newline at end of file diff --git a/tests/fuzzers/keystore/keystore-fuzzer.go b/tests/fuzzers/keystore/keystore-fuzzer.go index 07a85d77b55d..e3bcae92e159 100644 --- a/tests/fuzzers/keystore/keystore-fuzzer.go +++ b/tests/fuzzers/keystore/keystore-fuzzer.go @@ -22,7 +22,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" ) -func fuzz(input []byte) int { +func Fuzz(input []byte) int { ks := keystore.NewKeyStore("/tmp/ks", keystore.LightScryptN, keystore.LightScryptP) a, err := ks.NewAccount(string(input)) diff --git a/tests/fuzzers/les/debug/main.go b/tests/fuzzers/les/debug/main.go new file mode 100644 index 000000000000..77a612703002 --- /dev/null +++ b/tests/fuzzers/les/debug/main.go @@ -0,0 +1,40 @@ +// Copyright 2020 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package main + +import ( + "fmt" + "os" + + "github.com/ethereum/go-ethereum/tests/fuzzers/les" +) + +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "Usage: debug \n") + fmt.Fprintf(os.Stderr, "Example\n") + fmt.Fprintf(os.Stderr, " $ debug ../crashers/4bbef6857c733a87ecf6fd8b9e7238f65eb9862a\n") + os.Exit(1) + } + crasher := os.Args[1] + data, err := os.ReadFile(crasher) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) + os.Exit(1) + } + les.Fuzz(data) +} diff --git a/tests/fuzzers/les/les-fuzzer.go b/tests/fuzzers/les/les-fuzzer.go index 209dda0bb915..240a0d9412ea 100644 --- a/tests/fuzzers/les/les-fuzzer.go +++ b/tests/fuzzers/les/les-fuzzer.go @@ -279,7 +279,7 @@ func (f *fuzzer) doFuzz(msgCode uint64, packet interface{}) { fn(f, peer, func() bool { return true }) } -func fuzz(input []byte) int { +func Fuzz(input []byte) int { // We expect some large inputs if len(input) < 100 { return -1 diff --git a/tests/fuzzers/les/les_test.go b/tests/fuzzers/les/les_test.go deleted file mode 100644 index 53af45ceb48d..000000000000 --- a/tests/fuzzers/les/les_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package les - -import "testing" - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} diff --git a/tests/fuzzers/rangeproof/debug/main.go b/tests/fuzzers/rangeproof/debug/main.go new file mode 100644 index 000000000000..d4cab8ec460a --- /dev/null +++ b/tests/fuzzers/rangeproof/debug/main.go @@ -0,0 +1,40 @@ +// Copyright 2020 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package main + +import ( + "fmt" + "os" + + "github.com/ethereum/go-ethereum/tests/fuzzers/rangeproof" +) + +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "Usage: debug \n") + fmt.Fprintf(os.Stderr, "Example\n") + fmt.Fprintf(os.Stderr, " $ debug ../crashers/4bbef6857c733a87ecf6fd8b9e7238f65eb9862a\n") + os.Exit(1) + } + crasher := os.Args[1] + data, err := os.ReadFile(crasher) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) + os.Exit(1) + } + rangeproof.Fuzz(data) +} diff --git a/tests/fuzzers/rangeproof/rangeproof-fuzzer.go b/tests/fuzzers/rangeproof/rangeproof-fuzzer.go index 6b5ca9088064..c9d781553673 100644 --- a/tests/fuzzers/rangeproof/rangeproof-fuzzer.go +++ b/tests/fuzzers/rangeproof/rangeproof-fuzzer.go @@ -185,7 +185,7 @@ func (f *fuzzer) fuzz() int { // - 0 otherwise // // other values are reserved for future use. -func fuzz(input []byte) int { +func Fuzz(input []byte) int { if len(input) < 100 { return 0 } diff --git a/tests/fuzzers/rangeproof/rangeproof_test.go b/tests/fuzzers/rangeproof/rangeproof_test.go deleted file mode 100644 index bc7badc5b3cb..000000000000 --- a/tests/fuzzers/rangeproof/rangeproof_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package rangeproof - -import "testing" - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} diff --git a/tests/fuzzers/rlp/rlp_fuzzer.go b/tests/fuzzers/rlp/rlp_fuzzer.go index 0da8ccdd7766..9fcdb5776930 100644 --- a/tests/fuzzers/rlp/rlp_fuzzer.go +++ b/tests/fuzzers/rlp/rlp_fuzzer.go @@ -38,7 +38,7 @@ func decodeEncode(input []byte, val interface{}, i int) { } } -func fuzz(input []byte) int { +func Fuzz(input []byte) int { if len(input) == 0 { return 0 } diff --git a/tests/fuzzers/rlp/rlp_test.go b/tests/fuzzers/rlp/rlp_test.go deleted file mode 100644 index 377b3961bf14..000000000000 --- a/tests/fuzzers/rlp/rlp_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package rlp - -import "testing" - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} diff --git a/tests/fuzzers/runtime/runtime_test.go b/tests/fuzzers/runtime/runtime_fuzz.go similarity index 67% rename from tests/fuzzers/runtime/runtime_test.go rename to tests/fuzzers/runtime/runtime_fuzz.go index 2d73a56ca172..b30e9243d89d 100644 --- a/tests/fuzzers/runtime/runtime_test.go +++ b/tests/fuzzers/runtime/runtime_fuzz.go @@ -1,4 +1,4 @@ -// Copyright 2023 The go-ethereum Authors +// Copyright 2017 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -17,15 +17,20 @@ package runtime import ( - "testing" - "github.com/ethereum/go-ethereum/core/vm/runtime" ) -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, code, input []byte) { - runtime.Execute(code, input, &runtime.Config{ - GasLimit: 12000000, - }) +// Fuzz is the basic entry point for the go-fuzz tool +// +// This returns 1 for valid parse:able/runnable code, 0 +// for invalid opcode. +func Fuzz(input []byte) int { + _, _, err := runtime.Execute(input, input, &runtime.Config{ + GasLimit: 12000000, }) + // invalid opcode + if err != nil && len(err.Error()) > 6 && err.Error()[:7] == "invalid" { + return 0 + } + return 1 } diff --git a/tests/fuzzers/secp256k1/secp_fuzzer.go b/tests/fuzzers/secp256k1/secp_fuzzer.go new file mode 100644 index 000000000000..47083d5fe3a9 --- /dev/null +++ b/tests/fuzzers/secp256k1/secp_fuzzer.go @@ -0,0 +1,50 @@ +// Copyright 2021 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +// build +gofuzz + +package secp256k1 + +import ( + "fmt" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/ethereum/go-ethereum/crypto/secp256k1" + fuzz "github.com/google/gofuzz" +) + +func Fuzz(input []byte) int { + var ( + fuzzer = fuzz.NewFromGoFuzz(input) + curveA = secp256k1.S256() + curveB = btcec.S256() + dataP1 []byte + dataP2 []byte + ) + // first point + fuzzer.Fuzz(&dataP1) + x1, y1 := curveB.ScalarBaseMult(dataP1) + // second point + fuzzer.Fuzz(&dataP2) + x2, y2 := curveB.ScalarBaseMult(dataP2) + resAX, resAY := curveA.Add(x1, y1, x2, y2) + resBX, resBY := curveB.Add(x1, y1, x2, y2) + if resAX.Cmp(resBX) != 0 || resAY.Cmp(resBY) != 0 { + fmt.Printf("%s %s %s %s\n", x1, y1, x2, y2) + panic(fmt.Sprintf("Addition failed: geth: %s %s btcd: %s %s", resAX, resAY, resBX, resBY)) + } + return 0 +} diff --git a/tests/fuzzers/secp256k1/secp_test.go b/tests/fuzzers/secp256k1/secp_test.go index fbdd8e6ac24c..0ca16cb9bfcf 100644 --- a/tests/fuzzers/secp256k1/secp_test.go +++ b/tests/fuzzers/secp256k1/secp_test.go @@ -16,39 +16,9 @@ package secp256k1 -import ( - "fmt" - "testing" - - "github.com/btcsuite/btcd/btcec/v2" - "github.com/ethereum/go-ethereum/crypto/secp256k1" -) +import "testing" func TestFuzzer(t *testing.T) { - a, b := "00000000N0000000/R0000000000000000", "0U0000S0000000mkhP000000000000000U" - fuzz([]byte(a), []byte(b)) -} - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, a, b []byte) { - fuzz(a, b) - }) -} - -func fuzz(dataP1, dataP2 []byte) int { - var ( - curveA = secp256k1.S256() - curveB = btcec.S256() - ) - // first point - x1, y1 := curveB.ScalarBaseMult(dataP1) - // second points - x2, y2 := curveB.ScalarBaseMult(dataP2) - resAX, resAY := curveA.Add(x1, y1, x2, y2) - resBX, resBY := curveB.Add(x1, y1, x2, y2) - if resAX.Cmp(resBX) != 0 || resAY.Cmp(resBY) != 0 { - fmt.Printf("%s %s %s %s\n", x1, y1, x2, y2) - panic(fmt.Sprintf("Addition failed: geth: %s %s btcd: %s %s", resAX, resAY, resBX, resBY)) - } - return 0 + test := "00000000N0000000/R00000000000000000U0000S0000000mkhP000000000000000U" + Fuzz([]byte(test)) } diff --git a/tests/fuzzers/bn256/bn256_test.go b/tests/fuzzers/snap/debug/main.go similarity index 64% rename from tests/fuzzers/bn256/bn256_test.go rename to tests/fuzzers/snap/debug/main.go index 8b2f962284cf..df46bb1e22b9 100644 --- a/tests/fuzzers/bn256/bn256_test.go +++ b/tests/fuzzers/snap/debug/main.go @@ -1,4 +1,4 @@ -// Copyright 2023 The go-ethereum Authors +// Copyright 2020 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -14,24 +14,25 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package bn256 +package main -import "testing" +import ( + "fmt" + "os" -func FuzzAdd(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzAdd(data) - }) -} - -func FuzzMul(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzMul(data) - }) -} + "github.com/ethereum/go-ethereum/tests/fuzzers/snap" +) -func FuzzPair(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzPair(data) - }) +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "Usage: debug \n") + os.Exit(1) + } + crasher := os.Args[1] + data, err := os.ReadFile(crasher) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) + os.Exit(1) + } + snap.FuzzTrieNodes(data) } diff --git a/tests/fuzzers/snap/fuzz_handler.go b/tests/fuzzers/snap/fuzz_handler.go index 20521bb92a92..784b526dc05f 100644 --- a/tests/fuzzers/snap/fuzz_handler.go +++ b/tests/fuzzers/snap/fuzz_handler.go @@ -141,3 +141,20 @@ func doFuzz(input []byte, obj interface{}, code int) int { } return 1 } + +// To run a fuzzer, do +// $ CGO_ENABLED=0 go-fuzz-build -func FuzzTrieNodes +// $ go-fuzz + +func FuzzARange(input []byte) int { + return doFuzz(input, &snap.GetAccountRangePacket{}, snap.GetAccountRangeMsg) +} +func FuzzSRange(input []byte) int { + return doFuzz(input, &snap.GetStorageRangesPacket{}, snap.GetStorageRangesMsg) +} +func FuzzByteCodes(input []byte) int { + return doFuzz(input, &snap.GetByteCodesPacket{}, snap.GetByteCodesMsg) +} +func FuzzTrieNodes(input []byte) int { + return doFuzz(input, &snap.GetTrieNodesPacket{}, snap.GetTrieNodesMsg) +} diff --git a/tests/fuzzers/keystore/keystore_test.go b/tests/fuzzers/stacktrie/debug/main.go similarity index 63% rename from tests/fuzzers/keystore/keystore_test.go rename to tests/fuzzers/stacktrie/debug/main.go index 167ff6c47157..6b634f05c23c 100644 --- a/tests/fuzzers/keystore/keystore_test.go +++ b/tests/fuzzers/stacktrie/debug/main.go @@ -1,4 +1,4 @@ -// Copyright 2023 The go-ethereum Authors +// Copyright 2020 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -14,12 +14,25 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package keystore +package main -import "testing" +import ( + "fmt" + "os" -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) + "github.com/ethereum/go-ethereum/tests/fuzzers/stacktrie" +) + +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "Usage: debug ") + os.Exit(1) + } + crasher := os.Args[1] + data, err := os.ReadFile(crasher) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) + os.Exit(1) + } + stacktrie.Debug(data) } diff --git a/tests/fuzzers/stacktrie/trie_fuzzer.go b/tests/fuzzers/stacktrie/trie_fuzzer.go index 9e02176e3edc..b818f2ec3ca6 100644 --- a/tests/fuzzers/stacktrie/trie_fuzzer.go +++ b/tests/fuzzers/stacktrie/trie_fuzzer.go @@ -115,7 +115,7 @@ type kv struct { // - 0 otherwise // // other values are reserved for future use. -func fuzz(data []byte) int { +func Fuzz(data []byte) int { f := fuzzer{ input: bytes.NewReader(data), exhausted: false, diff --git a/tests/fuzzers/stacktrie/trie_test.go b/tests/fuzzers/stacktrie/trie_test.go deleted file mode 100644 index f6f755f76a5f..000000000000 --- a/tests/fuzzers/stacktrie/trie_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package stacktrie - -import "testing" - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} diff --git a/tests/fuzzers/trie/corpus/data b/tests/fuzzers/trie/corpus/data new file mode 100644 index 000000000000..c4a4839cb80f --- /dev/null +++ b/tests/fuzzers/trie/corpus/data @@ -0,0 +1 @@ +asdlfkjasf23oiejfasdfadkfqlkjfasdlkfjalwk4jfalsdkfjawlefkjsadlfkjasldkfjwalefkjasdlfkjM \ No newline at end of file diff --git a/tests/fuzzers/trie/trie-fuzzer.go b/tests/fuzzers/trie/trie-fuzzer.go index a505fa78a0f7..687f5efb1cea 100644 --- a/tests/fuzzers/trie/trie-fuzzer.go +++ b/tests/fuzzers/trie/trie-fuzzer.go @@ -130,7 +130,7 @@ func Generate(input []byte) randTest { // - 0 otherwise // // other values are reserved for future use. -func fuzz(input []byte) int { +func Fuzz(input []byte) int { program := Generate(input) if len(program) == 0 { return 0 diff --git a/tests/fuzzers/trie/trie_test.go b/tests/fuzzers/trie/trie_test.go deleted file mode 100644 index a7d28a806edc..000000000000 --- a/tests/fuzzers/trie/trie_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package trie - -import "testing" - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} diff --git a/tests/fuzzers/txfetcher/txfetcher_fuzzer.go b/tests/fuzzers/txfetcher/txfetcher_fuzzer.go index 51f2fc3b4d97..8b501645b663 100644 --- a/tests/fuzzers/txfetcher/txfetcher_fuzzer.go +++ b/tests/fuzzers/txfetcher/txfetcher_fuzzer.go @@ -48,7 +48,7 @@ func init() { } } -func fuzz(input []byte) int { +func Fuzz(input []byte) int { // Don't generate insanely large test cases, not much value in them if len(input) > 16*1024 { return 0 diff --git a/tests/fuzzers/txfetcher/txfetcher_test.go b/tests/fuzzers/txfetcher/txfetcher_test.go deleted file mode 100644 index ac2e6b1c6778..000000000000 --- a/tests/fuzzers/txfetcher/txfetcher_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package txfetcher - -import "testing" - -func Fuzz(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzz(data) - }) -} diff --git a/tests/fuzzers/vflux/clientpool-fuzzer.go b/tests/fuzzers/vflux/clientpool-fuzzer.go index de694a7b3f2a..b3b523cc8243 100644 --- a/tests/fuzzers/vflux/clientpool-fuzzer.go +++ b/tests/fuzzers/vflux/clientpool-fuzzer.go @@ -214,7 +214,7 @@ func (f *fuzzer) atomicBalanceOp(balance vfs.AtomicBalanceOperator, id enode.ID) } } -func fuzzClientPool(input []byte) int { +func FuzzClientPool(input []byte) int { if len(input) > 10000 { return -1 } diff --git a/tests/fuzzers/vflux/clientpool_test.go b/tests/fuzzers/vflux/clientpool_test.go deleted file mode 100644 index 40c5f229056f..000000000000 --- a/tests/fuzzers/vflux/clientpool_test.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package vflux - -import "testing" - -func FuzzClientPool(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - fuzzClientPool(data) - }) -} diff --git a/tests/fuzzers/snap/fuzz_test.go b/tests/fuzzers/vflux/debug/main.go similarity index 51% rename from tests/fuzzers/snap/fuzz_test.go rename to tests/fuzzers/vflux/debug/main.go index 1c39f2bb50c8..e6cec0460661 100644 --- a/tests/fuzzers/snap/fuzz_test.go +++ b/tests/fuzzers/vflux/debug/main.go @@ -1,4 +1,4 @@ -// Copyright 2023 The go-ethereum Authors +// Copyright 2020 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -14,34 +14,30 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package snap +package main import ( - "testing" + "fmt" + "os" - "github.com/ethereum/go-ethereum/eth/protocols/snap" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/tests/fuzzers/vflux" ) -func FuzzARange(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - doFuzz(data, &snap.GetAccountRangePacket{}, snap.GetAccountRangeMsg) - }) -} - -func FuzzSRange(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - doFuzz(data, &snap.GetStorageRangesPacket{}, snap.GetStorageRangesMsg) - }) -} - -func FuzzByteCodes(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - doFuzz(data, &snap.GetByteCodesPacket{}, snap.GetByteCodesMsg) - }) -} +func main() { + log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) -func FuzzTrieNodes(f *testing.F) { - f.Fuzz(func(t *testing.T, data []byte) { - doFuzz(data, &snap.GetTrieNodesPacket{}, snap.GetTrieNodesMsg) - }) + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "Usage: debug \n") + fmt.Fprintf(os.Stderr, "Example\n") + fmt.Fprintf(os.Stderr, " $ debug ../crashers/4bbef6857c733a87ecf6fd8b9e7238f65eb9862a\n") + os.Exit(1) + } + crasher := os.Args[1] + data, err := os.ReadFile(crasher) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading crasher %v: %v", crasher, err) + os.Exit(1) + } + vflux.FuzzClientPool(data) }