aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/base-images/base-builder/bazel_build_fuzz_tests
blob: dca79f3f279073d2be4fe231fc2bd32527343681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/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 [ "$FUZZING_LANGUAGE" = "jvm" ]; then
    BAZEL_LANGUAGE=java
else
    BAZEL_LANGUAGE=cc
fi

if [[ -z "${BAZEL_FUZZ_TEST_QUERY:-}" ]]; then
    BAZEL_FUZZ_TEST_QUERY="
        let all_fuzz_tests = attr(tags, \"${BAZEL_FUZZ_TEST_TAG}\", \"//...\") in
        let lang_fuzz_tests = attr(generator_function, \"^${BAZEL_LANGUAGE}_fuzz_test\$\", \$all_fuzz_tests) in
        \$lang_fuzz_tests - attr(tags, \"${BAZEL_FUZZ_TEST_EXCLUDE_TAG}\", \$lang_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"
    "--@rules_fuzzing//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" \
    "--cxxopt=-stdlib=libc++" \
    "--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