| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 1 | //===-- asan_linux.cc -----------------------------------------------------===// |
| 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 is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Posix-specific details. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #if defined(__linux__) || defined(__APPLE__) |
| 15 | |
| 16 | #include "asan_internal.h" |
| 17 | #include "asan_interceptors.h" |
| 18 | #include "asan_stack.h" |
| 19 | #include "asan_thread_registry.h" |
| 20 | |
| 21 | #include <signal.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <sys/resource.h> |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame^] | 24 | #include <unistd.h> |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 25 | |
| 26 | namespace __asan { |
| 27 | |
| 28 | static void MaybeInstallSigaction(int signum, |
| 29 | void (*handler)(int, siginfo_t *, void *)) { |
| 30 | if (!AsanInterceptsSignal(signum)) |
| 31 | return; |
| 32 | struct sigaction sigact; |
| 33 | real_memset(&sigact, 0, sizeof(sigact)); |
| 34 | sigact.sa_sigaction = handler; |
| 35 | sigact.sa_flags = SA_SIGINFO; |
| 36 | CHECK(0 == real_sigaction(signum, &sigact, 0)); |
| 37 | } |
| 38 | |
| 39 | static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) { |
| 40 | uintptr_t addr = (uintptr_t)siginfo->si_addr; |
| 41 | // Write the first message using the bullet-proof write. |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame^] | 42 | if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie(); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 43 | uintptr_t pc, sp, bp; |
| 44 | GetPcSpBp(context, &pc, &sp, &bp); |
| 45 | Report("ERROR: AddressSanitizer crashed on unknown address %p" |
| 46 | " (pc %p sp %p bp %p T%d)\n", |
| 47 | addr, pc, sp, bp, |
| 48 | asanThreadRegistry().GetCurrentTidOrMinusOne()); |
| 49 | Printf("AddressSanitizer can not provide additional info. ABORTING\n"); |
| 50 | GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp); |
| 51 | stack.PrintStack(); |
| 52 | ShowStatsAndAbort(); |
| 53 | } |
| 54 | |
| 55 | void InstallSignalHandlers() { |
| 56 | MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV); |
| 57 | MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV); |
| 58 | } |
| 59 | |
| 60 | void AsanDisableCoreDumper() { |
| 61 | struct rlimit nocore; |
| 62 | nocore.rlim_cur = 0; |
| 63 | nocore.rlim_max = 0; |
| 64 | setrlimit(RLIMIT_CORE, &nocore); |
| 65 | } |
| 66 | |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame^] | 67 | void AsanDie() { |
| 68 | _exit(FLAG_exitcode); |
| 69 | } |
| 70 | |
| 71 | int GetPid() { |
| 72 | return getpid(); |
| 73 | } |
| 74 | |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 75 | } // namespace __asan |
| 76 | |
| 77 | #endif // __linux__ || __APPLE_ |