Skip to content

[TSan] Add positive test for TSan + Dispatch on Linux #24756

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 2 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/ClangImporter/Dispatch_test.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %target-typecheck-verify-swift

// REQUIRES: libdispatch
// UNSUPPORTED: OS=linux-gnu

import Dispatch

Expand Down
5 changes: 2 additions & 3 deletions test/IRGen/tsan-attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

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

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

// TSAN: define {{.*}} @"$s4main4testyyF"() [[DEFAULT_ATTRS:#[0-9]+]]
public func test() {
Expand Down
7 changes: 3 additions & 4 deletions test/IRGen/tsan_coroutines.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// This test case used to crash when tsan ran before co-routine lowering.
// RUN: %target-swift-frontend -emit-ir -sanitize=thread %s | %FileCheck %s

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

public class C { }

Expand All @@ -23,7 +22,7 @@ extension Foobar {
}

// We used to crash emitting the subscript function.
// CHECK: define swiftcc { i8*, %T15tsan_coroutines1CC* } @"$s15tsan_coroutines6FoobarVyAA1CCAC5IndexVcir"
// CHECK: define{{( dllexport| protected)?}} swiftcc { i8*, %T15tsan_coroutines1CC* } @"$s15tsan_coroutines6FoobarVyAA1CCAC5IndexVcir"
@_borrowed
public subscript(position: Index) -> C {
return things.values[position.myIndex]
Expand Down
4 changes: 2 additions & 2 deletions test/Profiler/instrprof_tsan.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-swift-frontend -emit-ir -profile-generate -sanitize=thread %s | %FileCheck %s

// REQUIRES: OS=macosx
// REQUIRES: CPU=x86_64
// TSan is only supported on 64 bit.
// REQUIRES: PTRSIZE=64

// CHECK: define {{.*}}empty
// CHECK-NOT: load{{.*}}empty
Expand Down
2 changes: 1 addition & 1 deletion test/Runtime/lazy_witness_table_cycle.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: foundation

// SR-5958
import Foundation
Expand Down
6 changes: 2 additions & 4 deletions test/SILGen/tsan_instrumentation.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// RUN: %target-swift-emit-silgen -sanitize=thread %s | %FileCheck %s
// REQUIRES: tsan_runtime

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

func takesInout(_ p: inout Int) { }
func takesInout(_ p: inout MyStruct) { }
Expand Down
1 change: 1 addition & 0 deletions test/Sanitizers/tsan-emptyarraystorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// RUN: %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: tsan_runtime
// REQUIRES: foundation
// UNSUPPORTED: OS=tvos

// FIXME: This should be covered by "tsan_runtime"; older versions of Apple OSs
Expand Down
43 changes: 43 additions & 0 deletions test/Sanitizers/tsan-libdispatch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %target-swiftc_driver %s -g -sanitize=thread %import-libdispatch -o %t_tsan-binary
// RUN: %target-codesign %t_tsan-binary
// RUN: not env %env-TSAN_OPTIONS=abort_on_error=0 %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: tsan_runtime
// UNSUPPORTED: OS=tvos

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

// Test ThreadSanitizer execution end-to-end with libdispatch.

import Dispatch

let sync1 = DispatchSemaphore(value: 0)
let sync2 = DispatchSemaphore(value: 0)
let finish = DispatchSemaphore(value: 0)

let q = DispatchQueue(label: "q", attributes: .concurrent)

var racy = 1

q.async {
sync1.wait()
sync2.signal()
racy = 2
finish.signal()
}
q.async {
sync1.signal()
sync2.wait()
racy = 3
finish.signal()
}

finish.wait()
finish.wait()

print("Done!")

// CHECK: ThreadSanitizer: data race
// CHECK: Done!
22 changes: 13 additions & 9 deletions test/Sanitizers/tsan-norace-block-release.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// RUN: %target-swiftc_driver %s -g -sanitize=thread -target %sanitizers-target-triple -o %t_tsan-binary
// RUN: %target-swiftc_driver %s -g -sanitize=thread %import-libdispatch -target %sanitizers-target-triple -o %t_tsan-binary
// RUN: %target-codesign %t_tsan-binary
// RUN: env %env-TSAN_OPTIONS=abort_on_error=0:ignore_interceptors_accesses=1 %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// 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'
// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: tsan_runtime

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

// Test that we do not report a race on block release operation.
import Foundation

public class Sad : NSObject {
import Dispatch
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#else
#error("Unsupported platform")
#endif

public class Sad {
private var _source: DispatchSourceTimer?
public override init() {
public init() {
_source = DispatchSource.makeTimerSource()

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

super.init()
_source?.resume()
}
deinit {
Expand All @@ -40,4 +45,3 @@ sleep(1)
print("Done.")

// CHECK: Done.
// CHECK-NOT: ThreadSanitizer: data race
23 changes: 14 additions & 9 deletions test/Sanitizers/tsan-norace-deinit-run-time.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// RUN: %target-swiftc_driver %s -g -sanitize=thread -target %sanitizers-target-triple -o %t_tsan-binary
// RUN: %target-swiftc_driver %s -g -sanitize=thread %import-libdispatch -target %sanitizers-target-triple -o %t_tsan-binary
// RUN: %target-codesign %t_tsan-binary
// RUN: env %env-TSAN_OPTIONS=abort_on_error=0:ignore_interceptors_accesses=1 %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// 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'
// REQUIRES: executable_test
// REQUIRES: objc_interop
// REQUIRES: tsan_runtime

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

// Test that we do not report a race on deinit; the synchronization is guaranteed by runtime.
import Foundation

public class TestDeallocObject : NSObject {
import Dispatch
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#else
#error("Unsupported platform")
#endif

public class TestDeallocObject {
public var v : Int
public override init() {
public init() {
v = 1
}

Expand All @@ -33,7 +39,7 @@ public class TestDeallocObject : NSObject {
}
}

if (true) {
do {
var tdo : TestDeallocObject = TestDeallocObject()
tdo.accessMember()

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

// CHECK: Done.
// CHECK-NOT: ThreadSanitizer: data race
2 changes: 1 addition & 1 deletion test/Sanitizers/tsan.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread -o %t_tsan-binary
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread %import-libdispatch -o %t_tsan-binary
// RUN: %target-codesign %t_tsan-binary
// RUN: not env %env-TSAN_OPTIONS="abort_on_error=0" %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// REQUIRES: executable_test
Expand Down
52 changes: 26 additions & 26 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ if run_vendor == 'apple':
config.target_codesign = "codesign -f -s -"
config.target_runtime = "objc"

config.available_features.add('libdispatch')
config.available_features.add('foundation')
config.available_features.add('objc_interop')

xcrun_prefix = (
"xcrun --toolchain %s --sdk %r" %
(config.darwin_xcrun_toolchain, config.variant_sdk))
Expand Down Expand Up @@ -978,6 +982,20 @@ elif run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'windows-cygnus', 'wi
config.target_sdk_name = "linux"
config.target_runtime = "native"
config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract")

libdispatch_artifact_dir = make_path(config.libdispatch_build_path, 'src')
libdispatch_artifacts = [
make_path(libdispatch_artifact_dir, 'libdispatch.so'),
make_path(libdispatch_artifact_dir, 'libswiftDispatch.so'),
make_path(libdispatch_artifact_dir, 'swift', 'Dispatch.swiftmodule')]
if (all(os.path.exists(p) for p in libdispatch_artifacts)):
config.available_features.add('libdispatch')
config.libdispatch_artifact_dir = libdispatch_artifact_dir
libdispatch_source_dir = make_path(config.swift_src_root, os.pardir, 'swift-corelibs-libdispatch')
libdispatch_swift_module_dir = make_path(libdispatch_artifact_dir, 'swift')
config.import_libdispatch = ('-I %s -I %s -L %s'
% (libdispatch_source_dir, libdispatch_swift_module_dir, libdispatch_artifact_dir))

config.target_build_swift = (
'%s -target %s %s %s %s %s %s'
% (config.swiftc, config.variant_triple, resource_dir_opt, mcp_opt,
Expand Down Expand Up @@ -1294,7 +1312,7 @@ runtime_libs = {
'fuzzer': 'fuzzer_runtime'
}

if run_ptrsize != "32":
if run_ptrsize == '64' and 'libdispatch' in config.available_features:
runtime_libs['tsan'] = 'tsan_runtime'

check_runtime_libs(runtime_libs)
Expand Down Expand Up @@ -1393,14 +1411,15 @@ if os.path.exists(static_libswiftCore_path):
# default Swift tests to use the just-built libraries
target_stdlib_path = platform_module_dir
if not kIsWindows:
libdispatch_path = getattr(config, 'libdispatch_artifact_dir', '')
if 'use_os_stdlib' not in lit_config.params:
lit_config.note('Testing with the just-built libraries at ' + target_stdlib_path)
config.target_run = (
"/usr/bin/env "
"DYLD_LIBRARY_PATH='{0}' " # Apple option
"LD_LIBRARY_PATH='{0}' " # Linux option
"LD_LIBRARY_PATH='{0}:{1}' " # Linux option
"SIMCTL_CHILD_DYLD_LIBRARY_PATH='{0}' " # Simulator option
.format(target_stdlib_path)) + config.target_run
.format(target_stdlib_path, libdispatch_path)) + config.target_run
else:
os_stdlib_path = ''
if run_vendor == 'apple':
Expand All @@ -1411,9 +1430,9 @@ if not kIsWindows:
config.target_run = (
"/usr/bin/env "
"DYLD_LIBRARY_PATH='{0}' " # Apple option
"LD_LIBRARY_PATH='{0}' " # Linux option
"LD_LIBRARY_PATH='{0}:{1}' " # Linux option
"SIMCTL_CHILD_DYLD_LIBRARY_PATH='{0}' " # Simulator option
.format(all_stdlib_path)) + config.target_run
.format(all_stdlib_path, libdispatch_path)) + config.target_run

if not getattr(config, 'target_run_simple_swift', None):
config.target_run_simple_swift_parameterized = \
Expand Down Expand Up @@ -1457,7 +1476,7 @@ if not getattr(config, 'target_run_simple_swift', None):
% (config.target_build_swift, mcp_opt, config.target_codesign, config.target_run))

#
# When changing substitutions, update docs/Testing.rst.
# When changing substitutions, update docs/Testing.md.
#

config.substitutions.append(('%target-runtime', config.target_runtime))
Expand Down Expand Up @@ -1580,6 +1599,7 @@ config.substitutions.append(('%FileCheck',
config.filecheck,
'--enable-windows-compatibility' if kIsWindows else '')))
config.substitutions.append(('%raw-FileCheck', pipes.quote(config.filecheck)))
config.substitutions.append(('%import-libdispatch', getattr(config, 'import_libdispatch', '')))

if config.lldb_build_root != "":
config.available_features.add('lldb')
Expand Down Expand Up @@ -1608,25 +1628,5 @@ if platform.system() == 'Linux':
config.available_features.add("LinuxDistribution=" + distributor + '-' + release)
lit_config.note('Running tests on %s-%s' % (distributor, release))

if run_vendor == 'apple':
config.available_features.add('libdispatch')
config.available_features.add('foundation')
config.available_features.add('objc_interop')
else:
# TODO(yln): Works with the packaged swift distribution, but not during build.
# We need to make libdispatch/foundation available in the test resource directory
# or pass along the proper library include paths in the compiler invocations that are used
# to build the tests.
def has_lib(name):
return False

if has_lib('dispatch'):
config.available_features.add('libdispatch')
else:
# TSan runtime requires libdispatch on non-Apple platforms
config.available_features.discard('tsan_runtime')

if has_lib('Foundation'):
config.available_features.add('foundation')

lit_config.note("Available features: " + ", ".join(sorted(config.available_features)))
1 change: 1 addition & 0 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ config.swift_test_results_dir = \

config.coverage_mode = "@SWIFT_ANALYZE_CODE_COVERAGE@"
config.lldb_build_root = "@LLDB_BUILD_DIR@"
config.libdispatch_build_path = "@SWIFT_PATH_TO_LIBDISPATCH_BUILD@"

# --- Darwin ---
config.darwin_xcrun_toolchain = "@SWIFT_DARWIN_XCRUN_TOOLCHAIN@"
Expand Down
1 change: 1 addition & 0 deletions test/stdlib/Dispatch.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: libdispatch
// UNSUPPORTED: OS=linux-gnu

import Dispatch
import StdlibUnittest
Expand Down
7 changes: 4 additions & 3 deletions test/stdlib/DispatchData.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// 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
// 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
// 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
// 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
// REQUIRES: executable_test
// REQUIRES: libdispatch

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

autoreleasepool {
do {
let size = 1024
let p = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
let _ = DispatchData(bytesNoCopy: UnsafeBufferPointer(start: p, count: size), deallocator: .custom(q, {
t = 1
p.deallocate();
}))
}

Expand Down
3 changes: 1 addition & 2 deletions test/stdlib/DispatchDeprecationMacOS.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// RUN: %swift -typecheck -target x86_64-apple-macosx10.9 -verify -sdk %sdk %s
// REQUIRES: OS=macosx
// REQUIRES: objc_interop
// REQUIRES: libdispatch

import Foundation
import Dispatch

// Don't warn because these APIs were deprecated in macOS 10.10 and the
Expand Down
3 changes: 1 addition & 2 deletions test/stdlib/DispatchDeprecationWatchOS.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// RUN: %swift -typecheck -target i386-apple-watchos2.0 -verify -sdk %sdk %s
// REQUIRES: CPU=i386, OS=watchos
// REQUIRES: objc_interop
// REQUIRES: libdispatch

import Foundation
import Dispatch

// These are deprecated on all versions of watchOS.
Expand Down
Loading