From f7f5566055bd9b455a2d628a2d659c0f376e50e3 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Thu, 17 Jan 2013 13:38:16 +0000 Subject: [PATCH] [tsan] move prctl interceptor from asan to common_interceptors thus enabling it for tsan too llvm-svn: 172719 --- .../lib/asan/asan_intercepted_functions.h | 2 - compiler-rt/lib/asan/asan_interceptors.cc | 33 ++++---------- .../sanitizer_common_interceptors.h | 43 ++++++++++++------- .../sanitizer_platform_interceptors.h | 2 + compiler-rt/lib/tsan/lit_tests/thread_name.cc | 3 +- compiler-rt/lib/tsan/rtl/tsan_interceptors.cc | 3 +- compiler-rt/lib/tsan/rtl/tsan_stat.cc | 1 + compiler-rt/lib/tsan/rtl/tsan_stat.h | 1 + 8 files changed, 44 insertions(+), 44 deletions(-) diff --git a/compiler-rt/lib/asan/asan_intercepted_functions.h b/compiler-rt/lib/asan/asan_intercepted_functions.h index 1d82940530ec..ff5acdbf3d72 100644 --- a/compiler-rt/lib/asan/asan_intercepted_functions.h +++ b/compiler-rt/lib/asan/asan_intercepted_functions.h @@ -42,10 +42,8 @@ using __sanitizer::uptr; #if defined(__linux__) # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1 -# define ASAN_INTERCEPT_PRCTL 1 #else # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0 -# define ASAN_INTERCEPT_PRCTL 0 #endif #if !defined(__APPLE__) diff --git a/compiler-rt/lib/asan/asan_interceptors.cc b/compiler-rt/lib/asan/asan_interceptors.cc index a1d09e28d39c..ee6975f09128 100644 --- a/compiler-rt/lib/asan/asan_interceptors.cc +++ b/compiler-rt/lib/asan/asan_interceptors.cc @@ -75,6 +75,12 @@ static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) { return internal_strnlen(s, maxlen); } +static void SetThreadName(const char *name) { + AsanThread *t = asanThreadRegistry().GetCurrent(); + if (t) + t->summary()->set_name(name); +} + } // namespace __asan // ---------------------- Wrappers ---------------- {{{1 @@ -83,8 +89,9 @@ using namespace __asan; // NOLINT #define COMMON_INTERCEPTOR_WRITE_RANGE(ptr, size) ASAN_WRITE_RANGE(ptr, size) #define COMMON_INTERCEPTOR_READ_RANGE(ptr, size) ASAN_READ_RANGE(ptr, size) #define COMMON_INTERCEPTOR_ENTER(func, ...) ENSURE_ASAN_INITED() -#define COMMON_INTERCEPTOR_FD_ACQUIRE(fd) -#define COMMON_INTERCEPTOR_FD_RELEASE(fd) +#define COMMON_INTERCEPTOR_FD_ACQUIRE(fd) do { } while (false) +#define COMMON_INTERCEPTOR_FD_RELEASE(fd) do { } while (false) +#define COMMON_INTERCEPTOR_SET_THREAD_NAME(name) SetThreadName(name) #include "sanitizer_common/sanitizer_common_interceptors.h" static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { @@ -166,25 +173,6 @@ INTERCEPTOR(void, siglongjmp, void *env, int val) { } #endif -#if ASAN_INTERCEPT_PRCTL -#define PR_SET_NAME 15 -INTERCEPTOR(int, prctl, int option, - unsigned long arg2, unsigned long arg3, // NOLINT - unsigned long arg4, unsigned long arg5) { // NOLINT - int res = REAL(prctl(option, arg2, arg3, arg4, arg5)); - if (option == PR_SET_NAME) { - AsanThread *t = asanThreadRegistry().GetCurrent(); - if (t) { - char buff[17]; - internal_strncpy(buff, (char*)arg2, 16); - buff[16] = 0; - t->summary()->set_name(buff); - } - } - return res; -} -#endif - #if ASAN_INTERCEPT___CXA_THROW INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) { CHECK(REAL(__cxa_throw)); @@ -731,9 +719,6 @@ void InitializeAsanInterceptors() { #if ASAN_INTERCEPT_SIGLONGJMP ASAN_INTERCEPT_FUNC(siglongjmp); #endif -#if ASAN_INTERCEPT_PRCTL - ASAN_INTERCEPT_FUNC(prctl); -#endif // Intercept exception handling functions. #if ASAN_INTERCEPT___CXA_THROW diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.h index 312e659b9b32..0381216bde4d 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.h @@ -17,6 +17,7 @@ // COMMON_INTERCEPTOR_WRITE_RANGE // COMMON_INTERCEPTOR_FD_ACQUIRE // COMMON_INTERCEPTOR_FD_RELEASE +// COMMON_INTERCEPTOR_SET_THREAD_NAME //===----------------------------------------------------------------------===// #ifndef SANITIZER_COMMON_INTERCEPTORS_H #define SANITIZER_COMMON_INTERCEPTORS_H @@ -34,6 +35,9 @@ INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) { COMMON_INTERCEPTOR_FD_ACQUIRE(fd); return res; } +# define INIT_READ INTERCEPT_FUNCTION(read) +#else +# define INIT_READ #endif #if SANITIZER_INTERCEPT_PREAD @@ -46,6 +50,9 @@ INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) { COMMON_INTERCEPTOR_FD_ACQUIRE(fd); return res; } +# define INIT_PREAD INTERCEPT_FUNCTION(pread) +#else +# define INIT_PREAD #endif #if SANITIZER_INTERCEPT_PREAD64 @@ -58,29 +65,35 @@ INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) { COMMON_INTERCEPTOR_FD_ACQUIRE(fd); return res; } -#endif - -#if SANITIZER_INTERCEPT_READ -# define INIT_READ INTERCEPT_FUNCTION(read) -#else -# define INIT_READ -#endif - -#if SANITIZER_INTERCEPT_PREAD -# define INIT_PREAD INTERCEPT_FUNCTION(pread) -#else -# define INIT_PREAD -#endif - -#if SANITIZER_INTERCEPT_PREAD64 # define INIT_PREAD64 INTERCEPT_FUNCTION(pread64) #else # define INIT_PREAD64 #endif +#if SANITIZER_INTERCEPT_PRCTL +INTERCEPTOR(int, prctl, int option, + unsigned long arg2, unsigned long arg3, // NOLINT + unsigned long arg4, unsigned long arg5) { // NOLINT + COMMON_INTERCEPTOR_ENTER(prctl, option, arg2, arg3, arg4, arg5); + static const int PR_SET_NAME = 15; + int res = REAL(prctl(option, arg2, arg3, arg4, arg5)); + if (option == PR_SET_NAME) { + char buff[16]; + internal_strncpy(buff, (char*)arg2, 15); + buff[15] = 0; + COMMON_INTERCEPTOR_SET_THREAD_NAME(buff); + } + return res; +} +# define INIT_PRCTL INTERCEPT_FUNCTION(prctl) +#else +# define INIT_PRCTL +#endif // SANITIZER_INTERCEPT_PRCTL + #define SANITIZER_COMMON_INTERCEPTORS_INIT \ INIT_READ; \ INIT_PREAD; \ INIT_PREAD64; \ + INIT_PRCTL; \ #endif // SANITIZER_COMMON_INTERCEPTORS_H diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index 57cf42f26379..1402060a1aed 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -24,7 +24,9 @@ #if defined(__linux__) && !defined(ANDROID) # define SANITIZER_INTERCEPT_PREAD64 1 +# define SANITIZER_INTERCEPT_PRCTL 1 #else +# define SANITIZER_INTERCEPT_PRCTL 0 # define SANITIZER_INTERCEPT_PREAD64 0 #endif diff --git a/compiler-rt/lib/tsan/lit_tests/thread_name.cc b/compiler-rt/lib/tsan/lit_tests/thread_name.cc index 9a86f4f29eda..0ca0b1769976 100644 --- a/compiler-rt/lib/tsan/lit_tests/thread_name.cc +++ b/compiler-rt/lib/tsan/lit_tests/thread_name.cc @@ -15,8 +15,7 @@ void *Thread1(void *x) { } void *Thread2(void *x) { - AnnotateThreadName(__FILE__, __LINE__, "Thread2"); - // TODO: pthread_setname_np(pthread_self(), "Thread2"); + pthread_setname_np(pthread_self(), "Thread2"); Global--; return NULL; } diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc index e246d2be99d1..7dc295d40935 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc @@ -1623,9 +1623,10 @@ TSAN_INTERCEPTOR(int, fork, int fake) { #define COMMON_INTERCEPTOR_READ_RANGE(ptr, size) \ MemoryAccessRange(thr, pc, (uptr)ptr, size, false) #define COMMON_INTERCEPTOR_ENTER(func, ...) \ - SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__) + SCOPED_TSAN_INTERCEPTOR(func, __VA_ARGS__) #define COMMON_INTERCEPTOR_FD_ACQUIRE(fd) FdAcquire(thr, pc, fd) #define COMMON_INTERCEPTOR_FD_RELEASE(fd) FdRelease(thr, pc, fd) +#define COMMON_INTERCEPTOR_SET_THREAD_NAME(name) ThreadSetName(thr, name) #include "sanitizer_common/sanitizer_common_interceptors.h" namespace __tsan { diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.cc b/compiler-rt/lib/tsan/rtl/tsan_stat.cc index 645e1d0f5fcd..7c205315f153 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_stat.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_stat.cc @@ -204,6 +204,7 @@ void StatOutput(u64 *stat) { name[StatInt_pipe] = " pipe "; name[StatInt_pipe2] = " pipe2 "; name[StatInt_read] = " read "; + name[StatInt_prctl] = " prctl "; name[StatInt_pread] = " pread "; name[StatInt_pread64] = " pread64 "; name[StatInt_readv] = " readv "; diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.h b/compiler-rt/lib/tsan/rtl/tsan_stat.h index 397e6ee46755..d9e430eaefda 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_stat.h +++ b/compiler-rt/lib/tsan/rtl/tsan_stat.h @@ -199,6 +199,7 @@ enum StatType { StatInt_pipe, StatInt_pipe2, StatInt_read, + StatInt_prctl, StatInt_pread, StatInt_pread64, StatInt_readv,