2016-11-18 19:44:37 +00:00
|
|
|
#!/bin/bash -eu
|
2016-07-20 21:58:10 +00:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
################################################################################
|
2016-08-06 17:29:42 +00:00
|
|
|
|
2016-08-08 02:53:25 +00:00
|
|
|
echo "---------------------------------------------------------------"
|
|
|
|
|
2022-06-09 09:39:25 +00:00
|
|
|
# This is a temporary fix: fall back to LLVM14's old pass manager
|
2022-06-09 13:07:23 +00:00
|
|
|
if [ -n "${OLD_LLVMPASS-}" ]; then
|
2022-06-09 09:39:25 +00:00
|
|
|
export SANITIZER_FLAGS_introspector=$(echo $SANITIZER_FLAGS_introspector | sed -r 's/-O0/-flegacy-pass-manager/')
|
|
|
|
fi
|
|
|
|
|
2021-02-16 15:06:58 +00:00
|
|
|
if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
|
2022-07-20 17:26:43 +00:00
|
|
|
if [ "$FUZZING_ENGINE" != "libfuzzer" ] && [ "$FUZZING_ENGINE" != "wycheproof" ]; then
|
|
|
|
echo "ERROR: JVM projects can be fuzzed with libFuzzer or tested with wycheproof engines only."
|
2021-02-16 15:06:58 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-12-18 23:51:44 +00:00
|
|
|
if [ "$SANITIZER" != "address" ] && [ "$SANITIZER" != "coverage" ] && [ "$SANITIZER" != "undefined" ] && [ "$SANITIZER" != "none" ] && [ "$SANITIZER" != "introspector" ]; then
|
|
|
|
echo "ERROR: JVM projects can be fuzzed with AddressSanitizer or UndefinedBehaviorSanitizer or Introspector only."
|
2021-02-16 15:06:58 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ "$ARCHITECTURE" != "x86_64" ]; then
|
|
|
|
echo "ERROR: JVM projects can be fuzzed on x86_64 architecture only."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-11-20 06:41:12 +00:00
|
|
|
if [ "$FUZZING_LANGUAGE" = "python" ]; then
|
|
|
|
if [ "$FUZZING_ENGINE" != "libfuzzer" ]; then
|
|
|
|
echo "ERROR: Python projects can be fuzzed with libFuzzer engine only."
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-11-01 18:51:50 +00:00
|
|
|
if [ "$SANITIZER" != "address" ] && [ "$SANITIZER" != "undefined" ] && [ "$SANITIZER" != "coverage" ] && [ "$SANITIZER" != "introspector" ]; then
|
|
|
|
echo "ERROR: Python projects can be fuzzed with AddressSanitizer or UndefinedBehaviorSanitizer or Coverage or Fuzz Introspector only."
|
2020-11-20 06:41:12 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ "$ARCHITECTURE" != "x86_64" ]; then
|
|
|
|
echo "ERROR: Python projects can be fuzzed on x86_64 architecture only."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-12-27 17:13:07 +00:00
|
|
|
if [ -z "${SANITIZER_FLAGS-}" ]; then
|
2016-12-02 18:58:51 +00:00
|
|
|
FLAGS_VAR="SANITIZER_FLAGS_${SANITIZER}"
|
2017-05-08 18:17:27 +00:00
|
|
|
export SANITIZER_FLAGS=${!FLAGS_VAR-}
|
2016-12-02 18:58:51 +00:00
|
|
|
fi
|
|
|
|
|
2019-08-12 17:54:18 +00:00
|
|
|
if [[ $ARCHITECTURE == "i386" ]]; then
|
2019-05-13 22:01:25 +00:00
|
|
|
export CFLAGS="-m32 $CFLAGS"
|
2021-09-20 17:00:35 +00:00
|
|
|
cp -R /usr/i386/lib/* /usr/local/lib
|
2019-05-13 22:01:25 +00:00
|
|
|
fi
|
2021-02-16 15:06:58 +00:00
|
|
|
# JVM projects are fuzzed with Jazzer, which has libFuzzer built in.
|
|
|
|
if [[ $FUZZING_ENGINE != "none" ]] && [[ $FUZZING_LANGUAGE != "jvm" ]]; then
|
2018-06-26 20:08:14 +00:00
|
|
|
# compile script might override environment, use . to call it.
|
|
|
|
. compile_${FUZZING_ENGINE}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $SANITIZER_FLAGS = *sanitize=memory* ]]
|
|
|
|
then
|
2018-01-11 01:56:33 +00:00
|
|
|
# Take all libraries from lib/msan and MSAN_LIBS_PATH
|
2016-12-16 04:57:07 +00:00
|
|
|
# export CXXFLAGS_EXTRA="-L/usr/msan/lib $CXXFLAGS_EXTRA"
|
2021-09-20 17:00:35 +00:00
|
|
|
cp -R /usr/msan/lib/* /usr/local/lib/
|
2018-01-11 01:56:33 +00:00
|
|
|
|
2021-08-25 15:36:05 +00:00
|
|
|
echo 'Building without MSan instrumented libraries.'
|
2016-08-15 22:24:03 +00:00
|
|
|
fi
|
|
|
|
|
2017-05-08 18:17:27 +00:00
|
|
|
# Coverage flag overrides.
|
2017-10-21 00:15:02 +00:00
|
|
|
COVERAGE_FLAGS_VAR="COVERAGE_FLAGS_${SANITIZER}"
|
2018-06-26 20:08:14 +00:00
|
|
|
if [[ -n ${!COVERAGE_FLAGS_VAR+x} ]]
|
|
|
|
then
|
2017-05-08 18:17:27 +00:00
|
|
|
export COVERAGE_FLAGS="${!COVERAGE_FLAGS_VAR}"
|
|
|
|
fi
|
|
|
|
|
2021-02-02 05:16:56 +00:00
|
|
|
# Don't need coverage instrumentation for engine-less, afl++ builds.
|
|
|
|
if [ $FUZZING_ENGINE = "none" ] || [ $FUZZING_ENGINE = "afl" ]; then
|
2017-10-31 23:11:00 +00:00
|
|
|
export COVERAGE_FLAGS=
|
|
|
|
fi
|
|
|
|
|
2020-05-21 17:16:52 +00:00
|
|
|
# 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.
|
2020-05-25 19:55:58 +00:00
|
|
|
if [ "$SANITIZER" != "undefined" ] && [ "$SANITIZER" != "coverage" ] && [ "$ARCHITECTURE" != 'i386' ]; then
|
2020-05-22 00:37:02 +00:00
|
|
|
export RUSTFLAGS="--cfg fuzzing -Zsanitizer=${SANITIZER} -Cdebuginfo=1 -Cforce-frame-pointers"
|
|
|
|
else
|
|
|
|
export RUSTFLAGS="--cfg fuzzing -Cdebuginfo=1 -Cforce-frame-pointers"
|
|
|
|
fi
|
2021-03-08 15:05:35 +00:00
|
|
|
if [ "$SANITIZER" = "coverage" ]
|
|
|
|
then
|
|
|
|
# link to C++ from comment in f5098035eb1a14aa966c8651d88ea3d64323823d
|
|
|
|
export RUSTFLAGS="$RUSTFLAGS -Zinstrument-coverage -C link-arg=-lc++"
|
|
|
|
fi
|
2020-05-17 23:45:54 +00:00
|
|
|
|
2020-05-21 17:16:52 +00:00
|
|
|
# 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++
|
2020-05-17 23:45:54 +00:00
|
|
|
|
2020-05-21 17:16:52 +00:00
|
|
|
export CFLAGS="$CFLAGS $SANITIZER_FLAGS $COVERAGE_FLAGS"
|
|
|
|
export CXXFLAGS="$CFLAGS $CXXFLAGS_EXTRA"
|
2016-07-22 21:05:53 +00:00
|
|
|
|
2020-11-20 19:10:51 +00:00
|
|
|
if [ "$FUZZING_LANGUAGE" = "python" ]; then
|
2021-08-16 15:56:54 +00:00
|
|
|
sanitizer_with_fuzzer_lib_dir=`python3 -c "import atheris; import os; print(atheris.path())"`
|
2020-12-11 19:38:43 +00:00
|
|
|
sanitizer_with_fuzzer_output_lib=$OUT/sanitizer_with_fuzzer.so
|
|
|
|
if [ "$SANITIZER" = "address" ]; then
|
|
|
|
cp $sanitizer_with_fuzzer_lib_dir/asan_with_fuzzer.so $sanitizer_with_fuzzer_output_lib
|
|
|
|
elif [ "$SANITIZER" = "undefined" ]; then
|
2020-12-11 22:35:43 +00:00
|
|
|
cp $sanitizer_with_fuzzer_lib_dir/ubsan_with_fuzzer.so $sanitizer_with_fuzzer_output_lib
|
2020-12-11 19:38:43 +00:00
|
|
|
fi
|
2020-11-20 19:10:51 +00:00
|
|
|
|
|
|
|
# Disable leak checking as it is unsupported.
|
2020-12-11 22:35:43 +00:00
|
|
|
export CFLAGS="$CFLAGS -fno-sanitize=function,leak,vptr,"
|
|
|
|
export CXXFLAGS="$CXXFLAGS -fno-sanitize=function,leak,vptr"
|
2020-11-20 19:10:51 +00:00
|
|
|
fi
|
|
|
|
|
2020-12-13 02:58:59 +00:00
|
|
|
# Copy latest llvm-symbolizer in $OUT for stack symbolization.
|
|
|
|
cp $(which llvm-symbolizer) $OUT/
|
|
|
|
|
2021-02-16 14:56:36 +00:00
|
|
|
# Copy Jazzer to $OUT if needed.
|
|
|
|
if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
|
2021-06-10 14:57:42 +00:00
|
|
|
cp $(which jazzer_agent_deploy.jar) $(which jazzer_driver) $OUT/
|
|
|
|
jazzer_driver_with_sanitizer=$OUT/jazzer_driver_with_sanitizer
|
|
|
|
if [ "$SANITIZER" = "address" ]; then
|
2022-11-24 02:21:20 +00:00
|
|
|
cp $(which jazzer_driver_asan) $jazzer_driver_with_sanitizer
|
2021-06-10 14:57:42 +00:00
|
|
|
elif [ "$SANITIZER" = "undefined" ]; then
|
2022-11-24 02:21:20 +00:00
|
|
|
cp $(which jazzer_driver_ubsan) $jazzer_driver_with_sanitizer
|
2021-06-19 17:59:46 +00:00
|
|
|
elif [ "$SANITIZER" = "coverage" ]; then
|
|
|
|
# Coverage builds require no instrumentation.
|
|
|
|
cp $(which jazzer_driver) $jazzer_driver_with_sanitizer
|
2021-06-10 14:57:42 +00:00
|
|
|
fi
|
2021-06-23 15:22:40 +00:00
|
|
|
|
|
|
|
# Disable leak checking since the JVM triggers too many false positives.
|
2021-08-11 17:35:29 +00:00
|
|
|
export CFLAGS="$CFLAGS -fno-sanitize=leak"
|
|
|
|
export CXXFLAGS="$CXXFLAGS -fno-sanitize=leak"
|
2021-02-16 14:56:36 +00:00
|
|
|
fi
|
|
|
|
|
2022-01-20 01:22:27 +00:00
|
|
|
if [ "$SANITIZER" = "introspector" ]; then
|
|
|
|
export AR=llvm-ar
|
2022-04-11 17:04:38 +00:00
|
|
|
export NM=llvm-nm
|
2022-01-20 01:22:27 +00:00
|
|
|
export RANLIB=llvm-ranlib
|
|
|
|
|
2022-05-24 06:10:38 +00:00
|
|
|
export CFLAGS="$CFLAGS -g"
|
|
|
|
export CXXFLAGS="$CXXFLAGS -g"
|
|
|
|
export FI_BRANCH_PROFILE=1
|
2022-10-28 19:06:15 +00:00
|
|
|
export FUZZ_INTROSPECTOR=1
|
2022-05-24 06:10:38 +00:00
|
|
|
|
2022-01-20 01:22:27 +00:00
|
|
|
# Move ar and ranlib
|
|
|
|
mv /usr/bin/ar /usr/bin/old-ar
|
2022-04-11 17:04:38 +00:00
|
|
|
mv /usr/bin/nm /usr/bin/old-nm
|
2022-01-20 01:22:27 +00:00
|
|
|
mv /usr/bin/ranlib /usr/bin/old-ranlib
|
|
|
|
|
|
|
|
ln -sf /usr/local/bin/llvm-ar /usr/bin/ar
|
2022-04-11 17:04:38 +00:00
|
|
|
ln -sf /usr/local/bin/llvm-nm /usr/bin/nm
|
2022-01-20 01:22:27 +00:00
|
|
|
ln -sf /usr/local/bin/llvm-ranlib /usr/bin/ranlib
|
|
|
|
fi
|
|
|
|
|
2020-05-21 17:16:52 +00:00
|
|
|
echo "---------------------------------------------------------------"
|
2016-07-22 21:05:53 +00:00
|
|
|
echo "CC=$CC"
|
|
|
|
echo "CXX=$CXX"
|
|
|
|
echo "CFLAGS=$CFLAGS"
|
|
|
|
echo "CXXFLAGS=$CXXFLAGS"
|
2021-06-02 18:51:26 +00:00
|
|
|
echo "RUSTFLAGS=$RUSTFLAGS"
|
2016-08-08 02:53:25 +00:00
|
|
|
echo "---------------------------------------------------------------"
|
|
|
|
|
2016-12-27 17:39:59 +00:00
|
|
|
BUILD_CMD="bash -eux $SRC/build.sh"
|
2018-06-14 22:00:46 +00:00
|
|
|
|
2021-08-27 20:00:03 +00:00
|
|
|
# Set +u temporarily to continue even if GOPATH and OSSFUZZ_RUSTPATH are undefined.
|
|
|
|
set +u
|
2018-06-14 22:00:46 +00:00
|
|
|
# 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.
|
2021-05-17 18:55:53 +00:00
|
|
|
COPY_SOURCES_CMD="cp -rL --parents $SRC $WORK /usr/include /usr/local/include $GOPATH $OSSFUZZ_RUSTPATH /rustc $OUT"
|
2021-08-27 20:00:03 +00:00
|
|
|
set -u
|
2021-05-17 18:55:53 +00:00
|
|
|
|
2021-08-27 20:00:03 +00:00
|
|
|
if [ "$FUZZING_LANGUAGE" = "rust" ]; then
|
|
|
|
# Copy rust std lib to its path with a hash.
|
|
|
|
export rustch=`rustc --version --verbose | grep commit-hash | cut -d' ' -f2`
|
|
|
|
mkdir -p /rustc/$rustch/
|
|
|
|
cp -r /rust/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/ /rustc/$rustch/
|
|
|
|
fi
|
2018-06-14 22:00:46 +00:00
|
|
|
|
2017-03-04 00:37:19 +00:00
|
|
|
if [ "${BUILD_UID-0}" -ne "0" ]; then
|
2017-02-11 15:57:49 +00:00
|
|
|
adduser -u $BUILD_UID --disabled-password --gecos '' builder
|
2016-12-27 21:47:44 +00:00
|
|
|
chown -R builder $SRC $OUT $WORK
|
2016-12-21 23:01:44 +00:00
|
|
|
su -c "$BUILD_CMD" builder
|
2018-10-01 13:43:21 +00:00
|
|
|
if [ "$SANITIZER" = "coverage" ]; then
|
2018-08-22 14:20:09 +00:00
|
|
|
# Some directories have broken symlinks (e.g. honggfuzz), ignore the errors.
|
2019-01-14 21:01:16 +00:00
|
|
|
su -c "$COPY_SOURCES_CMD" builder 2>/dev/null || true
|
2018-06-14 22:00:46 +00:00
|
|
|
fi
|
2016-12-21 23:01:44 +00:00
|
|
|
else
|
|
|
|
$BUILD_CMD
|
2018-10-01 13:43:21 +00:00
|
|
|
if [ "$SANITIZER" = "coverage" ]; then
|
2018-08-22 14:20:09 +00:00
|
|
|
# Some directories have broken symlinks (e.g. honggfuzz), ignore the errors.
|
2019-01-14 21:01:16 +00:00
|
|
|
$COPY_SOURCES_CMD 2>/dev/null || true
|
2018-06-14 22:00:46 +00:00
|
|
|
fi
|
2016-12-21 23:01:44 +00:00
|
|
|
fi
|
2019-06-10 17:00:20 +00:00
|
|
|
|
2022-01-20 01:22:27 +00:00
|
|
|
if [ "$SANITIZER" = "introspector" ]; then
|
|
|
|
unset CXXFLAGS
|
|
|
|
unset CFLAGS
|
2022-12-11 06:08:49 +00:00
|
|
|
export G_ANALYTICS_TAG="G-8WTFM1Y62J"
|
2022-01-20 01:22:27 +00:00
|
|
|
apt-get install -y libjpeg-dev zlib1g-dev
|
|
|
|
pip3 install --upgrade setuptools
|
2022-11-02 19:54:18 +00:00
|
|
|
pip3 install cxxfilt pyyaml beautifulsoup4 lxml soupsieve
|
|
|
|
pip3 install --prefer-binary matplotlib
|
2022-01-20 01:22:27 +00:00
|
|
|
|
2022-12-18 23:51:44 +00:00
|
|
|
if [ "$FUZZING_LANGUAGE" = "jvm" ]; then
|
|
|
|
echo "GOING jvm route"
|
|
|
|
|
|
|
|
set -x
|
|
|
|
# Output will be put in /out/
|
|
|
|
python3 /fuzz-introspector/frontends/java/oss-fuzz-main.py
|
|
|
|
# Move files temporarily to fit workflow of other languages.
|
|
|
|
mkdir -p $SRC/my-fi-data
|
|
|
|
find $OUT/ -name *.data -exec mv {} $SRC/my-fi-data/ \;
|
|
|
|
find $OUT/ -name *.data.yaml -exec mv {} $SRC/my-fi-data/ \;
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p $SRC/inspector
|
2022-11-01 18:51:50 +00:00
|
|
|
find $SRC/ -name "fuzzerLogFile-*.data" -exec cp {} $SRC/inspector/ \;
|
|
|
|
find $SRC/ -name "fuzzerLogFile-*.data.yaml" -exec cp {} $SRC/inspector/ \;
|
2022-12-18 23:51:44 +00:00
|
|
|
|
2022-01-20 01:22:27 +00:00
|
|
|
# Move coverage report.
|
2022-01-31 00:39:11 +00:00
|
|
|
if [ -d "$OUT/textcov_reports" ]
|
|
|
|
then
|
2022-11-08 22:24:41 +00:00
|
|
|
find $OUT/textcov_reports/ -name "*.covreport" -exec cp {} $SRC/inspector/ \;
|
|
|
|
find $OUT/textcov_reports/ -name "*.json" -exec cp {} $SRC/inspector/ \;
|
2022-05-24 06:10:38 +00:00
|
|
|
fi
|
|
|
|
|
2022-01-20 01:22:27 +00:00
|
|
|
cd $SRC/inspector
|
2022-04-04 22:38:37 +00:00
|
|
|
|
2022-11-21 15:21:59 +00:00
|
|
|
# Make fuzz-introspector HTML report.
|
2022-12-09 19:27:15 +00:00
|
|
|
REPORT_ARGS="--name=$PROJECT_NAME"
|
2022-11-21 15:21:59 +00:00
|
|
|
# Only pass coverage_url when COVERAGE_URL is set (in cloud builds)
|
|
|
|
if [[ ! -z "${COVERAGE_URL+x}" ]]; then
|
|
|
|
REPORT_ARGS="$REPORT_ARGS --coverage_url=${COVERAGE_URL}"
|
|
|
|
fi
|
|
|
|
|
2022-11-01 18:51:50 +00:00
|
|
|
# Do different things depending on languages
|
|
|
|
if [ "$FUZZING_LANGUAGE" = "python" ]; then
|
|
|
|
echo "GOING python route"
|
|
|
|
set -x
|
2022-11-21 15:21:59 +00:00
|
|
|
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
|
2022-11-01 18:51:50 +00:00
|
|
|
REPORT_ARGS="$REPORT_ARGS --language=python"
|
|
|
|
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
|
|
|
|
cp -rf $SRC/inspector $OUT/inspector
|
2022-12-18 23:51:44 +00:00
|
|
|
elif [ "$FUZZING_LANGUAGE" = "jvm" ]; then
|
|
|
|
echo "GOING jvm route"
|
|
|
|
set -x
|
|
|
|
find $OUT/ -name "jacoco.xml" -exec cp {} $SRC/inspector/ \;
|
|
|
|
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
|
|
|
|
REPORT_ARGS="$REPORT_ARGS --language=jvm"
|
|
|
|
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
|
|
|
|
cp -rf $SRC/inspector $OUT/inspector
|
2022-11-01 18:51:50 +00:00
|
|
|
else
|
|
|
|
# C/C++
|
|
|
|
|
|
|
|
# Correlate fuzzer binaries to fuzz-introspector's raw data
|
|
|
|
python3 /fuzz-introspector/src/main.py correlate --binaries_dir=$OUT/
|
|
|
|
|
|
|
|
# Generate fuzz-introspector HTML report, this generates
|
|
|
|
# the file exe_to_fuzz_introspector_logs.yaml
|
2022-11-21 15:21:59 +00:00
|
|
|
REPORT_ARGS="$REPORT_ARGS --target_dir=$SRC/inspector"
|
2022-11-01 18:51:50 +00:00
|
|
|
# Use the just-generated correlation file
|
|
|
|
REPORT_ARGS="$REPORT_ARGS --correlation_file=exe_to_fuzz_introspector_logs.yaml"
|
2022-12-18 23:51:44 +00:00
|
|
|
python3 /fuzz-introspector/src/main.py report $REPORT_ARGS
|
2022-11-01 18:51:50 +00:00
|
|
|
|
|
|
|
cp -rf $SRC/inspector $OUT/inspector
|
2022-04-04 22:38:37 +00:00
|
|
|
fi
|
2022-01-20 01:22:27 +00:00
|
|
|
fi
|