Skip to content

[TSAN] add instrumentation for pthread_mutex_clocklock #75713

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 1 commit into from
Dec 18, 2023

Conversation

Yvan-xy
Copy link
Contributor

@Yvan-xy Yvan-xy commented Dec 16, 2023

The function pthread_mutex_clocklock is not supported by TSAN yet, which is mentioned by llvm/llvm-project/issues/62623. This patch is to handle this function.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2023

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Yvan (Yvan-xy)

Changes

The function pthread_mutex_clocklock is not supported by TSAN yet, which is mentioned by llvm/llvm-project/issues/62623. This patch is to handle this function.


Full diff: https://github.com/llvm/llvm-project/pull/75713.diff

2 Files Affected:

  • (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp (+15)
  • (added) compiler-rt/test/tsan/pthread_mutex_clocklock.cpp (+29)
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 80f86ca98ed9cd..571c3f149d2a80 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -1380,6 +1380,20 @@ TSAN_INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
   return res;
 }
 
+TSAN_INTERCEPTOR(int, pthread_mutex_clocklock, void *m, 
+                 __sanitizer_clockid_t clock, void *abstime) {
+  SCOPED_TSAN_INTERCEPTOR(pthread_mutex_clocklock, m, clock, abstime);
+  MutexPreLock(thr, pc, (uptr)m);
+  int res = REAL(pthread_mutex_clocklock)(m, clock, abstime);
+  if (res == errno_EOWNERDEAD)
+    MutexRepair(thr, pc, (uptr)m);
+  if (res == 0 || res == errno_EOWNERDEAD)
+    MutexPostLock(thr, pc, (uptr)m);
+  if (res == errno_EINVAL)
+    MutexInvalidAccess(thr, pc, (uptr)m);
+  return res;
+}
+
 #if SANITIZER_GLIBC
 #  if !__GLIBC_PREREQ(2, 34)
 // glibc 2.34 applies a non-default version for the two functions. They are no
@@ -2902,6 +2916,7 @@ void InitializeInterceptors() {
   TSAN_INTERCEPT(pthread_mutex_trylock);
   TSAN_INTERCEPT(pthread_mutex_timedlock);
   TSAN_INTERCEPT(pthread_mutex_unlock);
+  TSAN_INTERCEPT(pthread_mutex_clocklock);
 #if SANITIZER_GLIBC
 #  if !__GLIBC_PREREQ(2, 34)
   TSAN_INTERCEPT(__pthread_mutex_lock);
diff --git a/compiler-rt/test/tsan/pthread_mutex_clocklock.cpp b/compiler-rt/test/tsan/pthread_mutex_clocklock.cpp
new file mode 100644
index 00000000000000..dbca0b42039181
--- /dev/null
+++ b/compiler-rt/test/tsan/pthread_mutex_clocklock.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: darwin
+#include <pthread.h>
+#include <stdio.h>
+
+pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
+struct timespec ts = {0};
+
+void *tfunc(void *p) {
+  if (!pthread_mutex_trylock(&m)) {
+    puts("Second thread could not lock mutex");
+    pthread_mutex_unlock(&m);
+  }
+  return p;
+}
+
+int main() {
+  if (!pthread_mutex_clocklock(&m, CLOCK_REALTIME, &ts)) {
+    pthread_t thr;
+    pthread_create(&thr, 0, tfunc, 0);
+    pthread_join(thr, 0);
+    pthread_mutex_unlock(&m);
+  } else
+    puts("Failed to lock mutex");
+  fprintf(stderr, "PASS\n");
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer: unlock of an unlocked mutex
+// CHECK: PASS
\ No newline at end of file

Copy link

github-actions bot commented Dec 16, 2023

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

@Yvan-xy Yvan-xy force-pushed the tsan_pthread_mutex_clocklock branch from 5968418 to a6d0178 Compare December 16, 2023 20:02
@Yvan-xy Yvan-xy force-pushed the tsan_pthread_mutex_clocklock branch from a6d0178 to 6e93be4 Compare December 16, 2023 20:03
@vitalybuka vitalybuka merged commit 5ccad1b into llvm:main Dec 18, 2023
@Yvan-xy Yvan-xy deleted the tsan_pthread_mutex_clocklock branch December 18, 2023 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants