1*f7c14bbaSAndroid Build Coastguard Worker#!/bin/bash 2*f7c14bbaSAndroid Build Coastguard Workerset -eux 3*f7c14bbaSAndroid Build Coastguard Worker 4*f7c14bbaSAndroid Build Coastguard WorkerSANITIZER=${SANITIZER:-address} 5*f7c14bbaSAndroid Build Coastguard Workerflags="-O1 -fno-omit-frame-pointer -g -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=$SANITIZER -fsanitize=fuzzer-no-link" 6*f7c14bbaSAndroid Build Coastguard Worker 7*f7c14bbaSAndroid Build Coastguard Workerexport CC=${CC:-clang} 8*f7c14bbaSAndroid Build Coastguard Workerexport CFLAGS=${CFLAGS:-$flags} 9*f7c14bbaSAndroid Build Coastguard Worker 10*f7c14bbaSAndroid Build Coastguard Workerexport CXX=${CXX:-clang++} 11*f7c14bbaSAndroid Build Coastguard Workerexport CXXFLAGS=${CXXFLAGS:-$flags} 12*f7c14bbaSAndroid Build Coastguard Worker 13*f7c14bbaSAndroid Build Coastguard Workercd "$(dirname -- "$0")/.." 14*f7c14bbaSAndroid Build Coastguard Worker 15*f7c14bbaSAndroid Build Coastguard Workerexport OUT=${OUT:-"$(pwd)/out"} 16*f7c14bbaSAndroid Build Coastguard Workermkdir -p "$OUT" 17*f7c14bbaSAndroid Build Coastguard Worker 18*f7c14bbaSAndroid Build Coastguard Workerexport LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer} 19*f7c14bbaSAndroid Build Coastguard Worker 20*f7c14bbaSAndroid Build Coastguard Worker# libelf is compiled with _FORTIFY_SOURCE by default and it 21*f7c14bbaSAndroid Build Coastguard Worker# isn't compatible with MSan. It was borrowed 22*f7c14bbaSAndroid Build Coastguard Worker# from https://github.com/google/oss-fuzz/pull/7422 23*f7c14bbaSAndroid Build Coastguard Workerif [[ "$SANITIZER" == memory ]]; then 24*f7c14bbaSAndroid Build Coastguard Worker CFLAGS+=" -U_FORTIFY_SOURCE" 25*f7c14bbaSAndroid Build Coastguard Worker CXXFLAGS+=" -U_FORTIFY_SOURCE" 26*f7c14bbaSAndroid Build Coastguard Workerfi 27*f7c14bbaSAndroid Build Coastguard Worker 28*f7c14bbaSAndroid Build Coastguard Worker# The alignment check is turned off by default on OSS-Fuzz/CFLite so it should be 29*f7c14bbaSAndroid Build Coastguard Worker# turned on explicitly there. It was borrowed from 30*f7c14bbaSAndroid Build Coastguard Worker# https://github.com/google/oss-fuzz/pull/7092 31*f7c14bbaSAndroid Build Coastguard Workerif [[ "$SANITIZER" == undefined ]]; then 32*f7c14bbaSAndroid Build Coastguard Worker additional_ubsan_checks=alignment 33*f7c14bbaSAndroid Build Coastguard Worker UBSAN_FLAGS="-fsanitize=$additional_ubsan_checks -fno-sanitize-recover=$additional_ubsan_checks" 34*f7c14bbaSAndroid Build Coastguard Worker CFLAGS+=" $UBSAN_FLAGS" 35*f7c14bbaSAndroid Build Coastguard Worker CXXFLAGS+=" $UBSAN_FLAGS" 36*f7c14bbaSAndroid Build Coastguard Workerfi 37*f7c14bbaSAndroid Build Coastguard Worker 38*f7c14bbaSAndroid Build Coastguard Worker# Ideally libbelf should be built using release tarballs available 39*f7c14bbaSAndroid Build Coastguard Worker# at https://sourceware.org/elfutils/ftp/. Unfortunately sometimes they 40*f7c14bbaSAndroid Build Coastguard Worker# fail to compile (for example, elfutils-0.185 fails to compile with LDFLAGS enabled 41*f7c14bbaSAndroid Build Coastguard Worker# due to https://bugs.gentoo.org/794601) so let's just point the script to 42*f7c14bbaSAndroid Build Coastguard Worker# commits referring to versions of libelf that actually can be built 43*f7c14bbaSAndroid Build Coastguard Workerrm -rf elfutils 44*f7c14bbaSAndroid Build Coastguard Workergit clone https://sourceware.org/git/elfutils.git 45*f7c14bbaSAndroid Build Coastguard Worker( 46*f7c14bbaSAndroid Build Coastguard Workercd elfutils 47*f7c14bbaSAndroid Build Coastguard Workergit checkout 67a187d4c1790058fc7fd218317851cb68bb087c 48*f7c14bbaSAndroid Build Coastguard Workergit log --oneline -1 49*f7c14bbaSAndroid Build Coastguard Worker 50*f7c14bbaSAndroid Build Coastguard Worker# ASan isn't compatible with -Wl,--no-undefined: https://github.com/google/sanitizers/issues/380 51*f7c14bbaSAndroid Build Coastguard Workersed -i 's/^\(NO_UNDEFINED=\).*/\1/' configure.ac 52*f7c14bbaSAndroid Build Coastguard Worker 53*f7c14bbaSAndroid Build Coastguard Worker# ASan isn't compatible with -Wl,-z,defs either: 54*f7c14bbaSAndroid Build Coastguard Worker# https://clang.llvm.org/docs/AddressSanitizer.html#usage 55*f7c14bbaSAndroid Build Coastguard Workersed -i 's/^\(ZDEFS_LDFLAGS=\).*/\1/' configure.ac 56*f7c14bbaSAndroid Build Coastguard Worker 57*f7c14bbaSAndroid Build Coastguard Workerif [[ "$SANITIZER" == undefined ]]; then 58*f7c14bbaSAndroid Build Coastguard Worker # That's basicaly what --enable-sanitize-undefined does to turn off unaligned access 59*f7c14bbaSAndroid Build Coastguard Worker # elfutils heavily relies on on i386/x86_64 but without changing compiler flags along the way 60*f7c14bbaSAndroid Build Coastguard Worker sed -i 's/\(check_undefined_val\)=[0-9]/\1=1/' configure.ac 61*f7c14bbaSAndroid Build Coastguard Workerfi 62*f7c14bbaSAndroid Build Coastguard Worker 63*f7c14bbaSAndroid Build Coastguard Workerautoreconf -i -f 64*f7c14bbaSAndroid Build Coastguard Workerif ! ./configure --enable-maintainer-mode --disable-debuginfod --disable-libdebuginfod \ 65*f7c14bbaSAndroid Build Coastguard Worker --disable-demangler --without-bzlib --without-lzma --without-zstd \ 66*f7c14bbaSAndroid Build Coastguard Worker CC="$CC" CFLAGS="-Wno-error $CFLAGS" CXX="$CXX" CXXFLAGS="-Wno-error $CXXFLAGS" LDFLAGS="$CFLAGS"; then 67*f7c14bbaSAndroid Build Coastguard Worker cat config.log 68*f7c14bbaSAndroid Build Coastguard Worker exit 1 69*f7c14bbaSAndroid Build Coastguard Workerfi 70*f7c14bbaSAndroid Build Coastguard Worker 71*f7c14bbaSAndroid Build Coastguard Workermake -C config -j$(nproc) V=1 72*f7c14bbaSAndroid Build Coastguard Workermake -C lib -j$(nproc) V=1 73*f7c14bbaSAndroid Build Coastguard Workermake -C libelf -j$(nproc) V=1 74*f7c14bbaSAndroid Build Coastguard Worker) 75*f7c14bbaSAndroid Build Coastguard Worker 76*f7c14bbaSAndroid Build Coastguard Workermake -C src BUILD_STATIC_ONLY=y V=1 clean 77*f7c14bbaSAndroid Build Coastguard Workermake -C src -j$(nproc) CFLAGS="-I$(pwd)/elfutils/libelf $CFLAGS" BUILD_STATIC_ONLY=y V=1 78*f7c14bbaSAndroid Build Coastguard Worker 79*f7c14bbaSAndroid Build Coastguard Worker$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 80*f7c14bbaSAndroid Build Coastguard Worker$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" 81*f7c14bbaSAndroid Build Coastguard Worker 82*f7c14bbaSAndroid Build Coastguard Workercp fuzz/bpf-object-fuzzer_seed_corpus.zip "$OUT" 83