blob: 4e7ca7e77bde85d9ebbc779330339f8eb72ace14 [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"
16#if 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 Samsonov2c5fc3b2012-06-04 14:27:50 +000021#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000022#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000023
Kostya Serebryany78d87d32012-01-05 01:07:27 +000024#include <sys/time.h>
25#include <sys/resource.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000026#include <sys/mman.h>
27#include <sys/syscall.h>
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000028#include <sys/types.h>
29#include <fcntl.h>
Kostya Serebryany78d87d32012-01-05 01:07:27 +000030#include <pthread.h>
Kostya Serebryanycd271f52012-01-05 00:44:33 +000031#include <stdio.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000032#include <unistd.h>
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000033#include <unwind.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000034
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000035#if SANITIZER_ANDROID
36#include <ucontext.h>
37#else
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000038#include <sys/ucontext.h>
39#endif
40
Evgeniy Stepanov4cc26312012-03-26 09:48:41 +000041extern "C" void* _DYNAMIC;
42
Kostya Serebryany019b76f2011-11-30 01:07:02 +000043namespace __asan {
44
Alexander Potapenkofefc1e92012-08-24 09:22:05 +000045void MaybeReexec() {
46 // No need to re-exec on Linux.
47}
48
Kostya Serebryany019b76f2011-11-30 01:07:02 +000049void *AsanDoesNotSupportStaticLinkage() {
50 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000051 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000052}
53
Kostya Serebryany8d032042012-05-31 14:35:53 +000054void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanov6db97e82014-02-10 13:34:43 +000055#if defined(__arm__)
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000056 ucontext_t *ucontext = (ucontext_t*)context;
57 *pc = ucontext->uc_mcontext.arm_pc;
58 *bp = ucontext->uc_mcontext.arm_fp;
59 *sp = ucontext->uc_mcontext.arm_sp;
Kostya Serebryanyc98ce282014-02-13 07:50:20 +000060# elif defined(__aarch64__)
61 ucontext_t *ucontext = (ucontext_t*)context;
62 *pc = ucontext->uc_mcontext.pc;
63 *bp = ucontext->uc_mcontext.regs[29];
64 *sp = ucontext->uc_mcontext.sp;
Kostya Serebryanya92b07d2013-11-18 08:20:13 +000065# elif defined(__hppa__)
66 ucontext_t *ucontext = (ucontext_t*)context;
67 *pc = ucontext->uc_mcontext.sc_iaoq[0];
68 /* GCC uses %r3 whenever a frame pointer is needed. */
69 *bp = ucontext->uc_mcontext.sc_gr[3];
70 *sp = ucontext->uc_mcontext.sc_gr[30];
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000071# elif defined(__x86_64__)
72 ucontext_t *ucontext = (ucontext_t*)context;
73 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
74 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
75 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
76# elif defined(__i386__)
77 ucontext_t *ucontext = (ucontext_t*)context;
78 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
79 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
80 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Dmitry Vyukov4ee90c232012-11-16 11:26:05 +000081# elif defined(__sparc__)
82 ucontext_t *ucontext = (ucontext_t*)context;
83 uptr *stk_ptr;
84# if defined (__arch64__)
85 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
86 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
87 stk_ptr = (uptr *) (*sp + 2047);
88 *bp = stk_ptr[15];
89# else
90 *pc = ucontext->uc_mcontext.gregs[REG_PC];
91 *sp = ucontext->uc_mcontext.gregs[REG_O6];
92 stk_ptr = (uptr *) *sp;
93 *bp = stk_ptr[15];
94# endif
Kostya Serebryanyc1aa0e82013-06-03 14:49:25 +000095# elif defined(__mips__)
96 ucontext_t *ucontext = (ucontext_t*)context;
97 *pc = ucontext->uc_mcontext.gregs[31];
98 *bp = ucontext->uc_mcontext.gregs[30];
99 *sp = ucontext->uc_mcontext.gregs[29];
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +0000100#else
101# error "Unsupported arch"
102#endif
103}
104
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000105bool AsanInterceptsSignal(int signum) {
Alexander Potapenkocf4bef32014-01-28 09:28:57 +0000106 return signum == SIGSEGV && common_flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +0000107}
108
Alexander Potapenko51e64882012-07-23 14:07:58 +0000109void AsanPlatformThreadInit() {
110 // Nothing here for now.
111}
112
Evgeniy Stepanovd3b56602013-03-19 13:54:41 +0000113#if !SANITIZER_ANDROID
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000114void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000115 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000116 *stack = (uptr)ucp->uc_stack.ss_sp;
117 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000118}
119#else
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000120void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonov95856132013-01-18 09:20:06 +0000121 UNIMPLEMENTED();
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000122}
123#endif
124
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000125} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000126
Alexey Samsonov21cb7432013-04-03 07:29:53 +0000127#endif // SANITIZER_LINUX