[FreeBSD] Provide FreeBSD-specific things for ASan.
[blocksruntime.git] / lib / asan / asan_linux.cc
blob3482e5de7f7054ae497042426ac897498114b51d
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.
12 // Linux-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX
18 #include "asan_interceptors.h"
19 #include "asan_internal.h"
20 #include "asan_thread.h"
21 #include "sanitizer_common/sanitizer_flags.h"
22 #include "sanitizer_common/sanitizer_libc.h"
23 #include "sanitizer_common/sanitizer_procmaps.h"
25 #include <sys/time.h>
26 #include <sys/resource.h>
27 #include <sys/mman.h>
28 #include <sys/syscall.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <pthread.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <unwind.h>
36 #if SANITIZER_ANDROID
37 #include <ucontext.h>
38 #else
39 #include <sys/ucontext.h>
40 #endif
42 extern "C" void* _DYNAMIC;
44 namespace __asan {
46 void MaybeReexec() {
47 // No need to re-exec on Linux.
50 void *AsanDoesNotSupportStaticLinkage() {
51 // This will fail to link with -static.
52 return &_DYNAMIC; // defined in link.h
55 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
56 #if 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(__aarch64__)
62 ucontext_t *ucontext = (ucontext_t*)context;
63 *pc = ucontext->uc_mcontext.pc;
64 *bp = ucontext->uc_mcontext.regs[29];
65 *sp = ucontext->uc_mcontext.sp;
66 #elif defined(__hppa__)
67 ucontext_t *ucontext = (ucontext_t*)context;
68 *pc = ucontext->uc_mcontext.sc_iaoq[0];
69 /* GCC uses %r3 whenever a frame pointer is needed. */
70 *bp = ucontext->uc_mcontext.sc_gr[3];
71 *sp = ucontext->uc_mcontext.sc_gr[30];
72 #elif defined(__x86_64__)
73 # if SANITIZER_FREEBSD
74 ucontext_t *ucontext = (ucontext_t*)context;
75 *pc = ucontext->uc_mcontext.mc_rip;
76 *bp = ucontext->uc_mcontext.mc_rbp;
77 *sp = ucontext->uc_mcontext.mc_rsp;
78 # else
79 ucontext_t *ucontext = (ucontext_t*)context;
80 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
81 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
82 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
83 # endif
84 #elif defined(__i386__)
85 # if SANITIZER_FREEBSD
86 ucontext_t *ucontext = (ucontext_t*)context;
87 *pc = ucontext->uc_mcontext.mc_eip;
88 *bp = ucontext->uc_mcontext.mc_ebp;
89 *sp = ucontext->uc_mcontext.mc_esp;
90 # else
91 ucontext_t *ucontext = (ucontext_t*)context;
92 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
93 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
94 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
95 # endif
96 #elif defined(__sparc__)
97 ucontext_t *ucontext = (ucontext_t*)context;
98 uptr *stk_ptr;
99 # if defined (__arch64__)
100 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
101 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
102 stk_ptr = (uptr *) (*sp + 2047);
103 *bp = stk_ptr[15];
104 # else
105 *pc = ucontext->uc_mcontext.gregs[REG_PC];
106 *sp = ucontext->uc_mcontext.gregs[REG_O6];
107 stk_ptr = (uptr *) *sp;
108 *bp = stk_ptr[15];
109 # endif
110 #elif defined(__mips__)
111 ucontext_t *ucontext = (ucontext_t*)context;
112 *pc = ucontext->uc_mcontext.gregs[31];
113 *bp = ucontext->uc_mcontext.gregs[30];
114 *sp = ucontext->uc_mcontext.gregs[29];
115 #else
116 # error "Unsupported arch"
117 #endif
120 bool AsanInterceptsSignal(int signum) {
121 return signum == SIGSEGV && common_flags()->handle_segv;
124 void AsanPlatformThreadInit() {
125 // Nothing here for now.
128 #if !SANITIZER_ANDROID
129 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
130 ucontext_t *ucp = (ucontext_t*)context;
131 *stack = (uptr)ucp->uc_stack.ss_sp;
132 *ssize = ucp->uc_stack.ss_size;
134 #else
135 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
136 UNIMPLEMENTED();
138 #endif
140 } // namespace __asan
142 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX