-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41f46ec
[Threading][TSan] Fix TSan errors from lazy init on Linux.
al45tair ee5efff
[Threading][TSan] Update after review comments.
al45tair 8ed8a28
[Threading][TSan] Move ThreadSanitizer.cpp into the main runtime.
al45tair bad716f
[Threading][TSan] Rearrange things again.
al45tair 3f0018d
[Threading][TSan] Fix linkage issue.
al45tair 18b359f
[Threading][TSan] Fix builds where TSan isn't supported.
al45tair f38b9a9
[Threading][TSan] Update comments.
al45tair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//===--- ThreadSanitizer.h - Thread Sanitizer support --------- -*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Helper functions for code that needs to integrate with the thread | ||
// sanitizer. In particular, TSan can't see inside the runtime libraries, | ||
// so we occasionally need to give it a hint that we're doing synchronization | ||
// in order to avoid false positives. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_THREADING_THREAD_SANITIZER_H | ||
#define SWIFT_THREADING_THREAD_SANITIZER_H | ||
|
||
#include "swift/shims/Visibility.h" | ||
|
||
namespace swift { | ||
|
||
#if defined(_WIN32) || defined(__wasi__) || !__has_include(<dlfcn.h>) | ||
|
||
#define SWIFT_THREADING_TSAN_SUPPORT 0 | ||
|
||
namespace tsan { | ||
|
||
inline bool enabled() { return false; } | ||
template <typename T> T *acquire(T *ptr) { return ptr; } | ||
template <typename T> T *release(T *ptr) { return ptr; } | ||
|
||
} // namespace tsan | ||
#else | ||
|
||
#define SWIFT_THREADING_TSAN_SUPPORT 1 | ||
|
||
// If we're static linking to libswiftThreading.a, these symbols can come | ||
// from there. If, on the other hand, we're dynamically linked, we want | ||
// to get them from libswiftCore.dylib instead. | ||
#if SWIFT_THREADING_STATIC | ||
#define SWIFT_THREADING_EXPORT extern "C" | ||
#else | ||
#define SWIFT_THREADING_EXPORT SWIFT_RUNTIME_EXPORT | ||
#endif | ||
|
||
namespace threading_impl { | ||
|
||
SWIFT_THREADING_EXPORT bool _swift_tsan_enabled; | ||
SWIFT_THREADING_EXPORT void (*_swift_tsan_acquire)(const void *ptr); | ||
SWIFT_THREADING_EXPORT void (*_swift_tsan_release)(const void *ptr); | ||
|
||
} // namespace threading_impl | ||
|
||
namespace tsan { | ||
|
||
/// Returns true if TSan is enabled | ||
inline bool enabled() { | ||
return threading_impl::_swift_tsan_enabled; | ||
} | ||
|
||
/// Inform TSan about a synchronization operation. | ||
/// | ||
/// This is used when TSan cannot see the synchronization operation, for | ||
/// example, if it is using a custom primitive for which TSan doesn't have | ||
/// a built-in interceptor. This does not necessarily mean a lock or a C(++) | ||
/// atomic operation - it could be any kind of synchronization mechanism. | ||
/// | ||
/// An acquire-release pair using the same address establishes an ordering | ||
/// constraint in TSan's happens-before graph, which TSan uses to determine | ||
/// whether two memory accesses from different threads have a well-defined | ||
/// order. | ||
/// | ||
/// For instance, in | ||
/// | ||
/// Thread 1 Thread 2 | ||
/// | ||
/// access to y | ||
/// tsan::release(x) | ||
/// lock given away | ||
/// | ||
/// --> sync point --> | ||
/// | ||
/// lock taken | ||
/// tsan::acquire(x) | ||
/// access to y | ||
/// | ||
/// the access to y from Thread 2 is safe relative to the preceding access to | ||
/// y on Thread 1 because it is preceded by an acquire of x that was itself | ||
/// preceded by a release of x. | ||
template <typename T> | ||
T *acquire(T *ptr) { | ||
if (threading_impl::_swift_tsan_acquire) { | ||
threading_impl::_swift_tsan_acquire(ptr); | ||
} | ||
return ptr; | ||
} | ||
|
||
/// Inform TSan about a synchronization operation. | ||
/// | ||
/// This is the counterpart to tsan::acquire. | ||
template <typename T> | ||
T *release(T *ptr) { | ||
if (threading_impl::_swift_tsan_release) { | ||
threading_impl::_swift_tsan_release(ptr); | ||
} | ||
return ptr; | ||
} | ||
|
||
} // namespace tsan | ||
|
||
#endif | ||
|
||
} // namespace swift | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===--- ThreadSanitizer.cpp - Thread Sanitizer support -------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Helper functions for code that needs to integrate with the thread | ||
// sanitizer. In particular, TSan can't see inside the runtime libraries, | ||
// so we occasionally need to give it a hint that we're doing synchronization | ||
// in order to avoid false positives. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "swift/Threading/ThreadSanitizer.h" | ||
|
||
#if SWIFT_THREADING_TSAN_SUPPORT | ||
|
||
#include "swift/shims/Visibility.h" | ||
|
||
#include <cstdio> | ||
|
||
namespace swift { | ||
namespace threading_impl { | ||
|
||
SWIFT_THREADING_EXPORT bool _swift_tsan_enabled = false; | ||
SWIFT_THREADING_EXPORT void (*_swift_tsan_acquire)(const void *) = nullptr; | ||
SWIFT_THREADING_EXPORT void (*_swift_tsan_release)(const void *) = nullptr; | ||
|
||
#if __has_include(<dlfcn.h>) | ||
#include <dlfcn.h> | ||
|
||
// The TSan library code will call this function when it starts up | ||
extern "C" SWIFT_ATTRIBUTE_FOR_EXPORTS | ||
void __tsan_on_initialize() { | ||
_swift_tsan_enabled = true; | ||
_swift_tsan_acquire = (void (*)(const void *))dlsym(RTLD_DEFAULT, | ||
"__tsan_acquire"); | ||
_swift_tsan_release = (void (*)(const void *))dlsym(RTLD_DEFAULT, | ||
"__tsan_release"); | ||
|
||
// Always call through to the next image; this won't work on macOS, but it's | ||
// important on Linux to allow others to hook into the thread sanitizer if | ||
// they wish. | ||
void (*next_init)(void); | ||
next_init = (void (*)(void))dlsym(RTLD_NEXT, "__tsan_on_initialize"); | ||
if (next_init) { | ||
next_init(); | ||
} | ||
} | ||
#endif // __has_include(<dlfcn.h>) | ||
|
||
} // namespace threading_impl | ||
} // namespace swift | ||
|
||
#endif // SWIFT_THREADING_TSAN_SUPPORT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:D