From 69fe0ba415c887c5bd66ebca1a8c7d5fb7c8d890 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Fri, 18 Jan 2013 06:43:13 +0000 Subject: [PATCH] [sanitizer] reapply r172719, r172721-172723, r172725, and also fix the warning on Mac. llvm-svn: 172791 --- .../lib/asan/asan_intercepted_functions.h | 9 ++- compiler-rt/lib/asan/asan_interceptors.cc | 33 +++----- compiler-rt/lib/asan/tests/asan_test.cc | 46 ++++------- compiler-rt/lib/msan/msan_interceptors.cc | 38 +++------ .../sanitizer_common_interceptors.h | 77 ++++++++++++++++--- .../sanitizer_platform_interceptors.h | 19 +++-- compiler-rt/lib/tsan/lit_tests/thread_name.cc | 3 +- compiler-rt/lib/tsan/rtl/tsan_interceptors.cc | 30 +------- compiler-rt/lib/tsan/rtl/tsan_stat.cc | 1 + compiler-rt/lib/tsan/rtl/tsan_stat.h | 1 + 10 files changed, 129 insertions(+), 128 deletions(-) diff --git a/compiler-rt/lib/asan/asan_intercepted_functions.h b/compiler-rt/lib/asan/asan_intercepted_functions.h index 1d82940530ec..25eec8b5d14b 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__) @@ -167,6 +165,13 @@ DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, pread64, int fd, void *buf, SIZE_T count, OFF64_T offset); # endif +#if SANITIZER_INTERCEPT_WRITE +DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, write, int fd, void *ptr, SIZE_T count); +#endif +#if SANITIZER_INTERCEPT_PWRITE +DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count); +#endif + # if ASAN_INTERCEPT_MLOCKX // mlock/munlock DECLARE_FUNCTION_AND_WRAPPER(int, mlock, const void *addr, SIZE_T len); diff --git a/compiler-rt/lib/asan/asan_interceptors.cc b/compiler-rt/lib/asan/asan_interceptors.cc index ef0fde340dd9..b858fd48f916 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); } +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) { @@ -180,25 +187,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)); @@ -745,9 +733,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/asan/tests/asan_test.cc b/compiler-rt/lib/asan/tests/asan_test.cc index f9abc31ae51f..ef7846dbc0d1 100644 --- a/compiler-rt/lib/asan/tests/asan_test.cc +++ b/compiler-rt/lib/asan/tests/asan_test.cc @@ -1667,44 +1667,30 @@ TEST(AddressSanitizer, DISABLED_MemIntrinsicCallByPointerTest) { CallMemTransferByPointer(&memmove); } -#if defined(__linux__) && !defined(ANDROID) && !defined(__ANDROID__) +#if defined(__linux__) && !defined(ANDROID) +#define READ_TEST(READ_N_BYTES) \ + char *x = new char[10]; \ + int fd = open("/proc/self/stat", O_RDONLY); \ + ASSERT_GT(fd, 0); \ + EXPECT_DEATH(READ_N_BYTES, \ + ASAN_PCRE_DOTALL \ + "AddressSanitizer: heap-buffer-overflow" \ + ".* is located 0 bytes to the right of 10-byte region"); \ + close(fd); \ + delete [] x; \ + TEST(AddressSanitizer, pread) { - char *x = new char[10]; - int fd = open("/proc/self/stat", O_RDONLY); - ASSERT_GT(fd, 0); - EXPECT_DEATH(pread(fd, x, 15, 0), - ASAN_PCRE_DOTALL - "AddressSanitizer: heap-buffer-overflow" - ".* is located 0 bytes to the right of 10-byte region"); - close(fd); - delete [] x; + READ_TEST(pread(fd, x, 15, 0)); } TEST(AddressSanitizer, pread64) { - char *x = new char[10]; - int fd = open("/proc/self/stat", O_RDONLY); - ASSERT_GT(fd, 0); - EXPECT_DEATH(pread64(fd, x, 15, 0), - ASAN_PCRE_DOTALL - "AddressSanitizer: heap-buffer-overflow" - ".* is located 0 bytes to the right of 10-byte region"); - close(fd); - delete [] x; + READ_TEST(pread64(fd, x, 15, 0)); } TEST(AddressSanitizer, read) { - char *x = new char[10]; - int fd = open("/proc/self/stat", O_RDONLY); - ASSERT_GT(fd, 0); - EXPECT_DEATH(read(fd, x, 15), - ASAN_PCRE_DOTALL - "AddressSanitizer: heap-buffer-overflow" - ".* is located 0 bytes to the right of 10-byte region"); - close(fd); - delete [] x; + READ_TEST(read(fd, x, 15)); } - -#endif // defined(__linux__) && !defined(ANDROID) && !defined(__ANDROID__) +#endif // defined(__linux__) && !defined(ANDROID) // This test case fails // Clang optimizes memcpy/memset calls which lead to unaligned access diff --git a/compiler-rt/lib/msan/msan_interceptors.cc b/compiler-rt/lib/msan/msan_interceptors.cc index e108156772cc..a739d16e471b 100644 --- a/compiler-rt/lib/msan/msan_interceptors.cc +++ b/compiler-rt/lib/msan/msan_interceptors.cc @@ -67,30 +67,6 @@ INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb, return res; } -INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) { - ENSURE_MSAN_INITED(); - SSIZE_T res = REAL(read)(fd, ptr, count); - if (res > 0) - __msan_unpoison(ptr, res); - return res; -} - -INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) { - ENSURE_MSAN_INITED(); - SSIZE_T res = REAL(pread)(fd, ptr, count, offset); - if (res > 0) - __msan_unpoison(ptr, res); - return res; -} - -INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) { - ENSURE_MSAN_INITED(); - SSIZE_T res = REAL(pread64)(fd, ptr, count, offset); - if (res > 0) - __msan_unpoison(ptr, res); - return res; -} - INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) { ENSURE_MSAN_INITED(); SSIZE_T res = REAL(readlink)(path, buf, bufsiz); @@ -759,6 +735,15 @@ INTERCEPTOR(int, getrusage, int who, void *usage) { return res; } +#define COMMON_INTERCEPTOR_WRITE_RANGE(ptr, size) \ + __msan_unpoison(ptr, size) +#define COMMON_INTERCEPTOR_READ_RANGE(ptr, size) do { } while (false) +#define COMMON_INTERCEPTOR_ENTER(func, ...) ENSURE_MSAN_INITED() +#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) do { } while (false) // FIXME +#include "sanitizer_common/sanitizer_common_interceptors.h" + // static void *fast_memset(void *ptr, int c, SIZE_T n) { // hack until we have a really fast internal_memset @@ -868,6 +853,8 @@ namespace __msan { void InitializeInterceptors() { static int inited = 0; CHECK_EQ(inited, 0); + SANITIZER_COMMON_INTERCEPTORS_INIT; + INTERCEPT_FUNCTION(mmap); INTERCEPT_FUNCTION(mmap64); INTERCEPT_FUNCTION(posix_memalign); @@ -877,9 +864,6 @@ void InitializeInterceptors() { INTERCEPT_FUNCTION(free); INTERCEPT_FUNCTION(fread); INTERCEPT_FUNCTION(fread_unlocked); - INTERCEPT_FUNCTION(read); - INTERCEPT_FUNCTION(pread); - INTERCEPT_FUNCTION(pread64); INTERCEPT_FUNCTION(readlink); INTERCEPT_FUNCTION(readdir); INTERCEPT_FUNCTION(memcpy); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.h index 312e659b9b32..c213caaeceba 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,81 @@ INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) { COMMON_INTERCEPTOR_FD_ACQUIRE(fd); return res; } +# define INIT_PREAD64 INTERCEPT_FUNCTION(pread64) +#else +# define INIT_PREAD64 #endif -#if SANITIZER_INTERCEPT_READ -# define INIT_READ INTERCEPT_FUNCTION(read) +#if SANITIZER_INTERCEPT_WRITE +INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) { + COMMON_INTERCEPTOR_ENTER(write, fd, ptr, count); + if (fd >= 0) + COMMON_INTERCEPTOR_FD_RELEASE(fd); + SSIZE_T res = REAL(write)(fd, ptr, count); + if (res > 0) + COMMON_INTERCEPTOR_READ_RANGE(ptr, res); + return res; +} +# define INIT_WRITE INTERCEPT_FUNCTION(write) #else -# define INIT_READ +# define INIT_WRITE #endif -#if SANITIZER_INTERCEPT_PREAD -# define INIT_PREAD INTERCEPT_FUNCTION(pread) +#if SANITIZER_INTERCEPT_PWRITE +INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count) { + COMMON_INTERCEPTOR_ENTER(pwrite, fd, ptr, count); + if (fd >= 0) + COMMON_INTERCEPTOR_FD_RELEASE(fd); + SSIZE_T res = REAL(pwrite)(fd, ptr, count); + if (res > 0) + COMMON_INTERCEPTOR_READ_RANGE(ptr, res); + return res; +} +# define INIT_PWRITE INTERCEPT_FUNCTION(pwrite) #else -# define INIT_PREAD +# define INIT_PWRITE #endif -#if SANITIZER_INTERCEPT_PREAD64 -# define INIT_PREAD64 INTERCEPT_FUNCTION(pread64) +#if SANITIZER_INTERCEPT_PWRITE64 +INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count) { + COMMON_INTERCEPTOR_ENTER(pwrite64, fd, ptr, count); + if (fd >= 0) + COMMON_INTERCEPTOR_FD_RELEASE(fd); + SSIZE_T res = REAL(pwrite64)(fd, ptr, count); + if (res > 0) + COMMON_INTERCEPTOR_READ_RANGE(ptr, res); + return res; +} +# define INIT_PWRITE64 INTERCEPT_FUNCTION(pwrite64) #else -# define INIT_PREAD64 +# define INIT_PWRITE64 #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; \ + INIT_WRITE; \ #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..86251d3d5afa 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -15,17 +15,24 @@ #include "sanitizer_internal_defs.h" #if !defined(_WIN32) -# define SANITIZER_INTERCEPT_READ 1 -# define SANITIZER_INTERCEPT_PREAD 1 +# define SI_NOT_WINDOWS 1 #else -# define SANITIZER_INTERCEPT_READ 0 -# define SANITIZER_INTERCEPT_PREAD 0 +# define SI_NOT_WINDOWS 0 #endif #if defined(__linux__) && !defined(ANDROID) -# define SANITIZER_INTERCEPT_PREAD64 1 +# define SI_LINUX_NOT_ANDROID 1 #else -# define SANITIZER_INTERCEPT_PREAD64 0 +# define SI_LINUX_NOT_ANDROID 0 #endif +# define SANITIZER_INTERCEPT_READ SI_NOT_WINDOWS +# define SANITIZER_INTERCEPT_PREAD SI_NOT_WINDOWS +# define SANITIZER_INTERCEPT_WRITE SI_NOT_WINDOWS +# define SANITIZER_INTERCEPT_PWRITE SI_NOT_WINDOWS + +# define SANITIZER_INTERCEPT_PREAD64 SI_LINUX_NOT_ANDROID +# define SANITIZER_INTERCEPT_PWRITE64 SI_LINUX_NOT_ANDROID +# define SANITIZER_INTERCEPT_PRCTL SI_LINUX_NOT_ANDROID + 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..5d94e4200ebb 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc @@ -1259,30 +1259,6 @@ TSAN_INTERCEPTOR(long_t, preadv64, int fd, void *vec, int cnt, u64 off) { return res; } -TSAN_INTERCEPTOR(long_t, write, int fd, void *buf, long_t sz) { - SCOPED_TSAN_INTERCEPTOR(write, fd, buf, sz); - if (fd >= 0) - FdRelease(thr, pc, fd); - int res = REAL(write)(fd, buf, sz); - return res; -} - -TSAN_INTERCEPTOR(long_t, pwrite, int fd, void *buf, long_t sz, unsigned off) { - SCOPED_TSAN_INTERCEPTOR(pwrite, fd, buf, sz, off); - if (fd >= 0) - FdRelease(thr, pc, fd); - int res = REAL(pwrite)(fd, buf, sz, off); - return res; -} - -TSAN_INTERCEPTOR(long_t, pwrite64, int fd, void *buf, long_t sz, u64 off) { - SCOPED_TSAN_INTERCEPTOR(pwrite64, fd, buf, sz, off); - if (fd >= 0) - FdRelease(thr, pc, fd); - int res = REAL(pwrite64)(fd, buf, sz, off); - return res; -} - TSAN_INTERCEPTOR(long_t, writev, int fd, void *vec, int cnt) { SCOPED_TSAN_INTERCEPTOR(writev, fd, vec, cnt); if (fd >= 0) @@ -1623,9 +1599,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 { @@ -1798,9 +1775,6 @@ void InitializeInterceptors() { TSAN_INTERCEPT(readv); TSAN_INTERCEPT(preadv64); - TSAN_INTERCEPT(write); - TSAN_INTERCEPT(pwrite); - TSAN_INTERCEPT(pwrite64); TSAN_INTERCEPT(writev); TSAN_INTERCEPT(pwritev64); TSAN_INTERCEPT(send); 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, -- 2.34.1