2020-06-12 08:51:45 +03:00
|
|
|
#!/bin/sh -e
|
|
|
|
#
|
|
|
|
# OSS-Fuzz build script. See:
|
|
|
|
# https://google.github.io/oss-fuzz/getting-started/new-project-guide/#buildsh
|
|
|
|
#
|
|
|
|
# The file is consumed by:
|
|
|
|
# https://github.com/google/oss-fuzz/blob/master/projects/qemu/Dockerfiles
|
|
|
|
#
|
|
|
|
# This code is licensed under the GPL version 2 or later. See
|
|
|
|
# the COPYING file in the top-level directory.
|
|
|
|
#
|
|
|
|
|
|
|
|
# build project
|
|
|
|
# e.g.
|
|
|
|
# ./autogen.sh
|
|
|
|
# ./configure
|
|
|
|
# make -j$(nproc) all
|
|
|
|
|
|
|
|
# build fuzzers
|
|
|
|
# e.g.
|
|
|
|
# $CXX $CXXFLAGS -std=c++11 -Iinclude \
|
|
|
|
# /path/to/name_of_fuzzer.cc -o $OUT/name_of_fuzzer \
|
2020-08-09 18:17:00 +03:00
|
|
|
# -fsanitize=fuzzer /path/to/library.a
|
2020-06-12 08:51:45 +03:00
|
|
|
|
|
|
|
fatal () {
|
|
|
|
echo "Error : ${*}, exiting."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
OSS_FUZZ_BUILD_DIR="./build-oss-fuzz/"
|
|
|
|
|
|
|
|
# There seems to be a bug in clang-11 (used for builds on oss-fuzz) :
|
|
|
|
# accel/tcg/cputlb.o: In function `load_memop':
|
|
|
|
# accel/tcg/cputlb.c:1505: undefined reference to `qemu_build_not_reached'
|
|
|
|
#
|
|
|
|
# When building with optimization, the compiler is expected to prove that the
|
|
|
|
# statement cannot be reached, and remove it. For some reason clang-11 doesn't
|
|
|
|
# remove it, resulting in an unresolved reference to qemu_build_not_reached
|
|
|
|
# Undefine the __OPTIMIZE__ macro which compiler.h relies on to choose whether
|
|
|
|
# to " #define qemu_build_not_reached() g_assert_not_reached() "
|
|
|
|
EXTRA_CFLAGS="$CFLAGS -U __OPTIMIZE__"
|
|
|
|
|
|
|
|
if ! { [ -e "./COPYING" ] &&
|
|
|
|
[ -e "./MAINTAINERS" ] &&
|
|
|
|
[ -e "./Makefile" ] &&
|
|
|
|
[ -e "./docs" ] &&
|
|
|
|
[ -e "./VERSION" ] &&
|
|
|
|
[ -e "./linux-user" ] &&
|
|
|
|
[ -e "./softmmu" ];} ; then
|
|
|
|
fatal "Please run the script from the top of the QEMU tree"
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p $OSS_FUZZ_BUILD_DIR || fatal "mkdir $OSS_FUZZ_BUILD_DIR failed"
|
|
|
|
cd $OSS_FUZZ_BUILD_DIR || fatal "cd $OSS_FUZZ_BUILD_DIR failed"
|
|
|
|
|
|
|
|
|
|
|
|
if [ -z ${OUT+x} ]; then
|
|
|
|
DEST_DIR=$(realpath "./DEST_DIR")
|
|
|
|
else
|
|
|
|
DEST_DIR=$OUT
|
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir -p "$DEST_DIR/lib/" # Copy the shared libraries here
|
|
|
|
|
|
|
|
# Build once to get the list of dynamic lib paths, and copy them over
|
2020-08-09 18:17:00 +03:00
|
|
|
../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
|
2020-08-03 18:04:25 +03:00
|
|
|
--prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
|
2020-07-17 09:57:42 +03:00
|
|
|
--extra-cflags="$EXTRA_CFLAGS" --target-list="i386-softmmu"
|
2020-06-12 08:51:45 +03:00
|
|
|
|
2020-08-03 18:04:25 +03:00
|
|
|
if ! make "-j$(nproc)" qemu-fuzz-i386; then
|
2020-06-12 08:51:45 +03:00
|
|
|
fatal "Build failed. Please specify a compiler with fuzzing support"\
|
2020-09-17 10:50:23 +03:00
|
|
|
"using the \$CC and \$CXX environment variables"\
|
2020-06-12 08:51:45 +03:00
|
|
|
"\nFor example: CC=clang CXX=clang++ $0"
|
|
|
|
fi
|
|
|
|
|
2020-08-03 18:04:25 +03:00
|
|
|
for i in $(ldd ./qemu-fuzz-i386 | cut -f3 -d' '); do
|
2020-06-12 08:51:45 +03:00
|
|
|
cp "$i" "$DEST_DIR/lib/"
|
|
|
|
done
|
2020-08-03 18:04:25 +03:00
|
|
|
rm qemu-fuzz-i386
|
2020-06-12 08:51:45 +03:00
|
|
|
|
|
|
|
# Build a second time to build the final binary with correct rpath
|
2020-08-03 18:04:25 +03:00
|
|
|
../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
|
|
|
|
--prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
|
2020-09-02 17:26:57 +03:00
|
|
|
--extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="-Wl,-rpath,\$ORIGIN/lib" \
|
2020-08-03 18:04:25 +03:00
|
|
|
--target-list="i386-softmmu"
|
|
|
|
make "-j$(nproc)" qemu-fuzz-i386 V=1
|
2020-06-12 08:51:45 +03:00
|
|
|
|
|
|
|
# Copy over the datadir
|
|
|
|
cp -r ../pc-bios/ "$DEST_DIR/pc-bios"
|
|
|
|
|
|
|
|
# Run the fuzzer with no arguments, to print the help-string and get the list
|
|
|
|
# of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according
|
|
|
|
# to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using
|
|
|
|
# executable name)
|
2020-08-03 18:04:25 +03:00
|
|
|
for target in $(./qemu-fuzz-i386 | awk '$1 ~ /\*/ {print $2}');
|
2020-06-12 08:51:45 +03:00
|
|
|
do
|
2020-08-03 18:04:25 +03:00
|
|
|
cp qemu-fuzz-i386 "$DEST_DIR/qemu-fuzz-i386-target-$target"
|
2020-06-12 08:51:45 +03:00
|
|
|
done
|
|
|
|
|
|
|
|
echo "Done. The fuzzers are located in $DEST_DIR"
|
|
|
|
exit 0
|