From: Thomas Huth <thuth@redhat.com>
To: peter.maydell@linaro.org, qemu-devel@nongnu.org
Cc: Alexander Bulekov <alxndr@bu.edu>
Subject: [PULL 4/7] fuzz: add oss-fuzz build-script
Date: Tue, 16 Jun 2020 10:58:10 +0200 [thread overview]
Message-ID: <20200616085813.29296-5-thuth@redhat.com> (raw)
In-Reply-To: <20200616085813.29296-1-thuth@redhat.com>
From: Alexander Bulekov <alxndr@bu.edu>
It is neater to keep this in the QEMU repo, since any change that
requires an update to the oss-fuzz build configuration, can make the
necessary changes in the same series.
Suggested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
Message-Id: <20200612055145.12101-1-alxndr@bu.edu>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
MAINTAINERS | 1 +
scripts/oss-fuzz/build.sh | 105 ++++++++++++++++++++++++++++++++++++++
2 files changed, 106 insertions(+)
create mode 100755 scripts/oss-fuzz/build.sh
diff --git a/MAINTAINERS b/MAINTAINERS
index a922775e45..9ec50e165d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2339,6 +2339,7 @@ R: Bandan Das <bsd@redhat.com>
R: Stefan Hajnoczi <stefanha@redhat.com>
S: Maintained
F: tests/qtest/fuzz/
+F: scripts/oss-fuzz/
Register API
M: Alistair Francis <alistair@alistair23.me>
diff --git a/scripts/oss-fuzz/build.sh b/scripts/oss-fuzz/build.sh
new file mode 100755
index 0000000000..f5cee3d67e
--- /dev/null
+++ b/scripts/oss-fuzz/build.sh
@@ -0,0 +1,105 @@
+#!/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 \
+# $LIB_FUZZING_ENGINE /path/to/library.a
+
+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 ${LIB_FUZZING_ENGINE+x} ]; then
+ LIB_FUZZING_ENGINE="-fsanitize=fuzzer"
+fi
+
+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
+../configure --disable-werror --cc="$CC" --cxx="$CXX" \
+ --extra-cflags="$EXTRA_CFLAGS"
+
+if ! make CONFIG_FUZZ=y CFLAGS="$LIB_FUZZING_ENGINE" "-j$(nproc)" \
+ i386-softmmu/fuzz; then
+ fatal "Build failed. Please specify a compiler with fuzzing support"\
+ "using the \$CC and \$CXX environemnt variables, or specify a"\
+ "\$LIB_FUZZING_ENGINE compatible with your compiler"\
+ "\nFor example: CC=clang CXX=clang++ $0"
+fi
+
+for i in $(ldd ./i386-softmmu/qemu-fuzz-i386 | cut -f3 -d' '); do
+ cp "$i" "$DEST_DIR/lib/"
+done
+rm ./i386-softmmu/qemu-fuzz-i386
+
+# Build a second time to build the final binary with correct rpath
+../configure --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" --disable-werror \
+ --cc="$CC" --cxx="$CXX" --extra-cflags="$EXTRA_CFLAGS" \
+ --extra-ldflags="-Wl,-rpath,'\$\$ORIGIN/lib'"
+make CONFIG_FUZZ=y CFLAGS="$LIB_FUZZING_ENGINE" "-j$(nproc)" i386-softmmu/fuzz
+
+# 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)
+for target in $(./i386-softmmu/qemu-fuzz-i386 | awk '$1 ~ /\*/ {print $2}');
+do
+ cp ./i386-softmmu/qemu-fuzz-i386 "$DEST_DIR/qemu-fuzz-i386-target-$target"
+done
+
+echo "Done. The fuzzers are located in $DEST_DIR"
+exit 0
--
2.18.1
next prev parent reply other threads:[~2020-06-16 9:02 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-16 8:58 [PULL 0/7] fuzzing and other test-related patches Thomas Huth
2020-06-16 8:58 ` [PULL 1/7] bios-tables-test: Fix "-tpmdev: invalid option" Thomas Huth
2020-06-16 8:58 ` [PULL 2/7] fuzz: skip QTest serialization Thomas Huth
2020-06-16 8:58 ` [PULL 3/7] fuzz: Add support for logging QTest commands Thomas Huth
2020-06-16 8:58 ` Thomas Huth [this message]
2020-06-16 8:58 ` [PULL 5/7] tests/qtest: Fix LGPL information in the file headers Thomas Huth
2020-06-16 8:58 ` [PULL 6/7] tests/acceptance: Add boot tests for sh4 QEMU advent calendar image Thomas Huth
2020-06-16 8:58 ` [PULL 7/7] configure: Let SLOF be initialized by ./scripts/git-submodule.sh Thomas Huth
2020-06-16 10:00 ` [PULL 0/7] fuzzing and other test-related patches Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200616085813.29296-5-thuth@redhat.com \
--to=thuth@redhat.com \
--cc=alxndr@bu.edu \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).