Skip to content

Commit 8f317c1

Browse files
authored
[rtsan] Add versioned pthread_cond interceptors (#155970)
This fixes #146120, confirmed by the original reporter Previously reviewed as #155181, but re-submitting for better book-keeping. Adds versioned pthread_cond interceptors, and the pthread_cond_init/_destroy interceptors
1 parent 75fd5b0 commit 8f317c1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "interception/interception.h"
1717
#include "sanitizer_common/sanitizer_allocator_dlsym.h"
18+
#include "sanitizer_common/sanitizer_glibc_version.h"
1819
#include "sanitizer_common/sanitizer_platform_interceptors.h"
1920

2021
#include "interception/interception.h"
@@ -766,6 +767,12 @@ INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) {
766767
return REAL(pthread_join)(thread, value_ptr);
767768
}
768769

770+
INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *cond,
771+
const pthread_condattr_t *a) {
772+
__rtsan_notify_intercepted_call("pthread_cond_init");
773+
return REAL(pthread_cond_init)(cond, a);
774+
}
775+
769776
INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
770777
__rtsan_notify_intercepted_call("pthread_cond_signal");
771778
return REAL(pthread_cond_signal)(cond);
@@ -788,6 +795,11 @@ INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond,
788795
return REAL(pthread_cond_timedwait)(cond, mutex, ts);
789796
}
790797

798+
INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *cond) {
799+
__rtsan_notify_intercepted_call("pthread_cond_destroy");
800+
return REAL(pthread_cond_destroy)(cond);
801+
}
802+
791803
INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
792804
__rtsan_notify_intercepted_call("pthread_rwlock_rdlock");
793805
return REAL(pthread_rwlock_rdlock)(lock);
@@ -1641,10 +1653,26 @@ void __rtsan::InitializeInterceptors() {
16411653
INTERCEPT_FUNCTION(pthread_mutex_lock);
16421654
INTERCEPT_FUNCTION(pthread_mutex_unlock);
16431655
INTERCEPT_FUNCTION(pthread_join);
1656+
1657+
// See the comment in tsan_interceptors_posix.cpp.
1658+
#if SANITIZER_GLIBC && !__GLIBC_PREREQ(2, 36) && \
1659+
(defined(__x86_64__) || defined(__mips__) || SANITIZER_PPC64V1 || \
1660+
defined(__s390x__))
1661+
INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2");
1662+
INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2");
1663+
INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
1664+
INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2");
1665+
INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
1666+
INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2");
1667+
#else
1668+
INTERCEPT_FUNCTION(pthread_cond_init);
16441669
INTERCEPT_FUNCTION(pthread_cond_signal);
16451670
INTERCEPT_FUNCTION(pthread_cond_broadcast);
16461671
INTERCEPT_FUNCTION(pthread_cond_wait);
16471672
INTERCEPT_FUNCTION(pthread_cond_timedwait);
1673+
INTERCEPT_FUNCTION(pthread_cond_destroy);
1674+
#endif
1675+
16481676
INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
16491677
INTERCEPT_FUNCTION(pthread_rwlock_unlock);
16501678
INTERCEPT_FUNCTION(pthread_rwlock_wrlock);

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,24 @@ TEST(TestRtsanInterceptors, SpinLockLockDiesWhenRealtime) {
12411241
}
12421242
#endif
12431243

1244+
TEST(TestRtsanInterceptors, PthreadCondInitDiesWhenRealtime) {
1245+
pthread_cond_t cond{};
1246+
auto Func = [&cond]() { pthread_cond_init(&cond, nullptr); };
1247+
ExpectRealtimeDeath(Func, "pthread_cond_init");
1248+
ExpectNonRealtimeSurvival(Func);
1249+
}
1250+
1251+
TEST(TestRtsanInterceptors, PthreadCondDestroyDiesWhenRealtime) {
1252+
pthread_cond_t cond{};
1253+
ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
1254+
1255+
auto Func = [&cond]() { pthread_cond_destroy(&cond); };
1256+
ExpectRealtimeDeath(Func, "pthread_cond_destroy");
1257+
ExpectNonRealtimeSurvival(Func);
1258+
1259+
pthread_cond_destroy(&cond);
1260+
}
1261+
12441262
TEST(TestRtsanInterceptors, PthreadCondSignalDiesWhenRealtime) {
12451263
pthread_cond_t cond{};
12461264
ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));

0 commit comments

Comments
 (0)