1 //===-- 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
15 #include "sanitizer_platform.h"
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX
17
18 #include "sanitizer_allocator_internal.h"
19 #include "sanitizer_common.h"
20 #include "sanitizer_flags.h"
21 #include "sanitizer_internal_defs.h"
22 #include "sanitizer_libc.h"
23 #include "sanitizer_linux.h"
24 #include "sanitizer_mutex.h"
25 #include "sanitizer_placement_new.h"
26 #include "sanitizer_procmaps.h"
27 #include "sanitizer_stacktrace.h"
28 #include "sanitizer_symbolizer.h"
29
30 #if !SANITIZER_FREEBSD
31 #include <asm/param.h>
32 #endif
33
34 // For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
35 // format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
36 // access stat from asm/stat.h, without conflicting with definition in
37 // sys/stat.h, we use this trick.
38 #if defined(__mips64)
39 #include <sys/types.h>
40 #define stat kernel_stat
41 #include <asm/stat.h>
42 #undef stat
43 #endif
44
45 #include <dlfcn.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #if !SANITIZER_ANDROID
49 #include <link.h>
50 #endif
51 #include <pthread.h>
52 #include <sched.h>
53 #include <sys/mman.h>
54 #include <sys/ptrace.h>
55 #include <sys/resource.h>
56 #include <sys/stat.h>
57 #include <sys/syscall.h>
58 #include <sys/time.h>
59 #include <sys/types.h>
60 #include <ucontext.h>
61 #include <unistd.h>
62
63 #if SANITIZER_FREEBSD
64 #include <sys/sysctl.h>
65 #include <machine/atomic.h>
66 extern "C" {
67 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
68 // FreeBSD 9.2 and 10.0.
69 #include <sys/umtx.h>
70 }
71 extern char **environ; // provided by crt1
72 #endif // SANITIZER_FREEBSD
73
74 #if !SANITIZER_ANDROID
75 #include <sys/signal.h>
76 #endif
77
78 #if SANITIZER_ANDROID
79 #include <android/log.h>
80 #include <sys/system_properties.h>
81 #endif
82
83 #if SANITIZER_LINUX
84 // <linux/time.h>
85 struct kernel_timeval {
86 long tv_sec;
87 long tv_usec;
88 };
89
90 // <linux/futex.h> is broken on some linux distributions.
91 const int FUTEX_WAIT = 0;
92 const int FUTEX_WAKE = 1;
93 #endif // SANITIZER_LINUX
94
95 // Are we using 32-bit or 64-bit Linux syscalls?
96 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
97 // but it still needs to use 64-bit syscalls.
98 #if SANITIZER_LINUX && (defined(__x86_64__) || SANITIZER_WORDSIZE == 64)
99 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
100 #else
101 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
102 #endif
103
104 namespace __sanitizer {
105
106 #if SANITIZER_LINUX && defined(__x86_64__)
107 #include "sanitizer_syscall_linux_x86_64.inc"
108 #else
109 #include "sanitizer_syscall_generic.inc"
110 #endif
111
112 // --------------- sanitizer_libc.h
internal_mmap(void * addr,uptr length,int prot,int flags,int fd,u64 offset)113 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
114 u64 offset) {
115 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
116 return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
117 offset);
118 #else
119 // mmap2 specifies file offset in 4096-byte units.
120 CHECK(IsAligned(offset, 4096));
121 return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
122 offset / 4096);
123 #endif
124 }
125
internal_munmap(void * addr,uptr length)126 uptr internal_munmap(void *addr, uptr length) {
127 return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
128 }
129
internal_mprotect(void * addr,uptr length,int prot)130 int internal_mprotect(void *addr, uptr length, int prot) {
131 return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot);
132 }
133
internal_close(fd_t fd)134 uptr internal_close(fd_t fd) {
135 return internal_syscall(SYSCALL(close), fd);
136 }
137
internal_open(const char * filename,int flags)138 uptr internal_open(const char *filename, int flags) {
139 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
140 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
141 #else
142 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
143 #endif
144 }
145
internal_open(const char * filename,int flags,u32 mode)146 uptr internal_open(const char *filename, int flags, u32 mode) {
147 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
148 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
149 mode);
150 #else
151 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
152 #endif
153 }
154
internal_read(fd_t fd,void * buf,uptr count)155 uptr internal_read(fd_t fd, void *buf, uptr count) {
156 sptr res;
157 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf,
158 count));
159 return res;
160 }
161
internal_write(fd_t fd,const void * buf,uptr count)162 uptr internal_write(fd_t fd, const void *buf, uptr count) {
163 sptr res;
164 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf,
165 count));
166 return res;
167 }
168
internal_ftruncate(fd_t fd,uptr size)169 uptr internal_ftruncate(fd_t fd, uptr size) {
170 sptr res;
171 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd,
172 (OFF_T)size));
173 return res;
174 }
175
176 #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && !SANITIZER_FREEBSD
stat64_to_stat(struct stat64 * in,struct stat * out)177 static void stat64_to_stat(struct stat64 *in, struct stat *out) {
178 internal_memset(out, 0, sizeof(*out));
179 out->st_dev = in->st_dev;
180 out->st_ino = in->st_ino;
181 out->st_mode = in->st_mode;
182 out->st_nlink = in->st_nlink;
183 out->st_uid = in->st_uid;
184 out->st_gid = in->st_gid;
185 out->st_rdev = in->st_rdev;
186 out->st_size = in->st_size;
187 out->st_blksize = in->st_blksize;
188 out->st_blocks = in->st_blocks;
189 out->st_atime = in->st_atime;
190 out->st_mtime = in->st_mtime;
191 out->st_ctime = in->st_ctime;
192 out->st_ino = in->st_ino;
193 }
194 #endif
195
196 #if defined(__mips64)
kernel_stat_to_stat(struct kernel_stat * in,struct stat * out)197 static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) {
198 internal_memset(out, 0, sizeof(*out));
199 out->st_dev = in->st_dev;
200 out->st_ino = in->st_ino;
201 out->st_mode = in->st_mode;
202 out->st_nlink = in->st_nlink;
203 out->st_uid = in->st_uid;
204 out->st_gid = in->st_gid;
205 out->st_rdev = in->st_rdev;
206 out->st_size = in->st_size;
207 out->st_blksize = in->st_blksize;
208 out->st_blocks = in->st_blocks;
209 out->st_atime = in->st_atime_nsec;
210 out->st_mtime = in->st_mtime_nsec;
211 out->st_ctime = in->st_ctime_nsec;
212 out->st_ino = in->st_ino;
213 }
214 #endif
215
internal_stat(const char * path,void * buf)216 uptr internal_stat(const char *path, void *buf) {
217 #if SANITIZER_FREEBSD
218 return internal_syscall(SYSCALL(stat), path, buf);
219 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
220 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
221 (uptr)buf, 0);
222 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
223 # if defined(__mips64)
224 // For mips64, stat syscall fills buffer in the format of kernel_stat
225 struct kernel_stat kbuf;
226 int res = internal_syscall(SYSCALL(stat), path, &kbuf);
227 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
228 return res;
229 # else
230 return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf);
231 # endif
232 #else
233 struct stat64 buf64;
234 int res = internal_syscall(SYSCALL(stat64), path, &buf64);
235 stat64_to_stat(&buf64, (struct stat *)buf);
236 return res;
237 #endif
238 }
239
internal_lstat(const char * path,void * buf)240 uptr internal_lstat(const char *path, void *buf) {
241 #if SANITIZER_FREEBSD
242 return internal_syscall(SYSCALL(lstat), path, buf);
243 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
244 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
245 (uptr)buf, AT_SYMLINK_NOFOLLOW);
246 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
247 return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf);
248 #else
249 struct stat64 buf64;
250 int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
251 stat64_to_stat(&buf64, (struct stat *)buf);
252 return res;
253 #endif
254 }
255
internal_fstat(fd_t fd,void * buf)256 uptr internal_fstat(fd_t fd, void *buf) {
257 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
258 return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
259 #else
260 struct stat64 buf64;
261 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
262 stat64_to_stat(&buf64, (struct stat *)buf);
263 return res;
264 #endif
265 }
266
internal_filesize(fd_t fd)267 uptr internal_filesize(fd_t fd) {
268 struct stat st;
269 if (internal_fstat(fd, &st))
270 return -1;
271 return (uptr)st.st_size;
272 }
273
internal_dup2(int oldfd,int newfd)274 uptr internal_dup2(int oldfd, int newfd) {
275 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
276 return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
277 #else
278 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
279 #endif
280 }
281
internal_readlink(const char * path,char * buf,uptr bufsize)282 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
283 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
284 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD,
285 (uptr)path, (uptr)buf, bufsize);
286 #else
287 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
288 #endif
289 }
290
internal_unlink(const char * path)291 uptr internal_unlink(const char *path) {
292 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
293 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
294 #else
295 return internal_syscall(SYSCALL(unlink), (uptr)path);
296 #endif
297 }
298
internal_rename(const char * oldpath,const char * newpath)299 uptr internal_rename(const char *oldpath, const char *newpath) {
300 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
301 return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
302 (uptr)newpath);
303 #else
304 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
305 #endif
306 }
307
internal_sched_yield()308 uptr internal_sched_yield() {
309 return internal_syscall(SYSCALL(sched_yield));
310 }
311
internal__exit(int exitcode)312 void internal__exit(int exitcode) {
313 #if SANITIZER_FREEBSD
314 internal_syscall(SYSCALL(exit), exitcode);
315 #else
316 internal_syscall(SYSCALL(exit_group), exitcode);
317 #endif
318 Die(); // Unreachable.
319 }
320
internal_execve(const char * filename,char * const argv[],char * const envp[])321 uptr internal_execve(const char *filename, char *const argv[],
322 char *const envp[]) {
323 return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv,
324 (uptr)envp);
325 }
326
327 // ----------------- sanitizer_common.h
FileExists(const char * filename)328 bool FileExists(const char *filename) {
329 struct stat st;
330 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
331 if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0))
332 #else
333 if (internal_stat(filename, &st))
334 #endif
335 return false;
336 // Sanity check: filename is a regular file.
337 return S_ISREG(st.st_mode);
338 }
339
GetTid()340 uptr GetTid() {
341 #if SANITIZER_FREEBSD
342 return (uptr)pthread_self();
343 #else
344 return internal_syscall(SYSCALL(gettid));
345 #endif
346 }
347
NanoTime()348 u64 NanoTime() {
349 #if SANITIZER_FREEBSD
350 timeval tv;
351 #else
352 kernel_timeval tv;
353 #endif
354 internal_memset(&tv, 0, sizeof(tv));
355 internal_syscall(SYSCALL(gettimeofday), (uptr)&tv, 0);
356 return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
357 }
358
359 // Like getenv, but reads env directly from /proc (on Linux) or parses the
360 // 'environ' array (on FreeBSD) and does not use libc. This function should be
361 // called first inside __asan_init.
GetEnv(const char * name)362 const char *GetEnv(const char *name) {
363 #if SANITIZER_FREEBSD
364 if (::environ != 0) {
365 uptr NameLen = internal_strlen(name);
366 for (char **Env = ::environ; *Env != 0; Env++) {
367 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
368 return (*Env) + NameLen + 1;
369 }
370 }
371 return 0; // Not found.
372 #elif SANITIZER_LINUX
373 static char *environ;
374 static uptr len;
375 static bool inited;
376 if (!inited) {
377 inited = true;
378 uptr environ_size;
379 len = ReadFileToBuffer("/proc/self/environ",
380 &environ, &environ_size, 1 << 26);
381 }
382 if (!environ || len == 0) return 0;
383 uptr namelen = internal_strlen(name);
384 const char *p = environ;
385 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
386 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
387 const char* endp =
388 (char*)internal_memchr(p, '\0', len - (p - environ));
389 if (endp == 0) // this entry isn't NUL terminated
390 return 0;
391 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
392 return p + namelen + 1; // point after =
393 p = endp + 1;
394 }
395 return 0; // Not found.
396 #else
397 #error "Unsupported platform"
398 #endif
399 }
400
401 extern "C" {
402 SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
403 }
404
405 #if !SANITIZER_GO
ReadNullSepFileToArray(const char * path,char *** arr,int arr_size)406 static void ReadNullSepFileToArray(const char *path, char ***arr,
407 int arr_size) {
408 char *buff;
409 uptr buff_size = 0;
410 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
411 ReadFileToBuffer(path, &buff, &buff_size, 1024 * 1024);
412 (*arr)[0] = buff;
413 int count, i;
414 for (count = 1, i = 1; ; i++) {
415 if (buff[i] == 0) {
416 if (buff[i+1] == 0) break;
417 (*arr)[count] = &buff[i+1];
418 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
419 count++;
420 }
421 }
422 (*arr)[count] = 0;
423 }
424 #endif
425
GetArgsAndEnv(char *** argv,char *** envp)426 static void GetArgsAndEnv(char*** argv, char*** envp) {
427 #if !SANITIZER_GO
428 if (&__libc_stack_end) {
429 #endif
430 uptr* stack_end = (uptr*)__libc_stack_end;
431 int argc = *stack_end;
432 *argv = (char**)(stack_end + 1);
433 *envp = (char**)(stack_end + argc + 2);
434 #if !SANITIZER_GO
435 } else {
436 static const int kMaxArgv = 2000, kMaxEnvp = 2000;
437 ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
438 ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
439 }
440 #endif
441 }
442
ReExec()443 void ReExec() {
444 char **argv, **envp;
445 GetArgsAndEnv(&argv, &envp);
446 uptr rv = internal_execve("/proc/self/exe", argv, envp);
447 int rverrno;
448 CHECK_EQ(internal_iserror(rv, &rverrno), true);
449 Printf("execve failed, errno %d\n", rverrno);
450 Die();
451 }
452
453 enum MutexState {
454 MtxUnlocked = 0,
455 MtxLocked = 1,
456 MtxSleeping = 2
457 };
458
BlockingMutex()459 BlockingMutex::BlockingMutex() {
460 internal_memset(this, 0, sizeof(*this));
461 }
462
Lock()463 void BlockingMutex::Lock() {
464 CHECK_EQ(owner_, 0);
465 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
466 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
467 return;
468 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
469 #if SANITIZER_FREEBSD
470 _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
471 #else
472 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
473 #endif
474 }
475 }
476
Unlock()477 void BlockingMutex::Unlock() {
478 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
479 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
480 CHECK_NE(v, MtxUnlocked);
481 if (v == MtxSleeping) {
482 #if SANITIZER_FREEBSD
483 _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
484 #else
485 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE, 1, 0, 0, 0);
486 #endif
487 }
488 }
489
CheckLocked()490 void BlockingMutex::CheckLocked() {
491 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
492 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
493 }
494
495 // ----------------- sanitizer_linux.h
496 // The actual size of this structure is specified by d_reclen.
497 // Note that getdents64 uses a different structure format. We only provide the
498 // 32-bit syscall here.
499 struct linux_dirent {
500 #if SANITIZER_X32
501 u64 d_ino;
502 u64 d_off;
503 #else
504 unsigned long d_ino;
505 unsigned long d_off;
506 #endif
507 unsigned short d_reclen;
508 char d_name[256];
509 };
510
511 // Syscall wrappers.
internal_ptrace(int request,int pid,void * addr,void * data)512 uptr internal_ptrace(int request, int pid, void *addr, void *data) {
513 return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
514 (uptr)data);
515 }
516
internal_waitpid(int pid,int * status,int options)517 uptr internal_waitpid(int pid, int *status, int options) {
518 return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
519 0 /* rusage */);
520 }
521
internal_getpid()522 uptr internal_getpid() {
523 return internal_syscall(SYSCALL(getpid));
524 }
525
internal_getppid()526 uptr internal_getppid() {
527 return internal_syscall(SYSCALL(getppid));
528 }
529
internal_getdents(fd_t fd,struct linux_dirent * dirp,unsigned int count)530 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
531 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
532 return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
533 #else
534 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
535 #endif
536 }
537
internal_lseek(fd_t fd,OFF_T offset,int whence)538 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
539 return internal_syscall(SYSCALL(lseek), fd, offset, whence);
540 }
541
542 #if SANITIZER_LINUX
internal_prctl(int option,uptr arg2,uptr arg3,uptr arg4,uptr arg5)543 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
544 return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
545 }
546 #endif
547
internal_sigaltstack(const struct sigaltstack * ss,struct sigaltstack * oss)548 uptr internal_sigaltstack(const struct sigaltstack *ss,
549 struct sigaltstack *oss) {
550 return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
551 }
552
internal_fork()553 int internal_fork() {
554 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
555 return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
556 #else
557 return internal_syscall(SYSCALL(fork));
558 #endif
559 }
560
561 #if SANITIZER_LINUX
562 #define SA_RESTORER 0x04000000
563 // Doesn't set sa_restorer, use with caution (see below).
internal_sigaction_norestorer(int signum,const void * act,void * oldact)564 int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
565 __sanitizer_kernel_sigaction_t k_act, k_oldact;
566 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
567 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
568 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
569 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
570 if (u_act) {
571 k_act.handler = u_act->handler;
572 k_act.sigaction = u_act->sigaction;
573 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
574 sizeof(__sanitizer_kernel_sigset_t));
575 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
576 k_act.sa_flags = u_act->sa_flags | SA_RESTORER;
577 // FIXME: most often sa_restorer is unset, however the kernel requires it
578 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
579 // If sa_restorer passed to the kernel is NULL, the program may crash upon
580 // signal delivery or fail to unwind the stack in the signal handler.
581 // libc implementation of sigaction() passes its own restorer to
582 // rt_sigaction, so we need to do the same (we'll need to reimplement the
583 // restorers; for x86_64 the restorer address can be obtained from
584 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
585 k_act.sa_restorer = u_act->sa_restorer;
586 }
587
588 uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
589 (uptr)(u_act ? &k_act : NULL),
590 (uptr)(u_oldact ? &k_oldact : NULL),
591 (uptr)sizeof(__sanitizer_kernel_sigset_t));
592
593 if ((result == 0) && u_oldact) {
594 u_oldact->handler = k_oldact.handler;
595 u_oldact->sigaction = k_oldact.sigaction;
596 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
597 sizeof(__sanitizer_kernel_sigset_t));
598 u_oldact->sa_flags = k_oldact.sa_flags;
599 u_oldact->sa_restorer = k_oldact.sa_restorer;
600 }
601 return result;
602 }
603 #endif // SANITIZER_LINUX
604
internal_sigprocmask(int how,__sanitizer_sigset_t * set,__sanitizer_sigset_t * oldset)605 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
606 __sanitizer_sigset_t *oldset) {
607 #if SANITIZER_FREEBSD
608 return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
609 #else
610 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
611 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
612 return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how,
613 (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
614 sizeof(__sanitizer_kernel_sigset_t));
615 #endif
616 }
617
internal_sigfillset(__sanitizer_sigset_t * set)618 void internal_sigfillset(__sanitizer_sigset_t *set) {
619 internal_memset(set, 0xff, sizeof(*set));
620 }
621
622 #if SANITIZER_LINUX
internal_sigdelset(__sanitizer_sigset_t * set,int signum)623 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
624 signum -= 1;
625 CHECK_GE(signum, 0);
626 CHECK_LT(signum, sizeof(*set) * 8);
627 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
628 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
629 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
630 k_set->sig[idx] &= ~(1 << bit);
631 }
632 #endif // SANITIZER_LINUX
633
634 // ThreadLister implementation.
ThreadLister(int pid)635 ThreadLister::ThreadLister(int pid)
636 : pid_(pid),
637 descriptor_(-1),
638 buffer_(4096),
639 error_(true),
640 entry_((struct linux_dirent *)buffer_.data()),
641 bytes_read_(0) {
642 char task_directory_path[80];
643 internal_snprintf(task_directory_path, sizeof(task_directory_path),
644 "/proc/%d/task/", pid);
645 uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
646 if (internal_iserror(openrv)) {
647 error_ = true;
648 Report("Can't open /proc/%d/task for reading.\n", pid);
649 } else {
650 error_ = false;
651 descriptor_ = openrv;
652 }
653 }
654
GetNextTID()655 int ThreadLister::GetNextTID() {
656 int tid = -1;
657 do {
658 if (error_)
659 return -1;
660 if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
661 return -1;
662 if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
663 entry_->d_name[0] <= '9') {
664 // Found a valid tid.
665 tid = (int)internal_atoll(entry_->d_name);
666 }
667 entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
668 } while (tid < 0);
669 return tid;
670 }
671
Reset()672 void ThreadLister::Reset() {
673 if (error_ || descriptor_ < 0)
674 return;
675 internal_lseek(descriptor_, 0, SEEK_SET);
676 }
677
~ThreadLister()678 ThreadLister::~ThreadLister() {
679 if (descriptor_ >= 0)
680 internal_close(descriptor_);
681 }
682
error()683 bool ThreadLister::error() { return error_; }
684
GetDirectoryEntries()685 bool ThreadLister::GetDirectoryEntries() {
686 CHECK_GE(descriptor_, 0);
687 CHECK_NE(error_, true);
688 bytes_read_ = internal_getdents(descriptor_,
689 (struct linux_dirent *)buffer_.data(),
690 buffer_.size());
691 if (internal_iserror(bytes_read_)) {
692 Report("Can't read directory entries from /proc/%d/task.\n", pid_);
693 error_ = true;
694 return false;
695 } else if (bytes_read_ == 0) {
696 return false;
697 }
698 entry_ = (struct linux_dirent *)buffer_.data();
699 return true;
700 }
701
GetPageSize()702 uptr GetPageSize() {
703 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
704 return EXEC_PAGESIZE;
705 #else
706 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
707 #endif
708 }
709
710 static char proc_self_exe_cache_str[kMaxPathLength];
711 static uptr proc_self_exe_cache_len = 0;
712
ReadBinaryName(char * buf,uptr buf_len)713 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
714 if (proc_self_exe_cache_len > 0) {
715 // If available, use the cached module name.
716 uptr module_name_len =
717 internal_snprintf(buf, buf_len, "%s", proc_self_exe_cache_str);
718 CHECK_LT(module_name_len, buf_len);
719 return module_name_len;
720 }
721 #if SANITIZER_FREEBSD
722 const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
723 size_t Size = buf_len;
724 bool IsErr = (sysctl(Mib, 4, buf, &Size, NULL, 0) != 0);
725 int readlink_error = IsErr ? errno : 0;
726 uptr module_name_len = Size;
727 #else
728 uptr module_name_len = internal_readlink(
729 "/proc/self/exe", buf, buf_len);
730 int readlink_error;
731 bool IsErr = internal_iserror(module_name_len, &readlink_error);
732 #endif
733 if (IsErr) {
734 // We can't read /proc/self/exe for some reason, assume the name of the
735 // binary is unknown.
736 Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, "
737 "some stack frames may not be symbolized\n", readlink_error);
738 module_name_len = internal_snprintf(buf, buf_len, "/proc/self/exe");
739 CHECK_LT(module_name_len, buf_len);
740 }
741 return module_name_len;
742 }
743
CacheBinaryName()744 void CacheBinaryName() {
745 if (!proc_self_exe_cache_len) {
746 proc_self_exe_cache_len =
747 ReadBinaryName(proc_self_exe_cache_str, kMaxPathLength);
748 }
749 }
750
751 // Match full names of the form /path/to/base_name{-,.}*
LibraryNameIs(const char * full_name,const char * base_name)752 bool LibraryNameIs(const char *full_name, const char *base_name) {
753 const char *name = full_name;
754 // Strip path.
755 while (*name != '\0') name++;
756 while (name > full_name && *name != '/') name--;
757 if (*name == '/') name++;
758 uptr base_name_length = internal_strlen(base_name);
759 if (internal_strncmp(name, base_name, base_name_length)) return false;
760 return (name[base_name_length] == '-' || name[base_name_length] == '.');
761 }
762
763 #if !SANITIZER_ANDROID
764 // Call cb for each region mapped by map.
ForEachMappedRegion(link_map * map,void (* cb)(const void *,uptr))765 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
766 CHECK_NE(map, nullptr);
767 #if !SANITIZER_FREEBSD
768 typedef ElfW(Phdr) Elf_Phdr;
769 typedef ElfW(Ehdr) Elf_Ehdr;
770 #endif // !SANITIZER_FREEBSD
771 char *base = (char *)map->l_addr;
772 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
773 char *phdrs = base + ehdr->e_phoff;
774 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
775
776 // Find the segment with the minimum base so we can "relocate" the p_vaddr
777 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
778 // objects have a non-zero base.
779 uptr preferred_base = (uptr)-1;
780 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
781 Elf_Phdr *phdr = (Elf_Phdr *)iter;
782 if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
783 preferred_base = (uptr)phdr->p_vaddr;
784 }
785
786 // Compute the delta from the real base to get a relocation delta.
787 sptr delta = (uptr)base - preferred_base;
788 // Now we can figure out what the loader really mapped.
789 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
790 Elf_Phdr *phdr = (Elf_Phdr *)iter;
791 if (phdr->p_type == PT_LOAD) {
792 uptr seg_start = phdr->p_vaddr + delta;
793 uptr seg_end = seg_start + phdr->p_memsz;
794 // None of these values are aligned. We consider the ragged edges of the
795 // load command as defined, since they are mapped from the file.
796 seg_start = RoundDownTo(seg_start, GetPageSizeCached());
797 seg_end = RoundUpTo(seg_end, GetPageSizeCached());
798 cb((void *)seg_start, seg_end - seg_start);
799 }
800 }
801 }
802 #endif
803
804 #if defined(__x86_64__) && SANITIZER_LINUX
805 // We cannot use glibc's clone wrapper, because it messes with the child
806 // task's TLS. It writes the PID and TID of the child task to its thread
807 // descriptor, but in our case the child task shares the thread descriptor with
808 // the parent (because we don't know how to allocate a new thread
809 // descriptor to keep glibc happy). So the stock version of clone(), when
810 // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
internal_clone(int (* fn)(void *),void * child_stack,int flags,void * arg,int * parent_tidptr,void * newtls,int * child_tidptr)811 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
812 int *parent_tidptr, void *newtls, int *child_tidptr) {
813 long long res;
814 if (!fn || !child_stack)
815 return -EINVAL;
816 CHECK_EQ(0, (uptr)child_stack % 16);
817 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
818 ((unsigned long long *)child_stack)[0] = (uptr)fn;
819 ((unsigned long long *)child_stack)[1] = (uptr)arg;
820 register void *r8 __asm__("r8") = newtls;
821 register int *r10 __asm__("r10") = child_tidptr;
822 __asm__ __volatile__(
823 /* %rax = syscall(%rax = SYSCALL(clone),
824 * %rdi = flags,
825 * %rsi = child_stack,
826 * %rdx = parent_tidptr,
827 * %r8 = new_tls,
828 * %r10 = child_tidptr)
829 */
830 "syscall\n"
831
832 /* if (%rax != 0)
833 * return;
834 */
835 "testq %%rax,%%rax\n"
836 "jnz 1f\n"
837
838 /* In the child. Terminate unwind chain. */
839 // XXX: We should also terminate the CFI unwind chain
840 // here. Unfortunately clang 3.2 doesn't support the
841 // necessary CFI directives, so we skip that part.
842 "xorq %%rbp,%%rbp\n"
843
844 /* Call "fn(arg)". */
845 "popq %%rax\n"
846 "popq %%rdi\n"
847 "call *%%rax\n"
848
849 /* Call _exit(%rax). */
850 "movq %%rax,%%rdi\n"
851 "movq %2,%%rax\n"
852 "syscall\n"
853
854 /* Return to parent. */
855 "1:\n"
856 : "=a" (res)
857 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
858 "S"(child_stack),
859 "D"(flags),
860 "d"(parent_tidptr),
861 "r"(r8),
862 "r"(r10)
863 : "rsp", "memory", "r11", "rcx");
864 return res;
865 }
866 #elif defined(__mips__)
867 // TODO(sagarthakur): clone function is to be rewritten in assembly.
internal_clone(int (* fn)(void *),void * child_stack,int flags,void * arg,int * parent_tidptr,void * newtls,int * child_tidptr)868 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
869 int *parent_tidptr, void *newtls, int *child_tidptr) {
870 return clone(fn, child_stack, flags, arg, parent_tidptr,
871 newtls, child_tidptr);
872 }
873 #endif // defined(__x86_64__) && SANITIZER_LINUX
874
875 #if SANITIZER_ANDROID
876 static atomic_uint8_t android_log_initialized;
877
AndroidLogInit()878 void AndroidLogInit() {
879 atomic_store(&android_log_initialized, 1, memory_order_release);
880 }
881 // This thing is not, strictly speaking, async signal safe, but it does not seem
882 // to cause any issues. Alternative is writing to log devices directly, but
883 // their location and message format might change in the future, so we'd really
884 // like to avoid that.
AndroidLogWrite(const char * buffer)885 void AndroidLogWrite(const char *buffer) {
886 if (!atomic_load(&android_log_initialized, memory_order_acquire))
887 return;
888
889 char *copy = internal_strdup(buffer);
890 char *p = copy;
891 char *q;
892 // __android_log_write has an implicit message length limit.
893 // Print one line at a time.
894 do {
895 q = internal_strchr(p, '\n');
896 if (q) *q = '\0';
897 __android_log_write(ANDROID_LOG_INFO, NULL, p);
898 if (q) p = q + 1;
899 } while (q);
900 InternalFree(copy);
901 }
902
GetExtraActivationFlags(char * buf,uptr size)903 void GetExtraActivationFlags(char *buf, uptr size) {
904 CHECK(size > PROP_VALUE_MAX);
905 __system_property_get("asan.options", buf);
906 }
907 #endif
908
IsDeadlySignal(int signum)909 bool IsDeadlySignal(int signum) {
910 return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv;
911 }
912
913 #ifndef SANITIZER_GO
internal_start_thread(void (* func)(void * arg),void * arg)914 void *internal_start_thread(void(*func)(void *arg), void *arg) {
915 // Start the thread with signals blocked, otherwise it can steal user signals.
916 __sanitizer_sigset_t set, old;
917 internal_sigfillset(&set);
918 internal_sigprocmask(SIG_SETMASK, &set, &old);
919 void *th;
920 real_pthread_create(&th, 0, (void*(*)(void *arg))func, arg);
921 internal_sigprocmask(SIG_SETMASK, &old, 0);
922 return th;
923 }
924
internal_join_thread(void * th)925 void internal_join_thread(void *th) {
926 real_pthread_join(th, 0);
927 }
928 #else
internal_start_thread(void (* func)(void *),void * arg)929 void *internal_start_thread(void (*func)(void *), void *arg) { return 0; }
930
internal_join_thread(void * th)931 void internal_join_thread(void *th) {}
932 #endif
933
GetPcSpBp(void * context,uptr * pc,uptr * sp,uptr * bp)934 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
935 #if defined(__arm__)
936 ucontext_t *ucontext = (ucontext_t*)context;
937 *pc = ucontext->uc_mcontext.arm_pc;
938 *bp = ucontext->uc_mcontext.arm_fp;
939 *sp = ucontext->uc_mcontext.arm_sp;
940 #elif defined(__aarch64__)
941 ucontext_t *ucontext = (ucontext_t*)context;
942 *pc = ucontext->uc_mcontext.pc;
943 *bp = ucontext->uc_mcontext.regs[29];
944 *sp = ucontext->uc_mcontext.sp;
945 #elif defined(__hppa__)
946 ucontext_t *ucontext = (ucontext_t*)context;
947 *pc = ucontext->uc_mcontext.sc_iaoq[0];
948 /* GCC uses %r3 whenever a frame pointer is needed. */
949 *bp = ucontext->uc_mcontext.sc_gr[3];
950 *sp = ucontext->uc_mcontext.sc_gr[30];
951 #elif defined(__x86_64__)
952 # if SANITIZER_FREEBSD
953 ucontext_t *ucontext = (ucontext_t*)context;
954 *pc = ucontext->uc_mcontext.mc_rip;
955 *bp = ucontext->uc_mcontext.mc_rbp;
956 *sp = ucontext->uc_mcontext.mc_rsp;
957 # else
958 ucontext_t *ucontext = (ucontext_t*)context;
959 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
960 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
961 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
962 # endif
963 #elif defined(__i386__)
964 # if SANITIZER_FREEBSD
965 ucontext_t *ucontext = (ucontext_t*)context;
966 *pc = ucontext->uc_mcontext.mc_eip;
967 *bp = ucontext->uc_mcontext.mc_ebp;
968 *sp = ucontext->uc_mcontext.mc_esp;
969 # else
970 ucontext_t *ucontext = (ucontext_t*)context;
971 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
972 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
973 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
974 # endif
975 #elif defined(__powerpc__) || defined(__powerpc64__)
976 ucontext_t *ucontext = (ucontext_t*)context;
977 *pc = ucontext->uc_mcontext.regs->nip;
978 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
979 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
980 // pointer, but GCC always uses r31 when we need a frame pointer.
981 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
982 #elif defined(__sparc__)
983 ucontext_t *ucontext = (ucontext_t*)context;
984 uptr *stk_ptr;
985 # if defined (__arch64__)
986 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
987 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
988 stk_ptr = (uptr *) (*sp + 2047);
989 *bp = stk_ptr[15];
990 # else
991 *pc = ucontext->uc_mcontext.gregs[REG_PC];
992 *sp = ucontext->uc_mcontext.gregs[REG_O6];
993 stk_ptr = (uptr *) *sp;
994 *bp = stk_ptr[15];
995 # endif
996 #elif defined(__mips__)
997 ucontext_t *ucontext = (ucontext_t*)context;
998 *pc = ucontext->uc_mcontext.gregs[31];
999 *bp = ucontext->uc_mcontext.gregs[30];
1000 *sp = ucontext->uc_mcontext.gregs[29];
1001 #else
1002 # error "Unsupported arch"
1003 #endif
1004 }
1005
1006 } // namespace __sanitizer
1007
1008 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX
1009