aboutsummaryrefslogtreecommitdiff
path: root/infra
diff options
context:
space:
mode:
authorjonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com>2021-08-31 08:30:11 -0700
committerGitHub <noreply@github.com>2021-08-31 08:30:11 -0700
commit0d7a3348544978cb1f366b57db5e8d559369bca4 (patch)
treeec3f9be99fc42ec90d5fe4c0ab6be7cabdcf664a /infra
parentc2947cd4b93da92c4d2271cb951002bec75162f8 (diff)
downloadoss-fuzz-0d7a3348544978cb1f366b57db5e8d559369bca4.tar.gz
[base-builder-new] Fix issues (#6363)
Include cargo and compile_go_fuzzer.
Diffstat (limited to 'infra')
-rwxr-xr-xinfra/base-images/base-builder-new/cargo51
-rwxr-xr-xinfra/base-images/base-builder-new/compile_go_fuzzer60
2 files changed, 111 insertions, 0 deletions
diff --git a/infra/base-images/base-builder-new/cargo b/infra/base-images/base-builder-new/cargo
new file mode 100755
index 000000000..d83e1d2e7
--- /dev/null
+++ b/infra/base-images/base-builder-new/cargo
@@ -0,0 +1,51 @@
+#!/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.
+#
+# This is a wrapper around calling cargo
+# This just expands RUSTFLAGS in case of a coverage build
+# We need this until https://github.com/rust-lang/cargo/issues/5450 is merged
+# because cargo uses relative paths for the current crate
+# and absolute paths for its dependencies
+#
+################################################################################
+
+if [ "$SANITIZER" = "coverage" ] && [ $1 = "build" ]
+then
+ crate_src_abspath=`cargo metadata --no-deps --format-version 1 | jq -r '.workspace_root'`
+ export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix src=$crate_src_abspath/src"
+fi
+
+if [ "$SANITIZER" = "coverage" ] && [ $1 = "fuzz" ] && [ $2 = "build" ]
+then
+ # hack to turn cargo fuzz build into cargo build so as to get coverage
+ # cargo fuzz adds "--target" "x86_64-unknown-linux-gnu"
+ (
+ # go into fuzz directory if not already the case
+ cd fuzz || true
+ fuzz_src_abspath=`pwd`
+ export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix fuzz_targets=$fuzz_src_abspath/fuzz_targets"
+ # we do not want to trigger debug assertions and stops
+ export RUSTFLAGS="$RUSTFLAGS -C debug-assertions=no"
+ # do not optimize with --release, leading to Malformed instrumentation profile data
+ cargo build --bins
+ # copies the build output in the expected target directory
+ cd `cargo metadata --format-version 1 --no-deps | jq -r '.target_directory'`
+ mkdir -p x86_64-unknown-linux-gnu/release
+ cp -r debug/* x86_64-unknown-linux-gnu/release/
+ )
+ exit 0
+fi
+
+/rust/bin/cargo "$@"
diff --git a/infra/base-images/base-builder-new/compile_go_fuzzer b/infra/base-images/base-builder-new/compile_go_fuzzer
new file mode 100755
index 000000000..dd8c9f6a1
--- /dev/null
+++ b/infra/base-images/base-builder-new/compile_go_fuzzer
@@ -0,0 +1,60 @@
+#!/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
+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"@*"}'`; } || cd -
+# 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
+ 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
+
+ # The repo is the module path/name, which is already created above in case it doesn't exist,
+ # but not always the same as the module path. This is necessary to handle SIV properly.
+ fuzzed_repo=$(go list $tags -f {{.Module}} "$path")
+ 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"
+ 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
+)