Skip to content

Commit d10a2f6

Browse files
tests/fuzzers: update fuzzers to be based on go-native fuzzing (#28352)
This change modifies the fuzzers to use the native golang fuzzing framework instead of go-fuzz
1 parent da55b23 commit d10a2f6

38 files changed

+641
-562
lines changed

crypto/blake2b/blake2b_f_fuzz.go

-58
This file was deleted.

crypto/blake2b/blake2b_f_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blake2b
22

33
import (
4+
"encoding/binary"
45
"fmt"
56
"reflect"
67
"testing"
@@ -57,3 +58,60 @@ var testVectorsF = []testVector{
5758
},
5859
},
5960
}
61+
62+
func Fuzz(f *testing.F) {
63+
f.Fuzz(func(t *testing.T, data []byte) {
64+
fuzz(data)
65+
})
66+
}
67+
68+
func fuzz(data []byte) {
69+
// Make sure the data confirms to the input model
70+
if len(data) != 211 {
71+
return
72+
}
73+
// Parse everything and call all the implementations
74+
var (
75+
rounds = binary.BigEndian.Uint16(data[0:2])
76+
77+
h [8]uint64
78+
m [16]uint64
79+
t [2]uint64
80+
f uint64
81+
)
82+
83+
for i := 0; i < 8; i++ {
84+
offset := 2 + i*8
85+
h[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
86+
}
87+
for i := 0; i < 16; i++ {
88+
offset := 66 + i*8
89+
m[i] = binary.LittleEndian.Uint64(data[offset : offset+8])
90+
}
91+
t[0] = binary.LittleEndian.Uint64(data[194:202])
92+
t[1] = binary.LittleEndian.Uint64(data[202:210])
93+
94+
if data[210]%2 == 1 { // Avoid spinning the fuzzer to hit 0/1
95+
f = 0xFFFFFFFFFFFFFFFF
96+
}
97+
98+
// Run the blake2b compression on all instruction sets and cross reference
99+
want := h
100+
fGeneric(&want, &m, t[0], t[1], f, uint64(rounds))
101+
102+
have := h
103+
fSSE4(&have, &m, t[0], t[1], f, uint64(rounds))
104+
if have != want {
105+
panic("SSE4 mismatches generic algo")
106+
}
107+
have = h
108+
fAVX(&have, &m, t[0], t[1], f, uint64(rounds))
109+
if have != want {
110+
panic("AVX mismatches generic algo")
111+
}
112+
have = h
113+
fAVX2(&have, &m, t[0], t[1], f, uint64(rounds))
114+
if have != want {
115+
panic("AVX2 mismatches generic algo")
116+
}
117+
}

oss-fuzz.sh

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#/bin/bash -eu
2-
# Copyright 2020 Google Inc.
1+
#!/bin/bash -eu
2+
# Copyright 2022 Google LLC
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -15,17 +15,6 @@
1515
#
1616
################################################################################
1717

18-
# This file is for integration with Google OSS-Fuzz.
19-
# The following ENV variables are available when executing on OSS-fuzz:
20-
#
21-
# /out/ $OUT Directory to store build artifacts (fuzz targets, dictionaries, options files, seed corpus archives).
22-
# /src/ $SRC Directory to checkout source files.
23-
# /work/ $WORK Directory to store intermediate files.
24-
#
25-
# $CC, $CXX, $CCC The C and C++ compiler binaries.
26-
# $CFLAGS, $CXXFLAGS C and C++ compiler flags.
27-
# $LIB_FUZZING_ENGINE C++ compiler argument to link fuzz target against the prebuilt engine library (e.g. libFuzzer).
28-
2918
# This sets the -coverpgk for the coverage report when the corpus is executed through go test
3019
coverpkg="github.com/ethereum/go-ethereum/..."
3120

@@ -59,25 +48,38 @@ DOG
5948
cd -
6049
}
6150

62-
function compile_fuzzer {
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
51+
function build_native_go_fuzzer() {
52+
fuzzer=$1
53+
function=$2
54+
path=$3
55+
tags="-tags gofuzz"
56+
57+
if [[ $SANITIZER == *coverage* ]]; then
58+
coverbuild $path $function $fuzzer $coverpkg
59+
else
60+
go-118-fuzz-build $tags -o $fuzzer.a -func $function $path
61+
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE $fuzzer.a -o $OUT/$fuzzer
62+
fi
63+
}
6764

65+
function compile_fuzzer() {
6866
path=$GOPATH/src/github.com/ethereum/go-ethereum/$1
69-
func=$2
67+
function=$2
7068
fuzzer=$3
7169

7270
echo "Building $fuzzer"
71+
cd $path
72+
73+
# Install build dependencies
74+
go install github.com/AdamKorcz/go-118-fuzz-build@latest
75+
go get github.com/AdamKorcz/go-118-fuzz-build/testing
7376

74-
# Do a coverage-build or a regular build
75-
if [[ $SANITIZER = *coverage* ]]; then
76-
coverbuild $path $func $fuzzer $coverpkg
77+
# Test if file contains a line with "func $function(" and "testing.F".
78+
if [ $(grep -r "func $function(" $path | grep "testing.F" | wc -l) -eq 1 ]
79+
then
80+
build_native_go_fuzzer $fuzzer $function $path
7781
else
78-
(cd $path && \
79-
go-fuzz -func $func -o $WORK/$fuzzer.a . && \
80-
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE $WORK/$fuzzer.a -o $OUT/$fuzzer)
82+
echo "Could not find the function: func ${function}(f *testing.F)"
8183
fi
8284

8385
## Check if there exists a seed corpus file
@@ -87,9 +89,11 @@ function compile_fuzzer {
8789
cp $corpusfile $OUT/
8890
echo "Found seed corpus: $corpusfile"
8991
fi
92+
cd -
9093
}
9194

92-
compile_fuzzer tests/fuzzers/bitutil Fuzz fuzzBitutilCompress
95+
compile_fuzzer tests/fuzzers/bitutil FuzzEncoder fuzzBitutilEncoder
96+
compile_fuzzer tests/fuzzers/bitutil FuzzDecoder fuzzBitutilDecoder
9397
compile_fuzzer tests/fuzzers/bn256 FuzzAdd fuzzBn256Add
9498
compile_fuzzer tests/fuzzers/bn256 FuzzMul fuzzBn256Mul
9599
compile_fuzzer tests/fuzzers/bn256 FuzzPair fuzzBn256Pair

tests/fuzzers/abi/abifuzzer.go

-170
This file was deleted.

0 commit comments

Comments
 (0)