blob: c6085aa7b05647d1a4a5bd3a89d3b45ca3d85535 [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 Samsonov56b6ee92014-04-01 13:16:30 +000079static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
80 void *data) {
81 // Continue until the first dynamic library is found
82 if (!info->dlpi_name || info->dlpi_name[0] == 0)
83 return 0;
84
85 *(const char **)data = info->dlpi_name;
86 return 1;
87}
88
89static bool IsDynamicRTName(const char *libname) {
90 return internal_strstr(libname, "libclang_rt.asan") ||
91 internal_strstr(libname, "libasan.so");
92}
93
94void AsanCheckDynamicRTPrereqs() {
95 // FIXME: can we do something like this for Android?
96#if !SANITIZER_ANDROID
97 // Ensure that dynamic RT is the first DSO in the list
98 const char *first_dso_name = 0;
99 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
100 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
101 Report("ASan runtime does not come first in initial library list; "
102 "you should either link runtime to your application or "
103 "manually preload it with LD_PRELOAD.\n");
104 Die();
105 }
106#endif
107}
108
109void AsanCheckIncompatibleRT() {
110#if !SANITIZER_ANDROID
111 if (ASAN_DYNAMIC) {
112 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
113 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
114 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
115 Report("Your application is linked against "
116 "incompatible ASan runtimes.\n");
117 Die();
118 }
119 } else {
120 // Ensure that dynamic runtime is not present. We should detect it
121 // as early as possible, otherwise ASan interceptors could bind to
122 // the functions in dynamic ASan runtime instead of the functions in
123 // system libraries, causing crashes later in ASan initialization.
124 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
125 char filename[128];
126 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
127 if (IsDynamicRTName(filename)) {
128 Report("Your application is linked against "
129 "incompatible ASan runtimes.\n");
130 Die();
131 }
132 }
133
134 CHECK_NE(__asan_rt_version, ASAN_RT_VERSION_DYNAMIC);
135 __asan_rt_version = ASAN_RT_VERSION_STATIC;
136 }
137#endif
138}
139
Kostya Serebryany8d032042012-05-31 14:35:53 +0000140void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +0000141#if defined(__arm__)
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000142 ucontext_t *ucontext = (ucontext_t*)context;
143 *pc = ucontext->uc_mcontext.arm_pc;
144 *bp = ucontext->uc_mcontext.arm_fp;
145 *sp = ucontext->uc_mcontext.arm_sp;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000146#elif defined(__aarch64__)
Kostya Serebryanyc98ce282014-02-13 07:50:20 +0000147 ucontext_t *ucontext = (ucontext_t*)context;
148 *pc = ucontext->uc_mcontext.pc;
149 *bp = ucontext->uc_mcontext.regs[29];
150 *sp = ucontext->uc_mcontext.sp;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000151#elif defined(__hppa__)
Kostya Serebryanya92b07d2013-11-18 08:20:13 +0000152 ucontext_t *ucontext = (ucontext_t*)context;
153 *pc = ucontext->uc_mcontext.sc_iaoq[0];
154 /* GCC uses %r3 whenever a frame pointer is needed. */
155 *bp = ucontext->uc_mcontext.sc_gr[3];
156 *sp = ucontext->uc_mcontext.sc_gr[30];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000157#elif defined(__x86_64__)
158# if SANITIZER_FREEBSD
159 ucontext_t *ucontext = (ucontext_t*)context;
160 *pc = ucontext->uc_mcontext.mc_rip;
161 *bp = ucontext->uc_mcontext.mc_rbp;
162 *sp = ucontext->uc_mcontext.mc_rsp;
163# else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000164 ucontext_t *ucontext = (ucontext_t*)context;
165 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
166 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
167 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000168# endif
169#elif defined(__i386__)
170# if SANITIZER_FREEBSD
171 ucontext_t *ucontext = (ucontext_t*)context;
172 *pc = ucontext->uc_mcontext.mc_eip;
173 *bp = ucontext->uc_mcontext.mc_ebp;
174 *sp = ucontext->uc_mcontext.mc_esp;
175# else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000176 ucontext_t *ucontext = (ucontext_t*)context;
177 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
178 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
179 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000180# endif
181#elif defined(__sparc__)
Dmitry Vyukov4ee90c232012-11-16 11:26:05 +0000182 ucontext_t *ucontext = (ucontext_t*)context;
183 uptr *stk_ptr;
184# if defined (__arch64__)
185 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
186 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
187 stk_ptr = (uptr *) (*sp + 2047);
188 *bp = stk_ptr[15];
189# else
190 *pc = ucontext->uc_mcontext.gregs[REG_PC];
191 *sp = ucontext->uc_mcontext.gregs[REG_O6];
192 stk_ptr = (uptr *) *sp;
193 *bp = stk_ptr[15];
194# endif
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000195#elif defined(__mips__)
Kostya Serebryanyc1aa0e82013-06-03 14:49:25 +0000196 ucontext_t *ucontext = (ucontext_t*)context;
197 *pc = ucontext->uc_mcontext.gregs[31];
198 *bp = ucontext->uc_mcontext.gregs[30];
199 *sp = ucontext->uc_mcontext.gregs[29];
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000200#else
201# error "Unsupported arch"
202#endif
203}
204
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000205bool AsanInterceptsSignal(int signum) {
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000206 return signum == SIGSEGV && common_flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000207}
208
Alexander Potapenko51e64882012-07-23 14:07:58 +0000209void AsanPlatformThreadInit() {
210 // Nothing here for now.
211}
212
Evgeniy Stepanovd3b56602013-03-19 13:54:41 +0000213#if !SANITIZER_ANDROID
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000214void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000215 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000216 *stack = (uptr)ucp->uc_stack.ss_sp;
217 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000218}
219#else
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000220void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonov95856132013-01-18 09:20:06 +0000221 UNIMPLEMENTED();
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000222}
223#endif
224
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000225} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000226
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000227#endif // SANITIZER_FREEBSD || SANITIZER_LINUX