diff --git a/infra/base-images/base-builder/compile_native_go_fuzzer b/infra/base-images/base-builder/compile_native_go_fuzzer index a7c58a61905d..b5f5b748698e 100755 --- a/infra/base-images/base-builder/compile_native_go_fuzzer +++ b/infra/base-images/base-builder/compile_native_go_fuzzer @@ -20,12 +20,23 @@ function rewrite_go_fuzz_harness() { fuzzer_filename=$1 fuzz_function=$2 + remove_tests=$3 # Create a copy of the fuzzer to not modify the existing fuzzer. cp $fuzzer_filename "${fuzzer_filename}"_fuzz_.go mv $fuzzer_filename /tmp/ fuzzer_fn="${fuzzer_filename}"_fuzz_.go + if "${remove_tests}"; then + # Remove the body of go testing funcs that may be co-located. + echo "removing *testing.T" + sed -i -e '/func Test.*testing.T) {$/ {:r;/\n}/!{N;br}; s/\n.*\n/\n/}' "${fuzzer_fn}" + # After removing the body of the go testing funcs, consolidate the imports. + if command -v goimports; then + goimports -w "${fuzzer_fn}" + fi + fi + # Replace *testing.F with *go118fuzzbuildutils.F. echo "replacing *testing.F" sed -i "s/func $fuzz_function(\([a-zA-Z0-9]*\) \*testing\.F)/func $fuzz_function(\1 \*go118fuzzbuildutils\.F)/g" "${fuzzer_fn}" @@ -73,6 +84,8 @@ path=$1 function=$2 fuzzer=$3 tags="-tags gofuzz" +# remove_tests removes the body of any test funcs co-located with the fuzzing tests. +remove_tests="${4:-false}" # Get absolute path. abs_file_dir=$(go list $tags -f {{.Dir}} $path) @@ -84,7 +97,7 @@ fuzzer_filename=$(grep -r -l --include='*.go' -s "$function" "${abs_file_dir}") if [ $(grep -r "func $function" $fuzzer_filename | grep "testing.F" | wc -l) -eq 1 ] then - rewrite_go_fuzz_harness $fuzzer_filename $function + rewrite_go_fuzz_harness $fuzzer_filename $function $remove_tests build_native_go_fuzzer $fuzzer $function $abs_file_dir # Clean up. diff --git a/projects/testing-native-go-fuzzing/Dockerfile b/projects/testing-native-go-fuzzing/Dockerfile index 00b8d69a0879..c874d2339073 100644 --- a/projects/testing-native-go-fuzzing/Dockerfile +++ b/projects/testing-native-go-fuzzing/Dockerfile @@ -19,10 +19,11 @@ RUN git clone --depth 1 https://github.com/vitessio/vitess RUN go install golang.org/dl/gotip@latest \ && gotip download RUN go install github.com/AdamKorcz/go-118-fuzz-build@latest +RUN go install golang.org/x/tools/cmd/goimports@latest COPY build.sh \ - native_ossfuzz_coverage_runnger.go \ - fuzzers/tablet_manager_fuzzer_test.go \ - fuzzers/parser_fuzzer_test.go \ - fuzzers/ast_fuzzer_test.go \ - $SRC/ + native_ossfuzz_coverage_runnger.go \ + fuzzers/tablet_manager_fuzzer_test.go \ + fuzzers/parser_fuzzer_test.go \ + fuzzers/ast_fuzzer_test.go \ + $SRC/ WORKDIR $SRC/vitess