blob: 777cb43bfa264f16cfd512e502febf3e0c8a2d4d [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
Alexey Samsonov11ff0a22014-04-02 13:09:22 +000099static void ReportIncompatibleRT() {
100 Report("Your application is linked against incompatible ASan runtimes.\n");
101 Die();
102}
103
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000104void AsanCheckDynamicRTPrereqs() {
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000105 // Ensure that dynamic RT is the first DSO in the list
106 const char *first_dso_name = 0;
107 dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
108 if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
109 Report("ASan runtime does not come first in initial library list; "
110 "you should either link runtime to your application or "
111 "manually preload it with LD_PRELOAD.\n");
112 Die();
113 }
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000114}
115
116void AsanCheckIncompatibleRT() {
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000117 if (ASAN_DYNAMIC) {
118 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
119 __asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
120 } else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
Alexey Samsonov11ff0a22014-04-02 13:09:22 +0000121 ReportIncompatibleRT();
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000122 }
123 } else {
Alexey Samsonov11ff0a22014-04-02 13:09:22 +0000124 if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
125 // Ensure that dynamic runtime is not present. We should detect it
126 // as early as possible, otherwise ASan interceptors could bind to
127 // the functions in dynamic ASan runtime instead of the functions in
128 // system libraries, causing crashes later in ASan initialization.
129 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
130 char filename[128];
131 while (proc_maps.Next(0, 0, 0, filename, sizeof(filename), 0)) {
132 if (IsDynamicRTName(filename)) {
133 Report("Your application is linked against "
134 "incompatible ASan runtimes.\n");
135 Die();
136 }
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000137 }
Alexey Samsonov11ff0a22014-04-02 13:09:22 +0000138 __asan_rt_version = ASAN_RT_VERSION_STATIC;
139 } else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
140 ReportIncompatibleRT();
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000141 }
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000142 }
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000143}
Alexey Samsonov2c66a222014-04-02 09:36:36 +0000144#endif // SANITIZER_ANDROID
Alexey Samsonov56b6ee92014-04-01 13:16:30 +0000145
Kostya Serebryany8d032042012-05-31 14:35:53 +0000146void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +0000147#if defined(__arm__)
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000148 ucontext_t *ucontext = (ucontext_t*)context;
149 *pc = ucontext->uc_mcontext.arm_pc;
150 *bp = ucontext->uc_mcontext.arm_fp;
151 *sp = ucontext->uc_mcontext.arm_sp;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000152#elif defined(__aarch64__)
Kostya Serebryanyc98ce282014-02-13 07:50:20 +0000153 ucontext_t *ucontext = (ucontext_t*)context;
154 *pc = ucontext->uc_mcontext.pc;
155 *bp = ucontext->uc_mcontext.regs[29];
156 *sp = ucontext->uc_mcontext.sp;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000157#elif defined(__hppa__)
Kostya Serebryanya92b07d2013-11-18 08:20:13 +0000158 ucontext_t *ucontext = (ucontext_t*)context;
159 *pc = ucontext->uc_mcontext.sc_iaoq[0];
160 /* GCC uses %r3 whenever a frame pointer is needed. */
161 *bp = ucontext->uc_mcontext.sc_gr[3];
162 *sp = ucontext->uc_mcontext.sc_gr[30];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000163#elif defined(__x86_64__)
164# if SANITIZER_FREEBSD
165 ucontext_t *ucontext = (ucontext_t*)context;
166 *pc = ucontext->uc_mcontext.mc_rip;
167 *bp = ucontext->uc_mcontext.mc_rbp;
168 *sp = ucontext->uc_mcontext.mc_rsp;
169# else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000170 ucontext_t *ucontext = (ucontext_t*)context;
171 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
172 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
173 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000174# endif
175#elif defined(__i386__)
176# if SANITIZER_FREEBSD
177 ucontext_t *ucontext = (ucontext_t*)context;
178 *pc = ucontext->uc_mcontext.mc_eip;
179 *bp = ucontext->uc_mcontext.mc_ebp;
180 *sp = ucontext->uc_mcontext.mc_esp;
181# else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000182 ucontext_t *ucontext = (ucontext_t*)context;
183 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
184 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
185 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000186# endif
187#elif defined(__sparc__)
Dmitry Vyukov4ee90c232012-11-16 11:26:05 +0000188 ucontext_t *ucontext = (ucontext_t*)context;
189 uptr *stk_ptr;
190# if defined (__arch64__)
191 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
192 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
193 stk_ptr = (uptr *) (*sp + 2047);
194 *bp = stk_ptr[15];
195# else
196 *pc = ucontext->uc_mcontext.gregs[REG_PC];
197 *sp = ucontext->uc_mcontext.gregs[REG_O6];
198 stk_ptr = (uptr *) *sp;
199 *bp = stk_ptr[15];
200# endif
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000201#elif defined(__mips__)
Kostya Serebryanyc1aa0e82013-06-03 14:49:25 +0000202 ucontext_t *ucontext = (ucontext_t*)context;
203 *pc = ucontext->uc_mcontext.gregs[31];
204 *bp = ucontext->uc_mcontext.gregs[30];
205 *sp = ucontext->uc_mcontext.gregs[29];
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000206#else
207# error "Unsupported arch"
208#endif
209}
210
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000211bool AsanInterceptsSignal(int signum) {
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000212 return signum == SIGSEGV && common_flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000213}
214
Alexander Potapenko51e64882012-07-23 14:07:58 +0000215void AsanPlatformThreadInit() {
216 // Nothing here for now.
217}
218
Evgeniy Stepanovd3b56602013-03-19 13:54:41 +0000219#if !SANITIZER_ANDROID
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000220void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000221 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000222 *stack = (uptr)ucp->uc_stack.ss_sp;
223 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000224}
225#else
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000226void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonov95856132013-01-18 09:20:06 +0000227 UNIMPLEMENTED();
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000228}
229#endif
230
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000231} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000232
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000233#endif // SANITIZER_FREEBSD || SANITIZER_LINUX