configure: add --enable-tsan flag + fiber annotations for coroutine-ucontext

We tried running QEMU under tsan in 2016, but tsan's lack of support for
longjmp-based fibers was a blocker:
  https://groups.google.com/forum/#!topic/thread-sanitizer/se0YuzfWazw

Fortunately, thread sanitizer gained fiber support in early 2019:
  https://reviews.llvm.org/D54889

This patch brings tsan support upstream by importing the patch that annotated
QEMU's coroutines as tsan fibers in Android's QEMU fork:
  https://android-review.googlesource.com/c/platform/external/qemu/+/844675

Tested with '--enable-tsan --cc=clang-9 --cxx=clang++-9 --disable-werror'
configure flags.

Signed-off-by: Lingfeng Yang <lfy@google.com>
Signed-off-by: Emilio G. Cota <cota@braap.org>
[cota: minor modifications + configure changes]
Signed-off-by: Robert Foley <robert.foley@linaro.org>
[RF: configure changes, coroutine fix + minor modifications]
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20200609200738.445-2-robert.foley@linaro.org>
Message-Id: <20200612190237.30436-5-alex.bennee@linaro.org>
This commit is contained in:
Lingfeng Yang 2020-06-12 20:02:23 +01:00 committed by Alex Bennée
parent 897e34f20f
commit 0aebab04b9
2 changed files with 103 additions and 10 deletions

47
configure vendored
View File

@ -395,6 +395,7 @@ gprof="no"
debug_tcg="no" debug_tcg="no"
debug="no" debug="no"
sanitizers="no" sanitizers="no"
tsan="no"
fortify_source="" fortify_source=""
strip_opt="yes" strip_opt="yes"
tcg_interpreter="no" tcg_interpreter="no"
@ -1152,6 +1153,10 @@ for opt do
;; ;;
--disable-sanitizers) sanitizers="no" --disable-sanitizers) sanitizers="no"
;; ;;
--enable-tsan) tsan="yes"
;;
--disable-tsan) tsan="no"
;;
--enable-sparse) sparse="yes" --enable-sparse) sparse="yes"
;; ;;
--disable-sparse) sparse="no" --disable-sparse) sparse="no"
@ -1764,6 +1769,7 @@ Advanced options (experts only):
--with-pkgversion=VERS use specified string as sub-version of the package --with-pkgversion=VERS use specified string as sub-version of the package
--enable-debug enable common debug build options --enable-debug enable common debug build options
--enable-sanitizers enable default sanitizers --enable-sanitizers enable default sanitizers
--enable-tsan enable thread sanitizer
--disable-strip disable stripping binaries --disable-strip disable stripping binaries
--disable-werror disable compilation abort on warning --disable-werror disable compilation abort on warning
--disable-stack-protector disable compiler-provided stack protection --disable-stack-protector disable compiler-provided stack protection
@ -6220,6 +6226,30 @@ if test "$fuzzing" = "yes" ; then
fi fi
fi fi
# Thread sanitizer is, for now, much noisier than the other sanitizers;
# keep it separate until that is not the case.
if test "$tsan" = "yes" && test "$sanitizers" = "yes"; then
error_exit "TSAN is not supported with other sanitiziers."
fi
have_tsan=no
have_tsan_iface_fiber=no
if test "$tsan" = "yes" ; then
write_c_skeleton
if compile_prog "$CPU_CFLAGS -Werror -fsanitize=thread" "" ; then
have_tsan=yes
fi
cat > $TMPC << EOF
#include <sanitizer/tsan_interface.h>
int main(void) {
__tsan_create_fiber(0);
return 0;
}
EOF
if compile_prog "$CPU_CFLAGS -Werror -fsanitize=thread" "" ; then
have_tsan_iface_fiber=yes
fi
fi
########################################## ##########################################
# check for libpmem # check for libpmem
@ -6377,6 +6407,16 @@ if test "$have_asan" = "yes"; then
"Without code annotation, the report may be inferior." "Without code annotation, the report may be inferior."
fi fi
fi fi
if test "$have_tsan" = "yes" ; then
if test "$have_tsan_iface_fiber" = "yes" ; then
QEMU_CFLAGS="-fsanitize=thread $QEMU_CFLAGS"
QEMU_LDFLAGS="-fsanitize=thread $QEMU_LDFLAGS"
else
error_exit "Cannot enable TSAN due to missing fiber annotation interface."
fi
elif test "$tsan" = "yes" ; then
error_exit "Cannot enable TSAN due to missing sanitize thread interface."
fi
if test "$have_ubsan" = "yes"; then if test "$have_ubsan" = "yes"; then
QEMU_CFLAGS="-fsanitize=undefined $QEMU_CFLAGS" QEMU_CFLAGS="-fsanitize=undefined $QEMU_CFLAGS"
QEMU_LDFLAGS="-fsanitize=undefined $QEMU_LDFLAGS" QEMU_LDFLAGS="-fsanitize=undefined $QEMU_LDFLAGS"
@ -6412,7 +6452,8 @@ if test "$werror" = "yes"; then
QEMU_CFLAGS="-Werror $QEMU_CFLAGS" QEMU_CFLAGS="-Werror $QEMU_CFLAGS"
fi fi
if test "$solaris" = "no" ; then # Exclude --warn-common with TSan to suppress warnings from the TSan libraries.
if test "$solaris" = "no" && test "$tsan" = "no"; then
if $ld --version 2>/dev/null | grep "GNU ld" >/dev/null 2>/dev/null ; then if $ld --version 2>/dev/null | grep "GNU ld" >/dev/null 2>/dev/null ; then
QEMU_LDFLAGS="-Wl,--warn-common $QEMU_LDFLAGS" QEMU_LDFLAGS="-Wl,--warn-common $QEMU_LDFLAGS"
fi fi
@ -7476,6 +7517,10 @@ if test "$have_asan_iface_fiber" = "yes" ; then
echo "CONFIG_ASAN_IFACE_FIBER=y" >> $config_host_mak echo "CONFIG_ASAN_IFACE_FIBER=y" >> $config_host_mak
fi fi
if test "$have_tsan" = "yes" && test "$have_tsan_iface_fiber" = "yes" ; then
echo "CONFIG_TSAN=y" >> $config_host_mak
fi
if test "$has_environ" = "yes" ; then if test "$has_environ" = "yes" ; then
echo "CONFIG_HAS_ENVIRON=y" >> $config_host_mak echo "CONFIG_HAS_ENVIRON=y" >> $config_host_mak
fi fi

