blob: 70214c53cd0aaf0f1c4f8b6793793c05af633b62 [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 Serebryany9fd01e52012-01-09 18:53:15 +000067bool AsanInterceptsSignal(int signum) {
68 return signum == SIGSEGV && FLAG_handle_segv;
69}
70
Kostya Serebryanya7720962011-12-28 23:28:54 +000071static void *asan_mmap(void *addr, size_t length, int prot, int flags,
Kostya Serebryany019b76f2011-11-30 01:07:02 +000072 int fd, uint64_t offset) {
73# if __WORDSIZE == 64
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000074 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000075# else
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000076 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000077# endif
78}
79
Kostya Serebryany6c4bd802011-12-28 22:58:01 +000080void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) {
81 size = RoundUpTo(size, kPageSize);
82 void *res = asan_mmap(0, size,
83 PROT_READ | PROT_WRITE,
84 MAP_PRIVATE | MAP_ANON, -1, 0);
85 if (res == (void*)-1) {
86 OutOfMemoryMessageAndDie(mem_type, size);
87 }
88 return res;
89}
90
Kostya Serebryanya7720962011-12-28 23:28:54 +000091void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) {
92 return asan_mmap((void*)fixed_addr, size,
93 PROT_READ | PROT_WRITE,
94 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
95 0, 0);
96}
97
98void *AsanMmapFixedReserve(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,
102 0, 0);
103}
104
105void *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");
117 ASAN_DIE;
118 }
119}
120
121ssize_t AsanWrite(int fd, const void *buf, size_t count) {
122 return (ssize_t)syscall(__NR_write, fd, buf, count);
123}
124
125int AsanOpenReadonly(const char* filename) {
126 return open(filename, O_RDONLY);
127}
128
129ssize_t AsanRead(int fd, void *buf, size_t count) {
130 return (ssize_t)syscall(__NR_read, fd, buf, count);
131}
132
133int AsanClose(int fd) {
134 return close(fd);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000135}
136
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000137AsanProcMaps::AsanProcMaps() {
138 proc_self_maps_buff_len_ =
139 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
140 &proc_self_maps_buff_mmaped_size_, 1 << 20);
141 CHECK(proc_self_maps_buff_len_ > 0);
142 // AsanWrite(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
143 Reset();
144}
145
146AsanProcMaps::~AsanProcMaps() {
147 AsanUnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
148}
149
150void AsanProcMaps::Reset() {
151 current_ = proc_self_maps_buff_;
152}
153
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000154bool AsanProcMaps::Next(uintptr_t *start, uintptr_t *end,
155 uintptr_t *offset, char filename[],
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000156 size_t filename_size) {
157 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
158 if (current_ >= last) return false;
159 int consumed = 0;
160 char flags[10];
161 int major, minor;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000162 uintptr_t inode;
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000163 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
164 if (next_line == NULL)
165 next_line = last;
166 if (SScanf(current_,
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000167 "%lx-%lx %4s %lx %x:%x %ld %n",
Kostya Serebryanycd271f52012-01-05 00:44:33 +0000168 start, end, flags, offset, &major, &minor,
169 &inode, &consumed) != 7)
170 return false;
171 current_ += consumed;
172 // Skip spaces.
173 while (current_ < next_line && *current_ == ' ')
174 current_++;
175 // Fill in the filename.
176 size_t i = 0;
177 while (current_ < next_line) {
178 if (filename && i < filename_size - 1)
179 filename[i++] = *current_;
180 current_++;
181 }
182 if (filename && i < filename_size)
183 filename[i] = 0;
184 current_ = next_line + 1;
185 return true;
186}
187
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000188struct DlIterateData {
189 int count;
190 uintptr_t addr;
191 uintptr_t offset;
192 char *filename;
193 size_t filename_size;
194};
195
196static int dl_iterate_phdr_callback(struct dl_phdr_info *info,
197 size_t size, void *raw_data) {
198 DlIterateData *data = (DlIterateData*)raw_data;
199 int count = data->count++;
200 if (info->dlpi_addr > data->addr)
201 return 0;
202 if (count == 0) {
203 // The first item (the main executable) does not have a so name,
204 // but we can just read it from /proc/self/exe.
205 ssize_t path_len = readlink("/proc/self/exe",
206 data->filename, data->filename_size - 1);
207 data->filename[path_len] = 0;
208 } else {
209 CHECK(info->dlpi_name);
210 real_strncpy(data->filename, info->dlpi_name, data->filename_size);
211 }
212 data->offset = data->addr - info->dlpi_addr;
213 return 1;
214}
215
216// Gets the object name and the offset using dl_iterate_phdr.
217bool AsanProcMaps::GetObjectNameAndOffset(uintptr_t addr, uintptr_t *offset,
218 char filename[],
219 size_t filename_size) {
220 DlIterateData data;
221 data.count = 0;
222 data.addr = addr;
223 data.filename = filename;
224 data.filename_size = filename_size;
225 if (dl_iterate_phdr(dl_iterate_phdr_callback, &data)) {
226 *offset = data.offset;
227 return true;
228 }
229 return false;
230}
231
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000232void AsanThread::SetThreadStackTopAndBottom() {
233 if (tid() == 0) {
234 // This is the main thread. Libpthread may not be initialized yet.
235 struct rlimit rl;
236 CHECK(getrlimit(RLIMIT_STACK, &rl) == 0);
237
238 // Find the mapping that contains a stack variable.
239 AsanProcMaps proc_maps;
Kostya Serebryany3b7fb102012-01-05 23:50:34 +0000240 uintptr_t start, end, offset;
241 uintptr_t prev_end = 0;
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000242 while (proc_maps.Next(&start, &end, &offset, NULL, 0)) {
243 if ((uintptr_t)&rl < end)
244 break;
245 prev_end = end;
246 }
247 CHECK((uintptr_t)&rl >= start && (uintptr_t)&rl < end);
248
249 // Get stacksize from rlimit, but clip it so that it does not overlap
250 // with other mappings.
251 size_t stacksize = rl.rlim_cur;
252 if (stacksize > end - prev_end)
253 stacksize = end - prev_end;
254 if (stacksize > kMaxThreadStackSize)
255 stacksize = kMaxThreadStackSize;
256 stack_top_ = end;
257 stack_bottom_ = end - stacksize;
258 CHECK(AddrIsInStack((uintptr_t)&rl));
259 return;
260 }
261 pthread_attr_t attr;
262 CHECK(pthread_getattr_np(pthread_self(), &attr) == 0);
263 size_t stacksize = 0;
264 void *stackaddr = NULL;
265 pthread_attr_getstack(&attr, &stackaddr, &stacksize);
266 pthread_attr_destroy(&attr);
267
268 stack_top_ = (uintptr_t)stackaddr + stacksize;
269 stack_bottom_ = (uintptr_t)stackaddr;
270 // When running with unlimited stack size, we still want to set some limit.
271 // The unlimited stack size is caused by 'ulimit -s unlimited'.
272 // Also, for some reason, GNU make spawns subrocesses with unlimited stack.
273 if (stacksize > kMaxThreadStackSize) {
274 stack_bottom_ = stack_top_ - kMaxThreadStackSize;
275 }
276 CHECK(AddrIsInStack((uintptr_t)&attr));
277}
278
Kostya Serebryany2b087182012-01-06 02:12:25 +0000279void AsanDisableCoreDumper() {
280 struct rlimit nocore;
281 nocore.rlim_cur = 0;
282 nocore.rlim_max = 0;
283 setrlimit(RLIMIT_CORE, &nocore);
284}
Kostya Serebryany78d87d32012-01-05 01:07:27 +0000285
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000286} // namespace __asan
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000287
288#endif // __linux__