Improve stack overflow diagnostics (take 2).

This reverts commits eb1b07469f and
d14dc3b87f, and fixes the bug where
we were calling mmap (which might cause errno to be set) before
__set_tls (which is required to implement errno).

Bug: 8557703
Change-Id: I2c36d00240c56e156e1bb430d8c22a73a068b70c
This commit is contained in:
Elliott Hughes
2013-07-17 13:33:19 -07:00
parent 40e7a87864
commit 84114c8dd5
5 changed files with 74 additions and 33 deletions

View File

@@ -28,14 +28,15 @@
#include "linker.h"
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
extern "C" int tgkill(int tgid, int tid, int sig);
@@ -109,7 +110,7 @@ static int socket_abstract_client(const char* name, int type) {
* mutex is being held, so we don't want to use any libc functions that
* could allocate memory or hold a lock.
*/
static void logSignalSummary(int signum, const siginfo_t* info) {
static void log_signal_summary(int signum, const siginfo_t* info) {
const char* signal_name;
switch (signum) {
case SIGILL: signal_name = "SIGILL"; break;
@@ -149,26 +150,26 @@ static void logSignalSummary(int signum, const siginfo_t* info) {
/*
* Returns true if the handler for signal "signum" has SA_SIGINFO set.
*/
static bool haveSiginfo(int signum) {
struct sigaction oldact, newact;
static bool have_siginfo(int signum) {
struct sigaction old_action, new_action;
memset(&newact, 0, sizeof(newact));
newact.sa_handler = SIG_DFL;
newact.sa_flags = SA_RESTART;
sigemptyset(&newact.sa_mask);
memset(&new_action, 0, sizeof(new_action));
new_action.sa_handler = SIG_DFL;
new_action.sa_flags = SA_RESTART;
sigemptyset(&new_action.sa_mask);
if (sigaction(signum, &newact, &oldact) < 0) {
if (sigaction(signum, &new_action, &old_action) < 0) {
__libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
strerror(errno));
return false;
}
bool ret = (oldact.sa_flags & SA_SIGINFO) != 0;
bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
if (sigaction(signum, &oldact, NULL) == -1) {
if (sigaction(signum, &old_action, NULL) == -1) {
__libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
strerror(errno));
}
return ret;
return result;
}
/*
@@ -180,11 +181,11 @@ void debuggerd_signal_handler(int n, siginfo_t* info, void*) {
* It's possible somebody cleared the SA_SIGINFO flag, which would mean
* our "info" arg holds an undefined value.
*/
if (!haveSiginfo(n)) {
if (!have_siginfo(n)) {
info = NULL;
}
logSignalSummary(n, info);
log_signal_summary(n, info);
pid_t tid = gettid();
int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM);
@@ -245,19 +246,23 @@ void debuggerd_signal_handler(int n, siginfo_t* info, void*) {
}
void debuggerd_init() {
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_sigaction = debuggerd_signal_handler;
act.sa_flags = SA_RESTART | SA_SIGINFO;
sigemptyset(&act.sa_mask);
struct sigaction action;
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_sigaction = debuggerd_signal_handler;
action.sa_flags = SA_RESTART | SA_SIGINFO;
sigaction(SIGILL, &act, NULL);
sigaction(SIGABRT, &act, NULL);
sigaction(SIGBUS, &act, NULL);
sigaction(SIGFPE, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
// Use the alternate signal stack if available so we can catch stack overflows.
action.sa_flags |= SA_ONSTACK;
sigaction(SIGABRT, &action, NULL);
sigaction(SIGBUS, &action, NULL);
sigaction(SIGFPE, &action, NULL);
sigaction(SIGILL, &action, NULL);
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGSEGV, &action, NULL);
#if defined(SIGSTKFLT)
sigaction(SIGSTKFLT, &act, NULL);
sigaction(SIGSTKFLT, &action, NULL);
#endif
sigaction(SIGPIPE, &act, NULL);
sigaction(SIGTRAP, &action, NULL);
}