Skip to content

Commit

Permalink
BUG#24566529: BACKPORT BUG#23575445 TO 5.6
Browse files Browse the repository at this point in the history
Stack traces were not printed by our signal handler on FreeBSD
since we didn't find backtrace(). However, since FreeBSD 10.0
backtrace() is available, but it requires the execinfo library.

This patch fixes the problem by adding -lexecinfo when
doing CMake configure checks and when linking the server
(if execinfo exists). Note that the number of stack frames
returned by backtrace() seems very limited - at least on
FreeBSD 10.2.

Also, the PTR_SANE macro which tries to check if a pointer
is invalid (used when printing pointer values in stack traces)
gave false negatives on OSX/FreeBSD. On these platforms we
now simply check if the pointer is non-null. This also removes
a sbrk() deprecation warning when building on OS X. (It was
before only disabled with building using XCode).
  • Loading branch information
karthik-kamath committed Sep 15, 2016
1 parent cea437f commit 75271e5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
1 change: 0 additions & 1 deletion cmake/os/WindowsCache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ SET(HAVE_BACKTRACE_SYMBOLS_FD CACHE INTERNAL "")
SET(HAVE_BMOVE CACHE INTERNAL "")
SET(HAVE_BSD_SIGNALS CACHE INTERNAL "")
SET(HAVE_BSEARCH 1 CACHE INTERNAL "")
SET(HAVE_BSS_START CACHE INTERNAL "")
SET(HAVE_CHOWN CACHE INTERNAL "")
SET(HAVE_CLOCK_GETTIME CACHE INTERNAL "")
SET(HAVE_COMPRESS CACHE INTERNAL "")
Expand Down
1 change: 0 additions & 1 deletion config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@
#cmakedefine HAVE_AIO_READ 1
/* Symbols we may use */
/* used by stacktrace functions */
#cmakedefine HAVE_BSS_START 1
#cmakedefine HAVE_BACKTRACE 1
#cmakedefine HAVE_BACKTRACE_SYMBOLS 1
#cmakedefine HAVE_BACKTRACE_SYMBOLS_FD 1
Expand Down
14 changes: 5 additions & 9 deletions configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,14 @@ IF(UNIX)
IF(NOT LIBRT)
MY_SEARCH_LIBS(clock_gettime rt LIBRT)
ENDIF()
MY_SEARCH_LIBS(backtrace execinfo LIBEXECINFO)

FIND_PACKAGE(Threads)

SET(CMAKE_REQUIRED_LIBRARIES
${LIBM} ${LIBNSL} ${LIBBIND} ${LIBCRYPT} ${LIBSOCKET} ${LIBDL} ${CMAKE_THREAD_LIBS_INIT} ${LIBRT})
${LIBM} ${LIBNSL} ${LIBBIND} ${LIBCRYPT} ${LIBSOCKET} ${LIBDL}
${CMAKE_THREAD_LIBS_INIT} ${LIBRT} ${LIBEXECINFO}
)
# Need explicit pthread for gcc -fsanitize=address
IF(CMAKE_USE_PTHREADS_INIT AND CMAKE_C_FLAGS MATCHES "-fsanitize=")
SET(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} pthread)
Expand Down Expand Up @@ -1077,14 +1081,6 @@ CHECK_CXX_SOURCE_COMPILES("
HAVE_ABI_CXA_DEMANGLE)
ENDIF()

CHECK_C_SOURCE_COMPILES("
int main(int argc, char **argv)
{
extern char *__bss_start;
return __bss_start ? 1 : 0;
}"
HAVE_BSS_START)

CHECK_C_SOURCE_COMPILES("
int main()
{
Expand Down
4 changes: 2 additions & 2 deletions mysys/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -60,7 +60,7 @@ ENDIF()

ADD_CONVENIENCE_LIBRARY(mysys ${MYSYS_SOURCES})
TARGET_LINK_LIBRARIES(mysys dbug strings ${ZLIB_LIBRARY}
${LIBNSL} ${LIBM} ${LIBRT})
${LIBNSL} ${LIBM} ${LIBRT} ${LIBEXECINFO})
DTRACE_INSTRUMENT(mysys)

# Need explicit pthread for gcc -fsanitize=address
Expand Down
18 changes: 10 additions & 8 deletions mysys/stacktrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@
#include <execinfo.h>
#endif

#ifdef __linux__
/* __bss_start doesn't seem to work on FreeBSD and doesn't exist on OSX/Solaris. */
#define PTR_SANE(p) ((p) && (char*)(p) >= heap_start && (char*)(p) <= heap_end)

static char *heap_start;

#ifdef HAVE_BSS_START
extern char *__bss_start;
#endif
#else
#define PTR_SANE(p) (p)
#endif /* __linux */

void my_init_stacktrace()
{
#ifdef HAVE_BSS_START
#ifdef __linux__
heap_start = (char*) &__bss_start;
#endif
#endif /* __linux__ */
}

#ifdef __linux__
Expand Down Expand Up @@ -134,14 +135,15 @@ static int safe_print_str(const char *addr, int max_len)

void my_safe_print_str(const char* val, int max_len)
{
#ifdef __linux__
/* Only needed by the linux version of PTR_SANE */
char *heap_end;

#ifdef __linux__
if (!safe_print_str(val, max_len))
return;
#endif

heap_end= (char*) sbrk(0);
#endif

if (!PTR_SANE(val))
{
Expand Down

0 comments on commit 75271e5

Please sign in to comment.