aboutsummaryrefslogtreecommitdiff
path: root/infra/base-images/base-builder/bazel_build_fuzz_tests
diff options
context:
space:
mode:
Diffstat (limited to 'infra/base-images/base-builder/bazel_build_fuzz_tests')
-rwxr-xr-xinfra/base-images/base-builder/bazel_build_fuzz_tests80
1 files changed, 80 insertions, 0 deletions
diff --git a/infra/base-images/base-builder/bazel_build_fuzz_tests b/infra/base-images/base-builder/bazel_build_fuzz_tests
new file mode 100755
index 000000000..86740ee01
--- /dev/null
+++ b/infra/base-images/base-builder/bazel_build_fuzz_tests
@@ -0,0 +1,80 @@
+#!/bin/bash -eu
+#
+# Copyright 2021 Google LLC
+#
+# 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.
+#
+################################################################################
+
+: "${BAZEL_FUZZ_TEST_TAG:=fuzz-test}"
+: "${BAZEL_FUZZ_TEST_EXCLUDE_TAG:=no-oss-fuzz}"
+: "${BAZEL_PACKAGE_SUFFIX:=_oss_fuzz}"
+: "${BAZEL_TOOL:=bazel}"
+: "${BAZEL_EXTRA_BUILD_FLAGS:=}"
+
+if [[ -z "${BAZEL_FUZZ_TEST_QUERY:-}" ]]; then
+ BAZEL_FUZZ_TEST_QUERY="
+ let all_fuzz_tests = attr(tags, \"${BAZEL_FUZZ_TEST_TAG}\", \"//...\") in
+ \$all_fuzz_tests - attr(tags, \"${BAZEL_FUZZ_TEST_EXCLUDE_TAG}\", \$all_fuzz_tests)
+ "
+fi
+
+echo "Using Bazel query to find fuzz targets: ${BAZEL_FUZZ_TEST_QUERY}"
+
+declare -r OSS_FUZZ_TESTS=(
+ $(bazel query "${BAZEL_FUZZ_TEST_QUERY}" | sed "s/$/${BAZEL_PACKAGE_SUFFIX}/")
+)
+
+echo "Found ${#OSS_FUZZ_TESTS[@]} fuzz test packages:"
+for oss_fuzz_test in "${OSS_FUZZ_TESTS[@]}"; do
+ echo " ${oss_fuzz_test}"
+done
+
+declare -r BAZEL_BUILD_FLAGS=(
+ "-c" "opt"
+ "--//fuzzing:cc_engine=@rules_fuzzing_oss_fuzz//:oss_fuzz_engine" \
+ "--@rules_fuzzing//fuzzing:cc_engine_instrumentation=oss-fuzz" \
+ "--@rules_fuzzing//fuzzing:cc_engine_sanitizer=none" \
+ "--linkopt=-lc++" \
+ "--action_env=CC=${CC}" "--action_env=CXX=${CXX}" \
+ ${BAZEL_EXTRA_BUILD_FLAGS[*]}
+)
+
+echo "Building the fuzz tests with the following Bazel options:"
+echo " ${BAZEL_BUILD_FLAGS[@]}"
+
+${BAZEL_TOOL} build "${BAZEL_BUILD_FLAGS[@]}" "${OSS_FUZZ_TESTS[@]}"
+
+echo "Extracting the fuzz test packages in the output directory."
+for oss_fuzz_archive in $(find bazel-bin/ -name "*${BAZEL_PACKAGE_SUFFIX}.tar"); do
+ tar -xvf "${oss_fuzz_archive}" -C "${OUT}"
+done
+
+if [ "$SANITIZER" = "coverage" ]; then
+ echo "Collecting the repository source files for coverage tracking."
+ declare -r COVERAGE_SOURCES="${OUT}/proc/self/cwd"
+ mkdir -p "${COVERAGE_SOURCES}"
+ declare -r RSYNC_FILTER_ARGS=(
+ "--include" "*.h"
+ "--include" "*.cc"
+ "--include" "*.hpp"
+ "--include" "*.cpp"
+ "--include" "*.c"
+ "--include" "*.inc"
+ "--include" "*/"
+ "--exclude" "*"
+ )
+ rsync -avLk "${RSYNC_FILTER_ARGS[@]}" \
+ "$(bazel info execution_root)/" \
+ "${COVERAGE_SOURCES}/"
+fi