From 72f2f23cac671700dc2e8de21c083ab30bc46f10 Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Sun, 22 Oct 2023 11:08:48 +0200 Subject: [PATCH] Fix thread safe issue Declare LogLevel as _Atomic as it can be accessed from 2 different threads. ================== WARNING: ThreadSanitizer: data race (pid=35789) Write of size 4 at 0x7f123aafe310 by thread T3 (mutexes: write M0): #0 IFDHICCPresence CCID/src/ifdhandler.c:1986:12 (libccid.so+0xaa94) (BuildId: 34d145182e49b1532fafda06218243582b2f9366) #1 IFDStatusICC PCSC/src/ifdwrapper.c:349:7 (pcscd+0xd7725) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #2 EHStatusHandlerThread PCSC/src/eventhandler.c:332:8 (pcscd+0xd6b26) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) Previous read of size 4 at 0x7f123aafe310 by thread T10: #0 IFDHSetCapabilities CCID/src/ifdhandler.c:696:2 (libccid.so+0xb032) (BuildId: 34d145182e49b1532fafda06218243582b2f9366) #1 IFDSetCapabilities PCSC/src/ifdwrapper.c:221:7 (pcscd+0xe6bc7) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #2 SCardSetAttrib PCSC/src/winscard.c:1470:7 (pcscd+0xe6bc7) #3 ContextThread PCSC/src/winscard_svc.c:792:16 (pcscd+0xe6bc7) #4 __tsan_thread_start_func (pcscd+0x53d66) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #5 __tsan_thread_start_func (pcscd+0x53d66) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) As if synchronized via sleep: #0 nanosleep (pcscd+0x515ed) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #1 SYS_USleep PCSC/src/sys_unix.c:87:9 (pcscd+0xd709a) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #2 EHStatusHandlerThread PCSC/src/eventhandler.c (pcscd+0xd709a) #3 __tsan_thread_start_func (pcscd+0x53d66) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) Location is global 'LogLevel' of size 4 at 0x7f123aafe310 (libccid.so+0x22310) Mutex M0 (0x7b0c00000ed0) created at: #0 pthread_mutex_init (pcscd+0x555cf) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #1 RFAddReader PCSC/src/readerfactory.c:355:9 (pcscd+0xdb5aa) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #2 HPAddDevice PCSC/src/hotplug_libudev.c:512:8 (pcscd+0xe33c9) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #3 HPScanUSB PCSC/src/hotplug_libudev.c:579:3 (pcscd+0xe29dd) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #4 HPRegisterForHotplugEvents PCSC/src/hotplug_libudev.c:761:2 (pcscd+0xe29dd) #5 main PCSC/src/pcscdaemon.c:766:7 (pcscd+0xd87e7) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) Thread T3 (tid=35793, running) created by main thread at: #0 pthread_create (pcscd+0x53dfd) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #1 ThreadCreate PCSC/src/utils.c:184:8 (pcscd+0xe3c6b) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #2 EHSpawnEventHandler PCSC/src/eventhandler.c:233:7 (pcscd+0xd6910) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #3 RFAddReader PCSC/src/readerfactory.c:397:8 (pcscd+0xdb7ad) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #4 HPAddDevice PCSC/src/hotplug_libudev.c:512:8 (pcscd+0xe33c9) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #5 HPScanUSB PCSC/src/hotplug_libudev.c:579:3 (pcscd+0xe29dd) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #6 HPRegisterForHotplugEvents PCSC/src/hotplug_libudev.c:761:2 (pcscd+0xe29dd) #7 main PCSC/src/pcscdaemon.c:766:7 (pcscd+0xd87e7) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) Thread T10 (tid=40070, running) created by main thread at: #0 pthread_create (pcscd+0x53dfd) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #1 ThreadCreate PCSC/src/utils.c:184:8 (pcscd+0xe3c6b) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #2 CreateContextThread PCSC/src/winscard_svc.c:239:7 (pcscd+0xd931e) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #3 SVCServiceRunLoop PCSC/src/pcscdaemon.c:131:9 (pcscd+0xd931e) #4 main PCSC/src/pcscdaemon.c:799:2 (pcscd+0xd88e0) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) #5 main PCSC/src/pcscdaemon.c:799:2 (pcscd+0xd88e0) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe) SUMMARY: ThreadSanitizer: data race CCID/src/ifdhandler.c:1986:12 in IFDHICCPresence ================== --- src/debug.h | 2 +- src/ifdhandler.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/debug.h b/src/debug.h index ad2ab14a..d805a929 100644 --- a/src/debug.h +++ b/src/debug.h @@ -40,7 +40,7 @@ #define __FUNCTION__ "" #endif -extern int LogLevel; +extern _Atomic int LogLevel; #define DEBUG_LEVEL_CRITICAL 1 #define DEBUG_LEVEL_INFO 2 diff --git a/src/ifdhandler.c b/src/ifdhandler.c index 50302590..539e5902 100644 --- a/src/ifdhandler.c +++ b/src/ifdhandler.c @@ -64,7 +64,7 @@ static CcidDesc CcidSlots[CCID_DRIVER_MAX_READERS]; static pthread_mutex_t ifdh_context_mutex = PTHREAD_MUTEX_INITIALIZER; #endif -int LogLevel = DEBUG_LEVEL_CRITICAL | DEBUG_LEVEL_INFO; +_Atomic int LogLevel = DEBUG_LEVEL_CRITICAL | DEBUG_LEVEL_INFO; int DriverOptions = 0; int PowerOnVoltage = -1; static bool DebugInitialized = false;