Data race in threadpool.c
Hello,
I'm developing a new dynamic race detection tool on top of ThreadSanitizer, which found a new data race in the X264 project.
In particular, in the threadpool.c
file, there is no synchronization between the read on the variable pool->exit
in line 50 and the write on the same variable in line 148. Below please find the detailed report. Even though this variable is declared as volatile, this still corresponds to a data race according to the C standard.
The following command led to this bug: x264 --threads=4 -o out.264 ./input.yuv
. I also attached the input file: input.yuv
==================
WARNING: ThreadSanitizer: data race (pid=1067947)
Write of size 4 at 0x7b5000000400 by main thread (mutexes: write M0):
#0 x264_8_threadpool_delete ~/x264/common/threadpool.c:148:16 (x264+0x14a20e)
#1 close_file ~/x264/input/thread.c:134:5 (x264+0x11623f)
#2 free_filter ~/x264/filters/video/source.c:80:5 (x264+0x11054d)
#3 main ~/x264/x264.c:396:9 (x264+0xfc742)
Previous read of size 4 at 0x7b5000000400 by thread T1:
#0 threadpool_thread ~/x264/common/threadpool.c:50:22 (x264+0x149e63)
Location is heap block of size 472 at 0x7b5000000400 allocated by main thread:
#0 memalign ~/llvm-project-llvmorg-16.0.0/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:837:3 (x264+0x7581c)
#1 x264_malloc ~/x264/common/base.c:129:21 (x264+0x118dac)
#2 x264_8_threadpool_init ~/x264/common/threadpool.c:79:5 (x264+0x149ccb)
#3 open_file ~/x264/input/thread.c:66:9 (x264+0x115ecf)
#4 parse ~/x264/x264.c:1691:13 (x264+0xfe899)
#5 main ~/x264/x264.c:380:9 (x264+0xfc3a6)
Mutex M0 (0x7b50000004b8) created at:
#0 pthread_mutex_init ~/llvm-project-llvmorg-16.0.0/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:1342:3 (x264+0x6203f)
#1 x264_8_sync_frame_list_init ~/x264/common/frame.c:860:9 (x264+0x12b5d2)
#2 x264_8_threadpool_init ~/x264/common/threadpool.c:87:9 (x264+0x149d58)
#3 open_file ~/x264/input/thread.c:66:9 (x264+0x115ecf)
#4 parse ~/x264/x264.c:1691:13 (x264+0xfe899)
#5 main ~/x264/x264.c:380:9 (x264+0xfc3a6)
Thread T1 (tid=1067950, running) created by main thread at:
#0 pthread_create ~/llvm-project-llvmorg-16.0.0/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp:1049:3 (x264+0x61d43)
#1 x264_8_threadpool_init ~/x264/common/threadpool.c:98:13 (x264+0x149dff)
#2 open_file ~/x264/input/thread.c:66:9 (x264+0x115ecf)
#3 parse ~/x264/x264.c:1691:13 (x264+0xfe899)
#4 main ~/x264/x264.c:380:9 (x264+0xfc3a6)
SUMMARY: ThreadSanitizer: data race ~/x264/common/threadpool.c:148:16 in x264_8_threadpool_delete
==================