aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/base-images/base-builder/compile
blob: 8e0342caaf2e726f22e3107e3aa0437d890ac88a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash -eu
# Copyright 2016 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.
#
################################################################################

echo "---------------------------------------------------------------"

if [ "$SANITIZER" = "dataflow" ] && [ "$FUZZING_ENGINE" != "dataflow" ]; then
  echo "ERROR: 'dataflow' sanitizer can be used with 'dataflow' engine only."
  exit 1
fi

if [ -z "${SANITIZER_FLAGS-}" ]; then
  FLAGS_VAR="SANITIZER_FLAGS_${SANITIZER}"
  export SANITIZER_FLAGS=${!FLAGS_VAR-}
fi

if [[ $ARCHITECTURE == "i386" ]]; then
    export CFLAGS="-m32 $CFLAGS"
    cp -R /usr/i386/lib/* /usr/lib
fi
if [[ $FUZZING_ENGINE != "none" ]]; then
  # compile script might override environment, use . to call it.
  . compile_${FUZZING_ENGINE}
fi

if [[ $SANITIZER_FLAGS = *sanitize=memory* ]]
then
  # Take all libraries from lib/msan and MSAN_LIBS_PATH
  # export CXXFLAGS_EXTRA="-L/usr/msan/lib $CXXFLAGS_EXTRA"
  cp -R /usr/msan/lib/* /usr/lib/

  if [[ -z "${MSAN_LIBS_PATH-}" ]]; then
    echo 'WARNING: Building without MSan instrumented libraries.'
  else
    # Copy all static libraries only. Don't include .so files because they can
    # break non MSan compiled programs.
    (cd "$MSAN_LIBS_PATH" && find . -name '*.a' -exec cp --parents '{}' / ';')
  fi
fi

# Coverage flag overrides.
COVERAGE_FLAGS_VAR="COVERAGE_FLAGS_${SANITIZER}"
if [[ -n ${!COVERAGE_FLAGS_VAR+x} ]]
then
  export COVERAGE_FLAGS="${!COVERAGE_FLAGS_VAR}"
fi

 # Don't need coverage instrumentation for engine-less builds.
if [ $FUZZING_ENGINE = "none" ]; then
  export COVERAGE_FLAGS=
fi

# Rust does not support sanitizers and coverage flags via CFLAGS/CXXFLAGS, so
# use RUSTFLAGS.
# FIXME: Support code coverage once support is in.
# See https://github.com/rust-lang/rust/issues/34701.
if [ "$SANITIZER" != "undefined" ] && [ "$ARCHITECTURE" != 'i386' ]; then
  export RUSTFLAGS="--cfg fuzzing -Zsanitizer=${SANITIZER} -Cdebuginfo=1 -Cforce-frame-pointers"
else
  export RUSTFLAGS="--cfg fuzzing -Cdebuginfo=1 -Cforce-frame-pointers"
fi

# Add Rust libfuzzer flags.
# See https://github.com/rust-fuzz/libfuzzer/blob/master/build.rs#L12.
export CUSTOM_LIBFUZZER_PATH="$LIB_FUZZING_ENGINE_DEPRECATED"
export CUSTOM_LIBFUZZER_STD_CXX=c++

export CFLAGS="$CFLAGS $SANITIZER_FLAGS $COVERAGE_FLAGS"
export CXXFLAGS="$CFLAGS $CXXFLAGS_EXTRA"

echo "---------------------------------------------------------------"
echo "CC=$CC"
echo "CXX=$CXX"
echo "CFLAGS=$CFLAGS"
echo "CXXFLAGS=$CXXFLAGS"
echo "---------------------------------------------------------------"

BUILD_CMD="bash -eux $SRC/build.sh"

# We need to preserve source code files for generating a code coverage report.
# We need exact files that were compiled, so copy both $SRC and $WORK dirs.
COPY_SOURCES_CMD="cp -rL --parents $SRC $WORK /usr/include /usr/local/include $OUT"

if [ "${BUILD_UID-0}" -ne "0" ]; then
  adduser -u $BUILD_UID --disabled-password --gecos '' builder
  chown -R builder $SRC $OUT $WORK
  su -c "$BUILD_CMD" builder
  if [ "$SANITIZER" = "coverage" ]; then
    # Some directories have broken symlinks (e.g. honggfuzz), ignore the errors.
    su -c "$COPY_SOURCES_CMD" builder 2>/dev/null || true
  fi
else
  $BUILD_CMD
  if [ "$SANITIZER" = "coverage" ]; then
    # Some directories have broken symlinks (e.g. honggfuzz), ignore the errors.
    $COPY_SOURCES_CMD 2>/dev/null || true
  fi
fi

if [[ "$FUZZING_ENGINE" = "dataflow" ]]; then
  # Remove seed corpus as it can be huge but is not needed for a dataflow build.
  rm -f $OUT/*.zip
fi