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

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


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

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

>From 5313b69f33635c5e9e81628e6f9d5b6546ee54a0 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Thu, 3 Oct 2024 12:37:55 -0700
Subject: [PATCH] [rtsan] Add test to ensure coroutines get caught

---
 compiler-rt/test/rtsan/coroutine.cpp | 31 ++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 compiler-rt/test/rtsan/coroutine.cpp

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



More information about the llvm-commits mailing list