1#!/bin/sh -e 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 \ 23# $LIB_FUZZING_ENGINE /path/to/library.a 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 57if [ -z ${LIB_FUZZING_ENGINE+x} ]; then 58 LIB_FUZZING_ENGINE="-fsanitize=fuzzer" 59fi 60 61if [ -z ${OUT+x} ]; then 62 DEST_DIR=$(realpath "./DEST_DIR") 63else 64 DEST_DIR=$OUT 65fi 66 67mkdir -p "$DEST_DIR/lib/" # Copy the shared libraries here 68 69# Build once to get the list of dynamic lib paths, and copy them over 70../configure --disable-werror --cc="$CC" --cxx="$CXX" \ 71 --extra-cflags="$EXTRA_CFLAGS" --target-list="i386-softmmu" 72 73if ! make CONFIG_FUZZ=y CFLAGS="$LIB_FUZZING_ENGINE" "-j$(nproc)" \ 74 i386-softmmu/fuzz; then 75 fatal "Build failed. Please specify a compiler with fuzzing support"\ 76 "using the \$CC and \$CXX environemnt variables, or specify a"\ 77 "\$LIB_FUZZING_ENGINE compatible with your compiler"\ 78 "\nFor example: CC=clang CXX=clang++ $0" 79fi 80 81for i in $(ldd ./i386-softmmu/qemu-fuzz-i386 | cut -f3 -d' '); do 82 cp "$i" "$DEST_DIR/lib/" 83done 84rm ./i386-softmmu/qemu-fuzz-i386 85 86# Build a second time to build the final binary with correct rpath 87../configure --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" --disable-werror \ 88 --cc="$CC" --cxx="$CXX" --extra-cflags="$EXTRA_CFLAGS" \ 89 --extra-ldflags="-Wl,-rpath,'\$\$ORIGIN/lib'" 90make CONFIG_FUZZ=y CFLAGS="$LIB_FUZZING_ENGINE" "-j$(nproc)" i386-softmmu/fuzz 91 92# Copy over the datadir 93cp -r ../pc-bios/ "$DEST_DIR/pc-bios" 94 95# Run the fuzzer with no arguments, to print the help-string and get the list 96# of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according 97# to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using 98# executable name) 99for target in $(./i386-softmmu/qemu-fuzz-i386 | awk '$1 ~ /\*/ {print $2}'); 100do 101 cp ./i386-softmmu/qemu-fuzz-i386 "$DEST_DIR/qemu-fuzz-i386-target-$target" 102done 103 104echo "Done. The fuzzers are located in $DEST_DIR" 105exit 0 106