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