Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7582] Dispatch as a synchronisation mechanism fails TSan on Linux #655

Closed
weissi opened this issue May 2, 2018 · 5 comments
Closed

Comments

@weissi
Copy link
Member

weissi commented May 2, 2018

Previous ID SR-7582
Radar rdar://problem/40293937
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s libdispatch
Labels Bug, Linux
Assignee @yln
Priority Medium

md5: 91cf68bf77060dfb66150dd2bc982357

Issue Description:

This easy program that should be thread synchronised just fine thanks to Dispatch fails TSan on Linux:

import Dispatch

var a: Int = 0
let q = DispatchQueue(label: "q")
q.async {
    a += 1
}

let aViaThread = q.sync { () -> Int in
    a += 1
    return a
}

print(aViaThread)

compiled with

swiftc -sanitize=thread test.swift

and then run with

./test

should raise no TSan (thread sanitizer) warnings on both Linux and macOS. It works just fine on Darwin but fails TSan on Linux:

$ ./test
==================
WARNING: ThreadSanitizer: data race (pid=1)
  Write of size 8 at 0x7b0c00000020 by main thread:
    #​0 free /code/workspace/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:621:3 (test+0xe461)
    #&#8203;1 _T08Dispatch0A5QueueC5asyncyAA0A5GroupCSg5group_AA0A3QoSV3qosAA0A13WorkItemFlagsV5flagsyyXB7executetF <null> (libdispatch.so+0x3c4ef)
    #&#8203;2 __libc_start_main <null> (libc.so.6+0x21ec4)

  Previous read of size 8 at 0x7b0c00000020 by thread T1:
    #&#8203;0 _T0Ieg_IeyB_TR <null> (test+0xbe59)
    #&#8203;1 _dispatch_call_block_and_release <null> (libdispatch.so+0x4d566)

  Thread T1 (tid=8, running) created by main thread at:
    #&#8203;0 pthread_create /code/workspace/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:889:3 (test+0xf9e3)
    #&#8203;1 _dispatch_global_queue_poke_slow <null> (libdispatch.so+0x59665)
    #&#8203;2 __libc_start_main <null> (libc.so.6+0x21ec4)

SUMMARY: ThreadSanitizer: data race (/usr/lib/swift/linux/libdispatch.so+0x3c4ef) in _T08Dispatch0A5QueueC5asyncyAA0A5GroupCSg5group_AA0A3QoSV3qosAA0A13WorkItemFlagsV5flagsyyXB7executetF
==================
==================
WARNING: ThreadSanitizer: data race (pid=1)
  Write of size 8 at 0x7b0c00000028 by main thread:
    #&#8203;0 free /code/workspace/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:621:3 (test+0xe461)
    #&#8203;1 _T08Dispatch0A5QueueC5asyncyAA0A5GroupCSg5group_AA0A3QoSV3qosAA0A13WorkItemFlagsV5flagsyyXB7executetF <null> (libdispatch.so+0x3c4ef)
    #&#8203;2 __libc_start_main <null> (libc.so.6+0x21ec4)

  Previous read of size 8 at 0x7b0c00000028 by thread T1:
    #&#8203;0 _T0Ieg_IeyB_TR <null> (test+0xbe71)
    #&#8203;1 _dispatch_call_block_and_release <null> (libdispatch.so+0x4d566)

  Thread T1 (tid=8, running) created by main thread at:
    #&#8203;0 pthread_create /code/workspace/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:889:3 (test+0xf9e3)
    #&#8203;1 _dispatch_global_queue_poke_slow <null> (libdispatch.so+0x59665)
    #&#8203;2 __libc_start_main <null> (libc.so.6+0x21ec4)

SUMMARY: ThreadSanitizer: data race (/usr/lib/swift/linux/libdispatch.so+0x3c4ef) in _T08Dispatch0A5QueueC5asyncyAA0A5GroupCSg5group_AA0A3QoSV3qosAA0A13WorkItemFlagsV5flagsyyXB7executetF
==================
==================
WARNING: ThreadSanitizer: Swift access race (pid=1)
  Modifying access of Swift variable at 0x5569bb345c88 by main thread:
    #&#8203;0 _T04testSiycfU0_ <null> (test+0xbf95)
    #&#8203;1 _T0Sis5Error_pIgdzo_SisAA_pIgrzo_TR <null> (test+0xc07d)
    #&#8203;2 _T0Sis5Error_pIgdzo_SisAA_pIgrzo_TRTA <null> (test+0xc25e)
    #&#8203;3 _T08Dispatch0A5QueueC11_syncHelper33_F417D752D2C4E9330E1C700411CE0C6ALLxyyycc2fn_xyKc7executexs5Error_pKc6rescuetKlFyxyKccfU_yycfU_ <null> (libdispatch.so+0x3c73b)
    #&#8203;4 __libc_start_main <null> (libc.so.6+0x21ec4)

  Previous write of size 8 at 0x5569bb345c88 by thread T1:
    #&#8203;0 _T04testyycfU_ <null> (test+0xbe08)
    #&#8203;1 _T0Ieg_IeyB_TR <null> (test+0xbe90)
    #&#8203;2 _dispatch_call_block_and_release <null> (libdispatch.so+0x4d566)

  Location is global '_T04test1aSivp' of size 8 at 0x5569bb345c88 (test+0x0000000b1c88)

  Thread T1 (tid=8, running) created by main thread at:
    #&#8203;0 pthread_create /code/workspace/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:889:3 (test+0xf9e3)
    #&#8203;1 _dispatch_global_queue_poke_slow <null> (libdispatch.so+0x59665)
    #&#8203;2 __libc_start_main <null> (libc.so.6+0x21ec4)

SUMMARY: ThreadSanitizer: Swift access race (/tmp/test+0xbf95) in _T04testSiycfU0_
==================
2
ThreadSanitizer: reported 3 warnings

environment: Swift 4.1 release

@weissi
Copy link
Member Author

weissi commented May 6, 2018

CC phabouzit (JIRA User) and @kubamracek

@weissi
Copy link
Member Author

weissi commented May 13, 2018

CC das (JIRA User)

@weissi
Copy link
Member Author

weissi commented May 16, 2018

@swift-ci create

@weissi
Copy link
Member Author

weissi commented Aug 27, 2019

@yln this should be fixed, right?

@yln
Copy link

yln commented Aug 27, 2019

Yes, this is fixed on Linux starting with Swift 5.1.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants