From c2b6cb074707b3874f47cdcb523cd9cea6ed6410 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Fri, 25 Oct 2013 13:01:31 +0000 Subject: [PATCH] [sanitizer] Intercept pthread_attr_get*. llvm-svn: 193405 --- compiler-rt/lib/asan/asan_interceptors.cc | 2 - compiler-rt/lib/msan/msan_interceptors.cc | 2 - compiler-rt/lib/msan/tests/CMakeLists.txt | 1 + compiler-rt/lib/msan/tests/msan_test.cc | 71 +++++++++++++++++ .../sanitizer_common_interceptors.inc | 76 +++++++++++++++++++ .../sanitizer_platform_interceptors.h | 4 + compiler-rt/lib/tsan/rtl/tsan_interceptors.cc | 5 +- compiler-rt/lib/tsan/rtl/tsan_stat.cc | 11 +++ compiler-rt/lib/tsan/rtl/tsan_stat.h | 10 +++ 9 files changed, 175 insertions(+), 7 deletions(-) diff --git a/compiler-rt/lib/asan/asan_interceptors.cc b/compiler-rt/lib/asan/asan_interceptors.cc index 514c10818b74..42bcbd8e1f58 100644 --- a/compiler-rt/lib/asan/asan_interceptors.cc +++ b/compiler-rt/lib/asan/asan_interceptors.cc @@ -151,8 +151,6 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { } #if ASAN_INTERCEPT_PTHREAD_CREATE -extern "C" int pthread_attr_getdetachstate(void *attr, int *v); - INTERCEPTOR(int, pthread_create, void *thread, void *attr, void *(*start_routine)(void*), void *arg) { EnsureMainThreadIDIsCorrect(); diff --git a/compiler-rt/lib/msan/msan_interceptors.cc b/compiler-rt/lib/msan/msan_interceptors.cc index aea2ef6b9847..842270dca203 100644 --- a/compiler-rt/lib/msan/msan_interceptors.cc +++ b/compiler-rt/lib/msan/msan_interceptors.cc @@ -1040,8 +1040,6 @@ INTERCEPTOR(int, signal, int signo, uptr cb) { extern "C" int pthread_attr_init(void *attr); extern "C" int pthread_attr_destroy(void *attr); -extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize); -extern "C" int pthread_attr_getstack(void *attr, uptr *stack, uptr *stacksize); extern "C" int pthread_setspecific(unsigned key, const void *v); extern "C" int pthread_yield(); diff --git a/compiler-rt/lib/msan/tests/CMakeLists.txt b/compiler-rt/lib/msan/tests/CMakeLists.txt index 39aea1c9c9ab..9c49f167fa19 100644 --- a/compiler-rt/lib/msan/tests/CMakeLists.txt +++ b/compiler-rt/lib/msan/tests/CMakeLists.txt @@ -51,6 +51,7 @@ set(MSAN_UNITTEST_COMMON_CFLAGS -fno-exceptions -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer + -Wno-deprecated-declarations ) set(MSAN_UNITTEST_INSTRUMENTED_CFLAGS ${MSAN_UNITTEST_COMMON_CFLAGS} diff --git a/compiler-rt/lib/msan/tests/msan_test.cc b/compiler-rt/lib/msan/tests/msan_test.cc index 9f6d7d50d0f6..51365828a032 100644 --- a/compiler-rt/lib/msan/tests/msan_test.cc +++ b/compiler-rt/lib/msan/tests/msan_test.cc @@ -2504,6 +2504,77 @@ TEST(MemorySanitizer, PreAllocatedStackThread) { ASSERT_EQ(0, res); } +TEST(MemorySanitizer, pthread_attr_get) { + pthread_attr_t attr; + int res; + res = pthread_attr_init(&attr); + ASSERT_EQ(0, res); + { + int v; + res = pthread_attr_getdetachstate(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + size_t v; + res = pthread_attr_getguardsize(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + struct sched_param v; + res = pthread_attr_getschedparam(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + int v; + res = pthread_attr_getschedpolicy(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + int v; + res = pthread_attr_getinheritsched(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + int v; + res = pthread_attr_getscope(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + void *v; + res = pthread_attr_getstackaddr(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + size_t v; + res = pthread_attr_getstacksize(&attr, &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + { + void *v; + size_t w; + res = pthread_attr_getstack(&attr, &v, &w); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + EXPECT_NOT_POISONED(w); + } + { + cpu_set_t v; + res = pthread_attr_getaffinity_np(&attr, sizeof(v), &v); + ASSERT_EQ(0, res); + EXPECT_NOT_POISONED(v); + } + res = pthread_attr_destroy(&attr); + ASSERT_EQ(0, res); +} + TEST(MemorySanitizer, pthread_getschedparam) { int policy; struct sched_param param; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index e69a21d0b588..270192ad512f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -2540,6 +2540,79 @@ INTERCEPTOR(int, random_r, void *buf, u32 *result) { #define INIT_RANDOM_R #endif +#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \ + SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED +#define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \ + INTERCEPTOR(int, pthread_attr_get##what, void *attr, void *r) { \ + void *ctx; \ + COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_get##what, attr, r); \ + int res = REAL(pthread_attr_get##what)(attr, r); \ + if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \ + return res; \ + } +#endif + +#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET +INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int)) +INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T)) +INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz) +INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int)) +INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int)) +INTERCEPTOR_PTHREAD_ATTR_GET(stackaddr, sizeof(void *)) +INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T)) +INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size); + int res = REAL(pthread_attr_getstack)(attr, addr, size); + if (!res) { + if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr)); + if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size)); + } + return res; +} + +#define INIT_PTHREAD_ATTR_GET \ + INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \ + INTERCEPT_FUNCTION(pthread_attr_getguardsize); \ + INTERCEPT_FUNCTION(pthread_attr_getschedparam); \ + INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \ + INTERCEPT_FUNCTION(pthread_attr_getinheritsched); \ + INTERCEPT_FUNCTION(pthread_attr_getscope); \ + INTERCEPT_FUNCTION(pthread_attr_getstackaddr); \ + INTERCEPT_FUNCTION(pthread_attr_getstacksize); \ + INTERCEPT_FUNCTION(pthread_attr_getstack); \ + INTERCEPT_FUNCTION(pthread_attr_getaffinity_np); +#else +#define INIT_PTHREAD_ATTR_GET +#endif + +#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED +INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int)) + +#define INIT_PTHREAD_ATTR_GETINHERITSCHED \ + INTERCEPT_FUNCTION(pthread_attr_getinheritsched); +#else +#define INIT_PTHREAD_ATTR_GETINHERITSCHED +#endif + +#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP +INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize, + void *cpuset) { + void *ctx; + COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize, + cpuset); + int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset); + if (!res && cpusetsize && cpuset) + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize); + return res; +} + +#define INIT_PTHREAD_ATTR_GETAFFINITY_NP \ + INTERCEPT_FUNCTION(pthread_attr_getaffinity_np); +#else +#define INIT_PTHREAD_ATTR_GETAFFINITY_NP +#endif + #define SANITIZER_COMMON_INTERCEPTORS_INIT \ INIT_STRCMP; \ INIT_STRNCMP; \ @@ -2635,4 +2708,7 @@ INTERCEPTOR(int, random_r, void *buf, u32 *result) { INIT_ETHER_R; \ INIT_SHMCTL; \ INIT_RANDOM_R; \ + INIT_PTHREAD_ATTR_GET; \ + INIT_PTHREAD_ATTR_GETINHERITSCHED; \ + INIT_PTHREAD_ATTR_GETAFFINITY_NP; \ /**/ diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index 8610f1b84edd..048b90769d69 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -137,6 +137,10 @@ # define SANITIZER_INTERCEPT_ETHER_R SI_LINUX_NOT_ANDROID # define SANITIZER_INTERCEPT_SHMCTL SI_LINUX_NOT_ANDROID # define SANITIZER_INTERCEPT_RANDOM_R SI_LINUX_NOT_ANDROID +# define SANITIZER_INTERCEPT_PTHREAD_ATTR_GET SI_NOT_WINDOWS +# define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED \ + SI_MAC || SI_LINUX_NOT_ANDROID +# define SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP SI_LINUX_NOT_ANDROID # define SANITIZER_INTERCEPT__EXIT SI_LINUX diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc index 97742ddfb192..d888cdbc51bf 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc @@ -43,9 +43,8 @@ struct ucontext_t { extern "C" int pthread_attr_init(void *attr); extern "C" int pthread_attr_destroy(void *attr); -extern "C" int pthread_attr_getdetachstate(void *attr, int *v); +DECLARE_REAL(int, pthread_attr_getdetachstate, void *, void *) extern "C" int pthread_attr_setstacksize(void *attr, uptr stacksize); -extern "C" int pthread_attr_getstacksize(void *attr, uptr *stacksize); extern "C" int pthread_key_create(unsigned *key, void (*destructor)(void* v)); extern "C" int pthread_setspecific(unsigned key, const void *v); extern "C" int pthread_mutexattr_gettype(void *a, int *type); @@ -881,7 +880,7 @@ TSAN_INTERCEPTOR(int, pthread_create, attr = &myattr; } int detached = 0; - pthread_attr_getdetachstate(attr, &detached); + REAL(pthread_attr_getdetachstate)(attr, &detached); AdjustStackSizeLinux(attr); ThreadParam p; diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.cc b/compiler-rt/lib/tsan/rtl/tsan_stat.cc index 64c2bd0129d5..773c77872b7c 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_stat.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_stat.cc @@ -395,6 +395,17 @@ void StatOutput(u64 *stat) { name[StatInt_shmctl] = " shmctl "; name[StatInt_random_r] = " random_r "; + name[StatInt_pthread_attr_getdetachstate] = " pthread_addr_getdetachstate "; // NOLINT + name[StatInt_pthread_attr_getguardsize] = " pthread_addr_getguardsize "; // NOLINT + name[StatInt_pthread_attr_getschedparam] = " pthread_addr_getschedparam "; // NOLINT + name[StatInt_pthread_attr_getschedpolicy] = " pthread_addr_getschedpolicy "; // NOLINT + name[StatInt_pthread_attr_getinheritsched] = " pthread_addr_getinheritsched "; // NOLINT + name[StatInt_pthread_attr_getscope] = " pthread_addr_getscope "; // NOLINT + name[StatInt_pthread_attr_getstackaddr] = " pthread_addr_getstackaddr "; // NOLINT + name[StatInt_pthread_attr_getstacksize] = " pthread_addr_getstacksize "; // NOLINT + name[StatInt_pthread_attr_getstack] = " pthread_addr_getstack "; // NOLINT + name[StatInt_pthread_attr_getaffinity_np] = " pthread_addr_getaffinity_np "; // NOLINT + name[StatAnnotation] = "Dynamic annotations "; name[StatAnnotateHappensBefore] = " HappensBefore "; name[StatAnnotateHappensAfter] = " HappensAfter "; diff --git a/compiler-rt/lib/tsan/rtl/tsan_stat.h b/compiler-rt/lib/tsan/rtl/tsan_stat.h index bf7d6c8af18b..45fd507d9ab1 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_stat.h +++ b/compiler-rt/lib/tsan/rtl/tsan_stat.h @@ -389,6 +389,16 @@ enum StatType { StatInt_ether_line, StatInt_shmctl, StatInt_random_r, + StatInt_pthread_attr_getdetachstate, + StatInt_pthread_attr_getguardsize, + StatInt_pthread_attr_getschedparam, + StatInt_pthread_attr_getschedpolicy, + StatInt_pthread_attr_getinheritsched, + StatInt_pthread_attr_getscope, + StatInt_pthread_attr_getstackaddr, + StatInt_pthread_attr_getstacksize, + StatInt_pthread_attr_getstack, + StatInt_pthread_attr_getaffinity_np, // Dynamic annotations. StatAnnotation,