Introduce fuzz-force-options package
authorMaria Guseva <m.guseva@samsung.com>
Fri, 5 May 2017 16:04:28 +0000 (19:04 +0300)
committerMaria Guseva <m.guseva@samsung.com>
Tue, 11 Jul 2017 02:46:03 +0000 (11:46 +0900)
The packages forces compile options for Address Sanitization and coverage
needed for fuzzing.

packaging/gcc-force-options [new file with mode: 0755]
packaging/gcc-unforce-options [new file with mode: 0755]
packaging/libFuzzer.spec

diff --git a/packaging/gcc-force-options b/packaging/gcc-force-options
new file mode 100755 (executable)
index 0000000..34f4945
--- /dev/null
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+set -e
+
+if [ $# -eq 0 ]; then
+       cat << EOF
+Syntax:
+  $(basename $0) OPT1 [OPT2...]"
+
+Example:
+  $(basename $0) [prepend|append] -fsanitize=address -fno-common -U_FORTIFY_SOURCE
+  By default flags are appending.
+EOF
+       exit 1
+fi
+
+FLAGS="$@"
+
+if [ $(find $(dirname $0) -name \*-real | wc -l) -gt 0 ]; then
+       echo >&2 "$(basename $0): directory was already processed, aborting"
+       exit 1
+fi
+
+case "$1" in
+prepend)
+       shift
+       PREFLAGS="$@"
+       POSTFLAGS=
+       ;;
+append)
+       shift
+       PREFLAGS=
+       POSTFLAGS="$@"
+       ;;
+*)
+       PREFLAGS=
+       POSTFLAGS="$@"
+       ;;
+esac
+
+TMP=$(pwd)/tmp.$$
+cat > $TMP << EOF
+#!/bin/sh
+if echo "$PREFLAGS "\$@" $POSTFLAGS" | grep -q -- "-fsanitize=undefined" && echo "\$@" | grep -q "\.gch\>"; then
+       # UBSan doesn't support precompiled headers. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66343
+       echo "Precompiled headers currently not supported by UBSan" >&2
+       # Don't instrument kernel modules
+       # Don't instrument with "-nostdlib" linking
+elif ! echo "\$@" | grep -q -e __KERNEL__ -e \-nostdlib; then
+       # Use readlink in order to follow symlinks if any
+       \$(readlink -f \$0)-real $PREFLAGS "\$@" $POSTFLAGS
+else
+       \$(readlink -f \$0)-real "\$@"
+fi
+EOF
+chmod +x $TMP
+
+find -L $(dirname $0) -type f -a -perm -a=x | grep -E '(gcc|g\+\+|c\+\+)$' | while read tool; do
+       mv $tool $tool-real
+       cp $TMP $tool
+done
+
+if [ -d /emul ]; then
+       find -L /emul -type f -a -perm -a=x | grep -E '(gcc|g\+\+|c\+\+)$' | while read tool; do
+               ln -sf $(basename $tool) $tool-real
+       done
+fi
+
+rm $TMP
+
diff --git a/packaging/gcc-unforce-options b/packaging/gcc-unforce-options
new file mode 100755 (executable)
index 0000000..cf36d04
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+if [ $# -gt 0 ]; then
+       echo >&2 "$(basename $0): unexpected arguments"
+       exit 1
+fi
+
+find $(dirname $0) -name \*-real | while read tool_real; do
+       tool=$(echo "$tool_real" | sed -e 's/-real$//')
+       mv $tool_real $tool
+done
+if [ -d /emul ]; then
+       find /emul -name \*-real | while read tool_real; do
+               rm $tool_real
+       done
+fi
+
index 30372e74c75d1374707483a158e7e8cd9e42b83b..8e8b707b06f0b7b530b112179e5d7e9437441fa6 100644 (file)
@@ -6,6 +6,8 @@ Summary:        libFuzzer
 Url:            http://llvm.org/docs/LibFuzzer.html
 Group:          Development/Testing
 Source0:        %{name}-%{version}.tar.xz
+Source1:        gcc-force-options
+Source2:        gcc-unforce-options
 
 %description
 LibFuzzer is in-process, coverage-guided, evolutionary fuzzing engine.
@@ -24,9 +26,35 @@ ar rcs libsancov_cb.a sancov_cb.o
 mkdir -p %{buildroot}%{_libdir}
 cp lib/Fuzzer/libFuzzer.a %{buildroot}%{_libdir}
 cp libsancov_cb.a %{buildroot}%{_libdir}
+mkdir -p %{buildroot}%{_prefix}/bin
+cp %{SOURCE1} %{SOURCE2} %{buildroot}%{_prefix}/bin
 
 %files
 %manifest %{name}.manifest
 %defattr(-,root,root,-)
 %{_libdir}/libFuzzer.a
+
+%package -n fuzz-force-options
+Summary:    Scripts to enable automatic package sanitization for fuzzing
+Group:      System Environment
+# Need to have gcc installed before running the force-options script
+Requires:   gcc libasan
+
+%description -n fuzz-force-options
+Scripts for ASan and SanCov instrumentation needed for fuzzing.
+
+%post -n fuzz-force-options
+# Change mode to allow all users to run gcc-force/unforce-options
+chmod a+w /usr/bin
+/usr/bin/gcc-force-options -fsanitize-coverage=trace-pc -fsanitize=address -fno-common -fno-omit-frame-pointer -U_FORTIFY_SOURCE -lsancov_cb
+chmod a-w /usr/bin
+
+%preun -n fuzz-force-options
+chmod a+w /usr/bin
+/usr/bin/gcc-unforce-options
+chmod a-w /usr/bin
+
+%files -n fuzz-force-options
+%{_prefix}/bin/gcc-force-options
+%{_prefix}/bin/gcc-unforce-options
 %{_libdir}/libsancov_cb.a