From d7c52625f2da8322b4f0adb4c67674f0d66e619a Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 20 Jun 2017 14:26:56 -0700 Subject: [PATCH] Shave another uninteresting stack frame off aborts. With this, stack frame 0 is the abort, not tgkill. arm: #00 pc 0001a41c /system/lib/libc.so (abort+63) arm64: #00 pc 000000000001d75c /system/lib64/libc.so (abort+120) Also "include what you use" for . Bug: N/A Test: ran `crasher abort` and `crasher64 abort` Change-Id: I6517ac67b39b4133e890d52efc115071c812958b --- libc/bionic/abort.cpp | 25 ++++++++++++++++++++++--- libc/bionic/bionic_arc4random.cpp | 1 - libc/bionic/clone.cpp | 1 + libc/bionic/fork.cpp | 1 - libc/bionic/gettid.cpp | 1 + tests/dlext_test.cpp | 1 + 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/libc/bionic/abort.cpp b/libc/bionic/abort.cpp index 3ba83d1f4..f401cab90 100644 --- a/libc/bionic/abort.cpp +++ b/libc/bionic/abort.cpp @@ -32,6 +32,26 @@ #include #include +// We call tgkill(2) directly instead of raise (or even the libc tgkill wrapper), to reduce the +// number of uninteresting stack frames at the top of a crash. +static inline __always_inline void inline_tgkill(pid_t pid, pid_t tid, int sig) { +#if defined(__arm__) + register int r0 __asm__("r0") = pid; + register int r1 __asm__("r1") = tid; + register int r2 __asm__("r2") = sig; + register int r7 __asm__("r7") = __NR_tgkill; + __asm__("swi #0" : "=r"(r0) : "r"(r0), "r"(r1), "r"(r2), "r"(r7) : "memory"); +#elif defined(__aarch64__) + register long x0 __asm__("x0") = pid; + register long x1 __asm__("x1") = tid; + register long x2 __asm__("x2") = sig; + register long x8 __asm__("x8") = __NR_tgkill; + __asm__("svc #0" : "=r"(x0) : "r"(x0), "r"(x1), "r"(x2), "r"(x8) : "memory"); +#else + syscall(__NR_tgkill, pid, tid, sig); +#endif +} + void abort() { // Protect ourselves against stale cached PID/TID values by fetching them via syscall. // http://b/37769298 @@ -45,8 +65,7 @@ void abort() { sigdelset(&mask, SIGABRT); sigprocmask(SIG_SETMASK, &mask, NULL); - // Use tgkill directly instead of raise, to avoid inserting spurious stack frames. - tgkill(pid, tid, SIGABRT); + inline_tgkill(pid, tid, SIGABRT); // If SIGABRT ignored, or caught and the handler returns, // remove the SIGABRT signal handler and raise SIGABRT again. @@ -57,7 +76,7 @@ void abort() { sigaction(SIGABRT, &sa, &sa); sigprocmask(SIG_SETMASK, &mask, NULL); - tgkill(pid, tid, SIGABRT); + inline_tgkill(pid, tid, SIGABRT); // If we get this far, just exit. _exit(127); diff --git a/libc/bionic/bionic_arc4random.cpp b/libc/bionic/bionic_arc4random.cpp index a4842f6f5..a33990020 100644 --- a/libc/bionic/bionic_arc4random.cpp +++ b/libc/bionic/bionic_arc4random.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include diff --git a/libc/bionic/clone.cpp b/libc/bionic/clone.cpp index 3a20aa93e..d7ce37f57 100644 --- a/libc/bionic/clone.cpp +++ b/libc/bionic/clone.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "pthread_internal.h" diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp index 32ea255bb..efcbb8c7c 100644 --- a/libc/bionic/fork.cpp +++ b/libc/bionic/fork.cpp @@ -27,7 +27,6 @@ */ #include -#include #include "pthread_internal.h" diff --git a/libc/bionic/gettid.cpp b/libc/bionic/gettid.cpp index fe25a4d81..eb5cfd6ab 100644 --- a/libc/bionic/gettid.cpp +++ b/libc/bionic/gettid.cpp @@ -27,6 +27,7 @@ */ #include +#include #include "pthread_internal.h" diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp index e3ee7d7d8..b264e536b 100644 --- a/tests/dlext_test.cpp +++ b/tests/dlext_test.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include