Skip to contentSkip to conversation

Sanitizers configuration

PATCH In review Marc-André Lureau 3 participants 6 messages Jan 16, 2018 – Jan 30, 2018 Arm Samsung Exynos

lore
Share & tools
lore
#0 lore raw

cover Sanitizers configuration

Hi,

This is a few reworked patches from "[PATCH v3 00/18] Various build-sys and sanitizer related fixes" series.

It enables sanitizers by default with --enable-debug. But sanitizers can be also enabled/disabled independently with a configure option.

If ASAN is detected but coroutine annotations are not available, then a simple message is printed during configure.

Marc-André Lureau (3):
exynos4210: workaround UBSAN compilation error
build-sys: add --enable-sanitizers
ucontext: annotate coroutine stack for ASAN

 include/qemu/compiler.h      |  4 +++
 hw/display/exynos4210_fimd.c |  2 +-
 util/coroutine-ucontext.c    | 48 ++++++++++++++++++++++++++++++++
 .travis.yml                  |  3 +-
 configure                    | 65 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 120 insertions(+), 2 deletions(-)
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
#1 lore raw

PATCH 1/3 exynos4210: workaround UBSAN compilation error

gcc 5.4.0-6ubuntu1~16.04.5 build with UBSAN enabled error:

CC hw/display/exynos4210_fimd.o
/home/petmay01/linaro/qemu-for-merges/hw/display/exynos4210_fimd.c: In function ‘fimd_get_buffer_id’:
/home/petmay01/linaro/qemu-for-merges/hw/display/exynos4210_fimd.c:1105:5:
error: case label does not reduce to an integer constant
case FIMD_WINCON_BUF2_STAT:

Because FIMD_WINCON_BUF2_STAT case contains an integer overflow, use U suffix to get the unsigned type.

---
 hw/display/exynos4210_fimd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/exynos4210_fimd.c +1 -1
index fd0b2bec65..86e37e93e9 100644
@@ -98,7 +98,7 @@
 #define FIMD_WINCON_BUFSTATUS       ((1 << 21) | (1 << 31))
 #define FIMD_WINCON_BUF0_STAT       ((0 << 21) | (0 << 31))
 #define FIMD_WINCON_BUF1_STAT       ((1 << 21) | (0 << 31))
-#define FIMD_WINCON_BUF2_STAT       ((0 << 21) | (1 << 31))
+#define FIMD_WINCON_BUF2_STAT       ((0 << 21) | (1U << 31))
 #define FIMD_WINCON_BUFSELECT       ((1 << 20) | (1 << 30))
 #define FIMD_WINCON_BUF0_SEL        ((0 << 20) | (0 << 30))
 #define FIMD_WINCON_BUF1_SEL        ((1 << 20) | (0 << 30))
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
#2 lore raw

Re: [Qemu-arm] [PATCH 1/3] exynos4210: workaround UBSAN compilation error

Quoted 11 lines · Marc-André Lureau

gcc 5.4.0-6ubuntu1~16.04.5 build with UBSAN enabled error:

CC hw/display/exynos4210_fimd.o
/home/petmay01/linaro/qemu-for-merges/hw/display/exynos4210_fimd.c: In function ‘fimd_get_buffer_id’:
/home/petmay01/linaro/qemu-for-merges/hw/display/exynos4210_fimd.c:1105:5:
error: case label does not reduce to an integer constant
case FIMD_WINCON_BUF2_STAT:

Because FIMD_WINCON_BUF2_STAT case contains an integer overflow, use U suffix to get the unsigned type.

We should note that this is a gcc bug here (we compile with -fwrapv so integer overflows like this are well-defined) which we are working around.

Quoted 19 lines
---
 hw/display/exynos4210_fimd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/exynos4210_fimd.c +1 -2
index fd0b2bec65..86e37e93e9 100644
@@ -98,7 +98,7 @@
 #define FIMD_WINCON_BUFSTATUS       ((1 << 21) | (1 << 31))
 #define FIMD_WINCON_BUF0_STAT       ((0 << 21) | (0 << 31))
 #define FIMD_WINCON_BUF1_STAT       ((1 << 21) | (0 << 31))
-#define FIMD_WINCON_BUF2_STAT       ((0 << 21) | (1 << 31))
+#define FIMD_WINCON_BUF2_STAT       ((0 << 21) | (1U << 31))
 #define FIMD_WINCON_BUFSELECT       ((1 << 20) | (1 << 30))
 #define FIMD_WINCON_BUF0_SEL        ((0 << 20) | (0 << 30))
 #define FIMD_WINCON_BUF1_SEL        ((1 << 20) | (0 << 30))
