blob: 845493de0956cd08e21e7b7e607e350b1c0413f4 [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//===----------------------------------------------------------------------===//
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +000014#ifdef __linux__
Kostya Serebryany019b76f2011-11-30 01:07:02 +000015
Kostya Serebryanycd271f52012-01-05 00:44:33 +000016#include "asan_interceptors.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000017#include "asan_internal.h"
Kostya Serebryany78d87d32012-01-05 01:07:27 +000018#include "asan_thread.h"
Kostya Serebryanyee928772012-08-28 13:25:55 +000019#include "asan_thread_registry.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000020#include "sanitizer_common/sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000021#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000022
Kostya Serebryany78d87d32012-01-05 01:07:27 +000023#include <sys/time.h>
24#include <sys/resource.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000025#include <sys/mman.h>
26#include <sys/syscall.h>
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000027#include <sys/types.h>
28#include <fcntl.h>
Kostya Serebryany78d87d32012-01-05 01:07:27 +000029#include <pthread.h>
Kostya Serebryanycd271f52012-01-05 00:44:33 +000030#include <stdio.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000031#include <unistd.h>
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000032#include <unwind.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000033
Evgeniy Stepanovbe013982012-08-27 11:15:55 +000034#if !ASAN_ANDROID
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000035// FIXME: where to get ucontext on Android?
36#include <sys/ucontext.h>
37#endif
38
Evgeniy Stepanov4cc26312012-03-26 09:48:41 +000039extern "C" void* _DYNAMIC;
40
Kostya Serebryany019b76f2011-11-30 01:07:02 +000041namespace __asan {
42
Alexander Potapenkofefc1e92012-08-24 09:22:05 +000043void MaybeReexec() {
44 // No need to re-exec on Linux.
45}
46
Kostya Serebryany019b76f2011-11-30 01:07:02 +000047void *AsanDoesNotSupportStaticLinkage() {
48 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000049 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000050}
51
Kostya Serebryany8d032042012-05-31 14:35:53 +000052void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Evgeniy Stepanovbe013982012-08-27 11:15:55 +000053#if ASAN_ANDROID
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000054 *pc = *sp = *bp = 0;
55#elif defined(__arm__)
56 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;
60# elif defined(__x86_64__)
61 ucontext_t *ucontext = (ucontext_t*)context;
62 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
63 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
64 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
65# elif defined(__i386__)
66 ucontext_t *ucontext = (ucontext_t*)context;
67 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
68 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
69 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
Kostya Serebryany46de5802012-11-20 07:00:42 +000070# elif defined(__powerpc__) || defined(__powerpc64__)
71 ucontext_t *ucontext = (ucontext_t*)context;
72 *pc = ucontext->uc_mcontext.regs->nip;
73 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
74 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
75 // pointer, but GCC always uses r31 when we need a frame pointer.
76 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
Dmitry Vyukov4ee90c232012-11-16 11:26:05 +000077# 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
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000091#else
92# error "Unsupported arch"
93#endif
94}
95
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000096bool AsanInterceptsSignal(int signum) {
Alexey Samsonov34efb8e2012-07-09 14:36:04 +000097 return signum == SIGSEGV && flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000098}
99
Alexander Potapenko51e64882012-07-23 14:07:58 +0000100void AsanPlatformThreadInit() {
101 // Nothing here for now.
102}
103
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000104void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, bool fast) {
Kostya Serebryany214b7152012-12-03 18:39:21 +0000105#if defined(__arm__) || \
106 defined(__powerpc__) || defined(__powerpc64__) || \
107 defined(__sparc__)
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000108 fast = false;
109#endif
110 if (!fast)
Kostya Serebryanybb698792012-12-13 12:31:55 +0000111 return stack->SlowUnwindStack(pc, max_s);
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000112 stack->size = 0;
113 stack->trace[0] = pc;
114 if (max_s > 1) {
115 stack->max_size = max_s;
Kostya Serebryanyee928772012-08-28 13:25:55 +0000116 if (!asan_inited) return;
117 if (AsanThread *t = asanThreadRegistry().GetCurrent())
118 stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom());
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000119 }
120}
121
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000122#if !ASAN_ANDROID
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000123void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000124 ucontext_t *ucp = (ucontext_t*)context;
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000125 *stack = (uptr)ucp->uc_stack.ss_sp;
126 *ssize = ucp->uc_stack.ss_size;
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000127}
128#else
Alexey Samsonov4f1885a2013-01-17 15:45:28 +0000129void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
Alexey Samsonov95856132013-01-18 09:20:06 +0000130 UNIMPLEMENTED();
Alexey Samsonovaac36b32012-11-23 10:14:44 +0000131}
132#endif
133
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000134} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000135
136#endif // __linux__