aboutsummaryrefslogtreecommitdiff
path: root/infra/base-images/base-builder/compile_go_fuzzer
diff options
context:
space:
mode:
Diffstat (limited to 'infra/base-images/base-builder/compile_go_fuzzer')
-rwxr-xr-xinfra/base-images/base-builder/compile_go_fuzzer29
1 files changed, 19 insertions, 10 deletions
diff --git a/infra/base-images/base-builder/compile_go_fuzzer b/infra/base-images/base-builder/compile_go_fuzzer
index 8f8cde759..2342800fb 100755
--- a/infra/base-images/base-builder/compile_go_fuzzer
+++ b/infra/base-images/base-builder/compile_go_fuzzer
@@ -18,27 +18,35 @@
path=$1
function=$2
fuzzer=$3
-tags=""
+tags="-tags gofuzz"
if [[ $# -eq 4 ]]; then
tags="-tags $4"
fi
+# makes directory change temporary
+(
+cd $GOPATH/src/$path || true
+# in the case we are in the right directory, with go.mod but no go.sum
+go mod tidy || true
+# project was downloaded with go get if go list fails
+go list $tags $path || { cd $GOPATH/pkg/mod/ && cd `echo $path | cut -d/ -f1-3 | awk '{print $1"@*"}'`; }
+# project does not have go.mod if go list fails again
+go list $tags $path || { go mod init $path && go mod tidy ;}
+
if [[ $SANITIZER = *coverage* ]]; then
- cd $GOPATH/src/$path
- fuzzed_package=`go list $tags -f '{{.Name}}'`
+ fuzzed_package=`go list $tags -f '{{.Name}}' $path`
+ abspath=`go list $tags -f {{.Dir}} $path`
+ cd $abspath
cp $GOPATH/ossfuzz_coverage_runner.go ./"${function,,}"_test.go
sed -i -e 's/FuzzFunction/'$function'/' ./"${function,,}"_test.go
sed -i -e 's/mypackagebeingfuzzed/'$fuzzed_package'/' ./"${function,,}"_test.go
sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go
- echo "#!/bin/sh" > $OUT/$fuzzer
- echo "cd $path" >> $OUT/$fuzzer
- # The fuzzer may be in a subdirectory, but we want the coverage report for the whole repository
fuzzed_repo=`echo $path | cut -d/ -f-3`
- echo "go test -run Test${function}Corpus -v $tags -coverpkg $fuzzed_repo/... -coverprofile \$1 " >> $OUT/$fuzzer
- chmod +x $OUT/$fuzzer
-
- cd -
+ abspath_repo=`go list -m $tags -f {{.Dir}} $fuzzed_repo || go list $tags -f {{.Dir}} $fuzzed_repo`
+ # give equivalence to absolute paths in another file, as go test -cover uses golangish pkg.Dir
+ echo "s=$fuzzed_repo"="$abspath_repo"= > $OUT/$fuzzer.gocovpath
+ go test -run Test${function}Corpus -v $tags -coverpkg $fuzzed_repo/... -c -o $OUT/$fuzzer $path
else
# Compile and instrument all Go files relevant to this fuzz target.
echo "Running go-fuzz $tags -func $function -o $fuzzer.a $path"
@@ -47,3 +55,4 @@ else
# Link Go code ($fuzzer.a) with fuzzing engine to produce fuzz target binary.
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE $fuzzer.a -o $OUT/$fuzzer
fi
+)