Skip to content

Conversation

airpfei
Copy link
Contributor

@airpfei airpfei commented Oct 1, 2025

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

@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2025

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

Author: Fei Peng (airpfei)

Changes

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 --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);

Copy link

github-actions bot commented Oct 1, 2025

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

@airpfei airpfei force-pushed the fix_pthread_detach_interceptor_crash branch from a35a95d to 528c47b Compare October 1, 2025 22:29
@vitalybuka vitalybuka requested a review from fmayer October 2, 2025 01:42
@fmayer fmayer requested a review from enh-google October 4, 2025 21:04
@airpfei airpfei requested a review from fmayer October 6, 2025 17:43
@fmayer
Copy link
Contributor

fmayer commented Oct 6, 2025

Lgtm. Please also wait for @enh-google to approve.

Copy link
Contributor

@enh-google enh-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd only not clicked "approve" because i was too lazy to also type out "please wait for fmayer" :-)

@enh-google enh-google merged commit 42a0e87 into llvm:main Oct 6, 2025
9 checks passed
@airpfei airpfei deleted the fix_pthread_detach_interceptor_crash branch October 6, 2025 17:52
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.

4 participants