summaryrefslogtreecommitdiff
path: root/util/coroutine-ucontext.c
diff options
context:
space:
mode:
authorLingfeng Yang <lfy@google.com>2020-06-12 20:02:23 +0100
committerAlex Bennée <alex.bennee@linaro.org>2020-06-16 14:49:05 +0100
commit0aebab04b9289bd37017593b413ce7a762b54c55 (patch)
tree98905d512be034c19c907c07c351868bc7d9fe3d /util/coroutine-ucontext.c
parent897e34f20f2196089ad4910d95a7c74ee14292c8 (diff)
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>
Diffstat (limited to 'util/coroutine-ucontext.c')
-rw-r--r--util/coroutine-ucontext.c66
1 files changed, 57 insertions, 9 deletions
diff --git a/util/coroutine-ucontext.c b/util/coroutine-ucontext.c
index bd593e61bc..613f4c118e 100644
--- a/util/coroutine-ucontext.c
+++ b/util/coroutine-ucontext.c
@@ -37,12 +37,19 @@
#endif
#endif
+#ifdef CONFIG_TSAN
+#include <sanitizer/tsan_interface.h>
+#endif
+
typedef struct {
Coroutine base;
void *stack;
size_t stack_size;
sigjmp_buf env;
+ void *tsan_co_fiber;
+ void *tsan_caller_fiber;
+
#ifdef CONFIG_VALGRIND_H
unsigned int valgrind_stack_id;
#endif
@@ -65,7 +72,18 @@ union cc_arg {
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
const void *bottom_old;
@@ -78,13 +96,30 @@ static void finish_switch_fiber(void *fake_stack_save)
leader.stack_size = size_old;
}
#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,
- const void *bottom, size_t size)
+static inline __attribute__((always_inline)) void start_switch_fiber(
+ CoroutineAction action, void **fake_stack_save,
+ const void *bottom, size_t size, void *new_fiber)
{
#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
}
@@ -104,8 +139,12 @@ static void coroutine_trampoline(int i0, int i1)
/* Initialize longjmp environment and switch back the caller */
if (!sigsetjmp(self->env, 0)) {
- start_switch_fiber(&fake_stack_save,
- leader.stack, leader.stack_size);
+ start_switch_fiber(
+ COROUTINE_YIELD,
+ &fake_stack_save,
+ leader.stack,
+ leader.stack_size,
+ self->tsan_caller_fiber);
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
}
@@ -154,12 +193,16 @@ Coroutine *qemu_coroutine_new(void)
arg.p = co;
+ on_new_fiber(co);
makecontext(&uc, (void (*)(void))coroutine_trampoline,
2, arg.i[0], arg.i[1]);
/* swapcontext() in, siglongjmp() back out */
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);
}
@@ -216,8 +259,8 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
ret = sigsetjmp(from->env, 0);
if (ret == 0) {
- start_switch_fiber(action == COROUTINE_TERMINATE ?
- NULL : &fake_stack_save, to->stack, to->stack_size);
+ start_switch_fiber(action, &fake_stack_save,
+ to->stack, to->stack_size, to->tsan_co_fiber);
siglongjmp(to->env, action);
}
@@ -231,6 +274,11 @@ Coroutine *qemu_coroutine_self(void)
if (!current) {
current = &leader.base;
}
+#ifdef CONFIG_TSAN
+ if (!leader.tsan_co_fiber) {
+ leader.tsan_co_fiber = __tsan_get_current_fiber();
+ }
+#endif
return current;
}