From cc970aac70679288c24e7fbc080285e40743c5d2 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 23 Apr 2019 19:14:02 +0100 Subject: [PATCH] Add sigaltstack support to MacOS crashlog --- config.lib | 27 ++++++++++++ src/os/macosx/crashlog_osx.cpp | 76 +++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/config.lib b/config.lib index 44758cbbea..f37e851af3 100644 --- a/config.lib +++ b/config.lib @@ -1956,6 +1956,33 @@ EOL log 1 "checking ucontext... found" CFLAGS="$CFLAGS -DWITH_UCONTEXT" fi + + log 2 "executing $cc_host $CFLAGS $CFLAGS_ENV $LDFLAGS $LDFLAGS_ENV $STATIC_FLAGS -o tmp.config.sigaltstack -x c++ - -ldl" + "$cc_host" $CFLAGS $CFLAGS_ENV $LDFLAGS $LDFLAGS_ENV $STATIC_FLAGS -o tmp.config.sigaltstack -x c++ - -ldl 2> /dev/null << EOL + #include + #include + #include + int main() { + pthread_t self = pthread_self(); + void *stacktop = pthread_get_stackaddr_np(self); + size_t stack_size = pthread_get_stacksize_np(self); + stack_t ss; + ss.ss_sp = calloc(SIGSTKSZ, 1); + ss.ss_size = SIGSTKSZ; + ss.ss_flags = 0; + sigaltstack(&ss, NULL); + return 0; + } +EOL + ret=$? + rm -f tmp.config.sigaltstack + log 2 " exit code $ret" + if [ $ret -ne 0 ]; then + log 1 "checking sigaltstack... no" + else + log 1 "checking sigaltstack... found" + CFLAGS="$CFLAGS -DWITH_SIGALTSTACK" + fi fi if [ "$os" = "HAIKU" ]; then diff --git a/src/os/macosx/crashlog_osx.cpp b/src/os/macosx/crashlog_osx.cpp index 3bad2e0f46..f2000d03fa 100644 --- a/src/os/macosx/crashlog_osx.cpp +++ b/src/os/macosx/crashlog_osx.cpp @@ -25,6 +25,9 @@ #ifdef WITH_UCONTEXT #include #endif +#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT) +#include +#endif #include "../../safeguards.h" @@ -174,22 +177,9 @@ class CrashLogOSX : public CrashLog { buffer += seprintf(buffer, last, "\nStacktrace:\n"); void **frame; -#if defined(__ppc__) || defined(__ppc64__) - /* Apple says __builtin_frame_address can be broken on PPC. */ - __asm__ volatile("mr %0, r1" : "=r" (frame)); -#else - frame = (void **)__builtin_frame_address(0); -#endif - - for (int i = 0; frame != NULL && i < MAX_STACK_FRAMES; i++) { - /* Get IP for current stack frame. */ -#if defined(__ppc__) || defined(__ppc64__) - void *ip = frame[2]; -#else - void *ip = frame[1]; -#endif - if (ip == NULL) break; + int i = 0; + auto print_frame = [&](void *ip) { /* Print running index. */ buffer += seprintf(buffer, last, " [%02d]", i); @@ -221,6 +211,49 @@ class CrashLogOSX : public CrashLog { free(func_name); } buffer += seprintf(buffer, last, "\n"); + }; + +#if defined(__ppc__) || defined(__ppc64__) + /* Apple says __builtin_frame_address can be broken on PPC. */ + __asm__ volatile("mr %0, r1" : "=r" (frame)); +#elif defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT) && (defined(__x86_64__) || defined(__i386)) + if (this->signal_instruction_ptr_valid) { + print_frame(this->signal_instruction_ptr); + i++; + } + + pthread_t self = pthread_self(); + char *stacktop = (char *) pthread_get_stackaddr_np(self); + char *stackbot = stacktop - pthread_get_stacksize_np(self); + stacktop -= 2 * sizeof(void *); + + ucontext_t *ucontext = static_cast(context); +#if defined(__x86_64__) + void **bp = (void **) ucontext->uc_mcontext->__ss.__rbp; + void **sp = (void **) ucontext->uc_mcontext->__ss.__rsp; +#else + void **bp = (void **) ucontext->uc_mcontext->__ss.__ebp; + void **sp = (void **) ucontext->uc_mcontext->__ss.__esp; +#endif + if (IS_ALIGNED(bp) && reinterpret_cast(bp) >= reinterpret_cast(sp) && reinterpret_cast(bp) >= reinterpret_cast(stackbot) && reinterpret_cast(bp) <= reinterpret_cast(stacktop)) { + frame = bp; + } else { + frame = nullptr; + } +#else + frame = (void **)__builtin_frame_address(0); +#endif + + for (; frame != nullptr && i < MAX_STACK_FRAMES; i++) { + /* Get IP for current stack frame. */ +#if defined(__ppc__) || defined(__ppc64__) + void *ip = frame[2]; +#else + void *ip = frame[1]; +#endif + if (ip == nullptr) break; + + print_frame(ip); /* Get address of next stack frame. */ void **next = (void **)frame[0]; @@ -263,7 +296,7 @@ class CrashLogOSX : public CrashLog { args.push_back("--batch"); args.push_back("-o"); - args.push_back(IsNonMainThread() ? "bt all" : "bt"); + args.push_back(IsNonMainThread() ? "bt all" : "bt 100"); if (this->GetMessage() == nullptr && this->signal_instruction_ptr_valid) { seprintf(disasm_buffer, lastof(disasm_buffer), "disassemble -b -F intel -c 1 -s %p", this->signal_instruction_ptr); @@ -448,10 +481,21 @@ void CDECL HandleCrash(int signum, siginfo_t *si, void *context) /* static */ void CrashLog::InitialiseCrashLog() { +#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT) + const size_t stack_size = max(SIGSTKSZ, 512*1024); + stack_t ss; + ss.ss_sp = CallocT(stack_size); + ss.ss_size = stack_size; + ss.ss_flags = 0; + sigaltstack(&ss, nullptr); +#endif for (const int *i = _signals_to_handle; i != endof(_signals_to_handle); i++) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_flags = SA_SIGINFO | SA_RESTART; +#if defined(WITH_SIGALTSTACK) && defined(WITH_UCONTEXT) + sa.sa_flags |= SA_ONSTACK; +#endif sigemptyset(&sa.sa_mask); sa.sa_sigaction = HandleCrash; sigaction(*i, &sa, nullptr);