Skip to content

Commit

Permalink
Access cardAtrLength in a thread safe way
Browse files Browse the repository at this point in the history
The field .cardAtrLength is used to know if a card is inserted and is
accessed from different threads.

We define it as _Atomic to have atomic accesses to the field and fix a
TSAN warning:
==================
WARNING: ThreadSanitizer: data race (pid=14987)
  Read of size 4 at 0x557bacd39cb0 by main thread:
    #0 RFWaitForReaderInit PCSC/src/readerfactory.c:1430:43 (pcscd+0xdda24) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #1 main PCSC/src/pcscdaemon.c:773:2 (pcscd+0xd87d4) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)

  Previous write of size 8 at 0x557bacd39cb0 by thread T3:
    #0 EHStatusHandlerThread PCSC/src/eventhandler.c:314:40 (pcscd+0xd6a47) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)

  Location is global 'readerStates' of size 2944 at 0x557bacd39c00 (pcscd+0x1496cb0)

  Thread T3 (tid=14992, running) created by main thread at:
    #0 pthread_create <null> (pcscd+0x53dfd) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #1 ThreadCreate PCSC/src/utils.c:184:8 (pcscd+0xe38cb) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #2 EHSpawnEventHandler PCSC/src/eventhandler.c:233:7 (pcscd+0xd6960) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #3 RFAddReader PCSC/src/readerfactory.c:397:8 (pcscd+0xdb632) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #4 HPAddDevice PCSC/src/hotplug_libudev.c:512:8 (pcscd+0xe3029) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #5 HPScanUSB PCSC/src/hotplug_libudev.c:579:3 (pcscd+0xe263d) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)
    #6 HPRegisterForHotplugEvents PCSC/src/hotplug_libudev.c:761:2 (pcscd+0xe263d)
    #7 main PCSC/src/pcscdaemon.c:766:7 (pcscd+0xd87c7) (BuildId: b43179fc4c418df0dad65d97dc7a0d9cc226d42f)

SUMMARY: ThreadSanitizer: data race PCSC/src/readerfactory.c:1430:43 in RFWaitForReaderInit
==================

Thanks to Stefan Ehmann for the bug report.
  • Loading branch information
LudovicRousseau committed Nov 19, 2023
1 parent 9466fc0 commit 53f2798
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/eventhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
int32_t readerSharing; /**< PCSCLITE_SHARING_* sharing status */

UCHAR cardAtr[MAX_ATR_SIZE]; /**< ATR */
uint32_t cardAtrLength; /**< ATR length */
_Atomic uint32_t cardAtrLength; /**< ATR length */
uint32_t cardProtocol; /**< SCARD_PROTOCOL_* value */
}
READER_STATE;
Expand Down

0 comments on commit 53f2798

Please sign in to comment.