blob: 061bb193034df69a35835ce80147fafa062ecc8f [file] [log] [blame]
Kostya Serebryanya7e760a2012-01-09 19:18:27 +00001//===-- 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"
Evgeniy Stepanovc99f7002012-05-23 15:21:50 +000018#include "asan_mapping.h"
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000019#include "asan_stack.h"
20#include "asan_thread_registry.h"
Alexey Samsonov2221f552012-06-05 08:48:10 +000021#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000022#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000023
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000024#include <pthread.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000025#include <signal.h>
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000026#include <stdlib.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000027#include <sys/time.h>
28#include <sys/resource.h>
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000029#include <unistd.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000030
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000031static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough.
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000032
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000033namespace __asan {
34
35static void MaybeInstallSigaction(int signum,
36 void (*handler)(int, siginfo_t *, void *)) {
37 if (!AsanInterceptsSignal(signum))
38 return;
39 struct sigaction sigact;
Alexey Samsonov09672ca2012-02-08 13:45:31 +000040 REAL(memset)(&sigact, 0, sizeof(sigact));
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000041 sigact.sa_sigaction = handler;
42 sigact.sa_flags = SA_SIGINFO;
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000043 if (flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
Alexey Samsonov09672ca2012-02-08 13:45:31 +000044 CHECK(0 == REAL(sigaction)(signum, &sigact, 0));
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000045 if (flags()->verbosity >= 1) {
Alexander Potapenkoa87bdaa2012-05-30 15:29:11 +000046 Report("Installed the sigaction for signal %d\n", signum);
47 }
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000048}
49
50static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000051 uptr addr = (uptr)siginfo->si_addr;
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000052 // Write the first message using the bullet-proof write.
Alexey Samsonov47657ce2012-06-06 07:02:44 +000053 if (13 != internal_write(2, "ASAN:SIGSEGV\n", 13)) Die();
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000054 uptr pc, sp, bp;
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000055 GetPcSpBp(context, &pc, &sp, &bp);
Alexey Samsonove9541012012-06-06 13:11:29 +000056 AsanReport("ERROR: AddressSanitizer crashed on unknown address %p"
57 " (pc %p sp %p bp %p T%d)\n",
58 (void*)addr, (void*)pc, (void*)sp, (void*)bp,
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +000059 asanThreadRegistry().GetCurrentTidOrInvalid());
Alexey Samsonove9541012012-06-06 13:11:29 +000060 AsanPrintf("AddressSanitizer can not provide additional info. ABORTING\n");
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +000061 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000062 stack.PrintStack();
63 ShowStatsAndAbort();
64}
65
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000066void SetAlternateSignalStack() {
67 stack_t altstack, oldstack;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000068 CHECK(0 == sigaltstack(0, &oldstack));
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000069 // If the alternate stack is already in place, do nothing.
70 if ((oldstack.ss_flags & SS_DISABLE) == 0) return;
71 // TODO(glider): the mapped stack should have the MAP_STACK flag in the
72 // future. It is not required by man 2 sigaltstack now (they're using
73 // malloc()).
Alexey Samsonova25b3462012-06-06 16:15:07 +000074 void* base = MmapOrDie(kAltStackSize, __FUNCTION__);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000075 altstack.ss_sp = base;
76 altstack.ss_flags = 0;
77 altstack.ss_size = kAltStackSize;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000078 CHECK(0 == sigaltstack(&altstack, 0));
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000079 if (flags()->verbosity > 0) {
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000080 Report("Alternative stack for T%d set: [%p,%p)\n",
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +000081 asanThreadRegistry().GetCurrentTidOrInvalid(),
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000082 altstack.ss_sp, (char*)altstack.ss_sp + altstack.ss_size);
83 }
84}
85
86void UnsetAlternateSignalStack() {
87 stack_t altstack, oldstack;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000088 altstack.ss_sp = 0;
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000089 altstack.ss_flags = SS_DISABLE;
90 altstack.ss_size = 0;
91 CHECK(0 == sigaltstack(&altstack, &oldstack));
Alexey Samsonova25b3462012-06-06 16:15:07 +000092 UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000093}
94
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000095void InstallSignalHandlers() {
Alexander Potapenkof03d8af2012-04-05 10:54:52 +000096 // Set the alternate signal stack for the main thread.
97 // This will cause SetAlternateSignalStack to be called twice, but the stack
98 // will be actually set only once.
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000099 if (flags()->use_sigaltstack) SetAlternateSignalStack();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000100 MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
101 MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
102}
103
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000104// ---------------------- TSD ---------------- {{{1
105
106static pthread_key_t tsd_key;
107static bool tsd_key_inited = false;
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000108void AsanTSDInit(void (*destructor)(void *tsd)) {
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000109 CHECK(!tsd_key_inited);
110 tsd_key_inited = true;
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000111 CHECK(0 == pthread_key_create(&tsd_key, destructor));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000112}
113
114void *AsanTSDGet() {
115 CHECK(tsd_key_inited);
116 return pthread_getspecific(tsd_key);
117}
118
119void AsanTSDSet(void *tsd) {
120 CHECK(tsd_key_inited);
121 pthread_setspecific(tsd_key, tsd);
122}
123
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000124} // namespace __asan
125
126#endif // __linux__ || __APPLE_