Skip to content

Commit b543e5f

Browse files
author
Paulo Gomes
authored
Merge pull request #529 from pjbgf/fuzz-update
fuzz: Fix upstream build and optimise execution
2 parents c657c60 + c435ad9 commit b543e5f

File tree

5 files changed

+50
-119
lines changed

5 files changed

+50
-119
lines changed

.github/workflows/cifuzz.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ jobs:
1313
steps:
1414
- name: Checkout
1515
uses: actions/checkout@v3
16+
- name: Setup Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: 1.18.x
1620
- name: Restore Go cache
1721
uses: actions/cache@v3
1822
with:
19-
path: /home/runner/work/_temp/_github_home/go/pkg/mod
23+
path: ~/go/pkg/mod
2024
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
2125
restore-keys: |
22-
${{ runner.os }}-go-
26+
${{ runner.os }}-go
2327
- name: Smoke test Fuzzers
2428
run: make fuzz-smoketest

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ fuzz-build:
151151
rm -rf $(BUILD_DIR)/fuzz/
152152
mkdir -p $(BUILD_DIR)/fuzz/out/
153153

154-
docker build . --tag local-fuzzing:latest -f tests/fuzz/Dockerfile.builder
154+
docker build . --pull --tag local-fuzzing:latest -f tests/fuzz/Dockerfile.builder
155155
docker run --rm \
156156
-e FUZZING_LANGUAGE=go -e SANITIZER=address \
157157
-e CIFUZZ_DEBUG='True' -e OSS_FUZZ_PROJECT_NAME=fluxcd \
158+
-v "$(shell go env GOMODCACHE):/root/go/pkg/mod" \
158159
-v "$(BUILD_DIR)/fuzz/out":/out \
159160
local-fuzzing:latest
160161

tests/fuzz/Dockerfile.builder

-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
FROM golang:1.18 AS go
2-
31
FROM gcr.io/oss-fuzz-base/base-builder-go
42

5-
# ensures golang 1.18 to enable go native fuzzing.
6-
COPY --from=go /usr/local/go /usr/local/
7-
83
COPY ./ $GOPATH/src/github.com/fluxcd/helm-controller/
94
COPY ./tests/fuzz/oss_fuzz_build.sh $SRC/build.sh
105

11-
# Temporarily overrides compile_native_go_fuzzer.
12-
# Pending upstream merge: https://github.com/google/oss-fuzz/pull/8285
13-
COPY tests/fuzz/compile_native_go_fuzzer.sh /usr/local/bin/compile_native_go_fuzzer
14-
RUN go install golang.org/x/tools/cmd/goimports@latest
15-
166
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)