-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add experimental support for Thread Sanitizer #2076
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92fae2e
Add experimental support for Thread Sanitizer.
AnnaZaks f49e6ad
Fix a data race in the conformsToProtocol method.
AnnaZaks 5320def
Tweak the memory ordering in ConcurrentMap
AnnaZaks d81c6ba
[tsan] Suppress a benign race during metadata cache lookup
AnnaZaks 246bc7a
[stdlib] Fix a data race in Array initialization code
AnnaZaks 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
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
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 |
---|---|---|
|
@@ -16,14 +16,18 @@ | |
//===----------------------------------------------------------------------===// | ||
|
||
#include "swift/Option/SanitizerOptions.h" | ||
#include "swift/Basic/Platform.h" | ||
#include "swift/AST/DiagnosticEngine.h" | ||
#include "swift/AST/DiagnosticsFrontend.h" | ||
#include "llvm/ADT/StringSwitch.h" | ||
using namespace swift; | ||
|
||
static StringRef toStringRef(const SanitizerKind kind) { | ||
switch (kind) { | ||
case SanitizerKind::Address: | ||
return "address"; | ||
case SanitizerKind::Thread: | ||
return "thread"; | ||
case SanitizerKind::None: | ||
llvm_unreachable("Getting a name for SanitizerKind::None"); | ||
} | ||
|
@@ -36,23 +40,50 @@ SanitizerKind swift::parseSanitizerArgValues(const llvm::opt::Arg *A, | |
SanitizerKind kind = SanitizerKind::None; | ||
|
||
// Find the sanitizer kind. | ||
// TODO: Add support for dealing with multiple sanitizers. | ||
SanitizerKind pKind = SanitizerKind::None; | ||
for (int i = 0, n = A->getNumValues(); i != n; ++i) { | ||
const char *Value = A->getValue(i); | ||
if (StringRef(Value).equals("address")) { | ||
kind = SanitizerKind::Address; | ||
} else { | ||
kind = | ||
llvm::StringSwitch<SanitizerKind>(A->getValue(i)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird indentation here. |
||
.Case("address", SanitizerKind::Address) | ||
.Case("thread", SanitizerKind::Thread) | ||
.Default(SanitizerKind::None); | ||
|
||
if (kind == SanitizerKind::None) { | ||
Diags.diagnose(SourceLoc(), diag::error_unsupported_option_argument, | ||
A->getOption().getName(), A->getValue(i)); | ||
A->getOption().getPrefixedName(), A->getValue(i)); | ||
return kind; | ||
} | ||
|
||
// Currently, more than one sanitizer cannot be enabled at the same time. | ||
if (pKind != SanitizerKind::None && pKind != kind) { | ||
SmallString<128> pb; | ||
SmallString<128> b; | ||
Diags.diagnose(SourceLoc(), diag::error_argument_not_allowed_with, | ||
(A->getOption().getPrefixedName() + toStringRef(pKind)).toStringRef(pb), | ||
(A->getOption().getPrefixedName() + toStringRef(kind)).toStringRef(b)); | ||
} | ||
pKind = kind; | ||
} | ||
|
||
if (kind == SanitizerKind::None) | ||
return kind; | ||
|
||
// Check if the target is supported for this sanitizer. | ||
if (!Triple.isOSDarwin() && kind != SanitizerKind::None) { | ||
SmallVector<char, 128> buffer; | ||
// None of the sanitizers work on Linux right now. | ||
if (!Triple.isOSDarwin()) { | ||
SmallString<128> b; | ||
Diags.diagnose(SourceLoc(), diag::error_unsupported_opt_for_target, | ||
(A->getOption().getPrefixedName() + toStringRef(kind)).toStringRef(b), | ||
Triple.getTriple()); | ||
} | ||
// Thread Sanitizer only works on OS X and the simulators. It's only supported | ||
// on 64 bit arcitectures. | ||
if (kind == SanitizerKind::Thread && | ||
(!(Triple.isMacOSX() || tripleIsAnySimulator(Triple)) || | ||
!Triple.isArch64Bit())) { | ||
SmallString<128> b; | ||
Diags.diagnose(SourceLoc(), diag::error_unsupported_opt_for_target, | ||
(A->getOption().getName() + toStringRef(kind)).toStringRef(buffer), | ||
(A->getOption().getPrefixedName() + toStringRef(kind)).toStringRef(b), | ||
Triple.getTriple()); | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -1,31 +1,54 @@ | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-apple-macosx10.9 %s | FileCheck -check-prefix=ASAN -check-prefix=OSX %s | ||
|
||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-apple-ios7.1 %s | FileCheck -check-prefix=ASAN -check-prefix=IOSSIM %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target arm64-apple-ios7.1 %s | FileCheck -check-prefix=ASAN -check-prefix=IOS %s | ||
|
||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-apple-tvos9.0 %s | FileCheck -check-prefix=ASAN -check-prefix=tvOS_SIM %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target arm64-apple-tvos9.0 %s | FileCheck -check-prefix=ASAN -check-prefix=tvOS %s | ||
|
||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target i386-apple-watchos2.0 %s | FileCheck -check-prefix=ASAN -check-prefix=watchOS_SIM %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target armv7k-apple-watchos2.0 %s | FileCheck -check-prefix=ASAN -check-prefix=watchOS %s | ||
|
||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-unknown-linux-gnu %s 2>&1 | FileCheck -check-prefix=LINUX %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-apple-macosx10.9 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_OSX %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-apple-ios7.1 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_IOSSIM %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target arm64-apple-ios7.1 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_IOS %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-apple-tvos9.0 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_tvOS_SIM %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target arm64-apple-tvos9.0 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_tvOS %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target i386-apple-watchos2.0 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_watchOS_SIM %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=address -target armv7k-apple-watchos2.0 %s | FileCheck -check-prefix=ASAN -check-prefix=ASAN_watchOS %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=address -target x86_64-unknown-linux-gnu %s 2>&1 | FileCheck -check-prefix=ASAN_LINUX %s | ||
|
||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=thread -target x86_64-apple-macosx10.9 %s | FileCheck -check-prefix=TSAN -check-prefix=TSAN_OSX %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=thread -target x86-apple-macosx10.9 %s 2>&1 | FileCheck -check-prefix=TSAN_OSX_32 %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=thread -target x86_64-apple-ios7.1 %s | FileCheck -check-prefix=TSAN -check-prefix=TSAN_IOSSIM %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=thread -target arm64-apple-ios7.1 %s 2>&1 | FileCheck -check-prefix=TSAN_IOS %s | ||
// RUN: %swiftc_driver -driver-print-jobs -sanitize=thread -target x86_64-apple-tvos9.0 %s | FileCheck -check-prefix=TSAN -check-prefix=TSAN_tvOS_SIM %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=thread -target arm64-apple-tvos9.0 %s 2>&1 | FileCheck -check-prefix=TSAN_tvOS %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=thread -target i386-apple-watchos2.0 %s 2>&1 | FileCheck -check-prefix=TSAN_watchOS_SIM %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=thread -target armv7k-apple-watchos2.0 %s 2>&1 | FileCheck -check-prefix=TSAN_watchOS %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=thread -target x86_64-unknown-linux-gnu %s 2>&1 | FileCheck -check-prefix=TSAN_LINUX %s | ||
|
||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=address,unknown %s 2>&1 | FileCheck -check-prefix=BADARG %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=address -sanitize=unknown %s 2>&1 | FileCheck -check-prefix=BADARG %s | ||
// RUN: not %swiftc_driver -driver-print-jobs -sanitize=address,thread %s 2>&1 | FileCheck -check-prefix=INCOMPATIBLESANITIZERS %s | ||
|
||
// ASAN: swift | ||
// ASAN: -sanitize=address | ||
|
||
// OSX: lib/swift/clang/lib/darwin/libclang_rt.asan_osx_dynamic.dylib | ||
// IOS: lib/swift/clang/lib/darwin/libclang_rt.asan_ios_dynamic.dylib | ||
// IOSSIM: lib/swift/clang/lib/darwin/libclang_rt.asan_iossim_dynamic.dylib | ||
// tvOS: lib/swift/clang/lib/darwin/libclang_rt.asan_tvos_dynamic.dylib | ||
// tvOS_SIM: lib/swift/clang/lib/darwin/libclang_rt.asan_tvossim_dynamic.dylib | ||
// watchOS: lib/swift/clang/lib/darwin/libclang_rt.asan_watchos_dynamic.dylib | ||
// watchOS_SIM: lib/swift/clang/lib/darwin/libclang_rt.asan_watchossim_dynamic.dylib | ||
// LINUX: unsupported option 'sanitize=address' for target 'x86_64-unknown-linux-gnu' | ||
// ASAN_OSX: lib/swift/clang/lib/darwin/libclang_rt.asan_osx_dynamic.dylib | ||
// ASAN_IOSSIM: lib/swift/clang/lib/darwin/libclang_rt.asan_iossim_dynamic.dylib | ||
// ASAN_IOS: lib/swift/clang/lib/darwin/libclang_rt.asan_ios_dynamic.dylib | ||
// ASAN_tvOS_SIM: lib/swift/clang/lib/darwin/libclang_rt.asan_tvossim_dynamic.dylib | ||
// ASAN_tvOS: lib/swift/clang/lib/darwin/libclang_rt.asan_tvos_dynamic.dylib | ||
// ASAN_watchOS_SIM: lib/swift/clang/lib/darwin/libclang_rt.asan_watchossim_dynamic.dylib | ||
// ASAN_watchOS: lib/swift/clang/lib/darwin/libclang_rt.asan_watchos_dynamic.dylib | ||
// ASAN_LINUX: unsupported option '-sanitize=address' for target 'x86_64-unknown-linux-gnu' | ||
|
||
// ASAN: -rpath @executable_path | ||
|
||
// BADARG: unsupported argument 'unknown' to option 'sanitize=' | ||
// TSAN: swift | ||
// TSAN: -sanitize=thread | ||
|
||
// TSAN_OSX: lib/swift/clang/lib/darwin/libclang_rt.tsan_osx_dynamic.dylib | ||
// TSAN_OSX_32: unsupported option '-sanitize=thread' for target 'x86-apple-macosx10.9' | ||
// TSAN_IOSSIM: lib/swift/clang/lib/darwin/libclang_rt.tsan_iossim_dynamic.dylib | ||
// TSAN_IOS: unsupported option '-sanitize=thread' for target 'arm64-apple-ios7.1' | ||
// TSAN_tvOS_SIM: lib/swift/clang/lib/darwin/libclang_rt.tsan_tvossim_dynamic.dylib | ||
// TSAN_tvOS: unsupported option '-sanitize=thread' for target 'arm64-apple-tvos9.0' | ||
// TSAN_watchOS_SIM: unsupported option '-sanitize=thread' for target 'i386-apple-watchos2.0' | ||
// TSAN_watchOS: unsupported option '-sanitize=thread' for target 'armv7k-apple-watchos2.0' | ||
// TSAN_LINUX: unsupported option '-sanitize=thread' for target 'x86_64-unknown-linux-gnu' | ||
|
||
// TSAN: -rpath @executable_path | ||
|
||
// BADARG: unsupported argument 'unknown' to option '-sanitize=' | ||
// INCOMPATIBLESANITIZERS: argument '-sanitize=address' is not allowed with '-sanitize=thread' |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
// This test verifies that we add the function attributes used by sanitizers. | ||
|
||
// RUN: %target-swift-frontend -emit-ir -sanitize=address %s | FileCheck %s -check-prefix=ASAN | ||
// RUN: %target-swift-frontend -emit-ir -target x86_64-apple-macosx10.9 -sanitize=thread %s | FileCheck %s -check-prefix=TSAN | ||
|
||
// XFAIL: linux | ||
|
||
func test() { | ||
} | ||
|
||
// ASAN: Function Attrs: sanitize_address | ||
// TSAN: Function Attrs: sanitize_thread |
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.
Okay, but I'm going to link to C++ DR 2445 in protest. :)