blob: c2f8810eae41284da9115d00700ebf2b0d96c26d [file] [log] [blame]
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +00001//===-- sanitizer_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 shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries and implements linux-specific functions from
12// sanitizer_libc.h.
13//===----------------------------------------------------------------------===//
14#ifdef __linux__
15
Alexey Samsonov28a98952012-06-07 06:15:12 +000016#include "sanitizer_common.h"
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000017#include "sanitizer_internal_defs.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000018#include "sanitizer_libc.h"
Alexey Samsonov961276a2012-07-03 08:24:14 +000019#include "sanitizer_placement_new.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000020#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000021
Alexey Samsonovdde1f112012-06-05 07:05:10 +000022#include <fcntl.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000023#include <pthread.h>
Alexey Samsonov58a3c582012-06-18 08:44:30 +000024#include <sched.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000025#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000026#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000027#include <sys/stat.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000028#include <sys/syscall.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000029#include <sys/time.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000030#include <sys/types.h>
31#include <unistd.h>
32
33namespace __sanitizer {
34
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000035// --------------- sanitizer_libc.h
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000036void *internal_mmap(void *addr, uptr length, int prot, int flags,
37 int fd, u64 offset) {
38#if __WORDSIZE == 64
39 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
40#else
41 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
42#endif
43}
44
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000045int internal_munmap(void *addr, uptr length) {
46 return syscall(__NR_munmap, addr, length);
47}
48
Alexey Samsonov03c8b842012-06-05 08:32:53 +000049int internal_close(fd_t fd) {
50 return syscall(__NR_close, fd);
51}
52
Alexey Samsonovdde1f112012-06-05 07:05:10 +000053fd_t internal_open(const char *filename, bool write) {
54 return syscall(__NR_open, filename,
Kostya Serebryany64166ca2012-06-06 14:11:31 +000055 write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000056}
57
Alexey Samsonov03c8b842012-06-05 08:32:53 +000058uptr internal_read(fd_t fd, void *buf, uptr count) {
59 return (uptr)syscall(__NR_read, fd, buf, count);
60}
61
62uptr internal_write(fd_t fd, const void *buf, uptr count) {
63 return (uptr)syscall(__NR_write, fd, buf, count);
64}
65
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000066uptr internal_filesize(fd_t fd) {
Alexey Samsonov961276a2012-07-03 08:24:14 +000067#if __WORDSIZE == 64
68 struct stat st;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000069 if (syscall(__NR_fstat, fd, &st))
70 return -1;
Alexey Samsonov961276a2012-07-03 08:24:14 +000071#else
72 struct stat64 st;
73 if (syscall(__NR_fstat64, fd, &st))
74 return -1;
75#endif
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000076 return (uptr)st.st_size;
77}
78
79int internal_dup2(int oldfd, int newfd) {
80 return syscall(__NR_dup2, oldfd, newfd);
81}
82
Alexey Samsonov58a3c582012-06-18 08:44:30 +000083int internal_sched_yield() {
84 return syscall(__NR_sched_yield);
85}
86
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000087// ----------------- sanitizer_common.h
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000088void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000089 uptr *stack_bottom) {
90 static const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M
91 CHECK(stack_top);
92 CHECK(stack_bottom);
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000093 if (at_initialization) {
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000094 // This is the main thread. Libpthread may not be initialized yet.
95 struct rlimit rl;
Kostya Serebryany98390d02012-06-20 15:19:17 +000096 CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000097
98 // Find the mapping that contains a stack variable.
Alexey Samsonovcc622112012-08-27 13:48:48 +000099 MemoryMappingLayout proc_maps;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000100 uptr start, end, offset;
101 uptr prev_end = 0;
102 while (proc_maps.Next(&start, &end, &offset, 0, 0)) {
103 if ((uptr)&rl < end)
104 break;
105 prev_end = end;
106 }
107 CHECK((uptr)&rl >= start && (uptr)&rl < end);
108
109 // Get stacksize from rlimit, but clip it so that it does not overlap
110 // with other mappings.
111 uptr stacksize = rl.rlim_cur;
112 if (stacksize > end - prev_end)
113 stacksize = end - prev_end;
114 // When running with unlimited stack size, we still want to set some limit.
115 // The unlimited stack size is caused by 'ulimit -s unlimited'.
116 // Also, for some reason, GNU make spawns subprocesses with unlimited stack.
117 if (stacksize > kMaxThreadStackSize)
118 stacksize = kMaxThreadStackSize;
119 *stack_top = end;
120 *stack_bottom = end - stacksize;
121 return;
122 }
123 pthread_attr_t attr;
Kostya Serebryany98390d02012-06-20 15:19:17 +0000124 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000125 uptr stacksize = 0;
126 void *stackaddr = 0;
127 pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize);
128 pthread_attr_destroy(&attr);
129
130 *stack_top = (uptr)stackaddr + stacksize;
131 *stack_bottom = (uptr)stackaddr;
132 CHECK(stacksize < kMaxThreadStackSize); // Sanity check.
133}
134
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000135// Like getenv, but reads env directly from /proc and does not use libc.
136// This function should be called first inside __asan_init.
137const char *GetEnv(const char *name) {
138 static char *environ;
139 static uptr len;
140 static bool inited;
141 if (!inited) {
142 inited = true;
143 uptr environ_size;
144 len = ReadFileToBuffer("/proc/self/environ",
145 &environ, &environ_size, 1 << 26);
146 }
147 if (!environ || len == 0) return 0;
148 uptr namelen = internal_strlen(name);
149 const char *p = environ;
150 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
151 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
152 const char* endp =
153 (char*)internal_memchr(p, '\0', len - (p - environ));
154 if (endp == 0) // this entry isn't NUL terminated
155 return 0;
156 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
157 return p + namelen + 1; // point after =
158 p = endp + 1;
159 }
160 return 0; // Not found.
161}
162
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000163// ----------------- sanitizer_procmaps.h
Alexey Samsonovcc622112012-08-27 13:48:48 +0000164MemoryMappingLayout::MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000165 proc_self_maps_buff_len_ =
166 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
167 &proc_self_maps_buff_mmaped_size_, 1 << 26);
Kostya Serebryany98390d02012-06-20 15:19:17 +0000168 CHECK_GT(proc_self_maps_buff_len_, 0);
Alexey Samsonov28a98952012-06-07 06:15:12 +0000169 // internal_write(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
170 Reset();
171}
172
Alexey Samsonovcc622112012-08-27 13:48:48 +0000173MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000174 UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
175}
176
Alexey Samsonovcc622112012-08-27 13:48:48 +0000177void MemoryMappingLayout::Reset() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000178 current_ = proc_self_maps_buff_;
179}
180
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000181// Parse a hex value in str and update str.
182static uptr ParseHex(char **str) {
183 uptr x = 0;
184 char *s;
185 for (s = *str; ; s++) {
186 char c = *s;
187 uptr v = 0;
188 if (c >= '0' && c <= '9')
189 v = c - '0';
190 else if (c >= 'a' && c <= 'f')
191 v = c - 'a' + 10;
192 else if (c >= 'A' && c <= 'F')
193 v = c - 'A' + 10;
194 else
195 break;
196 x = x * 16 + v;
197 }
198 *str = s;
199 return x;
200}
201
202static bool IsOnOf(char c, char c1, char c2) {
203 return c == c1 || c == c2;
204}
205
206static bool IsDecimal(char c) {
207 return c >= '0' && c <= '9';
208}
209
Alexey Samsonovcc622112012-08-27 13:48:48 +0000210bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
211 char filename[], uptr filename_size) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000212 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
213 if (current_ >= last) return false;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000214 uptr dummy;
215 if (!start) start = &dummy;
216 if (!end) end = &dummy;
217 if (!offset) offset = &dummy;
218 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
219 if (next_line == 0)
220 next_line = last;
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000221 // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
222 *start = ParseHex(&current_);
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000223 CHECK_EQ(*current_++, '-');
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000224 *end = ParseHex(&current_);
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000225 CHECK_EQ(*current_++, ' ');
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000226 CHECK(IsOnOf(*current_++, '-', 'r'));
227 CHECK(IsOnOf(*current_++, '-', 'w'));
228 CHECK(IsOnOf(*current_++, '-', 'x'));
229 CHECK(IsOnOf(*current_++, 's', 'p'));
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000230 CHECK_EQ(*current_++, ' ');
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000231 *offset = ParseHex(&current_);
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000232 CHECK_EQ(*current_++, ' ');
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000233 ParseHex(&current_);
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000234 CHECK_EQ(*current_++, ':');
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000235 ParseHex(&current_);
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000236 CHECK_EQ(*current_++, ' ');
Kostya Serebryanyd32d5372012-06-29 13:05:36 +0000237 while (IsDecimal(*current_))
238 current_++;
Kostya Serebryany5766a9e2012-06-29 14:14:32 +0000239 CHECK_EQ(*current_++, ' ');
Alexey Samsonov28a98952012-06-07 06:15:12 +0000240 // Skip spaces.
241 while (current_ < next_line && *current_ == ' ')
242 current_++;
243 // Fill in the filename.
244 uptr i = 0;
245 while (current_ < next_line) {
246 if (filename && i < filename_size - 1)
247 filename[i++] = *current_;
248 current_++;
249 }
250 if (filename && i < filename_size)
251 filename[i] = 0;
252 current_ = next_line + 1;
253 return true;
254}
255
Alexey Samsonovcc622112012-08-27 13:48:48 +0000256// Gets the object name and the offset by walking MemoryMappingLayout.
257bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
258 char filename[],
259 uptr filename_size) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000260 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
261}
262
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000263} // namespace __sanitizer
264
265#endif // __linux__