[compiler-rt] [rtsan] Add test to ensure coroutines get caught (PR #111049)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 3 12:40:49 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Chris Apple (cjappl)

<details>
<summary>Changes</summary>

Coroutines allocate when they are called:

https://en.cppreference.com/w/cpp/language/coroutines

> When a coroutine begins execution, it performs the following:

>     [allocates](https://en.cppreference.com/w/cpp/language/coroutines#Dynamic_allocation) the coroutine state object using [operator new](https://en.cppreference.com/w/cpp/memory/new/operator_new).

Just adding this test as a sanity check that we catch this allocation, and it passes on my machines

---
Full diff: https://github.com/llvm/llvm-project/pull/111049.diff


1 Files Affected:

- (added) compiler-rt/test/rtsan/coroutine.cpp (+31) 


``````````diff
diff --git a/compiler-rt/test/rtsan/coroutine.cpp b/compiler-rt/test/rtsan/coroutine.cpp
new file mode 100644
index 00000000000000..bd7c0ad630c0f4
--- /dev/null
+++ b/compiler-rt/test/rtsan/coroutine.cpp
@@ -0,0 +1,31 @@
+// RUN: %clangxx -std=c++20 -fsanitize=realtime %s -o %t
+// RUN: not %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: ios
+
+// Intent: Coroutines allocate memory and are not allowed in a [[clang::nonblocking]] function.
+
+#include <coroutine>
+
+struct SimpleCoroutine {
+  struct promise_type {
+    SimpleCoroutine get_return_object() { return SimpleCoroutine{}; }
+    std::suspend_never initial_suspend() { return {}; }
+    std::suspend_never final_suspend() noexcept { return {}; }
+    void unhandled_exception() {}
+    void return_void() {}
+  };
+};
+
+SimpleCoroutine example_coroutine() { co_return; }
+
+void calls_a_coroutine() [[clang::nonblocking]] { example_coroutine(); }
+
+int main() {
+  calls_a_coroutine();
+  return 0;
+}
+
+// CHECK: ==ERROR: RealtimeSanitizer
+
+// Somewhere in the stack this should be mentioned
+// CHECK: calls_a_coroutine

``````````

</details>


https://github.com/llvm/llvm-project/pull/111049


More information about the llvm-commits mailing list