forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TSAN] Add __tsan_check_no_mutexes_held helper
This adds a new helper that can be called from application code to ensure that no mutexes are held on specific code paths. This is useful for multiple scenarios, including ensuring no locks are held: - at thread exit - in peformance-critical code - when a coroutine is suspended (can cause deadlocks) See this discourse thread for more discussion: https://discourse.llvm.org/t/add-threadsanitizer-check-to-prevent-coroutine-suspending-while-holding-a-lock-potential-deadlock/74051 This resubmits and fixes llvm#69372 (was reverted because of build breakage). This also includes the followup change llvm#71471 (to fix a land race).
- Loading branch information
Showing
8 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,34 @@ | ||
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s | ||
#include "test.h" | ||
|
||
pthread_mutex_t mtx; | ||
|
||
__attribute__((noinline)) void Func1() { | ||
pthread_mutex_lock(&mtx); | ||
__tsan_check_no_mutexes_held(); | ||
pthread_mutex_unlock(&mtx); | ||
} | ||
|
||
__attribute__((noinline)) void Func2() { | ||
pthread_mutex_lock(&mtx); | ||
pthread_mutex_unlock(&mtx); | ||
__tsan_check_no_mutexes_held(); | ||
} | ||
|
||
int main() { | ||
pthread_mutex_init(&mtx, NULL); | ||
Func1(); | ||
Func2(); | ||
return 0; | ||
} | ||
|
||
// CHECK: WARNING: ThreadSanitizer: mutex held in the wrong context | ||
// CHECK: {{.*}}__tsan_check_no_mutexes_held{{.*}} | ||
// CHECK: {{.*}}Func1{{.*}} | ||
// CHECK: {{.*}}main{{.*}} | ||
// CHECK: Mutex {{.*}} created at: | ||
// CHECK: {{.*}}pthread_mutex_init{{.*}} | ||
// CHECK: {{.*}}main{{.*}} | ||
// CHECK: SUMMARY: ThreadSanitizer: mutex held in the wrong context {{.*}}mutex_held_wrong_context.cpp{{.*}}Func1 | ||
|
||
// CHECK-NOT: SUMMARY: ThreadSanitizer: mutex held in the wrong context {{.*}}mutex_held_wrong_context.cpp{{.*}}Func2 |