hw/arm/smmuv3: Fix potential integer overflow (CID 1432363)
[qemu/ar7.git] / scripts / oss-fuzz / build.sh
blobfcae4a0c26e766d7597f653119db065518dab330
1 #!/bin/sh -e
3 # OSS-Fuzz build script. See:
4 # https://google.github.io/oss-fuzz/getting-started/new-project-guide/#buildsh
6 # The file is consumed by:
7 # https://github.com/google/oss-fuzz/blob/master/projects/qemu/Dockerfiles
9 # This code is licensed under the GPL version 2 or later. See
10 # the COPYING file in the top-level directory.
13 # build project
14 # e.g.
15 # ./autogen.sh
16 # ./configure
17 # make -j$(nproc) all
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 \
23 # -fsanitize=fuzzer /path/to/library.a
25 fatal () {
26 echo "Error : ${*}, exiting."
27 exit 1
30 OSS_FUZZ_BUILD_DIR="./build-oss-fuzz/"
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'
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() "
41 EXTRA_CFLAGS="$CFLAGS -U __OPTIMIZE__"
43 if ! { [ -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"
53 mkdir -p $OSS_FUZZ_BUILD_DIR || fatal "mkdir $OSS_FUZZ_BUILD_DIR failed"
54 cd $OSS_FUZZ_BUILD_DIR || fatal "cd $OSS_FUZZ_BUILD_DIR failed"
57 if [ -z ${OUT+x} ]; then
58 DEST_DIR=$(realpath "./DEST_DIR")
59 else
60 DEST_DIR=$OUT
63 mkdir -p "$DEST_DIR/lib/" # Copy the shared libraries here
65 mkdir -p "$DEST_DIR/bin/" # Copy executables that shouldn't
66 # be treated as fuzzers by oss-fuzz here
68 # Build once to get the list of dynamic lib paths, and copy them over
69 ../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
70 --prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
71 --extra-cflags="$EXTRA_CFLAGS" --target-list="i386-softmmu"
73 if ! make "-j$(nproc)" qemu-fuzz-i386; then
74 fatal "Build failed. Please specify a compiler with fuzzing support"\
75 "using the \$CC and \$CXX environment variables"\
76 "\nFor example: CC=clang CXX=clang++ $0"
79 for i in $(ldd ./qemu-fuzz-i386 | cut -f3 -d' '); do
80 cp "$i" "$DEST_DIR/lib/"
81 done
82 rm qemu-fuzz-i386
84 # Build a second time to build the final binary with correct rpath
85 ../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
86 --prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
87 --extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="-Wl,-rpath,\$ORIGIN/lib" \
88 --target-list="i386-softmmu"
89 make "-j$(nproc)" qemu-fuzz-i386 V=1
91 # Copy over the datadir
92 cp -r ../pc-bios/ "$DEST_DIR/pc-bios"
94 cp "./qemu-fuzz-i386" "$DEST_DIR/bin/"
96 # Run the fuzzer with no arguments, to print the help-string and get the list
97 # of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according
98 # to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using
99 # executable name)
100 for target in $(./qemu-fuzz-i386 | awk '$1 ~ /\*/ {print $2}');
102 # Ignore the generic-fuzz target, as it requires some environment variables
103 # to be configured. We have some generic-fuzz-{pc-q35, floppy, ...} targets
104 # that are thin wrappers around this target that set the required
105 # environment variables according to predefined configs.
106 if [ "$target" != "generic-fuzz" ]; then
107 ln "$DEST_DIR/bin/qemu-fuzz-i386" \
108 "$DEST_DIR/qemu-fuzz-i386-target-$target"
110 done
112 echo "Done. The fuzzers are located in $DEST_DIR"
113 exit 0