17c2aad20Sopenharmony_ci#!/bin/bash
27c2aad20Sopenharmony_ciset -eux
37c2aad20Sopenharmony_ci
47c2aad20Sopenharmony_ciSANITIZER=${SANITIZER:-address}
57c2aad20Sopenharmony_ciflags="-O1 -fno-omit-frame-pointer -g -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=$SANITIZER -fsanitize=fuzzer-no-link"
67c2aad20Sopenharmony_ci
77c2aad20Sopenharmony_ciexport CC=${CC:-clang}
87c2aad20Sopenharmony_ciexport CFLAGS=${CFLAGS:-$flags}
97c2aad20Sopenharmony_ci
107c2aad20Sopenharmony_ciexport CXX=${CXX:-clang++}
117c2aad20Sopenharmony_ciexport CXXFLAGS=${CXXFLAGS:-$flags}
127c2aad20Sopenharmony_ci
137c2aad20Sopenharmony_cicd "$(dirname -- "$0")/.."
147c2aad20Sopenharmony_ci
157c2aad20Sopenharmony_ciexport OUT=${OUT:-"$(pwd)/out"}
167c2aad20Sopenharmony_cimkdir -p "$OUT"
177c2aad20Sopenharmony_ci
187c2aad20Sopenharmony_ciexport LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer}
197c2aad20Sopenharmony_ci
207c2aad20Sopenharmony_ci# libelf is compiled with _FORTIFY_SOURCE by default and it
217c2aad20Sopenharmony_ci# isn't compatible with MSan. It was borrowed
227c2aad20Sopenharmony_ci# from https://github.com/google/oss-fuzz/pull/7422
237c2aad20Sopenharmony_ciif [[ "$SANITIZER" == memory ]]; then
247c2aad20Sopenharmony_ci    CFLAGS+=" -U_FORTIFY_SOURCE"
257c2aad20Sopenharmony_ci    CXXFLAGS+=" -U_FORTIFY_SOURCE"
267c2aad20Sopenharmony_cifi
277c2aad20Sopenharmony_ci
287c2aad20Sopenharmony_ci# The alignment check is turned off by default on OSS-Fuzz/CFLite so it should be
297c2aad20Sopenharmony_ci# turned on explicitly there. It was borrowed from
307c2aad20Sopenharmony_ci# https://github.com/google/oss-fuzz/pull/7092
317c2aad20Sopenharmony_ciif [[ "$SANITIZER" == undefined ]]; then
327c2aad20Sopenharmony_ci    additional_ubsan_checks=alignment
337c2aad20Sopenharmony_ci    UBSAN_FLAGS="-fsanitize=$additional_ubsan_checks -fno-sanitize-recover=$additional_ubsan_checks"
347c2aad20Sopenharmony_ci    CFLAGS+=" $UBSAN_FLAGS"
357c2aad20Sopenharmony_ci    CXXFLAGS+=" $UBSAN_FLAGS"
367c2aad20Sopenharmony_cifi
377c2aad20Sopenharmony_ci
387c2aad20Sopenharmony_ci# Ideally libbelf should be built using release tarballs available
397c2aad20Sopenharmony_ci# at https://sourceware.org/elfutils/ftp/. Unfortunately sometimes they
407c2aad20Sopenharmony_ci# fail to compile (for example, elfutils-0.185 fails to compile with LDFLAGS enabled
417c2aad20Sopenharmony_ci# due to https://bugs.gentoo.org/794601) so let's just point the script to
427c2aad20Sopenharmony_ci# commits referring to versions of libelf that actually can be built
437c2aad20Sopenharmony_cirm -rf elfutils
447c2aad20Sopenharmony_cigit clone https://sourceware.org/git/elfutils.git
457c2aad20Sopenharmony_ci(
467c2aad20Sopenharmony_cicd elfutils
477c2aad20Sopenharmony_cigit checkout 67a187d4c1790058fc7fd218317851cb68bb087c
487c2aad20Sopenharmony_cigit log --oneline -1
497c2aad20Sopenharmony_ci
507c2aad20Sopenharmony_ci# ASan isn't compatible with -Wl,--no-undefined: https://github.com/google/sanitizers/issues/380
517c2aad20Sopenharmony_cised -i 's/^\(NO_UNDEFINED=\).*/\1/' configure.ac
527c2aad20Sopenharmony_ci
537c2aad20Sopenharmony_ci# ASan isn't compatible with -Wl,-z,defs either:
547c2aad20Sopenharmony_ci# https://clang.llvm.org/docs/AddressSanitizer.html#usage
557c2aad20Sopenharmony_cised -i 's/^\(ZDEFS_LDFLAGS=\).*/\1/' configure.ac
567c2aad20Sopenharmony_ci
577c2aad20Sopenharmony_ciif [[ "$SANITIZER" == undefined ]]; then
587c2aad20Sopenharmony_ci    # That's basicaly what --enable-sanitize-undefined does to turn off unaligned access
597c2aad20Sopenharmony_ci    # elfutils heavily relies on on i386/x86_64 but without changing compiler flags along the way
607c2aad20Sopenharmony_ci    sed -i 's/\(check_undefined_val\)=[0-9]/\1=1/' configure.ac
617c2aad20Sopenharmony_cifi
627c2aad20Sopenharmony_ci
637c2aad20Sopenharmony_ciautoreconf -i -f
647c2aad20Sopenharmony_ciif ! ./configure --enable-maintainer-mode --disable-debuginfod --disable-libdebuginfod \
657c2aad20Sopenharmony_ci            --disable-demangler --without-bzlib --without-lzma --without-zstd \
667c2aad20Sopenharmony_ci	    CC="$CC" CFLAGS="-Wno-error $CFLAGS" CXX="$CXX" CXXFLAGS="-Wno-error $CXXFLAGS" LDFLAGS="$CFLAGS"; then
677c2aad20Sopenharmony_ci    cat config.log
687c2aad20Sopenharmony_ci    exit 1
697c2aad20Sopenharmony_cifi
707c2aad20Sopenharmony_ci
717c2aad20Sopenharmony_cimake -C config -j$(nproc) V=1
727c2aad20Sopenharmony_cimake -C lib -j$(nproc) V=1
737c2aad20Sopenharmony_cimake -C libelf -j$(nproc) V=1
747c2aad20Sopenharmony_ci)
757c2aad20Sopenharmony_ci
767c2aad20Sopenharmony_cimake -C src BUILD_STATIC_ONLY=y V=1 clean
777c2aad20Sopenharmony_cimake -C src -j$(nproc) CFLAGS="-I$(pwd)/elfutils/libelf $CFLAGS" BUILD_STATIC_ONLY=y V=1
787c2aad20Sopenharmony_ci
797c2aad20Sopenharmony_ci$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
807c2aad20Sopenharmony_ci$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"
817c2aad20Sopenharmony_ci
827c2aad20Sopenharmony_cicp fuzz/bpf-object-fuzzer_seed_corpus.zip "$OUT"
83