Skip to content

Commit 42a0e87

Browse files
authored
[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
1 parent 8763812 commit 42a0e87

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,22 @@ TSAN_INTERCEPTOR(int, pthread_create,
11301130

11311131
TSAN_INTERCEPTOR(int, pthread_join, void *th, void **ret) {
11321132
SCOPED_INTERCEPTOR_RAW(pthread_join, th, ret);
1133+
#if SANITIZER_ANDROID
1134+
{
1135+
// In Bionic, if the target thread has already exited when pthread_detach is
1136+
// called, pthread_detach will call pthread_join internally to clean it up.
1137+
// In that case, the thread has already been consumed by the pthread_detach
1138+
// interceptor.
1139+
Tid tid = ctx->thread_registry.FindThread(
1140+
[](ThreadContextBase* tctx, void* arg) {
1141+
return tctx->user_id == (uptr)arg;
1142+
},
1143+
th);
1144+
if (tid == kInvalidTid) {
1145+
return REAL(pthread_join)(th, ret);
1146+
}
1147+
}
1148+
#endif
11331149
Tid tid = ThreadConsumeTid(thr, pc, (uptr)th);
11341150
ThreadIgnoreBegin(thr, pc);
11351151
int res = BLOCK_REAL(pthread_join)(th, ret);

0 commit comments

Comments
 (0)