1 //===-- asan_linux.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
26 #include <sys/resource.h>
28 #include <sys/syscall.h>
29 #include <sys/types.h>
39 #include <sys/ucontext.h>
42 extern "C" void* _DYNAMIC
;
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
) {
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
;
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
];
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
;
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
];
96 #elif defined(__sparc__)
97 ucontext_t
*ucontext
= (ucontext_t
*)context
;
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);
105 *pc
= ucontext
->uc_mcontext
.gregs
[REG_PC
];
106 *sp
= ucontext
->uc_mcontext
.gregs
[REG_O6
];
107 stk_ptr
= (uptr
*) *sp
;
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];
116 # error "Unsupported arch"
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
;
135 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
140 } // namespace __asan
142 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX