Skip to content

Commit 9298944

Browse files
authored
Merge pull request #24756 from apple/tsan-positive-tests
[TSan] Add positive test for TSan + Dispatch on Linux
2 parents 7beb358 + 1f08ed4 commit 9298944

26 files changed

+145
-75
lines changed

test/ClangImporter/Dispatch_test.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %target-typecheck-verify-swift
22

33
// REQUIRES: libdispatch
4+
// UNSUPPORTED: OS=linux-gnu
45

56
import Dispatch
67

test/IRGen/tsan-attributes.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
// RUN: %target-swift-frontend -emit-ir -sanitize=thread %s | %FileCheck %s -check-prefix=TSAN
44

5-
// TSan is currently only supported on 64 bit mac and simulators.
6-
// (We do not test the simulators here.)
7-
// REQUIRES: CPU=x86_64, OS=macosx
5+
// TSan is only supported on 64 bit.
6+
// REQUIRES: PTRSIZE=64
87

98
// TSAN: define {{.*}} @"$s4main4testyyF"() [[DEFAULT_ATTRS:#[0-9]+]]
109
public func test() {

test/IRGen/tsan_coroutines.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// This test case used to crash when tsan ran before co-routine lowering.
22
// RUN: %target-swift-frontend -emit-ir -sanitize=thread %s | %FileCheck %s
33

4-
// TSan is currently only supported on 64 bit mac and simulators.
5-
// (We do not test the simulators here.)
6-
// REQUIRES: CPU=x86_64, OS=macosx
4+
// TSan is only supported on 64 bit.
5+
// REQUIRES: PTRSIZE=64
76

87
public class C { }
98

@@ -23,7 +22,7 @@ extension Foobar {
2322
}
2423

2524
// We used to crash emitting the subscript function.
26-
// CHECK: define swiftcc { i8*, %T15tsan_coroutines1CC* } @"$s15tsan_coroutines6FoobarVyAA1CCAC5IndexVcir"
25+
// CHECK: define{{( dllexport| protected)?}} swiftcc { i8*, %T15tsan_coroutines1CC* } @"$s15tsan_coroutines6FoobarVyAA1CCAC5IndexVcir"
2726
@_borrowed
2827
public subscript(position: Index) -> C {
2928
return things.values[position.myIndex]

test/Profiler/instrprof_tsan.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-swift-frontend -emit-ir -profile-generate -sanitize=thread %s | %FileCheck %s
22

3-
// REQUIRES: OS=macosx
4-
// REQUIRES: CPU=x86_64
3+
// TSan is only supported on 64 bit.
4+
// REQUIRES: PTRSIZE=64
55

66
// CHECK: define {{.*}}empty
77
// CHECK-NOT: load{{.*}}empty

test/Runtime/lazy_witness_table_cycle.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
3-
// REQUIRES: objc_interop
3+
// REQUIRES: foundation
44

55
// SR-5958
66
import Foundation

test/SILGen/tsan_instrumentation.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// RUN: %target-swift-emit-silgen -sanitize=thread %s | %FileCheck %s
2-
// REQUIRES: tsan_runtime
32

4-
// FIXME: This should be covered by "tsan_runtime"; older versions of Apple OSs
5-
// don't support TSan.
6-
// UNSUPPORTED: remote_run
3+
// TSan is only supported on 64 bit.
4+
// REQUIRES: PTRSIZE=64
75

86
func takesInout(_ p: inout Int) { }
97
func takesInout(_ p: inout MyStruct) { }

test/Sanitizers/tsan-emptyarraystorage.swift

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: %target-run %t_tsan-binary 2>&1 | %FileCheck %s
44
// REQUIRES: executable_test
55
// REQUIRES: tsan_runtime
6+
// REQUIRES: foundation
67
// UNSUPPORTED: OS=tvos
78

89
// FIXME: This should be covered by "tsan_runtime"; older versions of Apple OSs
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %target-swiftc_driver %s -g -sanitize=thread %import-libdispatch -o %t_tsan-binary
2+
// RUN: %target-codesign %t_tsan-binary
3+
// RUN: not env %env-TSAN_OPTIONS=abort_on_error=0 %target-run %t_tsan-binary 2>&1 | %FileCheck %s
4+
// REQUIRES: executable_test
5+
// REQUIRES: tsan_runtime
6+
// UNSUPPORTED: OS=tvos
7+
8+
// FIXME: This should be covered by "tsan_runtime"; older versions of Apple OSs
9+
// don't support TSan.
10+
// UNSUPPORTED: remote_run
11+
12+
// Test ThreadSanitizer execution end-to-end with libdispatch.
13+
14+
import Dispatch
15+
16+
let sync1 = DispatchSemaphore(value: 0)
17+
let sync2 = DispatchSemaphore(value: 0)
18+
let finish = DispatchSemaphore(value: 0)
19+
20+
let q = DispatchQueue(label: "q", attributes: .concurrent)
21+
22+
var racy = 1
23+
24+
q.async {
25+
sync1.wait()
26+
sync2.signal()
27+
racy = 2
28+
finish.signal()
29+
}
30+
q.async {
31+
sync1.signal()
32+
sync2.wait()
33+
racy = 3
34+
finish.signal()
35+
}
36+
37+
finish.wait()
38+
finish.wait()
39+
40+
print("Done!")
41+
42+
// CHECK: ThreadSanitizer: data race
43+
// CHECK: Done!

test/Sanitizers/tsan-norace-block-release.swift

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
// RUN: %target-swiftc_driver %s -g -sanitize=thread -target %sanitizers-target-triple -o %t_tsan-binary
1+
// RUN: %target-swiftc_driver %s -g -sanitize=thread %import-libdispatch -target %sanitizers-target-triple -o %t_tsan-binary
22
// RUN: %target-codesign %t_tsan-binary
3-
// RUN: env %env-TSAN_OPTIONS=abort_on_error=0:ignore_interceptors_accesses=1 %target-run %t_tsan-binary 2>&1 | %FileCheck %s
3+
// RUN: env %env-TSAN_OPTIONS=abort_on_error=0:ignore_interceptors_accesses=1 %target-run %t_tsan-binary 2>&1 | %FileCheck %s --implicit-check-not='ThreadSanitizer'
44
// REQUIRES: executable_test
5-
// REQUIRES: objc_interop
65
// REQUIRES: tsan_runtime
76

87
// FIXME: This should be covered by "tsan_runtime"; older versions of Apple OSs
98
// don't support TSan.
109
// UNSUPPORTED: remote_run
1110

1211
// Test that we do not report a race on block release operation.
13-
import Foundation
14-
15-
public class Sad : NSObject {
12+
import Dispatch
13+
#if canImport(Darwin)
14+
import Darwin
15+
#elseif canImport(Glibc)
16+
import Glibc
17+
#else
18+
#error("Unsupported platform")
19+
#endif
20+
21+
public class Sad {
1622
private var _source: DispatchSourceTimer?
17-
public override init() {
23+
public init() {
1824
_source = DispatchSource.makeTimerSource()
1925

2026
// If this line is commented out no data race.
2127
_source?.setEventHandler(handler: globalFuncHandler)
2228

23-
super.init()
2429
_source?.resume()
2530
}
2631
deinit {
@@ -40,4 +45,3 @@ sleep(1)
4045
print("Done.")
4146

4247
// CHECK: Done.
43-
// CHECK-NOT: ThreadSanitizer: data race

test/Sanitizers/tsan-norace-deinit-run-time.swift

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
// RUN: %target-swiftc_driver %s -g -sanitize=thread -target %sanitizers-target-triple -o %t_tsan-binary
1+
// RUN: %target-swiftc_driver %s -g -sanitize=thread %import-libdispatch -target %sanitizers-target-triple -o %t_tsan-binary
22
// RUN: %target-codesign %t_tsan-binary
3-
// RUN: env %env-TSAN_OPTIONS=abort_on_error=0:ignore_interceptors_accesses=1 %target-run %t_tsan-binary 2>&1 | %FileCheck %s
3+
// RUN: env %env-TSAN_OPTIONS=abort_on_error=0:ignore_interceptors_accesses=1 %target-run %t_tsan-binary 2>&1 | %FileCheck %s --implicit-check-not='ThreadSanitizer'
44
// REQUIRES: executable_test
5-
// REQUIRES: objc_interop
65
// REQUIRES: tsan_runtime
76

87
// FIXME: This should be covered by "tsan_runtime"; older versions of Apple OSs
98
// don't support TSan.
109
// UNSUPPORTED: remote_run
1110

1211
// Test that we do not report a race on deinit; the synchronization is guaranteed by runtime.
13-
import Foundation
14-
15-
public class TestDeallocObject : NSObject {
12+
import Dispatch
13+
#if canImport(Darwin)
14+
import Darwin
15+
#elseif canImport(Glibc)
16+
import Glibc
17+
#else
18+
#error("Unsupported platform")
19+
#endif
20+
21+
public class TestDeallocObject {
1622
public var v : Int
17-
public override init() {
23+
public init() {
1824
v = 1
1925
}
2026

@@ -33,7 +39,7 @@ public class TestDeallocObject : NSObject {
3339
}
3440
}
3541

36-
if (true) {
42+
do {
3743
var tdo : TestDeallocObject = TestDeallocObject()
3844
tdo.accessMember()
3945

@@ -52,4 +58,3 @@ if (true) {
5258
print("Done.")
5359

5460
// CHECK: Done.
55-
// CHECK-NOT: ThreadSanitizer: data race

test/Sanitizers/tsan.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread -o %t_tsan-binary
1+
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread %import-libdispatch -o %t_tsan-binary
22
// RUN: %target-codesign %t_tsan-binary
33
// RUN: not env %env-TSAN_OPTIONS="abort_on_error=0" %target-run %t_tsan-binary 2>&1 | %FileCheck %s
44
// REQUIRES: executable_test

test/lit.cfg

+26-26
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,10 @@ if run_vendor == 'apple':
728728
config.target_codesign = "codesign -f -s -"
729729
config.target_runtime = "objc"
730730

731+
config.available_features.add('libdispatch')
732+
config.available_features.add('foundation')
733+
config.available_features.add('objc_interop')
734+
731735
xcrun_prefix = (
732736
"xcrun --toolchain %s --sdk %r" %
733737
(config.darwin_xcrun_toolchain, config.variant_sdk))
@@ -986,6 +990,20 @@ elif run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'windows-cygnus', 'wi
986990
config.target_sdk_name = "linux"
987991
config.target_runtime = "native"
988992
config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract")
993+
994+
libdispatch_artifact_dir = make_path(config.libdispatch_build_path, 'src')
995+
libdispatch_artifacts = [
996+
make_path(libdispatch_artifact_dir, 'libdispatch.so'),
997+
make_path(libdispatch_artifact_dir, 'libswiftDispatch.so'),
998+
make_path(libdispatch_artifact_dir, 'swift', 'Dispatch.swiftmodule')]
999+
if (all(os.path.exists(p) for p in libdispatch_artifacts)):
1000+
config.available_features.add('libdispatch')
1001+
config.libdispatch_artifact_dir = libdispatch_artifact_dir
1002+
libdispatch_source_dir = make_path(config.swift_src_root, os.pardir, 'swift-corelibs-libdispatch')
1003+
libdispatch_swift_module_dir = make_path(libdispatch_artifact_dir, 'swift')
1004+
config.import_libdispatch = ('-I %s -I %s -L %s'
1005+
% (libdispatch_source_dir, libdispatch_swift_module_dir, libdispatch_artifact_dir))
1006+
9891007
config.target_build_swift = (
9901008
'%s -target %s %s %s %s %s %s'
9911009
% (config.swiftc, config.variant_triple, resource_dir_opt, mcp_opt,
@@ -1302,7 +1320,7 @@ runtime_libs = {
13021320
'fuzzer': 'fuzzer_runtime'
13031321
}
13041322

1305-
if run_ptrsize != "32":
1323+
if run_ptrsize == '64' and 'libdispatch' in config.available_features:
13061324
runtime_libs['tsan'] = 'tsan_runtime'
13071325

13081326
check_runtime_libs(runtime_libs)
@@ -1401,14 +1419,15 @@ if os.path.exists(static_libswiftCore_path):
14011419
# default Swift tests to use the just-built libraries
14021420
target_stdlib_path = platform_module_dir
14031421
if not kIsWindows:
1422+
libdispatch_path = getattr(config, 'libdispatch_artifact_dir', '')
14041423
if 'use_os_stdlib' not in lit_config.params:
14051424
lit_config.note('Testing with the just-built libraries at ' + target_stdlib_path)
14061425
config.target_run = (
14071426
"/usr/bin/env "
14081427
"DYLD_LIBRARY_PATH='{0}' " # Apple option
1409-
"LD_LIBRARY_PATH='{0}' " # Linux option
1428+
"LD_LIBRARY_PATH='{0}:{1}' " # Linux option
14101429
"SIMCTL_CHILD_DYLD_LIBRARY_PATH='{0}' " # Simulator option
1411-
.format(target_stdlib_path)) + config.target_run
1430+
.format(target_stdlib_path, libdispatch_path)) + config.target_run
14121431
else:
14131432
os_stdlib_path = ''
14141433
if run_vendor == 'apple':
@@ -1419,9 +1438,9 @@ if not kIsWindows:
14191438
config.target_run = (
14201439
"/usr/bin/env "
14211440
"DYLD_LIBRARY_PATH='{0}' " # Apple option
1422-
"LD_LIBRARY_PATH='{0}' " # Linux option
1441+
"LD_LIBRARY_PATH='{0}:{1}' " # Linux option
14231442
"SIMCTL_CHILD_DYLD_LIBRARY_PATH='{0}' " # Simulator option
1424-
.format(all_stdlib_path)) + config.target_run
1443+
.format(all_stdlib_path, libdispatch_path)) + config.target_run
14251444

14261445
if not getattr(config, 'target_run_simple_swift', None):
14271446
config.target_run_simple_swift_parameterized = \
@@ -1465,7 +1484,7 @@ if not getattr(config, 'target_run_simple_swift', None):
14651484
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run))
14661485

14671486
#
1468-
# When changing substitutions, update docs/Testing.rst.
1487+
# When changing substitutions, update docs/Testing.md.
14691488
#
14701489

14711490
config.substitutions.append(('%target-runtime', config.target_runtime))
@@ -1588,6 +1607,7 @@ config.substitutions.append(('%FileCheck',
15881607
config.filecheck,
15891608
'--enable-windows-compatibility' if kIsWindows else '')))
15901609
config.substitutions.append(('%raw-FileCheck', pipes.quote(config.filecheck)))
1610+
config.substitutions.append(('%import-libdispatch', getattr(config, 'import_libdispatch', '')))
15911611

15921612
if config.lldb_build_root != "":
15931613
config.available_features.add('lldb')
@@ -1616,25 +1636,5 @@ if platform.system() == 'Linux':
16161636
config.available_features.add("LinuxDistribution=" + distributor + '-' + release)
16171637
lit_config.note('Running tests on %s-%s' % (distributor, release))
16181638

1619-
if run_vendor == 'apple':
1620-
config.available_features.add('libdispatch')
1621-
config.available_features.add('foundation')
1622-
config.available_features.add('objc_interop')
1623-
else:
1624-
# TODO(yln): Works with the packaged swift distribution, but not during build.
1625-
# We need to make libdispatch/foundation available in the test resource directory
1626-
# or pass along the proper library include paths in the compiler invocations that are used
1627-
# to build the tests.
1628-
def has_lib(name):
1629-
return False
1630-
1631-
if has_lib('dispatch'):
1632-
config.available_features.add('libdispatch')
1633-
else:
1634-
# TSan runtime requires libdispatch on non-Apple platforms
1635-
config.available_features.discard('tsan_runtime')
1636-
1637-
if has_lib('Foundation'):
1638-
config.available_features.add('foundation')
16391639

16401640
lit_config.note("Available features: " + ", ".join(sorted(config.available_features)))

test/lit.site.cfg.in

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ config.swift_test_results_dir = \
3232

3333
config.coverage_mode = "@SWIFT_ANALYZE_CODE_COVERAGE@"
3434
config.lldb_build_root = "@LLDB_BUILD_DIR@"
35+
config.libdispatch_build_path = "@SWIFT_PATH_TO_LIBDISPATCH_BUILD@"
3536

3637
# --- Darwin ---
3738
config.darwin_xcrun_toolchain = "@SWIFT_DARWIN_XCRUN_TOOLCHAIN@"

test/stdlib/Dispatch.swift

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33
// REQUIRES: libdispatch
4+
// UNSUPPORTED: OS=linux-gnu
45

56
import Dispatch
67
import StdlibUnittest

test/stdlib/DispatchData.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -swift-version 4 %s -o %t/a.out-4 && %target-codesign %t/a.out-4 && %target-run %t/a.out-4
3-
// RUN: %target-build-swift -swift-version 4.2 %s -o %t/a.out-4.2 && %target-codesign %t/a.out-4.2 && %target-run %t/a.out-4.2
2+
// RUN: %target-build-swift -swift-version 4 %s %import-libdispatch -o %t/a.out-4 && %target-codesign %t/a.out-4 && %target-run %t/a.out-4
3+
// RUN: %target-build-swift -swift-version 4.2 %s %import-libdispatch -o %t/a.out-4.2 && %target-codesign %t/a.out-4.2 && %target-run %t/a.out-4.2
44
// REQUIRES: executable_test
55
// REQUIRES: libdispatch
66

@@ -22,11 +22,12 @@ DispatchAPI.test("dispatch_data_t deallocator") {
2222
let q = DispatchQueue(label: "dealloc queue")
2323
var t = 0
2424

25-
autoreleasepool {
25+
do {
2626
let size = 1024
2727
let p = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
2828
let _ = DispatchData(bytesNoCopy: UnsafeBufferPointer(start: p, count: size), deallocator: .custom(q, {
2929
t = 1
30+
p.deallocate();
3031
}))
3132
}
3233

test/stdlib/DispatchDeprecationMacOS.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// RUN: %swift -typecheck -target x86_64-apple-macosx10.9 -verify -sdk %sdk %s
22
// REQUIRES: OS=macosx
3-
// REQUIRES: objc_interop
3+
// REQUIRES: libdispatch
44

5-
import Foundation
65
import Dispatch
76

87
// Don't warn because these APIs were deprecated in macOS 10.10 and the

test/stdlib/DispatchDeprecationWatchOS.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// RUN: %swift -typecheck -target i386-apple-watchos2.0 -verify -sdk %sdk %s
22
// REQUIRES: CPU=i386, OS=watchos
3-
// REQUIRES: objc_interop
3+
// REQUIRES: libdispatch
44

5-
import Foundation
65
import Dispatch
76

87
// These are deprecated on all versions of watchOS.

0 commit comments

Comments
 (0)