From 3f1e8028d51fdc9d0bf60323c99a09431f8dd408 Mon Sep 17 00:00:00 2001 From: Chris Apple Date: Sat, 26 Jul 2025 09:29:40 -0700 Subject: [PATCH] [rtsan] Add test for pthread_cond_wait segfault --- compiler-rt/test/rtsan/pthread_cond_wait.cpp | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 compiler-rt/test/rtsan/pthread_cond_wait.cpp diff --git a/compiler-rt/test/rtsan/pthread_cond_wait.cpp b/compiler-rt/test/rtsan/pthread_cond_wait.cpp new file mode 100644 index 0000000000000..915ec07228554 --- /dev/null +++ b/compiler-rt/test/rtsan/pthread_cond_wait.cpp @@ -0,0 +1,46 @@ +// RUN: %clangxx -fsanitize=realtime %s -o %t +// RUN: %run %t 2>&1 | FileCheck %s + +// UNSUPPORTED: ios + +// Intent: Ensures that pthread_cond_signal does not segfault under rtsan +// See issue #146120 + +#include +#include +#include +#include + +#include + +int main() { + std::cout << "Entry to main!" << std::endl; + std::mutex mut; + std::condition_variable cv; + bool go{false}; + + const auto fut = std::async(std::launch::async, [&] { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + { + std::unique_lock lock(mut); + go = true; + } + cv.notify_one(); + }); + + std::unique_lock lock(mut); + // normal wait is fine + // cv.wait(lock, [&] { return go; }); + // but timed wait could segfault + + // NOTE: If this test segfaults on a test runner, please comment + // out this line and submit the patch. + // I will follow up with a fix of the underlying problem, + // but first I need to understand if it fails a test runner + cv.wait_for(lock, std::chrono::milliseconds(200), [&] { return go; }); + + std::cout << "Exit from main!" << std::endl; +} + +// CHECK: Entry to main! +// CHECK-NEXT: Exit from main!