blob: 1e5ceae4ed744cc1ed7eb38b583be7efb93f7dc6 [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 Serebryany78d87d32012-01-05 01:07:27 +000028#include <pthread.h>
Kostya Serebryanycd271f52012-01-05 00:44:33 +000029#include <stdio.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000030#include <unistd.h>
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +000031#include <unwind.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000032
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000033#ifndef ANDROID
34// FIXME: where to get ucontext on Android?
35#include <sys/ucontext.h>
36#endif
37
Evgeniy Stepanov4cc26312012-03-26 09:48:41 +000038extern "C" void* _DYNAMIC;
39
Kostya Serebryany019b76f2011-11-30 01:07:02 +000040namespace __asan {
41
Kostya Serebryany02d5ec52012-05-22 11:54:44 +000042const size_t kMaxThreadStackSize = 256 * (1 << 20); // 256M
43
Kostya Serebryany019b76f2011-11-30 01:07:02 +000044void *AsanDoesNotSupportStaticLinkage() {
45 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000046 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000047}
48
Alexander Potapenko720aaef2012-02-13 17:09:40 +000049bool AsanShadowRangeIsAvailable() {
50 // FIXME: shall we need anything here on Linux?
51 return true;
52}
53
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000054void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
55#ifdef ANDROID
56 *pc = *sp = *bp = 0;
57#elif defined(__arm__)
58 ucontext_t *ucontext = (ucontext_t*)context;
59 *pc = ucontext->uc_mcontext.arm_pc;
60 *bp = ucontext->uc_mcontext.arm_fp;
61 *sp = ucontext->uc_mcontext.arm_sp;
62# elif defined(__x86_64__)
63 ucontext_t *ucontext = (ucontext_t*)context;
64 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
65 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
66 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
67# elif defined(__i386__)
68 ucontext_t *ucontext = (ucontext_t*)context;
69 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
70 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
71 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
72#else
73# error "Unsupported arch"
74#endif
75}
76
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000077bool AsanInterceptsSignal(int signum) {
78 return signum == SIGSEGV && FLAG_handle_segv;
79}
80
Kostya Serebryanya7720962011-12-28 23:28:54 +000081static void *asan_mmap(void *addr, size_t length, int prot, int flags,
Kostya Serebryany019b76f2011-11-30 01:07:02 +000082 int fd, uint64_t offset) {
83# if __WORDSIZE == 64
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000084 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000085# else
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000086 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000087# endif
88}
89
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000090void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) {
91 size = RoundUpTo(size, kPageSize);
92 void *res = asan_mmap(0, size,
93 PROT_READ | PROT_WRITE,
94 MAP_PRIVATE | MAP_ANON, -1, 0);
95 if (res == (void*)-1) {
96 OutOfMemoryMessageAndDie(mem_type, size);
97 }
98 return res;
99}
100
Kostya Serebryanya7720962011-12-28 23:28:54 +0000101void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
102 return asan_mmap((void*)fixed_addr, size,
103 PROT_READ | PROT_WRITE,
104 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
105 0, 0);
106}
107
Kostya Serebryanya7720962011-12-28 23:28:54 +0000108void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
109 return asan_mmap((void*)fixed_addr, size,
110 PROT_NONE,
111 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
112 0, 0);
113}
114
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000115void AsanUnmapOrDie(void *addr, size_t size) {
116 if (!addr || !size) return;
117 int res = syscall(__NR_munmap, addr, size);
118 if (res != 0) {
119 Report("Failed to unmap\n");
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000120 AsanDie();
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000121 }
122}
123
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000124size_t AsanWrite(int fd, const void *buf, size_t count) {
125 return (size_t)syscall(__NR_write, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000126}
127
128int AsanOpenReadonly(const char* filename) {
Kostya Serebryany546ba362012-02-06 19:23:38 +0000129 return syscall(__NR_open, filename, O_RDONLY);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000130}
131
Alexander Potapenko553c2082012-01-13 12:59:48 +0000132// Like getenv, but reads env directly from /proc and does not use libc.
133// This function should be called first inside __asan_init.
134const char* AsanGetEnv(const char* name) {
135 static char *environ;
136 static size_t len;
137 static bool inited;
138 if (!inited) {
139 inited = true;
140 size_t environ_size;
141 len = ReadFileToBuffer("/proc/self/environ",
Kostya Serebryany614b53d2012-02-07 00:47:35 +0000142 &environ, &environ_size, 1 << 26);
Alexander Potapenko553c2082012-01-13 12:59:48 +0000143 }
144 if (!environ || len == 0) return NULL;
145 size_t namelen = internal_strlen(name);
146 const char *p = environ;
147 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
148 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
149 const char* endp =
150 (char*)internal_memchr(p, '\0', len - (p - environ));
151 if (endp == NULL) // this entry isn't NUL terminated
152 return NULL;
153 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
154 return p + namelen + 1; // point after =
155 p = endp + 1;
156 }
157 return NULL; // Not found.
158}
159
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000160size_t AsanRead(int fd, void *buf, size_t count) {
161 return (size_t)syscall(__NR_read, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000162}
163
164int AsanClose(int fd) {
Kostya Serebryany546ba362012-02-06 19:23:38 +0000165 return syscall(__NR_close, fd);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000166}
167
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000168AsanProcMaps::AsanProcMaps() {
169 proc_self_maps_buff_len_ =
170 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
Kostya Serebryany614b53d2012-02-07 00:47:35 +0000171 &proc_self_maps_buff_mmaped_size_, 1 << 26);
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000172 CHECK(proc_self_maps_buff_len_ > 0);
173 // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
174 Reset();
175}
176
177AsanProcMaps::~AsanProcMaps() {
178 AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
179}
180
181void AsanProcMaps::Reset() {
182 current_ = proc_self_maps_buff_;
183}
184
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000185bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end,
186 uintptr_t *offset, char filename[],
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000187 size_t filename_size) {
188 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
189 if (current_ >= last) return false;
190 int consumed = 0;
191 char flags[10];
192 int major, minor;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000193 uintptr_t inode;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000194 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
195 if (next_line == NULL)
196 next_line = last;
197 if (SScanf(current_,
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000198 "%lx-%lx %4s %lx %x:%x %ld %n",
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000199 start, end, flags, offset, &major, &minor,
200 &inode, &consumed) != 7)
201 return false;
202 current_ += consumed;
203 // Skip spaces.
204 while (current_ < next_line && *current_ == ' ')
205 current_++;
206 // Fill in the filename.
207 size_t i = 0;
208 while (current_ < next_line) {
209 if (filename && i < filename_size - 1)
210 filename[i++] = *current_;
211 current_++;
212 }
213 if (filename && i < filename_size)
214 filename[i] = 0;
215 current_ = next_line + 1;
216 return true;
217}
218
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000219// Gets the object name and the offset by walking AsanProcMaps.
220bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
221 char filename[],
222 size_t filename_size) {
Alexander Potapenko42573862012-01-18 11:16:05 +0000223 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
Evgeniy Stepanov1b65b172012-01-16 12:45:07 +0000224}
225
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000226void AsanThread::SetThreadStackTopAndBottom() {
227 if (tid() == 0) {
228 // This is the main thread. Libpthread may not be initialized yet.
229 struct rlimit rl;
230 CHECK(getrlimit(RLIMIT_STACK, &rl) == 0);
231
232 // Find the mapping that contains a stack variable.
233 AsanProcMaps proc_maps;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000234 uintptr_t start, end, offset;
235 uintptr_t prev_end = 0;
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000236 while (proc_maps.Next(&start, &end, &offset, NULL, 0)) {
237 if ((uintptr_t)&rl < end)
238 break;
239 prev_end = end;
240 }
241 CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end);
242
243 // Get stacksize from rlimit, but clip it so that it does not overlap
244 // with other mappings.
245 size_t stacksize = rl.rlim_cur;
246 if (stacksize > end - prev_end)
247 stacksize = end - prev_end;
Kostya Serebryany02d5ec52012-05-22 11:54:44 +0000248 // When running with unlimited stack size, we still want to set some limit.
249 // The unlimited stack size is caused by 'ulimit -s unlimited'.
Dmitry Vyukov4a176752012-05-23 06:14:21 +0000250 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000251 if (stacksize > kMaxThreadStackSize)
252 stacksize = kMaxThreadStackSize;
253 stack_top_ = end;
254 stack_bottom_ = end - stacksize;
255 CHECK(AddrIsInStack((uintptr_t)&rl));
256 return;
257 }
258 pthread_attr_t attr;
259 CHECK(pthread_getattr_np(pthread_self(), &attr) == 0);
260 size_t stacksize = 0;
261 void *stackaddr = NULL;
262 pthread_attr_getstack(&attr, &stackaddr, &stacksize);
263 pthread_attr_destroy(&attr);
264
265 stack_top_ = (uintptr_t)stackaddr + stacksize;
266 stack_bottom_ = (uintptr_t)stackaddr;
Kostya Serebryany02d5ec52012-05-22 11:54:44 +0000267 CHECK(stacksize < kMaxThreadStackSize); // Sanity check.
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000268 CHECK(AddrIsInStack((uintptr_t)&attr));
269}
270
Kostya Serebryanya82f0d42012-01-10 21:24:40 +0000271AsanLock::AsanLock(LinkerInitialized) {
272 // We assume that pthread_mutex_t initialized to all zeroes is a valid
273 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
274 // a gcc warning:
275 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
276}
277
278void AsanLock::Lock() {
279 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
280 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
281 CHECK(!owner_);
282 owner_ = (uintptr_t)pthread_self();
283}
284
285void AsanLock::Unlock() {
286 CHECK(owner_ == (uintptr_t)pthread_self());
287 owner_ = 0;
288 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
289}
290
Evgeniy Stepanov84c44a82012-01-19 11:34:18 +0000291#ifdef __arm__
292#define UNWIND_STOP _URC_END_OF_STACK
293#define UNWIND_CONTINUE _URC_NO_REASON
294#else
295#define UNWIND_STOP _URC_NORMAL_STOP
296#define UNWIND_CONTINUE _URC_NO_REASON
297#endif
298
299uintptr_t Unwind_GetIP(struct _Unwind_Context *ctx) {
300#ifdef __arm__
301 uintptr_t val;
302 _Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
303 15 /* r15 = PC */, _UVRSD_UINT32, &val);
304 CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
305 // Clear the Thumb bit.
306 return val & ~(uintptr_t)1;
307#else
308 return _Unwind_GetIP(ctx);
309#endif
310}
311
312_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx,
313 void *param) {
314 AsanStackTrace *b = (AsanStackTrace*)param;
315 CHECK(b->size < b->max_size);
316 uintptr_t pc = Unwind_GetIP(ctx);
317 b->trace[b->size++] = pc;
318 if (b->size == b->max_size) return UNWIND_STOP;
319 return UNWIND_CONTINUE;
320}
321
322void AsanStackTrace::GetStackTrace(size_t max_s, uintptr_t pc, uintptr_t bp) {
323 size = 0;
324 trace[0] = pc;
325 if ((max_s) > 1) {
326 max_size = max_s;
327#ifdef __arm__
328 _Unwind_Backtrace(Unwind_Trace, this);
329#else
330 FastUnwindStack(pc, bp);
331#endif
332 }
333}
334
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000335} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000336
337#endif // __linux__