|
16 | 16 |
|
17 | 17 | set -euxo pipefail
|
18 | 18 |
|
| 19 | +# This file aims for: |
| 20 | +# - Dynamically discover and build all fuzz tests within the repository. |
| 21 | +# - Work for both local make fuzz-smoketest and the upstream oss-fuzz. |
| 22 | + |
19 | 23 | GOPATH="${GOPATH:-/root/go}"
|
20 | 24 | GO_SRC="${GOPATH}/src"
|
21 | 25 | PROJECT_PATH="github.com/fluxcd/kustomize-controller"
|
22 |
| -TMP_DIR=$(mktemp -d /tmp/oss_fuzz-XXXXXX) |
23 |
| - |
24 |
| -cleanup(){ |
25 |
| - rm -rf "${TMP_DIR}" |
26 |
| -} |
27 |
| -trap cleanup EXIT |
28 | 26 |
|
| 27 | +# install_deps installs all dependencies needed for upstream oss-fuzz. |
| 28 | +# Unfortunately we can't pin versions here, as we want to always |
| 29 | +# have the latest, so that we can reproduce errors occuring upstream. |
29 | 30 | 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 |
| 31 | + if ! command -v go-118-fuzz-build &> /dev/null; then |
| 32 | + go install github.com/AdamKorcz/go-118-fuzz-build@latest |
43 | 33 | fi
|
44 | 34 | }
|
45 | 35 |
|
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 |
| - # Remove gomega reference as it is not used by Fuzz tests. |
54 |
| - sed -i 's;. "github.com/onsi/gomega";;g' "${filename}" |
55 |
| - |
56 |
| - # After removing the body of the go testing funcs, consolidate the imports. |
57 |
| - goimports -w "${filename}" |
58 |
| -} |
59 |
| - |
60 | 36 | install_deps
|
61 | 37 |
|
62 | 38 | cd "${GO_SRC}/${PROJECT_PATH}"
|
63 | 39 |
|
64 |
| -go get github.com/AdamKorcz/go-118-fuzz-build/utils |
| 40 | +# Ensure any project-specific requirements are catered for ahead of |
| 41 | +# the generic build process. |
| 42 | +if [ -f "tests/fuzz/oss_fuzz_prebuild.sh" ]; then |
| 43 | + tests/fuzz/oss_fuzz_prebuild.sh |
| 44 | +fi |
| 45 | + |
| 46 | +modules=$(find . -mindepth 1 -maxdepth 4 -type f -name 'go.mod' | cut -c 3- | sed 's|/[^/]*$$||' | sort -u | sed 's;/go.mod;;g' | sed 's;go.mod;.;g') |
65 | 47 |
|
66 |
| -mkdir -p controllers/testdata/crd |
67 |
| -cp config/crd/bases/*.yaml controllers/testdata/crd |
| 48 | +for module in ${modules}; do |
68 | 49 |
|
69 |
| -# Iterate through all Go Fuzz targets, compiling each into a fuzzer. |
70 |
| -test_files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' .) |
71 |
| -for file in ${test_files} |
72 |
| -do |
73 |
| - remove_test_funcs "${file}" |
| 50 | + cd "${GO_SRC}/${PROJECT_PATH}/${module}" |
74 | 51 |
|
75 |
| - targets=$(grep -oP 'func \K(Fuzz\w*)' "${file}") |
76 |
| - for target_name in ${targets} |
77 |
| - do |
78 |
| - fuzzer_name=$(echo "${target_name}" | tr '[:upper:]' '[:lower:]') |
79 |
| - target_dir=$(dirname "${file}") |
| 52 | + # TODO: stop ignoring recorder_fuzzer_test.go. Temporary fix for fuzzing building issues. |
| 53 | + test_files=$(grep -r --include='**_test.go' --files-with-matches 'func Fuzz' . || echo "") |
| 54 | + if [ -z "${test_files}" ]; then |
| 55 | + continue |
| 56 | + fi |
80 | 57 |
|
81 |
| - echo "Building ${file}.${target_name} into ${fuzzer_name}" |
82 |
| - compile_native_go_fuzzer "${target_dir}" "${target_name}" "${fuzzer_name}" |
| 58 | + go get github.com/AdamKorcz/go-118-fuzz-build/testing |
| 59 | + |
| 60 | + # Iterate through all Go Fuzz targets, compiling each into a fuzzer. |
| 61 | + for file in ${test_files}; do |
| 62 | + # If the subdir is a module, skip this file, as it will be handled |
| 63 | + # at the next iteration of the outer loop. |
| 64 | + if [ -f "$(dirname "${file}")/go.mod" ]; then |
| 65 | + continue |
| 66 | + fi |
| 67 | + |
| 68 | + targets=$(grep -oP 'func \K(Fuzz\w*)' "${file}") |
| 69 | + for target_name in ${targets}; do |
| 70 | + # Transform module path into module name (e.g. git/libgit2 to git_libgit2). |
| 71 | + module_name=$(echo ${module} | tr / _) |
| 72 | + # Compose fuzzer name based on the lowercase version of the func names. |
| 73 | + # The module name is added after the fuzz prefix, for better discoverability. |
| 74 | + fuzzer_name=$(echo "${target_name}" | tr '[:upper:]' '[:lower:]' | sed "s;fuzz_;fuzz_${module_name}_;g") |
| 75 | + target_dir=$(dirname "${file}") |
| 76 | + |
| 77 | + echo "Building ${file}.${target_name} into ${fuzzer_name}" |
| 78 | + compile_native_go_fuzzer "${target_dir}" "${target_name}" "${fuzzer_name}" |
| 79 | + done |
83 | 80 | done
|
84 | 81 | done
|
0 commit comments