]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/oss-fuzz.sh
tools: explicitly specify "setup" subcommand
[thirdparty/systemd.git] / tools / oss-fuzz.sh
CommitLineData
ff12a795 1#!/usr/bin/env bash
db9ecf05 2# SPDX-License-Identifier: LGPL-2.1-or-later
7db7d5b7
JR
3
4set -ex
5
6export LC_CTYPE=C.UTF-8
7
36cd9913
ZJS
8export CC=${CC:-clang}
9export CXX=${CXX:-clang++}
10clang_version="$($CC --version | sed -nr 's/.*version ([^ ]+?) .*/\1/p' | sed -r 's/-$//')"
11
31e57a35 12SANITIZER=${SANITIZER:-address -fsanitize-address-use-after-scope}
85ed9124 13flags="-O1 -fno-omit-frame-pointer -g -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=$SANITIZER"
31e57a35 14
36cd9913
ZJS
15clang_lib="/usr/lib64/clang/${clang_version}/lib/linux"
16[ -d "$clang_lib" ] || clang_lib="/usr/lib/clang/${clang_version}/lib/linux"
17
31e57a35
JR
18export CFLAGS=${CFLAGS:-$flags}
19export CXXFLAGS=${CXXFLAGS:-$flags}
36cd9913
ZJS
20export LDFLAGS=${LDFLAGS:--L${clang_lib}}
21
31e57a35
JR
22export WORK=${WORK:-$(pwd)}
23export OUT=${OUT:-$(pwd)/out}
f7e0d22d 24mkdir -p "$OUT"
31e57a35 25
f7e0d22d
FS
26build="$WORK/build"
27rm -rf "$build"
28mkdir -p "$build"
7d941c06 29
31e57a35 30if [ -z "$FUZZING_ENGINE" ]; then
cc5549ca 31 fuzzflag="llvm-fuzz=true"
1f034000
EV
32else
33 fuzzflag="oss-fuzz=true"
2fd1beb3
EV
34
35 apt-get update
36 apt-get install -y gperf m4 gettext python3-pip \
b22f5ed5 37 libcap-dev libmount-dev \
69aa4982 38 pkg-config wget python3-jinja2 zipmerge
4b65fc87 39
b22f5ed5
EV
40 if [[ "$ARCHITECTURE" == i386 ]]; then
41 apt-get install -y pkg-config:i386 libcap-dev:i386 libmount-dev:i386
42 fi
43
4b65fc87
EV
44 # gnu-efi is installed here to enable -Dgnu-efi behind which fuzz-bcd
45 # is hidden. It isn't linked against efi. It doesn't
46 # even include "efi.h" because "bcd.c" can work in "unit test" mode
47 # where it isn't necessary.
48 apt-get install -y gnu-efi zstd
49
4997d1b9
EV
50 pip3 install -r .github/workflows/requirements.txt --require-hashes
51
52 # https://github.com/google/oss-fuzz/issues/6868
53 ORIG_PYTHONPATH=$(python3 -c 'import sys;print(":".join(sys.path[1:]))')
54 export PYTHONPATH="$ORIG_PYTHONPATH:/usr/lib/python3/dist-packages/"
2fd1beb3 55
1f034000 56 if [[ "$SANITIZER" == undefined ]]; then
c84059f1
EV
57 additional_ubsan_checks=pointer-overflow,alignment
58 UBSAN_FLAGS="-fsanitize=$additional_ubsan_checks -fno-sanitize-recover=$additional_ubsan_checks"
1f034000
EV
59 CFLAGS="$CFLAGS $UBSAN_FLAGS"
60 CXXFLAGS="$CXXFLAGS $UBSAN_FLAGS"
61 fi
ebd4541e
EV
62
63 if [[ "$SANITIZER" == introspector ]]; then
64 # fuzz-introspector passes -fuse-ld=gold and -flto using CFLAGS/LDFLAGS and due to
65 # https://github.com/mesonbuild/meson/issues/6377#issuecomment-575977919 and
66 # https://github.com/mesonbuild/meson/issues/6377 it doesn't mix well with meson.
67 # It's possible to build systemd with duct tape there using something like
68 # https://github.com/google/oss-fuzz/pull/7583#issuecomment-1104011067 but
69 # apparently even with gold and lto some parts of systemd are missing from
70 # reports (presumably due to https://github.com/google/oss-fuzz/issues/7598).
71 # Let's just fail here for now to make it clear that fuzz-introspector isn't supported.
72 exit 1
73 fi
31e57a35
JR
74fi
75
e3b2f7c0 76if ! meson setup "$build" "-D$fuzzflag" -Db_lundef=false; then
f7e0d22d 77 cat "$build/meson-logs/meson-log.txt"
77591e97
EV
78 exit 1
79fi
80
f7e0d22d 81ninja -v -C "$build" fuzzers
7db7d5b7 82
4b65fc87
EV
83# Compressed BCD files are kept in test/test-bcd so let's unpack them
84# and put them all in the seed corpus.
85bcd=$(mktemp -d)
86for i in test/test-bcd/*.zst; do
87 unzstd "$i" -o "$bcd/$(basename "${i%.zst}")";
88done
89zip -jqr "$OUT/fuzz-bcd_seed_corpus.zip" "$bcd"
90rm -rf "$bcd"
91
e0ec0450
EV
92hosts=$(mktemp)
93wget -O "$hosts" https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
94zip -jq "$OUT/fuzz-etc-hosts_seed_corpus.zip" "$hosts"
95rm -rf "$hosts"
96
93b575b2
ZJS
97# The seed corpus is a separate flat archive for each fuzzer,
98# with a fixed name ${fuzzer}_seed_corpus.zip.
81f84a2c 99for d in test/fuzz/fuzz-*; do
bef8d18b
FS
100 fuzzer="$(basename "$d")"
101 # Include the build-generated corpora if any as well
102 readarray -t generated < <(find "$build/test/fuzz" -maxdepth 1 -name "${fuzzer}*" -type f)
103 zip -jqr "$OUT/${fuzzer}_seed_corpus.zip" "$d" "${generated[@]}"
2bd37c5b
JR
104done
105
106# get fuzz-dns-packet corpus
f7e0d22d
FS
107df="$build/dns-fuzzing"
108git clone --depth 1 https://github.com/CZ-NIC/dns-fuzzing "$df"
109zip -jqr "$OUT/fuzz-dns-packet_seed_corpus.zip" "$df/packet"
7db7d5b7 110
4287c855
ZJS
111install -Dt "$OUT/src/shared/" \
112 "$build"/src/shared/libsystemd-shared-*.so \
113 "$build"/src/core/libsystemd-core-*.so
7db7d5b7 114
b22f5ed5
EV
115# Most i386 libraries have to be brought to the runtime environment somehow. Ideally they
116# should be linked statically but since it isn't possible another way to keep them close
117# to the fuzz targets is used here. The dependencies are copied to "$OUT/src/shared" and
118# then `rpath` is tweaked to make it possible for the linker to find them there. "$OUT/src/shared"
119# is chosen because the runtime search path of all the fuzz targets already points to it
120# to load "libsystemd-shared" and "libsystemd-core". Stuff like that should be avoided on
121# x86_64 because it tends to break coverage reports, fuzz-introspector, CIFuzz and so on.
122if [[ "$ARCHITECTURE" == i386 ]]; then
123 for lib_path in $(ldd "$OUT"/src/shared/libsystemd-shared-*.so | perl -lne 'print $1 if m{=>\s+(/lib\S+)}'); do
124 lib_name=$(basename "$lib_path")
125 cp "$lib_path" "$OUT/src/shared"
126 patchelf --set-rpath \$ORIGIN "$OUT/src/shared/$lib_name"
127 done
128 patchelf --set-rpath \$ORIGIN "$OUT"/src/shared/libsystemd-shared-*.so
129fi
130
f7e0d22d 131wget -O "$OUT/fuzz-json.dict" https://raw.githubusercontent.com/rc0r/afl-fuzz/master/dictionaries/json.dict
6315d12b 132
f7e0d22d
FS
133find "$build" -maxdepth 1 -type f -executable -name "fuzz-*" -exec mv {} "$OUT" \;
134find src -type f -name "fuzz-*.dict" -exec cp {} "$OUT" \;
135cp src/fuzz/*.options "$OUT"
69aa4982
EV
136
137if [[ "$MERGE_WITH_OSS_FUZZ_CORPORA" == "yes" ]]; then
138 for f in "$OUT/"fuzz-*; do
139 [[ -x "$f" ]] || continue
140 fuzzer=$(basename "$f")
141 t=$(mktemp)
142 if wget -O "$t" "https://storage.googleapis.com/systemd-backup.clusterfuzz-external.appspot.com/corpus/libFuzzer/systemd_${fuzzer}/public.zip"; then
143 zipmerge "$OUT/${fuzzer}_seed_corpus.zip" "$t"
144 fi
145 rm -rf "$t"
146 done
147fi