Skip to contentSkip to conversation

coroutine-ucontext: Save fake stack for pooled coroutine

PATCH Superseded Akihiko Odaki 3 participants 4 messages Jan 12, 2024 – Jan 17, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coroutine-ucontext: Save fake stack for pooled coroutine

Coroutine may be pooled even after COROUTINE_TERMINATE if CONFIG_COROUTINE_POOL is enabled and fake stack should be saved in such a case to keep AddressSanitizerUseAfterReturn working. Even worse, I'm seeing stack corruption without fake stack being saved.

---
 util/coroutine-ucontext.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
util/coroutine-ucontext.c +19 -2
index 7b304c79d942..e62ced5d6779 100644
@@ -124,8 +124,9 @@ void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save,
 {
 #ifdef CONFIG_ASAN
     __sanitizer_start_switch_fiber(
-            action == COROUTINE_TERMINATE ? NULL : fake_stack_save,
-            bottom, size);
+        !IS_ENABLED(CONFIG_COROUTINE_POOL) && action == COROUTINE_TERMINATE ?
+            NULL : fake_stack_save,
+        bottom, size);
 #endif
 }
 
@@ -269,10 +270,26 @@ static inline void valgrind_stack_deregister(CoroutineUContext *co)
 #endif
 #endif
 
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+static void coroutine_fn terminate(void *opaque)
+{
+    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, opaque);
+
+    __sanitizer_start_switch_fiber(NULL, to->stack, to->stack_size);
+    siglongjmp(to->env, COROUTINE_ENTER);
+}
+#endif
+
 void qemu_coroutine_delete(Coroutine *co_)
 {
     CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
 
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+    co_->entry_arg = qemu_coroutine_self();
+    co_->entry = terminate;
+    qemu_coroutine_switch(co_->entry_arg, co_, COROUTINE_ENTER);
+#endif
+
 #ifdef CONFIG_VALGRIND_H
     valgrind_stack_deregister(co);
 #endif

---
base-commit: f614acb7450282a119d85d759f27eae190476058
change-id: 20240112-asan-eb695c769f40

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw
Quoted 9 lines · Akihiko Odaki

Coroutine may be pooled even after COROUTINE_TERMINATE if CONFIG_COROUTINE_POOL is enabled and fake stack should be saved in such a case to keep AddressSanitizerUseAfterReturn working. Even worse, I'm seeing stack corruption without fake stack being saved.

---
 util/coroutine-ucontext.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

Adding Marc-André Lureau and Lingfeng Yang, who authored the code in question.

Stefan

Quoted 53 lines
util/coroutine-ucontext.c +19 -3
index 7b304c79d942..e62ced5d6779 100644
@@ -124,8 +124,9 @@ void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save,
 {
 #ifdef CONFIG_ASAN
     __sanitizer_start_switch_fiber(
-            action == COROUTINE_TERMINATE ? NULL : fake_stack_save,
-            bottom, size);
+        !IS_ENABLED(CONFIG_COROUTINE_POOL) && action == COROUTINE_TERMINATE ?
+            NULL : fake_stack_save,
+        bottom, size);
 #endif
 }
 
