Skip to content

Commit

Permalink
[TSan] Handle FreeBSD specific indirection of libpthread functions
Browse files Browse the repository at this point in the history
Similar to 60cc1d3 for NetBSD, add aliases and interceptors for the
following pthread related functions:

- pthread_cond_init(3)
- pthread_cond_destroy(3)
- pthread_cond_signal(3)
- pthread_cond_broadcast(3)
- pthread_cond_wait(3)
- pthread_mutex_init(3)
- pthread_mutex_destroy(3)
- pthread_mutex_lock(3)
- pthread_mutex_trylock(3)
- pthread_mutex_unlock(3)
- pthread_rwlock_init(3)
- pthread_rwlock_destroy(3)
- pthread_rwlock_rdlock(3)
- pthread_rwlock_tryrdlock(3)
- pthread_rwlock_wrlock(3)
- pthread_rwlock_trywrlock(3)
- pthread_rwlock_unlock(3)
- pthread_once(3)
- pthread_sigmask(3)

In FreeBSD's libc, a number of internal aliases of the pthread functions
are invoked, typically with an additional prefixed underscore, e.g.
_pthread_cond_init() and so on.

ThreadSanitizer needs to intercept these aliases too, otherwise some
false positive reports about data races might be produced.

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D119034
  • Loading branch information
DimitryAndric committed Feb 7, 2022
1 parent 74555fd commit 28fb22c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
8 changes: 8 additions & 0 deletions compiler-rt/lib/tsan/rtl/tsan_interceptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ inline bool MustIgnoreInterceptor(ThreadState *thr) {

#define TSAN_INTERCEPTOR(ret, func, ...) INTERCEPTOR(ret, func, __VA_ARGS__)

#if SANITIZER_FREEBSD
# define TSAN_INTERCEPTOR_FREEBSD_ALIAS(ret, func, ...) \
TSAN_INTERCEPTOR(ret, _pthread_##func, __VA_ARGS__) \
ALIAS(WRAPPER_NAME(pthread_##func));
#else
# define TSAN_INTERCEPTOR_FREEBSD_ALIAS(ret, func, ...)
#endif

#if SANITIZER_NETBSD
# define TSAN_INTERCEPTOR_NETBSD_ALIAS(ret, func, ...) \
TSAN_INTERCEPTOR(ret, __libc_##func, __VA_ARGS__) \
Expand Down
69 changes: 57 additions & 12 deletions compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,20 +289,25 @@ void ScopedInterceptor::DisableIgnoresImpl() {
}

#define TSAN_INTERCEPT(func) INTERCEPT_FUNCTION(func)
#if SANITIZER_FREEBSD || SANITIZER_NETBSD
# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION(func)
#else
# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION_VER(func, ver)
#endif
#if SANITIZER_FREEBSD
# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION(func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS_THR(func)
#elif SANITIZER_NETBSD
# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION(func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(func) \
INTERCEPT_FUNCTION(__libc_##func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS_THR(func) \
INTERCEPT_FUNCTION(__libc_thr_##func)
# define TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(func) \
INTERCEPT_FUNCTION(_pthread_##func)
#else
# define TSAN_INTERCEPT_VER(func, ver) INTERCEPT_FUNCTION_VER(func, ver)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS_THR(func)
# define TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(func)
#endif
#if SANITIZER_NETBSD
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(func) \
INTERCEPT_FUNCTION(__libc_##func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS_THR(func) \
INTERCEPT_FUNCTION(__libc_thr_##func)
#else
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(func)
# define TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS_THR(func)
#endif

#define READ_STRING_OF_LEN(thr, pc, s, len, n) \
Expand Down Expand Up @@ -2713,6 +2718,26 @@ TSAN_INTERCEPTOR(void, thr_exit, tid_t *state) {
#define TSAN_MAYBE_INTERCEPT_THR_EXIT
#endif

TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, cond_init, void *c, void *a)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, cond_destroy, void *c)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, cond_signal, void *c)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, cond_broadcast, void *c)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, cond_wait, void *c, void *m)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, mutex_init, void *m, void *a)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, mutex_destroy, void *m)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, mutex_lock, void *m)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, mutex_trylock, void *m)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, mutex_unlock, void *m)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_init, void *l, void *a)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_destroy, void *l)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_rdlock, void *l)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_tryrdlock, void *l)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_wrlock, void *l)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_trywrlock, void *l)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, rwlock_unlock, void *l)
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, once, void *o, void (*i)())
TSAN_INTERCEPTOR_FREEBSD_ALIAS(int, sigmask, int f, void *n, void *o)

TSAN_INTERCEPTOR_NETBSD_ALIAS(int, cond_init, void *c, void *a)
TSAN_INTERCEPTOR_NETBSD_ALIAS(int, cond_signal, void *c)
TSAN_INTERCEPTOR_NETBSD_ALIAS(int, cond_broadcast, void *c)
Expand Down Expand Up @@ -2941,6 +2966,26 @@ void InitializeInterceptors() {
}
#endif

TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(cond_init);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(cond_destroy);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(cond_signal);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(cond_broadcast);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(cond_wait);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(mutex_init);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(mutex_destroy);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(mutex_lock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(mutex_trylock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(mutex_unlock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_init);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_destroy);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_rdlock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_tryrdlock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_wrlock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_trywrlock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(rwlock_unlock);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(once);
TSAN_MAYBE_INTERCEPT_FREEBSD_ALIAS(sigmask);

TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(cond_init);
TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(cond_signal);
TSAN_MAYBE_INTERCEPT_NETBSD_ALIAS(cond_broadcast);
Expand Down

0 comments on commit 28fb22c

Please sign in to comment.