Skip to content

Commit

Permalink
Fix a thread safe issue accessing contextsList
Browse files Browse the repository at this point in the history
ContextsDeinitialize() was called by thread A in the middle of
list_delete() from thread B.
We already have a mutex to protect access to contextsList.

==================
WARNING: ThreadSanitizer: data race (pid=17581)
  Write of size 8 at 0x5606b8d11b60 by main thread:
    #0 list_clear PCSC/src/simclist.c:694:12 (pcscd+0xde66c) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #1 list_destroy PCSC/src/simclist.c:313:5 (pcscd+0xde26c) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #2 ContextsDeinitialize PCSC/src/winscard_svc.c:159:2 (pcscd+0xe4d13) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #3 SVCServiceRunLoop PCSC/src/pcscdaemon.c:122:4 (pcscd+0xd9699) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #4 main PCSC/src/pcscdaemon.c:799:2 (pcscd+0xd8930) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)

  Previous write of size 8 at 0x5606b8d11b60 by thread T6 (mutexes: write M0):
    #0 list_drop_elem PCSC/src/simclist.c:1457:36 (pcscd+0xdf63e) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #1 list_delete_at PCSC/src/simclist.c:580:5 (pcscd+0xdf63e)
    #2 list_delete PCSC/src/simclist.c:563:6 (pcscd+0xdf1c7) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #3 MSGCleanupClient PCSC/src/winscard_svc.c:1083:8 (pcscd+0xe9f6a) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #4 ContextThread PCSC/src/winscard_svc.c:818:2 (pcscd+0xe98fd) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)

  As if synchronized via sleep:
    #0 nanosleep <null> (pcscd+0x515ed) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #1 SYS_Sleep PCSC/src/sys_unix.c:69:9 (pcscd+0xdf8e9) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #2 SVCServiceRunLoop PCSC/src/pcscdaemon.c:117:10 (pcscd+0xd968a) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #3 main PCSC/src/pcscdaemon.c:799:2 (pcscd+0xd8930) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)

  Location is global 'contextsList' of size 120 at 0x5606b8d11b50 (pcscd+0x1497b60)

  Mutex M0 (0x5606b8d11bc8) created at:
    #0 pthread_mutex_init <null> (pcscd+0x555cf) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #1 ContextsInitialize PCSC/src/winscard_svc.c:144:8 (pcscd+0xe4be5) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #2 main PCSC/src/pcscdaemon.c:737:7 (pcscd+0xd8780) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)

  Thread T6 (tid=17591, finished) created by main thread at:
    #0 pthread_create <null> (pcscd+0x53dfd) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #1 ThreadCreate PCSC/src/utils.c:184:8 (pcscd+0xe399b) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #2 CreateContextThread PCSC/src/winscard_svc.c:237:7 (pcscd+0xd936e) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #3 SVCServiceRunLoop PCSC/src/pcscdaemon.c:131:9 (pcscd+0xd936e)
    #4 main PCSC/src/pcscdaemon.c:799:2 (pcscd+0xd8930) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)
    #5 main PCSC/src/pcscdaemon.c:799:2 (pcscd+0xd8930) (BuildId: 807b741f905324f3ef4d96796ee2663eb8beddec)

SUMMARY: ThreadSanitizer: data race PCSC/src/simclist.c:694:12 in list_clear
==================
  • Loading branch information
LudovicRousseau committed Nov 19, 2023
1 parent 53f2798 commit b64c8a9
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/winscard_svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ LONG ContextsInitialize(int customMaxThreadCounter,
void ContextsDeinitialize(void)
{
int listSize;
(void)pthread_mutex_lock(&contextsList_lock);
listSize = list_size(&contextsList);
#ifdef NO_LOG
(void)listSize;
Expand All @@ -157,6 +158,7 @@ void ContextsDeinitialize(void)
/* This is currently a no-op. It should terminate the threads properly. */

list_destroy(&contextsList);
(void)pthread_mutex_unlock(&contextsList_lock);
}

/**
Expand Down

0 comments on commit b64c8a9

Please sign in to comment.