blob: 17e2e379cf3260cd995960851f135a1c848bae60 [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>
32
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
Kostya Serebryany019b76f2011-11-30 01:07:02 +000038namespace __asan {
39
40void *AsanDoesNotSupportStaticLinkage() {
41 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000042 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000043}
44
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000045void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
46#ifdef ANDROID
47 *pc = *sp = *bp = 0;
48#elif defined(__arm__)
49 ucontext_t *ucontext = (ucontext_t*)context;
50 *pc = ucontext->uc_mcontext.arm_pc;
51 *bp = ucontext->uc_mcontext.arm_fp;
52 *sp = ucontext->uc_mcontext.arm_sp;
53# elif defined(__x86_64__)
54 ucontext_t *ucontext = (ucontext_t*)context;
55 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
56 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
57 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
58# elif defined(__i386__)
59 ucontext_t *ucontext = (ucontext_t*)context;
60 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
61 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
62 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
63#else
64# error "Unsupported arch"
65#endif
66}
67
Kostya Serebryany9fd01e52012-01-09 18:53:15 +000068bool AsanInterceptsSignal(int signum) {
69 return signum == SIGSEGV && FLAG_handle_segv;
70}
71
Kostya Serebryanya7720962011-12-28 23:28:54 +000072static void *asan_mmap(void *addr, size_t length, int prot, int flags,
Kostya Serebryany019b76f2011-11-30 01:07:02 +000073 int fd, uint64_t offset) {
74# if __WORDSIZE == 64
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000075 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000076# else
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000077 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000078# endif
79}
80
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000081void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) {
82 size = RoundUpTo(size, kPageSize);
83 void *res = asan_mmap(0, size,
84 PROT_READ | PROT_WRITE,
85 MAP_PRIVATE | MAP_ANON, -1, 0);
86 if (res == (void*)-1) {
87 OutOfMemoryMessageAndDie(mem_type, size);
88 }
89 return res;
90}
91
Kostya Serebryanya7720962011-12-28 23:28:54 +000092void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
93 return asan_mmap((void*)fixed_addr, size,
94 PROT_READ | PROT_WRITE,
95 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
96 0, 0);
97}
98
99void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size) {
100 return asan_mmap((void*)fixed_addr, size,
101 PROT_READ | PROT_WRITE,
102 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
103 0, 0);
104}
105
106void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
107 return asan_mmap((void*)fixed_addr, size,
108 PROT_NONE,
109 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
110 0, 0);
111}
112
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000113void AsanUnmapOrDie(void *addr, size_t size) {
114 if (!addr || !size) return;
115 int res = syscall(__NR_munmap, addr, size);
116 if (res != 0) {
117 Report("Failed to unmap\n");
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000118 AsanDie();
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000119 }
120}
121
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000122size_t AsanWrite(int fd, const void *buf, size_t count) {
123 return (size_t)syscall(__NR_write, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000124}
125
126int AsanOpenReadonly(const char* filename) {
127 return open(filename, O_RDONLY);
128}
129
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000130size_t AsanRead(int fd, void *buf, size_t count) {
131 return (size_t)syscall(__NR_read, fd, buf, count);
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000132}
133
134int AsanClose(int fd) {
135 return close(fd);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000136}
137
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000138AsanProcMaps::AsanProcMaps() {
139 proc_self_maps_buff_len_ =
140 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
141 &proc_self_maps_buff_mmaped_size_, 1 << 20);
142 CHECK(proc_self_maps_buff_len_ > 0);
143 // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
144 Reset();
145}
146
147AsanProcMaps::~AsanProcMaps() {
148 AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
149}
150
151void AsanProcMaps::Reset() {
152 current_ = proc_self_maps_buff_;
153}
154
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000155bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end,
156 uintptr_t *offset, char filename[],
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000157 size_t filename_size) {
158 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
159 if (current_ >= last) return false;
160 int consumed = 0;
161 char flags[10];
162 int major, minor;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000163 uintptr_t inode;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000164 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
165 if (next_line == NULL)
166 next_line = last;
167 if (SScanf(current_,
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000168 "%lx-%lx %4s %lx %x:%x %ld %n",
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000169 start, end, flags, offset, &major, &minor,
170 &inode, &consumed) != 7)
171 return false;
172 current_ += consumed;
173 // Skip spaces.
174 while (current_ < next_line && *current_ == ' ')
175 current_++;
176 // Fill in the filename.
177 size_t i = 0;
178 while (current_ < next_line) {
179 if (filename && i < filename_size - 1)
180 filename[i++] = *current_;
181 current_++;
182 }
183 if (filename && i < filename_size)
184 filename[i] = 0;
185 current_ = next_line + 1;
186 return true;
187}
188
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000189struct DlIterateData {
190 int count;
191 uintptr_t addr;
192 uintptr_t offset;
193 char *filename;
194 size_t filename_size;
195};
196
197static int dl_iterate_phdr_callback(struct dl_phdr_info *info,
198 size_t size, void *raw_data) {
199 DlIterateData *data = (DlIterateData*)raw_data;
200 int count = data->count++;
201 if (info->dlpi_addr > data->addr)
202 return 0;
203 if (count == 0) {
204 // The first item (the main executable) does not have a so name,
205 // but we can just read it from /proc/self/exe.
Kostya Serebryanyedb4a8a2012-01-09 23:11:26 +0000206 size_t path_len = readlink("/proc/self/exe",
207 data->filename, data->filename_size - 1);
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000208 data->filename[path_len] = 0;
209 } else {
210 CHECK(info->dlpi_name);
211 real_strncpy(data->filename, info->dlpi_name, data->filename_size);
212 }
213 data->offset = data->addr - info->dlpi_addr;
214 return 1;
215}
216
217// Gets the object name and the offset using dl_iterate_phdr.
218bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
219 char filename[],
220 size_t filename_size) {
221 DlIterateData data;
222 data.count = 0;
223 data.addr = addr;
224 data.filename = filename;
225 data.filename_size = filename_size;
226 if (dl_iterate_phdr(dl_iterate_phdr_callback, &data)) {
227 *offset = data.offset;
228 return true;
229 }
230 return false;
231}
232
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000233void AsanThread::SetThreadStackTopAndBottom() {
234 if (tid() == 0) {
235 // This is the main thread. Libpthread may not be initialized yet.
236 struct rlimit rl;
237 CHECK(getrlimit(RLIMIT_STACK, &rl) == 0);
238
239 // Find the mapping that contains a stack variable.
240 AsanProcMaps proc_maps;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000241 uintptr_t start, end, offset;
242 uintptr_t prev_end = 0;
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000243 while (proc_maps.Next(&start, &end, &offset, NULL, 0)) {
244 if ((uintptr_t)&rl < end)
245 break;
246 prev_end = end;
247 }
248 CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end);
249
250 // Get stacksize from rlimit, but clip it so that it does not overlap
251 // with other mappings.
252 size_t stacksize = rl.rlim_cur;
253 if (stacksize > end - prev_end)
254 stacksize = end - prev_end;
255 if (stacksize > kMaxThreadStackSize)
256 stacksize = kMaxThreadStackSize;
257 stack_top_ = end;
258 stack_bottom_ = end - stacksize;
259 CHECK(AddrIsInStack((uintptr_t)&rl));
260 return;
261 }
262 pthread_attr_t attr;
263 CHECK(pthread_getattr_np(pthread_self(), &attr) == 0);
264 size_t stacksize = 0;
265 void *stackaddr = NULL;
266 pthread_attr_getstack(&attr, &stackaddr, &stacksize);
267 pthread_attr_destroy(&attr);
268
269 stack_top_ = (uintptr_t)stackaddr + stacksize;
270 stack_bottom_ = (uintptr_t)stackaddr;
271 // When running with unlimited stack size, we still want to set some limit.
272 // The unlimited stack size is caused by 'ulimit -s unlimited'.
273 // Also, for some reason, GNU make spawns subrocesses with unlimited stack.
274 if (stacksize > kMaxThreadStackSize) {
275 stack_bottom_ = stack_top_ - kMaxThreadStackSize;
276 }
277 CHECK(AddrIsInStack((uintptr_t)&attr));
278}
279
Kostya Serebryanya82f0d42012-01-10 21:24:40 +0000280AsanLock::AsanLock(LinkerInitialized) {
281 // We assume that pthread_mutex_t initialized to all zeroes is a valid
282 // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers
283 // a gcc warning:
284 // extended initializer lists only available with -std=c++0x or -std=gnu++0x
285}
286
287void AsanLock::Lock() {
288 CHECK(sizeof(pthread_mutex_t) <= sizeof(opaque_storage_));
289 pthread_mutex_lock((pthread_mutex_t*)&opaque_storage_);
290 CHECK(!owner_);
291 owner_ = (uintptr_t)pthread_self();
292}
293
294void AsanLock::Unlock() {
295 CHECK(owner_ == (uintptr_t)pthread_self());
296 owner_ = 0;
297 pthread_mutex_unlock((pthread_mutex_t*)&opaque_storage_);
298}
299
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000300} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000301
302#endif // __linux__