2020-11-19 22:18:25 +00:00
|
|
|
#!/bin/bash -eu
|
|
|
|
# Copyright 2020 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
path=$1
|
|
|
|
function=$2
|
|
|
|
fuzzer=$3
|
2021-03-17 14:42:12 +00:00
|
|
|
tags="-tags gofuzz"
|
2020-11-19 22:18:25 +00:00
|
|
|
if [[ $# -eq 4 ]]; then
|
|
|
|
tags="-tags $4"
|
|
|
|
fi
|
|
|
|
|
2021-02-22 23:25:47 +00:00
|
|
|
# makes directory change temporary
|
|
|
|
(
|
2021-02-19 18:03:22 +00:00
|
|
|
cd $GOPATH/src/$path || true
|
2021-02-22 17:06:53 +00:00
|
|
|
# in the case we are in the right directory, with go.mod but no go.sum
|
|
|
|
go mod tidy || true
|
2021-02-19 18:03:22 +00:00
|
|
|
# project was downloaded with go get if go list fails
|
2021-02-22 23:25:47 +00:00
|
|
|
go list $tags $path || { cd $GOPATH/pkg/mod/ && cd `echo $path | cut -d/ -f1-3 | awk '{print $1"@*"}'`; }
|
2021-02-19 18:03:22 +00:00
|
|
|
# project does not have go.mod if go list fails again
|
2021-02-23 15:35:32 +00:00
|
|
|
go list $tags $path || { go mod init $path && go mod tidy ;}
|
2021-02-19 18:03:22 +00:00
|
|
|
|
2020-11-19 22:18:25 +00:00
|
|
|
if [[ $SANITIZER = *coverage* ]]; then
|
2021-02-19 18:03:22 +00:00
|
|
|
fuzzed_package=`go list $tags -f '{{.Name}}' $path`
|
2021-03-09 14:07:33 +00:00
|
|
|
abspath=`go list $tags -f {{.Dir}} $path`
|
|
|
|
cd $abspath
|
2020-11-19 22:18:25 +00:00
|
|
|
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
|
|
|
|
|
2021-01-07 15:41:26 +00:00
|
|
|
fuzzed_repo=`echo $path | cut -d/ -f-3`
|
2021-03-17 14:42:12 +00:00
|
|
|
abspath_repo=`go list -m $tags -f {{.Dir}} $fuzzed_repo || go list $tags -f {{.Dir}} $fuzzed_repo`
|
2021-03-16 19:19:02 +00:00
|
|
|
# give equivalence to absolute paths in another file, as go test -cover uses golangish pkg.Dir
|
2021-03-17 14:42:12 +00:00
|
|
|
echo "s=$fuzzed_repo"="$abspath_repo$"= > $OUT/$fuzzer.gocovpath
|
2021-03-14 15:29:13 +00:00
|
|
|
go test -run Test${function}Corpus -v $tags -coverpkg $fuzzed_repo/... -c -o $OUT/$fuzzer $path
|
2020-11-19 22:18:25 +00:00
|
|
|
else
|
|
|
|
# Compile and instrument all Go files relevant to this fuzz target.
|
|
|
|
echo "Running go-fuzz $tags -func $function -o $fuzzer.a $path"
|
|
|
|
go-fuzz $tags -func $function -o $fuzzer.a $path
|
|
|
|
|
|
|
|
# Link Go code ($fuzzer.a) with fuzzing engine to produce fuzz target binary.
|
|
|
|
$CXX $CXXFLAGS $LIB_FUZZING_ENGINE $fuzzer.a -o $OUT/$fuzzer
|
|
|
|
fi
|
2021-02-22 23:25:47 +00:00
|
|
|
)
|