[compiler-rt] 42a0e87 - [compiler-rt][TSan] fix crash caused by intercpting pthread_detach on Android (#161596)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 6 10:49:58 PDT 2025


Author: Fei Peng
Date: 2025-10-06T13:49:54-04:00
New Revision: 42a0e871689d213ff8ea8ad21fe350fd1b337a71

URL: https://github.com/llvm/llvm-project/commit/42a0e871689d213ff8ea8ad21fe350fd1b337a71
DIFF: https://github.com/llvm/llvm-project/commit/42a0e871689d213ff8ea8ad21fe350fd1b337a71.diff

LOG: [compiler-rt][TSan] fix crash caused by intercpting pthread_detach on Android (#161596)

In Bionic, pthread_detach calls pthread_join, so the thread has already
been consumed by the pthread_detach interceptor.


https://android.googlesource.com/platform/bionic/+/refs/heads/android16-release/libc/bionic/pthread_detach.cpp

Added: 
    

Modified: 
    compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index 28e35229bd5e4..37c69b1ca9965 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -1130,6 +1130,22 @@ TSAN_INTERCEPTOR(int, pthread_create,
 
 TSAN_INTERCEPTOR(int, pthread_join, void *th, void **ret) {
   SCOPED_INTERCEPTOR_RAW(pthread_join, th, ret);
+#if SANITIZER_ANDROID
+  {
+    // In Bionic, if the target thread has already exited when pthread_detach is
+    // called, pthread_detach will call pthread_join internally to clean it up.
+    // In that case, the thread has already been consumed by the pthread_detach
+    // interceptor.
+    Tid tid = ctx->thread_registry.FindThread(
+        [](ThreadContextBase* tctx, void* arg) {
+          return tctx->user_id == (uptr)arg;
+        },
+        th);
+    if (tid == kInvalidTid) {
+      return REAL(pthread_join)(th, ret);
+    }
+  }
+#endif
   Tid tid = ThreadConsumeTid(thr, pc, (uptr)th);
   ThreadIgnoreBegin(thr, pc);
   int res = BLOCK_REAL(pthread_join)(th, ret);


        


More information about the llvm-commits mailing list