• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18# Force Python3, run configure.py to pick the right build config
19PYTHON=python3
20yes "" | ${PYTHON} configure.py
21
22# Since Bazel passes flags to compilers via `--copt`, `--conlyopt` and
23# `--cxxopt`, we need to move all flags from `$CFLAGS` and `$CXXFLAGS` to these.
24# We don't use `--copt` as warnings issued by C compilers when encountering a
25# C++-only option results in errors during build.
26#
27# Note: Make sure that by this line `$CFLAGS` and `$CXXFLAGS` are properly set
28# up as further changes to them won't be visible to Bazel.
29#
30# Note: for builds using the undefined behavior sanitizer we need to link
31# `clang_rt` ubsan library. Since Bazel uses `clang` for linking instead of
32# `clang++`, we need to add the additional `--linkopt` flag.
33# See issue: https://github.com/bazelbuild/bazel/issues/8777
34declare -r EXTRA_FLAGS="\
35$(
36for f in ${CFLAGS}; do
37  echo "--conlyopt=${f}" "--linkopt=${f}"
38done
39for f in ${CXXFLAGS}; do
40    echo "--cxxopt=${f}" "--linkopt=${f}"
41done
42if [ "$SANITIZER" = "undefined" ]
43then
44  echo "--linkopt=$(find $(llvm-config --libdir) -name libclang_rt.ubsan_standalone_cxx-x86_64.a | head -1)"
45fi
46)"
47
48# Determine all fuzz targets. To control what gets fuzzed with OSSFuzz, all
49# supported fuzzers are in `//tensorflow/security/fuzzing`.
50# Ignore fuzzers tagged with `no_oss` in opensource.
51declare -r FUZZERS=$(bazel query 'kind(cc_.*, tests(//tensorflow/security/fuzzing/...)) - attr(tags, no_oss, kind(cc_.*, tests(//tensorflow/security/fuzzing/...)))')
52
53# Build the fuzzer targets.
54# Pass in `--config=libc++` to link against libc++.
55# Pass in `--verbose_failures` so it is easy to debug compile crashes.
56# Pass in `--strip=never` to ensure coverage support.
57# Pass in `$LIB_FUZZING_ENGINE` to `--copt` and `--linkopt` to ensure we have a
58# `main` symbol defined (all these fuzzers build without a `main` and by default
59# `$CFLAGS` and `CXXFLAGS` compile with `-fsanitize=fuzzer-no-link`).
60# Since we have `assert` in fuzzers, make sure `NDEBUG` is not defined
61bazel build \
62  --config=libc++ \
63  ${EXTRA_FLAGS} \
64  --verbose_failures \
65  --strip=never \
66  --copt=${LIB_FUZZING_ENGINE} \
67  --linkopt=${LIB_FUZZING_ENGINE} \
68  --copt='-UNDEBUG' \
69  -- ${FUZZERS}
70
71# The fuzzers built above are in the `bazel-bin/` symlink. But they need to be
72# in `$OUT`, so move them accordingly.
73for bazel_target in ${FUZZERS}; do
74  colon_index=$(expr index "${bazel_target}" ":")
75  fuzz_name="${bazel_target:$colon_index}"
76  bazel_location="bazel-bin/${bazel_target/:/\/}"
77  cp ${bazel_location} ${OUT}/$fuzz_name
78done
79
80# For coverage, we need to remap source files to correspond to the Bazel build
81# paths. We also need to resolve all symlinks that Bazel creates.
82if [ "$SANITIZER" = "coverage" ]
83then
84  declare -r RSYNC_CMD="rsync -aLkR"
85  declare -r REMAP_PATH=${OUT}/proc/self/cwd/
86  mkdir -p ${REMAP_PATH}
87
88  # Sync existing code.
89  ${RSYNC_CMD} tensorflow/ ${REMAP_PATH}
90
91  # Sync generated proto files.
92  ${RSYNC_CMD} ./bazel-out/k8-opt/bin/tensorflow/core/protobuf ${REMAP_PATH}
93
94  # Sync external dependencies. We don't need to include `bazel-tensorflow`.
95  # Also, remove `external/org_tensorflow` which is a copy of the entire source
96  # code that Bazel creates. Not removing this would cause `rsync` to expand a
97  # symlink that ends up pointing to itself!
98  pushd bazel-tensorflow
99  [[ -e external/org_tensorflow ]] && unlink external/org_tensorflow
100  ${RSYNC_CMD} external/ ${REMAP_PATH}
101  popd
102fi
103
104# Finally, make sure we don't accidentally run with stuff from the bazel cache.
105rm -f bazel-*
106