1#!/bin/bash -eu 2# Copyright 2018 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 18declare -r FUZZ_TARGET_QUERY=' 19 let all_fuzz_tests = attr(tags, "fuzz_target", "...") in 20 $all_fuzz_tests - attr(tags, "no_fuzz", $all_fuzz_tests) 21' 22declare -r OSS_FUZZ_TARGETS="$(bazel query "${FUZZ_TARGET_QUERY}" | sed 's/$/_oss_fuzz/')" 23 24declare -r EXTRA_BAZEL_FLAGS="$( 25if [ "$SANITIZER" = "undefined" ] 26then 27 # Bazel uses clang to link binary, which does not link clang_rt ubsan library for C++ automatically. 28 # See issue: https://github.com/bazelbuild/bazel/issues/8777 29 echo "--linkopt=\"$(find $(llvm-config --libdir) -name libclang_rt.ubsan_standalone_cxx-x86_64.a | head -1)\"" 30elif [ "$SANITIZER" = "address" ] 31then 32 echo "--copt=-D__SANITIZE_ADDRESS__" "--copt=-DADDRESS_SANITIZER=1" "--linkopt=-fsanitize=address" 33fi 34)" 35 36# The Envoy build configuration may clobber CFLAGS/CXXFLAGS, so we use separate 37# environment variables that are understood by rules_fuzzing. 38export FUZZING_CFLAGS="$CFLAGS" 39export FUZZING_CXXFLAGS="$CXXFLAGS" 40 41# Disable instrumentation in various external libraries. These 42# are fuzzed elsewhere. 43# The following disables both coverage-instrumentation and other sanitizer instrumentation. 44# We disable instrumentation in: 45# antlr4 46# google_protobuf 47# absl 48# googltest 49# grpc 50# boringssl 51# re2 52# upb 53# brotli 54# cel_cpp 55# yaml_cpp 56# wasm_cpp_host 57# libprotobuf-mutator 58# google_url (gurl) 59# lightstep_tracer 60# In addition to this, we disable instrumentation in all *.pb.cc (protobuf-generated files) 61# and everything in the bazel-out directory. 62declare -r DI="$( 63if [ "$SANITIZER" != "coverage" ] 64then 65# Envoy code. Disable coverage instrumentation 66 echo " --per_file_copt=^.*source/extensions/access_loggers/.*\.cc\$@-fsanitize-coverage=0" 67 echo " --per_file_copt=^.*source/common/protobuf/.*\.cc\$@-fsanitize-coverage=0" 68 69# Envoy test code. Disable coverage instrumentation 70 echo " --per_file_copt=^.*test/.*\.cc\$@-fsanitize-coverage=0" 71 72# External dependencies. Disable all instrumentation. 73 echo " --per_file_copt=^.*antlr4_runtimes.*\.cpp\$@-fsanitize-coverage=0,-fno-sanitize=all" 74 echo " --per_file_copt=^.*com_google_protobuf.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 75 echo " --per_file_copt=^.*com_google_absl.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 76 echo " --per_file_copt=^.*googletest.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 77 echo " --per_file_copt=^.*com_github_grpc_grpc.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 78 echo " --per_file_copt=^.*boringssl.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 79 echo " --per_file_copt=^.*com_googlesource_code_re2.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 80 echo " --per_file_copt=^.*upb.*\.cpp\$@-fsanitize-coverage=0,-fno-sanitize=all" 81 echo " --per_file_copt=^.*org_brotli.*\.cpp\$@-fsanitize-coverage=0,-fno-sanitize=all" 82 echo " --per_file_copt=^.*com_google_cel_cpp.*\.cpp\$@-fsanitize-coverage=0,-fno-sanitize=all" 83 echo " --per_file_copt=^.*com_github_jbeder_yaml_cpp.*\.cpp\$@-fsanitize-coverage=0,-fno-sanitize=all" 84 echo " --per_file_copt=^.*proxy_wasm_cpp_host/.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 85 echo " --per_file_copt=^.*com_github_google_libprotobuf_mutator/.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 86 echo " --per_file_copt=^.*com_googlesource_googleurl/.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 87 echo " --per_file_copt=^.*com_lightstep_tracer_cpp/.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 88 89# All protobuf code and code in bazel-out 90 echo " --per_file_copt=^.*\.pb\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 91 echo " --per_file_copt=^.*bazel-out/.*\.cc\$@-fsanitize-coverage=0,-fno-sanitize=all" 92fi 93)" 94 95 96# Benchmark about 3 GB per CPU (10 threads for 28.8 GB RAM) 97# TODO(asraa): Remove deprecation warnings when Envoy and deps moves to C++17 98bazel build --verbose_failures --dynamic_mode=off ${DI} \ 99 --spawn_strategy=standalone --genrule_strategy=standalone \ 100 --local_cpu_resources=HOST_CPUS*0.32 \ 101 --//source/extensions/wasm_runtime/v8:enabled=false \ 102 --build_tag_filters=-no_asan --config=oss-fuzz \ 103 ${EXTRA_BAZEL_FLAGS} \ 104 ${OSS_FUZZ_TARGETS[*]} 105 106# Profiling with coverage requires that we resolve+copy all Bazel symlinks and 107# also remap everything under proc/self/cwd to correspond to Bazel build paths. 108if [ "$SANITIZER" = "coverage" ] 109then 110 # The build invoker looks for sources in $SRC, but it turns out that we need 111 # to not be buried under src/, paths are expected at out/proc/self/cwd by 112 # the profiler. 113 declare -r REMAP_PATH="${OUT}/proc/self/cwd" 114 mkdir -p "${REMAP_PATH}" 115 # For .cc, we only really care about source/ today. 116 rsync -av "${SRC}"/envoy/source "${REMAP_PATH}" 117 rsync -av "${SRC}"/envoy/test "${REMAP_PATH}" 118 # Remove filesystem loop manually. 119 rm -rf "${SRC}"/envoy/bazel-envoy/external/envoy 120 # Clean up symlinks with a missing referrant. 121 find "${SRC}"/envoy/bazel-envoy/external -follow -type l -ls -delete || echo "Symlink cleanup soft fail" 122 rsync -avLk "${SRC}"/envoy/bazel-envoy/external "${REMAP_PATH}" 123 # For .h, and some generated artifacts, we need bazel-out/. Need to heavily 124 # filter out the build objects from bazel-out/. Also need to resolve symlinks, 125 # since they don't make sense outside the build container. 126 declare -r RSYNC_FILTER_ARGS=("--include" "*.h" "--include" "*.cc" "--include" \ 127 "*.hpp" "--include" "*.cpp" "--include" "*.c" "--include" "*/" "--exclude" "*") 128 rsync -avLk "${RSYNC_FILTER_ARGS[@]}" "${SRC}"/envoy/bazel-out "${REMAP_PATH}" 129 rsync -avLkR "${RSYNC_FILTER_ARGS[@]}" "${HOME}" "${OUT}" 130 rsync -avLkR "${RSYNC_FILTER_ARGS[@]}" /tmp "${OUT}" 131fi 132 133for oss_fuzz_archive in $(find bazel-bin/ -name '*_oss_fuzz.tar'); do 134 tar -xvf "${oss_fuzz_archive}" -C "${OUT}" 135done 136 137# Cleanup bazel- symlinks to avoid oss-fuzz trying to copy out of the build 138# cache. 139rm -f bazel-* 140