]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - tools/oss-fuzz.sh
tools: shellcheck-ify tool scripts
[thirdparty/systemd.git] / tools / oss-fuzz.sh
... / ...
CommitLineData
1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3
4set -ex
5
6export LC_CTYPE=C.UTF-8
7
8export CC=${CC:-clang}
9export CXX=${CXX:-clang++}
10clang_version="$($CC --version | sed -nr 's/.*version ([^ ]+?) .*/\1/p' | sed -r 's/-$//')"
11
12SANITIZER=${SANITIZER:-address -fsanitize-address-use-after-scope}
13flags="-O1 -fno-omit-frame-pointer -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=$SANITIZER"
14
15clang_lib="/usr/lib64/clang/${clang_version}/lib/linux"
16[ -d "$clang_lib" ] || clang_lib="/usr/lib/clang/${clang_version}/lib/linux"
17
18export CFLAGS=${CFLAGS:-$flags}
19export CXXFLAGS=${CXXFLAGS:-$flags}
20export LDFLAGS=${LDFLAGS:--L${clang_lib}}
21
22export WORK=${WORK:-$(pwd)}
23export OUT=${OUT:-$(pwd)/out}
24mkdir -p "$OUT"
25
26build="$WORK/build"
27rm -rf "$build"
28mkdir -p "$build"
29
30if [ -z "$FUZZING_ENGINE" ]; then
31 fuzzflag="llvm-fuzz=true"
32else
33 fuzzflag="oss-fuzz=true"
34 if [[ "$SANITIZER" == undefined ]]; then
35 UBSAN_FLAGS="-fsanitize=pointer-overflow -fno-sanitize-recover=pointer-overflow"
36 CFLAGS="$CFLAGS $UBSAN_FLAGS"
37 CXXFLAGS="$CXXFLAGS $UBSAN_FLAGS"
38 fi
39fi
40
41if ! meson "$build" "-D$fuzzflag" -Db_lundef=false; then
42 cat "$build/meson-logs/meson-log.txt"
43 exit 1
44fi
45
46ninja -v -C "$build" fuzzers
47
48# The seed corpus is a separate flat archive for each fuzzer,
49# with a fixed name ${fuzzer}_seed_corpus.zip.
50for d in "$(dirname "$0")/../test/fuzz/fuzz-"*; do
51 zip -jqr "$OUT/$(basename "$d")_seed_corpus.zip" "$d"
52done
53
54# get fuzz-dns-packet corpus
55df="$build/dns-fuzzing"
56git clone --depth 1 https://github.com/CZ-NIC/dns-fuzzing "$df"
57zip -jqr "$OUT/fuzz-dns-packet_seed_corpus.zip" "$df/packet"
58
59install -Dt "$OUT/src/shared/" "$build"/src/shared/libsystemd-shared-*.so
60
61wget -O "$OUT/fuzz-json.dict" https://raw.githubusercontent.com/rc0r/afl-fuzz/master/dictionaries/json.dict
62
63find "$build" -maxdepth 1 -type f -executable -name "fuzz-*" -exec mv {} "$OUT" \;
64find src -type f -name "fuzz-*.dict" -exec cp {} "$OUT" \;
65cp src/fuzz/*.options "$OUT"