blob: 8ad9de781a491dd05b86e9a95dd8452f18ddc67a [file] [log] [blame]
Kostya Serebryany019b76f2011-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 Stepanov0af67232013-03-19 14:33:38 +000014
15#include "sanitizer_common/sanitizer_platform.h"
Alexey Samsonov5ec35b72014-03-06 09:05:52 +000016#if SANITIZER_FREEBSD || SANITIZER_LINUX
Kostya Serebryany019b76f2011-11-30 01:07:02 +000017
Kostya Serebryanycd271f52012-01-05 00:44:33 +000018#include "asan_interceptors.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000019#include "asan_internal.h"
Kostya Serebryany78d87d32012-01-05 01:07:27 +000020#include "asan_thread.h"
Alexey Samsonov3d9adc02014-03-04 13:12:25 +000021#include "sanitizer_common/sanitizer_flags.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000022#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000023#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000024
Kostya Serebryany78d87d32012-01-05 01:07:27 +000025#include <sys/time.h>
26#include <sys/resource.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000027#include <sys/mman.h>
28#include <sys/syscall.h>
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000029#include <sys/types.h>
30#include <fcntl.h>
Kostya Serebryany78d87d32012-01-05 01:07:27 +000031#include <pthread.h>
Kostya Serebryanycd271f52012-01-05 00:44:33 +000032#include <stdio.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000033#include <unistd.h>
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000034#include <unwind.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000035
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000036#if SANITIZER_ANDROID
37#include <ucontext.h>
Alexey Samsonov56b6ee92014-04-01 13:16:30 +000038extern "C" void* _DYNAMIC;
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000039#else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000040#include <sys/ucontext.h>
Alexey Samsonov56b6ee92014-04-01 13:16:30 +000041#include <dlfcn.h>
42#include <link.h>
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000043#endif
44
Viktor Kutuzovebb00e12014-03-12 12:44:36 +000045// x86_64 FreeBSD 9.2 and older define 64-bit register names in both 64-bit
46// and 32-bit modes.
47#if SANITIZER_FREEBSD
48#include <sys/param.h>
49# if __FreeBSD_version <= 902001 // v9.2
50# define mc_eip mc_rip
51# define mc_ebp mc_rbp
52# define mc_esp mc_rsp
53# endif
54#endif
55
Alexey Samsonov56b6ee92014-04-01 13:16:30 +000056typedef enum {
57 ASAN_RT_VERSION_UNDEFINED = 0,
58 ASAN_RT_VERSION_DYNAMIC,
59 ASAN_RT_VERSION_STATIC,
60} asan_rt_version_t;
61
62// FIXME: perhaps also store abi version here?
63extern "C" {
64SANITIZER_INTERFACE_ATTRIBUTE
65asan_rt_version_t __asan_rt_version;
66}
Evgeniy Stepanov4cc26312012-03-26 09:48:41 +000067
Kostya Serebryany019b76f2011-11-30 01:07:02 +000068namespace __asan {
69
Alexander Potapenkofefc1e92012-08-24 09:22:05 +000070void MaybeReexec() {
71 // No need to re-exec on Linux.
72}
73
Kostya Serebryany019b76f2011-11-30 01:07:02 +000074void *AsanDoesNotSupportStaticLinkage() {
75 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000076 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000077}
78
Alexey Samsonov2c66a222014-04-02 09:36:36 +000079#if SANITIZER_ANDROID
80// FIXME: should we do anything for Android?
81void AsanCheckDynamicRTPrereqs() {}
82void AsanCheckIncompatibleRT() {}
83#else
Alexey Samsonov56b6ee92014-04-01 13:16:30 +000084static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
85 void *data) {
86 // Continue until the first dynamic library is found
87 if (!info->dlpi_name || info->dlpi_name[0] == 0)
88 return 0;
89
90 *(const char **)data = info->dlpi_name;
91 return 1;
92}
93
94static bool IsDynamicRTName(const char *libname) {
95 return internal_strstr(libname, "libclang_rt.asan") ||
96 internal_strstr(libname, "libasan.so");
97}
98
99void AsanCheckDynamicRTPrereqs() {
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000100 // Ensure that dynamic RT is the first DSO in the list
101 const char *first_dso_name = 0;
102 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
103 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
104 Report("ASan runtime does not come first in initial library list; "
105 "you should either link runtime to your application or "
106 "manually preload it with LD_PRELOAD.\n");
107 Die();
108 }
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000109}
110
111void AsanCheckIncompatibleRT() {
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000112 if (ASAN_DYNAMIC) {
113 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
114 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
115 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
116 Report("Your application is linked against "
117 "incompatible ASan runtimes.\n");
118 Die();
119 }
120 } else {
121 // Ensure that dynamic runtime is not present. We should detect it
122 // as early as possible, otherwise ASan interceptors could bind to
123 // the functions in dynamic ASan runtime instead of the functions in
124 // system libraries, causing crashes later in ASan initialization.
125 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
126 char filename[128];
127 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
128 if (IsDynamicRTName(filename)) {
129 Report("Your application is linked against "
130 "incompatible ASan runtimes.\n");
131 Die();
132 }
133 }
134
135 CHECK_NE(__asan_rt_version, ASAN_RT_VERSION_DYNAMIC);
136 __asan_rt_version = ASAN_RT_VERSION_STATIC;
137 }
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000138}
Alexey Samsonov2c66a222014-04-02 09:36:36 +0000139#endif // SANITIZER_ANDROID
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000140
Kostya Serebryany8d032042012-05-31 14:35:53 +0000141void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +0000142#if defined(__arm__)
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000143 ucontext_t *ucontext = (ucontext_t*)context;
144 *pc = ucontext->uc_mcontext.arm_pc;
145 *bp = ucontext->uc_mcontext.arm_fp;
146 *sp = ucontext->uc_mcontext.arm_sp;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000147#elif defined(__aarch64__)
Kostya Serebryanyc98ce282014-02-13 07:50:20 +0000148 ucontext_t *ucontext = (ucontext_t*)context;
149 *pc = ucontext->uc_mcontext.pc;
150 *bp = ucontext->uc_mcontext.regs[29];
151 *sp = ucontext->uc_mcontext.sp;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000152#elif defined(__hppa__)
Kostya Serebryanya92b07d2013-11-18 08:20:13 +0000153 ucontext_t *ucontext = (ucontext_t*)context;
154 *pc = ucontext->uc_mcontext.sc_iaoq[0];
155 /* GCC uses %r3 whenever a frame pointer is needed. */
156 *bp = ucontext->uc_mcontext.sc_gr[3];
157 *sp = ucontext->uc_mcontext.sc_gr[30];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000158#elif defined(__x86_64__)
159# if SANITIZER_FREEBSD
160 ucontext_t *ucontext = (ucontext_t*)context;
161 *pc = ucontext->uc_mcontext.mc_rip;
162 *bp = ucontext->uc_mcontext.mc_rbp;
163 *sp = ucontext->uc_mcontext.mc_rsp;
164# else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000165 ucontext_t *ucontext = (ucontext_t*)context;
166 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
167 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
168 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000169# endif
170#elif defined(__i386__)
171# if SANITIZER_FREEBSD
172 ucontext_t *ucontext = (ucontext_t*)context;
173 *pc = ucontext->uc_mcontext.mc_eip;
174 *bp = ucontext->uc_mcontext.mc_ebp;
175 *sp = ucontext->uc_mcontext.mc_esp;
176# else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000177 ucontext_t *ucontext = (ucontext_t*)context;
178 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
179 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
180 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000181# endif
182#elif defined(__sparc__)
Dmitry Vyukov4ee90c232012-11-16 11:26:05 +0000183 ucontext_t *ucontext = (ucontext_t*)context;
184 uptr *stk_ptr;
185# if defined (__arch64__)
186 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
187 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
188 stk_ptr = (uptr *) (*sp + 2047);
189 *bp = stk_ptr[15];
190# else
191 *pc = ucontext->uc_mcontext.gregs[REG_PC];
192 *sp = ucontext->uc_mcontext.gregs[REG_O6];
193 stk_ptr = (uptr *) *sp;
194 *bp = stk_ptr[15];
195# endif
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000196#elif defined(__mips__)
Kostya Serebryanyc1aa0e82013-06-03 14:49:25 +0000197 ucontext_t *ucontext = (ucontext_t*)context;
198 *pc = ucontext->uc_mcontext.gregs[31];
199 *bp = ucontext->uc_mcontext.gregs[30];
200 *sp = ucontext->uc_mcontext.gregs[29];
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000201#else
202# error "Unsupported arch"
203#endif
204}
205
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000206bool AsanInterceptsSignal(int signum) {
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000207 return signum == SIGSEGV && common_flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000208}
209
Alexander Potapenko51e64882012-07-23 14:07:58 +0000210void AsanPlatformThreadInit() {
211 // Nothing here for now.
212}
213
Evgeniy Stepanovd3b56602013-03-19 13:54:41 +0000214#if !SANITIZER_ANDROID
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000215void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000216 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000217 *stack = (uptr)ucp->uc_stack.ss_sp;
218 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000219}
220#else
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000221void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonov95856132013-01-18 09:20:06 +0000222 UNIMPLEMENTED();
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000223}
224#endif
225
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000226} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000227
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000228#endif // SANITIZER_FREEBSD || SANITIZER_LINUX