<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/134344>134344</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[TSAN] Threadsanitizer false negative
</td>
</tr>
<tr>
<th>Labels</th>
<td>
false-negative
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jaidevshastri
</td>
</tr>
</table>
<pre>
Hi,
TSAN reports no race when enabled with `-fsanitize=thread` on the test program below. This behavior has been observed with precompiled llvm+clang binaries (v16) and compiled llvm project binaries (v16.0.6).
```
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t l;
int x = 1;
void* writer1(void* _) {
pthread_mutex_lock(&l);
x = 0; //Wx1
pthread_mutex_unlock(&l);
return NULL;
}
void* writer2(void* _) {
pthread_mutex_lock(&l);
pthread_mutex_unlock(&l);
x = 1; //Wx2
return NULL;
}
int main() {
pthread_t thr[2];
pthread_mutex_init(&l, NULL);
pthread_create(&thr[0], NULL, writer1, NULL);
pthread_create(&thr[1], NULL, writer2, NULL);
pthread_join(thr[0], NULL);
pthread_join(thr[1], NULL);
pthread_mutex_destroy(&l);
return 0;
}
```
When writer1 acquires the lock `l` before writer2, the epoch information seems to be transferred such that the happens-before (HB) analysis on the shared memory `x` returns true: Wx1 HB Wx2. TSAN thus fails to assert the race between Wx1 and Wx2.
When writer2 acquires the lock `l` before writer1, the happens-before analysis correctly asserts the lack of ordering between Wx1 and Wx2, thereby reporting race.
Could the above behavior be clarified? Specifically, I would like to know if this is a limitation of pure HB analysis or a limitation/bug of TSAN.
I am also attaching my understanding of the two scheduling sequences for the test program and the vector clock information for reference.
[Tsan-norace-seq.pdf](https://github.com/user-attachments/files/19598094/Tsan-norace-seq.pdf)
[Tsan-race-sequence.pdf](https://github.com/user-attachments/files/19598095/Tsan-race-sequence.pdf)
Any information on this issue would be much appreciated.
Thanks!
Jaidev
</pre>
<img width="1" height="1" alt="" src="http://email.email.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