@@ -269,10 +270,26 @@ static inline void valgrind_stack_deregister(CoroutineUContext *co)
 #endif
 #endif
 
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+static void coroutine_fn terminate(void *opaque)
+{
+    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, opaque);
+
+    __sanitizer_start_switch_fiber(NULL, to->stack, to->stack_size);
+    siglongjmp(to->env, COROUTINE_ENTER);
+}
+#endif
+
 void qemu_coroutine_delete(Coroutine *co_)
 {
     CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
 
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+    co_->entry_arg = qemu_coroutine_self();
+    co_->entry = terminate;
+    qemu_coroutine_switch(co_->entry_arg, co_, COROUTINE_ENTER);
+#endif
+
 #ifdef CONFIG_VALGRIND_H
     valgrind_stack_deregister(co);
 #endif
 
---

base-commit: f614acb7450282a119d85d759f27eae190476058
change-id: 20240112-asan-eb695c769f40

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#2 lore raw

Hi

Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Akihiko Odaki

Coroutine may be pooled even after COROUTINE_TERMINATE if CONFIG_COROUTINE_POOL is enabled and fake stack should be saved in such a case to keep AddressSanitizerUseAfterReturn working. Even worse, I'm seeing stack corruption without fake stack being saved.

Thanks Akihiko, this is solving a crash when enabling ASAN!

Quoted 6 lines
Quoted 3 lines
---
 util/coroutine-ucontext.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

Adding Marc-André Lureau and Lingfeng Yang, who authored the code in question.

Side note:
I am surprised that commit 0aebab04b9 "configure: add --enable-tsan flag + fiber annotations" changed code like this:
{
#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

*fake_stack_save = curr_fiber:
Is TSAN compatible with ASAN ? I guess not.

It would probably help to have more explicit comments & errors if such a case happens.

Quoted 4 lines

Stefan

Quoted 1 line
Quoted 13 lines

util/coroutine-ucontext.c void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save, · line 129

Quoted 13 lines
util/coroutine-ucontext.c +3 -2
index 7b304c79d942..e62ced5d6779 100644
@@ -124,8 +124,9 @@ void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save,
 {
 #ifdef CONFIG_ASAN
     __sanitizer_start_switch_fiber(
-            action == COROUTINE_TERMINATE ? NULL : fake_stack_save,
-            bottom, size);
+        !IS_ENABLED(CONFIG_COROUTINE_POOL) && action == COROUTINE_TERMINATE ?
+            NULL : fake_stack_save,
+        bottom, size);

Ok, changing back the commit from Lingfeng when coroutine pools are enabled.

Quoted 7 lines
Quoted 7 lines
util/coroutine-ucontext.c +0 -0
 #endif
 }
 
@@ -269,10 +270,26 @@ static inline void valgrind_stack_deregister(CoroutineUContext *co)
 #endif
 #endif
 
Quoted 8 lines

util/coroutine-ucontext.c static inline void valgrind_stack_deregister(CoroutineUContext *co) · line 280

Quoted 8 lines
util/coroutine-ucontext.c +8 -0
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+static void coroutine_fn terminate(void *opaque)
+{
+    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, opaque);
+
+    __sanitizer_start_switch_fiber(NULL, to->stack, to->stack_size);
+    siglongjmp(to->env, COROUTINE_ENTER);
+}

looking at https://github.com/llvm/llvm-project/blob/main/compiler-rt/test/asan/TestCases/Linux/swapcontext_annotation.cpp,
that seems correct to me to destroy the fake_stack.

However, not switching back with qemu_coroutine_switch() may create
issues: set_current() (and tsan) not being called appropriately.

Should we introduce another action like COROUTINE_DELETE?

