blob: b2df5a98ec8880b7a08924a6620b96c36507d4f8 [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"
16#if 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"
Kostya Serebryany2b939c32012-08-28 13:25:55 +000021#include "asan_thread_registry.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
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000036#if !SANITIZER_ANDROID
Kostya Serebryany9107c262012-01-06 19:11:09 +000037// FIXME: where to get ucontext on Android?
38#include <sys/ucontext.h>
39#endif
40
Evgeniy Stepanovaa33a502012-03-26 09:48:41 +000041extern "C" void* _DYNAMIC;
42
Kostya Serebryany1e172b42011-11-30 01:07:02 +000043namespace __asan {
44
Alexander Potapenkoeb8c46e2012-08-24 09:22:05 +000045void MaybeReexec() {
46 // No need to re-exec on Linux.
47}
48
Kostya Serebryany1e172b42011-11-30 01:07:02 +000049void *AsanDoesNotSupportStaticLinkage() {
50 // This will fail to link with -static.
Kostya Serebryanyefb3fa32012-01-05 23:50:34 +000051 return &_DYNAMIC; // defined in link.h
Kostya Serebryany1e172b42011-11-30 01:07:02 +000052}
53
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000054void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000055#if SANITIZER_ANDROID
Kostya Serebryany9107c262012-01-06 19:11:09 +000056 *pc = *sp = *bp = 0;
57#elif defined(__arm__)
58 ucontext_t *ucontext = (ucontext_t*)context;
59 *pc = ucontext->uc_mcontext.arm_pc;
60 *bp = ucontext->uc_mcontext.arm_fp;
61 *sp = ucontext->uc_mcontext.arm_sp;
62# elif defined(__x86_64__)
63 ucontext_t *ucontext = (ucontext_t*)context;
64 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
65 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
66 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
67# elif defined(__i386__)
68 ucontext_t *ucontext = (ucontext_t*)context;
69 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
70 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
71 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Kostya Serebryanyd7d46502012-11-20 07:00:42 +000072# elif defined(__powerpc__) || defined(__powerpc64__)
73 ucontext_t *ucontext = (ucontext_t*)context;
74 *pc = ucontext->uc_mcontext.regs->nip;
75 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
76 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
77 // pointer, but GCC always uses r31 when we need a frame pointer.
78 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
Dmitry Vyukov16da7942012-11-16 11:26:05 +000079# elif defined(__sparc__)
80 ucontext_t *ucontext = (ucontext_t*)context;
81 uptr *stk_ptr;
82# if defined (__arch64__)
83 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
84 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
85 stk_ptr = (uptr *) (*sp + 2047);
86 *bp = stk_ptr[15];
87# else
88 *pc = ucontext->uc_mcontext.gregs[REG_PC];
89 *sp = ucontext->uc_mcontext.gregs[REG_O6];
90 stk_ptr = (uptr *) *sp;
91 *bp = stk_ptr[15];
92# endif
Kostya Serebryany9107c262012-01-06 19:11:09 +000093#else
94# error "Unsupported arch"
95#endif
96}
97
Kostya Serebryany4803ab92012-01-09 18:53:15 +000098bool AsanInterceptsSignal(int signum) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +000099 return signum == SIGSEGV && flags()->handle_segv;
Kostya Serebryany4803ab92012-01-09 18:53:15 +0000100}
101
Alexander Potapenko75b19eb2012-07-23 14:07:58 +0000102void AsanPlatformThreadInit() {
103 // Nothing here for now.
104}
105
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000106void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, bool fast) {
Kostya Serebryany68aad432012-12-03 18:39:21 +0000107#if defined(__arm__) || \
108 defined(__powerpc__) || defined(__powerpc64__) || \
109 defined(__sparc__)
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000110 fast = false;
111#endif
112 if (!fast)
Kostya Serebryany49d616e2012-12-13 12:31:55 +0000113 return stack->SlowUnwindStack(pc, max_s);
Kostya Serebryanya30c8f92012-12-13 09:34:23 +0000114 stack->size = 0;
115 stack->trace[0] = pc;
116 if (max_s > 1) {
117 stack->max_size = max_s;
Kostya Serebryany2b939c32012-08-28 13:25:55 +0000118 if (!asan_inited) return;
119 if (AsanThread *t = asanThreadRegistry().GetCurrent())
120 stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom());
Evgeniy Stepanov9cfa1942012-01-19 11:34:18 +0000121 }
122}
123
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +0000124#if !SANITIZER_ANDROID
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000125void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000126 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000127 *stack = (uptr)ucp->uc_stack.ss_sp;
128 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000129}
130#else
Alexey Samsonov57db4ba2013-01-17 15:45:28 +0000131void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovca2849c2013-01-18 09:20:06 +0000132 UNIMPLEMENTED();
Alexey Samsonovf3950c62012-11-23 10:14:44 +0000133}
134#endif
135
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136} // namespace __asan
Kostya Serebryanyd6567c52011-12-01 21:40:52 +0000137
138#endif // __linux__