| 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 | |
| Kostya Serebryany | dde7c33 | 2012-01-11 02:39:16 +0000 | [diff] [blame] | 27 | #ifdef ANDROID |
| 28 | #include <sys/atomics.h> |
| 29 | #endif |
| 30 | |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 31 | namespace __asan { |
| 32 | |
| 33 | static void MaybeInstallSigaction(int signum, |
| 34 | void (*handler)(int, siginfo_t *, void *)) { |
| 35 | if (!AsanInterceptsSignal(signum)) |
| 36 | return; |
| 37 | struct sigaction sigact; |
| Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame^] | 38 | REAL(memset)(&sigact, 0, sizeof(sigact)); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 39 | sigact.sa_sigaction = handler; |
| 40 | sigact.sa_flags = SA_SIGINFO; |
| Alexey Samsonov | 09672ca | 2012-02-08 13:45:31 +0000 | [diff] [blame^] | 41 | CHECK(0 == REAL(sigaction)(signum, &sigact, 0)); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) { |
| 45 | uintptr_t addr = (uintptr_t)siginfo->si_addr; |
| 46 | // Write the first message using the bullet-proof write. |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 47 | if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie(); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 48 | uintptr_t pc, sp, bp; |
| 49 | GetPcSpBp(context, &pc, &sp, &bp); |
| 50 | Report("ERROR: AddressSanitizer crashed on unknown address %p" |
| 51 | " (pc %p sp %p bp %p T%d)\n", |
| 52 | addr, pc, sp, bp, |
| 53 | asanThreadRegistry().GetCurrentTidOrMinusOne()); |
| 54 | Printf("AddressSanitizer can not provide additional info. ABORTING\n"); |
| Evgeniy Stepanov | 9cfa194 | 2012-01-19 11:34:18 +0000 | [diff] [blame] | 55 | GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp); |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 56 | stack.PrintStack(); |
| 57 | ShowStatsAndAbort(); |
| 58 | } |
| 59 | |
| 60 | void InstallSignalHandlers() { |
| 61 | MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV); |
| 62 | MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV); |
| 63 | } |
| 64 | |
| 65 | void AsanDisableCoreDumper() { |
| 66 | struct rlimit nocore; |
| 67 | nocore.rlim_cur = 0; |
| 68 | nocore.rlim_max = 0; |
| 69 | setrlimit(RLIMIT_CORE, &nocore); |
| 70 | } |
| 71 | |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 72 | void AsanDie() { |
| Kostya Serebryany | cb00d13 | 2012-01-31 00:52:18 +0000 | [diff] [blame] | 73 | if (FLAG_sleep_before_dying) { |
| 74 | Report("Sleeping for %d second(s)\n", FLAG_sleep_before_dying); |
| 75 | sleep(FLAG_sleep_before_dying); |
| 76 | } |
| Kostya Serebryany | 0ecf5eb | 2012-01-09 23:11:26 +0000 | [diff] [blame] | 77 | _exit(FLAG_exitcode); |
| 78 | } |
| 79 | |
| 80 | int GetPid() { |
| 81 | return getpid(); |
| 82 | } |
| 83 | |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 84 | uintptr_t GetThreadSelf() { |
| 85 | return (uintptr_t)pthread_self(); |
| 86 | } |
| 87 | |
| Kostya Serebryany | dde7c33 | 2012-01-11 02:39:16 +0000 | [diff] [blame] | 88 | int AtomicInc(int *a) { |
| 89 | #ifdef ANDROID |
| 90 | return __atomic_inc(a) + 1; |
| 91 | #else |
| 92 | return __sync_add_and_fetch(a, 1); |
| 93 | #endif |
| 94 | } |
| 95 | |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 96 | // ---------------------- TSD ---------------- {{{1 |
| 97 | |
| 98 | static pthread_key_t tsd_key; |
| 99 | static bool tsd_key_inited = false; |
| Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 100 | void AsanTSDInit(void (*destructor)(void *tsd)) { |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 101 | CHECK(!tsd_key_inited); |
| 102 | tsd_key_inited = true; |
| Kostya Serebryany | f58f998 | 2012-02-07 00:27:15 +0000 | [diff] [blame] | 103 | CHECK(0 == pthread_key_create(&tsd_key, destructor)); |
| Kostya Serebryany | cc4e686 | 2012-01-11 02:21:06 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void *AsanTSDGet() { |
| 107 | CHECK(tsd_key_inited); |
| 108 | return pthread_getspecific(tsd_key); |
| 109 | } |
| 110 | |
| 111 | void AsanTSDSet(void *tsd) { |
| 112 | CHECK(tsd_key_inited); |
| 113 | pthread_setspecific(tsd_key, tsd); |
| 114 | } |
| 115 | |
| Kostya Serebryany | a7e760a | 2012-01-09 19:18:27 +0000 | [diff] [blame] | 116 | } // namespace __asan |
| 117 | |
| 118 | #endif // __linux__ || __APPLE_ |