build.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. # build project
  13. # e.g.
  14. # ./autogen.sh
  15. # ./configure
  16. # make -j$(nproc) all
  17. # build fuzzers
  18. # e.g.
  19. # $CXX $CXXFLAGS -std=c++11 -Iinclude \
  20. # /path/to/name_of_fuzzer.cc -o $OUT/name_of_fuzzer \
  21. # -fsanitize=fuzzer /path/to/library.a
  22. fatal () {
  23. echo "Error : ${*}, exiting."
  24. exit 1
  25. }
  26. OSS_FUZZ_BUILD_DIR="./build-oss-fuzz/"
  27. # There seems to be a bug in clang-11 (used for builds on oss-fuzz) :
  28. # accel/tcg/cputlb.o: In function `load_memop':
  29. # accel/tcg/cputlb.c:1505: undefined reference to `qemu_build_not_reached'
  30. #
  31. # When building with optimization, the compiler is expected to prove that the
  32. # statement cannot be reached, and remove it. For some reason clang-11 doesn't
  33. # remove it, resulting in an unresolved reference to qemu_build_not_reached
  34. # Undefine the __OPTIMIZE__ macro which compiler.h relies on to choose whether
  35. # to " #define qemu_build_not_reached() g_assert_not_reached() "
  36. EXTRA_CFLAGS="$CFLAGS -U __OPTIMIZE__"
  37. if ! { [ -e "./COPYING" ] &&
  38. [ -e "./MAINTAINERS" ] &&
  39. [ -e "./Makefile" ] &&
  40. [ -e "./docs" ] &&
  41. [ -e "./VERSION" ] &&
  42. [ -e "./linux-user" ] &&
  43. [ -e "./softmmu" ];} ; then
  44. fatal "Please run the script from the top of the QEMU tree"
  45. fi
  46. mkdir -p $OSS_FUZZ_BUILD_DIR || fatal "mkdir $OSS_FUZZ_BUILD_DIR failed"
  47. cd $OSS_FUZZ_BUILD_DIR || fatal "cd $OSS_FUZZ_BUILD_DIR failed"
  48. if [ -z ${OUT+x} ]; then
  49. DEST_DIR=$(realpath "./DEST_DIR")
  50. else
  51. DEST_DIR=$OUT
  52. fi
  53. mkdir -p "$DEST_DIR/lib/" # Copy the shared libraries here
  54. # Build once to get the list of dynamic lib paths, and copy them over
  55. ../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
  56. --prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
  57. --extra-cflags="$EXTRA_CFLAGS" --target-list="i386-softmmu"
  58. if ! make "-j$(nproc)" qemu-fuzz-i386; then
  59. fatal "Build failed. Please specify a compiler with fuzzing support"\
  60. "using the \$CC and \$CXX environment variables"\
  61. "\nFor example: CC=clang CXX=clang++ $0"
  62. fi
  63. for i in $(ldd ./qemu-fuzz-i386 | cut -f3 -d' '); do
  64. cp "$i" "$DEST_DIR/lib/"
  65. done
  66. rm qemu-fuzz-i386
  67. # Build a second time to build the final binary with correct rpath
  68. ../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
  69. --prefix="$DEST_DIR" --bindir="$DEST_DIR" --datadir="$DEST_DIR/data/" \
  70. --extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="-Wl,-rpath,\$ORIGIN/lib" \
  71. --target-list="i386-softmmu"
  72. make "-j$(nproc)" qemu-fuzz-i386 V=1
  73. # Copy over the datadir
  74. cp -r ../pc-bios/ "$DEST_DIR/pc-bios"
  75. # Run the fuzzer with no arguments, to print the help-string and get the list
  76. # of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according
  77. # to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using
  78. # executable name)
  79. for target in $(./qemu-fuzz-i386 | awk '$1 ~ /\*/ {print $2}');
  80. do
  81. cp qemu-fuzz-i386 "$DEST_DIR/qemu-fuzz-i386-target-$target"
  82. done
  83. echo "Done. The fuzzers are located in $DEST_DIR"
  84. exit 0