2018-02-02 17:17:53 +00:00
|
|
|
#!/bin/bash -eu
|
|
|
|
# Copyright 2018 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.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
|
2018-02-03 16:33:30 +00:00
|
|
|
# Disable UBSan vptr since target built with -fno-rtti.
|
|
|
|
export CFLAGS="$CFLAGS -fno-sanitize=vptr"
|
|
|
|
export CXXFLAGS="$CXXFLAGS -fno-sanitize=vptr"
|
|
|
|
|
2018-04-19 20:48:58 +00:00
|
|
|
declare -r FUZZER_TARGETS_CC=$(find . -name *_fuzz_test.cc)
|
|
|
|
declare -r FUZZER_TARGETS="$(for t in ${FUZZER_TARGETS_CC}; do echo "${t:2:-3}"; done)"
|
2018-02-02 17:17:53 +00:00
|
|
|
|
|
|
|
FUZZER_DICTIONARIES="\
|
|
|
|
"
|
|
|
|
|
|
|
|
# Skip gperftools, ASAN runs don't use tcmalloc.
|
|
|
|
export DISABLE_GPERFTOOLS_BUILD=1
|
2019-02-05 19:10:44 +00:00
|
|
|
sed -i 's#envoy_dependencies()#envoy_dependencies(skip_targets=["tcmalloc_and_profiler","tcmalloc_debug"])#' WORKSPACE
|
2018-02-02 17:17:53 +00:00
|
|
|
|
2018-02-27 21:20:28 +00:00
|
|
|
# Copy $CFLAGS and $CXXFLAGS into Bazel command-line flags, for both
|
|
|
|
# compilation and linking.
|
|
|
|
#
|
|
|
|
# Some flags, such as `-stdlib=libc++`, generate warnings if used on a C source
|
|
|
|
# file. Since the build runs with `-Werror` this will cause it to break, so we
|
|
|
|
# use `--conlyopt` and `--cxxopt` instead of `--copt`.
|
2019-02-05 19:10:44 +00:00
|
|
|
#
|
|
|
|
# While we shouldn't need to set --host_linkopt, it turns out that some builds
|
|
|
|
# with host toolchains, e.g. protobuf, pickup the fact that we're doing ASAN for
|
|
|
|
# the target when building libraries but don't cleanly handle the host link for
|
|
|
|
# build tools (protoc). It seems somewhat harmless to be building protoc ASAN.
|
2018-02-27 21:20:28 +00:00
|
|
|
declare -r EXTRA_BAZEL_FLAGS="$(
|
|
|
|
for f in ${CFLAGS}; do
|
2019-02-05 19:10:44 +00:00
|
|
|
echo "--conlyopt=${f}" "--linkopt=${f}" "--host_linkopt=${f}"
|
2018-02-27 21:20:28 +00:00
|
|
|
done
|
|
|
|
for f in ${CXXFLAGS}; do
|
2019-02-05 19:10:44 +00:00
|
|
|
echo "--cxxopt=${f}" "--linkopt=${f}" "--host_linkopt=${f}"
|
2018-02-27 21:20:28 +00:00
|
|
|
done
|
|
|
|
)"
|
|
|
|
|
2018-08-15 13:43:21 +00:00
|
|
|
declare BAZEL_BUILD_TARGETS=""
|
2018-11-02 14:41:32 +00:00
|
|
|
declare BAZEL_CORPUS_TARGETS=""
|
2018-08-15 13:43:21 +00:00
|
|
|
declare FILTERED_FUZZER_TARGETS=""
|
|
|
|
for t in ${FUZZER_TARGETS}
|
|
|
|
do
|
|
|
|
declare BAZEL_PATH="//"$(dirname "$t")":"$(basename "$t")
|
|
|
|
declare TAGGED=$(bazel query "attr('tags', 'no_fuzz', ${BAZEL_PATH})")
|
|
|
|
if [ -z "${TAGGED}" ]
|
|
|
|
then
|
|
|
|
FILTERED_FUZZER_TARGETS+="$t "
|
|
|
|
BAZEL_BUILD_TARGETS+="${BAZEL_PATH}_driverless "
|
2018-11-02 14:41:32 +00:00
|
|
|
BAZEL_CORPUS_TARGETS+="${BAZEL_PATH}_corpus_tar "
|
2018-08-15 13:43:21 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Build driverless libraries.
|
2019-02-25 15:07:27 +00:00
|
|
|
# TODO(htuch): Remove the CC/CXX/CFLAGS/CXXFLAGS passing, this is only there for
|
|
|
|
# cmake_external limitation in understanding --cxxopt etc., it should not be
|
|
|
|
# necessary once
|
|
|
|
# https://github.com/bazelbuild/rules_foreign_cc/issues/154#issuecomment-466504751
|
|
|
|
# is resolved and we cleanup libc++ support in the main repo.
|
2018-02-02 17:17:53 +00:00
|
|
|
bazel build --verbose_failures --dynamic_mode=off --spawn_strategy=standalone \
|
|
|
|
--genrule_strategy=standalone --strip=never \
|
2018-08-09 18:25:15 +00:00
|
|
|
--copt=-fno-sanitize=vptr --linkopt=-fno-sanitize=vptr --linkopt=-lc++fs \
|
2018-02-02 17:17:53 +00:00
|
|
|
--define tcmalloc=disabled --define signal_trace=disabled \
|
|
|
|
--define ENVOY_CONFIG_ASAN=1 --copt -D__SANITIZE_ADDRESS__ \
|
|
|
|
--define force_libcpp=enabled \
|
2019-02-25 15:07:27 +00:00
|
|
|
--action_env CC \
|
|
|
|
--action_env CXX \
|
|
|
|
--action_env CFLAGS \
|
|
|
|
--action_env CXXFLAGS \
|
2018-08-15 13:43:21 +00:00
|
|
|
--build_tag_filters=-no_asan \
|
2018-02-02 17:17:53 +00:00
|
|
|
${EXTRA_BAZEL_FLAGS} \
|
2018-11-02 14:41:32 +00:00
|
|
|
${BAZEL_BUILD_TARGETS[*]} ${BAZEL_CORPUS_TARGETS[*]}
|
2018-02-02 17:17:53 +00:00
|
|
|
|
2018-08-17 00:07:56 +00:00
|
|
|
# Profiling with coverage requires that we resolve+copy all Bazel symlinks and
|
|
|
|
# also remap everything under proc/self/cwd to correspond to Bazel build paths.
|
2018-10-01 13:43:21 +00:00
|
|
|
if [ "$SANITIZER" = "coverage" ]
|
2018-08-17 00:07:56 +00:00
|
|
|
then
|
2018-08-17 13:50:01 +00:00
|
|
|
# The build invoker looks for sources in $SRC, but it turns out that we need
|
|
|
|
# to not be buried under src/, paths are expected at out/proc/self/cwd by
|
|
|
|
# the profiler.
|
|
|
|
declare -r REMAP_PATH="${OUT}/proc/self/cwd"
|
2018-08-17 00:07:56 +00:00
|
|
|
mkdir -p "${REMAP_PATH}"
|
|
|
|
# For .cc, we only really care about source/ today.
|
|
|
|
rsync -av "${SRC}"/envoy/source "${REMAP_PATH}"
|
2018-08-30 03:50:53 +00:00
|
|
|
rsync -av "${SRC}"/envoy/test "${REMAP_PATH}"
|
2018-11-14 17:53:41 +00:00
|
|
|
# Clean up symlinks with a missing referrant.
|
|
|
|
find "${SRC}"/envoy/bazel-envoy/external -follow -type l -ls -delete || echo "Symlink cleanup soft fail"
|
2018-08-30 03:50:53 +00:00
|
|
|
rsync -avLk "${SRC}"/envoy/bazel-envoy/external "${REMAP_PATH}"
|
2018-08-17 00:07:56 +00:00
|
|
|
# For .h, and some generated artifacts, we need bazel-out/. Need to heavily
|
|
|
|
# filter out the build objects from bazel-out/. Also need to resolve symlinks,
|
|
|
|
# since they don't make sense outside the build container.
|
2019-02-25 15:07:27 +00:00
|
|
|
declare -r RSYNC_FILTER_ARGS=("--include" "*.h" "--include" "*.cc" "--include" \
|
|
|
|
"*.hpp" "--include" "*.cpp" "--include" "*.c" "--include" "*/" "--exclude" "*")
|
|
|
|
rsync -avLk "${RSYNC_FILTER_ARGS[@]}" "${SRC}"/envoy/bazel-out "${REMAP_PATH}"
|
2019-03-01 04:59:45 +00:00
|
|
|
rsync -avLkR "${RSYNC_FILTER_ARGS[@]}" "${HOME}" "${OUT}"
|
2019-03-01 14:41:20 +00:00
|
|
|
rsync -avLkR "${RSYNC_FILTER_ARGS[@]}" /tmp "${OUT}"
|
2018-08-17 00:07:56 +00:00
|
|
|
fi
|
|
|
|
|
2018-11-02 14:41:32 +00:00
|
|
|
# Copy out test driverless binaries from bazel-bin/.
|
2018-08-15 13:43:21 +00:00
|
|
|
for t in ${FILTERED_FUZZER_TARGETS}
|
2018-02-02 17:17:53 +00:00
|
|
|
do
|
2018-04-19 20:48:58 +00:00
|
|
|
TARGET_BASE="$(expr "$t" : '.*/\(.*\)_fuzz_test')"
|
2018-08-15 13:43:21 +00:00
|
|
|
TARGET_DRIVERLESS=bazel-bin/"${t}"_driverless
|
2018-11-02 14:41:32 +00:00
|
|
|
echo "Copying fuzzer $t"
|
2018-08-15 13:43:21 +00:00
|
|
|
cp "${TARGET_DRIVERLESS}" "${OUT}"/"${TARGET_BASE}"_fuzz_test
|
2018-11-02 14:41:32 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Zip up related test corpuses.
|
|
|
|
# TODO(htuch): just use the .tar directly when
|
|
|
|
# https://github.com/google/oss-fuzz/issues/1918 is fixed.
|
|
|
|
CORPUS_UNTAR_PATH="${PWD}"/_tmp_corpus
|
|
|
|
for t in ${FILTERED_FUZZER_TARGETS}
|
|
|
|
do
|
|
|
|
echo "Extracting and zipping fuzzer $t corpus"
|
|
|
|
rm -rf "${CORPUS_UNTAR_PATH}"
|
|
|
|
mkdir -p "${CORPUS_UNTAR_PATH}"
|
|
|
|
tar -C "${CORPUS_UNTAR_PATH}" -xvf bazel-bin/"${t}"_corpus_tar.tar
|
|
|
|
TARGET_BASE="$(expr "$t" : '.*/\(.*\)_fuzz_test')"
|
2018-02-02 17:17:53 +00:00
|
|
|
zip "${OUT}/${TARGET_BASE}"_fuzz_test_seed_corpus.zip \
|
2018-11-02 14:41:32 +00:00
|
|
|
"${CORPUS_UNTAR_PATH}"/*
|
2018-02-02 17:17:53 +00:00
|
|
|
done
|
2018-11-02 14:41:32 +00:00
|
|
|
rm -rf "${CORPUS_UNTAR_PATH}"
|
2018-02-02 17:17:53 +00:00
|
|
|
|
|
|
|
# Copy dictionaries and options files to $OUT/
|
|
|
|
for d in $FUZZER_DICTIONARIES; do
|
|
|
|
cp "$d" "${OUT}"/
|
|
|
|
done
|
2018-08-28 20:04:13 +00:00
|
|
|
|
|
|
|
# Cleanup bazel- symlinks to avoid oss-fuzz trying to copy out of the build
|
|
|
|
# cache.
|
|
|
|
rm -f bazel-*
|