Skip to content

Commit

Permalink
Fix data race issue accessing a logging variable
Browse files Browse the repository at this point in the history
The struct timeval used in log_line() can't be accessed in a atomic way
so we should protect it using a mutex.

==================
WARNING: ThreadSanitizer: data race (pid=5391)
  Read of size 8 at 0x55b3bba7b950 by thread T3 (mutexes: write M17):
    #0 log_line /PCSC-debug/src/debuglog.c:217 (pcscd+0x9d7e)
    #1 log_msg /PCSC-debug/src/debuglog.c:148 (pcscd+0x9a85)
    #2 IFDHPowerICC <null> (libccid.so+0x8a54)
    #3 EHStatusHandlerThread /PCSC-debug/src/eventhandler.c:395 (pcscd+0xbafe)

  Previous write of size 8 at 0x55b3bba7b950 by main thread:
    #0 log_line /PCSC-debug/src/debuglog.c:232 (pcscd+0x9e77)
    #1 log_msg /PCSC-debug/src/debuglog.c:148 (pcscd+0x9a85)
    #2 get_driver /PCSC-debug/src/hotplug_libudev.c:298 (pcscd+0x1f75d)
    #3 HPAddDevice /PCSC-debug/src/hotplug_libudev.c:394 (pcscd+0x1ff32)
    #4 HPScanUSB /PCSC-debug/src/hotplug_libudev.c:579 (pcscd+0x209db)
    #5 HPRegisterForHotplugEvents /PCSC-debug/src/hotplug_libudev.c:761
(pcscd+0x21255)
    #6 main /PCSC-debug/src/pcscdaemon.c:766 (pcscd+0xe6fa)

  Location is global 'last_time.3' of size 16 at 0x55b3bba7b950
(pcscd+0x35950)

  Mutex M17 (0x7b0c000014d0) created at:
    #0 pthread_mutex_init ../../../../src/libsanitizer/tsan/
tsan_interceptors_posix.cpp:1295 (libtsan.so.2+0x50468)
    #1 RFAddReader /PCSC-debug/src/readerfactory.c:355 (pcscd+0x10b8c)
    #2 HPAddDevice /PCSC-debug/src/hotplug_libudev.c:512 (pcscd+0x205b0)
    #3 HPScanUSB /PCSC-debug/src/hotplug_libudev.c:579 (pcscd+0x209db)
    #4 HPRegisterForHotplugEvents /PCSC-debug/src/hotplug_libudev.c:761
(pcscd+0x21255)
    #5 main /PCSC-debug/src/pcscdaemon.c:766 (pcscd+0xe6fa)

  Thread T3 (tid=5395, running) created by main thread at:
    #0 pthread_create ../../../../src/libsanitizer/tsan/
tsan_interceptors_posix.cpp:1001 (libtsan.so.2+0x5e686)
    #1 ThreadCreate /PCSC-debug/src/utils.c:184 (pcscd+0x21755)
    #2 EHSpawnEventHandler /PCSC-debug/src/eventhandler.c:233 (pcscd+0xb2d9)
    #3 RFAddReader /PCSC-debug/src/readerfactory.c:397 (pcscd+0x10f1a)
    #4 HPAddDevice /PCSC-debug/src/hotplug_libudev.c:512 (pcscd+0x205b0)
    #5 HPScanUSB /PCSC-debug/src/hotplug_libudev.c:579 (pcscd+0x209db)
    #6 HPRegisterForHotplugEvents /PCSC-debug/src/hotplug_libudev.c:761
(pcscd+0x21255)
    #7 main /PCSC-debug/src/pcscdaemon.c:766 (pcscd+0xe6fa)

SUMMARY: ThreadSanitizer: data race /PCSC-debug/src/debuglog.c:217 in log_line
==================

Thanks to Stefan Ehmann for the bug report and patch.
  • Loading branch information
LudovicRousseau committed Nov 19, 2023
1 parent b64c8a9 commit b680db2
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/debuglog.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ static char LogLevel = PCSC_LOG_ERROR;

static signed char LogDoColor = 0; /**< no color by default */

static pthread_mutex_t LastTimeMutex = PTHREAD_MUTEX_INITIALIZER;

static void log_line(const int priority, const char *DebugBuffer,
unsigned int rv);

Expand Down Expand Up @@ -213,6 +215,7 @@ static void log_line(const int priority, const char *DebugBuffer,
pthread_t thread_id;
const char *rv_text = NULL;

(void)pthread_mutex_lock(&LastTimeMutex);
gettimeofday(&new_time, NULL);
if (0 == last_time.tv_sec)
last_time = new_time;
Expand All @@ -230,6 +233,7 @@ static void log_line(const int priority, const char *DebugBuffer,
delta = 99999999;

last_time = new_time;
(void)pthread_mutex_unlock(&LastTimeMutex);

thread_id = pthread_self();

Expand Down

0 comments on commit b680db2

Please sign in to comment.