View File

@ -37,12 +37,19 @@
#endif #endif
#endif #endif
#ifdef CONFIG_TSAN
#include <sanitizer/tsan_interface.h>
#endif
typedef struct { typedef struct {
Coroutine base; Coroutine base;
void *stack; void *stack;
size_t stack_size; size_t stack_size;
sigjmp_buf env; sigjmp_buf env;
void *tsan_co_fiber;
void *tsan_caller_fiber;
#ifdef CONFIG_VALGRIND_H #ifdef CONFIG_VALGRIND_H
unsigned int valgrind_stack_id; unsigned int valgrind_stack_id;
#endif #endif
@ -65,7 +72,18 @@ union cc_arg {
int i[2]; int i[2];
}; };
static void finish_switch_fiber(void *fake_stack_save) /* QEMU_ALWAYS_INLINE only does so if __OPTIMIZE__, so we cannot use it. */
static inline __attribute__((always_inline))
void on_new_fiber(CoroutineUContext *co)
{
#ifdef CONFIG_TSAN
co->tsan_co_fiber = __tsan_create_fiber(0); /* flags: sync on switch */
co->tsan_caller_fiber = __tsan_get_current_fiber();
#endif
}
static inline __attribute__((always_inline))
void finish_switch_fiber(void *fake_stack_save)
{ {
#ifdef CONFIG_ASAN #ifdef CONFIG_ASAN
const void *bottom_old; const void *bottom_old;
@ -78,13 +96,30 @@ static void finish_switch_fiber(void *fake_stack_save)
leader.stack_size = size_old; leader.stack_size = size_old;
} }
#endif #endif
#ifdef CONFIG_TSAN
if (fake_stack_save) {
__tsan_release(fake_stack_save);
__tsan_switch_to_fiber(fake_stack_save, 0); /* 0=synchronize */
}
#endif
} }
static void start_switch_fiber(void **fake_stack_save, static inline __attribute__((always_inline)) void start_switch_fiber(
const void *bottom, size_t size) CoroutineAction action, void **fake_stack_save,
const void *bottom, size_t size, void *new_fiber)
{ {
#ifdef CONFIG_ASAN #ifdef CONFIG_ASAN
__sanitizer_start_switch_fiber(fake_stack_save, bottom, size); __sanitizer_start_switch_fiber(
action == COROUTINE_TERMINATE ? NULL : fake_stack_save,
bottom, size);
#endif
#ifdef CONFIG_TSAN
void *curr_fiber =
__tsan_get_current_fiber();
__tsan_acquire(curr_fiber);
*fake_stack_save = curr_fiber;
__tsan_switch_to_fiber(new_fiber, 0); /* 0=synchronize */
#endif #endif
} }
@ -104,8 +139,12 @@ static void coroutine_trampoline(int i0, int i1)
/* Initialize longjmp environment and switch back the caller */ /* Initialize longjmp environment and switch back the caller */
if (!sigsetjmp(self->env, 0)) { if (!sigsetjmp(self->env, 0)) {
start_switch_fiber(&fake_stack_save, start_switch_fiber(
leader.stack, leader.stack_size); COROUTINE_YIELD,
&fake_stack_save,
leader.stack,
leader.stack_size,
self->tsan_caller_fiber);
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
} }
@ -154,12 +193,16 @@ Coroutine *qemu_coroutine_new(void)
arg.p = co; arg.p = co;
on_new_fiber(co);
makecontext(&uc, (void (*)(void))coroutine_trampoline, makecontext(&uc, (void (*)(void))coroutine_trampoline,
2, arg.i[0], arg.i[1]); 2, arg.i[0], arg.i[1]);
/* swapcontext() in, siglongjmp() back out */ /* swapcontext() in, siglongjmp() back out */
if (!sigsetjmp(old_env, 0)) { if (!sigsetjmp(old_env, 0)) {
start_switch_fiber(&fake_stack_save, co->stack, co->stack_size); start_switch_fiber(
COROUTINE_YIELD,
&fake_stack_save,
co->stack, co->stack_size, co->tsan_co_fiber);
swapcontext(&old_uc, &uc); swapcontext(&old_uc, &uc);
} }
@ -216,8 +259,8 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
ret = sigsetjmp(from->env, 0); ret = sigsetjmp(from->env, 0);
if (ret == 0) { if (ret == 0) {
start_switch_fiber(action == COROUTINE_TERMINATE ? start_switch_fiber(action, &fake_stack_save,
NULL : &fake_stack_save, to->stack, to->stack_size); to->stack, to->stack_size, to->tsan_co_fiber);
siglongjmp(to->env, action); siglongjmp(to->env, action);
} }
@ -231,6 +274,11 @@ Coroutine *qemu_coroutine_self(void)
if (!current) { if (!current) {
current = &leader.base; current = &leader.base;
} }
#ifdef CONFIG_TSAN
if (!leader.tsan_co_fiber) {
leader.tsan_co_fiber = __tsan_get_current_fiber();
}
#endif
return current; return current;
} }