autobuild: extend autobuild with samba-fuzz job to build the fuzzers in AFL mode...
[metze/samba-autobuild/.git] / lib / fuzzing / oss-fuzz / build_samba.sh
1 #!/bin/sh -eux
2 #
3 # This is not a general-purpose build script, but instead one specific to the Google oss-fuzz compile environment.
4 #
5 # https://google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements
6 #
7 # https://github.com/google/oss-fuzz/blob/master/infra/base-images/base-builder/README.md#provided-environment-variables
8 #
9 # We have to push to oss-fuzz CFLAGS into the waf ADDITIONAL_CFLAGS
10 # as otherwise waf's configure fails linking the first test binary
11 #
12 # CFLAGS are supplied by the caller, eg the oss-fuzz compile command
13 #
14 # Additional arguments are passed to configure, to allow this to be
15 # tested in autobuild.py
16 #
17 ADDITIONAL_CFLAGS="$CFLAGS"
18 export ADDITIONAL_CFLAGS
19 CFLAGS=""
20 export CFLAGS
21 LD="$CXX"
22 export LD
23
24 # $SANITIZER is provided by the oss-fuzz "compile" command
25 #
26 # We need to add the waf configure option as otherwise when we also
27 # get (eg) -fsanitize=address via the CFLAGS we will fail to link
28 # correctly
29
30 case "$SANITIZER" in
31     address)
32         SANITIZER_ARG='--address-sanitizer'
33         ;;
34     undefined)
35         SANITIZER_ARG='--undefined-sanitizer'
36         ;;
37 esac
38
39 # $LIB_FUZZING_ENGINE is provided by the oss-fuzz "compile" command
40 #
41
42 ./configure -C --without-gettext --enable-debug --enable-developer \
43             --enable-libfuzzer \
44             $SANITIZER_ARG \
45             --disable-warnings-as-errors \
46             --abi-check-disable \
47             --fuzz-target-ldflags="$LIB_FUZZING_ENGINE" \
48             --nonshared-binary=ALL \
49             "$@" \
50             LINK_CC="$CXX"
51
52 make -j
53
54 # Make a directory for the system shared libraries to be copied into
55 mkdir -p $OUT/lib
56
57 # We can't static link to all the system libs with waf, so copy them
58 # to $OUT/lib and set the rpath to point there.  This is similar to how
59 # firefox handles this.
60
61 for x in bin/fuzz_*
62 do
63     cp $x $OUT/
64     bin=`basename $x`
65
66     # Copy any system libraries needed by this fuzzer to $OUT/lib
67     ldd $OUT/$bin | cut -f 2 -d '>' | cut -f 1 -d \( | cut -f 2 -d  ' ' | xargs -i cp \{\} $OUT/lib/
68
69     # Change RUNPATH so that the copied libraries are found on the
70     # runner
71     chrpath -r '$ORIGIN/lib' $OUT/$bin
72
73     # Truncate the original binary to save space
74     echo -n > $x
75 done