blob: 78ec98d671b21b72b654a77aea5c79545e3e1443 [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"
18#include "asan_stack.h"
19#include "asan_thread_registry.h"
20
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000021#include <pthread.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000022#include <signal.h>
23#include <sys/time.h>
24#include <sys/resource.h>
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000025#include <unistd.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000026
Kostya Serebryanydde7c332012-01-11 02:39:16 +000027#ifdef ANDROID
28#include <sys/atomics.h>
29#endif
30
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000031namespace __asan {
32
33static void MaybeInstallSigaction(int signum,
34 void (*handler)(int, siginfo_t *, void *)) {
35 if (!AsanInterceptsSignal(signum))
36 return;
37 struct sigaction sigact;
Alexey Samsonov09672ca2012-02-08 13:45:31 +000038 REAL(memset)(&sigact, 0, sizeof(sigact));
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000039 sigact.sa_sigaction = handler;
40 sigact.sa_flags = SA_SIGINFO;
Alexey Samsonov09672ca2012-02-08 13:45:31 +000041 CHECK(0 == REAL(sigaction)(signum, &sigact, 0));
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000042}
43
44static 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 Serebryany0ecf5eb2012-01-09 23:11:26 +000047 if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000048 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 Stepanov9cfa1942012-01-19 11:34:18 +000055 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000056 stack.PrintStack();
57 ShowStatsAndAbort();
58}
59
60void InstallSignalHandlers() {
61 MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
62 MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
63}
64
65void AsanDisableCoreDumper() {
66 struct rlimit nocore;
67 nocore.rlim_cur = 0;
68 nocore.rlim_max = 0;
69 setrlimit(RLIMIT_CORE, &nocore);
70}
71
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000072void AsanDie() {
Kostya Serebryanycb00d132012-01-31 00:52:18 +000073 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 Serebryany0ecf5eb2012-01-09 23:11:26 +000077 _exit(FLAG_exitcode);
78}
79
80int GetPid() {
81 return getpid();
82}
83
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000084uintptr_t GetThreadSelf() {
85 return (uintptr_t)pthread_self();
86}
87
Kostya Serebryanydde7c332012-01-11 02:39:16 +000088int 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 Serebryanycc4e6862012-01-11 02:21:06 +000096// ---------------------- TSD ---------------- {{{1
97
98static pthread_key_t tsd_key;
99static bool tsd_key_inited = false;
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000100void AsanTSDInit(void (*destructor)(void *tsd)) {
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000101 CHECK(!tsd_key_inited);
102 tsd_key_inited = true;
Kostya Serebryanyf58f9982012-02-07 00:27:15 +0000103 CHECK(0 == pthread_key_create(&tsd_key, destructor));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +0000104}
105
106void *AsanTSDGet() {
107 CHECK(tsd_key_inited);
108 return pthread_getspecific(tsd_key);
109}
110
111void AsanTSDSet(void *tsd) {
112 CHECK(tsd_key_inited);
113 pthread_setspecific(tsd_key, tsd);
114}
115
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000116} // namespace __asan
117
118#endif // __linux__ || __APPLE_