Skip to content

Commit f3a7fd2

Browse files
committed
Fix TSAN issues in thread support library
-------------------------------------------------------- The issue was the test is attempting to check for change in variable through a while loop, but did not protect the data read using mutext lock. This could potentially lead to reading of unexpected data and cause a tsan error. The fix wraps the checks for variable state inside the mutex lock.
1 parent aca879b commit f3a7fd2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/acl_threadsupport/test/acl_test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ SimpleString StringFrom(uint32_t x);
1414
SimpleString StringFrom(uint64_t x);
1515
SimpleString StringFrom(intptr_t x);
1616
SimpleString StringFrom(size_t x);
17+
bool check_threadtest_state(int state, bool check_eq);

lib/acl_threadsupport/test/acl_threadsupport_test.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ volatile int threadvar = 0;
9595

9696
void *test_thread(void *) {
9797
while (1) {
98-
while (threadtest_state == SERVER_STATE)
98+
while (check_threadtest_state(SERVER_STATE, true))
9999
acl_thread_yield();
100100
if (threadtest_state == END_STATE) {
101101
return 0;
@@ -108,6 +108,18 @@ void *test_thread(void *) {
108108
}
109109
}
110110

111+
bool check_threadtest_state(int state, bool check_eq) {
112+
bool result;
113+
acl_mutex_lock(&mymutex);
114+
if (check_eq) {
115+
result = threadtest_state == state;
116+
} else {
117+
result = threadtest_state != state;
118+
}
119+
acl_mutex_unlock(&mymutex);
120+
return result;
121+
}
122+
111123
TEST(threadsupport, threads) {
112124
// Setup
113125
int res = 0;
@@ -125,15 +137,16 @@ TEST(threadsupport, threads) {
125137
res = acl_mutex_unlock(&mymutex);
126138
CHECK_EQUAL(0, res);
127139

128-
while (threadtest_state != SERVER_STATE)
140+
while (check_threadtest_state(SERVER_STATE, false))
129141
acl_thread_yield();
130142
res = acl_mutex_lock(&mymutex);
131143
CHECK_EQUAL(0, res);
132144
CHECK_EQUAL(threadvar, 7 + 13);
133145
threadtest_state = CLIENT_STATE;
134146
res = acl_mutex_unlock(&mymutex);
135147
CHECK_EQUAL(0, res);
136-
while (threadtest_state != SERVER_STATE)
148+
// while (check_threadtest_state(SERVER_STATE, false))
149+
while (check_threadtest_state(SERVER_STATE, false))
137150
acl_thread_yield();
138151
res = acl_mutex_lock(&mymutex);
139152
CHECK_EQUAL(0, res);

0 commit comments

Comments
 (0)