Skip to content

Commit

Permalink
Fix thread safe issue
Browse files Browse the repository at this point in the history
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 <null> (pcscd+0x53d66) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe)
    #5 __tsan_thread_start_func <null> (pcscd+0x53d66) (BuildId: 4d1e07aaf5145539b5a42a2ed57bb9b51830ddfe)

  As if synchronized via sleep:
    #0 nanosleep <null> (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 <null> (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 <null> (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 <null> (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 <null> (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
==================
  • Loading branch information
LudovicRousseau committed Oct 22, 2023
1 parent 8f197d0 commit 72f2f23
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#define __FUNCTION__ ""
#endif

extern int LogLevel;
extern _Atomic int LogLevel;

#define DEBUG_LEVEL_CRITICAL 1
#define DEBUG_LEVEL_INFO 2
Expand Down
2 changes: 1 addition & 1 deletion src/ifdhandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 72f2f23

Please sign in to comment.