• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash -eu
2#
3# Copyright 2021 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17################################################################################
18
19: "${BAZEL_FUZZ_TEST_TAG:=fuzz-test}"
20: "${BAZEL_FUZZ_TEST_EXCLUDE_TAG:=no-oss-fuzz}"
21: "${BAZEL_PACKAGE_SUFFIX:=_oss_fuzz}"
22: "${BAZEL_TOOL:=bazel}"
23: "${BAZEL_EXTRA_BUILD_FLAGS:=}"
24
25if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
26    BAZEL_LANGUAGE=java
27else
28    BAZEL_LANGUAGE=cc
29fi
30
31if [[ -z "${BAZEL_FUZZ_TEST_QUERY:-}" ]]; then
32    BAZEL_FUZZ_TEST_QUERY="
33        let all_fuzz_tests = attr(tags, \"${BAZEL_FUZZ_TEST_TAG}\", \"//...\") in
34        let lang_fuzz_tests = attr(generator_function, \"^${BAZEL_LANGUAGE}_fuzz_test\$\", \$all_fuzz_tests) in
35        \$lang_fuzz_tests - attr(tags, \"${BAZEL_FUZZ_TEST_EXCLUDE_TAG}\", \$lang_fuzz_tests)
36    "
37fi
38
39echo "Using Bazel query to find fuzz targets: ${BAZEL_FUZZ_TEST_QUERY}"
40
41declare -r OSS_FUZZ_TESTS=(
42    $(bazel query "${BAZEL_FUZZ_TEST_QUERY}" | sed "s/$/${BAZEL_PACKAGE_SUFFIX}/")
43)
44
45echo "Found ${#OSS_FUZZ_TESTS[@]} fuzz test packages:"
46for oss_fuzz_test in "${OSS_FUZZ_TESTS[@]}"; do
47    echo "  ${oss_fuzz_test}"
48done
49
50declare -r BAZEL_BUILD_FLAGS=(
51    "-c" "opt"
52    "--@rules_fuzzing//fuzzing:cc_engine=@rules_fuzzing_oss_fuzz//:oss_fuzz_engine" \
53    "--@rules_fuzzing//fuzzing:cc_engine_instrumentation=oss-fuzz" \
54    "--@rules_fuzzing//fuzzing:cc_engine_sanitizer=none" \
55    "--cxxopt=-stdlib=libc++" \
56    "--linkopt=-lc++" \
57    "--action_env=CC=${CC}" "--action_env=CXX=${CXX}" \
58    ${BAZEL_EXTRA_BUILD_FLAGS[*]}
59)
60
61echo "Building the fuzz tests with the following Bazel options:"
62echo "  ${BAZEL_BUILD_FLAGS[@]}"
63
64${BAZEL_TOOL} build "${BAZEL_BUILD_FLAGS[@]}" "${OSS_FUZZ_TESTS[@]}"
65
66echo "Extracting the fuzz test packages in the output directory."
67for oss_fuzz_archive in $(find bazel-bin/ -name "*${BAZEL_PACKAGE_SUFFIX}.tar"); do
68    tar -xvf "${oss_fuzz_archive}" -C "${OUT}"
69done
70
71if [ "$SANITIZER" = "coverage" ]; then
72    echo "Collecting the repository source files for coverage tracking."
73    declare -r COVERAGE_SOURCES="${OUT}/proc/self/cwd"
74    mkdir -p "${COVERAGE_SOURCES}"
75    declare -r RSYNC_FILTER_ARGS=(
76        "--include" "*.h"
77        "--include" "*.cc"
78        "--include" "*.hpp"
79        "--include" "*.cpp"
80        "--include" "*.c"
81        "--include" "*.inc"
82        "--include" "*/"
83        "--exclude" "*"
84    )
85    rsync -avLk "${RSYNC_FILTER_ARGS[@]}" \
86        "$(bazel info execution_root)/" \
87        "${COVERAGE_SOURCES}/"
88fi
89