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