--
Quoted 2 lines

2.16.0.rc1.1.gef27df75a1

thanks
-- PMM

#3 lore raw

PATCH 2/3 build-sys: add --enable-sanitizers

In reply to Marc-André Lureau

Typical slowdown introduced by AddressSanitizer is 2x. UBSan shouldn't have much impact on runtime cost.

Enable it by default when --enable-debug, unless --disable-sanitizers.

---
 configure | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
configure +35 -0
index 6d8c996c62..9a435917cc 100755
@@ -342,6 +342,7 @@ rdma=""
 gprof="no"
 debug_tcg="no"
 debug="no"
+sanitizers=""
 fortify_source=""
 strip_opt="yes"
 tcg_interpreter="no"
@@ -994,6 +995,10 @@ for opt do
       strip_opt="no"
       fortify_source="no"
   ;;
+  --enable-sanitizers) sanitizers="yes"
+  ;;
+  --disable-sanitizers) sanitizers="no"
+  ;;
   --enable-sparse) sparse="yes"
   ;;
   --disable-sparse) sparse="no"
@@ -1471,6 +1476,7 @@ Advanced options (experts only):
   --firmwarepath=PATH      search PATH for firmware files
   --with-confsuffix=SUFFIX suffix for QEMU data inside datadir/libdir/sysconfdir [$confsuffix]
   --enable-debug           enable common debug build options
+  --enable-sanitizers      enable default sanitizers
   --disable-strip          disable stripping binaries
   --disable-werror         disable compilation abort on warning
   --disable-stack-protector disable compiler-provided stack protection
@@ -5185,6 +5191,28 @@ if compile_prog "" "" ; then
     have_utmpx=yes
 fi
 
+##########################################
+# checks for sanitizers
+
+write_c_skeleton
+
+have_asan=no
+have_ubsan=no
+
+# enable sanitizers by default if --enable-debug
+if test "$sanitizers" = "" -a "$debug" = "yes"; then
+  sanitizers=yes
+fi
+
+if test "$sanitizers" = "yes" ; then
+  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=address" ""; then
+      have_asan=yes
+  fi
+  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=undefined" ""; then
+      have_ubsan=yes
+  fi
+fi
+
 ##########################################
 # End of CC checks
 # After here, no more $cc or $ld runs
@@ -5209,6 +5237,13 @@ else
     CFLAGS="-O2 $CFLAGS"
 fi
 
+if test "$have_asan" = "yes"; then
+  CFLAGS="-fsanitize=address $CFLAGS"
+fi
+if test "$have_ubsan" = "yes"; then
+  CFLAGS="-fsanitize=undefined $CFLAGS"
+fi
+
 ##########################################
 # Do we have libnfs
 if test "$libnfs" != "no" ; then
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
#4 lore raw

PATCH 3/3 ucontext: annotate coroutine stack for ASAN

In reply to Marc-André Lureau

It helps ASAN to detect more leaks on coroutine stacks, and to get rid of some extra warnings.

Before:

tests/test-coroutine -p
/basic/lifecycle
/basic/lifecycle: ==20781==WARNING: ASan doesn't fully support makecontext/swapcontext functions and may produce false positives in some cases!
==20781==WARNING: ASan is ignoring requested __asan_handle_no_return:
stack top: 0x7ffcb184d000; bottom 0x7ff6c4cfd000; size: 0x0005ecb50000 (25446121472)
False positive error reports may follow
For details see https://github.com/google/sanitizers/issues/189
OK

After:

tests/test-coroutine -p /basic/lifecycle /basic/lifecycle: ==21110==WARNING: ASan doesn't fully support makecontext/swapcontext functions and may produce false positives in some cases!
OK

A similar work would need to be done for sigaltstack & windows fibers to have similar coverage. Since ucontext is preferred, I didn't bother checking the other coroutine implementations for now.

Update travis to fix the build with ASAN annotations.

---
 include/qemu/compiler.h   |  4 ++++
 util/coroutine-ucontext.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
 .travis.yml               |  3 ++-
 configure                 | 30 +++++++++++++++++++++++++++++
 4 files changed, 84 insertions(+), 1 deletion(-)
include/qemu/compiler.h +4 -0
index 340e5fdc09..5fcc4f7ec7 100644
@@ -111,4 +111,8 @@
 #define GCC_FMT_ATTR(n, m)
 #endif
 
