1//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines some helpful functions for dealing with the possibility of 11// Unix signals occurring while your program is running. 12// 13//===----------------------------------------------------------------------===// 14 15#include "Unix.h" 16#include "llvm/ADT/STLExtras.h" 17#include "llvm/Demangle/Demangle.h" 18#include "llvm/Support/Format.h" 19#include "llvm/Support/FileSystem.h" 20#include "llvm/Support/FileUtilities.h" 21#include "llvm/Support/MemoryBuffer.h" 22#include "llvm/Support/Mutex.h" 23#include "llvm/Support/Program.h" 24#include "llvm/Support/UniqueLock.h" 25#include "llvm/Support/raw_ostream.h" 26#include <algorithm> 27#include <string> 28#if HAVE_EXECINFO_H 29# include <execinfo.h> // For backtrace(). 30#endif 31#if HAVE_SIGNAL_H 32#include <signal.h> 33#endif 34#if HAVE_SYS_STAT_H 35#include <sys/stat.h> 36#endif 37#if HAVE_DLFCN_H 38#include <dlfcn.h> 39#endif 40#if HAVE_MACH_MACH_H 41#include <mach/mach.h> 42#endif 43#if HAVE_LINK_H 44#include <link.h> 45#endif 46#ifdef HAVE__UNWIND_BACKTRACE 47// FIXME: We should be able to use <unwind.h> for any target that has an 48// _Unwind_Backtrace function, but on FreeBSD the configure test passes 49// despite the function not existing, and on Android, <unwind.h> conflicts 50// with <link.h>. 51#if defined(__GLIBC__) || defined(__APPLE__) 52#include <unwind.h> 53#else 54#undef HAVE__UNWIND_BACKTRACE 55#endif 56#endif 57 58using namespace llvm; 59 60static RETSIGTYPE SignalHandler(int Sig); // defined below. 61 62static ManagedStatic<SmartMutex<true> > SignalsMutex; 63 64/// InterruptFunction - The function to call if ctrl-c is pressed. 65static void (*InterruptFunction)() = nullptr; 66 67static ManagedStatic<std::vector<std::string>> FilesToRemove; 68 69static StringRef Argv0; 70 71// IntSigs - Signals that represent requested termination. There's no bug 72// or failure, or if there is, it's not our direct responsibility. For whatever 73// reason, our continued execution is no longer desirable. 74static const int IntSigs[] = { 75 SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 76}; 77 78// KillSigs - Signals that represent that we have a bug, and our prompt 79// termination has been ordered. 80static const int KillSigs[] = { 81 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT 82#ifdef SIGSYS 83 , SIGSYS 84#endif 85#ifdef SIGXCPU 86 , SIGXCPU 87#endif 88#ifdef SIGXFSZ 89 , SIGXFSZ 90#endif 91#ifdef SIGEMT 92 , SIGEMT 93#endif 94}; 95 96static unsigned NumRegisteredSignals = 0; 97static struct { 98 struct sigaction SA; 99 int SigNo; 100} RegisteredSignalInfo[array_lengthof(IntSigs) + array_lengthof(KillSigs)]; 101 102 103static void RegisterHandler(int Signal) { 104 assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) && 105 "Out of space for signal handlers!"); 106 107 struct sigaction NewHandler; 108 109 NewHandler.sa_handler = SignalHandler; 110 NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK; 111 sigemptyset(&NewHandler.sa_mask); 112 113 // Install the new handler, save the old one in RegisteredSignalInfo. 114 sigaction(Signal, &NewHandler, 115 &RegisteredSignalInfo[NumRegisteredSignals].SA); 116 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal; 117 ++NumRegisteredSignals; 118} 119 120#if defined(HAVE_SIGALTSTACK) 121// Hold onto both the old and new alternate signal stack so that it's not 122// reported as a leak. We don't make any attempt to remove our alt signal 123// stack if we remove our signal handlers; that can't be done reliably if 124// someone else is also trying to do the same thing. 125static stack_t OldAltStack; 126static void* NewAltStackPointer; 127 128static void CreateSigAltStack() { 129 const size_t AltStackSize = MINSIGSTKSZ + 64 * 1024; 130 131 // If we're executing on the alternate stack, or we already have an alternate 132 // signal stack that we're happy with, there's nothing for us to do. Don't 133 // reduce the size, some other part of the process might need a larger stack 134 // than we do. 135 if (sigaltstack(nullptr, &OldAltStack) != 0 || 136 OldAltStack.ss_flags & SS_ONSTACK || 137 (OldAltStack.ss_sp && OldAltStack.ss_size >= AltStackSize)) 138 return; 139 140 stack_t AltStack = {}; 141 AltStack.ss_sp = reinterpret_cast<char *>(malloc(AltStackSize)); 142 NewAltStackPointer = AltStack.ss_sp; // Save to avoid reporting a leak. 143 AltStack.ss_size = AltStackSize; 144 if (sigaltstack(&AltStack, &OldAltStack) != 0) 145 free(AltStack.ss_sp); 146} 147#else 148static void CreateSigAltStack() {} 149#endif 150 151static void RegisterHandlers() { 152 // We need to dereference the signals mutex during handler registration so 153 // that we force its construction. This is to prevent the first use being 154 // during handling an actual signal because you can't safely call new in a 155 // signal handler. 156 *SignalsMutex; 157 158 // If the handlers are already registered, we're done. 159 if (NumRegisteredSignals != 0) return; 160 161 // Create an alternate stack for signal handling. This is necessary for us to 162 // be able to reliably handle signals due to stack overflow. 163 CreateSigAltStack(); 164 165 for (auto S : IntSigs) RegisterHandler(S); 166 for (auto S : KillSigs) RegisterHandler(S); 167} 168 169static void UnregisterHandlers() { 170 // Restore all of the signal handlers to how they were before we showed up. 171 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i) 172 sigaction(RegisteredSignalInfo[i].SigNo, 173 &RegisteredSignalInfo[i].SA, nullptr); 174 NumRegisteredSignals = 0; 175} 176 177 178/// RemoveFilesToRemove - Process the FilesToRemove list. This function 179/// should be called with the SignalsMutex lock held. 180/// NB: This must be an async signal safe function. It cannot allocate or free 181/// memory, even in debug builds. 182static void RemoveFilesToRemove() { 183 // Avoid constructing ManagedStatic in the signal handler. 184 // If FilesToRemove is not constructed, there are no files to remove. 185 if (!FilesToRemove.isConstructed()) 186 return; 187 188 // We avoid iterators in case of debug iterators that allocate or release 189 // memory. 190 std::vector<std::string>& FilesToRemoveRef = *FilesToRemove; 191 for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) { 192 const char *path = FilesToRemoveRef[i].c_str(); 193 194 // Get the status so we can determine if it's a file or directory. If we 195 // can't stat the file, ignore it. 196 struct stat buf; 197 if (stat(path, &buf) != 0) 198 continue; 199 200 // If this is not a regular file, ignore it. We want to prevent removal of 201 // special files like /dev/null, even if the compiler is being run with the 202 // super-user permissions. 203 if (!S_ISREG(buf.st_mode)) 204 continue; 205 206 // Otherwise, remove the file. We ignore any errors here as there is nothing 207 // else we can do. 208 unlink(path); 209 } 210} 211 212// SignalHandler - The signal handler that runs. 213static RETSIGTYPE SignalHandler(int Sig) { 214 // Restore the signal behavior to default, so that the program actually 215 // crashes when we return and the signal reissues. This also ensures that if 216 // we crash in our signal handler that the program will terminate immediately 217 // instead of recursing in the signal handler. 218 UnregisterHandlers(); 219 220 // Unmask all potentially blocked kill signals. 221 sigset_t SigMask; 222 sigfillset(&SigMask); 223 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); 224 225 { 226 unique_lock<SmartMutex<true>> Guard(*SignalsMutex); 227 RemoveFilesToRemove(); 228 229 if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig) 230 != std::end(IntSigs)) { 231 if (InterruptFunction) { 232 void (*IF)() = InterruptFunction; 233 Guard.unlock(); 234 InterruptFunction = nullptr; 235 IF(); // run the interrupt function. 236 return; 237 } 238 239 Guard.unlock(); 240 raise(Sig); // Execute the default handler. 241 return; 242 } 243 } 244 245 // Otherwise if it is a fault (like SEGV) run any handler. 246 llvm::sys::RunSignalHandlers(); 247 248#ifdef __s390__ 249 // On S/390, certain signals are delivered with PSW Address pointing to 250 // *after* the faulting instruction. Simply returning from the signal 251 // handler would continue execution after that point, instead of 252 // re-raising the signal. Raise the signal manually in those cases. 253 if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP) 254 raise(Sig); 255#endif 256} 257 258void llvm::sys::RunInterruptHandlers() { 259 sys::SmartScopedLock<true> Guard(*SignalsMutex); 260 RemoveFilesToRemove(); 261} 262 263void llvm::sys::SetInterruptFunction(void (*IF)()) { 264 { 265 sys::SmartScopedLock<true> Guard(*SignalsMutex); 266 InterruptFunction = IF; 267 } 268 RegisterHandlers(); 269} 270 271// RemoveFileOnSignal - The public API 272bool llvm::sys::RemoveFileOnSignal(StringRef Filename, 273 std::string* ErrMsg) { 274 { 275 sys::SmartScopedLock<true> Guard(*SignalsMutex); 276 FilesToRemove->push_back(Filename); 277 } 278 279 RegisterHandlers(); 280 return false; 281} 282 283// DontRemoveFileOnSignal - The public API 284void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) { 285 sys::SmartScopedLock<true> Guard(*SignalsMutex); 286 std::vector<std::string>::reverse_iterator RI = 287 find(reverse(*FilesToRemove), Filename); 288 std::vector<std::string>::iterator I = FilesToRemove->end(); 289 if (RI != FilesToRemove->rend()) 290 I = FilesToRemove->erase(RI.base()-1); 291} 292 293/// AddSignalHandler - Add a function to be called when a signal is delivered 294/// to the process. The handler can have a cookie passed to it to identify 295/// what instance of the handler it is. 296void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { 297 CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie)); 298 RegisterHandlers(); 299} 300 301#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && HAVE_LINK_H && \ 302 (defined(__linux__) || defined(__FreeBSD__) || \ 303 defined(__FreeBSD_kernel__) || defined(__NetBSD__)) 304struct DlIteratePhdrData { 305 void **StackTrace; 306 int depth; 307 bool first; 308 const char **modules; 309 intptr_t *offsets; 310 const char *main_exec_name; 311}; 312 313static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) { 314 DlIteratePhdrData *data = (DlIteratePhdrData*)arg; 315 const char *name = data->first ? data->main_exec_name : info->dlpi_name; 316 data->first = false; 317 for (int i = 0; i < info->dlpi_phnum; i++) { 318 const auto *phdr = &info->dlpi_phdr[i]; 319 if (phdr->p_type != PT_LOAD) 320 continue; 321 intptr_t beg = info->dlpi_addr + phdr->p_vaddr; 322 intptr_t end = beg + phdr->p_memsz; 323 for (int j = 0; j < data->depth; j++) { 324 if (data->modules[j]) 325 continue; 326 intptr_t addr = (intptr_t)data->StackTrace[j]; 327 if (beg <= addr && addr < end) { 328 data->modules[j] = name; 329 data->offsets[j] = addr - info->dlpi_addr; 330 } 331 } 332 } 333 return 0; 334} 335 336/// If this is an ELF platform, we can find all loaded modules and their virtual 337/// addresses with dl_iterate_phdr. 338static bool findModulesAndOffsets(void **StackTrace, int Depth, 339 const char **Modules, intptr_t *Offsets, 340 const char *MainExecutableName, 341 StringSaver &StrPool) { 342 DlIteratePhdrData data = {StackTrace, Depth, true, 343 Modules, Offsets, MainExecutableName}; 344 dl_iterate_phdr(dl_iterate_phdr_cb, &data); 345 return true; 346} 347#else 348/// This platform does not have dl_iterate_phdr, so we do not yet know how to 349/// find all loaded DSOs. 350static bool findModulesAndOffsets(void **StackTrace, int Depth, 351 const char **Modules, intptr_t *Offsets, 352 const char *MainExecutableName, 353 StringSaver &StrPool) { 354 return false; 355} 356#endif // defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && ... 357 358#if ENABLE_BACKTRACES && defined(HAVE__UNWIND_BACKTRACE) 359static int unwindBacktrace(void **StackTrace, int MaxEntries) { 360 if (MaxEntries < 0) 361 return 0; 362 363 // Skip the first frame ('unwindBacktrace' itself). 364 int Entries = -1; 365 366 auto HandleFrame = [&](_Unwind_Context *Context) -> _Unwind_Reason_Code { 367 // Apparently we need to detect reaching the end of the stack ourselves. 368 void *IP = (void *)_Unwind_GetIP(Context); 369 if (!IP) 370 return _URC_END_OF_STACK; 371 372 assert(Entries < MaxEntries && "recursively called after END_OF_STACK?"); 373 if (Entries >= 0) 374 StackTrace[Entries] = IP; 375 376 if (++Entries == MaxEntries) 377 return _URC_END_OF_STACK; 378 return _URC_NO_REASON; 379 }; 380 381 _Unwind_Backtrace( 382 [](_Unwind_Context *Context, void *Handler) { 383 return (*static_cast<decltype(HandleFrame) *>(Handler))(Context); 384 }, 385 static_cast<void *>(&HandleFrame)); 386 return std::max(Entries, 0); 387} 388#endif 389 390// PrintStackTrace - In the case of a program crash or fault, print out a stack 391// trace so that the user has an indication of why and where we died. 392// 393// On glibc systems we have the 'backtrace' function, which works nicely, but 394// doesn't demangle symbols. 395void llvm::sys::PrintStackTrace(raw_ostream &OS) { 396#if ENABLE_BACKTRACES 397 static void *StackTrace[256]; 398 int depth = 0; 399#if defined(HAVE_BACKTRACE) 400 // Use backtrace() to output a backtrace on Linux systems with glibc. 401 if (!depth) 402 depth = backtrace(StackTrace, static_cast<int>(array_lengthof(StackTrace))); 403#endif 404#if defined(HAVE__UNWIND_BACKTRACE) 405 // Try _Unwind_Backtrace() if backtrace() failed. 406 if (!depth) 407 depth = unwindBacktrace(StackTrace, 408 static_cast<int>(array_lengthof(StackTrace))); 409#endif 410 if (!depth) 411 return; 412 413 if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS)) 414 return; 415#if HAVE_DLFCN_H && __GNUG__ && !defined(__CYGWIN__) 416 int width = 0; 417 for (int i = 0; i < depth; ++i) { 418 Dl_info dlinfo; 419 dladdr(StackTrace[i], &dlinfo); 420 const char* name = strrchr(dlinfo.dli_fname, '/'); 421 422 int nwidth; 423 if (!name) nwidth = strlen(dlinfo.dli_fname); 424 else nwidth = strlen(name) - 1; 425 426 if (nwidth > width) width = nwidth; 427 } 428 429 for (int i = 0; i < depth; ++i) { 430 Dl_info dlinfo; 431 dladdr(StackTrace[i], &dlinfo); 432 433 OS << format("%-2d", i); 434 435 const char* name = strrchr(dlinfo.dli_fname, '/'); 436 if (!name) OS << format(" %-*s", width, dlinfo.dli_fname); 437 else OS << format(" %-*s", width, name+1); 438 439 OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2, 440 (unsigned long)StackTrace[i]); 441 442 if (dlinfo.dli_sname != nullptr) { 443 OS << ' '; 444 int res; 445 char* d = itaniumDemangle(dlinfo.dli_sname, nullptr, nullptr, &res); 446 if (!d) OS << dlinfo.dli_sname; 447 else OS << d; 448 free(d); 449 450 // FIXME: When we move to C++11, use %t length modifier. It's not in 451 // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of 452 // the stack offset for a stack dump isn't likely to cause any problems. 453 OS << format(" + %u",(unsigned)((char*)StackTrace[i]- 454 (char*)dlinfo.dli_saddr)); 455 } 456 OS << '\n'; 457 } 458#elif defined(HAVE_BACKTRACE) 459 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); 460#endif 461#endif 462} 463 464static void PrintStackTraceSignalHandler(void *) { 465 PrintStackTrace(llvm::errs()); 466} 467 468void llvm::sys::DisableSystemDialogsOnCrash() {} 469 470/// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or 471/// SIGSEGV) is delivered to the process, print a stack trace and then exit. 472void llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0, 473 bool DisableCrashReporting) { 474 ::Argv0 = Argv0; 475 476 AddSignalHandler(PrintStackTraceSignalHandler, nullptr); 477 478#if defined(__APPLE__) && ENABLE_CRASH_OVERRIDES 479 // Environment variable to disable any kind of crash dialog. 480 if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) { 481 mach_port_t self = mach_task_self(); 482 483 exception_mask_t mask = EXC_MASK_CRASH; 484 485 kern_return_t ret = task_set_exception_ports(self, 486 mask, 487 MACH_PORT_NULL, 488 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, 489 THREAD_STATE_NONE); 490 (void)ret; 491 } 492#endif 493} 494