blob: 2bc9e4853090213a73e5dfb44b168b569522bf16 [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
27namespace __asan {
28
29static 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
40static 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 Serebryany0ecf5eb2012-01-09 23:11:26 +000043 if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000044 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
56void InstallSignalHandlers() {
57 MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
58 MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
59}
60
61void AsanDisableCoreDumper() {
62 struct rlimit nocore;
63 nocore.rlim_cur = 0;
64 nocore.rlim_max = 0;
65 setrlimit(RLIMIT_CORE, &nocore);
66}
67
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000068void AsanDie() {
69 _exit(FLAG_exitcode);
70}
71
72int GetPid() {
73 return getpid();
74}
75
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000076uintptr_t GetThreadSelf() {
77 return (uintptr_t)pthread_self();
78}
79
80// ---------------------- TSD ---------------- {{{1
81
82static pthread_key_t tsd_key;
83static bool tsd_key_inited = false;
84void AsanTSDInit() {
85 CHECK(!tsd_key_inited);
86 tsd_key_inited = true;
87 CHECK(0 == pthread_key_create(&tsd_key, 0));
88}
89
90void *AsanTSDGet() {
91 CHECK(tsd_key_inited);
92 return pthread_getspecific(tsd_key);
93}
94
95void AsanTSDSet(void *tsd) {
96 CHECK(tsd_key_inited);
97 pthread_setspecific(tsd_key, tsd);
98}
99
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000100} // namespace __asan
101
102#endif // __linux__ || __APPLE_