diagnostic-show-locus.c: remove unused field from class colorizer
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_linux.cc
blob5b6f18602e7dc0b0ba21ee7bb64af9415d87bbb5
1 //===-- sanitizer_linux.cc ------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements linux-specific functions from
10 // sanitizer_libc.h.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
15 #if SANITIZER_FREEBSD || SANITIZER_LINUX
17 #include "sanitizer_common.h"
18 #include "sanitizer_flags.h"
19 #include "sanitizer_internal_defs.h"
20 #include "sanitizer_libc.h"
21 #include "sanitizer_linux.h"
22 #include "sanitizer_mutex.h"
23 #include "sanitizer_placement_new.h"
24 #include "sanitizer_procmaps.h"
25 #include "sanitizer_stacktrace.h"
26 #include "sanitizer_symbolizer.h"
28 #if !SANITIZER_FREEBSD
29 #include <asm/param.h>
30 #endif
32 // For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
33 // format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
34 // access stat from asm/stat.h, without conflicting with definition in
35 // sys/stat.h, we use this trick.
36 #if defined(__mips64)
37 #include <asm/unistd.h>
38 #include <sys/types.h>
39 #define stat kernel_stat
40 #include <asm/stat.h>
41 #undef stat
42 #endif
44 #include <dlfcn.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <link.h>
48 #include <pthread.h>
49 #include <sched.h>
50 #include <sys/mman.h>
51 #include <sys/ptrace.h>
52 #include <sys/resource.h>
53 #include <sys/stat.h>
54 #include <sys/syscall.h>
55 #include <sys/time.h>
56 #include <sys/types.h>
57 #include <ucontext.h>
58 #include <unistd.h>
60 #if SANITIZER_FREEBSD
61 #include <sys/exec.h>
62 #include <sys/sysctl.h>
63 #include <vm/vm_param.h>
64 #include <vm/pmap.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>
71 extern char **environ; // provided by crt1
72 #endif // SANITIZER_FREEBSD
74 #if !SANITIZER_ANDROID
75 #include <sys/signal.h>
76 #endif
78 #if SANITIZER_LINUX
79 // <linux/time.h>
80 struct kernel_timeval {
81 long tv_sec;
82 long tv_usec;
85 // <linux/futex.h> is broken on some linux distributions.
86 const int FUTEX_WAIT = 0;
87 const int FUTEX_WAKE = 1;
88 #endif // SANITIZER_LINUX
90 // Are we using 32-bit or 64-bit Linux syscalls?
91 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
92 // but it still needs to use 64-bit syscalls.
93 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \
94 SANITIZER_WORDSIZE == 64)
95 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
96 #else
97 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
98 #endif
100 #if defined(__x86_64__) || SANITIZER_MIPS64
101 extern "C" {
102 extern void internal_sigreturn();
104 #endif
106 namespace __sanitizer {
108 #if SANITIZER_LINUX && defined(__x86_64__)
109 #include "sanitizer_syscall_linux_x86_64.inc"
110 #elif SANITIZER_LINUX && defined(__aarch64__)
111 #include "sanitizer_syscall_linux_aarch64.inc"
112 #else
113 #include "sanitizer_syscall_generic.inc"
114 #endif
116 // --------------- sanitizer_libc.h
117 #if !SANITIZER_S390
118 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
119 OFF_T offset) {
120 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
121 return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
122 offset);
123 #else
124 // mmap2 specifies file offset in 4096-byte units.
125 CHECK(IsAligned(offset, 4096));
126 return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
127 offset / 4096);
128 #endif
130 #endif // !SANITIZER_S390
132 uptr internal_munmap(void *addr, uptr length) {
133 return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
136 int internal_mprotect(void *addr, uptr length, int prot) {
137 return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot);
140 uptr internal_close(fd_t fd) {
141 return internal_syscall(SYSCALL(close), fd);
144 uptr internal_open(const char *filename, int flags) {
145 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
146 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
147 #else
148 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
149 #endif
152 uptr internal_open(const char *filename, int flags, u32 mode) {
153 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
154 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
155 mode);
156 #else
157 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
158 #endif
161 uptr internal_read(fd_t fd, void *buf, uptr count) {
162 sptr res;
163 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf,
164 count));
165 return res;
168 uptr internal_write(fd_t fd, const void *buf, uptr count) {
169 sptr res;
170 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf,
171 count));
172 return res;
175 uptr internal_ftruncate(fd_t fd, uptr size) {
176 sptr res;
177 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd,
178 (OFF_T)size));
179 return res;
182 #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && !SANITIZER_FREEBSD
183 static void stat64_to_stat(struct stat64 *in, struct stat *out) {
184 internal_memset(out, 0, sizeof(*out));
185 out->st_dev = in->st_dev;
186 out->st_ino = in->st_ino;
187 out->st_mode = in->st_mode;
188 out->st_nlink = in->st_nlink;
189 out->st_uid = in->st_uid;
190 out->st_gid = in->st_gid;
191 out->st_rdev = in->st_rdev;
192 out->st_size = in->st_size;
193 out->st_blksize = in->st_blksize;
194 out->st_blocks = in->st_blocks;
195 out->st_atime = in->st_atime;
196 out->st_mtime = in->st_mtime;
197 out->st_ctime = in->st_ctime;
198 out->st_ino = in->st_ino;
200 #endif
202 #if defined(__mips64)
203 static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) {
204 internal_memset(out, 0, sizeof(*out));
205 out->st_dev = in->st_dev;
206 out->st_ino = in->st_ino;
207 out->st_mode = in->st_mode;
208 out->st_nlink = in->st_nlink;
209 out->st_uid = in->st_uid;
210 out->st_gid = in->st_gid;
211 out->st_rdev = in->st_rdev;
212 out->st_size = in->st_size;
213 out->st_blksize = in->st_blksize;
214 out->st_blocks = in->st_blocks;
215 out->st_atime = in->st_atime_nsec;
216 out->st_mtime = in->st_mtime_nsec;
217 out->st_ctime = in->st_ctime_nsec;
218 out->st_ino = in->st_ino;
220 #endif
222 uptr internal_stat(const char *path, void *buf) {
223 #if SANITIZER_FREEBSD
224 return internal_syscall(SYSCALL(stat), path, buf);
225 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
226 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
227 (uptr)buf, 0);
228 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
229 # if defined(__mips64)
230 // For mips64, stat syscall fills buffer in the format of kernel_stat
231 struct kernel_stat kbuf;
232 int res = internal_syscall(SYSCALL(stat), path, &kbuf);
233 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
234 return res;
235 # else
236 return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf);
237 # endif
238 #else
239 struct stat64 buf64;
240 int res = internal_syscall(SYSCALL(stat64), path, &buf64);
241 stat64_to_stat(&buf64, (struct stat *)buf);
242 return res;
243 #endif
246 uptr internal_lstat(const char *path, void *buf) {
247 #if SANITIZER_FREEBSD
248 return internal_syscall(SYSCALL(lstat), path, buf);
249 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
250 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
251 (uptr)buf, AT_SYMLINK_NOFOLLOW);
252 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
253 # if SANITIZER_MIPS64
254 // For mips64, lstat syscall fills buffer in the format of kernel_stat
255 struct kernel_stat kbuf;
256 int res = internal_syscall(SYSCALL(lstat), path, &kbuf);
257 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
258 return res;
259 # else
260 return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf);
261 # endif
262 #else
263 struct stat64 buf64;
264 int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
265 stat64_to_stat(&buf64, (struct stat *)buf);
266 return res;
267 #endif
270 uptr internal_fstat(fd_t fd, void *buf) {
271 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
272 # if SANITIZER_MIPS64
273 // For mips64, fstat syscall fills buffer in the format of kernel_stat
274 struct kernel_stat kbuf;
275 int res = internal_syscall(SYSCALL(fstat), fd, &kbuf);
276 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
277 return res;
278 # else
279 return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
280 # endif
281 #else
282 struct stat64 buf64;
283 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
284 stat64_to_stat(&buf64, (struct stat *)buf);
285 return res;
286 #endif
289 uptr internal_filesize(fd_t fd) {
290 struct stat st;
291 if (internal_fstat(fd, &st))
292 return -1;
293 return (uptr)st.st_size;
296 uptr internal_dup2(int oldfd, int newfd) {
297 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
298 return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
299 #else
300 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
301 #endif
304 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
305 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
306 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD,
307 (uptr)path, (uptr)buf, bufsize);
308 #else
309 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
310 #endif
313 uptr internal_unlink(const char *path) {
314 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
315 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
316 #else
317 return internal_syscall(SYSCALL(unlink), (uptr)path);
318 #endif
321 uptr internal_rename(const char *oldpath, const char *newpath) {
322 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
323 return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
324 (uptr)newpath);
325 #else
326 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
327 #endif
330 uptr internal_sched_yield() {
331 return internal_syscall(SYSCALL(sched_yield));
334 void internal__exit(int exitcode) {
335 #if SANITIZER_FREEBSD
336 internal_syscall(SYSCALL(exit), exitcode);
337 #else
338 internal_syscall(SYSCALL(exit_group), exitcode);
339 #endif
340 Die(); // Unreachable.
343 unsigned int internal_sleep(unsigned int seconds) {
344 struct timespec ts;
345 ts.tv_sec = 1;
346 ts.tv_nsec = 0;
347 int res = internal_syscall(SYSCALL(nanosleep), &ts, &ts);
348 if (res) return ts.tv_sec;
349 return 0;
352 uptr internal_execve(const char *filename, char *const argv[],
353 char *const envp[]) {
354 return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv,
355 (uptr)envp);
358 // ----------------- sanitizer_common.h
359 bool FileExists(const char *filename) {
360 struct stat st;
361 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
362 if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0))
363 #else
364 if (internal_stat(filename, &st))
365 #endif
366 return false;
367 // Sanity check: filename is a regular file.
368 return S_ISREG(st.st_mode);
371 uptr GetTid() {
372 #if SANITIZER_FREEBSD
373 return (uptr)pthread_self();
374 #else
375 return internal_syscall(SYSCALL(gettid));
376 #endif
379 u64 NanoTime() {
380 #if SANITIZER_FREEBSD
381 timeval tv;
382 #else
383 kernel_timeval tv;
384 #endif
385 internal_memset(&tv, 0, sizeof(tv));
386 internal_syscall(SYSCALL(gettimeofday), (uptr)&tv, 0);
387 return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
390 // Like getenv, but reads env directly from /proc (on Linux) or parses the
391 // 'environ' array (on FreeBSD) and does not use libc. This function should be
392 // called first inside __asan_init.
393 const char *GetEnv(const char *name) {
394 #if SANITIZER_FREEBSD
395 if (::environ != 0) {
396 uptr NameLen = internal_strlen(name);
397 for (char **Env = ::environ; *Env != 0; Env++) {
398 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
399 return (*Env) + NameLen + 1;
402 return 0; // Not found.
403 #elif SANITIZER_LINUX
404 static char *environ;
405 static uptr len;
406 static bool inited;
407 if (!inited) {
408 inited = true;
409 uptr environ_size;
410 if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len))
411 environ = nullptr;
413 if (!environ || len == 0) return nullptr;
414 uptr namelen = internal_strlen(name);
415 const char *p = environ;
416 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
417 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
418 const char* endp =
419 (char*)internal_memchr(p, '\0', len - (p - environ));
420 if (!endp) // this entry isn't NUL terminated
421 return nullptr;
422 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
423 return p + namelen + 1; // point after =
424 p = endp + 1;
426 return nullptr; // Not found.
427 #else
428 #error "Unsupported platform"
429 #endif
432 #if !SANITIZER_FREEBSD
433 extern "C" {
434 SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
436 #endif
438 #if !SANITIZER_GO && !SANITIZER_FREEBSD
439 static void ReadNullSepFileToArray(const char *path, char ***arr,
440 int arr_size) {
441 char *buff;
442 uptr buff_size;
443 uptr buff_len;
444 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
445 if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) {
446 (*arr)[0] = nullptr;
447 return;
449 (*arr)[0] = buff;
450 int count, i;
451 for (count = 1, i = 1; ; i++) {
452 if (buff[i] == 0) {
453 if (buff[i+1] == 0) break;
454 (*arr)[count] = &buff[i+1];
455 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
456 count++;
459 (*arr)[count] = nullptr;
461 #endif
463 static void GetArgsAndEnv(char ***argv, char ***envp) {
464 #if !SANITIZER_FREEBSD
465 #if !SANITIZER_GO
466 if (&__libc_stack_end) {
467 #endif
468 uptr* stack_end = (uptr*)__libc_stack_end;
469 int argc = *stack_end;
470 *argv = (char**)(stack_end + 1);
471 *envp = (char**)(stack_end + argc + 2);
472 #if !SANITIZER_GO
473 } else {
474 static const int kMaxArgv = 2000, kMaxEnvp = 2000;
475 ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
476 ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
478 #endif
479 #else
480 // On FreeBSD, retrieving the argument and environment arrays is done via the
481 // kern.ps_strings sysctl, which returns a pointer to a structure containing
482 // this information. See also <sys/exec.h>.
483 ps_strings *pss;
484 size_t sz = sizeof(pss);
485 if (sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1) {
486 Printf("sysctl kern.ps_strings failed\n");
487 Die();
489 *argv = pss->ps_argvstr;
490 *envp = pss->ps_envstr;
491 #endif
494 char **GetArgv() {
495 char **argv, **envp;
496 GetArgsAndEnv(&argv, &envp);
497 return argv;
500 void ReExec() {
501 char **argv, **envp;
502 GetArgsAndEnv(&argv, &envp);
503 uptr rv = internal_execve("/proc/self/exe", argv, envp);
504 int rverrno;
505 CHECK_EQ(internal_iserror(rv, &rverrno), true);
506 Printf("execve failed, errno %d\n", rverrno);
507 Die();
510 enum MutexState {
511 MtxUnlocked = 0,
512 MtxLocked = 1,
513 MtxSleeping = 2
516 BlockingMutex::BlockingMutex() {
517 internal_memset(this, 0, sizeof(*this));
520 void BlockingMutex::Lock() {
521 CHECK_EQ(owner_, 0);
522 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
523 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
524 return;
525 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
526 #if SANITIZER_FREEBSD
527 _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
528 #else
529 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
530 #endif
534 void BlockingMutex::Unlock() {
535 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
536 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
537 CHECK_NE(v, MtxUnlocked);
538 if (v == MtxSleeping) {
539 #if SANITIZER_FREEBSD
540 _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
541 #else
542 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE, 1, 0, 0, 0);
543 #endif
547 void BlockingMutex::CheckLocked() {
548 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
549 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
552 // ----------------- sanitizer_linux.h
553 // The actual size of this structure is specified by d_reclen.
554 // Note that getdents64 uses a different structure format. We only provide the
555 // 32-bit syscall here.
556 struct linux_dirent {
557 #if SANITIZER_X32 || defined(__aarch64__)
558 u64 d_ino;
559 u64 d_off;
560 #else
561 unsigned long d_ino;
562 unsigned long d_off;
563 #endif
564 unsigned short d_reclen;
565 #ifdef __aarch64__
566 unsigned char d_type;
567 #endif
568 char d_name[256];
571 // Syscall wrappers.
572 uptr internal_ptrace(int request, int pid, void *addr, void *data) {
573 return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
574 (uptr)data);
577 uptr internal_waitpid(int pid, int *status, int options) {
578 return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
579 0 /* rusage */);
582 uptr internal_getpid() {
583 return internal_syscall(SYSCALL(getpid));
586 uptr internal_getppid() {
587 return internal_syscall(SYSCALL(getppid));
590 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
591 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
592 return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
593 #else
594 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
595 #endif
598 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
599 return internal_syscall(SYSCALL(lseek), fd, offset, whence);
602 #if SANITIZER_LINUX
603 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
604 return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
606 #endif
608 uptr internal_sigaltstack(const void *ss, void *oss) {
609 return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
612 int internal_fork() {
613 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
614 return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
615 #else
616 return internal_syscall(SYSCALL(fork));
617 #endif
620 #if SANITIZER_LINUX
621 #define SA_RESTORER 0x04000000
622 // Doesn't set sa_restorer if the caller did not set it, so use with caution
623 //(see below).
624 int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
625 __sanitizer_kernel_sigaction_t k_act, k_oldact;
626 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
627 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
628 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
629 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
630 if (u_act) {
631 k_act.handler = u_act->handler;
632 k_act.sigaction = u_act->sigaction;
633 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
634 sizeof(__sanitizer_kernel_sigset_t));
635 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
636 k_act.sa_flags = u_act->sa_flags | SA_RESTORER;
637 // FIXME: most often sa_restorer is unset, however the kernel requires it
638 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
639 // If sa_restorer passed to the kernel is NULL, the program may crash upon
640 // signal delivery or fail to unwind the stack in the signal handler.
641 // libc implementation of sigaction() passes its own restorer to
642 // rt_sigaction, so we need to do the same (we'll need to reimplement the
643 // restorers; for x86_64 the restorer address can be obtained from
644 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
645 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
646 k_act.sa_restorer = u_act->sa_restorer;
647 #endif
650 uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
651 (uptr)(u_act ? &k_act : nullptr),
652 (uptr)(u_oldact ? &k_oldact : nullptr),
653 (uptr)sizeof(__sanitizer_kernel_sigset_t));
655 if ((result == 0) && u_oldact) {
656 u_oldact->handler = k_oldact.handler;
657 u_oldact->sigaction = k_oldact.sigaction;
658 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
659 sizeof(__sanitizer_kernel_sigset_t));
660 u_oldact->sa_flags = k_oldact.sa_flags;
661 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
662 u_oldact->sa_restorer = k_oldact.sa_restorer;
663 #endif
665 return result;
668 // Invokes sigaction via a raw syscall with a restorer, but does not support
669 // all platforms yet.
670 // We disable for Go simply because we have not yet added to buildgo.sh.
671 #if (defined(__x86_64__) || SANITIZER_MIPS64) && !SANITIZER_GO
672 int internal_sigaction_syscall(int signum, const void *act, void *oldact) {
673 if (act == nullptr)
674 return internal_sigaction_norestorer(signum, act, oldact);
675 __sanitizer_sigaction u_adjust;
676 internal_memcpy(&u_adjust, act, sizeof(u_adjust));
677 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
678 if (u_adjust.sa_restorer == nullptr) {
679 u_adjust.sa_restorer = internal_sigreturn;
681 #endif
682 return internal_sigaction_norestorer(signum, (const void *)&u_adjust,
683 oldact);
685 #endif // defined(__x86_64__) && !SANITIZER_GO
686 #endif // SANITIZER_LINUX
688 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
689 __sanitizer_sigset_t *oldset) {
690 #if SANITIZER_FREEBSD
691 return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
692 #else
693 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
694 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
695 return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how,
696 (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
697 sizeof(__sanitizer_kernel_sigset_t));
698 #endif
701 void internal_sigfillset(__sanitizer_sigset_t *set) {
702 internal_memset(set, 0xff, sizeof(*set));
705 void internal_sigemptyset(__sanitizer_sigset_t *set) {
706 internal_memset(set, 0, sizeof(*set));
709 #if SANITIZER_LINUX
710 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
711 signum -= 1;
712 CHECK_GE(signum, 0);
713 CHECK_LT(signum, sizeof(*set) * 8);
714 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
715 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
716 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
717 k_set->sig[idx] &= ~(1 << bit);
720 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
721 signum -= 1;
722 CHECK_GE(signum, 0);
723 CHECK_LT(signum, sizeof(*set) * 8);
724 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
725 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
726 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
727 return k_set->sig[idx] & (1 << bit);
729 #endif // SANITIZER_LINUX
731 // ThreadLister implementation.
732 ThreadLister::ThreadLister(int pid)
733 : pid_(pid),
734 descriptor_(-1),
735 buffer_(4096),
736 error_(true),
737 entry_((struct linux_dirent *)buffer_.data()),
738 bytes_read_(0) {
739 char task_directory_path[80];
740 internal_snprintf(task_directory_path, sizeof(task_directory_path),
741 "/proc/%d/task/", pid);
742 uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
743 if (internal_iserror(openrv)) {
744 error_ = true;
745 Report("Can't open /proc/%d/task for reading.\n", pid);
746 } else {
747 error_ = false;
748 descriptor_ = openrv;
752 int ThreadLister::GetNextTID() {
753 int tid = -1;
754 do {
755 if (error_)
756 return -1;
757 if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
758 return -1;
759 if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
760 entry_->d_name[0] <= '9') {
761 // Found a valid tid.
762 tid = (int)internal_atoll(entry_->d_name);
764 entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
765 } while (tid < 0);
766 return tid;
769 void ThreadLister::Reset() {
770 if (error_ || descriptor_ < 0)
771 return;
772 internal_lseek(descriptor_, 0, SEEK_SET);
775 ThreadLister::~ThreadLister() {
776 if (descriptor_ >= 0)
777 internal_close(descriptor_);
780 bool ThreadLister::error() { return error_; }
782 bool ThreadLister::GetDirectoryEntries() {
783 CHECK_GE(descriptor_, 0);
784 CHECK_NE(error_, true);
785 bytes_read_ = internal_getdents(descriptor_,
786 (struct linux_dirent *)buffer_.data(),
787 buffer_.size());
788 if (internal_iserror(bytes_read_)) {
789 Report("Can't read directory entries from /proc/%d/task.\n", pid_);
790 error_ = true;
791 return false;
792 } else if (bytes_read_ == 0) {
793 return false;
795 entry_ = (struct linux_dirent *)buffer_.data();
796 return true;
799 uptr GetPageSize() {
800 // Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
801 #if SANITIZER_ANDROID
802 return 4096;
803 #elif SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
804 return EXEC_PAGESIZE;
805 #else
806 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
807 #endif
810 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
811 #if SANITIZER_FREEBSD
812 const int Mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
813 const char *default_module_name = "kern.proc.pathname";
814 size_t Size = buf_len;
815 bool IsErr = (sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
816 int readlink_error = IsErr ? errno : 0;
817 uptr module_name_len = Size;
818 #else
819 const char *default_module_name = "/proc/self/exe";
820 uptr module_name_len = internal_readlink(
821 default_module_name, buf, buf_len);
822 int readlink_error;
823 bool IsErr = internal_iserror(module_name_len, &readlink_error);
824 #endif
825 if (IsErr) {
826 // We can't read binary name for some reason, assume it's unknown.
827 Report("WARNING: reading executable name failed with errno %d, "
828 "some stack frames may not be symbolized\n", readlink_error);
829 module_name_len = internal_snprintf(buf, buf_len, "%s",
830 default_module_name);
831 CHECK_LT(module_name_len, buf_len);
833 return module_name_len;
836 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
837 #if SANITIZER_LINUX
838 char *tmpbuf;
839 uptr tmpsize;
840 uptr tmplen;
841 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
842 1024 * 1024)) {
843 internal_strncpy(buf, tmpbuf, buf_len);
844 UnmapOrDie(tmpbuf, tmpsize);
845 return internal_strlen(buf);
847 #endif
848 return ReadBinaryName(buf, buf_len);
851 // Match full names of the form /path/to/base_name{-,.}*
852 bool LibraryNameIs(const char *full_name, const char *base_name) {
853 const char *name = full_name;
854 // Strip path.
855 while (*name != '\0') name++;
856 while (name > full_name && *name != '/') name--;
857 if (*name == '/') name++;
858 uptr base_name_length = internal_strlen(base_name);
859 if (internal_strncmp(name, base_name, base_name_length)) return false;
860 return (name[base_name_length] == '-' || name[base_name_length] == '.');
863 #if !SANITIZER_ANDROID
864 // Call cb for each region mapped by map.
865 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
866 CHECK_NE(map, nullptr);
867 #if !SANITIZER_FREEBSD
868 typedef ElfW(Phdr) Elf_Phdr;
869 typedef ElfW(Ehdr) Elf_Ehdr;
870 #endif // !SANITIZER_FREEBSD
871 char *base = (char *)map->l_addr;
872 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
873 char *phdrs = base + ehdr->e_phoff;
874 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
876 // Find the segment with the minimum base so we can "relocate" the p_vaddr
877 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
878 // objects have a non-zero base.
879 uptr preferred_base = (uptr)-1;
880 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
881 Elf_Phdr *phdr = (Elf_Phdr *)iter;
882 if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
883 preferred_base = (uptr)phdr->p_vaddr;
886 // Compute the delta from the real base to get a relocation delta.
887 sptr delta = (uptr)base - preferred_base;
888 // Now we can figure out what the loader really mapped.
889 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
890 Elf_Phdr *phdr = (Elf_Phdr *)iter;
891 if (phdr->p_type == PT_LOAD) {
892 uptr seg_start = phdr->p_vaddr + delta;
893 uptr seg_end = seg_start + phdr->p_memsz;
894 // None of these values are aligned. We consider the ragged edges of the
895 // load command as defined, since they are mapped from the file.
896 seg_start = RoundDownTo(seg_start, GetPageSizeCached());
897 seg_end = RoundUpTo(seg_end, GetPageSizeCached());
898 cb((void *)seg_start, seg_end - seg_start);
902 #endif
904 #if defined(__x86_64__) && SANITIZER_LINUX
905 // We cannot use glibc's clone wrapper, because it messes with the child
906 // task's TLS. It writes the PID and TID of the child task to its thread
907 // descriptor, but in our case the child task shares the thread descriptor with
908 // the parent (because we don't know how to allocate a new thread
909 // descriptor to keep glibc happy). So the stock version of clone(), when
910 // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
911 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
912 int *parent_tidptr, void *newtls, int *child_tidptr) {
913 long long res;
914 if (!fn || !child_stack)
915 return -EINVAL;
916 CHECK_EQ(0, (uptr)child_stack % 16);
917 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
918 ((unsigned long long *)child_stack)[0] = (uptr)fn;
919 ((unsigned long long *)child_stack)[1] = (uptr)arg;
920 register void *r8 __asm__("r8") = newtls;
921 register int *r10 __asm__("r10") = child_tidptr;
922 __asm__ __volatile__(
923 /* %rax = syscall(%rax = SYSCALL(clone),
924 * %rdi = flags,
925 * %rsi = child_stack,
926 * %rdx = parent_tidptr,
927 * %r8 = new_tls,
928 * %r10 = child_tidptr)
930 "syscall\n"
932 /* if (%rax != 0)
933 * return;
935 "testq %%rax,%%rax\n"
936 "jnz 1f\n"
938 /* In the child. Terminate unwind chain. */
939 // XXX: We should also terminate the CFI unwind chain
940 // here. Unfortunately clang 3.2 doesn't support the
941 // necessary CFI directives, so we skip that part.
942 "xorq %%rbp,%%rbp\n"
944 /* Call "fn(arg)". */
945 "popq %%rax\n"
946 "popq %%rdi\n"
947 "call *%%rax\n"
949 /* Call _exit(%rax). */
950 "movq %%rax,%%rdi\n"
951 "movq %2,%%rax\n"
952 "syscall\n"
954 /* Return to parent. */
955 "1:\n"
956 : "=a" (res)
957 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
958 "S"(child_stack),
959 "D"(flags),
960 "d"(parent_tidptr),
961 "r"(r8),
962 "r"(r10)
963 : "rsp", "memory", "r11", "rcx");
964 return res;
966 #elif defined(__mips__)
967 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
968 int *parent_tidptr, void *newtls, int *child_tidptr) {
969 long long res;
970 if (!fn || !child_stack)
971 return -EINVAL;
972 CHECK_EQ(0, (uptr)child_stack % 16);
973 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
974 ((unsigned long long *)child_stack)[0] = (uptr)fn;
975 ((unsigned long long *)child_stack)[1] = (uptr)arg;
976 register void *a3 __asm__("$7") = newtls;
977 register int *a4 __asm__("$8") = child_tidptr;
978 // We don't have proper CFI directives here because it requires alot of code
979 // for very marginal benefits.
980 __asm__ __volatile__(
981 /* $v0 = syscall($v0 = __NR_clone,
982 * $a0 = flags,
983 * $a1 = child_stack,
984 * $a2 = parent_tidptr,
985 * $a3 = new_tls,
986 * $a4 = child_tidptr)
988 ".cprestore 16;\n"
989 "move $4,%1;\n"
990 "move $5,%2;\n"
991 "move $6,%3;\n"
992 "move $7,%4;\n"
993 /* Store the fifth argument on stack
994 * if we are using 32-bit abi.
996 #if SANITIZER_WORDSIZE == 32
997 "lw %5,16($29);\n"
998 #else
999 "move $8,%5;\n"
1000 #endif
1001 "li $2,%6;\n"
1002 "syscall;\n"
1004 /* if ($v0 != 0)
1005 * return;
1007 "bnez $2,1f;\n"
1009 /* Call "fn(arg)". */
1010 #if SANITIZER_WORDSIZE == 32
1011 #ifdef __BIG_ENDIAN__
1012 "lw $25,4($29);\n"
1013 "lw $4,12($29);\n"
1014 #else
1015 "lw $25,0($29);\n"
1016 "lw $4,8($29);\n"
1017 #endif
1018 #else
1019 "ld $25,0($29);\n"
1020 "ld $4,8($29);\n"
1021 #endif
1022 "jal $25;\n"
1024 /* Call _exit($v0). */
1025 "move $4,$2;\n"
1026 "li $2,%7;\n"
1027 "syscall;\n"
1029 /* Return to parent. */
1030 "1:\n"
1031 : "=r" (res)
1032 : "r"(flags),
1033 "r"(child_stack),
1034 "r"(parent_tidptr),
1035 "r"(a3),
1036 "r"(a4),
1037 "i"(__NR_clone),
1038 "i"(__NR_exit)
1039 : "memory", "$29" );
1040 return res;
1042 #elif defined(__aarch64__)
1043 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1044 int *parent_tidptr, void *newtls, int *child_tidptr) {
1045 long long res;
1046 if (!fn || !child_stack)
1047 return -EINVAL;
1048 CHECK_EQ(0, (uptr)child_stack % 16);
1049 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1050 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1051 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1053 register int (*__fn)(void *) __asm__("x0") = fn;
1054 register void *__stack __asm__("x1") = child_stack;
1055 register int __flags __asm__("x2") = flags;
1056 register void *__arg __asm__("x3") = arg;
1057 register int *__ptid __asm__("x4") = parent_tidptr;
1058 register void *__tls __asm__("x5") = newtls;
1059 register int *__ctid __asm__("x6") = child_tidptr;
1061 __asm__ __volatile__(
1062 "mov x0,x2\n" /* flags */
1063 "mov x2,x4\n" /* ptid */
1064 "mov x3,x5\n" /* tls */
1065 "mov x4,x6\n" /* ctid */
1066 "mov x8,%9\n" /* clone */
1068 "svc 0x0\n"
1070 /* if (%r0 != 0)
1071 * return %r0;
1073 "cmp x0, #0\n"
1074 "bne 1f\n"
1076 /* In the child, now. Call "fn(arg)". */
1077 "ldp x1, x0, [sp], #16\n"
1078 "blr x1\n"
1080 /* Call _exit(%r0). */
1081 "mov x8, %10\n"
1082 "svc 0x0\n"
1083 "1:\n"
1085 : "=r" (res)
1086 : "i"(-EINVAL),
1087 "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1088 "r"(__ptid), "r"(__tls), "r"(__ctid),
1089 "i"(__NR_clone), "i"(__NR_exit)
1090 : "x30", "memory");
1091 return res;
1093 #elif defined(__powerpc64__)
1094 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1095 int *parent_tidptr, void *newtls, int *child_tidptr) {
1096 long long res;
1097 /* Stack frame offsets. */
1098 #if _CALL_ELF != 2
1099 #define FRAME_MIN_SIZE 112
1100 #define FRAME_TOC_SAVE 40
1101 #else
1102 #define FRAME_MIN_SIZE 32
1103 #define FRAME_TOC_SAVE 24
1104 #endif
1105 if (!fn || !child_stack)
1106 return -EINVAL;
1107 CHECK_EQ(0, (uptr)child_stack % 16);
1108 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1109 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1110 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1112 register int (*__fn)(void *) __asm__("r3") = fn;
1113 register void *__cstack __asm__("r4") = child_stack;
1114 register int __flags __asm__("r5") = flags;
1115 register void * __arg __asm__("r6") = arg;
1116 register int * __ptidptr __asm__("r7") = parent_tidptr;
1117 register void * __newtls __asm__("r8") = newtls;
1118 register int * __ctidptr __asm__("r9") = child_tidptr;
1120 __asm__ __volatile__(
1121 /* fn, arg, child_stack are saved acrVoss the syscall */
1122 "mr 28, %5\n\t"
1123 "mr 29, %6\n\t"
1124 "mr 27, %8\n\t"
1126 /* syscall
1127 r3 == flags
1128 r4 == child_stack
1129 r5 == parent_tidptr
1130 r6 == newtls
1131 r7 == child_tidptr */
1132 "mr 3, %7\n\t"
1133 "mr 5, %9\n\t"
1134 "mr 6, %10\n\t"
1135 "mr 7, %11\n\t"
1136 "li 0, %3\n\t"
1137 "sc\n\t"
1139 /* Test if syscall was successful */
1140 "cmpdi cr1, 3, 0\n\t"
1141 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1142 "bne- cr1, 1f\n\t"
1144 /* Do the function call */
1145 "std 2, %13(1)\n\t"
1146 #if _CALL_ELF != 2
1147 "ld 0, 0(28)\n\t"
1148 "ld 2, 8(28)\n\t"
1149 "mtctr 0\n\t"
1150 #else
1151 "mr 12, 28\n\t"
1152 "mtctr 12\n\t"
1153 #endif
1154 "mr 3, 27\n\t"
1155 "bctrl\n\t"
1156 "ld 2, %13(1)\n\t"
1158 /* Call _exit(r3) */
1159 "li 0, %4\n\t"
1160 "sc\n\t"
1162 /* Return to parent */
1163 "1:\n\t"
1164 "mr %0, 3\n\t"
1165 : "=r" (res)
1166 : "0" (-1), "i" (EINVAL),
1167 "i" (__NR_clone), "i" (__NR_exit),
1168 "r" (__fn), "r" (__cstack), "r" (__flags),
1169 "r" (__arg), "r" (__ptidptr), "r" (__newtls),
1170 "r" (__ctidptr), "i" (FRAME_MIN_SIZE), "i" (FRAME_TOC_SAVE)
1171 : "cr0", "cr1", "memory", "ctr",
1172 "r0", "r29", "r27", "r28");
1173 return res;
1175 #endif // defined(__x86_64__) && SANITIZER_LINUX
1177 #if SANITIZER_ANDROID
1178 #if __ANDROID_API__ < 21
1179 extern "C" __attribute__((weak)) int dl_iterate_phdr(
1180 int (*)(struct dl_phdr_info *, size_t, void *), void *);
1181 #endif
1183 static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1184 void *data) {
1185 // Any name starting with "lib" indicates a bug in L where library base names
1186 // are returned instead of paths.
1187 if (info->dlpi_name && info->dlpi_name[0] == 'l' &&
1188 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') {
1189 *(bool *)data = true;
1190 return 1;
1192 return 0;
1195 static atomic_uint32_t android_api_level;
1197 static AndroidApiLevel AndroidDetectApiLevel() {
1198 if (!&dl_iterate_phdr)
1199 return ANDROID_KITKAT; // K or lower
1200 bool base_name_seen = false;
1201 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen);
1202 if (base_name_seen)
1203 return ANDROID_LOLLIPOP_MR1; // L MR1
1204 return ANDROID_POST_LOLLIPOP; // post-L
1205 // Plain L (API level 21) is completely broken wrt ASan and not very
1206 // interesting to detect.
1209 AndroidApiLevel AndroidGetApiLevel() {
1210 AndroidApiLevel level =
1211 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed);
1212 if (level) return level;
1213 level = AndroidDetectApiLevel();
1214 atomic_store(&android_api_level, level, memory_order_relaxed);
1215 return level;
1218 #endif
1220 bool IsHandledDeadlySignal(int signum) {
1221 if (common_flags()->handle_abort && signum == SIGABRT)
1222 return true;
1223 if (common_flags()->handle_sigill && signum == SIGILL)
1224 return true;
1225 if (common_flags()->handle_sigfpe && signum == SIGFPE)
1226 return true;
1227 return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv;
1230 #if !SANITIZER_GO
1231 void *internal_start_thread(void(*func)(void *arg), void *arg) {
1232 // Start the thread with signals blocked, otherwise it can steal user signals.
1233 __sanitizer_sigset_t set, old;
1234 internal_sigfillset(&set);
1235 #if SANITIZER_LINUX && !SANITIZER_ANDROID
1236 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
1237 // on any thread, setuid call hangs (see test/tsan/setuid.c).
1238 internal_sigdelset(&set, 33);
1239 #endif
1240 internal_sigprocmask(SIG_SETMASK, &set, &old);
1241 void *th;
1242 real_pthread_create(&th, nullptr, (void*(*)(void *arg))func, arg);
1243 internal_sigprocmask(SIG_SETMASK, &old, nullptr);
1244 return th;
1247 void internal_join_thread(void *th) {
1248 real_pthread_join(th, nullptr);
1250 #else
1251 void *internal_start_thread(void (*func)(void *), void *arg) { return 0; }
1253 void internal_join_thread(void *th) {}
1254 #endif
1256 #if defined(__aarch64__)
1257 // Android headers in the older NDK releases miss this definition.
1258 struct __sanitizer_esr_context {
1259 struct _aarch64_ctx head;
1260 uint64_t esr;
1263 static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1264 static const u32 kEsrMagic = 0x45535201;
1265 u8 *aux = ucontext->uc_mcontext.__reserved;
1266 while (true) {
1267 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1268 if (ctx->size == 0) break;
1269 if (ctx->magic == kEsrMagic) {
1270 *esr = ((__sanitizer_esr_context *)ctx)->esr;
1271 return true;
1273 aux += ctx->size;
1275 return false;
1277 #endif
1279 SignalContext::WriteFlag SignalContext::GetWriteFlag(void *context) {
1280 ucontext_t *ucontext = (ucontext_t *)context;
1281 #if defined(__x86_64__) || defined(__i386__)
1282 static const uptr PF_WRITE = 1U << 1;
1283 #if SANITIZER_FREEBSD
1284 uptr err = ucontext->uc_mcontext.mc_err;
1285 #else
1286 uptr err = ucontext->uc_mcontext.gregs[REG_ERR];
1287 #endif
1288 return err & PF_WRITE ? WRITE : READ;
1289 #elif defined(__arm__)
1290 static const uptr FSR_WRITE = 1U << 11;
1291 uptr fsr = ucontext->uc_mcontext.error_code;
1292 return fsr & FSR_WRITE ? WRITE : READ;
1293 #elif defined(__aarch64__)
1294 static const u64 ESR_ELx_WNR = 1U << 6;
1295 u64 esr;
1296 if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN;
1297 return esr & ESR_ELx_WNR ? WRITE : READ;
1298 #else
1299 (void)ucontext;
1300 return UNKNOWN; // FIXME: Implement.
1301 #endif
1304 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
1305 #if defined(__arm__)
1306 ucontext_t *ucontext = (ucontext_t*)context;
1307 *pc = ucontext->uc_mcontext.arm_pc;
1308 *bp = ucontext->uc_mcontext.arm_fp;
1309 *sp = ucontext->uc_mcontext.arm_sp;
1310 #elif defined(__aarch64__)
1311 ucontext_t *ucontext = (ucontext_t*)context;
1312 *pc = ucontext->uc_mcontext.pc;
1313 *bp = ucontext->uc_mcontext.regs[29];
1314 *sp = ucontext->uc_mcontext.sp;
1315 #elif defined(__hppa__)
1316 ucontext_t *ucontext = (ucontext_t*)context;
1317 *pc = ucontext->uc_mcontext.sc_iaoq[0];
1318 /* GCC uses %r3 whenever a frame pointer is needed. */
1319 *bp = ucontext->uc_mcontext.sc_gr[3];
1320 *sp = ucontext->uc_mcontext.sc_gr[30];
1321 #elif defined(__x86_64__)
1322 # if SANITIZER_FREEBSD
1323 ucontext_t *ucontext = (ucontext_t*)context;
1324 *pc = ucontext->uc_mcontext.mc_rip;
1325 *bp = ucontext->uc_mcontext.mc_rbp;
1326 *sp = ucontext->uc_mcontext.mc_rsp;
1327 # else
1328 ucontext_t *ucontext = (ucontext_t*)context;
1329 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
1330 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
1331 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
1332 # endif
1333 #elif defined(__i386__)
1334 # if SANITIZER_FREEBSD
1335 ucontext_t *ucontext = (ucontext_t*)context;
1336 *pc = ucontext->uc_mcontext.mc_eip;
1337 *bp = ucontext->uc_mcontext.mc_ebp;
1338 *sp = ucontext->uc_mcontext.mc_esp;
1339 # else
1340 ucontext_t *ucontext = (ucontext_t*)context;
1341 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
1342 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
1343 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
1344 # endif
1345 #elif defined(__powerpc__) || defined(__powerpc64__)
1346 ucontext_t *ucontext = (ucontext_t*)context;
1347 *pc = ucontext->uc_mcontext.regs->nip;
1348 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
1349 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
1350 // pointer, but GCC always uses r31 when we need a frame pointer.
1351 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
1352 #elif defined(__sparc__)
1353 ucontext_t *ucontext = (ucontext_t*)context;
1354 uptr *stk_ptr;
1355 # if defined (__arch64__)
1356 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
1357 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
1358 stk_ptr = (uptr *) (*sp + 2047);
1359 *bp = stk_ptr[15];
1360 # else
1361 *pc = ucontext->uc_mcontext.gregs[REG_PC];
1362 *sp = ucontext->uc_mcontext.gregs[REG_O6];
1363 stk_ptr = (uptr *) *sp;
1364 *bp = stk_ptr[15];
1365 # endif
1366 #elif defined(__mips__)
1367 ucontext_t *ucontext = (ucontext_t*)context;
1368 *pc = ucontext->uc_mcontext.pc;
1369 *bp = ucontext->uc_mcontext.gregs[30];
1370 *sp = ucontext->uc_mcontext.gregs[29];
1371 #elif defined(__s390__)
1372 ucontext_t *ucontext = (ucontext_t*)context;
1373 # if defined(__s390x__)
1374 *pc = ucontext->uc_mcontext.psw.addr;
1375 # else
1376 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
1377 # endif
1378 *bp = ucontext->uc_mcontext.gregs[11];
1379 *sp = ucontext->uc_mcontext.gregs[15];
1380 #else
1381 # error "Unsupported arch"
1382 #endif
1385 void MaybeReexec() {
1386 // No need to re-exec on Linux.
1389 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding) {
1390 UNREACHABLE("FindAvailableMemoryRange is not available");
1391 return 0;
1394 } // namespace __sanitizer
1396 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX