| 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 | |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 21 | #include <pthread.h> |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 22 | #include <signal.h> |
| 23 | #include <sys/time.h> |
| 24 | #include <sys/resource.h> |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 26 | |
| 27 | namespace __asan { |
| 28 | |
| 29 | static void MaybeInstallSigaction(int signum, |
| 30 | void (*handler)(int, siginfo_t *, void *)) { |
| 31 | if (!AsanInterceptsSignal(signum)) |
| 32 | return; |
| 33 | struct sigaction sigact; |
| 34 | real_memset(&sigact, 0, sizeof(sigact)); |
| 35 | sigact.sa_sigaction = handler; |
| 36 | sigact.sa_flags = SA_SIGINFO; |
| 37 | CHECK(0 == real_sigaction(signum, &sigact, 0)); |
| 38 | } |
| 39 | |
| 40 | static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) { |
| 41 | uintptr_t addr = (uintptr_t)siginfo->si_addr; |
| 42 | // Write the first message using the bullet-proof write. |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 43 | if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie(); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 44 | uintptr_t pc, sp, bp; |
| 45 | GetPcSpBp(context, &pc, &sp, &bp); |
| 46 | Report("ERROR: AddressSanitizer crashed on unknown address %p" |
| 47 | " (pc %p sp %p bp %p T%d)\n", |
| 48 | addr, pc, sp, bp, |
| 49 | asanThreadRegistry().GetCurrentTidOrMinusOne()); |
| 50 | Printf("AddressSanitizer can not provide additional info. ABORTING\n"); |
| 51 | GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp); |
| 52 | stack.PrintStack(); |
| 53 | ShowStatsAndAbort(); |
| 54 | } |
| 55 | |
| 56 | void InstallSignalHandlers() { |
| 57 | MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV); |
| 58 | MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV); |
| 59 | } |
| 60 | |
| 61 | void AsanDisableCoreDumper() { |
| 62 | struct rlimit nocore; |
| 63 | nocore.rlim_cur = 0; |
| 64 | nocore.rlim_max = 0; |
| 65 | setrlimit(RLIMIT_CORE, &nocore); |
| 66 | } |
| 67 | |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 68 | void AsanDie() { |
| 69 | _exit(FLAG_exitcode); |
| 70 | } |
| 71 | |
| 72 | int GetPid() { |
| 73 | return getpid(); |
| 74 | } |
| 75 | |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 76 | uintptr_t GetThreadSelf() { |
| 77 | return (uintptr_t)pthread_self(); |
| 78 | } |
| 79 | |
| 80 | // ---------------------- TSD ---------------- {{{1 |
| 81 | |
| 82 | static pthread_key_t tsd_key; |
| 83 | static bool tsd_key_inited = false; |
| 84 | void AsanTSDInit() { |
| 85 | CHECK(!tsd_key_inited); |
| 86 | tsd_key_inited = true; |
| 87 | CHECK(0 == pthread_key_create(&tsd_key, 0)); |
| 88 | } |
| 89 | |
| 90 | void *AsanTSDGet() { |
| 91 | CHECK(tsd_key_inited); |
| 92 | return pthread_getspecific(tsd_key); |
| 93 | } |
| 94 | |
| 95 | void AsanTSDSet(void *tsd) { |
| 96 | CHECK(tsd_key_inited); |
| 97 | pthread_setspecific(tsd_key, tsd); |
| 98 | } |
| 99 | |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 100 | } // namespace __asan |
| 101 | |
| 102 | #endif // __linux__ || __APPLE_ |