From 171fcc6fd03e3f5787b3d0ee8aedc8cb9bcc6f17 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 24 May 2016 18:58:14 -0400 Subject: [PATCH] Fix: don't call __builtin_return_address(0) on 32-bit powerpc Invoking __builtin_return_address(0) corrupts the stack, as previously noticed for the "ip" context. Disable its use on 32-bit powerpc everywhere else in the lttng-ust code base. Signed-off-by: Mathieu Desnoyers --- include/helper.h | 12 ++++++++++++ liblttng-ust-dl/lttng-ust-dl.c | 5 +++-- liblttng-ust-libc-wrapper/lttng-ust-malloc.c | 13 +++++++------ liblttng-ust-libc-wrapper/lttng-ust-pthread.c | 9 +++++---- liblttng-ust/tracef.c | 3 ++- liblttng-ust/tracelog.c | 3 ++- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/helper.h b/include/helper.h index 9d6617a8..9873fead 100644 --- a/include/helper.h +++ b/include/helper.h @@ -43,4 +43,16 @@ void *zmalloc(size_t len) #define LTTNG_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +/* + * Use of __builtin_return_address(0) sometimes seems to cause stack + * corruption on 32-bit PowerPC. Disable this feature on that + * architecture for now by always using the NULL value for the ip + * context. + */ +#if defined(__PPC__) && !defined(__PPC64__) +#define LTTNG_UST_CALLER_IP() NULL +#else /* #if defined(__PPC__) && !defined(__PPC64__) */ +#define LTTNG_UST_CALLER_IP() __builtin_return_address(0) +#endif /* #else #if defined(__PPC__) && !defined(__PPC64__) */ + #endif /* _LTTNG_UST_HELPER_H */ diff --git a/liblttng-ust-dl/lttng-ust-dl.c b/liblttng-ust-dl/lttng-ust-dl.c index e4cb4206..8f2faac0 100644 --- a/liblttng-ust-dl/lttng-ust-dl.c +++ b/liblttng-ust-dl/lttng-ust-dl.c @@ -27,6 +27,7 @@ #include #include +#include #include "usterr-signal-safe.h" /* Include link.h last else it conflicts with ust-dlfcn. */ @@ -136,7 +137,7 @@ void *dlopen(const char *filename, int flag) ret = dlinfo(handle, RTLD_DI_LINKMAP, &p); if (ret != -1 && p != NULL && p->l_addr != 0) { lttng_ust_dl_dlopen((void *) p->l_addr, p->l_name, - __builtin_return_address(0)); + LTTNG_UST_CALLER_IP()); } } @@ -152,7 +153,7 @@ int dlclose(void *handle) ret = dlinfo(handle, RTLD_DI_LINKMAP, &p); if (ret != -1 && p != NULL && p->l_addr != 0) { tracepoint(lttng_ust_dl, dlclose, - __builtin_return_address(0), + LTTNG_UST_CALLER_IP(), (void *) p->l_addr); } } diff --git a/liblttng-ust-libc-wrapper/lttng-ust-malloc.c b/liblttng-ust-libc-wrapper/lttng-ust-malloc.c index ba8e4d42..d1ad3cf5 100644 --- a/liblttng-ust-libc-wrapper/lttng-ust-malloc.c +++ b/liblttng-ust-libc-wrapper/lttng-ust-malloc.c @@ -28,6 +28,7 @@ #include #include #include +#include #define TRACEPOINT_DEFINE #define TRACEPOINT_CREATE_PROBES @@ -262,7 +263,7 @@ void *malloc(size_t size) retval = cur_alloc.malloc(size); if (URCU_TLS(malloc_nesting) == 1) { tracepoint(lttng_ust_libc, malloc, - size, retval, __builtin_return_address(0)); + size, retval, LTTNG_UST_CALLER_IP()); } URCU_TLS(malloc_nesting)--; return retval; @@ -282,7 +283,7 @@ void free(void *ptr) if (URCU_TLS(malloc_nesting) == 1) { tracepoint(lttng_ust_libc, free, - ptr, __builtin_return_address(0)); + ptr, LTTNG_UST_CALLER_IP()); } if (cur_alloc.free == NULL) { @@ -312,7 +313,7 @@ void *calloc(size_t nmemb, size_t size) retval = cur_alloc.calloc(nmemb, size); if (URCU_TLS(malloc_nesting) == 1) { tracepoint(lttng_ust_libc, calloc, - nmemb, size, retval, __builtin_return_address(0)); + nmemb, size, retval, LTTNG_UST_CALLER_IP()); } URCU_TLS(malloc_nesting)--; return retval; @@ -365,7 +366,7 @@ void *realloc(void *ptr, size_t size) end: if (URCU_TLS(malloc_nesting) == 1) { tracepoint(lttng_ust_libc, realloc, - ptr, size, retval, __builtin_return_address(0)); + ptr, size, retval, LTTNG_UST_CALLER_IP()); } URCU_TLS(malloc_nesting)--; return retval; @@ -387,7 +388,7 @@ void *memalign(size_t alignment, size_t size) if (URCU_TLS(malloc_nesting) == 1) { tracepoint(lttng_ust_libc, memalign, alignment, size, retval, - __builtin_return_address(0)); + LTTNG_UST_CALLER_IP()); } URCU_TLS(malloc_nesting)--; return retval; @@ -409,7 +410,7 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) if (URCU_TLS(malloc_nesting) == 1) { tracepoint(lttng_ust_libc, posix_memalign, *memptr, alignment, size, - retval, __builtin_return_address(0)); + retval, LTTNG_UST_CALLER_IP()); } URCU_TLS(malloc_nesting)--; return retval; diff --git a/liblttng-ust-libc-wrapper/lttng-ust-pthread.c b/liblttng-ust-libc-wrapper/lttng-ust-pthread.c index fd3af110..c6b8ae09 100644 --- a/liblttng-ust-libc-wrapper/lttng-ust-pthread.c +++ b/liblttng-ust-libc-wrapper/lttng-ust-pthread.c @@ -18,6 +18,7 @@ #define _GNU_SOURCE #include +#include #include #define TRACEPOINT_DEFINE @@ -48,10 +49,10 @@ int pthread_mutex_lock(pthread_mutex_t *mutex) thread_in_trace = 1; tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex, - __builtin_return_address(0)); + LTTNG_UST_CALLER_IP()); retval = mutex_lock(mutex); tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex, - retval, __builtin_return_address(0)); + retval, LTTNG_UST_CALLER_IP()); thread_in_trace = 0; return retval; } @@ -78,7 +79,7 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex) thread_in_trace = 1; retval = mutex_trylock(mutex); tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex, - retval, __builtin_return_address(0)); + retval, LTTNG_UST_CALLER_IP()); thread_in_trace = 0; return retval; } @@ -105,7 +106,7 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex) thread_in_trace = 1; retval = mutex_unlock(mutex); tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex, - retval, __builtin_return_address(0)); + retval, LTTNG_UST_CALLER_IP()); thread_in_trace = 0; return retval; } diff --git a/liblttng-ust/tracef.c b/liblttng-ust/tracef.c index dcae3be7..ea98e43e 100644 --- a/liblttng-ust/tracef.c +++ b/liblttng-ust/tracef.c @@ -23,6 +23,7 @@ #define _GNU_SOURCE #define _LGPL_SOURCE #include +#include #define TRACEPOINT_CREATE_PROBES #define TRACEPOINT_DEFINE @@ -40,7 +41,7 @@ void _lttng_ust_tracef(const char *fmt, ...) if (len < 0) goto end; __tracepoint_cb_lttng_ust_tracef___event(msg, len, - __builtin_return_address(0)); + LTTNG_UST_CALLER_IP()); free(msg); end: va_end(ap); diff --git a/liblttng-ust/tracelog.c b/liblttng-ust/tracelog.c index 93eb403a..65fc87ed 100644 --- a/liblttng-ust/tracelog.c +++ b/liblttng-ust/tracelog.c @@ -23,6 +23,7 @@ #define _GNU_SOURCE #define _LGPL_SOURCE #include +#include #define TRACEPOINT_CREATE_PROBES #define TRACEPOINT_DEFINE @@ -44,7 +45,7 @@ goto end; \ __tracepoint_cb_lttng_ust_tracelog___##level(file, \ line, func, msg, len, \ - __builtin_return_address(0)); \ + LTTNG_UST_CALLER_IP()); \ free(msg); \ end: \ va_end(ap); \ -- 2.34.1