blob: 0bb2bf4ad9ec1f3920c218b4500344a06d72bc94 [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
43void *AsanDoesNotSupportStaticLinkage() {
44 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000045 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000046}
47
Kostya Serebryany8d032042012-05-31 14:35:53 +000048void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000049#ifdef ANDROID
50 *pc = *sp = *bp = 0;
51#elif defined(__arm__)
52 ucontext_t *ucontext = (ucontext_t*)context;
53 *pc = ucontext->uc_mcontext.arm_pc;
54 *bp = ucontext->uc_mcontext.arm_fp;
55 *sp = ucontext->uc_mcontext.arm_sp;
56# elif defined(__x86_64__)
57 ucontext_t *ucontext = (ucontext_t*)context;
58 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
59 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
60 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
61# elif defined(__i386__)
62 ucontext_t *ucontext = (ucontext_t*)context;
63 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
64 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
65 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
66#else
67# error "Unsupported arch"
68#endif
69}
70
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000071bool AsanInterceptsSignal(int signum) {
Alexey Samsonov34efb8e2012-07-09 14:36:04 +000072 return signum == SIGSEGV && flags()->handle_segv;
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000073}
74
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000075AsanLock::AsanLock(LinkerInitialized) {
76 // We assume that pthread_mutex_t initialized to all zeroes is a valid
77 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
78 // a gcc warning:
79 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
80}
81
82void AsanLock::Lock() {
83 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
84 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
85 CHECK(!owner_);
Kostya Serebryany8d032042012-05-31 14:35:53 +000086 owner_ = (uptr)pthread_self();
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000087}
88
89void AsanLock::Unlock() {
Kostya Serebryany8d032042012-05-31 14:35:53 +000090 CHECK(owner_ == (uptr)pthread_self());
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000091 owner_ = 0;
92 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
93}
94
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000095#ifdef __arm__
96#define UNWIND_STOP _URC_END_OF_STACK
97#define UNWIND_CONTINUE _URC_NO_REASON
98#else
99#define UNWIND_STOP _URC_NORMAL_STOP
100#define UNWIND_CONTINUE _URC_NO_REASON
101#endif
102
Kostya Serebryany8d032042012-05-31 14:35:53 +0000103uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000104#ifdef __arm__
Kostya Serebryany8d032042012-05-31 14:35:53 +0000105 uptr val;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000106 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
107 15 /* r15 = PC */, _UVRSD_UINT32, &val);
108 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
109 // Clear the Thumb bit.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000110 return val & ~(uptr)1;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000111#else
112 return _Unwind_GetIP(ctx);
113#endif
114}
115
116_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
117 void *param) {
118 AsanStackTrace *b = (AsanStackTrace*)param;
119 CHECK(b->size < b->max_size);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000120 uptr pc = Unwind_GetIP(ctx);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000121 b->trace[b->size++] = pc;
122 if (b->size == b->max_size) return UNWIND_STOP;
123 return UNWIND_CONTINUE;
124}
125
Kostya Serebryany8d032042012-05-31 14:35:53 +0000126void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000127 size = 0;
128 trace[0] = pc;
129 if ((max_s) > 1) {
130 max_size = max_s;
131#ifdef __arm__
132 _Unwind_Backtrace(Unwind_Trace, this);
133#else
134 FastUnwindStack(pc, bp);
135#endif
136 }
137}
138
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000139} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000140
141#endif // __linux__