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_procmaps.h"
19 #include "asan_stack.h"
20 #include "asan_thread_registry.h"
21
22 #include <pthread.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 #include <sys/resource.h>
27 #include <unistd.h>
28
29 #ifdef ANDROID
30 #include <sys/atomics.h>
31 #endif
32
33 // Should not add dependency on libstdc++,
34 // since most of the stuff here is inlinable.
35 #include <algorithm>
36
37 static const size_t kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough.
38
39 namespace __asan {
40
MaybeInstallSigaction(int signum,void (* handler)(int,siginfo_t *,void *))41 static void MaybeInstallSigaction(int signum,
42 void (*handler)(int, siginfo_t *, void *)) {
43 if (!AsanInterceptsSignal(signum))
44 return;
45 struct sigaction sigact;
46 REAL(memset)(&sigact, 0, sizeof(sigact));
47 sigact.sa_sigaction = handler;
48 sigact.sa_flags = SA_SIGINFO;
49 if (FLAG_use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
50 CHECK(0 == REAL(sigaction)(signum, &sigact, 0));
51 }
52
ASAN_OnSIGSEGV(int,siginfo_t * siginfo,void * context)53 static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
54 uintptr_t addr = (uintptr_t)siginfo->si_addr;
55 // Write the first message using the bullet-proof write.
56 if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie();
57 uintptr_t pc, sp, bp;
58 GetPcSpBp(context, &pc, &sp, &bp);
59 Report("ERROR: AddressSanitizer crashed on unknown address %p"
60 " (pc %p sp %p bp %p T%d)\n",
61 addr, pc, sp, bp,
62 asanThreadRegistry().GetCurrentTidOrMinusOne());
63 Printf("AddressSanitizer can not provide additional info. ABORTING\n");
64 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
65 stack.PrintStack();
66 ShowStatsAndAbort();
67 }
68
SetAlternateSignalStack()69 void SetAlternateSignalStack() {
70 stack_t altstack, oldstack;
71 CHECK(0 == sigaltstack(NULL, &oldstack));
72 // If the alternate stack is already in place, do nothing.
73 if ((oldstack.ss_flags & SS_DISABLE) == 0) return;
74 // TODO(glider): the mapped stack should have the MAP_STACK flag in the
75 // future. It is not required by man 2 sigaltstack now (they're using
76 // malloc()).
77 void* base = AsanMmapSomewhereOrDie(kAltStackSize, __FUNCTION__);
78 altstack.ss_sp = base;
79 altstack.ss_flags = 0;
80 altstack.ss_size = kAltStackSize;
81 CHECK(0 == sigaltstack(&altstack, NULL));
82 if (FLAG_v > 0) {
83 Report("Alternative stack for T%d set: [%p,%p)\n",
84 asanThreadRegistry().GetCurrentTidOrMinusOne(),
85 altstack.ss_sp, (char*)altstack.ss_sp + altstack.ss_size);
86 }
87 }
88
UnsetAlternateSignalStack()89 void UnsetAlternateSignalStack() {
90 stack_t altstack, oldstack;
91 altstack.ss_sp = NULL;
92 altstack.ss_flags = SS_DISABLE;
93 altstack.ss_size = 0;
94 CHECK(0 == sigaltstack(&altstack, &oldstack));
95 AsanUnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
96 }
97
InstallSignalHandlers()98 void InstallSignalHandlers() {
99 // Set the alternate signal stack for the main thread.
100 // This will cause SetAlternateSignalStack to be called twice, but the stack
101 // will be actually set only once.
102 if (FLAG_use_sigaltstack) SetAlternateSignalStack();
103 MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
104 MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
105 }
106
AsanDisableCoreDumper()107 void AsanDisableCoreDumper() {
108 struct rlimit nocore;
109 nocore.rlim_cur = 0;
110 nocore.rlim_max = 0;
111 setrlimit(RLIMIT_CORE, &nocore);
112 }
113
AsanDumpProcessMap()114 void AsanDumpProcessMap() {
115 AsanProcMaps proc_maps;
116 uintptr_t start, end;
117 const intptr_t kBufSize = 4095;
118 char filename[kBufSize];
119 Report("Process memory map follows:\n");
120 while (proc_maps.Next(&start, &end, /* file_offset */NULL,
121 filename, kBufSize)) {
122 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
123 }
124 Report("End of process memory map.\n");
125 }
126
GetPid()127 int GetPid() {
128 return getpid();
129 }
130
GetThreadSelf()131 uintptr_t GetThreadSelf() {
132 return (uintptr_t)pthread_self();
133 }
134
SleepForSeconds(int seconds)135 void SleepForSeconds(int seconds) {
136 sleep(seconds);
137 }
138
Exit(int exitcode)139 void Exit(int exitcode) {
140 _exit(exitcode);
141 }
142
Abort()143 void Abort() {
144 abort();
145 }
146
Atexit(void (* function)(void))147 int Atexit(void (*function)(void)) {
148 return atexit(function);
149 }
150
AtomicInc(int * a)151 int AtomicInc(int *a) {
152 #ifdef ANDROID
153 return __atomic_inc(a) + 1;
154 #else
155 return __sync_add_and_fetch(a, 1);
156 #endif
157 }
158
AtomicExchange(uint16_t * a,uint16_t new_val)159 uint16_t AtomicExchange(uint16_t *a, uint16_t new_val) {
160 return __sync_lock_test_and_set(a, new_val);
161 }
162
SortArray(uintptr_t * array,size_t size)163 void SortArray(uintptr_t *array, size_t size) {
164 std::sort(array, array + size);
165 }
166
167 // ---------------------- TSD ---------------- {{{1
168
169 static pthread_key_t tsd_key;
170 static bool tsd_key_inited = false;
AsanTSDInit(void (* destructor)(void * tsd))171 void AsanTSDInit(void (*destructor)(void *tsd)) {
172 CHECK(!tsd_key_inited);
173 tsd_key_inited = true;
174 CHECK(0 == pthread_key_create(&tsd_key, destructor));
175 }
176
AsanTSDGet()177 void *AsanTSDGet() {
178 CHECK(tsd_key_inited);
179 return pthread_getspecific(tsd_key);
180 }
181
AsanTSDSet(void * tsd)182 void AsanTSDSet(void *tsd) {
183 CHECK(tsd_key_inited);
184 pthread_setspecific(tsd_key, tsd);
185 }
186
187 } // namespace __asan
188
189 #endif // __linux__ || __APPLE_
190