Quoted 24 lines
Quoted 24 lines
util/coroutine-ucontext.c +8 -1
+#endif
+
 void qemu_coroutine_delete(Coroutine *co_)
 {
     CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
 
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+    co_->entry_arg = qemu_coroutine_self();
+    co_->entry = terminate;
+    qemu_coroutine_switch(co_->entry_arg, co_, COROUTINE_ENTER);
+#endif
+
 #ifdef CONFIG_VALGRIND_H
     valgrind_stack_deregister(co);
 #endif
 
---

base-commit: f614acb7450282a119d85d759f27eae190476058
change-id: 20240112-asan-eb695c769f40

Best regards,
--
Akihiko Odaki <[email protected]>

Signature · 1 line
Marc-André Lureau
#3 lore raw
Quoted 40 lines · Marc-André Lureau

Hi

Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Akihiko Odaki

Coroutine may be pooled even after COROUTINE_TERMINATE if CONFIG_COROUTINE_POOL is enabled and fake stack should be saved in such a case to keep AddressSanitizerUseAfterReturn working. Even worse, I'm seeing stack corruption without fake stack being saved.

Thanks Akihiko, this is solving a crash when enabling ASAN!

Quoted 6 lines
Quoted 3 lines
---
  util/coroutine-ucontext.c | 21 +++++++++++++++++++--
  1 file changed, 19 insertions(+), 2 deletions(-)

Adding Marc-André Lureau and Lingfeng Yang, who authored the code in question.

Side note:
I am surprised that commit 0aebab04b9 "configure: add --enable-tsan flag + fiber annotations" changed code like this:
{
#ifdef CONFIG_ASAN

(quoted diff) +11 -1
-    __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
Quoted 2 lines

*fake_stack_save = curr_fiber:
Is TSAN compatible with ASAN ? I guess not.

meson.build has the following:
if get_option('tsan')
if get_option('sanitizers')
error('TSAN is not supported with other sanitizers')
endif

Quoted 3 lines

It would probably help to have more explicit comments & errors if such a case happens.

Added G_STATIC_ASSERT(!IS_ENABLED(CONFIG_TSAN)) in v2.

Quoted 41 lines
Quoted 5 lines

Stefan

Quoted 1 line
Quoted 13 lines

util/coroutine-ucontext.c void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save, · line 129

Quoted 13 lines
util/coroutine-ucontext.c +3 -2
index 7b304c79d942..e62ced5d6779 100644
@@ -124,8 +124,9 @@ void start_switch_fiber_asan(CoroutineAction action, void **fake_stack_save,
  {
  #ifdef CONFIG_ASAN
      __sanitizer_start_switch_fiber(
-            action == COROUTINE_TERMINATE ? NULL : fake_stack_save,
-            bottom, size);
+        !IS_ENABLED(CONFIG_COROUTINE_POOL) && action == COROUTINE_TERMINATE ?
+            NULL : fake_stack_save,
+        bottom, size);

Ok, changing back the commit from Lingfeng when coroutine pools are enabled.

Quoted 7 lines
Quoted 7 lines
util/coroutine-ucontext.c +0 -0
  #endif
  }
 
@@ -269,10 +270,26 @@ static inline void valgrind_stack_deregister(CoroutineUContext *co)
  #endif
  #endif
 
Quoted 8 lines

util/coroutine-ucontext.c static inline void valgrind_stack_deregister(CoroutineUContext *co) · line 280

Quoted 8 lines
util/coroutine-ucontext.c +8 -0
+#if defined(CONFIG_ASAN) && defined(CONFIG_COROUTINE_POOL)
+static void coroutine_fn terminate(void *opaque)
+{
+    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, opaque);
+
+    __sanitizer_start_switch_fiber(NULL, to->stack, to->stack_size);
+    siglongjmp(to->env, COROUTINE_ENTER);
+}

looking at https://github.com/llvm/llvm-project/blob/main/compiler-rt/test/asan/TestCases/Linux/swapcontext_annotation.cpp,
that seems correct to me to destroy the fake_stack.

Quoted 2 lines

However, not switching back with qemu_coroutine_switch() may create
issues: set_current() (and tsan) not being called appropriately.

Thanks for catching this. Added missing set_current() call and G_STATIC_ASSERT(!IS_ENABLED(CONFIG_TSAN)) in v2 as I described earlier.

Quoted 2 lines

Should we introduce another action like COROUTINE_DELETE?

I don't think so. CoroutineAction is part of the common interface for coroutine backends. The need to switch back to the destroyed coroutine is a peculiarity unique to ucontext and better to be contained in coroutine-ucontext.c.

Alternatively we can add a bool parameter to qemu_coroutine_switch() to tell to destroy the fake stack, but probably it's not that worth to share qemu_coroutine_switch() code; while coroutine-ucontext.c already have a few places that call start_switch_fiber_asan() or siglongjmp() and they are somewhat similar, they are still too diverged to unify.