https://bugs.kde.org/show_bug.cgi?id=484480
Bug ID: 484480 Summary: False positives when using sem_trywait Classification: Developer tools Product: valgrind Version: 3.22.0 Platform: Ubuntu OS: Linux Status: REPORTED Severity: normal Priority: NOR Component: helgrind Assignee: jsew...@acm.org Reporter: jmcgee01...@gmail.com Target Milestone: --- SUMMARY Using sem_trywait leads to false positives, even when it is guaranteed to have succeeded by the time that the access is made. STEPS TO REPRODUCE Compile the following code with `gcc -o test test.c -pthread`, then run with `valgrind --tool=helgrind ./test` ``` #include <stdio.h> #include <semaphore.h> #include <pthread.h> static char* result; static sem_t sem; static void* func(void* data) { result = "Finished!"; sem_post(&sem); return NULL; } int main() { sem_init(&sem, 0, 0); pthread_t tid; pthread_create(&tid, NULL, func, NULL); while (sem_trywait(&sem)) ; // do nothing but keep retrying /* The above loop could be replaced this instead: * if (sem_trywait(&sem)) * sem_wait(&sem); */ printf("%s\n", result); } ``` OBSERVED RESULT Helgrind reports the following: ==787273== Helgrind, a thread error detector ==787273== Copyright (C) 2007-2017, and GNU GPL'd, by OpenWorks LLP et al. ==787273== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==787273== Command: ./test ==787273== ==787273== ---Thread-Announcement------------------------------------------ ==787273== ==787273== Thread #1 is the program's root thread ==787273== ==787273== ---Thread-Announcement------------------------------------------ ==787273== ==787273== Thread #2 was created ==787273== at 0x4980F23: clone (in /usr/lib64/libc.so.6) ==787273== by 0x4981CDE: __clone_internal (in /usr/lib64/libc.so.6) ==787273== by 0x48FC834: create_thread (in /usr/lib64/libc.so.6) ==787273== by 0x48FD339: pthread_create@@GLIBC_2.34 (in /usr/lib64/libc.so.6) ==787273== by 0x4852D1C: pthread_create_WRK (hg_intercepts.c:445) ==787273== by 0x4854239: pthread_create@* (hg_intercepts.c:478) ==787273== by 0x4011C4: main (in /home/ugrads/majors/colinm25/test) ==787273== ==787273== ---------------------------------------------------------------- ==787273== ==787273== Possible data race during read of size 8 at 0x404080 by thread #1 ==787273== Locks held: none ==787273== at 0x4011D4: main (in /home/ugrads/majors/colinm25/test) ==787273== ==787273== This conflicts with a previous write of size 8 by thread #2 ==787273== Locks held: none ==787273== at 0x401172: func (in /home/ugrads/majors/colinm25/test) ==787273== by 0x4852F16: mythread_wrapper (hg_intercepts.c:406) ==787273== by 0x48FCC01: start_thread (in /usr/lib64/libc.so.6) ==787273== by 0x4980F33: clone (in /usr/lib64/libc.so.6) ==787273== Address 0x404080 is 0 bytes inside data symbol "result" ==787273== Finished! ==787273== ==787273== Use --history-level=approx or =none to gain increased speed, at ==787273== the cost of reduced accuracy of conflicting-access information ==787273== For lists of detected and suppressed errors, rerun with: -s ==787273== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) EXPECTED RESULT Helgrind should not report any warnings. SOFTWARE/OS VERSIONS Tested on CentOS Stream 9 with Valgrind 3.22.0 and Ubuntu Server 20.04 with Valgrind 3.15.0. -- You are receiving this mail because: You are watching all bug changes.