Fix a race condition in the bionic pthread tests.
It's possible for the main thread to leave the RunTimedTest
function before the waiting thread has had a chance to call e.g.
pthread_cond_timedwait(). In this case, pthread_cond_timedwait()
will access the local variable ts after its lifetime has ended. Fix
the bug by making ts a field of pthread_CondWakeupTest instead. The
lifetime of pthread_CondWakeupTest is tied to that of the waiting
thread via the pthread_join() call.
Found with HWASan + uaccess logging.
Change-Id: Iefe8deb30a367dc518013d741c425b041596b0d3
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index e4f9cb8..907a35c 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -1493,6 +1493,7 @@
};
std::atomic<Progress> progress;
pthread_t thread;
+ timespec ts;
std::function<int (pthread_cond_t* cond, pthread_mutex_t* mutex)> wait_function;
protected:
@@ -1524,11 +1525,10 @@
clockid_t clock,
std::function<int(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* timeout)>
wait_function) {
- timespec ts;
ASSERT_EQ(0, clock_gettime(clock, &ts));
ts.tv_sec += 1;
- StartWaitingThread([&wait_function, &ts](pthread_cond_t* cond, pthread_mutex_t* mutex) {
+ StartWaitingThread([&wait_function, this](pthread_cond_t* cond, pthread_mutex_t* mutex) {
return wait_function(cond, mutex, &ts);
});