+#ifndef __has_feature
+#define __has_feature(x) 0 /* compatibility with non-clang compilers */
+#endif
+
 #endif /* COMPILER_H */
util/coroutine-ucontext.c +48 -0

101 lines not on this page · raw message

.travis.yml +2 -1
index f583839755..f2291e87a6 100644
@@ -13,12 +13,13 @@ addons:
       - libattr1-dev
       - libbrlapi-dev
       - libcap-ng-dev
+      - libgcc-6-dev
       - libgnutls-dev
       - libgtk-3-dev
       - libiscsi-dev
       - liblttng-ust-dev
-      - libnfs-dev
       - libncurses5-dev
+      - libnfs-dev
       - libnss3-dev
       - libpixman-1-dev
       - libpng12-dev
configure +30 -0
index 9a435917cc..0160e24005 100755
@@ -5198,6 +5198,8 @@ write_c_skeleton
 
 have_asan=no
 have_ubsan=no
+have_asan_iface_h=no
+have_asan_iface_fiber=no
 
 # enable sanitizers by default if --enable-debug
 if test "$sanitizers" = "" -a "$debug" = "yes"; then
@@ -5211,12 +5213,29 @@ if test "$sanitizers" = "yes" ; then
   if compile_prog "$CPU_CFLAGS -Werror -fsanitize=undefined" ""; then
       have_ubsan=yes
   fi
+
+  if check_include "sanitizer/asan_interface.h" ; then
+      have_asan_iface_h=yes
+  fi
+
+  cat > $TMPC << EOF
+#include <sanitizer/asan_interface.h>
+int main(void) {
+  __sanitizer_start_switch_fiber(0, 0, 0);
+  return 0;
+}
+EOF
+  if compile_prog "$CPU_CFLAGS -Werror -fsanitize=address" "" ; then
+      have_asan_iface_fiber=yes
+  fi
 fi
 
 ##########################################
 # End of CC checks
 # After here, no more $cc or $ld runs
 
+write_c_skeleton
+
 if test "$gcov" = "yes" ; then
   CFLAGS="-fprofile-arcs -ftest-coverage -g $CFLAGS"
   LDFLAGS="-fprofile-arcs -ftest-coverage $LDFLAGS"
@@ -5239,6 +5258,13 @@ fi
 
 if test "$have_asan" = "yes"; then
   CFLAGS="-fsanitize=address $CFLAGS"
+  if test "$have_asan_iface_h" = "no" ; then
+      echo "ASAN build enabled, but ASAN header missing." \
+           "Without code annotation, the report may be inferior."
+  elif test "$have_asan_iface_fiber" = "no" ; then
+      echo "ASAN build enabled, but ASAN header is too old." \
+           "Without code annotation, the report may be inferior."
+  fi
 fi
 if test "$have_ubsan" = "yes"; then
   CFLAGS="-fsanitize=undefined $CFLAGS"
@@ -6220,6 +6246,10 @@ if test "$valgrind_h" = "yes" ; then
   echo "CONFIG_VALGRIND_H=y" >> $config_host_mak
 fi
 
+if test "$have_asan_iface_fiber" = "yes" ; then
+    echo "HAVE_ASAN_IFACE_FIBER=y" >> $config_host_mak
+fi
+
 if test "$has_environ" = "yes" ; then
   echo "CONFIG_HAS_ENVIRON=y" >> $config_host_mak
 fi
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
#5 lore raw
In reply to Marc-André Lureau

ping

Quoted 27 lines · Marc-André Lureau

Hi,

This is a few reworked patches from "[PATCH v3 00/18] Various build-sys and sanitizer related fixes" series.

It enables sanitizers by default with --enable-debug. But sanitizers can be also enabled/disabled independently with a configure option.

If ASAN is detected but coroutine annotations are not available, then a simple message is printed during configure.

Marc-André Lureau (3):
exynos4210: workaround UBSAN compilation error
build-sys: add --enable-sanitizers
ucontext: annotate coroutine stack for ASAN

 include/qemu/compiler.h      |  4 +++
 hw/display/exynos4210_fimd.c |  2 +-
 util/coroutine-ucontext.c    | 48 ++++++++++++++++++++++++++++++++
 .travis.yml                  |  3 +-
 configure                    | 65 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 120 insertions(+), 2 deletions(-)

--
2.16.0.rc1.1.gef27df75a1

Signature · 1 line
Marc-André Lureau