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