blob: fce9d1cd56ad158c2851cc52777b4ba5a613c76d [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +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// Linux-specific details.
13//===----------------------------------------------------------------------===//
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000014
15#include "sanitizer_common/sanitizer_platform.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016#if SANITIZER_FREEBSD || SANITIZER_LINUX
Kostya Serebryany1e172b42011-11-30 01:07:02 +000017
Kostya Serebryanydf499b42012-01-05 00:44:33 +000018#include "asan_interceptors.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000019#include "asan_internal.h"
Kostya Serebryanyc549dd72012-01-05 01:07:27 +000020#include "asan_thread.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070021#include "sanitizer_common/sanitizer_flags.h"
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000022#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000023#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000024
Kostya Serebryanyc549dd72012-01-05 01:07:27 +000025#include <sys/time.h>
26#include <sys/resource.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000027#include <sys/mman.h>
28#include <sys/syscall.h>
Kostya Serebryanyde496f42011-12-28 22:58:01 +000029#include <sys/types.h>
30#include <fcntl.h>
Kostya Serebryanyc549dd72012-01-05 01:07:27 +000031#include <pthread.h>
Kostya Serebryanydf499b42012-01-05 00:44:33 +000032#include <stdio.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000033#include <unistd.h>
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +000034#include <unwind.h>
Kostya Serebryany1e172b42011-11-30 01:07:02 +000035
Stephen Hines2d1fdb22014-05-28 23:58:16 -070036#if SANITIZER_FREEBSD
37#include <sys/link_elf.h>
Kostya Serebryany9107c262012-01-06 19:11:09 +000038#endif
39
Stephen Hines2d1fdb22014-05-28 23:58:16 -070040#if SANITIZER_ANDROID || SANITIZER_FREEBSD
41#include <ucontext.h>
Evgeniy Stepanovaa33a502012-03-26 09:48:41 +000042extern "C" void* _DYNAMIC;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070043#else
44#include <sys/ucontext.h>
45#include <dlfcn.h>
46#include <link.h>
47#endif
48
49// x86_64 FreeBSD 9.2 and older define 64-bit register names in both 64-bit
50// and 32-bit modes.
51#if SANITIZER_FREEBSD
52#include <sys/param.h>
53# if __FreeBSD_version <= 902001 // v9.2
54# define mc_eip mc_rip
55# define mc_ebp mc_rbp
56# define mc_esp mc_rsp
57# endif
58#endif
59
60typedef enum {
61 ASAN_RT_VERSION_UNDEFINED = 0,
62 ASAN_RT_VERSION_DYNAMIC,
63 ASAN_RT_VERSION_STATIC,
64} asan_rt_version_t;
65
66// FIXME: perhaps also store abi version here?
67extern "C" {
68SANITIZER_INTERFACE_ATTRIBUTE
69asan_rt_version_t __asan_rt_version;
70}
Evgeniy Stepanovaa33a502012-03-26 09:48:41 +000071
Kostya Serebryany1e172b42011-11-30 01:07:02 +000072namespace __asan {
73
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +000074void MaybeReexec() {
75 // No need to re-exec on Linux.
76}
77
Kostya Serebryany1e172b42011-11-30 01:07:02 +000078void *AsanDoesNotSupportStaticLinkage() {
79 // This will fail to link with -static.
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000080 return &_DYNAMIC; // defined in link.h
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081}
82
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000083#if SANITIZER_ANDROID
Stephen Hines2d1fdb22014-05-28 23:58:16 -070084// FIXME: should we do anything for Android?
85void AsanCheckDynamicRTPrereqs() {}
86void AsanCheckIncompatibleRT() {}
87#else
88static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
89 void *data) {
90 // Continue until the first dynamic library is found
91 if (!info->dlpi_name || info->dlpi_name[0] == 0)
92 return 0;
93
94 *(const char **)data = info->dlpi_name;
95 return 1;
96}
97
98static bool IsDynamicRTName(const char *libname) {
99 return internal_strstr(libname, "libclang_rt.asan") ||
100 internal_strstr(libname, "libasan.so");
101}
102
103static void ReportIncompatibleRT() {
104 Report("Your application is linked against incompatible ASan runtimes.\n");
105 Die();
106}
107
108void AsanCheckDynamicRTPrereqs() {
109 // Ensure that dynamic RT is the first DSO in the list
110 const char *first_dso_name = 0;
111 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
112 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
113 Report("ASan runtime does not come first in initial library list; "
114 "you should either link runtime to your application or "
115 "manually preload it with LD_PRELOAD.\n");
116 Die();
117 }
118}
119
120void AsanCheckIncompatibleRT() {
121 if (ASAN_DYNAMIC) {
122 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
123 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
124 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
125 ReportIncompatibleRT();
126 }
127 } else {
128 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
129 // Ensure that dynamic runtime is not present. We should detect it
130 // as early as possible, otherwise ASan interceptors could bind to
131 // the functions in dynamic ASan runtime instead of the functions in
132 // system libraries, causing crashes later in ASan initialization.
133 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
134 char filename[128];
135 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
136 if (IsDynamicRTName(filename)) {
137 Report("Your application is linked against "
138 "incompatible ASan runtimes.\n");
139 Die();
140 }
141 }
142 __asan_rt_version = ASAN_RT_VERSION_STATIC;
143 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
144 ReportIncompatibleRT();
145 }
146 }
147}
148#endif // SANITIZER_ANDROID
149
150void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
151#if defined(__arm__)
Kostya Serebryany9107c262012-01-06 19:11:09 +0000152 ucontext_t *ucontext = (ucontext_t*)context;
153 *pc = ucontext->uc_mcontext.arm_pc;
154 *bp = ucontext->uc_mcontext.arm_fp;
155 *sp = ucontext->uc_mcontext.arm_sp;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700156#elif defined(__aarch64__)
157 ucontext_t *ucontext = (ucontext_t*)context;
158 *pc = ucontext->uc_mcontext.pc;
159 *bp = ucontext->uc_mcontext.regs[29];
160 *sp = ucontext->uc_mcontext.sp;
161#elif defined(__hppa__)
Kostya Serebryany8b0c8a82013-11-18 08:20:13 +0000162 ucontext_t *ucontext = (ucontext_t*)context;
163 *pc = ucontext->uc_mcontext.sc_iaoq[0];
164 /* GCC uses %r3 whenever a frame pointer is needed. */
165 *bp = ucontext->uc_mcontext.sc_gr[3];
166 *sp = ucontext->uc_mcontext.sc_gr[30];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700167#elif defined(__x86_64__)
168# if SANITIZER_FREEBSD
169 ucontext_t *ucontext = (ucontext_t*)context;
170 *pc = ucontext->uc_mcontext.mc_rip;
171 *bp = ucontext->uc_mcontext.mc_rbp;
172 *sp = ucontext->uc_mcontext.mc_rsp;
173# else
Kostya Serebryany9107c262012-01-06 19:11:09 +0000174 ucontext_t *ucontext = (ucontext_t*)context;
175 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
176 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
177 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700178# endif
179#elif defined(__i386__)
180# if SANITIZER_FREEBSD
181 ucontext_t *ucontext = (ucontext_t*)context;
182 *pc = ucontext->uc_mcontext.mc_eip;
183 *bp = ucontext->uc_mcontext.mc_ebp;
184 *sp = ucontext->uc_mcontext.mc_esp;
185# else
Kostya Serebryany9107c262012-01-06 19:11:09 +0000186 ucontext_t *ucontext = (ucontext_t*)context;
187 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
188 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
189 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700190# endif
191#elif defined(__sparc__)
Dmitry Vyukov16da7942012-11-16 11:26:05 +0000192 ucontext_t *ucontext = (ucontext_t*)context;
193 uptr *stk_ptr;
194# if defined (__arch64__)
195 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
196 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
197 stk_ptr = (uptr *) (*sp + 2047);
198 *bp = stk_ptr[15];
199# else
200 *pc = ucontext->uc_mcontext.gregs[REG_PC];
201 *sp = ucontext->uc_mcontext.gregs[REG_O6];
202 stk_ptr = (uptr *) *sp;
203 *bp = stk_ptr[15];
204# endif
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700205#elif defined(__mips__)
Kostya Serebryany40527a52013-06-03 14:49:25 +0000206 ucontext_t *ucontext = (ucontext_t*)context;
207 *pc = ucontext->uc_mcontext.gregs[31];
208 *bp = ucontext->uc_mcontext.gregs[30];
209 *sp = ucontext->uc_mcontext.gregs[29];
Kostya Serebryany9107c262012-01-06 19:11:09 +0000210#else
211# error "Unsupported arch"
212#endif
213}
214
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000215bool AsanInterceptsSignal(int signum) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700216 return signum == SIGSEGV && common_flags()->handle_segv;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000217}
218
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000219void AsanPlatformThreadInit() {
220 // Nothing here for now.
221}
222
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +0000223#if !SANITIZER_ANDROID
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000224void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000225 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000226 *stack = (uptr)ucp->uc_stack.ss_sp;
227 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000228}
229#else
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000230void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovca2849c2013-01-18 09:20:06 +0000231 UNIMPLEMENTED();
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000232}
233#endif
234
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000235} // namespace __asan
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000236
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700237#endif // SANITIZER_FREEBSD || SANITIZER_LINUX