blob: ddaefb29f45d6395c6a413d8e48621538ab14d3f [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 Serebryanya82f0d42012-01-10 21:24:40 +000018#include "asan_lock.h"
Kostya Serebryany78d87d32012-01-05 01:07:27 +000019#include "asan_thread.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
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000034#ifndef ANDROID
35// 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) {
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000053#ifdef ANDROID
54 *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];
70#else
71# error "Unsupported arch"
72#endif
73}
74
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000075bool AsanInterceptsSignal(int signum) {
Alexey Samsonov34efb8e2012-07-09 14:36:04 +000076 return signum == SIGSEGV && flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000077}
78
Alexander Potapenko51e64882012-07-23 14:07:58 +000079void AsanPlatformThreadInit() {
80 // Nothing here for now.
81}
82
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000083AsanLock::AsanLock(LinkerInitialized) {
84 // We assume that pthread_mutex_t initialized to all zeroes is a valid
85 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
86 // a gcc warning:
87 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
88}
89
90void AsanLock::Lock() {
91 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
92 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
93 CHECK(!owner_);
Kostya Serebryany8d032042012-05-31 14:35:53 +000094 owner_ = (uptr)pthread_self();
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000095}
96
97void AsanLock::Unlock() {
Kostya Serebryany8d032042012-05-31 14:35:53 +000098 CHECK(owner_ == (uptr)pthread_self());
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000099 owner_ = 0;
100 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
101}
102
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000103#ifdef __arm__
104#define UNWIND_STOP _URC_END_OF_STACK
105#define UNWIND_CONTINUE _URC_NO_REASON
106#else
107#define UNWIND_STOP _URC_NORMAL_STOP
108#define UNWIND_CONTINUE _URC_NO_REASON
109#endif
110
Kostya Serebryany8d032042012-05-31 14:35:53 +0000111uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000112#ifdef __arm__
Kostya Serebryany8d032042012-05-31 14:35:53 +0000113 uptr val;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000114 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
115 15 /* r15 = PC */, _UVRSD_UINT32, &val);
116 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
117 // Clear the Thumb bit.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000118 return val & ~(uptr)1;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000119#else
120 return _Unwind_GetIP(ctx);
121#endif
122}
123
124_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
125 void *param) {
126 AsanStackTrace *b = (AsanStackTrace*)param;
127 CHECK(b->size < b->max_size);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000128 uptr pc = Unwind_GetIP(ctx);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000129 b->trace[b->size++] = pc;
130 if (b->size == b->max_size) return UNWIND_STOP;
131 return UNWIND_CONTINUE;
132}
133
Kostya Serebryany8d032042012-05-31 14:35:53 +0000134void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000135 size = 0;
136 trace[0] = pc;
137 if ((max_s) > 1) {
138 max_size = max_s;
139#ifdef __arm__
140 _Unwind_Backtrace(Unwind_Trace, this);
141#else
142 FastUnwindStack(pc, bp);
143#endif
144 }
145}
146
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000147} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000148
149#endif // __linux__