Skip to content

Commit b60aed6

Browse files
authored
[rtsan] Add test for pthread_cond_wait segfault (#150776)
Introduce the test from #146120 For future readers of this PR, if this test causes a segfault please comment out the line indicated by the comment (or revert this entire commit). My plan is to commit this, see if any test runners fail, then submit the fix in a follow on. I cannot repro this bug on my machine so I need some confirmation of the bug being fixed as it is submitted.
1 parent 9b41b62 commit b60aed6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t
2+
// RUN: %run %t 2>&1 | FileCheck %s
3+
4+
// UNSUPPORTED: ios
5+
6+
// Intent: Ensures that pthread_cond_signal does not segfault under rtsan
7+
// See issue #146120
8+
9+
#include <condition_variable>
10+
#include <future>
11+
#include <mutex>
12+
#include <thread>
13+
14+
#include <iostream>
15+
16+
int main() {
17+
std::cout << "Entry to main!" << std::endl;
18+
std::mutex mut;
19+
std::condition_variable cv;
20+
bool go{false};
21+
22+
const auto fut = std::async(std::launch::async, [&] {
23+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
24+
{
25+
std::unique_lock<std::mutex> lock(mut);
26+
go = true;
27+
}
28+
cv.notify_one();
29+
});
30+
31+
std::unique_lock<std::mutex> lock(mut);
32+
// normal wait is fine
33+
// cv.wait(lock, [&] { return go; });
34+
// but timed wait could segfault
35+
36+
// NOTE: If this test segfaults on a test runner, please comment
37+
// out this line and submit the patch.
38+
// I will follow up with a fix of the underlying problem,
39+
// but first I need to understand if it fails a test runner
40+
cv.wait_for(lock, std::chrono::milliseconds(200), [&] { return go; });
41+
42+
std::cout << "Exit from main!" << std::endl;
43+
}
44+
45+
// CHECK: Entry to main!
46+
// CHECK-NEXT: Exit from main!

0 commit comments

Comments
 (0)