blob: 9a3d6bdb15a61be500d58545dd395cdaad134111 [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
Alexander Potapenko51e64882012-07-23 14:07:58 +000075void AsanPlatformThreadInit() {
76 // Nothing here for now.
77}
78
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000079AsanLock::AsanLock(LinkerInitialized) {
80 // We assume that pthread_mutex_t initialized to all zeroes is a valid
81 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
82 // a gcc warning:
83 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
84}
85
86void AsanLock::Lock() {
87 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
88 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
89 CHECK(!owner_);
Kostya Serebryany8d032042012-05-31 14:35:53 +000090 owner_ = (uptr)pthread_self();
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000091}
92
93void AsanLock::Unlock() {
Kostya Serebryany8d032042012-05-31 14:35:53 +000094 CHECK(owner_ == (uptr)pthread_self());
Kostya Serebryanya82f0d42012-01-10 21:24:40 +000095 owner_ = 0;
96 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
97}
98
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000099#ifdef __arm__
100#define UNWIND_STOP _URC_END_OF_STACK
101#define UNWIND_CONTINUE _URC_NO_REASON
102#else
103#define UNWIND_STOP _URC_NORMAL_STOP
104#define UNWIND_CONTINUE _URC_NO_REASON
105#endif
106
Kostya Serebryany8d032042012-05-31 14:35:53 +0000107uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000108#ifdef __arm__
Kostya Serebryany8d032042012-05-31 14:35:53 +0000109 uptr val;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000110 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
111 15 /* r15 = PC */, _UVRSD_UINT32, &val);
112 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
113 // Clear the Thumb bit.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000114 return val & ~(uptr)1;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000115#else
116 return _Unwind_GetIP(ctx);
117#endif
118}
119
120_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
121 void *param) {
122 AsanStackTrace *b = (AsanStackTrace*)param;
123 CHECK(b->size < b->max_size);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000124 uptr pc = Unwind_GetIP(ctx);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000125 b->trace[b->size++] = pc;
126 if (b->size == b->max_size) return UNWIND_STOP;
127 return UNWIND_CONTINUE;
128}
129
Kostya Serebryany8d032042012-05-31 14:35:53 +0000130void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000131 size = 0;
132 trace[0] = pc;
133 if ((max_s) > 1) {
134 max_size = max_s;
135#ifdef __arm__
136 _Unwind_Backtrace(Unwind_Trace, this);
137#else
138 FastUnwindStack(pc, bp);
139#endif
140 }
141}
142
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000143} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000144
145#endif // __linux__