]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/oss-fuzz/build.sh
linux-user: Add emulation for MADV_WIPEONFORK and MADV_KEEPONFORK in madvise()
[mirror_qemu.git] / scripts / oss-fuzz / build.sh
CommitLineData
0e76929d 1#!/bin/bash -e
211635b3
AB
2#
3# OSS-Fuzz build script. See:
4# https://google.github.io/oss-fuzz/getting-started/new-project-guide/#buildsh
5#
6# The file is consumed by:
7# https://github.com/google/oss-fuzz/blob/master/projects/qemu/Dockerfiles
8#
9# This code is licensed under the GPL version 2 or later. See
10# the COPYING file in the top-level directory.
11#
12
13# build project
14# e.g.
15# ./autogen.sh
16# ./configure
17# make -j$(nproc) all
18
19# build fuzzers
20# e.g.
21# $CXX $CXXFLAGS -std=c++11 -Iinclude \
22# /path/to/name_of_fuzzer.cc -o $OUT/name_of_fuzzer \
77afc75f 23# -fsanitize=fuzzer /path/to/library.a
211635b3
AB
24
25fatal () {
26 echo "Error : ${*}, exiting."
27 exit 1
28}
29
30OSS_FUZZ_BUILD_DIR="./build-oss-fuzz/"
31
32# There seems to be a bug in clang-11 (used for builds on oss-fuzz) :
33# accel/tcg/cputlb.o: In function `load_memop':
34# accel/tcg/cputlb.c:1505: undefined reference to `qemu_build_not_reached'
35#
36# When building with optimization, the compiler is expected to prove that the
37# statement cannot be reached, and remove it. For some reason clang-11 doesn't
38# remove it, resulting in an unresolved reference to qemu_build_not_reached
39# Undefine the __OPTIMIZE__ macro which compiler.h relies on to choose whether
40# to " #define qemu_build_not_reached() g_assert_not_reached() "
41EXTRA_CFLAGS="$CFLAGS -U __OPTIMIZE__"
42
43if ! { [ -e "./COPYING" ] &&
44 [ -e "./MAINTAINERS" ] &&
45 [ -e "./Makefile" ] &&
46 [ -e "./docs" ] &&
47 [ -e "./VERSION" ] &&
48 [ -e "./linux-user" ] &&
49 [ -e "./softmmu" ];} ; then
50 fatal "Please run the script from the top of the QEMU tree"
51fi
52
53mkdir -p $OSS_FUZZ_BUILD_DIR || fatal "mkdir $OSS_FUZZ_BUILD_DIR failed"
54cd $OSS_FUZZ_BUILD_DIR || fatal "cd $OSS_FUZZ_BUILD_DIR failed"
55
56
211635b3
AB
57if [ -z ${OUT+x} ]; then
58 DEST_DIR=$(realpath "./DEST_DIR")
59else
60 DEST_DIR=$OUT
61fi
62
63mkdir -p "$DEST_DIR/lib/" # Copy the shared libraries here
64
65# Build once to get the list of dynamic lib paths, and copy them over
77afc75f 66../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
882084a0 67 --prefix="/opt/qemu-oss-fuzz" \
7cee363b 68 --extra-cflags="$EXTRA_CFLAGS" --target-list="i386-softmmu"
211635b3 69
64ed6f92 70if ! make "-j$(nproc)" qemu-fuzz-i386; then
211635b3 71 fatal "Build failed. Please specify a compiler with fuzzing support"\
65fdb3cc 72 "using the \$CC and \$CXX environment variables"\
211635b3
AB
73 "\nFor example: CC=clang CXX=clang++ $0"
74fi
75
3973e7ae
AB
76if [ "$GITLAB_CI" != "true" ]; then
77 for i in $(ldd ./qemu-fuzz-i386 | cut -f3 -d' '); do
78 cp "$i" "$DEST_DIR/lib/"
79 done
80 rm qemu-fuzz-i386
211635b3 81
3973e7ae
AB
82 # Build a second time to build the final binary with correct rpath
83 ../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
882084a0 84 --prefix="/opt/qemu-oss-fuzz" \
3973e7ae
AB
85 --extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="-Wl,-rpath,\$ORIGIN/lib" \
86 --target-list="i386-softmmu"
87 make "-j$(nproc)" qemu-fuzz-i386 V=1
88fi
211635b3 89
d5b50236 90# Place data files in the preinstall tree
882084a0 91make install DESTDIR=$DEST_DIR/qemu-bundle
d5b50236
PB
92rm -rf $DEST_DIR/qemu-bundle/opt/qemu-oss-fuzz/bin
93rm -rf $DEST_DIR/qemu-bundle/opt/qemu-oss-fuzz/libexec
211635b3 94
7906f11e 95targets=$(./qemu-fuzz-i386 | grep generic-fuzz | awk '$1 ~ /\*/ {print $2}')
bb451d24
AB
96base_copy="$DEST_DIR/qemu-fuzz-i386-target-$(echo "$targets" | head -n 1)"
97
98cp "./qemu-fuzz-i386" "$base_copy"
a942f64c 99
211635b3
AB
100# Run the fuzzer with no arguments, to print the help-string and get the list
101# of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according
102# to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using
103# executable name)
bb451d24 104for target in $(echo "$targets" | tail -n +2);
211635b3 105do
53e1a50d
AB
106 # Ignore the generic-fuzz target, as it requires some environment variables
107 # to be configured. We have some generic-fuzz-{pc-q35, floppy, ...} targets
108 # that are thin wrappers around this target that set the required
109 # environment variables according to predefined configs.
0e76929d 110 if [[ $target == "generic-fuzz-"* ]]; then
bb451d24 111 ln $base_copy \
53e1a50d
AB
112 "$DEST_DIR/qemu-fuzz-i386-target-$target"
113 fi
211635b3
AB
114done
115
116echo "Done. The fuzzers are located in $DEST_DIR"
117exit 0