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

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 1 15:27:43 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Fei Peng (airpfei)

<details>
<summary>Changes</summary>

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


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


1 Files Affected:

- (modified) compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp (+14) 


``````````diff
diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
index b46a81031258c..40bd7e217f4f7 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
@@ -1130,6 +1130,20 @@ 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, pthread_detach calls pthread_join, so 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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/161596


More information about the llvm-commits mailing list