Skip to content

Commit

Permalink
fuzz: Fix upstream build
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Paulo Gomes committed Sep 2, 2022
1 parent ca3f985 commit ee1cb15
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 111 deletions.
5 changes: 0 additions & 5 deletions tests/fuzz/Dockerfile.builder
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@ FROM gcr.io/oss-fuzz-base/base-builder-go
COPY ./ $GOPATH/src/github.com/fluxcd/helm-controller/
COPY ./tests/fuzz/oss_fuzz_build.sh $SRC/build.sh

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

WORKDIR $SRC
102 changes: 0 additions & 102 deletions tests/fuzz/compile_native_go_fuzzer.sh

This file was deleted.

46 changes: 42 additions & 4 deletions tests/fuzz/oss_fuzz_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,61 @@ set -euxo pipefail
GOPATH="${GOPATH:-/root/go}"
GO_SRC="${GOPATH}/src"
PROJECT_PATH="github.com/fluxcd/helm-controller"
TMP_DIR=$(mktemp -d /tmp/oss_fuzz-XXXXXX)

cleanup(){
rm -rf "${TMP_DIR}"
}
trap cleanup EXIT

install_deps(){
if ! command -v go-118-fuzz-build &> /dev/null || ! command -v addimport &> /dev/null; then
mkdir -p "${TMP_DIR}/go-118-fuzz-build"

git clone https://github.com/AdamKorcz/go-118-fuzz-build "${TMP_DIR}/go-118-fuzz-build"
cd "${TMP_DIR}/go-118-fuzz-build"
go build -o "${GOPATH}/bin/go-118-fuzz-build"

cd addimport
go build -o "${GOPATH}/bin/addimport"
fi

if ! command -v goimports &> /dev/null; then
go install golang.org/x/tools/cmd/goimports@latest
fi
}

# Removes the content of test funcs which could cause the Fuzz
# tests to break.
remove_test_funcs(){
filename=$1

echo "removing co-located *testing.T"
sed -i -e '/func Test.*testing.T) {$/ {:r;/\n}/!{N;br}; s/\n.*\n/\n/}' "${filename}"

# After removing the body of the go testing funcs, consolidate the imports.
goimports -w "${filename}"
}

install_deps

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

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

# Iterate through all Go Fuzz targets, compiling each into a fuzzer.
test_files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .)
for file in ${test_files}
do
remove_test_funcs "${file}"

targets=$(grep -oP 'func \K(Fuzz\w*)' "${file}")
for target_name in ${targets}
do
fuzzer_name=$(echo "${target_name}" | tr '[:upper:]' '[:lower:]')
target_dir=$(dirname "${file}")
fuzzer_name=$(echo "${target_name}" | tr '[:upper:]' '[:lower:]')
target_dir=$(dirname "${file}")

echo "Building ${file}.${target_name} into ${fuzzer_name}"
compile_native_go_fuzzer "${target_dir}" "${target_name}" "${fuzzer_name}" fuzz
compile_native_go_fuzzer "${target_dir}" "${target_name}" "${fuzzer_name}"
done
done

0 comments on commit ee1cb15

Please sign in to comment.