blob: 2167efa4a8728e5ba591d13eeb228730f10e94a6 [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 Serebryanycd271f52012-01-05 00:44:33 +000018#include "asan_procmaps.h"
Kostya Serebryany78d87d32012-01-05 01:07:27 +000019#include "asan_thread.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000020
Kostya Serebryany78d87d32012-01-05 01:07:27 +000021#include <sys/time.h>
22#include <sys/resource.h>
Kostya Serebryany019b76f2011-11-30 01:07:02 +000023#include <sys/mman.h>
24#include <sys/syscall.h>
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000025#include <sys/types.h>
26#include <fcntl.h>
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000027#include <link.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>
31
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000032#ifndef ANDROID
33// FIXME: where to get ucontext on Android?
34#include <sys/ucontext.h>
35#endif
36
Kostya Serebryany019b76f2011-11-30 01:07:02 +000037namespace __asan {
38
39void *AsanDoesNotSupportStaticLinkage() {
40 // This will fail to link with -static.
Kostya Serebryany3b7fb102012-01-05 23:50:34 +000041 return &_DYNAMIC; // defined in link.h
Kostya Serebryany019b76f2011-11-30 01:07:02 +000042}
43
Kostya Serebryany25d6c1b2012-01-06 19:11:09 +000044void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) {
45#ifdef ANDROID
46 *pc = *sp = *bp = 0;
47#elif defined(__arm__)
48 ucontext_t *ucontext = (ucontext_t*)context;
49 *pc = ucontext->uc_mcontext.arm_pc;
50 *bp = ucontext->uc_mcontext.arm_fp;
51 *sp = ucontext->uc_mcontext.arm_sp;
52# elif defined(__x86_64__)
53 ucontext_t *ucontext = (ucontext_t*)context;
54 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
55 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
56 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
57# elif defined(__i386__)
58 ucontext_t *ucontext = (ucontext_t*)context;
59 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
60 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
61 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
62#else
63# error "Unsupported arch"
64#endif
65}
66
Kostya Serebryanya7720962011-12-28 23:28:54 +000067static void *asan_mmap(void *addr, size_t length, int prot, int flags,
Kostya Serebryany019b76f2011-11-30 01:07:02 +000068 int fd, uint64_t offset) {
69# if __WORDSIZE == 64
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000070 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000071# else
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000072 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000073# endif
74}
75
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000076void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) {
77 size = RoundUpTo(size, kPageSize);
78 void *res = asan_mmap(0, size,
79 PROT_READ | PROT_WRITE,
80 MAP_PRIVATE | MAP_ANON, -1, 0);
81 if (res == (void*)-1) {
82 OutOfMemoryMessageAndDie(mem_type, size);
83 }
84 return res;
85}
86
Kostya Serebryanya7720962011-12-28 23:28:54 +000087void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
88 return asan_mmap((void*)fixed_addr, size,
89 PROT_READ | PROT_WRITE,
90 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
91 0, 0);
92}
93
94void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size) {
95 return asan_mmap((void*)fixed_addr, size,
96 PROT_READ | PROT_WRITE,
97 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
98 0, 0);
99}
100
101void *AsanMprotect(uintptr_t fixed_addr, size_t size) {
102 return asan_mmap((void*)fixed_addr, size,
103 PROT_NONE,
104 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
105 0, 0);
106}
107
Kostya Serebryany6c4bd802011-12-28 22:58:01 +0000108void AsanUnmapOrDie(void *addr, size_t size) {
109 if (!addr || !size) return;
110 int res = syscall(__NR_munmap, addr, size);
111 if (res != 0) {
112 Report("Failed to unmap\n");
113 ASAN_DIE;
114 }
115}
116
117ssize_t AsanWrite(int fd, const void *buf, size_t count) {
118 return (ssize_t)syscall(__NR_write, fd, buf, count);
119}
120
121int AsanOpenReadonly(const char* filename) {
122 return open(filename, O_RDONLY);
123}
124
125ssize_t AsanRead(int fd, void *buf, size_t count) {
126 return (ssize_t)syscall(__NR_read, fd, buf, count);
127}
128
129int AsanClose(int fd) {
130 return close(fd);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000131}
132
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000133AsanProcMaps::AsanProcMaps() {
134 proc_self_maps_buff_len_ =
135 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
136 &proc_self_maps_buff_mmaped_size_, 1 << 20);
137 CHECK(proc_self_maps_buff_len_ > 0);
138 // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
139 Reset();
140}
141
142AsanProcMaps::~AsanProcMaps() {
143 AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
144}
145
146void AsanProcMaps::Reset() {
147 current_ = proc_self_maps_buff_;
148}
149
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000150bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end,
151 uintptr_t *offset, char filename[],
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000152 size_t filename_size) {
153 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
154 if (current_ >= last) return false;
155 int consumed = 0;
156 char flags[10];
157 int major, minor;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000158 uintptr_t inode;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000159 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
160 if (next_line == NULL)
161 next_line = last;
162 if (SScanf(current_,
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000163 "%lx-%lx %4s %lx %x:%x %ld %n",
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000164 start, end, flags, offset, &major, &minor,
165 &inode, &consumed) != 7)
166 return false;
167 current_ += consumed;
168 // Skip spaces.
169 while (current_ < next_line && *current_ == ' ')
170 current_++;
171 // Fill in the filename.
172 size_t i = 0;
173 while (current_ < next_line) {
174 if (filename && i < filename_size - 1)
175 filename[i++] = *current_;
176 current_++;
177 }
178 if (filename && i < filename_size)
179 filename[i] = 0;
180 current_ = next_line + 1;
181 return true;
182}
183
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000184struct DlIterateData {
185 int count;
186 uintptr_t addr;
187 uintptr_t offset;
188 char *filename;
189 size_t filename_size;
190};
191
192static int dl_iterate_phdr_callback(struct dl_phdr_info *info,
193 size_t size, void *raw_data) {
194 DlIterateData *data = (DlIterateData*)raw_data;
195 int count = data->count++;
196 if (info->dlpi_addr > data->addr)
197 return 0;
198 if (count == 0) {
199 // The first item (the main executable) does not have a so name,
200 // but we can just read it from /proc/self/exe.
201 ssize_t path_len = readlink("/proc/self/exe",
202 data->filename, data->filename_size - 1);
203 data->filename[path_len] = 0;
204 } else {
205 CHECK(info->dlpi_name);
206 real_strncpy(data->filename, info->dlpi_name, data->filename_size);
207 }
208 data->offset = data->addr - info->dlpi_addr;
209 return 1;
210}
211
212// Gets the object name and the offset using dl_iterate_phdr.
213bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
214 char filename[],
215 size_t filename_size) {
216 DlIterateData data;
217 data.count = 0;
218 data.addr = addr;
219 data.filename = filename;
220 data.filename_size = filename_size;
221 if (dl_iterate_phdr(dl_iterate_phdr_callback, &data)) {
222 *offset = data.offset;
223 return true;
224 }
225 return false;
226}
227
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000228void AsanThread::SetThreadStackTopAndBottom() {
229 if (tid() == 0) {
230 // This is the main thread. Libpthread may not be initialized yet.
231 struct rlimit rl;
232 CHECK(getrlimit(RLIMIT_STACK, &rl) == 0);
233
234 // Find the mapping that contains a stack variable.
235 AsanProcMaps proc_maps;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000236 uintptr_t start, end, offset;
237 uintptr_t prev_end = 0;
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000238 while (proc_maps.Next(&start, &end, &offset, NULL, 0)) {
239 if ((uintptr_t)&rl < end)
240 break;
241 prev_end = end;
242 }
243 CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end);
244
245 // Get stacksize from rlimit, but clip it so that it does not overlap
246 // with other mappings.
247 size_t stacksize = rl.rlim_cur;
248 if (stacksize > end - prev_end)
249 stacksize = end - prev_end;
250 if (stacksize > kMaxThreadStackSize)
251 stacksize = kMaxThreadStackSize;
252 stack_top_ = end;
253 stack_bottom_ = end - stacksize;
254 CHECK(AddrIsInStack((uintptr_t)&rl));
255 return;
256 }
257 pthread_attr_t attr;
258 CHECK(pthread_getattr_np(pthread_self(), &attr) == 0);
259 size_t stacksize = 0;
260 void *stackaddr = NULL;
261 pthread_attr_getstack(&attr, &stackaddr, &stacksize);
262 pthread_attr_destroy(&attr);
263
264 stack_top_ = (uintptr_t)stackaddr + stacksize;
265 stack_bottom_ = (uintptr_t)stackaddr;
266 // When running with unlimited stack size, we still want to set some limit.
267 // The unlimited stack size is caused by 'ulimit -s unlimited'.
268 // Also, for some reason, GNU make spawns subrocesses with unlimited stack.
269 if (stacksize > kMaxThreadStackSize) {
270 stack_bottom_ = stack_top_ - kMaxThreadStackSize;
271 }
272 CHECK(AddrIsInStack((uintptr_t)&attr));
273}
274
Kostya Serebryany2b087182012-01-06 02:12:25 +0000275void AsanDisableCoreDumper() {
276 struct rlimit nocore;
277 nocore.rlim_cur = 0;
278 nocore.rlim_max = 0;
279 setrlimit(RLIMIT_CORE, &nocore);
280}
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000281
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000282} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000283
284#endif // __linux__