1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "runtime_common.h"
18 
19 #include <signal.h>
20 
21 #include <cinttypes>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 
26 #include <android-base/logging.h>
27 #include <android-base/stringprintf.h>
28 
29 #include "base/aborting.h"
30 #include "base/file_utils.h"
31 #include "base/logging.h"  // For LogHelper, GetCmdLine.
32 #include "base/macros.h"
33 #include "base/mutex.h"
34 #include "native_stack_dump.h"
35 #include "runtime.h"
36 #include "thread-current-inl.h"
37 #include "thread_list.h"
38 
39 namespace art {
40 
41 using android::base::StringPrintf;
42 
43 static constexpr bool kUseSigRTTimeout = true;
44 static constexpr bool kDumpNativeStackOnTimeout = true;
45 
GetSignalName(int signal_number)46 const char* GetSignalName(int signal_number) {
47   switch (signal_number) {
48     case SIGABRT: return "SIGABRT";
49     case SIGBUS: return "SIGBUS";
50     case SIGFPE: return "SIGFPE";
51     case SIGILL: return "SIGILL";
52     case SIGPIPE: return "SIGPIPE";
53     case SIGSEGV: return "SIGSEGV";
54 #if defined(SIGSTKFLT)
55     case SIGSTKFLT: return "SIGSTKFLT";
56 #endif
57     case SIGTRAP: return "SIGTRAP";
58   }
59   return "??";
60 }
61 
GetSignalCodeName(int signal_number,int signal_code)62 const char* GetSignalCodeName(int signal_number, int signal_code) {
63   // Try the signal-specific codes...
64   switch (signal_number) {
65     case SIGILL:
66       switch (signal_code) {
67         case ILL_ILLOPC: return "ILL_ILLOPC";
68         case ILL_ILLOPN: return "ILL_ILLOPN";
69         case ILL_ILLADR: return "ILL_ILLADR";
70         case ILL_ILLTRP: return "ILL_ILLTRP";
71         case ILL_PRVOPC: return "ILL_PRVOPC";
72         case ILL_PRVREG: return "ILL_PRVREG";
73         case ILL_COPROC: return "ILL_COPROC";
74         case ILL_BADSTK: return "ILL_BADSTK";
75       }
76       break;
77     case SIGBUS:
78       switch (signal_code) {
79         case BUS_ADRALN: return "BUS_ADRALN";
80         case BUS_ADRERR: return "BUS_ADRERR";
81         case BUS_OBJERR: return "BUS_OBJERR";
82       }
83       break;
84     case SIGFPE:
85       switch (signal_code) {
86         case FPE_INTDIV: return "FPE_INTDIV";
87         case FPE_INTOVF: return "FPE_INTOVF";
88         case FPE_FLTDIV: return "FPE_FLTDIV";
89         case FPE_FLTOVF: return "FPE_FLTOVF";
90         case FPE_FLTUND: return "FPE_FLTUND";
91         case FPE_FLTRES: return "FPE_FLTRES";
92         case FPE_FLTINV: return "FPE_FLTINV";
93         case FPE_FLTSUB: return "FPE_FLTSUB";
94       }
95       break;
96     case SIGSEGV:
97       switch (signal_code) {
98         case SEGV_MAPERR: return "SEGV_MAPERR";
99         case SEGV_ACCERR: return "SEGV_ACCERR";
100 #if defined(SEGV_BNDERR)
101         case SEGV_BNDERR: return "SEGV_BNDERR";
102 #endif
103       }
104       break;
105     case SIGTRAP:
106       switch (signal_code) {
107         case TRAP_BRKPT: return "TRAP_BRKPT";
108         case TRAP_TRACE: return "TRAP_TRACE";
109       }
110       break;
111   }
112   // Then the other codes...
113   switch (signal_code) {
114     case SI_USER:     return "SI_USER";
115 #if defined(SI_KERNEL)
116     case SI_KERNEL:   return "SI_KERNEL";
117 #endif
118     case SI_QUEUE:    return "SI_QUEUE";
119     case SI_TIMER:    return "SI_TIMER";
120     case SI_MESGQ:    return "SI_MESGQ";
121     case SI_ASYNCIO:  return "SI_ASYNCIO";
122 #if defined(SI_SIGIO)
123     case SI_SIGIO:    return "SI_SIGIO";
124 #endif
125 #if defined(SI_TKILL)
126     case SI_TKILL:    return "SI_TKILL";
127 #endif
128   }
129   // Then give up...
130   return "?";
131 }
132 
133 struct UContext {
UContextart::UContext134   explicit UContext(void* raw_context)
135       : context(reinterpret_cast<ucontext_t*>(raw_context)->uc_mcontext) {}
136 
137   void Dump(std::ostream& os) const;
138 
139   void DumpRegister32(std::ostream& os, const char* name, uint32_t value) const;
140   void DumpRegister64(std::ostream& os, const char* name, uint64_t value) const;
141 
142   void DumpX86Flags(std::ostream& os, uint32_t flags) const;
143   // Print some of the information from the status register (CPSR on ARMv7, PSTATE on ARMv8).
144   template <typename RegisterType>
145   void DumpArmStatusRegister(std::ostream& os, RegisterType status_register) const;
146 
147   mcontext_t& context;
148 };
149 
Dump(std::ostream & os) const150 void UContext::Dump(std::ostream& os) const {
151 #if defined(__APPLE__) && defined(__i386__)
152   DumpRegister32(os, "eax", context->__ss.__eax);
153   DumpRegister32(os, "ebx", context->__ss.__ebx);
154   DumpRegister32(os, "ecx", context->__ss.__ecx);
155   DumpRegister32(os, "edx", context->__ss.__edx);
156   os << '\n';
157 
158   DumpRegister32(os, "edi", context->__ss.__edi);
159   DumpRegister32(os, "esi", context->__ss.__esi);
160   DumpRegister32(os, "ebp", context->__ss.__ebp);
161   DumpRegister32(os, "esp", context->__ss.__esp);
162   os << '\n';
163 
164   DumpRegister32(os, "eip", context->__ss.__eip);
165   os << "                   ";
166   DumpRegister32(os, "eflags", context->__ss.__eflags);
167   DumpX86Flags(os, context->__ss.__eflags);
168   os << '\n';
169 
170   DumpRegister32(os, "cs",  context->__ss.__cs);
171   DumpRegister32(os, "ds",  context->__ss.__ds);
172   DumpRegister32(os, "es",  context->__ss.__es);
173   DumpRegister32(os, "fs",  context->__ss.__fs);
174   os << '\n';
175   DumpRegister32(os, "gs",  context->__ss.__gs);
176   DumpRegister32(os, "ss",  context->__ss.__ss);
177 #elif defined(__linux__) && defined(__i386__)
178   DumpRegister32(os, "eax", context.gregs[REG_EAX]);
179   DumpRegister32(os, "ebx", context.gregs[REG_EBX]);
180   DumpRegister32(os, "ecx", context.gregs[REG_ECX]);
181   DumpRegister32(os, "edx", context.gregs[REG_EDX]);
182   os << '\n';
183 
184   DumpRegister32(os, "edi", context.gregs[REG_EDI]);
185   DumpRegister32(os, "esi", context.gregs[REG_ESI]);
186   DumpRegister32(os, "ebp", context.gregs[REG_EBP]);
187   DumpRegister32(os, "esp", context.gregs[REG_ESP]);
188   os << '\n';
189 
190   DumpRegister32(os, "eip", context.gregs[REG_EIP]);
191   os << "                   ";
192   DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
193   DumpX86Flags(os, context.gregs[REG_EFL]);
194   os << '\n';
195 
196   DumpRegister32(os, "cs",  context.gregs[REG_CS]);
197   DumpRegister32(os, "ds",  context.gregs[REG_DS]);
198   DumpRegister32(os, "es",  context.gregs[REG_ES]);
199   DumpRegister32(os, "fs",  context.gregs[REG_FS]);
200   os << '\n';
201   DumpRegister32(os, "gs",  context.gregs[REG_GS]);
202   DumpRegister32(os, "ss",  context.gregs[REG_SS]);
203 #elif defined(__linux__) && defined(__x86_64__)
204   DumpRegister64(os, "rax", context.gregs[REG_RAX]);
205   DumpRegister64(os, "rbx", context.gregs[REG_RBX]);
206   DumpRegister64(os, "rcx", context.gregs[REG_RCX]);
207   DumpRegister64(os, "rdx", context.gregs[REG_RDX]);
208   os << '\n';
209 
210   DumpRegister64(os, "rdi", context.gregs[REG_RDI]);
211   DumpRegister64(os, "rsi", context.gregs[REG_RSI]);
212   DumpRegister64(os, "rbp", context.gregs[REG_RBP]);
213   DumpRegister64(os, "rsp", context.gregs[REG_RSP]);
214   os << '\n';
215 
216   DumpRegister64(os, "r8 ", context.gregs[REG_R8]);
217   DumpRegister64(os, "r9 ", context.gregs[REG_R9]);
218   DumpRegister64(os, "r10", context.gregs[REG_R10]);
219   DumpRegister64(os, "r11", context.gregs[REG_R11]);
220   os << '\n';
221 
222   DumpRegister64(os, "r12", context.gregs[REG_R12]);
223   DumpRegister64(os, "r13", context.gregs[REG_R13]);
224   DumpRegister64(os, "r14", context.gregs[REG_R14]);
225   DumpRegister64(os, "r15", context.gregs[REG_R15]);
226   os << '\n';
227 
228   DumpRegister64(os, "rip", context.gregs[REG_RIP]);
229   os << "   ";
230   DumpRegister32(os, "eflags", context.gregs[REG_EFL]);
231   DumpX86Flags(os, context.gregs[REG_EFL]);
232   os << '\n';
233 
234   DumpRegister32(os, "cs",  (context.gregs[REG_CSGSFS]) & 0x0FFFF);
235   DumpRegister32(os, "gs",  (context.gregs[REG_CSGSFS] >> 16) & 0x0FFFF);
236   DumpRegister32(os, "fs",  (context.gregs[REG_CSGSFS] >> 32) & 0x0FFFF);
237   os << '\n';
238 #elif defined(__linux__) && defined(__arm__)
239   DumpRegister32(os, "r0", context.arm_r0);
240   DumpRegister32(os, "r1", context.arm_r1);
241   DumpRegister32(os, "r2", context.arm_r2);
242   DumpRegister32(os, "r3", context.arm_r3);
243   os << '\n';
244 
245   DumpRegister32(os, "r4", context.arm_r4);
246   DumpRegister32(os, "r5", context.arm_r5);
247   DumpRegister32(os, "r6", context.arm_r6);
248   DumpRegister32(os, "r7", context.arm_r7);
249   os << '\n';
250 
251   DumpRegister32(os, "r8", context.arm_r8);
252   DumpRegister32(os, "r9", context.arm_r9);
253   DumpRegister32(os, "r10", context.arm_r10);
254   DumpRegister32(os, "fp", context.arm_fp);
255   os << '\n';
256 
257   DumpRegister32(os, "ip", context.arm_ip);
258   DumpRegister32(os, "sp", context.arm_sp);
259   DumpRegister32(os, "lr", context.arm_lr);
260   DumpRegister32(os, "pc", context.arm_pc);
261   os << '\n';
262 
263   DumpRegister32(os, "cpsr", context.arm_cpsr);
264   DumpArmStatusRegister(os, context.arm_cpsr);
265   os << '\n';
266 #elif defined(__linux__) && defined(__aarch64__)
267   for (size_t i = 0; i <= 30; ++i) {
268     std::string reg_name = "x" + std::to_string(i);
269     DumpRegister64(os, reg_name.c_str(), context.regs[i]);
270     if (i % 4 == 3) {
271       os << '\n';
272     }
273   }
274   os << '\n';
275 
276   DumpRegister64(os, "sp", context.sp);
277   DumpRegister64(os, "pc", context.pc);
278   os << '\n';
279 
280   DumpRegister64(os, "pstate", context.pstate);
281   DumpArmStatusRegister(os, context.pstate);
282   os << '\n';
283 #else
284   os << "Unknown architecture/word size/OS in ucontext dump";
285 #endif
286 }
287 
DumpRegister32(std::ostream & os,const char * name,uint32_t value) const288 void UContext::DumpRegister32(std::ostream& os, const char* name, uint32_t value) const {
289   os << StringPrintf(" %6s: 0x%08x", name, value);
290 }
291 
DumpRegister64(std::ostream & os,const char * name,uint64_t value) const292 void UContext::DumpRegister64(std::ostream& os, const char* name, uint64_t value) const {
293   os << StringPrintf(" %6s: 0x%016" PRIx64, name, value);
294 }
295 
DumpX86Flags(std::ostream & os,uint32_t flags) const296 void UContext::DumpX86Flags(std::ostream& os, uint32_t flags) const {
297   os << " [";
298   if ((flags & (1 << 0)) != 0) {
299     os << " CF";
300   }
301   if ((flags & (1 << 2)) != 0) {
302     os << " PF";
303   }
304   if ((flags & (1 << 4)) != 0) {
305     os << " AF";
306   }
307   if ((flags & (1 << 6)) != 0) {
308     os << " ZF";
309   }
310   if ((flags & (1 << 7)) != 0) {
311     os << " SF";
312   }
313   if ((flags & (1 << 8)) != 0) {
314     os << " TF";
315   }
316   if ((flags & (1 << 9)) != 0) {
317     os << " IF";
318   }
319   if ((flags & (1 << 10)) != 0) {
320     os << " DF";
321   }
322   if ((flags & (1 << 11)) != 0) {
323     os << " OF";
324   }
325   os << " ]";
326 }
327 
328 template <typename RegisterType>
DumpArmStatusRegister(std::ostream & os,RegisterType status_register) const329 void UContext::DumpArmStatusRegister(std::ostream& os, RegisterType status_register) const {
330   // Condition flags.
331   constexpr RegisterType kFlagV = 1U << 28;
332   constexpr RegisterType kFlagC = 1U << 29;
333   constexpr RegisterType kFlagZ = 1U << 30;
334   constexpr RegisterType kFlagN = 1U << 31;
335 
336   os << " [";
337   if ((status_register & kFlagN) != 0) {
338     os << " N";
339   }
340   if ((status_register & kFlagZ) != 0) {
341     os << " Z";
342   }
343   if ((status_register & kFlagC) != 0) {
344     os << " C";
345   }
346   if ((status_register & kFlagV) != 0) {
347     os << " V";
348   }
349   os << " ]";
350 }
351 
GetTimeoutSignal()352 int GetTimeoutSignal() {
353 #if defined(__APPLE__)
354   // Mac does not support realtime signals.
355   UNUSED(kUseSigRTTimeout);
356   return -1;
357 #else
358   return kUseSigRTTimeout ? (SIGRTMIN + 2) : -1;
359 #endif
360 }
361 
IsTimeoutSignal(int signal_number)362 static bool IsTimeoutSignal(int signal_number) {
363   return signal_number == GetTimeoutSignal();
364 }
365 
366 #if defined(__APPLE__)
367 // On macOS, clang complains about art::HandleUnexpectedSignalCommon's
368 // stack frame size being too large; disable that warning locally.
369 #pragma GCC diagnostic push
370 #pragma GCC diagnostic ignored "-Wframe-larger-than="
371 #endif
372 
GetFaultMessageForAbortLogging()373 std::string GetFaultMessageForAbortLogging() {
374   Runtime* runtime = Runtime::Current();
375   return  (runtime != nullptr) ? runtime->GetFaultMessage() : "";
376 }
377 
378 static std::atomic<bool> gIsRuntimeAbort = false;
379 
FlagRuntimeAbort()380 void FlagRuntimeAbort() {
381   gIsRuntimeAbort = true;
382 }
383 
HandleUnexpectedSignalCommonDump(int signal_number,siginfo_t * info,void * raw_context,bool handle_timeout_signal,bool dump_on_stderr)384 static void HandleUnexpectedSignalCommonDump(int signal_number,
385                                              siginfo_t* info,
386                                              void* raw_context,
387                                              bool handle_timeout_signal,
388                                              bool dump_on_stderr) {
389   auto logger = [&](auto& stream) {
390     bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||
391                         signal_number == SIGFPE || signal_number == SIGSEGV);
392     OsInfo os_info;
393     const char* cmd_line = GetCmdLine();
394     if (cmd_line == nullptr) {
395       cmd_line = "<unset>";  // Because no-one called InitLogging.
396     }
397     pid_t tid = GetTid();
398     std::string thread_name(GetThreadName(tid));
399     UContext thread_context(raw_context);
400     Backtrace thread_backtrace(raw_context);
401 
402     stream << "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***" << std::endl
403            << StringPrintf("Fatal signal %d (%s), code %d (%s)",
404                              signal_number,
405                              GetSignalName(signal_number),
406                              info->si_code,
407                              GetSignalCodeName(signal_number, info->si_code))
408            << (has_address ? StringPrintf(" fault addr %p", info->si_addr) : "") << std::endl
409            << "OS: " << Dumpable<OsInfo>(os_info) << std::endl
410            << "Cmdline: " << cmd_line << std::endl
411            << "Thread: " << tid << " \"" << thread_name << "\"" << std::endl
412            << "Registers:\n" << Dumpable<UContext>(thread_context) << std::endl
413            << "Backtrace:\n" << Dumpable<Backtrace>(thread_backtrace) << std::endl;
414     stream << std::flush;
415   };
416 
417   if (dump_on_stderr) {
418     // Note: We are using cerr directly instead of LOG macros to ensure even just partial output
419     //       makes it out. That means we lose the "dalvikvm..." prefix, but that is acceptable
420     //       considering this is an abort situation.
421     logger(std::cerr);
422   } else {
423     logger(LOG_STREAM(FATAL_WITHOUT_ABORT));
424   }
425   if (kIsDebugBuild && signal_number == SIGSEGV) {
426     PrintFileToLog("/proc/self/maps", android::base::LogSeverity::FATAL_WITHOUT_ABORT);
427   }
428 
429   Runtime* runtime = Runtime::Current();
430   if (runtime != nullptr) {
431     if (handle_timeout_signal && IsTimeoutSignal(signal_number)) {
432       // Special timeout signal. Try to dump all threads.
433       // Note: Do not use DumpForSigQuit, as that might disable native unwind, but the native parts
434       //       are of value here.
435       runtime->GetThreadList()->Dump(std::cerr, kDumpNativeStackOnTimeout);
436       std::cerr << std::endl;
437     }
438 
439     if (dump_on_stderr) {
440       std::cerr << "Fault message: " << GetFaultMessageForAbortLogging() << std::endl;
441     } else {
442       LOG(FATAL_WITHOUT_ABORT) << "Fault message: " << GetFaultMessageForAbortLogging();
443     }
444   }
445 }
446 
HandleUnexpectedSignalCommon(int signal_number,siginfo_t * info,void * raw_context,bool handle_timeout_signal,bool dump_on_stderr)447 void HandleUnexpectedSignalCommon(int signal_number,
448                                   siginfo_t* info,
449                                   void* raw_context,
450                                   bool handle_timeout_signal,
451                                   bool dump_on_stderr) {
452   bool runtime_abort = gIsRuntimeAbort.exchange(false);
453   if (runtime_abort) {
454     return;
455   }
456 
457   // Local _static_ storing the currently handled signal (or -1).
458   static int handling_unexpected_signal = -1;
459 
460   // Whether the dump code should be run under the unexpected-signal lock. For diagnostics we
461   // allow recursive unexpected-signals in certain cases - avoid a deadlock.
462   bool grab_lock = true;
463 
464   if (handling_unexpected_signal != -1) {
465     LogHelper::LogLineLowStack(__FILE__,
466                                __LINE__,
467                                ::android::base::FATAL_WITHOUT_ABORT,
468                                "HandleUnexpectedSignal reentered\n");
469     // Print the signal number. Don't use any standard functions, just some arithmetic. Just best
470     // effort, with a minimal buffer.
471     if (0 < signal_number && signal_number < 100) {
472       char buf[] = { ' ',
473                      'S',
474                      static_cast<char>('0' + (signal_number / 10)),
475                      static_cast<char>('0' + (signal_number % 10)),
476                      '\n',
477                      0 };
478       LogHelper::LogLineLowStack(__FILE__,
479                                  __LINE__,
480                                  ::android::base::FATAL_WITHOUT_ABORT,
481                                  buf);
482     }
483     if (handle_timeout_signal) {
484       if (IsTimeoutSignal(signal_number)) {
485         // Ignore a recursive timeout.
486         return;
487       }
488     }
489     // If we were handling a timeout signal, try to go on. Otherwise hard-exit.
490     // This relies on the expectation that we'll only ever get one timeout signal.
491     if (!handle_timeout_signal || handling_unexpected_signal != GetTimeoutSignal()) {
492       _exit(1);
493     }
494     grab_lock = false;  // The "outer" handling instance already holds the lock.
495   }
496   handling_unexpected_signal = signal_number;
497 
498   gAborting++;  // set before taking any locks
499 
500   if (grab_lock) {
501     MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
502 
503     HandleUnexpectedSignalCommonDump(signal_number,
504                                      info,
505                                      raw_context,
506                                      handle_timeout_signal,
507                                      dump_on_stderr);
508   } else {
509     HandleUnexpectedSignalCommonDump(signal_number,
510                                      info,
511                                      raw_context,
512                                      handle_timeout_signal,
513                                      dump_on_stderr);
514   }
515 }
516 
517 #if defined(__APPLE__)
518 #pragma GCC diagnostic pop
519 #endif
520 
InitPlatformSignalHandlersCommon(void (* newact)(int,siginfo_t *,void *),struct sigaction * oldact,bool handle_timeout_signal)521 void InitPlatformSignalHandlersCommon(void (*newact)(int, siginfo_t*, void*),
522                                       struct sigaction* oldact,
523                                       bool handle_timeout_signal) {
524   struct sigaction action;
525   memset(&action, 0, sizeof(action));
526   sigemptyset(&action.sa_mask);
527   action.sa_sigaction = newact;
528   // Use the three-argument sa_sigaction handler.
529   action.sa_flags |= SA_SIGINFO;
530   // Use the alternate signal stack so we can catch stack overflows.
531   action.sa_flags |= SA_ONSTACK;
532 
533   int rc = 0;
534   rc += sigaction(SIGABRT, &action, oldact);
535   rc += sigaction(SIGBUS, &action, oldact);
536   rc += sigaction(SIGFPE, &action, oldact);
537   rc += sigaction(SIGILL, &action, oldact);
538   rc += sigaction(SIGPIPE, &action, oldact);
539   rc += sigaction(SIGSEGV, &action, oldact);
540 #if defined(SIGSTKFLT)
541   rc += sigaction(SIGSTKFLT, &action, oldact);
542 #endif
543   rc += sigaction(SIGTRAP, &action, oldact);
544   // Special dump-all timeout.
545   if (handle_timeout_signal && GetTimeoutSignal() != -1) {
546     rc += sigaction(GetTimeoutSignal(), &action, oldact);
547   }
548   CHECK_EQ(rc, 0);
549 }
550 
551 }  // namespace art
552