Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

native go fuzzing: ignore non-fuzz tests #8285

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion infra/base-images/base-builder/compile_native_go_fuzzer
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions projects/testing-native-go-fuzzing/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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