• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "linker.h"
30 
31 #include <errno.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/mman.h>
36 #include <sys/prctl.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <unistd.h>
40 
41 extern "C" int tgkill(int tgid, int tid, int sig);
42 
43 #define DEBUGGER_SOCKET_NAME "android:debuggerd"
44 
45 enum debugger_action_t {
46     // dump a crash
47     DEBUGGER_ACTION_CRASH,
48     // dump a tombstone file
49     DEBUGGER_ACTION_DUMP_TOMBSTONE,
50     // dump a backtrace only back to the socket
51     DEBUGGER_ACTION_DUMP_BACKTRACE,
52 };
53 
54 /* message sent over the socket */
55 struct debugger_msg_t {
56   // version 1 included:
57   debugger_action_t action;
58   pid_t tid;
59 
60   // version 2 added:
61   uintptr_t abort_msg_address;
62 };
63 
64 // see man(2) prctl, specifically the section about PR_GET_NAME
65 #define MAX_TASK_NAME_LEN (16)
66 
socket_abstract_client(const char * name,int type)67 static int socket_abstract_client(const char* name, int type) {
68     sockaddr_un addr;
69 
70     // Test with length +1 for the *initial* '\0'.
71     size_t namelen = strlen(name);
72     if ((namelen + 1) > sizeof(addr.sun_path)) {
73         errno = EINVAL;
74         return -1;
75     }
76 
77     /* This is used for abstract socket namespace, we need
78      * an initial '\0' at the start of the Unix socket path.
79      *
80      * Note: The path in this case is *not* supposed to be
81      * '\0'-terminated. ("man 7 unix" for the gory details.)
82      */
83     memset(&addr, 0, sizeof(addr));
84     addr.sun_family = AF_LOCAL;
85     addr.sun_path[0] = 0;
86     memcpy(addr.sun_path + 1, name, namelen);
87 
88     socklen_t alen = namelen + offsetof(sockaddr_un, sun_path) + 1;
89 
90     int s = socket(AF_LOCAL, type, 0);
91     if (s == -1) {
92         return -1;
93     }
94 
95     int err = TEMP_FAILURE_RETRY(connect(s, (sockaddr*) &addr, alen));
96     if (err == -1) {
97         close(s);
98         s = -1;
99     }
100 
101     return s;
102 }
103 
104 /*
105  * Writes a summary of the signal to the log file.  We do this so that, if
106  * for some reason we're not able to contact debuggerd, there is still some
107  * indication of the failure in the log.
108  *
109  * We could be here as a result of native heap corruption, or while a
110  * mutex is being held, so we don't want to use any libc functions that
111  * could allocate memory or hold a lock.
112  */
log_signal_summary(int signum,const siginfo_t * info)113 static void log_signal_summary(int signum, const siginfo_t* info) {
114     const char* signal_name;
115     switch (signum) {
116         case SIGILL:    signal_name = "SIGILL";     break;
117         case SIGABRT:   signal_name = "SIGABRT";    break;
118         case SIGBUS:    signal_name = "SIGBUS";     break;
119         case SIGFPE:    signal_name = "SIGFPE";     break;
120         case SIGSEGV:   signal_name = "SIGSEGV";    break;
121 #if defined(SIGSTKFLT)
122         case SIGSTKFLT: signal_name = "SIGSTKFLT";  break;
123 #endif
124         case SIGPIPE:   signal_name = "SIGPIPE";    break;
125         default:        signal_name = "???";        break;
126     }
127 
128     char thread_name[MAX_TASK_NAME_LEN + 1]; // one more for termination
129     if (prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0) != 0) {
130         strcpy(thread_name, "<name unknown>");
131     } else {
132         // short names are null terminated by prctl, but the man page
133         // implies that 16 byte names are not.
134         thread_name[MAX_TASK_NAME_LEN] = 0;
135     }
136 
137     // "info" will be NULL if the siginfo_t information was not available.
138     if (info != NULL) {
139         __libc_format_log(ANDROID_LOG_FATAL, "libc",
140                           "Fatal signal %d (%s) at 0x%08x (code=%d), thread %d (%s)",
141                           signum, signal_name, reinterpret_cast<uintptr_t>(info->si_addr),
142                           info->si_code, gettid(), thread_name);
143     } else {
144         __libc_format_log(ANDROID_LOG_FATAL, "libc",
145                           "Fatal signal %d (%s), thread %d (%s)",
146                           signum, signal_name, gettid(), thread_name);
147     }
148 }
149 
150 /*
151  * Returns true if the handler for signal "signum" has SA_SIGINFO set.
152  */
have_siginfo(int signum)153 static bool have_siginfo(int signum) {
154     struct sigaction old_action, new_action;
155 
156     memset(&new_action, 0, sizeof(new_action));
157     new_action.sa_handler = SIG_DFL;
158     new_action.sa_flags = SA_RESTART;
159     sigemptyset(&new_action.sa_mask);
160 
161     if (sigaction(signum, &new_action, &old_action) < 0) {
162       __libc_format_log(ANDROID_LOG_WARN, "libc", "Failed testing for SA_SIGINFO: %s",
163                         strerror(errno));
164       return false;
165     }
166     bool result = (old_action.sa_flags & SA_SIGINFO) != 0;
167 
168     if (sigaction(signum, &old_action, NULL) == -1) {
169       __libc_format_log(ANDROID_LOG_WARN, "libc", "Restore failed in test for SA_SIGINFO: %s",
170                         strerror(errno));
171     }
172     return result;
173 }
174 
175 /*
176  * Catches fatal signals so we can ask debuggerd to ptrace us before
177  * we crash.
178  */
debuggerd_signal_handler(int n,siginfo_t * info,void *)179 void debuggerd_signal_handler(int n, siginfo_t* info, void*) {
180     /*
181      * It's possible somebody cleared the SA_SIGINFO flag, which would mean
182      * our "info" arg holds an undefined value.
183      */
184     if (!have_siginfo(n)) {
185         info = NULL;
186     }
187 
188     log_signal_summary(n, info);
189 
190     pid_t tid = gettid();
191     int s = socket_abstract_client(DEBUGGER_SOCKET_NAME, SOCK_STREAM);
192 
193     if (s >= 0) {
194         // debuggerd knows our pid from the credentials on the
195         // local socket but we need to tell it the tid of the crashing thread.
196         // debuggerd will be paranoid and verify that we sent a tid
197         // that's actually in our process.
198         debugger_msg_t msg;
199         msg.action = DEBUGGER_ACTION_CRASH;
200         msg.tid = tid;
201         msg.abort_msg_address = reinterpret_cast<uintptr_t>(gAbortMessage);
202         int ret = TEMP_FAILURE_RETRY(write(s, &msg, sizeof(msg)));
203         if (ret == sizeof(msg)) {
204             // if the write failed, there is no point trying to read a response.
205             ret = TEMP_FAILURE_RETRY(read(s, &tid, 1));
206             int saved_errno = errno;
207             notify_gdb_of_libraries();
208             errno = saved_errno;
209         }
210 
211         if (ret < 0) {
212             /* read or write failed -- broken connection? */
213             __libc_format_log(ANDROID_LOG_FATAL, "libc", "Failed while talking to debuggerd: %s",
214                               strerror(errno));
215         }
216 
217         close(s);
218     } else {
219         /* socket failed; maybe process ran out of fds */
220         __libc_format_log(ANDROID_LOG_FATAL, "libc", "Unable to open connection to debuggerd: %s",
221                           strerror(errno));
222     }
223 
224     /* remove our net so we fault for real when we return */
225     signal(n, SIG_DFL);
226 
227     /*
228      * These signals are not re-thrown when we resume.  This means that
229      * crashing due to (say) SIGPIPE doesn't work the way you'd expect it
230      * to.  We work around this by throwing them manually.  We don't want
231      * to do this for *all* signals because it'll screw up the address for
232      * faults like SIGSEGV.
233      */
234     switch (n) {
235         case SIGABRT:
236         case SIGFPE:
237         case SIGPIPE:
238 #ifdef SIGSTKFLT
239         case SIGSTKFLT:
240 #endif
241             (void) tgkill(getpid(), gettid(), n);
242             break;
243         default:    // SIGILL, SIGBUS, SIGSEGV
244             break;
245     }
246 }
247 
debuggerd_init()248 void debuggerd_init() {
249     struct sigaction action;
250     memset(&action, 0, sizeof(action));
251     sigemptyset(&action.sa_mask);
252     action.sa_sigaction = debuggerd_signal_handler;
253     action.sa_flags = SA_RESTART | SA_SIGINFO;
254 
255     // Use the alternate signal stack if available so we can catch stack overflows.
256     action.sa_flags |= SA_ONSTACK;
257 
258     sigaction(SIGABRT, &action, NULL);
259     sigaction(SIGBUS, &action, NULL);
260     sigaction(SIGFPE, &action, NULL);
261     sigaction(SIGILL, &action, NULL);
262     sigaction(SIGPIPE, &action, NULL);
263     sigaction(SIGSEGV, &action, NULL);
264 #if defined(SIGSTKFLT)
265     sigaction(SIGSTKFLT, &action, NULL);
266 #endif
267     sigaction(SIGTRAP, &action, NULL);
268 }
269