build.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/bin/bash -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="/opt/qemu-oss-fuzz" \
  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. if [ "$GITLAB_CI" != "true" ]; then
  64. for i in $(ldd ./qemu-fuzz-i386 | cut -f3 -d' '); do
  65. cp "$i" "$DEST_DIR/lib/"
  66. done
  67. rm qemu-fuzz-i386
  68. # Build a second time to build the final binary with correct rpath
  69. ../configure --disable-werror --cc="$CC" --cxx="$CXX" --enable-fuzzing \
  70. --prefix="/opt/qemu-oss-fuzz" \
  71. --extra-cflags="$EXTRA_CFLAGS" --extra-ldflags="-Wl,-rpath,\$ORIGIN/lib" \
  72. --target-list="i386-softmmu"
  73. make "-j$(nproc)" qemu-fuzz-i386 V=1
  74. fi
  75. # Place data files in the preinstall tree
  76. make install DESTDIR=$DEST_DIR/qemu-bundle
  77. rm -rf $DEST_DIR/qemu-bundle/opt/qemu-oss-fuzz/bin
  78. rm -rf $DEST_DIR/qemu-bundle/opt/qemu-oss-fuzz/libexec
  79. targets=$(./qemu-fuzz-i386 | grep generic-fuzz | awk '$1 ~ /\*/ {print $2}')
  80. base_copy="$DEST_DIR/qemu-fuzz-i386-target-$(echo "$targets" | head -n 1)"
  81. cp "./qemu-fuzz-i386" "$base_copy"
  82. # Run the fuzzer with no arguments, to print the help-string and get the list
  83. # of available fuzz-targets. Copy over the qemu-fuzz-i386, naming it according
  84. # to each available fuzz target (See 05509c8e6d fuzz: select fuzz target using
  85. # executable name)
  86. for target in $(echo "$targets" | tail -n +2);
  87. do
  88. # Ignore the generic-fuzz target, as it requires some environment variables
  89. # to be configured. We have some generic-fuzz-{pc-q35, floppy, ...} targets
  90. # that are thin wrappers around this target that set the required
  91. # environment variables according to predefined configs.
  92. if [[ $target == "generic-fuzz-"* ]]; then
  93. ln $base_copy \
  94. "$DEST_DIR/qemu-fuzz-i386-target-$target"
  95. fi
  96. done
  97. echo "Done. The fuzzers are located in $DEST_DIR"
  98. exit 0