From 07cca627eaab800d4874f3d0914d3cf7eaa601a9 Mon Sep 17 00:00:00 2001 From: cypherpunks Date: Tue, 15 Dec 2015 16:30:04 +0100 Subject: [PATCH 1/4] Fix backtrace compilation on FreeBSD On FreeBSD backtrace(3) uses size_t instead of int (as glibc does). This causes integer precision loss errors when we used int to store its results. The issue is fixed by using size_t to store the results of backtrace(3). The manual page of glibc does not mention that backtrace(3) returns negative values. Therefore, no unsigned integer wrapping occurs when its result is stored in an unsigned data type. --- changes/bug17827 | 3 +++ src/common/backtrace.c | 16 ++++++++-------- src/common/backtrace.h | 2 +- src/common/sandbox.c | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 changes/bug17827 diff --git a/changes/bug17827 b/changes/bug17827 new file mode 100644 index 000000000..04cd3b597 --- /dev/null +++ b/changes/bug17827 @@ -0,0 +1,3 @@ + o Minor bugfixes (compilation): + - Fix backtrace compilation on FreeBSD. Fixes bug 17827; bugfix on + tor-0.2.5.2-alpha. diff --git a/src/common/backtrace.c b/src/common/backtrace.c index a2d5378b2..8d544ed70 100644 --- a/src/common/backtrace.c +++ b/src/common/backtrace.c @@ -62,16 +62,16 @@ static tor_mutex_t cb_buf_mutex; * ucontext_t structure. */ void -clean_backtrace(void **stack, int depth, const ucontext_t *ctx) +clean_backtrace(void **stack, size_t depth, const ucontext_t *ctx) { #ifdef PC_FROM_UCONTEXT #if defined(__linux__) - const int n = 1; + const size_t n = 1; #elif defined(__darwin__) || defined(__APPLE__) || defined(__OpenBSD__) \ || defined(__FreeBSD__) - const int n = 2; + const size_t n = 2; #else - const int n = 1; + const size_t n = 1; #endif if (depth <= n) return; @@ -89,9 +89,9 @@ clean_backtrace(void **stack, int depth, const ucontext_t *ctx) void log_backtrace(int severity, int domain, const char *msg) { - int depth; + size_t depth; char **symbols; - int i; + size_t i; tor_mutex_acquire(&cb_buf_mutex); @@ -120,7 +120,7 @@ static void crash_handler(int sig, siginfo_t *si, void *ctx_) { char buf[40]; - int depth; + size_t depth; ucontext_t *ctx = (ucontext_t *) ctx_; int n_fds, i; const int *fds = NULL; @@ -174,7 +174,7 @@ install_bt_handler(void) * libc has pre-loaded the symbols we need to dump things, so that later * reads won't be denied by the sandbox code */ char **symbols; - int depth = backtrace(cb_buf, MAX_DEPTH); + size_t depth = backtrace(cb_buf, MAX_DEPTH); symbols = backtrace_symbols(cb_buf, depth); if (symbols) free(symbols); diff --git a/src/common/backtrace.h b/src/common/backtrace.h index a9151d795..838e18eed 100644 --- a/src/common/backtrace.h +++ b/src/common/backtrace.h @@ -13,7 +13,7 @@ void clean_up_backtrace_handler(void); #ifdef EXPOSE_CLEAN_BACKTRACE #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \ defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION) -void clean_backtrace(void **stack, int depth, const ucontext_t *ctx); +void clean_backtrace(void **stack, size_t depth, const ucontext_t *ctx); #endif #endif diff --git a/src/common/sandbox.c b/src/common/sandbox.c index b99576273..3a9f2a189 100644 --- a/src/common/sandbox.c +++ b/src/common/sandbox.c @@ -1598,7 +1598,7 @@ sigsys_debugging(int nr, siginfo_t *info, void *void_context) const char *syscall_name; int syscall; #ifdef USE_BACKTRACE - int depth; + size_t depth; int n_fds, i; const int *fds = NULL; #endif From e0aa4f837c62d1b6df37bba071023ec24f3191cc Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 16 Dec 2015 08:20:53 -0500 Subject: [PATCH 2/4] ... and fix the linux backtrace_symbols{,_fd} calls --- src/common/backtrace.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/backtrace.c b/src/common/backtrace.c index 8d544ed70..bed044247 100644 --- a/src/common/backtrace.c +++ b/src/common/backtrace.c @@ -96,7 +96,7 @@ log_backtrace(int severity, int domain, const char *msg) tor_mutex_acquire(&cb_buf_mutex); depth = backtrace(cb_buf, MAX_DEPTH); - symbols = backtrace_symbols(cb_buf, depth); + symbols = backtrace_symbols(cb_buf, (int)depth); tor_log(severity, domain, "%s. Stack trace:", msg); if (!symbols) { @@ -139,7 +139,7 @@ crash_handler(int sig, siginfo_t *si, void *ctx_) n_fds = tor_log_get_sigsafe_err_fds(&fds); for (i=0; i < n_fds; ++i) - backtrace_symbols_fd(cb_buf, depth, fds[i]); + backtrace_symbols_fd(cb_buf, (int)depth, fds[i]); abort(); } @@ -175,7 +175,7 @@ install_bt_handler(void) * reads won't be denied by the sandbox code */ char **symbols; size_t depth = backtrace(cb_buf, MAX_DEPTH); - symbols = backtrace_symbols(cb_buf, depth); + symbols = backtrace_symbols(cb_buf, (int) depth); if (symbols) free(symbols); } From 784e9fff9baa4fb63712de5dd9cc0ac530e2eb23 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 16 Dec 2015 09:05:49 -0500 Subject: [PATCH 3/4] ... and fix another backtrace_symbols_fd call in sandbox.c --- src/common/sandbox.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/sandbox.c b/src/common/sandbox.c index 3a9f2a189..950a92fbb 100644 --- a/src/common/sandbox.c +++ b/src/common/sandbox.c @@ -1630,7 +1630,7 @@ sigsys_debugging(int nr, siginfo_t *info, void *void_context) #ifdef USE_BACKTRACE n_fds = tor_log_get_sigsafe_err_fds(&fds); for (i=0; i < n_fds; ++i) - backtrace_symbols_fd(syscall_cb_buf, depth, fds[i]); + backtrace_symbols_fd(syscall_cb_buf, (int)depth, fds[i]); #endif #if defined(DEBUGGING_CLOSE) From 33b5bfb94824a55254f1ffcddf38ac17589a2744 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 16 Dec 2015 09:23:44 -0500 Subject: [PATCH 4/4] Don't call pthread_condattr_setclock() unless it exists Fixes bug 17819; bugfix on 0.2.6.3-alpha (specifically, d684dbb0). --- changes/bug17819 | 4 ++++ configure.ac | 1 + src/common/compat_pthreads.c | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changes/bug17819 diff --git a/changes/bug17819 b/changes/bug17819 new file mode 100644 index 000000000..45c55f74b --- /dev/null +++ b/changes/bug17819 @@ -0,0 +1,4 @@ + o Minor bugfixes (compilation): + - Don't try to use the pthrad_condattr_setclock() function unless + it actually exists. Fixes compilation on NetBSD-6.x. Fixes bug + 17819; bugfix on 0.2.6.3-alpha. diff --git a/configure.ac b/configure.ac index d37c34daa..2f7556838 100644 --- a/configure.ac +++ b/configure.ac @@ -425,6 +425,7 @@ AC_CHECK_FUNCS( if test "$bwin32" != true; then AC_CHECK_HEADERS(pthread.h) AC_CHECK_FUNCS(pthread_create) + AC_CHECK_FUNCS(pthread_condattr_setclock) fi if test "$bwin32" = true; then diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c index 4b32fc93d..b1d87d38f 100644 --- a/src/common/compat_pthreads.c +++ b/src/common/compat_pthreads.c @@ -185,7 +185,8 @@ tor_cond_init(tor_cond_t *cond) return -1; } -#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) \ + && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) /* Use monotonic time so when we timedwait() on it, any clock adjustment * won't affect the timeout value. */ if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC)) {