2014-09-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / libsanitizer / asan / asan_linux.cc
blob08d2885e54803b363f2c6677bf4960377681efc9
1 //===-- asan_linux.cc -----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Linux-specific details.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_FREEBSD || SANITIZER_LINUX
16 #include "asan_interceptors.h"
17 #include "asan_internal.h"
18 #include "asan_thread.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_libc.h"
21 #include "sanitizer_common/sanitizer_procmaps.h"
23 #include <sys/time.h>
24 #include <sys/resource.h>
25 #include <sys/mman.h>
26 #include <sys/syscall.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <pthread.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <unwind.h>
34 #if SANITIZER_FREEBSD
35 #include <sys/link_elf.h>
36 #endif
38 #if SANITIZER_ANDROID || SANITIZER_FREEBSD
39 #include <ucontext.h>
40 extern "C" void* _DYNAMIC;
41 #else
42 #include <sys/ucontext.h>
43 #include <dlfcn.h>
44 #include <link.h>
45 #endif
47 // x86_64 FreeBSD 9.2 and older define 64-bit register names in both 64-bit
48 // and 32-bit modes.
49 #if SANITIZER_FREEBSD
50 #include <sys/param.h>
51 # if __FreeBSD_version <= 902001 // v9.2
52 # define mc_eip mc_rip
53 # define mc_ebp mc_rbp
54 # define mc_esp mc_rsp
55 # endif
56 #endif
58 typedef enum {
59 ASAN_RT_VERSION_UNDEFINED = 0,
60 ASAN_RT_VERSION_DYNAMIC,
61 ASAN_RT_VERSION_STATIC,
62 } asan_rt_version_t;
64 // FIXME: perhaps also store abi version here?
65 extern "C" {
66 SANITIZER_INTERFACE_ATTRIBUTE
67 asan_rt_version_t __asan_rt_version;
70 namespace __asan {
72 void MaybeReexec() {
73 // No need to re-exec on Linux.
76 void *AsanDoesNotSupportStaticLinkage() {
77 // This will fail to link with -static.
78 return &_DYNAMIC; // defined in link.h
81 #if SANITIZER_ANDROID
82 // FIXME: should we do anything for Android?
83 void AsanCheckDynamicRTPrereqs() {}
84 void AsanCheckIncompatibleRT() {}
85 #else
86 static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
87 void *data) {
88 // Continue until the first dynamic library is found
89 if (!info->dlpi_name || info->dlpi_name[0] == 0)
90 return 0;
92 // Ignore vDSO
93 if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
94 return 0;
96 *(const char **)data = info->dlpi_name;
97 return 1;
100 static bool IsDynamicRTName(const char *libname) {
101 return internal_strstr(libname, "libclang_rt.asan") ||
102 internal_strstr(libname, "libasan.so");
105 static void ReportIncompatibleRT() {
106 Report("Your application is linked against incompatible ASan runtimes.\n");
107 Die();
110 void AsanCheckDynamicRTPrereqs() {
111 // Ensure that dynamic RT is the first DSO in the list
112 const char *first_dso_name = 0;
113 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
114 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
115 Report("ASan runtime does not come first in initial library list; "
116 "you should either link runtime to your application or "
117 "manually preload it with LD_PRELOAD.\n");
118 Die();
122 void AsanCheckIncompatibleRT() {
123 if (ASAN_DYNAMIC) {
124 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
125 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
126 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
127 ReportIncompatibleRT();
129 } else {
130 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
131 // Ensure that dynamic runtime is not present. We should detect it
132 // as early as possible, otherwise ASan interceptors could bind to
133 // the functions in dynamic ASan runtime instead of the functions in
134 // system libraries, causing crashes later in ASan initialization.
135 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
136 char filename[128];
137 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
138 if (IsDynamicRTName(filename)) {
139 Report("Your application is linked against "
140 "incompatible ASan runtimes.\n");
141 Die();
144 __asan_rt_version = ASAN_RT_VERSION_STATIC;
145 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
146 ReportIncompatibleRT();
150 #endif // SANITIZER_ANDROID
152 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
153 #if defined(__arm__)
154 ucontext_t *ucontext = (ucontext_t*)context;
155 *pc = ucontext->uc_mcontext.arm_pc;
156 *bp = ucontext->uc_mcontext.arm_fp;
157 *sp = ucontext->uc_mcontext.arm_sp;
158 #elif defined(__aarch64__)
159 ucontext_t *ucontext = (ucontext_t*)context;
160 *pc = ucontext->uc_mcontext.pc;
161 *bp = ucontext->uc_mcontext.regs[29];
162 *sp = ucontext->uc_mcontext.sp;
163 #elif defined(__hppa__)
164 ucontext_t *ucontext = (ucontext_t*)context;
165 *pc = ucontext->uc_mcontext.sc_iaoq[0];
166 /* GCC uses %r3 whenever a frame pointer is needed. */
167 *bp = ucontext->uc_mcontext.sc_gr[3];
168 *sp = ucontext->uc_mcontext.sc_gr[30];
169 #elif defined(__x86_64__)
170 # if SANITIZER_FREEBSD
171 ucontext_t *ucontext = (ucontext_t*)context;
172 *pc = ucontext->uc_mcontext.mc_rip;
173 *bp = ucontext->uc_mcontext.mc_rbp;
174 *sp = ucontext->uc_mcontext.mc_rsp;
175 # else
176 ucontext_t *ucontext = (ucontext_t*)context;
177 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
178 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
179 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
180 # endif
181 #elif defined(__i386__)
182 # if SANITIZER_FREEBSD
183 ucontext_t *ucontext = (ucontext_t*)context;
184 *pc = ucontext->uc_mcontext.mc_eip;
185 *bp = ucontext->uc_mcontext.mc_ebp;
186 *sp = ucontext->uc_mcontext.mc_esp;
187 # else
188 ucontext_t *ucontext = (ucontext_t*)context;
189 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
190 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
191 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
192 # endif
193 #elif defined(__powerpc__) || defined(__powerpc64__)
194 ucontext_t *ucontext = (ucontext_t*)context;
195 *pc = ucontext->uc_mcontext.regs->nip;
196 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
197 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
198 // pointer, but GCC always uses r31 when we need a frame pointer.
199 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
200 #elif defined(__sparc__)
201 ucontext_t *ucontext = (ucontext_t*)context;
202 uptr *stk_ptr;
203 # if defined (__arch64__)
204 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
205 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
206 stk_ptr = (uptr *) (*sp + 2047);
207 *bp = stk_ptr[15];
208 # else
209 *pc = ucontext->uc_mcontext.gregs[REG_PC];
210 *sp = ucontext->uc_mcontext.gregs[REG_O6];
211 stk_ptr = (uptr *) *sp;
212 *bp = stk_ptr[15];
213 # endif
214 #elif defined(__mips__)
215 ucontext_t *ucontext = (ucontext_t*)context;
216 *pc = ucontext->uc_mcontext.gregs[31];
217 *bp = ucontext->uc_mcontext.gregs[30];
218 *sp = ucontext->uc_mcontext.gregs[29];
219 #else
220 # error "Unsupported arch"
221 #endif
224 bool AsanInterceptsSignal(int signum) {
225 return signum == SIGSEGV && common_flags()->handle_segv;
228 void AsanPlatformThreadInit() {
229 // Nothing here for now.
232 #if !SANITIZER_ANDROID
233 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
234 ucontext_t *ucp = (ucontext_t*)context;
235 *stack = (uptr)ucp->uc_stack.ss_sp;
236 *ssize = ucp->uc_stack.ss_size;
238 #else
239 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
240 UNIMPLEMENTED();
242 #endif
244 } // namespace __asan
246 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX