Skip to content

[5.0] Disable TSan in coroutine functions #22223

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
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
10 changes: 8 additions & 2 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1228,13 +1228,19 @@ IRGenSILFunction::IRGenSILFunction(IRGenModule &IGM, SILFunction *f)
}
if (IGM.IRGen.Opts.Sanitizers & SanitizerKind::Thread) {
auto declContext = f->getDeclContext();
if (declContext && isa<DestructorDecl>(declContext))
if (f->getLoweredFunctionType()->isCoroutine()) {
// Disable TSan in coroutines; the instrumentation currently interferes
// with coroutine structural invariants.
} else if (declContext && isa<DestructorDecl>(declContext)) {
// Do not report races in deinit and anything called from it
// because TSan does not observe synchronization between retain
// count dropping to '0' and the object deinitialization.
// Also don't report races in coroutines because the instrumentation
// code breaks coroutine rules.
CurFn->addFnAttr("sanitize_thread_no_checking_at_run_time");
else
} else {
CurFn->addFnAttr(llvm::Attribute::SanitizeThread);
}
}

// Disable inlining of coroutine functions until we split.
Expand Down
20 changes: 17 additions & 3 deletions test/IRGen/tsan-attributes.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
// This test verifies that we add the function attributes used by TSan.

// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -emit-ir -sanitize=thread %s | %FileCheck %s -check-prefix=TSAN
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -emit-ir -disable-llvm-optzns -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

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

// TSAN: Function Attrs: sanitize_thread
// TSAN: define {{.*}} @"$s4main1xSivr"({{.*}}) [[COROUTINE_ATTRS:#[0-9]+]]
public var x: Int {
_read {
yield 0
}
}

// TSAN: attributes [[DEFAULT_ATTRS]] =
// TSAN-SAME: sanitize_thread
// TSAN-SAME: }

// TSAN: attributes [[COROUTINE_ATTRS]] =
// TSAN-NOT: sanitize_address
// TSAN-SAME: }