Skip to content

[Threading][TSan] Fix TSan errors from lazy init on Linux. #66721

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

Merged
merged 7 commits into from
Jun 22, 2023

Conversation

al45tair
Copy link
Contributor

Move the TSan functionality from Concurrency into Threading. Use it in the Linux ulock implementation so that TSan knows about ulock and will tolerate the newer swift_once implementation that uses it.

This should also slightly improve the performance of Concurrency, since the tsan tests will be inlined rather than it always doing a subroutine call.

rdar://110665213

@al45tair al45tair requested a review from yln June 16, 2023 21:48
@al45tair
Copy link
Contributor Author

@swift-ci Please test

@al45tair al45tair requested a review from mikeash June 16, 2023 21:48
@yln yln requested review from rokhinip and devincoughlin June 17, 2023 02:14
@finagolfin
Copy link
Member

Note that your new tsan test is not run on the linux CI, most likely because of #53973:

UNSUPPORTED: Swift(linux-x86_64) :: Sanitizers/tsan/once.swift (6673 of 16678)

@al45tair
Copy link
Contributor Author

Note that your new tsan test is not run on the linux CI, most likely because of #53973:

I'm aware. I've been manually running locally it for that reason; it took me a while to work out that I needed --libdispatch to do that. Before the fixes, the test fails, after the fixes, the test passes, so it's definitely fixing the underlying bug.

@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

@al45tair al45tair requested a review from weissi June 19, 2023 10:20
if (ulock_fastpath(__atomic_compare_exchange_n(lock, &zero, tid,
true, __ATOMIC_ACQUIRE,
__ATOMIC_RELAXED)))
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D

Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely, thanks so much!

@al45tair
Copy link
Contributor Author

Looks like I've somehow broken things elsewhere. Looking at that now.

@al45tair
Copy link
Contributor Author

al45tair commented Jun 19, 2023

OK, so this is broken on macOS because of the behaviour of RTLD_NEXT there (Darwin doesn't have a flat symbol namespace; RTLD_NEXT there only searches images that the current image was linked with — unlike on, say, Linux, where it searches the loaded images starting at the next one after this in the list). Might need to rearrange things a little.

@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

al45tair added 3 commits June 19, 2023 15:32
Move the TSan functionality from Concurrency into Threading.  Use it
in the Linux `ulock` implementation so that TSan knows about `ulock`
and will tolerate the newer `swift_once` implementation that uses it.

rdar://110665213
* Use the longer name ThreadSanitizer rather than TSan for the new files.
* Don't implement `tsan::consume` at all for now.
* Do the `tsan::release` for `ulock_unlock()` at the head of the function,
  not at the tail.
* Add a comment to test/Sanitizers/tsan/once.swift to explain the test a
  little more clearly.

rdar://110665213
On Darwin, `RTLD_NEXT` doesn't do what we need it to here, with the
result that if `libswiftCore`'s TSan initializer gets found first,
then `libswift_Concurrency` won't have its initializer called at all,
in spite of us using `RTLD_NEXT` to find the next definition.

Fix this by centralising the initializer in `libswiftCore` instead.

rdar://110665213
@al45tair
Copy link
Contributor Author

(Rebased to fix conflict)

@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

@al45tair
Copy link
Contributor Author

I think this version will likely fail the unit tests on Linux, because various things besides the runtime libraries use the Threading library, and not all of them link against the runtime. It looks like I'm going to need to rearrange things a little more.

al45tair added 2 commits June 19, 2023 17:00
We need ThreadSanitizer.cpp in libswiftCore for the runtime case, but
we also need it in libswiftThreading for non-runtime cases.

rdar://1106655213
We need to pick up the `_swift_tsan_xxx` symbols from libswiftCore in
most cases, but sometimes we're statically linked and in that case we
want to use a local copy.

rdar://1106655213
@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

On builds where TSan isn't supported, don't include any code in
ThreadSanitizer.cpp.

rdar://110665213
@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

@al45tair al45tair requested a review from yln June 20, 2023 11:51
Copy link
Contributor

@yln yln left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much Alastair!

LGTM, with one nit.

Please document the annotation APIs in terms of TSan "model synchronization operation" instead of C memory model.

Something like we had before:

/// release() establishes a happens-before relation with a preceding acquire()
/// on the same address.

Something that describes that we are modeling / "teaching" TSan about asynchronization operation (not necessarily lock-based):

T1                | T2

access from y
tsan_release(x)
lock given away
      --> sync point -->     <<< this is what we try to model
                     lock taken
                     tsan_aquire(x)
                     access to y

Updated the comments for tsan::acquire and tsan::release to better
reflect what TSan is actually doing.

rdar://110665213
@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test

@al45tair
Copy link
Contributor Author

@swift-ci Please smoke test Windows platform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants