Fix data race with members of struct DBusCounter
The first commit of this merge request provides a Dbus-only test case to reproduce the data race problem reported in #426, and the second commit fixes the problem. To see the problem, locally undo the second commit and run the test named "test-counter"-from a Linux gcc build with thread sanitizer enabled.
The detailed messages reported by the thread sanitizer on this problem are:
14/40 Test #14: test-counter ........................***Failed 0.08 sec
# Test data in /home/user/src/dbus/build-cmake/test/data
ok 1 - initial nss query did not leak memory
# Running test: unix_fd
==================
WARNING: ThreadSanitizer: data race (pid=11450)
Read of size 8 at 0x7b1400000ba0 by thread T2:
#0 _dbus_counter_get_unix_fd_value <null> (libdbus-1.so.3+0x63ebc)
#1 unix_fd_thread2 <null> (test-counter+0x405f5b)
#2 <null> <null> (libtsan.so.0+0x3238f)
Previous write of size 8 at 0x7b1400000ba0 by thread T1 (mutexes: write M38):
#0 _dbus_counter_adjust_unix_fd <null> (libdbus-1.so.3+0x63d39)
#1 unix_fd_thread1 <null> (test-counter+0x405eb7)
#2 <null> <null> (libtsan.so.0+0x3238f)
Location is heap block of size 72 at 0x7b1400000b90 allocated by main thread:
#0 calloc <null> (libtsan.so.0+0x3597e)
#1 dbus_malloc0 <null> (libdbus-1.so.3+0x8ad39)
#2 _dbus_counter_new <null> (libdbus-1.so.3+0x637a5)
#3 _dbus_counter_unix_fd_test <null> (test-counter+0x4060fd)
#4 _dbus_test_main <null> (test-counter+0x407b78)
#5 main <null> (test-counter+0x4062d4)
Mutex M38 (0x7b0c000013b0) created at:
#0 pthread_mutex_init <null> (libtsan.so.0+0x4c443)
#1 _dbus_platform_rmutex_new <null> (libdbus-1.so.3+0xa4ef1)
#2 _dbus_rmutex_new_at_location <null> (libdbus-1.so.3+0x6fea9)
#3 _dbus_counter_new <null> (libdbus-1.so.3+0x637dd)
#4 _dbus_counter_unix_fd_test <null> (test-counter+0x4060fd)
#5 _dbus_test_main <null> (test-counter+0x407b78)
#6 main <null> (test-counter+0x4062d4)
Thread T2 (tid=11453, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x5ed35)
#1 _dbus_counter_unix_fd_test <null> (test-counter+0x40614b)
#2 _dbus_test_main <null> (test-counter+0x407b78)
#3 main <null> (test-counter+0x4062d4)
Thread T1 (tid=11452, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x5ed35)
#1 _dbus_counter_unix_fd_test <null> (test-counter+0x40612c)
#2 _dbus_test_main <null> (test-counter+0x407b78)
#3 main <null> (test-counter+0x4062d4)
SUMMARY: ThreadSanitizer: data race (/home/user/src/dbus/build-cmake/lib/libdbus-1.so.3+0x63ebc) in _dbus_counter_get_unix_fd_value
==================
ok 2 - unix_fd
# unix_fd test took 0 seconds
ok 3 - unix_fd did not leak memory
# Running test: size
==================
WARNING: ThreadSanitizer: data race (pid=11450)
Read of size 8 at 0x7b1400000b98 by thread T4:
#0 _dbus_counter_get_size_value <null> (libdbus-1.so.3+0x63e7c)
#1 size_value_thread2 <null> (test-counter+0x40608c)
#2 <null> <null> (libtsan.so.0+0x3238f)
Previous write of size 8 at 0x7b1400000b98 by thread T3 (mutexes: write M52):
#0 _dbus_counter_adjust_size <null> (libdbus-1.so.3+0x63a8e)
#1 size_value_thread1 <null> (test-counter+0x405fe8)
#2 <null> <null> (libtsan.so.0+0x3238f)
Location is heap block of size 72 at 0x7b1400000b90 allocated by main thread:
#0 calloc <null> (libtsan.so.0+0x3597e)
#1 dbus_malloc0 <null> (libdbus-1.so.3+0x8ad39)
#2 _dbus_counter_new <null> (libdbus-1.so.3+0x637a5)
#3 _dbus_counter_size_value_test <null> (test-counter+0x4061d8)
#4 _dbus_test_main <null> (test-counter+0x407b78)
#5 main <null> (test-counter+0x4062d4)
Mutex M52 (0x7b0c000013b0) created at:
#0 pthread_mutex_init <null> (libtsan.so.0+0x4c443)
#1 _dbus_platform_rmutex_new <null> (libdbus-1.so.3+0xa4ef1)
#2 _dbus_rmutex_new_at_location <null> (libdbus-1.so.3+0x6fea9)
#3 _dbus_counter_new <null> (libdbus-1.so.3+0x637dd)
#4 _dbus_counter_size_value_test <null> (test-counter+0x4061d8)
#5 _dbus_test_main <null> (test-counter+0x407b78)
#6 main <null> (test-counter+0x4062d4)
Thread T4 (tid=11455, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x5ed35)
#1 _dbus_counter_size_value_test <null> (test-counter+0x406226)
#2 _dbus_test_main <null> (test-counter+0x407b78)
#3 main <null> (test-counter+0x4062d4)
Thread T3 (tid=11454, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x5ed35)
#1 _dbus_counter_size_value_test <null> (test-counter+0x406207)
#2 _dbus_test_main <null> (test-counter+0x407b78)
#3 main <null> (test-counter+0x4062d4)
SUMMARY: ThreadSanitizer: data race (/home/user/src/dbus/build-cmake/lib/libdbus-1.so.3+0x63e7c) in _dbus_counter_get_size_value
See #426 Fixes #446 (closed)
Edited by Ralf Habacker