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
This commit is contained in:
Peter Collingbourne 2021-12-02 12:38:46 -08:00
parent 5205e8ac5e
commit c5b81844b6
1 changed files with 2 additions and 2 deletions

View File

@ -1493,6 +1493,7 @@ class pthread_CondWakeupTest : public ::testing::Test {
};
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 @@ class pthread_CondWakeupTest : public ::testing::Test {
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);
});