blob: 3482e5de7f7054ae497042426ac897498114b51d [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>
38#else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000039#include <sys/ucontext.h>
40#endif
41
Evgeniy Stepanov4cc26312012-03-26 09:48:41 +000042extern "C" void* _DYNAMIC;
43
Kostya Serebryany019b76f2011-11-30 01:07:02 +000044namespace __asan {
45
Alexander Potapenkofefc1e92012-08-24 09:22:05 +000046void MaybeReexec() {
47 // No need to re-exec on Linux.
48}
49
Kostya Serebryany019b76f2011-11-30 01:07:02 +000050void *AsanDoesNotSupportStaticLinkage() {
51 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000052 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000053}
54
Kostya Serebryany8d032042012-05-31 14:35:53 +000055void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000056#if defined(__arm__)
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000057 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;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +000061#elif defined(__aarch64__)
Kostya Serebryanyc98ce282014-02-13 07:50:20 +000062 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;
Alexey Samsonov5ec35b72014-03-06 09:05:52 +000066#elif defined(__hppa__)
Kostya Serebryanya92b07d2013-11-18 08:20:13 +000067 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];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +000072#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
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000079 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];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +000083# 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
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000091 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];
Alexey Samsonov5ec35b72014-03-06 09:05:52 +000095# endif
96#elif defined(__sparc__)
Dmitry Vyukov4ee90c232012-11-16 11:26:05 +000097 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
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000110#elif defined(__mips__)
Kostya Serebryanyc1aa0e82013-06-03 14:49:25 +0000111 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];
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000115#else
116# error "Unsupported arch"
117#endif
118}
119
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000120bool AsanInterceptsSignal(int signum) {
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000121 return signum == SIGSEGV && common_flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000122}
123
Alexander Potapenko51e64882012-07-23 14:07:58 +0000124void AsanPlatformThreadInit() {
125 // Nothing here for now.
126}
127
Evgeniy Stepanovd3b56602013-03-19 13:54:41 +0000128#if !SANITIZER_ANDROID
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000129void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000130 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000131 *stack = (uptr)ucp->uc_stack.ss_sp;
132 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000133}
134#else
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000135void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonov95856132013-01-18 09:20:06 +0000136 UNIMPLEMENTED();
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000137}
138#endif
139
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000140} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000141
Alexey Samsonov5ec35b72014-03-06 09:05:52 +0000142#endif // SANITIZER_FREEBSD || SANITIZER_LINUX