1#!/bin/bash 2set -eux 3 4SANITIZER=${SANITIZER:-address} 5flags="-O1 -fno-omit-frame-pointer -g -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=$SANITIZER -fsanitize=fuzzer-no-link" 6 7export CC=${CC:-clang} 8export CFLAGS=${CFLAGS:-$flags} 9 10export CXX=${CXX:-clang++} 11export CXXFLAGS=${CXXFLAGS:-$flags} 12 13cd "$(dirname -- "$0")/.." 14 15export OUT=${OUT:-"$(pwd)/out"} 16mkdir -p "$OUT" 17 18export LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer} 19 20# Ideally libbelf should be built using release tarballs available 21# at https://sourceware.org/elfutils/ftp/. Unfortunately sometimes they 22# fail to compile (for example, elfutils-0.185 fails to compile with LDFLAGS enabled 23# due to https://bugs.gentoo.org/794601) so let's just point the script to 24# commits referring to versions of libelf that actually can be built 25rm -rf elfutils 26git clone git://sourceware.org/git/elfutils.git 27( 28cd elfutils 29git checkout 983e86fd89e8bf02f2d27ba5dce5bf078af4ceda 30git log --oneline -1 31 32# ASan isn't compatible with -Wl,--no-undefined: https://github.com/google/sanitizers/issues/380 33find -name Makefile.am | xargs sed -i 's/,--no-undefined//' 34 35# ASan isn't compatible with -Wl,-z,defs either: 36# https://clang.llvm.org/docs/AddressSanitizer.html#usage 37sed -i 's/^\(ZDEFS_LDFLAGS=\).*/\1/' configure.ac 38 39 40autoreconf -i -f 41if ! ./configure --enable-maintainer-mode --disable-debuginfod --disable-libdebuginfod \ 42 CC="$CC" CFLAGS="-Wno-error $CFLAGS" CXX="$CXX" CXXFLAGS="-Wno-error $CXXFLAGS" LDFLAGS="$CFLAGS"; then 43 cat config.log 44 exit 1 45fi 46 47make -C config -j$(nproc) V=1 48make -C lib -j$(nproc) V=1 49make -C libelf -j$(nproc) V=1 50) 51 52make -C src BUILD_STATIC_ONLY=y V=1 clean 53make -C src -j$(nproc) CFLAGS="-I$(pwd)/elfutils/libelf $CFLAGS" BUILD_STATIC_ONLY=y V=1 54 55$CC $CFLAGS -Isrc -Iinclude -Iinclude/uapi -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c fuzz/bpf-object-fuzzer.c -o bpf-object-fuzzer.o 56$CXX $CXXFLAGS $LIB_FUZZING_ENGINE bpf-object-fuzzer.o src/libbpf.a "$(pwd)/elfutils/libelf/libelf.a" -l:libz.a -o "$OUT/bpf-object-fuzzer" 57 58cp fuzz/bpf-object-fuzzer_seed_corpus.zip "$OUT" 59