Skip to content

Commit b9012a0

Browse files
authored
common,crypto: move fuzzers out of core (#22029)
* common,crypto: move fuzzers out of core * fuzzers: move vm fuzzer out from core * fuzzing: rework cover package logic * fuzzers: lint
1 parent 158f72c commit b9012a0

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
lines changed

oss-fuzz.sh

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,80 @@
2626
# $CFLAGS, $CXXFLAGS C and C++ compiler flags.
2727
# $LIB_FUZZING_ENGINE C++ compiler argument to link fuzz target against the prebuilt engine library (e.g. libFuzzer).
2828

29+
# This sets the -coverpgk for the coverage report when the corpus is executed through go test
30+
coverpkg="github.com/ethereum/go-ethereum/..."
31+
32+
function coverbuild {
33+
path=$1
34+
function=$2
35+
fuzzer=$3
36+
tags=""
37+
38+
if [[ $# -eq 4 ]]; then
39+
tags="-tags $4"
40+
fi
41+
cd $path
42+
fuzzed_package=`pwd | rev | cut -d'/' -f 1 | rev`
43+
cp $GOPATH/ossfuzz_coverage_runner.go ./"${function,,}"_test.go
44+
sed -i -e 's/FuzzFunction/'$function'/' ./"${function,,}"_test.go
45+
sed -i -e 's/mypackagebeingfuzzed/'$fuzzed_package'/' ./"${function,,}"_test.go
46+
sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go
47+
48+
cat << DOG > $OUT/$fuzzer
49+
#/bin/sh
50+
51+
cd $OUT/$path
52+
go test -run Test${function}Corpus -v $tags -coverprofile \$1 -coverpkg $coverpkg
53+
54+
DOG
55+
56+
chmod +x $OUT/$fuzzer
57+
#echo "Built script $OUT/$fuzzer"
58+
#cat $OUT/$fuzzer
59+
cd -
60+
}
61+
2962
function compile_fuzzer {
30-
path=$SRC/go-ethereum/$1
63+
# Inputs:
64+
# $1: The package to fuzz, within go-ethereum
65+
# $2: The name of the fuzzing function
66+
# $3: The name to give to the final fuzzing-binary
67+
68+
path=$GOPATH/src/github.com/ethereum/go-ethereum/$1
3169
func=$2
3270
fuzzer=$3
33-
corpusfile="${path}/testdata/${fuzzer}_seed_corpus.zip"
34-
echo "Building $fuzzer (expecting corpus at $corpusfile)"
35-
(cd $path && \
71+
72+
echo "Building $fuzzer"
73+
74+
# Do a coverage-build or a regular build
75+
if [[ $SANITIZER = *coverage* ]]; then
76+
coverbuild $path $func $fuzzer $coverpkg
77+
else
78+
(cd $path && \
3679
go-fuzz -func $func -o $WORK/$fuzzer.a . && \
37-
echo "First stage built OK" && \
38-
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE $WORK/$fuzzer.a -o $OUT/$fuzzer && \
39-
echo "Second stage built ok" )
40-
41-
## Check if there exists a seed corpus file
42-
if [ -f $corpusfile ]
43-
then
44-
cp $corpusfile $OUT/
45-
echo "Found seed corpus: $corpusfile"
46-
fi
80+
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE $WORK/$fuzzer.a -o $OUT/$fuzzer)
81+
fi
82+
83+
## Check if there exists a seed corpus file
84+
corpusfile="${path}/testdata/${fuzzer}_seed_corpus.zip"
85+
if [ -f $corpusfile ]
86+
then
87+
cp $corpusfile $OUT/
88+
echo "Found seed corpus: $corpusfile"
89+
fi
4790
}
4891

49-
compile_fuzzer common/bitutil Fuzz fuzzBitutilCompress
50-
compile_fuzzer crypto/bn256 FuzzAdd fuzzBn256Add
51-
compile_fuzzer crypto/bn256 FuzzMul fuzzBn256Mul
52-
compile_fuzzer crypto/bn256 FuzzPair fuzzBn256Pair
53-
compile_fuzzer core/vm/runtime Fuzz fuzzVmRuntime
54-
compile_fuzzer crypto/blake2b Fuzz fuzzBlake2b
92+
compile_fuzzer tests/fuzzers/bitutil Fuzz fuzzBitutilCompress
93+
compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add
94+
compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul
95+
compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair
96+
compile_fuzzer tests/fuzzers/runtime Fuzz fuzzVmRuntime
5597
compile_fuzzer tests/fuzzers/keystore Fuzz fuzzKeystore
5698
compile_fuzzer tests/fuzzers/txfetcher Fuzz fuzzTxfetcher
5799
compile_fuzzer tests/fuzzers/rlp Fuzz fuzzRlp
58100
compile_fuzzer tests/fuzzers/trie Fuzz fuzzTrie
59101
compile_fuzzer tests/fuzzers/stacktrie Fuzz fuzzStackTrie
60-
compile_fuzzer tests/fuzzers/difficulty Fuzz fuzzDifficulty
102+
compile_fuzzer tests/fuzzers/difficulty Fuzz fuzzDifficulty
61103

62104
compile_fuzzer tests/fuzzers/bls12381 FuzzG1Add fuzz_g1_add
63105
compile_fuzzer tests/fuzzers/bls12381 FuzzG1Mul fuzz_g1_mul
@@ -69,6 +111,10 @@ compile_fuzzer tests/fuzzers/bls12381 FuzzPairing fuzz_pairing
69111
compile_fuzzer tests/fuzzers/bls12381 FuzzMapG1 fuzz_map_g1
70112
compile_fuzzer tests/fuzzers/bls12381 FuzzMapG2 fuzz_map_g2
71113

114+
#TODO: move this to tests/fuzzers, if possible
115+
compile_fuzzer crypto/blake2b Fuzz fuzzBlake2b
116+
117+
72118
# This doesn't work very well @TODO
73119
#compile_fuzzertests/fuzzers/abi Fuzz fuzzAbi
74120

common/bitutil/compress_fuzz.go renamed to tests/fuzzers/bitutil/compress_fuzz.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// +build gofuzz
18-
1917
package bitutil
2018

21-
import "bytes"
19+
import (
20+
"bytes"
21+
22+
"github.com/ethereum/go-ethereum/common/bitutil"
23+
)
2224

2325
// Fuzz implements a go-fuzz fuzzer method to test various encoding method
2426
// invocations.
@@ -35,7 +37,7 @@ func Fuzz(data []byte) int {
3537
// fuzzEncode implements a go-fuzz fuzzer method to test the bitset encoding and
3638
// decoding algorithm.
3739
func fuzzEncode(data []byte) int {
38-
proc, _ := bitsetDecodeBytes(bitsetEncodeBytes(data), len(data))
40+
proc, _ := bitutil.DecompressBytes(bitutil.CompressBytes(data), len(data))
3941
if !bytes.Equal(data, proc) {
4042
panic("content mismatch")
4143
}
@@ -45,11 +47,11 @@ func fuzzEncode(data []byte) int {
4547
// fuzzDecode implements a go-fuzz fuzzer method to test the bit decoding and
4648
// reencoding algorithm.
4749
func fuzzDecode(data []byte) int {
48-
blob, err := bitsetDecodeBytes(data, 1024)
50+
blob, err := bitutil.DecompressBytes(data, 1024)
4951
if err != nil {
5052
return 0
5153
}
52-
if comp := bitsetEncodeBytes(blob); !bytes.Equal(comp, data) {
54+
if comp := bitutil.CompressBytes(blob); !bytes.Equal(comp, data) {
5355
panic("content mismatch")
5456
}
5557
return 1

crypto/bn256/bn256_fuzz.go renamed to tests/fuzzers/bn256/bn256_fuzz.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be found
33
// in the LICENSE file.
44

5-
// +build gofuzz
6-
75
package bn256
86

97
import (
@@ -24,7 +22,7 @@ func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1) {
2422
}
2523
xg := new(google.G1)
2624
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
27-
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
25+
panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
2826
}
2927
return xc, xg
3028
}
@@ -37,7 +35,7 @@ func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2) {
3735
}
3836
xg := new(google.G2)
3937
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
40-
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
38+
panic(fmt.Sprintf("Could not marshal cloudflare -> google: %v", err))
4139
}
4240
return xc, xg
4341
}

core/vm/runtime/fuzz.go renamed to tests/fuzzers/runtime/runtime_fuzz.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// +build gofuzz
18-
1917
package runtime
2018

19+
import (
20+
"github.com/ethereum/go-ethereum/core/vm/runtime"
21+
)
22+
2123
// Fuzz is the basic entry point for the go-fuzz tool
2224
//
2325
// This returns 1 for valid parsable/runable code, 0
2426
// for invalid opcode.
2527
func Fuzz(input []byte) int {
26-
_, _, err := Execute(input, input, &Config{
27-
GasLimit: 3000000,
28+
_, _, err := runtime.Execute(input, input, &runtime.Config{
29+
GasLimit: 12000000,
2830
})
29-
3031
// invalid opcode
31-
if err != nil && len(err.Error()) > 6 && string(err.Error()[:7]) == "invalid" {
32+
if err != nil && len(err.Error()) > 6 && err.Error()[:7] == "invalid" {
3233
return 0
3334
}
34-
3535
return 1
3636
}

0 commit comments

Comments
 (0)