From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Kevin Wolf" <kwolf@redhat.com>
Subject: [Qemu-devel] [PATCH 12/12] WIP ucontext: annotate coroutine stack for ASAN
Date: Fri, 8 Dec 2017 01:58:25 +0100 [thread overview]
Message-ID: <20171208005825.14587-13-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20171208005825.14587-1-marcandre.lureau@redhat.com>
Not strictly necessary, but it may help ASAN and remove some false
positives.
Sadly, this annotation produces an ASAN error:
$ tests/test-coroutine -p /basic/lifecycle
/basic/lifecycle: ==27655==WARNING: ASan doesn't fully support makecontext/swapcontext functions and may produce false positives in some cases!
==27655==AddressSanitizer CHECK failed: /builddir/build/BUILD/compiler-rt-4.0.1.src/lib/asan/asan_poisoning.cc:38 "((AddrIsAlignedByGranularity(addr + size))) != (0)" (0x0, 0x0)
#0 0x55d1e4e8a695 in __asan::AsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) (/home/elmarco/src/qq/build/tests/test-coroutine+0x1c0695)
#1 0x55d1e4ea6235 in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) (/home/elmarco/src/qq/build/tests/test-coroutine+0x1dc235)
#2 0x55d1e4e82ea4 in __asan::PoisonShadow(unsigned long, unsigned long, unsigned char) (/home/elmarco/src/qq/build/tests/test-coroutine+0x1b8ea4)
#3 0x55d1e4dde1e7 in __asan::FakeStack::Destroy(int) (/home/elmarco/src/qq/build/tests/test-coroutine+0x1141e7)
#4 0x55d1e528b775 in qemu_coroutine_switch /home/elmarco/src/qq/util/coroutine-ucontext.c:219:9
#5 0x55d1e528b18d in coroutine_trampoline /home/elmarco/src/qq/util/coroutine-ucontext.c:114:9
#6 0x7fb3e0087bef (/lib64/libc.so.6+0x50bef)
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
util/coroutine-ucontext.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/util/coroutine-ucontext.c b/util/coroutine-ucontext.c
index 6621f3f692..d200498c38 100644
--- a/util/coroutine-ucontext.c
+++ b/util/coroutine-ucontext.c
@@ -31,6 +31,25 @@
#include <valgrind/valgrind.h>
#endif
+#if (defined(__has_feature) && __has_feature(address_sanitizer)) || \
+ __SANITIZE_ADDRESS__
+#include <sanitizer/asan_interface.h>
+#else
+/* stub to check correct arguments */
+static inline void
+__sanitizer_start_switch_fiber(void **fake_stack_save,
+ const void *bottom, size_t size)
+{
+}
+
+static inline void
+__sanitizer_finish_switch_fiber(void *fake_stack_save,
+ const void **bottom_old,
+ size_t *size_old)
+{
+}
+#endif
+
typedef struct {
Coroutine base;
void *stack;
@@ -64,6 +83,15 @@ static void coroutine_trampoline(int i0, int i1)
union cc_arg arg;
CoroutineUContext *self;
Coroutine *co;
+ const void *bottom_old;
+ size_t size_old;
+ void *fake_stack_save;
+
+ __sanitizer_finish_switch_fiber(NULL, &bottom_old, &size_old);
+ if (!leader.stack) {
+ leader.stack = (void *)bottom_old;
+ leader.stack_size = size_old;
+ }
arg.i[0] = i0;
arg.i[1] = i1;
@@ -72,9 +100,14 @@ static void coroutine_trampoline(int i0, int i1)
/* Initialize longjmp environment and switch back the caller */
if (!sigsetjmp(self->env, 0)) {
+ __sanitizer_start_switch_fiber(&fake_stack_save,
+ bottom_old, size_old);
siglongjmp(*(sigjmp_buf *)co->entry_arg, 1);
}
+ __sanitizer_finish_switch_fiber(&fake_stack_save,
+ NULL, NULL);
+
while (true) {
co->entry(co->entry_arg);
qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
@@ -87,6 +120,7 @@ Coroutine *qemu_coroutine_new(void)
ucontext_t old_uc, uc;
sigjmp_buf old_env;
union cc_arg arg = {0};
+ void *fake_stack_save;
/* The ucontext functions preserve signal masks which incurs a
* system call overhead. sigsetjmp(buf, 0)/siglongjmp() does not
@@ -122,8 +156,13 @@ Coroutine *qemu_coroutine_new(void)
/* swapcontext() in, siglongjmp() back out */
if (!sigsetjmp(old_env, 0)) {
+ __sanitizer_start_switch_fiber(&fake_stack_save,
+ co->stack, co->stack_size);
swapcontext(&old_uc, &uc);
}
+
+ __sanitizer_finish_switch_fiber(&fake_stack_save, NULL, NULL);
+
return &co->base;
}
@@ -169,13 +208,21 @@ qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
int ret;
+ void *fake_stack_save;
current = to_;
ret = sigsetjmp(from->env, 0);
if (ret == 0) {
+
+ __sanitizer_start_switch_fiber(action == COROUTINE_TERMINATE ?
+ NULL : &fake_stack_save,
+ to->stack, to->stack_size);
siglongjmp(to->env, action);
}
+
+ __sanitizer_finish_switch_fiber(&fake_stack_save, NULL, NULL);
+
return ret;
}
--
2.15.1.355.g36791d7216
next prev parent reply other threads:[~2017-12-08 0:59 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-08 0:58 [Qemu-devel] [PATCH 00/12] Various build-sys and ASAN related fixes Marc-André Lureau
2017-12-08 0:58 ` [Qemu-devel] [PATCH 01/12] build-sys: fix qemu-ga -pthread linking Marc-André Lureau
2017-12-08 0:58 ` [Qemu-devel] [PATCH 02/12] build-sys: silence make by default Marc-André Lureau
2017-12-08 19:19 ` Eric Blake
2017-12-13 11:30 ` Marc-André Lureau
2017-12-08 0:58 ` [Qemu-devel] [PATCH 03/12] build-sys: add a rule to print a variable Marc-André Lureau
2017-12-08 19:21 ` Eric Blake
2017-12-08 0:58 ` [Qemu-devel] [PATCH 04/12] build-sys: add AddressSanitizer when --enable-debug if possible Marc-André Lureau
2017-12-08 0:58 ` [Qemu-devel] [PATCH 05/12] tests: fix check-qobject leak: Marc-André Lureau
2017-12-08 17:59 ` Markus Armbruster
2017-12-08 0:58 ` [Qemu-devel] [PATCH 06/12] vl: fix direct firmware directories leak Marc-André Lureau
2017-12-08 0:58 ` [Qemu-devel] [PATCH 07/12] readline: add a free function Marc-André Lureau
2017-12-08 10:05 ` Dr. David Alan Gilbert
2017-12-08 0:58 ` [Qemu-devel] [PATCH 08/12] tests: fix migration-test leak Marc-André Lureau
2017-12-08 9:57 ` Dr. David Alan Gilbert
2017-12-08 0:58 ` [Qemu-devel] [PATCH 09/12] crypto: fix stack-buffer-overflow error Marc-André Lureau
2017-12-08 9:49 ` Daniel P. Berrange
2017-12-08 0:58 ` [Qemu-devel] [PATCH 10/12] qemu-config: fix leak in query-command-line-options Marc-André Lureau
2017-12-08 0:58 ` [Qemu-devel] [PATCH 11/12] tests: fix qmp-test leak Marc-André Lureau
2017-12-08 18:01 ` Markus Armbruster
2017-12-08 0:58 ` Marc-André Lureau [this message]
2017-12-08 13:38 ` [Qemu-devel] [PATCH 12/12] WIP ucontext: annotate coroutine stack for ASAN Stefan Hajnoczi
2017-12-11 11:18 ` Marc-André Lureau
2017-12-11 11:31 ` [Qemu-devel] [PATCH 00/12] Various build-sys and ASAN related fixes no-reply
2017-12-11 11:34 ` no-reply
2017-12-11 12:25 ` no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171208005825.14587-13-marcandre.lureau@redhat.com \
--to=marcandre.lureau@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).