• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -eu
2# Copyright 2020 Google Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16################################################################################
17
18path=$1
19function=$2
20fuzzer=$3
21tags="-tags gofuzz"
22if [[ $#  -eq 4 ]]; then
23  tags="-tags $4"
24fi
25
26# makes directory change temporary
27(
28cd $GOPATH/src/$path || true
29# in the case we are in the right directory, with go.mod but no go.sum
30go mod tidy || true
31# project was downloaded with go get if go list fails
32go list $tags $path || { cd $GOPATH/pkg/mod/ && cd `echo $path | cut -d/ -f1-3 | awk '{print $1"@*"}'`; }
33# project does not have go.mod if go list fails again
34go list $tags $path || { go mod init $path && go mod tidy ;}
35
36if [[ $SANITIZER = *coverage* ]]; then
37  fuzzed_package=`go list $tags -f '{{.Name}}' $path`
38  abspath=`go list $tags -f {{.Dir}} $path`
39  cd $abspath
40  cp $GOPATH/ossfuzz_coverage_runner.go ./"${function,,}"_test.go
41  sed -i -e 's/FuzzFunction/'$function'/' ./"${function,,}"_test.go
42  sed -i -e 's/mypackagebeingfuzzed/'$fuzzed_package'/' ./"${function,,}"_test.go
43  sed -i -e 's/TestFuzzCorpus/Test'$function'Corpus/' ./"${function,,}"_test.go
44
45  fuzzed_repo=`echo $path | cut -d/ -f-3`
46  abspath_repo=`go list -m $tags -f {{.Dir}} $fuzzed_repo || go list $tags -f {{.Dir}} $fuzzed_repo`
47  # give equivalence to absolute paths in another file, as go test -cover uses golangish pkg.Dir
48  echo "s=$fuzzed_repo"="$abspath_repo"= > $OUT/$fuzzer.gocovpath
49  go test -run Test${function}Corpus -v $tags -coverpkg $fuzzed_repo/... -c -o $OUT/$fuzzer $path
50else
51  # Compile and instrument all Go files relevant to this fuzz target.
52  echo "Running go-fuzz $tags -func $function -o $fuzzer.a $path"
53  go-fuzz $tags -func $function -o $fuzzer.a $path
54
55  # Link Go code ($fuzzer.a) with fuzzing engine to produce fuzz target binary.
56  $CXX $CXXFLAGS $LIB_FUZZING_ENGINE $fuzzer.a -o $OUT/$fuzzer
57fi
58)
59