asan_linux.cc revision 24861607cb3c606ad46e724456eb851558c85330
1//===-- 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//===----------------------------------------------------------------------===//
14
15#include "sanitizer_common/sanitizer_platform.h"
16#if SANITIZER_LINUX
17
18#include "asan_interceptors.h"
19#include "asan_internal.h"
20#include "asan_thread.h"
21#include "sanitizer_common/sanitizer_libc.h"
22#include "sanitizer_common/sanitizer_procmaps.h"
23
24#include <sys/time.h>
25#include <sys/resource.h>
26#include <sys/mman.h>
27#include <sys/syscall.h>
28#include <sys/types.h>
29#include <fcntl.h>
30#include <pthread.h>
31#include <stdio.h>
32#include <unistd.h>
33#include <unwind.h>
34
35#if !SANITIZER_ANDROID
36// FIXME: where to get ucontext on Android?
37#include <sys/ucontext.h>
38#endif
39
40extern "C" void* _DYNAMIC;
41
42namespace __asan {
43
44void MaybeReexec() {
45  // No need to re-exec on Linux.
46}
47
48void *AsanDoesNotSupportStaticLinkage() {
49  // This will fail to link with -static.
50  return &_DYNAMIC;  // defined in link.h
51}
52
53void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
54#if SANITIZER_ANDROID
55  *pc = *sp = *bp = 0;
56#elif defined(__arm__)
57  ucontext_t *ucontext = (ucontext_t*)context;
58  *pc = ucontext->uc_mcontext.arm_pc;
59  *bp = ucontext->uc_mcontext.arm_fp;
60  *sp = ucontext->uc_mcontext.arm_sp;
61# elif defined(__hppa__)
62  ucontext_t *ucontext = (ucontext_t*)context;
63  *pc = ucontext->uc_mcontext.sc_iaoq[0];
64  /* GCC uses %r3 whenever a frame pointer is needed.  */
65  *bp = ucontext->uc_mcontext.sc_gr[3];
66  *sp = ucontext->uc_mcontext.sc_gr[30];
67# elif defined(__x86_64__)
68  ucontext_t *ucontext = (ucontext_t*)context;
69  *pc = ucontext->uc_mcontext.gregs[REG_RIP];
70  *bp = ucontext->uc_mcontext.gregs[REG_RBP];
71  *sp = ucontext->uc_mcontext.gregs[REG_RSP];
72# elif defined(__i386__)
73  ucontext_t *ucontext = (ucontext_t*)context;
74  *pc = ucontext->uc_mcontext.gregs[REG_EIP];
75  *bp = ucontext->uc_mcontext.gregs[REG_EBP];
76  *sp = ucontext->uc_mcontext.gregs[REG_ESP];
77# elif defined(__sparc__)
78  ucontext_t *ucontext = (ucontext_t*)context;
79  uptr *stk_ptr;
80# if defined (__arch64__)
81  *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
82  *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
83  stk_ptr = (uptr *) (*sp + 2047);
84  *bp = stk_ptr[15];
85# else
86  *pc = ucontext->uc_mcontext.gregs[REG_PC];
87  *sp = ucontext->uc_mcontext.gregs[REG_O6];
88  stk_ptr = (uptr *) *sp;
89  *bp = stk_ptr[15];
90# endif
91# elif defined(__mips__)
92  ucontext_t *ucontext = (ucontext_t*)context;
93  *pc = ucontext->uc_mcontext.gregs[31];
94  *bp = ucontext->uc_mcontext.gregs[30];
95  *sp = ucontext->uc_mcontext.gregs[29];
96#else
97# error "Unsupported arch"
98#endif
99}
100
101bool AsanInterceptsSignal(int signum) {
102  return signum == SIGSEGV && flags()->handle_segv;
103}
104
105void AsanPlatformThreadInit() {
106  // Nothing here for now.
107}
108
109#if !SANITIZER_ANDROID
110void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
111  ucontext_t *ucp = (ucontext_t*)context;
112  *stack = (uptr)ucp->uc_stack.ss_sp;
113  *ssize = ucp->uc_stack.ss_size;
114}
115#else
116void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
117  UNIMPLEMENTED();
118}
119#endif
120
121}  // namespace __asan
122
123#endif  // SANITIZER_LINUX
124