Skip to content

Commit 5b371de

Browse files
author
Paulo Gomes
committed
fuzz: Fix upstream build
The upstream build is failing with: /usr/local/bin/compile_native_go_fuzzer: line 35: addimport: command not found The changes uses the upstream approach to install the missing commands. Note that one of the reasons of the unilateral failure upstream is due to the base image changed in: google/oss-fuzz@e71e320 Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
1 parent cce0deb commit 5b371de

File tree

3 files changed

+42
-111
lines changed

3 files changed

+42
-111
lines changed

tests/fuzz/Dockerfile.builder

-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,4 @@ FROM gcr.io/oss-fuzz-base/base-builder-go
33
COPY ./ $GOPATH/src/github.com/fluxcd/helm-controller/
44
COPY ./tests/fuzz/oss_fuzz_build.sh $SRC/build.sh
55

6-
# Temporarily overrides compile_native_go_fuzzer.
7-
# Pending upstream merge: https://github.com/google/oss-fuzz/pull/8285
8-
COPY tests/fuzz/compile_native_go_fuzzer.sh /usr/local/bin/compile_native_go_fuzzer
9-
RUN go install golang.org/x/tools/cmd/goimports@latest
10-
116
WORKDIR $SRC

tests/fuzz/compile_native_go_fuzzer.sh

-102
This file was deleted.

tests/fuzz/oss_fuzz_build.sh

+42-4
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,61 @@ set -euxo pipefail
1919
GOPATH="${GOPATH:-/root/go}"
2020
GO_SRC="${GOPATH}/src"
2121
PROJECT_PATH="github.com/fluxcd/helm-controller"
22+
TMP_DIR=$(mktemp -d /tmp/oss_fuzz-XXXXXX)
23+
24+
cleanup(){
25+
rm -rf "${TMP_DIR}"
26+
}
27+
trap cleanup EXIT
28+
29+
install_deps(){
30+
if ! command -v go-118-fuzz-build &> /dev/null || ! command -v addimport &> /dev/null; then
31+
mkdir -p "${TMP_DIR}/go-118-fuzz-build"
32+
33+
git clone https://github.com/AdamKorcz/go-118-fuzz-build "${TMP_DIR}/go-118-fuzz-build"
34+
cd "${TMP_DIR}/go-118-fuzz-build"
35+
go build -o "${GOPATH}/bin/go-118-fuzz-build"
36+
37+
cd addimport
38+
go build -o "${GOPATH}/bin/addimport"
39+
fi
40+
41+
if ! command -v goimports &> /dev/null; then
42+
go install golang.org/x/tools/cmd/goimports@latest
43+
fi
44+
}
45+
46+
# Removes the content of test funcs which could cause the Fuzz
47+
# tests to break.
48+
remove_test_funcs(){
49+
filename=$1
50+
51+
echo "removing co-located *testing.T"
52+
sed -i -e '/func Test.*testing.T) {$/ {:r;/\n}/!{N;br}; s/\n.*\n/\n/}' "${filename}"
53+
54+
# After removing the body of the go testing funcs, consolidate the imports.
55+
goimports -w "${filename}"
56+
}
57+
58+
install_deps
2259

2360
cd "${GO_SRC}/${PROJECT_PATH}"
2461

25-
go install github.com/AdamKorcz/go-118-fuzz-build@latest
2662
go get github.com/AdamKorcz/go-118-fuzz-build/utils
2763

2864
# Iterate through all Go Fuzz targets, compiling each into a fuzzer.
2965
test_files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .)
3066
for file in ${test_files}
3167
do
68+
remove_test_funcs "${file}"
69+
3270
targets=$(grep -oP 'func \K(Fuzz\w*)' "${file}")
3371
for target_name in ${targets}
3472
do
35-
fuzzer_name=$(echo "${target_name}" | tr '[:upper:]' '[:lower:]')
36-
target_dir=$(dirname "${file}")
73+
fuzzer_name=$(echo "${target_name}" | tr '[:upper:]' '[:lower:]')
74+
target_dir=$(dirname "${file}")
3775

3876
echo "Building ${file}.${target_name} into ${fuzzer_name}"
39-
compile_native_go_fuzzer "${target_dir}" "${target_name}" "${fuzzer_name}" fuzz
77+
compile_native_go_fuzzer "${target_dir}" "${target_name}" "${fuzzer_name}"
4078
done
4179
done

0 commit comments

Comments
 (0)