Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix data races in EHStatusHandlerThread()
Problem: The thread running EHStatusHandlerThread() will race all the rContext->readerState->fields as well as the powerState and some other flags. These flags are both consumed and set by the thread running the ccid driver as well. Solution: Make the flow unidirectional*: the status thread will always set its own copy and "stash" a unique copy ready to be consumed by the driver thread. That copy is consumed and rcontext->readerState is updated in RFCheckReaderStatus() and a few other functions in readerfactory.c. Whenever a copy is consumed it is also freed. Note that even though this adds dynamic allocation it's at most 2 instances of READER_STATE. This PR uses the C11 _Atomic feature. I'm not very familiar with all the compilers pcsc is built with, but if you deem this to be incompatible, please review the overall approach and I can #ifdef and do some mutexes for platforms which do not provide _Atomic * the flow is unidirectional except for the readerName which is logged at the beginning of the status thread. That should not be a problem since the name is set from the driver thread before the status thread is spawned. ================== WARNING: ThreadSanitizer: data race (pid=28786) Write of size 4 at 0x000000f55c10 by thread T3: #0 EHStatusHandlerThread <null> (pcscd+0x4c3aed) Previous read of size 4 at 0x000000f55c10 by main thread: #0 RFWaitForReaderInit <null> (pcscd+0x4d0e27) #1 main <null> (pcscd+0x4c70c6) Location is global 'readerStates' of size 2944 at 0x000000f55b60 (pcscd+0x000000f55c10) Thread T3 (tid=28790, running) created by main thread at: #0 pthread_create <null> (pcscd+0x42be9b) #1 ThreadCreate <null> (pcscd+0x4e1c79) #2 EHSpawnEventHandler <null> (pcscd+0x4c2e9f) #3 RFAddReader <null> (pcscd+0x4cac07) #4 HPAddDevice <null> (pcscd+0x4e0a2e) #5 HPScanUSB <null> (pcscd+0x4dfe65) #6 HPRegisterForHotplugEvents <null> (pcscd+0x4dfd3a) #7 main <null> (pcscd+0x4c705c) SUMMARY: ThreadSanitizer: data race (/home/rousseau/sc/costa/PCSC/src/pcscd+0x4c3aed) in EHStatusHandlerThread ==================
- Loading branch information