blob: 40f9f94d02841773fc94fb8e29da1c5df98e2c0b [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"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000021
Kostya Serebryany78d87d32012-01-05 01:07:27 +000022#include <sys/time.h>
23#include <sys/resource.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000024#include <sys/mman.h>
25#include <sys/syscall.h>
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000026#include <sys/types.h>
27#include <fcntl.h>
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000028#include <link.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
Kostya Serebryany019b76f2011-11-30 01:07:02 +000039namespace __asan {
40
41void *AsanDoesNotSupportStaticLinkage() {
42 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000043 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000044}
45
Alexander Potapenko720aaef2012-02-13 17:09:40 +000046bool AsanShadowRangeIsAvailable() {
47 // FIXME: shall we need anything here on Linux?
48 return true;
49}
50
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000051void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
52#ifdef ANDROID
53 *pc = *sp = *bp = 0;
54#elif defined(__arm__)
55 ucontext_t *ucontext = (ucontext_t*)context;
56 *pc = ucontext->uc_mcontext.arm_pc;
57 *bp = ucontext->uc_mcontext.arm_fp;
58 *sp = ucontext->uc_mcontext.arm_sp;
59# elif defined(__x86_64__)
60 ucontext_t *ucontext = (ucontext_t*)context;
61 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
62 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
63 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
64# elif defined(__i386__)
65 ucontext_t *ucontext = (ucontext_t*)context;
66 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
67 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
68 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
69#else
70# error "Unsupported arch"
71#endif
72}
73
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000074bool AsanInterceptsSignal(int signum) {
75 return signum == SIGSEGV && FLAG_handle_segv;
76}
77
Kostya Serebryanya7720962011-12-28 23:28:54 +000078static void *asan_mmap(void *addr, size_t length, int prot, int flags,
Kostya Serebryany019b76f2011-11-30 01:07:02 +000079 int fd, uint64_t offset) {
80# if __WORDSIZE == 64
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000081 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000082# else
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000083 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000084# endif
85}
86
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000087void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) {
88 size = RoundUpTo(size, kPageSize);
89 void *res = asan_mmap(0, size,
90 PROT_READ | PROT_WRITE,
91 MAP_PRIVATE | MAP_ANON, -1, 0);
92 if (res == (void*)-1) {
93 OutOfMemoryMessageAndDie(mem_type, size);
94 }
95 return res;
96}
97
Kostya Serebryanya7720962011-12-28 23:28:54 +000098void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
99 return asan_mmap((void*)fixed_addr, size,
100 PROT_READ | PROT_WRITE,
101 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
102 0, 0);
103}
104
Kostya Serebryanya7720962011-12-28 23:28:54 +0000105void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
106 return asan_mmap((void*)fixed_addr, size,
107 PROT_NONE,
108 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
109 0, 0);
110}
111
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000112void AsanUnmapOrDie(void *addr, size_t size) {
113 if (!addr || !size) return;
114 int res = syscall(__NR_munmap, addr, size);
115 if (res != 0) {
116 Report("Failed to unmap\n");
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000117 AsanDie();
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000118 }
119}
120
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000121size_t AsanWrite(int fd, const void *buf, size_t count) {
122 return (size_t)syscall(__NR_write, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000123}
124
125int AsanOpenReadonly(const char* filename) {
Kostya Serebryany546ba362012-02-06 19:23:38 +0000126 return syscall(__NR_open, filename, O_RDONLY);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000127}
128
Alexander Potapenko553c2082012-01-13 12:59:48 +0000129// Like getenv, but reads env directly from /proc and does not use libc.
130// This function should be called first inside __asan_init.
131const char* AsanGetEnv(const char* name) {
132 static char *environ;
133 static size_t len;
134 static bool inited;
135 if (!inited) {
136 inited = true;
137 size_t environ_size;
138 len = ReadFileToBuffer("/proc/self/environ",
Kostya Serebryany614b53d2012-02-07 00:47:35 +0000139 &environ, &environ_size, 1 << 26);
Alexander Potapenko553c2082012-01-13 12:59:48 +0000140 }
141 if (!environ || len == 0) return NULL;
142 size_t namelen = internal_strlen(name);
143 const char *p = environ;
144 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
145 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
146 const char* endp =
147 (char*)internal_memchr(p, '\0', len - (p - environ));
148 if (endp == NULL) // this entry isn't NUL terminated
149 return NULL;
150 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
151 return p + namelen + 1; // point after =
152 p = endp + 1;
153 }
154 return NULL; // Not found.
155}
156
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000157size_t AsanRead(int fd, void *buf, size_t count) {
158 return (size_t)syscall(__NR_read, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000159}
160
161int AsanClose(int fd) {
Kostya Serebryany546ba362012-02-06 19:23:38 +0000162 return syscall(__NR_close, fd);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000163}
164
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000165AsanProcMaps::AsanProcMaps() {
166 proc_self_maps_buff_len_ =
167 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
Kostya Serebryany614b53d2012-02-07 00:47:35 +0000168 &proc_self_maps_buff_mmaped_size_, 1 << 26);
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000169 CHECK(proc_self_maps_buff_len_ > 0);
170 // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
171 Reset();
172}
173
174AsanProcMaps::~AsanProcMaps() {
175 AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
176}
177
178void AsanProcMaps::Reset() {
179 current_ = proc_self_maps_buff_;
180}
181
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000182bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end,
183 uintptr_t *offset, char filename[],
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000184 size_t filename_size) {
185 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
186 if (current_ >= last) return false;
187 int consumed = 0;
188 char flags[10];
189 int major, minor;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000190 uintptr_t inode;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000191 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
192 if (next_line == NULL)
193 next_line = last;
194 if (SScanf(current_,
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000195 "%lx-%lx %4s %lx %x:%x %ld %n",
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000196 start, end, flags, offset, &major, &minor,
197 &inode, &consumed) != 7)
198 return false;
199 current_ += consumed;
200 // Skip spaces.
201 while (current_ < next_line && *current_ == ' ')
202 current_++;
203 // Fill in the filename.
204 size_t i = 0;
205 while (current_ < next_line) {
206 if (filename && i < filename_size - 1)
207 filename[i++] = *current_;
208 current_++;
209 }
210 if (filename && i < filename_size)
211 filename[i] = 0;
212 current_ = next_line + 1;
213 return true;
214}
215
Kostya Serebryanyc7835f32012-03-08 21:19:07 +0000216#if 1
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000217
218// Gets the object name and the offset by walking AsanProcMaps.
219bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
220 char filename[],
221 size_t filename_size) {
Alexander Potapenko42573862012-01-18 11:16:05 +0000222 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000223}
224
Kostya Serebryanyc7835f32012-03-08 21:19:07 +0000225#else
226// dl_iterate_phdr machinery is not working well for us.
227// We either need to fix it or get rid of it.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000228struct DlIterateData {
229 int count;
230 uintptr_t addr;
231 uintptr_t offset;
232 char *filename;
233 size_t filename_size;
234};
235
236static int dl_iterate_phdr_callback(struct dl_phdr_info *info,
237 size_t size, void *raw_data) {
238 DlIterateData *data = (DlIterateData*)raw_data;
239 int count = data->count++;
240 if (info->dlpi_addr > data->addr)
241 return 0;
242 if (count == 0) {
243 // The first item (the main executable) does not have a so name,
244 // but we can just read it from /proc/self/exe.
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000245 size_t path_len = readlink("/proc/self/exe",
246 data->filename, data->filename_size - 1);
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000247 data->filename[path_len] = 0;
248 } else {
249 CHECK(info->dlpi_name);
Alexey Samsonove7254782012-02-08 13:45:31 +0000250 REAL(strncpy)(data->filename, info->dlpi_name, data->filename_size);
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000251 }
252 data->offset = data->addr - info->dlpi_addr;
253 return 1;
254}
255
256// Gets the object name and the offset using dl_iterate_phdr.
257bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
258 char filename[],
259 size_t filename_size) {
260 DlIterateData data;
261 data.count = 0;
262 data.addr = addr;
263 data.filename = filename;
264 data.filename_size = filename_size;
265 if (dl_iterate_phdr(dl_iterate_phdr_callback, &data)) {
266 *offset = data.offset;
267 return true;
268 }
269 return false;
270}
271
Alexey Samsonov209c5142012-01-17 06:39:10 +0000272#endif // __arm__
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000273
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000274void AsanThread::SetThreadStackTopAndBottom() {
275 if (tid() == 0) {
276 // This is the main thread. Libpthread may not be initialized yet.
277 struct rlimit rl;
278 CHECK(getrlimit(RLIMIT_STACK, &rl) == 0);
279
280 // Find the mapping that contains a stack variable.
281 AsanProcMaps proc_maps;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000282 uintptr_t start, end, offset;
283 uintptr_t prev_end = 0;
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000284 while (proc_maps.Next(&start, &end, &offset, NULL, 0)) {
285 if ((uintptr_t)&rl < end)
286 break;
287 prev_end = end;
288 }
289 CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end);
290
291 // Get stacksize from rlimit, but clip it so that it does not overlap
292 // with other mappings.
293 size_t stacksize = rl.rlim_cur;
294 if (stacksize > end - prev_end)
295 stacksize = end - prev_end;
296 if (stacksize > kMaxThreadStackSize)
297 stacksize = kMaxThreadStackSize;
298 stack_top_ = end;
299 stack_bottom_ = end - stacksize;
300 CHECK(AddrIsInStack((uintptr_t)&rl));
301 return;
302 }
303 pthread_attr_t attr;
304 CHECK(pthread_getattr_np(pthread_self(), &attr) == 0);
305 size_t stacksize = 0;
306 void *stackaddr = NULL;
307 pthread_attr_getstack(&attr, &stackaddr, &stacksize);
308 pthread_attr_destroy(&attr);
309
310 stack_top_ = (uintptr_t)stackaddr + stacksize;
311 stack_bottom_ = (uintptr_t)stackaddr;
312 // When running with unlimited stack size, we still want to set some limit.
313 // The unlimited stack size is caused by 'ulimit -s unlimited'.
314 // Also, for some reason, GNU make spawns subrocesses with unlimited stack.
315 if (stacksize > kMaxThreadStackSize) {
316 stack_bottom_ = stack_top_ - kMaxThreadStackSize;
317 }
318 CHECK(AddrIsInStack((uintptr_t)&attr));
319}
320
Kostya Serebryanya82f0d42012-01-10 21:24:40 +0000321AsanLock::AsanLock(LinkerInitialized) {
322 // We assume that pthread_mutex_t initialized to all zeroes is a valid
323 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
324 // a gcc warning:
325 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
326}
327
328void AsanLock::Lock() {
329 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
330 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
331 CHECK(!owner_);
332 owner_ = (uintptr_t)pthread_self();
333}
334
335void AsanLock::Unlock() {
336 CHECK(owner_ == (uintptr_t)pthread_self());
337 owner_ = 0;
338 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
339}
340
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000341#ifdef __arm__
342#define UNWIND_STOP _URC_END_OF_STACK
343#define UNWIND_CONTINUE _URC_NO_REASON
344#else
345#define UNWIND_STOP _URC_NORMAL_STOP
346#define UNWIND_CONTINUE _URC_NO_REASON
347#endif
348
349uintptr_t Unwind_GetIP(struct _Unwind_Context *ctx) {
350#ifdef __arm__
351 uintptr_t val;
352 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
353 15 /* r15 = PC */, _UVRSD_UINT32, &val);
354 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
355 // Clear the Thumb bit.
356 return val & ~(uintptr_t)1;
357#else
358 return _Unwind_GetIP(ctx);
359#endif
360}
361
362_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
363 void *param) {
364 AsanStackTrace *b = (AsanStackTrace*)param;
365 CHECK(b->size < b->max_size);
366 uintptr_t pc = Unwind_GetIP(ctx);
367 b->trace[b->size++] = pc;
368 if (b->size == b->max_size) return UNWIND_STOP;
369 return UNWIND_CONTINUE;
370}
371
372void AsanStackTrace::GetStackTrace(size_t max_s, uintptr_t pc, uintptr_t bp) {
373 size = 0;
374 trace[0] = pc;
375 if ((max_s) > 1) {
376 max_size = max_s;
377#ifdef __arm__
378 _Unwind_Backtrace(Unwind_Trace, this);
379#else
380 FastUnwindStack(pc, bp);
381#endif
382 }
383}
384
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000385} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000386
387#endif // __linux__