blob: 1577d2f5214b50a21b73143cc44fd89696f5e127 [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 Serebryanycd271f52012-01-05 00:44:33 +000019#include "asan_procmaps.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"
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
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000039using namespace __sanitizer; // NOLINT
40
Evgeniy Stepanov4cc26312012-03-26 09:48:41 +000041extern "C" void* _DYNAMIC;
42
Kostya Serebryany019b76f2011-11-30 01:07:02 +000043namespace __asan {
44
Kostya Serebryany8d032042012-05-31 14:35:53 +000045const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M
Kostya Serebryany02d5ec52012-05-22 11:54:44 +000046
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) {
76 return signum == SIGSEGV && FLAG_handle_segv;
77}
78
Kostya Serebryany8d032042012-05-31 14:35:53 +000079void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) {
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000080 size = RoundUpTo(size, kPageSize);
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000081 void *res = internal_mmap(0, size,
82 PROT_READ | PROT_WRITE,
83 MAP_PRIVATE | MAP_ANON, -1, 0);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000084 if (res == (void*)-1) {
85 OutOfMemoryMessageAndDie(mem_type, size);
86 }
87 return res;
88}
89
Kostya Serebryany8d032042012-05-31 14:35:53 +000090void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size) {
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000091 return internal_mmap((void*)fixed_addr, size,
92 PROT_READ | PROT_WRITE,
93 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
94 0, 0);
Kostya Serebryanya7720962011-12-28 23:28:54 +000095}
96
Kostya Serebryany8d032042012-05-31 14:35:53 +000097void *AsanMprotect(uptr fixed_addr, uptr size) {
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000098 return internal_mmap((void*)fixed_addr, size,
99 PROT_NONE,
100 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
101 0, 0);
Kostya Serebryanya7720962011-12-28 23:28:54 +0000102}
103
Kostya Serebryany8d032042012-05-31 14:35:53 +0000104void AsanUnmapOrDie(void *addr, uptr size) {
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000105 if (!addr || !size) return;
106 int res = syscall(__NR_munmap, addr, size);
107 if (res != 0) {
108 Report("Failed to unmap\n");
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000109 AsanDie();
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000110 }
111}
112
Kostya Serebryany8d032042012-05-31 14:35:53 +0000113uptr AsanWrite(int fd, const void *buf, uptr count) {
114 return (uptr)syscall(__NR_write, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000115}
116
117int AsanOpenReadonly(const char* filename) {
Kostya Serebryany546ba362012-02-06 19:23:38 +0000118 return syscall(__NR_open, filename, O_RDONLY);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000119}
120
Alexander Potapenko553c2082012-01-13 12:59:48 +0000121// Like getenv, but reads env directly from /proc and does not use libc.
122// This function should be called first inside __asan_init.
123const char* AsanGetEnv(const char* name) {
124 static char *environ;
Kostya Serebryany8d032042012-05-31 14:35:53 +0000125 static uptr len;
Alexander Potapenko553c2082012-01-13 12:59:48 +0000126 static bool inited;
127 if (!inited) {
128 inited = true;
Kostya Serebryany8d032042012-05-31 14:35:53 +0000129 uptr environ_size;
Alexander Potapenko553c2082012-01-13 12:59:48 +0000130 len = ReadFileToBuffer("/proc/self/environ",
Kostya Serebryany614b53d2012-02-07 00:47:35 +0000131 &environ, &environ_size, 1 << 26);
Alexander Potapenko553c2082012-01-13 12:59:48 +0000132 }
Kostya Serebryany8d032042012-05-31 14:35:53 +0000133 if (!environ || len == 0) return 0;
134 uptr namelen = internal_strlen(name);
Alexander Potapenko553c2082012-01-13 12:59:48 +0000135 const char *p = environ;
136 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
137 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
138 const char* endp =
139 (char*)internal_memchr(p, '\0', len - (p - environ));
Kostya Serebryany8d032042012-05-31 14:35:53 +0000140 if (endp == 0) // this entry isn't NUL terminated
141 return 0;
Alexander Potapenko553c2082012-01-13 12:59:48 +0000142 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
143 return p + namelen + 1; // point after =
144 p = endp + 1;
145 }
Kostya Serebryany8d032042012-05-31 14:35:53 +0000146 return 0; // Not found.
Alexander Potapenko553c2082012-01-13 12:59:48 +0000147}
148
Kostya Serebryany8d032042012-05-31 14:35:53 +0000149uptr AsanRead(int fd, void *buf, uptr count) {
150 return (uptr)syscall(__NR_read, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000151}
152
153int AsanClose(int fd) {
Kostya Serebryany546ba362012-02-06 19:23:38 +0000154 return syscall(__NR_close, fd);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000155}
156
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000157AsanProcMaps::AsanProcMaps() {
158 proc_self_maps_buff_len_ =
159 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
Kostya Serebryany614b53d2012-02-07 00:47:35 +0000160 &proc_self_maps_buff_mmaped_size_, 1 << 26);
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000161 CHECK(proc_self_maps_buff_len_ > 0);
162 // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
163 Reset();
164}
165
166AsanProcMaps::~AsanProcMaps() {
167 AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
168}
169
170void AsanProcMaps::Reset() {
171 current_ = proc_self_maps_buff_;
172}
173
Kostya Serebryany8d032042012-05-31 14:35:53 +0000174bool AsanProcMaps::Next(uptr *start, uptr *end,
175 uptr *offset, char filename[],
176 uptr filename_size) {
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000177 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
178 if (current_ >= last) return false;
179 int consumed = 0;
180 char flags[10];
181 int major, minor;
Kostya Serebryany8d032042012-05-31 14:35:53 +0000182 uptr inode;
183 uptr dummy;
Evgeniy Stepanov8152e222012-05-23 15:21:50 +0000184 if (!start) start = &dummy;
185 if (!end) end = &dummy;
186 if (!offset) offset = &dummy;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000187 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000188 if (next_line == 0)
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000189 next_line = last;
190 if (SScanf(current_,
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000191 "%lx-%lx %4s %lx %x:%x %ld %n",
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000192 start, end, flags, offset, &major, &minor,
193 &inode, &consumed) != 7)
194 return false;
195 current_ += consumed;
196 // Skip spaces.
197 while (current_ < next_line && *current_ == ' ')
198 current_++;
199 // Fill in the filename.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000200 uptr i = 0;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000201 while (current_ < next_line) {
202 if (filename && i < filename_size - 1)
203 filename[i++] = *current_;
204 current_++;
205 }
206 if (filename && i < filename_size)
207 filename[i] = 0;
208 current_ = next_line + 1;
209 return true;
210}
211
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000212// Gets the object name and the offset by walking AsanProcMaps.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000213bool AsanProcMaps::GetObjectNameAndOffset(uptr addr, uptr *offset,
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000214 char filename[],
Kostya Serebryany8d032042012-05-31 14:35:53 +0000215 uptr filename_size) {
Alexander Potapenko42573862012-01-18 11:16:05 +0000216 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000217}
218
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000219void AsanThread::SetThreadStackTopAndBottom() {
220 if (tid() == 0) {
221 // This is the main thread. Libpthread may not be initialized yet.
222 struct rlimit rl;
223 CHECK(getrlimit(RLIMIT_STACK, &rl) == 0);
224
225 // Find the mapping that contains a stack variable.
226 AsanProcMaps proc_maps;
Kostya Serebryany8d032042012-05-31 14:35:53 +0000227 uptr start, end, offset;
228 uptr prev_end = 0;
229 while (proc_maps.Next(&start, &end, &offset, 0, 0)) {
230 if ((uptr)&rl < end)
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000231 break;
232 prev_end = end;
233 }
Kostya Serebryany8d032042012-05-31 14:35:53 +0000234 CHECK((uptr)&rl >= start && (uptr)&rl < end);
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000235
236 // Get stacksize from rlimit, but clip it so that it does not overlap
237 // with other mappings.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000238 uptr stacksize = rl.rlim_cur;
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000239 if (stacksize > end - prev_end)
240 stacksize = end - prev_end;
Kostya Serebryany02d5ec52012-05-22 11:54:44 +0000241 // When running with unlimited stack size, we still want to set some limit.
242 // The unlimited stack size is caused by 'ulimit -s unlimited'.
Dmitry Vyukov4a176752012-05-23 06:14:21 +0000243 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000244 if (stacksize > kMaxThreadStackSize)
245 stacksize = kMaxThreadStackSize;
246 stack_top_ = end;
247 stack_bottom_ = end - stacksize;
Kostya Serebryany8d032042012-05-31 14:35:53 +0000248 CHECK(AddrIsInStack((uptr)&rl));
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000249 return;
250 }
251 pthread_attr_t attr;
252 CHECK(pthread_getattr_np(pthread_self(), &attr) == 0);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000253 uptr stacksize = 0;
254 void *stackaddr = 0;
255 pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize);
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000256 pthread_attr_destroy(&attr);
257
Kostya Serebryany8d032042012-05-31 14:35:53 +0000258 stack_top_ = (uptr)stackaddr + stacksize;
259 stack_bottom_ = (uptr)stackaddr;
Kostya Serebryany02d5ec52012-05-22 11:54:44 +0000260 CHECK(stacksize < kMaxThreadStackSize); // Sanity check.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000261 CHECK(AddrIsInStack((uptr)&attr));
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000262}
263
Kostya Serebryanya82f0d42012-01-10 21:24:40 +0000264AsanLock::AsanLock(LinkerInitialized) {
265 // We assume that pthread_mutex_t initialized to all zeroes is a valid
266 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
267 // a gcc warning:
268 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
269}
270
271void AsanLock::Lock() {
272 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
273 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
274 CHECK(!owner_);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000275 owner_ = (uptr)pthread_self();
Kostya Serebryanya82f0d42012-01-10 21:24:40 +0000276}
277
278void AsanLock::Unlock() {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000279 CHECK(owner_ == (uptr)pthread_self());
Kostya Serebryanya82f0d42012-01-10 21:24:40 +0000280 owner_ = 0;
281 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
282}
283
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000284#ifdef __arm__
285#define UNWIND_STOP _URC_END_OF_STACK
286#define UNWIND_CONTINUE _URC_NO_REASON
287#else
288#define UNWIND_STOP _URC_NORMAL_STOP
289#define UNWIND_CONTINUE _URC_NO_REASON
290#endif
291
Kostya Serebryany8d032042012-05-31 14:35:53 +0000292uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000293#ifdef __arm__
Kostya Serebryany8d032042012-05-31 14:35:53 +0000294 uptr val;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000295 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
296 15 /* r15 = PC */, _UVRSD_UINT32, &val);
297 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
298 // Clear the Thumb bit.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000299 return val & ~(uptr)1;
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000300#else
301 return _Unwind_GetIP(ctx);
302#endif
303}
304
305_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
306 void *param) {
307 AsanStackTrace *b = (AsanStackTrace*)param;
308 CHECK(b->size < b->max_size);
Kostya Serebryany8d032042012-05-31 14:35:53 +0000309 uptr pc = Unwind_GetIP(ctx);
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000310 b->trace[b->size++] = pc;
311 if (b->size == b->max_size) return UNWIND_STOP;
312 return UNWIND_CONTINUE;
313}
314
Kostya Serebryany8d032042012-05-31 14:35:53 +0000315void AsanStackTrace::GetStackTrace(uptr max_s, uptr pc, uptr bp) {
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000316 size = 0;
317 trace[0] = pc;
318 if ((max_s) > 1) {
319 max_size = max_s;
320#ifdef __arm__
321 _Unwind_Backtrace(Unwind_Trace, this);
322#else
323 FastUnwindStack(pc, bp);
324#endif
325 }
326}
327
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000328} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000329
330#endif // __linux__