Arm: Fix arm libsanitizer bootstrap failure
[gcc.git] / libsanitizer / sanitizer_common / sanitizer_linux.cpp
1 //===-- sanitizer_linux.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements linux-specific functions from
11 // sanitizer_libc.h.
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_platform.h"
15
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
17 SANITIZER_OPENBSD || SANITIZER_SOLARIS
18
19 #include "sanitizer_common.h"
20 #include "sanitizer_flags.h"
21 #include "sanitizer_getauxval.h"
22 #include "sanitizer_internal_defs.h"
23 #include "sanitizer_libc.h"
24 #include "sanitizer_linux.h"
25 #include "sanitizer_mutex.h"
26 #include "sanitizer_placement_new.h"
27 #include "sanitizer_procmaps.h"
28
29 #if SANITIZER_LINUX
30 #include <asm/param.h>
31 #endif
32
33 // For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
34 // format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
35 // access stat from asm/stat.h, without conflicting with definition in
36 // sys/stat.h, we use this trick.
37 #if defined(__mips64)
38 #include <asm/unistd.h>
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 #include <link.h>
49 #include <pthread.h>
50 #include <sched.h>
51 #include <signal.h>
52 #include <sys/mman.h>
53 #include <sys/param.h>
54 #if !SANITIZER_SOLARIS
55 #include <sys/ptrace.h>
56 #endif
57 #include <sys/resource.h>
58 #include <sys/stat.h>
59 #include <sys/syscall.h>
60 #include <sys/time.h>
61 #include <sys/types.h>
62 #if !SANITIZER_OPENBSD
63 #include <ucontext.h>
64 #endif
65 #if SANITIZER_OPENBSD
66 #include <sys/futex.h>
67 #include <sys/sysctl.h>
68 #endif
69 #include <unistd.h>
70
71 #if SANITIZER_LINUX
72 #include <sys/utsname.h>
73 #endif
74
75 #if SANITIZER_LINUX && !SANITIZER_ANDROID
76 #include <sys/personality.h>
77 #endif
78
79 #if SANITIZER_FREEBSD
80 #include <sys/exec.h>
81 #include <sys/sysctl.h>
82 #include <machine/atomic.h>
83 extern "C" {
84 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
85 // FreeBSD 9.2 and 10.0.
86 #include <sys/umtx.h>
87 }
88 #include <sys/thr.h>
89 #endif // SANITIZER_FREEBSD
90
91 #if SANITIZER_NETBSD
92 #include <limits.h> // For NAME_MAX
93 #include <sys/sysctl.h>
94 #include <sys/exec.h>
95 extern struct ps_strings *__ps_strings;
96 #endif // SANITIZER_NETBSD
97
98 #if SANITIZER_SOLARIS
99 #include <stdlib.h>
100 #include <thread.h>
101 #define environ _environ
102 #endif
103
104 extern char **environ;
105
106 #if SANITIZER_LINUX
107 // <linux/time.h>
108 struct kernel_timeval {
109 long tv_sec;
110 long tv_usec;
111 };
112
113 // <linux/futex.h> is broken on some linux distributions.
114 const int FUTEX_WAIT = 0;
115 const int FUTEX_WAKE = 1;
116 const int FUTEX_PRIVATE_FLAG = 128;
117 const int FUTEX_WAIT_PRIVATE = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
118 const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
119 #endif // SANITIZER_LINUX
120
121 // Are we using 32-bit or 64-bit Linux syscalls?
122 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
123 // but it still needs to use 64-bit syscalls.
124 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \
125 SANITIZER_WORDSIZE == 64)
126 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
127 #else
128 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
129 #endif
130
131 // Note : FreeBSD had implemented both
132 // Linux and OpenBSD apis, available from
133 // future 12.x version most likely
134 #if SANITIZER_LINUX && defined(__NR_getrandom)
135 # if !defined(GRND_NONBLOCK)
136 # define GRND_NONBLOCK 1
137 # endif
138 # define SANITIZER_USE_GETRANDOM 1
139 #else
140 # define SANITIZER_USE_GETRANDOM 0
141 #endif // SANITIZER_LINUX && defined(__NR_getrandom)
142
143 #if SANITIZER_OPENBSD
144 # define SANITIZER_USE_GETENTROPY 1
145 #else
146 # if SANITIZER_FREEBSD && __FreeBSD_version >= 1200000
147 # define SANITIZER_USE_GETENTROPY 1
148 # else
149 # define SANITIZER_USE_GETENTROPY 0
150 # endif
151 #endif // SANITIZER_USE_GETENTROPY
152
153 namespace __sanitizer {
154
155 #if SANITIZER_LINUX && defined(__x86_64__)
156 #include "sanitizer_syscall_linux_x86_64.inc"
157 #elif SANITIZER_LINUX && defined(__aarch64__)
158 #include "sanitizer_syscall_linux_aarch64.inc"
159 #elif SANITIZER_LINUX && defined(__arm__)
160 #include "sanitizer_syscall_linux_arm.inc"
161 #else
162 #include "sanitizer_syscall_generic.inc"
163 #endif
164
165 // --------------- sanitizer_libc.h
166 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
167 #if !SANITIZER_S390 && !SANITIZER_OPENBSD
168 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
169 OFF_T offset) {
170 #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
171 return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
172 offset);
173 #else
174 // mmap2 specifies file offset in 4096-byte units.
175 CHECK(IsAligned(offset, 4096));
176 return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
177 offset / 4096);
178 #endif
179 }
180 #endif // !SANITIZER_S390 && !SANITIZER_OPENBSD
181
182 #if !SANITIZER_OPENBSD
183 uptr internal_munmap(void *addr, uptr length) {
184 return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
185 }
186
187 int internal_mprotect(void *addr, uptr length, int prot) {
188 return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot);
189 }
190 #endif
191
192 uptr internal_close(fd_t fd) {
193 return internal_syscall(SYSCALL(close), fd);
194 }
195
196 uptr internal_open(const char *filename, int flags) {
197 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
198 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
199 #else
200 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
201 #endif
202 }
203
204 uptr internal_open(const char *filename, int flags, u32 mode) {
205 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
206 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
207 mode);
208 #else
209 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
210 #endif
211 }
212
213 uptr internal_read(fd_t fd, void *buf, uptr count) {
214 sptr res;
215 HANDLE_EINTR(res,
216 (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, count));
217 return res;
218 }
219
220 uptr internal_write(fd_t fd, const void *buf, uptr count) {
221 sptr res;
222 HANDLE_EINTR(res,
223 (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count));
224 return res;
225 }
226
227 uptr internal_ftruncate(fd_t fd, uptr size) {
228 sptr res;
229 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd,
230 (OFF_T)size));
231 return res;
232 }
233
234 #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX
235 static void stat64_to_stat(struct stat64 *in, struct stat *out) {
236 internal_memset(out, 0, sizeof(*out));
237 out->st_dev = in->st_dev;
238 out->st_ino = in->st_ino;
239 out->st_mode = in->st_mode;
240 out->st_nlink = in->st_nlink;
241 out->st_uid = in->st_uid;
242 out->st_gid = in->st_gid;
243 out->st_rdev = in->st_rdev;
244 out->st_size = in->st_size;
245 out->st_blksize = in->st_blksize;
246 out->st_blocks = in->st_blocks;
247 out->st_atime = in->st_atime;
248 out->st_mtime = in->st_mtime;
249 out->st_ctime = in->st_ctime;
250 }
251 #endif
252
253 #if defined(__mips64)
254 // Undefine compatibility macros from <sys/stat.h>
255 // so that they would not clash with the kernel_stat
256 // st_[a|m|c]time fields
257 #undef st_atime
258 #undef st_mtime
259 #undef st_ctime
260 #if defined(SANITIZER_ANDROID)
261 // Bionic sys/stat.h defines additional macros
262 // for compatibility with the old NDKs and
263 // they clash with the kernel_stat structure
264 // st_[a|m|c]time_nsec fields.
265 #undef st_atime_nsec
266 #undef st_mtime_nsec
267 #undef st_ctime_nsec
268 #endif
269 static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) {
270 internal_memset(out, 0, sizeof(*out));
271 out->st_dev = in->st_dev;
272 out->st_ino = in->st_ino;
273 out->st_mode = in->st_mode;
274 out->st_nlink = in->st_nlink;
275 out->st_uid = in->st_uid;
276 out->st_gid = in->st_gid;
277 out->st_rdev = in->st_rdev;
278 out->st_size = in->st_size;
279 out->st_blksize = in->st_blksize;
280 out->st_blocks = in->st_blocks;
281 #if defined(__USE_MISC) || \
282 defined(__USE_XOPEN2K8) || \
283 defined(SANITIZER_ANDROID)
284 out->st_atim.tv_sec = in->st_atime;
285 out->st_atim.tv_nsec = in->st_atime_nsec;
286 out->st_mtim.tv_sec = in->st_mtime;
287 out->st_mtim.tv_nsec = in->st_mtime_nsec;
288 out->st_ctim.tv_sec = in->st_ctime;
289 out->st_ctim.tv_nsec = in->st_ctime_nsec;
290 #else
291 out->st_atime = in->st_atime;
292 out->st_atimensec = in->st_atime_nsec;
293 out->st_mtime = in->st_mtime;
294 out->st_mtimensec = in->st_mtime_nsec;
295 out->st_ctime = in->st_ctime;
296 out->st_atimensec = in->st_ctime_nsec;
297 #endif
298 }
299 #endif
300
301 uptr internal_stat(const char *path, void *buf) {
302 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
303 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf, 0);
304 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
305 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
306 0);
307 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
308 # if defined(__mips64)
309 // For mips64, stat syscall fills buffer in the format of kernel_stat
310 struct kernel_stat kbuf;
311 int res = internal_syscall(SYSCALL(stat), path, &kbuf);
312 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
313 return res;
314 # else
315 return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf);
316 # endif
317 #else
318 struct stat64 buf64;
319 int res = internal_syscall(SYSCALL(stat64), path, &buf64);
320 stat64_to_stat(&buf64, (struct stat *)buf);
321 return res;
322 #endif
323 }
324
325 uptr internal_lstat(const char *path, void *buf) {
326 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
327 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf,
328 AT_SYMLINK_NOFOLLOW);
329 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
330 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
331 AT_SYMLINK_NOFOLLOW);
332 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
333 # if SANITIZER_MIPS64
334 // For mips64, lstat syscall fills buffer in the format of kernel_stat
335 struct kernel_stat kbuf;
336 int res = internal_syscall(SYSCALL(lstat), path, &kbuf);
337 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
338 return res;
339 # else
340 return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf);
341 # endif
342 #else
343 struct stat64 buf64;
344 int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
345 stat64_to_stat(&buf64, (struct stat *)buf);
346 return res;
347 #endif
348 }
349
350 uptr internal_fstat(fd_t fd, void *buf) {
351 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD || \
352 SANITIZER_LINUX_USES_64BIT_SYSCALLS
353 #if SANITIZER_MIPS64 && !SANITIZER_OPENBSD
354 // For mips64, fstat syscall fills buffer in the format of kernel_stat
355 struct kernel_stat kbuf;
356 int res = internal_syscall(SYSCALL(fstat), fd, &kbuf);
357 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
358 return res;
359 # else
360 return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
361 # endif
362 #else
363 struct stat64 buf64;
364 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
365 stat64_to_stat(&buf64, (struct stat *)buf);
366 return res;
367 #endif
368 }
369
370 uptr internal_filesize(fd_t fd) {
371 struct stat st;
372 if (internal_fstat(fd, &st))
373 return -1;
374 return (uptr)st.st_size;
375 }
376
377 uptr internal_dup(int oldfd) {
378 return internal_syscall(SYSCALL(dup), oldfd);
379 }
380
381 uptr internal_dup2(int oldfd, int newfd) {
382 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
383 return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
384 #else
385 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
386 #endif
387 }
388
389 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
390 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
391 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf,
392 bufsize);
393 #elif SANITIZER_OPENBSD
394 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf,
395 bufsize);
396 #else
397 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
398 #endif
399 }
400
401 uptr internal_unlink(const char *path) {
402 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS || SANITIZER_OPENBSD
403 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
404 #else
405 return internal_syscall(SYSCALL(unlink), (uptr)path);
406 #endif
407 }
408
409 uptr internal_rename(const char *oldpath, const char *newpath) {
410 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS || SANITIZER_OPENBSD
411 return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
412 (uptr)newpath);
413 #else
414 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
415 #endif
416 }
417
418 uptr internal_sched_yield() {
419 return internal_syscall(SYSCALL(sched_yield));
420 }
421
422 void internal__exit(int exitcode) {
423 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
424 internal_syscall(SYSCALL(exit), exitcode);
425 #else
426 internal_syscall(SYSCALL(exit_group), exitcode);
427 #endif
428 Die(); // Unreachable.
429 }
430
431 unsigned int internal_sleep(unsigned int seconds) {
432 struct timespec ts;
433 ts.tv_sec = seconds;
434 ts.tv_nsec = 0;
435 int res = internal_syscall(SYSCALL(nanosleep), &ts, &ts);
436 if (res) return ts.tv_sec;
437 return 0;
438 }
439
440 uptr internal_execve(const char *filename, char *const argv[],
441 char *const envp[]) {
442 return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv,
443 (uptr)envp);
444 }
445 #endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
446
447 // ----------------- sanitizer_common.h
448 bool FileExists(const char *filename) {
449 if (ShouldMockFailureToOpen(filename))
450 return false;
451 struct stat st;
452 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
453 if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0))
454 #else
455 if (internal_stat(filename, &st))
456 #endif
457 return false;
458 // Sanity check: filename is a regular file.
459 return S_ISREG(st.st_mode);
460 }
461
462 #if !SANITIZER_NETBSD
463 tid_t GetTid() {
464 #if SANITIZER_FREEBSD
465 long Tid;
466 thr_self(&Tid);
467 return Tid;
468 #elif SANITIZER_OPENBSD
469 return internal_syscall(SYSCALL(getthrid));
470 #elif SANITIZER_SOLARIS
471 return thr_self();
472 #else
473 return internal_syscall(SYSCALL(gettid));
474 #endif
475 }
476
477 int TgKill(pid_t pid, tid_t tid, int sig) {
478 #if SANITIZER_LINUX
479 return internal_syscall(SYSCALL(tgkill), pid, tid, sig);
480 #elif SANITIZER_FREEBSD
481 return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
482 #elif SANITIZER_OPENBSD
483 (void)pid;
484 return internal_syscall(SYSCALL(thrkill), tid, sig, nullptr);
485 #elif SANITIZER_SOLARIS
486 (void)pid;
487 return thr_kill(tid, sig);
488 #endif
489 }
490 #endif
491
492 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
493 u64 NanoTime() {
494 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
495 timeval tv;
496 #else
497 kernel_timeval tv;
498 #endif
499 internal_memset(&tv, 0, sizeof(tv));
500 internal_syscall(SYSCALL(gettimeofday), &tv, 0);
501 return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
502 }
503
504 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
505 return internal_syscall(SYSCALL(clock_gettime), clk_id, tp);
506 }
507 #endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
508
509 // Like getenv, but reads env directly from /proc (on Linux) or parses the
510 // 'environ' array (on some others) and does not use libc. This function
511 // should be called first inside __asan_init.
512 const char *GetEnv(const char *name) {
513 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD || \
514 SANITIZER_SOLARIS
515 if (::environ != 0) {
516 uptr NameLen = internal_strlen(name);
517 for (char **Env = ::environ; *Env != 0; Env++) {
518 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
519 return (*Env) + NameLen + 1;
520 }
521 }
522 return 0; // Not found.
523 #elif SANITIZER_LINUX
524 static char *environ;
525 static uptr len;
526 static bool inited;
527 if (!inited) {
528 inited = true;
529 uptr environ_size;
530 if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len))
531 environ = nullptr;
532 }
533 if (!environ || len == 0) return nullptr;
534 uptr namelen = internal_strlen(name);
535 const char *p = environ;
536 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
537 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
538 const char* endp =
539 (char*)internal_memchr(p, '\0', len - (p - environ));
540 if (!endp) // this entry isn't NUL terminated
541 return nullptr;
542 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
543 return p + namelen + 1; // point after =
544 p = endp + 1;
545 }
546 return nullptr; // Not found.
547 #else
548 #error "Unsupported platform"
549 #endif
550 }
551
552 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_OPENBSD
553 extern "C" {
554 SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
555 }
556 #endif
557
558 #if !SANITIZER_GO && !SANITIZER_FREEBSD && !SANITIZER_NETBSD && \
559 !SANITIZER_OPENBSD
560 static void ReadNullSepFileToArray(const char *path, char ***arr,
561 int arr_size) {
562 char *buff;
563 uptr buff_size;
564 uptr buff_len;
565 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
566 if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) {
567 (*arr)[0] = nullptr;
568 return;
569 }
570 (*arr)[0] = buff;
571 int count, i;
572 for (count = 1, i = 1; ; i++) {
573 if (buff[i] == 0) {
574 if (buff[i+1] == 0) break;
575 (*arr)[count] = &buff[i+1];
576 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
577 count++;
578 }
579 }
580 (*arr)[count] = nullptr;
581 }
582 #endif
583
584 #if !SANITIZER_OPENBSD
585 static void GetArgsAndEnv(char ***argv, char ***envp) {
586 #if SANITIZER_FREEBSD
587 // On FreeBSD, retrieving the argument and environment arrays is done via the
588 // kern.ps_strings sysctl, which returns a pointer to a structure containing
589 // this information. See also <sys/exec.h>.
590 ps_strings *pss;
591 uptr sz = sizeof(pss);
592 if (internal_sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1) {
593 Printf("sysctl kern.ps_strings failed\n");
594 Die();
595 }
596 *argv = pss->ps_argvstr;
597 *envp = pss->ps_envstr;
598 #elif SANITIZER_NETBSD
599 *argv = __ps_strings->ps_argvstr;
600 *envp = __ps_strings->ps_envstr;
601 #else // SANITIZER_FREEBSD
602 #if !SANITIZER_GO
603 if (&__libc_stack_end) {
604 #endif // !SANITIZER_GO
605 uptr* stack_end = (uptr*)__libc_stack_end;
606 int argc = *stack_end;
607 *argv = (char**)(stack_end + 1);
608 *envp = (char**)(stack_end + argc + 2);
609 #if !SANITIZER_GO
610 } else {
611 static const int kMaxArgv = 2000, kMaxEnvp = 2000;
612 ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
613 ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
614 }
615 #endif // !SANITIZER_GO
616 #endif // SANITIZER_FREEBSD
617 }
618
619 char **GetArgv() {
620 char **argv, **envp;
621 GetArgsAndEnv(&argv, &envp);
622 return argv;
623 }
624
625 char **GetEnviron() {
626 char **argv, **envp;
627 GetArgsAndEnv(&argv, &envp);
628 return envp;
629 }
630
631 #endif // !SANITIZER_OPENBSD
632
633 #if !SANITIZER_SOLARIS
634 enum MutexState {
635 MtxUnlocked = 0,
636 MtxLocked = 1,
637 MtxSleeping = 2
638 };
639
640 BlockingMutex::BlockingMutex() {
641 internal_memset(this, 0, sizeof(*this));
642 }
643
644 void BlockingMutex::Lock() {
645 CHECK_EQ(owner_, 0);
646 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
647 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
648 return;
649 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
650 #if SANITIZER_FREEBSD
651 _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
652 #elif SANITIZER_NETBSD
653 sched_yield(); /* No userspace futex-like synchronization */
654 #else
655 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT_PRIVATE, MtxSleeping,
656 0, 0, 0);
657 #endif
658 }
659 }
660
661 void BlockingMutex::Unlock() {
662 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
663 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release);
664 CHECK_NE(v, MtxUnlocked);
665 if (v == MtxSleeping) {
666 #if SANITIZER_FREEBSD
667 _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
668 #elif SANITIZER_NETBSD
669 /* No userspace futex-like synchronization */
670 #else
671 internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE_PRIVATE, 1, 0, 0, 0);
672 #endif
673 }
674 }
675
676 void BlockingMutex::CheckLocked() {
677 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
678 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
679 }
680 #endif // !SANITIZER_SOLARIS
681
682 // ----------------- sanitizer_linux.h
683 // The actual size of this structure is specified by d_reclen.
684 // Note that getdents64 uses a different structure format. We only provide the
685 // 32-bit syscall here.
686 #if SANITIZER_NETBSD
687 // Not used
688 #elif SANITIZER_OPENBSD
689 // struct dirent is different for Linux and us. At this moment, we use only
690 // d_fileno (Linux call this d_ino), d_reclen, and d_name.
691 struct linux_dirent {
692 u64 d_ino; // d_fileno
693 u16 d_reclen;
694 u16 d_namlen; // not used
695 u8 d_type; // not used
696 char d_name[NAME_MAX + 1];
697 };
698 #else
699 struct linux_dirent {
700 #if SANITIZER_X32 || defined(__aarch64__)
701 u64 d_ino;
702 u64 d_off;
703 #else
704 unsigned long d_ino;
705 unsigned long d_off;
706 #endif
707 unsigned short d_reclen;
708 #ifdef __aarch64__
709 unsigned char d_type;
710 #endif
711 char d_name[256];
712 };
713 #endif
714
715 #if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
716 // Syscall wrappers.
717 uptr internal_ptrace(int request, int pid, void *addr, void *data) {
718 return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
719 (uptr)data);
720 }
721
722 uptr internal_waitpid(int pid, int *status, int options) {
723 return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
724 0 /* rusage */);
725 }
726
727 uptr internal_getpid() {
728 return internal_syscall(SYSCALL(getpid));
729 }
730
731 uptr internal_getppid() {
732 return internal_syscall(SYSCALL(getppid));
733 }
734
735 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
736 #if SANITIZER_FREEBSD
737 return internal_syscall(SYSCALL(getdirentries), fd, (uptr)dirp, count, NULL);
738 #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
739 return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
740 #else
741 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
742 #endif
743 }
744
745 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
746 return internal_syscall(SYSCALL(lseek), fd, offset, whence);
747 }
748
749 #if SANITIZER_LINUX
750 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
751 return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
752 }
753 #endif
754
755 uptr internal_sigaltstack(const void *ss, void *oss) {
756 return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
757 }
758
759 int internal_fork() {
760 #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
761 return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
762 #else
763 return internal_syscall(SYSCALL(fork));
764 #endif
765 }
766
767 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
768 int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
769 uptr *oldlenp, const void *newp, uptr newlen) {
770 #if SANITIZER_OPENBSD
771 return sysctl(name, namelen, oldp, (size_t *)oldlenp, (void *)newp,
772 (size_t)newlen);
773 #else
774 return internal_syscall(SYSCALL(__sysctl), name, namelen, oldp,
775 (size_t *)oldlenp, newp, (size_t)newlen);
776 #endif
777 }
778
779 #if SANITIZER_FREEBSD
780 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
781 const void *newp, uptr newlen) {
782 static decltype(sysctlbyname) *real = nullptr;
783 if (!real)
784 real = (decltype(sysctlbyname) *)dlsym(RTLD_NEXT, "sysctlbyname");
785 CHECK(real);
786 return real(sname, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
787 }
788 #endif
789 #endif
790
791 #if SANITIZER_LINUX
792 #define SA_RESTORER 0x04000000
793 // Doesn't set sa_restorer if the caller did not set it, so use with caution
794 //(see below).
795 int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
796 __sanitizer_kernel_sigaction_t k_act, k_oldact;
797 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
798 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
799 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
800 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
801 if (u_act) {
802 k_act.handler = u_act->handler;
803 k_act.sigaction = u_act->sigaction;
804 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
805 sizeof(__sanitizer_kernel_sigset_t));
806 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
807 k_act.sa_flags = u_act->sa_flags | SA_RESTORER;
808 // FIXME: most often sa_restorer is unset, however the kernel requires it
809 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
810 // If sa_restorer passed to the kernel is NULL, the program may crash upon
811 // signal delivery or fail to unwind the stack in the signal handler.
812 // libc implementation of sigaction() passes its own restorer to
813 // rt_sigaction, so we need to do the same (we'll need to reimplement the
814 // restorers; for x86_64 the restorer address can be obtained from
815 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
816 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
817 k_act.sa_restorer = u_act->sa_restorer;
818 #endif
819 }
820
821 uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
822 (uptr)(u_act ? &k_act : nullptr),
823 (uptr)(u_oldact ? &k_oldact : nullptr),
824 (uptr)sizeof(__sanitizer_kernel_sigset_t));
825
826 if ((result == 0) && u_oldact) {
827 u_oldact->handler = k_oldact.handler;
828 u_oldact->sigaction = k_oldact.sigaction;
829 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
830 sizeof(__sanitizer_kernel_sigset_t));
831 u_oldact->sa_flags = k_oldact.sa_flags;
832 #if !SANITIZER_ANDROID || !SANITIZER_MIPS32
833 u_oldact->sa_restorer = k_oldact.sa_restorer;
834 #endif
835 }
836 return result;
837 }
838 #endif // SANITIZER_LINUX
839
840 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
841 __sanitizer_sigset_t *oldset) {
842 #if SANITIZER_FREEBSD || SANITIZER_OPENBSD
843 return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
844 #else
845 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
846 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
847 return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how,
848 (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
849 sizeof(__sanitizer_kernel_sigset_t));
850 #endif
851 }
852
853 void internal_sigfillset(__sanitizer_sigset_t *set) {
854 internal_memset(set, 0xff, sizeof(*set));
855 }
856
857 void internal_sigemptyset(__sanitizer_sigset_t *set) {
858 internal_memset(set, 0, sizeof(*set));
859 }
860
861 #if SANITIZER_LINUX
862 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
863 signum -= 1;
864 CHECK_GE(signum, 0);
865 CHECK_LT(signum, sizeof(*set) * 8);
866 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
867 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
868 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
869 k_set->sig[idx] &= ~(1 << bit);
870 }
871
872 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
873 signum -= 1;
874 CHECK_GE(signum, 0);
875 CHECK_LT(signum, sizeof(*set) * 8);
876 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
877 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
878 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
879 return k_set->sig[idx] & (1 << bit);
880 }
881 #elif SANITIZER_FREEBSD
882 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
883 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
884 sigdelset(rset, signum);
885 }
886
887 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
888 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
889 return sigismember(rset, signum);
890 }
891 #endif
892 #endif // !SANITIZER_SOLARIS
893
894 #if !SANITIZER_NETBSD
895 // ThreadLister implementation.
896 ThreadLister::ThreadLister(pid_t pid) : pid_(pid), buffer_(4096) {
897 char task_directory_path[80];
898 internal_snprintf(task_directory_path, sizeof(task_directory_path),
899 "/proc/%d/task/", pid);
900 descriptor_ = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
901 if (internal_iserror(descriptor_)) {
902 Report("Can't open /proc/%d/task for reading.\n", pid);
903 }
904 }
905
906 ThreadLister::Result ThreadLister::ListThreads(
907 InternalMmapVector<tid_t> *threads) {
908 if (internal_iserror(descriptor_))
909 return Error;
910 internal_lseek(descriptor_, 0, SEEK_SET);
911 threads->clear();
912
913 Result result = Ok;
914 for (bool first_read = true;; first_read = false) {
915 // Resize to max capacity if it was downsized by IsAlive.
916 buffer_.resize(buffer_.capacity());
917 CHECK_GE(buffer_.size(), 4096);
918 uptr read = internal_getdents(
919 descriptor_, (struct linux_dirent *)buffer_.data(), buffer_.size());
920 if (!read)
921 return result;
922 if (internal_iserror(read)) {
923 Report("Can't read directory entries from /proc/%d/task.\n", pid_);
924 return Error;
925 }
926
927 for (uptr begin = (uptr)buffer_.data(), end = begin + read; begin < end;) {
928 struct linux_dirent *entry = (struct linux_dirent *)begin;
929 begin += entry->d_reclen;
930 if (entry->d_ino == 1) {
931 // Inode 1 is for bad blocks and also can be a reason for early return.
932 // Should be emitted if kernel tried to output terminating thread.
933 // See proc_task_readdir implementation in Linux.
934 result = Incomplete;
935 }
936 if (entry->d_ino && *entry->d_name >= '0' && *entry->d_name <= '9')
937 threads->push_back(internal_atoll(entry->d_name));
938 }
939
940 // Now we are going to detect short-read or early EOF. In such cases Linux
941 // can return inconsistent list with missing alive threads.
942 // Code will just remember that the list can be incomplete but it will
943 // continue reads to return as much as possible.
944 if (!first_read) {
945 // The first one was a short-read by definition.
946 result = Incomplete;
947 } else if (read > buffer_.size() - 1024) {
948 // Read was close to the buffer size. So double the size and assume the
949 // worst.
950 buffer_.resize(buffer_.size() * 2);
951 result = Incomplete;
952 } else if (!threads->empty() && !IsAlive(threads->back())) {
953 // Maybe Linux early returned from read on terminated thread (!pid_alive)
954 // and failed to restore read position.
955 // See next_tid and proc_task_instantiate in Linux.
956 result = Incomplete;
957 }
958 }
959 }
960
961 bool ThreadLister::IsAlive(int tid) {
962 // /proc/%d/task/%d/status uses same call to detect alive threads as
963 // proc_task_readdir. See task_state implementation in Linux.
964 char path[80];
965 internal_snprintf(path, sizeof(path), "/proc/%d/task/%d/status", pid_, tid);
966 if (!ReadFileToVector(path, &buffer_) || buffer_.empty())
967 return false;
968 buffer_.push_back(0);
969 static const char kPrefix[] = "\nPPid:";
970 const char *field = internal_strstr(buffer_.data(), kPrefix);
971 if (!field)
972 return false;
973 field += internal_strlen(kPrefix);
974 return (int)internal_atoll(field) != 0;
975 }
976
977 ThreadLister::~ThreadLister() {
978 if (!internal_iserror(descriptor_))
979 internal_close(descriptor_);
980 }
981 #endif
982
983 #if SANITIZER_WORDSIZE == 32
984 // Take care of unusable kernel area in top gigabyte.
985 static uptr GetKernelAreaSize() {
986 #if SANITIZER_LINUX && !SANITIZER_X32
987 const uptr gbyte = 1UL << 30;
988
989 // Firstly check if there are writable segments
990 // mapped to top gigabyte (e.g. stack).
991 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
992 if (proc_maps.Error())
993 return 0;
994 MemoryMappedSegment segment;
995 while (proc_maps.Next(&segment)) {
996 if ((segment.end >= 3 * gbyte) && segment.IsWritable()) return 0;
997 }
998
999 #if !SANITIZER_ANDROID
1000 // Even if nothing is mapped, top Gb may still be accessible
1001 // if we are running on 64-bit kernel.
1002 // Uname may report misleading results if personality type
1003 // is modified (e.g. under schroot) so check this as well.
1004 struct utsname uname_info;
1005 int pers = personality(0xffffffffUL);
1006 if (!(pers & PER_MASK)
1007 && uname(&uname_info) == 0
1008 && internal_strstr(uname_info.machine, "64"))
1009 return 0;
1010 #endif // SANITIZER_ANDROID
1011
1012 // Top gigabyte is reserved for kernel.
1013 return gbyte;
1014 #else
1015 return 0;
1016 #endif // SANITIZER_LINUX && !SANITIZER_X32
1017 }
1018 #endif // SANITIZER_WORDSIZE == 32
1019
1020 uptr GetMaxVirtualAddress() {
1021 #if (SANITIZER_NETBSD || SANITIZER_OPENBSD) && defined(__x86_64__)
1022 return 0x7f7ffffff000ULL; // (0x00007f8000000000 - PAGE_SIZE)
1023 #elif SANITIZER_WORDSIZE == 64
1024 # if defined(__powerpc64__) || defined(__aarch64__)
1025 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
1026 // We somehow need to figure out which one we are using now and choose
1027 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
1028 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
1029 // of the address space, so simply checking the stack address is not enough.
1030 // This should (does) work for both PowerPC64 Endian modes.
1031 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
1032 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1033 # elif defined(__mips64)
1034 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
1035 # elif defined(__s390x__)
1036 return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
1037 #elif defined(__sparc__)
1038 return ~(uptr)0;
1039 # else
1040 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1041 # endif
1042 #else // SANITIZER_WORDSIZE == 32
1043 # if defined(__s390__)
1044 return (1ULL << 31) - 1; // 0x7fffffff;
1045 # else
1046 return (1ULL << 32) - 1; // 0xffffffff;
1047 # endif
1048 #endif // SANITIZER_WORDSIZE
1049 }
1050
1051 uptr GetMaxUserVirtualAddress() {
1052 uptr addr = GetMaxVirtualAddress();
1053 #if SANITIZER_WORDSIZE == 32 && !defined(__s390__)
1054 if (!common_flags()->full_address_space)
1055 addr -= GetKernelAreaSize();
1056 CHECK_LT(reinterpret_cast<uptr>(&addr), addr);
1057 #endif
1058 return addr;
1059 }
1060
1061 #if !SANITIZER_ANDROID
1062 uptr GetPageSize() {
1063 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
1064 return EXEC_PAGESIZE;
1065 #elif SANITIZER_USE_GETAUXVAL
1066 return getauxval(AT_PAGESZ);
1067 #elif SANITIZER_FREEBSD || SANITIZER_NETBSD
1068 // Use sysctl as sysconf can trigger interceptors internally.
1069 int pz = 0;
1070 uptr pzl = sizeof(pz);
1071 int mib[2] = {CTL_HW, HW_PAGESIZE};
1072 int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
1073 CHECK_EQ(rv, 0);
1074 return (uptr)pz;
1075 #else
1076 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
1077 #endif
1078 }
1079 #endif // !SANITIZER_ANDROID
1080
1081 #if !SANITIZER_OPENBSD
1082 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
1083 #if SANITIZER_SOLARIS
1084 const char *default_module_name = getexecname();
1085 CHECK_NE(default_module_name, NULL);
1086 return internal_snprintf(buf, buf_len, "%s", default_module_name);
1087 #else
1088 #if SANITIZER_FREEBSD || SANITIZER_NETBSD
1089 #if SANITIZER_FREEBSD
1090 const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
1091 #else
1092 const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
1093 #endif
1094 const char *default_module_name = "kern.proc.pathname";
1095 uptr Size = buf_len;
1096 bool IsErr =
1097 (internal_sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
1098 int readlink_error = IsErr ? errno : 0;
1099 uptr module_name_len = Size;
1100 #else
1101 const char *default_module_name = "/proc/self/exe";
1102 uptr module_name_len = internal_readlink(
1103 default_module_name, buf, buf_len);
1104 int readlink_error;
1105 bool IsErr = internal_iserror(module_name_len, &readlink_error);
1106 #endif // SANITIZER_SOLARIS
1107 if (IsErr) {
1108 // We can't read binary name for some reason, assume it's unknown.
1109 Report("WARNING: reading executable name failed with errno %d, "
1110 "some stack frames may not be symbolized\n", readlink_error);
1111 module_name_len = internal_snprintf(buf, buf_len, "%s",
1112 default_module_name);
1113 CHECK_LT(module_name_len, buf_len);
1114 }
1115 return module_name_len;
1116 #endif
1117 }
1118 #endif // !SANITIZER_OPENBSD
1119
1120 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
1121 #if SANITIZER_LINUX
1122 char *tmpbuf;
1123 uptr tmpsize;
1124 uptr tmplen;
1125 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
1126 1024 * 1024)) {
1127 internal_strncpy(buf, tmpbuf, buf_len);
1128 UnmapOrDie(tmpbuf, tmpsize);
1129 return internal_strlen(buf);
1130 }
1131 #endif
1132 return ReadBinaryName(buf, buf_len);
1133 }
1134
1135 // Match full names of the form /path/to/base_name{-,.}*
1136 bool LibraryNameIs(const char *full_name, const char *base_name) {
1137 const char *name = full_name;
1138 // Strip path.
1139 while (*name != '\0') name++;
1140 while (name > full_name && *name != '/') name--;
1141 if (*name == '/') name++;
1142 uptr base_name_length = internal_strlen(base_name);
1143 if (internal_strncmp(name, base_name, base_name_length)) return false;
1144 return (name[base_name_length] == '-' || name[base_name_length] == '.');
1145 }
1146
1147 #if !SANITIZER_ANDROID
1148 // Call cb for each region mapped by map.
1149 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
1150 CHECK_NE(map, nullptr);
1151 #if !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
1152 typedef ElfW(Phdr) Elf_Phdr;
1153 typedef ElfW(Ehdr) Elf_Ehdr;
1154 #endif // !SANITIZER_FREEBSD && !SANITIZER_OPENBSD
1155 char *base = (char *)map->l_addr;
1156 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
1157 char *phdrs = base + ehdr->e_phoff;
1158 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
1159
1160 // Find the segment with the minimum base so we can "relocate" the p_vaddr
1161 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
1162 // objects have a non-zero base.
1163 uptr preferred_base = (uptr)-1;
1164 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1165 Elf_Phdr *phdr = (Elf_Phdr *)iter;
1166 if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
1167 preferred_base = (uptr)phdr->p_vaddr;
1168 }
1169
1170 // Compute the delta from the real base to get a relocation delta.
1171 sptr delta = (uptr)base - preferred_base;
1172 // Now we can figure out what the loader really mapped.
1173 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1174 Elf_Phdr *phdr = (Elf_Phdr *)iter;
1175 if (phdr->p_type == PT_LOAD) {
1176 uptr seg_start = phdr->p_vaddr + delta;
1177 uptr seg_end = seg_start + phdr->p_memsz;
1178 // None of these values are aligned. We consider the ragged edges of the
1179 // load command as defined, since they are mapped from the file.
1180 seg_start = RoundDownTo(seg_start, GetPageSizeCached());
1181 seg_end = RoundUpTo(seg_end, GetPageSizeCached());
1182 cb((void *)seg_start, seg_end - seg_start);
1183 }
1184 }
1185 }
1186 #endif
1187
1188 #if defined(__x86_64__) && SANITIZER_LINUX
1189 // We cannot use glibc's clone wrapper, because it messes with the child
1190 // task's TLS. It writes the PID and TID of the child task to its thread
1191 // descriptor, but in our case the child task shares the thread descriptor with
1192 // the parent (because we don't know how to allocate a new thread
1193 // descriptor to keep glibc happy). So the stock version of clone(), when
1194 // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
1195 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1196 int *parent_tidptr, void *newtls, int *child_tidptr) {
1197 long long res;
1198 if (!fn || !child_stack)
1199 return -EINVAL;
1200 CHECK_EQ(0, (uptr)child_stack % 16);
1201 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1202 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1203 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1204 register void *r8 __asm__("r8") = newtls;
1205 register int *r10 __asm__("r10") = child_tidptr;
1206 __asm__ __volatile__(
1207 /* %rax = syscall(%rax = SYSCALL(clone),
1208 * %rdi = flags,
1209 * %rsi = child_stack,
1210 * %rdx = parent_tidptr,
1211 * %r8 = new_tls,
1212 * %r10 = child_tidptr)
1213 */
1214 "syscall\n"
1215
1216 /* if (%rax != 0)
1217 * return;
1218 */
1219 "testq %%rax,%%rax\n"
1220 "jnz 1f\n"
1221
1222 /* In the child. Terminate unwind chain. */
1223 // XXX: We should also terminate the CFI unwind chain
1224 // here. Unfortunately clang 3.2 doesn't support the
1225 // necessary CFI directives, so we skip that part.
1226 "xorq %%rbp,%%rbp\n"
1227
1228 /* Call "fn(arg)". */
1229 "popq %%rax\n"
1230 "popq %%rdi\n"
1231 "call *%%rax\n"
1232
1233 /* Call _exit(%rax). */
1234 "movq %%rax,%%rdi\n"
1235 "movq %2,%%rax\n"
1236 "syscall\n"
1237
1238 /* Return to parent. */
1239 "1:\n"
1240 : "=a" (res)
1241 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
1242 "S"(child_stack),
1243 "D"(flags),
1244 "d"(parent_tidptr),
1245 "r"(r8),
1246 "r"(r10)
1247 : "memory", "r11", "rcx");
1248 return res;
1249 }
1250 #elif defined(__mips__)
1251 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1252 int *parent_tidptr, void *newtls, int *child_tidptr) {
1253 long long res;
1254 if (!fn || !child_stack)
1255 return -EINVAL;
1256 CHECK_EQ(0, (uptr)child_stack % 16);
1257 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1258 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1259 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1260 register void *a3 __asm__("$7") = newtls;
1261 register int *a4 __asm__("$8") = child_tidptr;
1262 // We don't have proper CFI directives here because it requires alot of code
1263 // for very marginal benefits.
1264 __asm__ __volatile__(
1265 /* $v0 = syscall($v0 = __NR_clone,
1266 * $a0 = flags,
1267 * $a1 = child_stack,
1268 * $a2 = parent_tidptr,
1269 * $a3 = new_tls,
1270 * $a4 = child_tidptr)
1271 */
1272 ".cprestore 16;\n"
1273 "move $4,%1;\n"
1274 "move $5,%2;\n"
1275 "move $6,%3;\n"
1276 "move $7,%4;\n"
1277 /* Store the fifth argument on stack
1278 * if we are using 32-bit abi.
1279 */
1280 #if SANITIZER_WORDSIZE == 32
1281 "lw %5,16($29);\n"
1282 #else
1283 "move $8,%5;\n"
1284 #endif
1285 "li $2,%6;\n"
1286 "syscall;\n"
1287
1288 /* if ($v0 != 0)
1289 * return;
1290 */
1291 "bnez $2,1f;\n"
1292
1293 /* Call "fn(arg)". */
1294 #if SANITIZER_WORDSIZE == 32
1295 #ifdef __BIG_ENDIAN__
1296 "lw $25,4($29);\n"
1297 "lw $4,12($29);\n"
1298 #else
1299 "lw $25,0($29);\n"
1300 "lw $4,8($29);\n"
1301 #endif
1302 #else
1303 "ld $25,0($29);\n"
1304 "ld $4,8($29);\n"
1305 #endif
1306 "jal $25;\n"
1307
1308 /* Call _exit($v0). */
1309 "move $4,$2;\n"
1310 "li $2,%7;\n"
1311 "syscall;\n"
1312
1313 /* Return to parent. */
1314 "1:\n"
1315 : "=r" (res)
1316 : "r"(flags),
1317 "r"(child_stack),
1318 "r"(parent_tidptr),
1319 "r"(a3),
1320 "r"(a4),
1321 "i"(__NR_clone),
1322 "i"(__NR_exit)
1323 : "memory", "$29" );
1324 return res;
1325 }
1326 #elif defined(__aarch64__)
1327 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1328 int *parent_tidptr, void *newtls, int *child_tidptr) {
1329 long long res;
1330 if (!fn || !child_stack)
1331 return -EINVAL;
1332 CHECK_EQ(0, (uptr)child_stack % 16);
1333 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1334 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1335 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1336
1337 register int (*__fn)(void *) __asm__("x0") = fn;
1338 register void *__stack __asm__("x1") = child_stack;
1339 register int __flags __asm__("x2") = flags;
1340 register void *__arg __asm__("x3") = arg;
1341 register int *__ptid __asm__("x4") = parent_tidptr;
1342 register void *__tls __asm__("x5") = newtls;
1343 register int *__ctid __asm__("x6") = child_tidptr;
1344
1345 __asm__ __volatile__(
1346 "mov x0,x2\n" /* flags */
1347 "mov x2,x4\n" /* ptid */
1348 "mov x3,x5\n" /* tls */
1349 "mov x4,x6\n" /* ctid */
1350 "mov x8,%9\n" /* clone */
1351
1352 "svc 0x0\n"
1353
1354 /* if (%r0 != 0)
1355 * return %r0;
1356 */
1357 "cmp x0, #0\n"
1358 "bne 1f\n"
1359
1360 /* In the child, now. Call "fn(arg)". */
1361 "ldp x1, x0, [sp], #16\n"
1362 "blr x1\n"
1363
1364 /* Call _exit(%r0). */
1365 "mov x8, %10\n"
1366 "svc 0x0\n"
1367 "1:\n"
1368
1369 : "=r" (res)
1370 : "i"(-EINVAL),
1371 "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1372 "r"(__ptid), "r"(__tls), "r"(__ctid),
1373 "i"(__NR_clone), "i"(__NR_exit)
1374 : "x30", "memory");
1375 return res;
1376 }
1377 #elif defined(__powerpc64__)
1378 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1379 int *parent_tidptr, void *newtls, int *child_tidptr) {
1380 long long res;
1381 // Stack frame structure.
1382 #if SANITIZER_PPC64V1
1383 // Back chain == 0 (SP + 112)
1384 // Frame (112 bytes):
1385 // Parameter save area (SP + 48), 8 doublewords
1386 // TOC save area (SP + 40)
1387 // Link editor doubleword (SP + 32)
1388 // Compiler doubleword (SP + 24)
1389 // LR save area (SP + 16)
1390 // CR save area (SP + 8)
1391 // Back chain (SP + 0)
1392 # define FRAME_SIZE 112
1393 # define FRAME_TOC_SAVE_OFFSET 40
1394 #elif SANITIZER_PPC64V2
1395 // Back chain == 0 (SP + 32)
1396 // Frame (32 bytes):
1397 // TOC save area (SP + 24)
1398 // LR save area (SP + 16)
1399 // CR save area (SP + 8)
1400 // Back chain (SP + 0)
1401 # define FRAME_SIZE 32
1402 # define FRAME_TOC_SAVE_OFFSET 24
1403 #else
1404 # error "Unsupported PPC64 ABI"
1405 #endif
1406 if (!fn || !child_stack)
1407 return -EINVAL;
1408 CHECK_EQ(0, (uptr)child_stack % 16);
1409
1410 register int (*__fn)(void *) __asm__("r3") = fn;
1411 register void *__cstack __asm__("r4") = child_stack;
1412 register int __flags __asm__("r5") = flags;
1413 register void *__arg __asm__("r6") = arg;
1414 register int *__ptidptr __asm__("r7") = parent_tidptr;
1415 register void *__newtls __asm__("r8") = newtls;
1416 register int *__ctidptr __asm__("r9") = child_tidptr;
1417
1418 __asm__ __volatile__(
1419 /* fn and arg are saved across the syscall */
1420 "mr 28, %5\n\t"
1421 "mr 27, %8\n\t"
1422
1423 /* syscall
1424 r0 == __NR_clone
1425 r3 == flags
1426 r4 == child_stack
1427 r5 == parent_tidptr
1428 r6 == newtls
1429 r7 == child_tidptr */
1430 "mr 3, %7\n\t"
1431 "mr 5, %9\n\t"
1432 "mr 6, %10\n\t"
1433 "mr 7, %11\n\t"
1434 "li 0, %3\n\t"
1435 "sc\n\t"
1436
1437 /* Test if syscall was successful */
1438 "cmpdi cr1, 3, 0\n\t"
1439 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1440 "bne- cr1, 1f\n\t"
1441
1442 /* Set up stack frame */
1443 "li 29, 0\n\t"
1444 "stdu 29, -8(1)\n\t"
1445 "stdu 1, -%12(1)\n\t"
1446 /* Do the function call */
1447 "std 2, %13(1)\n\t"
1448 #if SANITIZER_PPC64V1
1449 "ld 0, 0(28)\n\t"
1450 "ld 2, 8(28)\n\t"
1451 "mtctr 0\n\t"
1452 #elif SANITIZER_PPC64V2
1453 "mr 12, 28\n\t"
1454 "mtctr 12\n\t"
1455 #else
1456 # error "Unsupported PPC64 ABI"
1457 #endif
1458 "mr 3, 27\n\t"
1459 "bctrl\n\t"
1460 "ld 2, %13(1)\n\t"
1461
1462 /* Call _exit(r3) */
1463 "li 0, %4\n\t"
1464 "sc\n\t"
1465
1466 /* Return to parent */
1467 "1:\n\t"
1468 "mr %0, 3\n\t"
1469 : "=r" (res)
1470 : "0" (-1),
1471 "i" (EINVAL),
1472 "i" (__NR_clone),
1473 "i" (__NR_exit),
1474 "r" (__fn),
1475 "r" (__cstack),
1476 "r" (__flags),
1477 "r" (__arg),
1478 "r" (__ptidptr),
1479 "r" (__newtls),
1480 "r" (__ctidptr),
1481 "i" (FRAME_SIZE),
1482 "i" (FRAME_TOC_SAVE_OFFSET)
1483 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1484 return res;
1485 }
1486 #elif defined(__i386__) && SANITIZER_LINUX
1487 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1488 int *parent_tidptr, void *newtls, int *child_tidptr) {
1489 int res;
1490 if (!fn || !child_stack)
1491 return -EINVAL;
1492 CHECK_EQ(0, (uptr)child_stack % 16);
1493 child_stack = (char *)child_stack - 7 * sizeof(unsigned int);
1494 ((unsigned int *)child_stack)[0] = (uptr)flags;
1495 ((unsigned int *)child_stack)[1] = (uptr)0;
1496 ((unsigned int *)child_stack)[2] = (uptr)fn;
1497 ((unsigned int *)child_stack)[3] = (uptr)arg;
1498 __asm__ __volatile__(
1499 /* %eax = syscall(%eax = SYSCALL(clone),
1500 * %ebx = flags,
1501 * %ecx = child_stack,
1502 * %edx = parent_tidptr,
1503 * %esi = new_tls,
1504 * %edi = child_tidptr)
1505 */
1506
1507 /* Obtain flags */
1508 "movl (%%ecx), %%ebx\n"
1509 /* Do the system call */
1510 "pushl %%ebx\n"
1511 "pushl %%esi\n"
1512 "pushl %%edi\n"
1513 /* Remember the flag value. */
1514 "movl %%ebx, (%%ecx)\n"
1515 "int $0x80\n"
1516 "popl %%edi\n"
1517 "popl %%esi\n"
1518 "popl %%ebx\n"
1519
1520 /* if (%eax != 0)
1521 * return;
1522 */
1523
1524 "test %%eax,%%eax\n"
1525 "jnz 1f\n"
1526
1527 /* terminate the stack frame */
1528 "xorl %%ebp,%%ebp\n"
1529 /* Call FN. */
1530 "call *%%ebx\n"
1531 #ifdef PIC
1532 "call here\n"
1533 "here:\n"
1534 "popl %%ebx\n"
1535 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1536 #endif
1537 /* Call exit */
1538 "movl %%eax, %%ebx\n"
1539 "movl %2, %%eax\n"
1540 "int $0x80\n"
1541 "1:\n"
1542 : "=a" (res)
1543 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
1544 "c"(child_stack),
1545 "d"(parent_tidptr),
1546 "S"(newtls),
1547 "D"(child_tidptr)
1548 : "memory");
1549 return res;
1550 }
1551 #elif defined(__arm__) && SANITIZER_LINUX
1552 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1553 int *parent_tidptr, void *newtls, int *child_tidptr) {
1554 unsigned int res;
1555 if (!fn || !child_stack)
1556 return -EINVAL;
1557 child_stack = (char *)child_stack - 2 * sizeof(unsigned int);
1558 ((unsigned int *)child_stack)[0] = (uptr)fn;
1559 ((unsigned int *)child_stack)[1] = (uptr)arg;
1560 register int r0 __asm__("r0") = flags;
1561 register void *r1 __asm__("r1") = child_stack;
1562 register int *r2 __asm__("r2") = parent_tidptr;
1563 register void *r3 __asm__("r3") = newtls;
1564 register int *r4 __asm__("r4") = child_tidptr;
1565 register int r7 __asm__("r7") = __NR_clone;
1566
1567 #if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__)
1568 # define ARCH_HAS_BX
1569 #endif
1570 #if __ARM_ARCH > 4
1571 # define ARCH_HAS_BLX
1572 #endif
1573
1574 #ifdef ARCH_HAS_BX
1575 # ifdef ARCH_HAS_BLX
1576 # define BLX(R) "blx " #R "\n"
1577 # else
1578 # define BLX(R) "mov lr, pc; bx " #R "\n"
1579 # endif
1580 #else
1581 # define BLX(R) "mov lr, pc; mov pc," #R "\n"
1582 #endif
1583
1584 __asm__ __volatile__(
1585 /* %r0 = syscall(%r7 = SYSCALL(clone),
1586 * %r0 = flags,
1587 * %r1 = child_stack,
1588 * %r2 = parent_tidptr,
1589 * %r3 = new_tls,
1590 * %r4 = child_tidptr)
1591 */
1592
1593 /* Do the system call */
1594 "swi 0x0\n"
1595
1596 /* if (%r0 != 0)
1597 * return %r0;
1598 */
1599 "cmp r0, #0\n"
1600 "bne 1f\n"
1601
1602 /* In the child, now. Call "fn(arg)". */
1603 "ldr r0, [sp, #4]\n"
1604 "ldr ip, [sp], #8\n"
1605 BLX(ip)
1606 /* Call _exit(%r0). */
1607 "mov r7, %7\n"
1608 "swi 0x0\n"
1609 "1:\n"
1610 "mov %0, r0\n"
1611 : "=r"(res)
1612 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7),
1613 "i"(__NR_exit)
1614 : "memory");
1615 return res;
1616 }
1617 #endif // defined(__x86_64__) && SANITIZER_LINUX
1618
1619 #if SANITIZER_ANDROID
1620 #if __ANDROID_API__ < 21
1621 extern "C" __attribute__((weak)) int dl_iterate_phdr(
1622 int (*)(struct dl_phdr_info *, size_t, void *), void *);
1623 #endif
1624
1625 static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1626 void *data) {
1627 // Any name starting with "lib" indicates a bug in L where library base names
1628 // are returned instead of paths.
1629 if (info->dlpi_name && info->dlpi_name[0] == 'l' &&
1630 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') {
1631 *(bool *)data = true;
1632 return 1;
1633 }
1634 return 0;
1635 }
1636
1637 static atomic_uint32_t android_api_level;
1638
1639 static AndroidApiLevel AndroidDetectApiLevelStatic() {
1640 #if __ANDROID_API__ <= 19
1641 return ANDROID_KITKAT;
1642 #elif __ANDROID_API__ <= 22
1643 return ANDROID_LOLLIPOP_MR1;
1644 #else
1645 return ANDROID_POST_LOLLIPOP;
1646 #endif
1647 }
1648
1649 static AndroidApiLevel AndroidDetectApiLevel() {
1650 if (!&dl_iterate_phdr)
1651 return ANDROID_KITKAT; // K or lower
1652 bool base_name_seen = false;
1653 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen);
1654 if (base_name_seen)
1655 return ANDROID_LOLLIPOP_MR1; // L MR1
1656 return ANDROID_POST_LOLLIPOP; // post-L
1657 // Plain L (API level 21) is completely broken wrt ASan and not very
1658 // interesting to detect.
1659 }
1660
1661 extern "C" __attribute__((weak)) void* _DYNAMIC;
1662
1663 AndroidApiLevel AndroidGetApiLevel() {
1664 AndroidApiLevel level =
1665 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed);
1666 if (level) return level;
1667 level = &_DYNAMIC == nullptr ? AndroidDetectApiLevelStatic()
1668 : AndroidDetectApiLevel();
1669 atomic_store(&android_api_level, level, memory_order_relaxed);
1670 return level;
1671 }
1672
1673 #endif
1674
1675 static HandleSignalMode GetHandleSignalModeImpl(int signum) {
1676 switch (signum) {
1677 case SIGABRT:
1678 return common_flags()->handle_abort;
1679 case SIGILL:
1680 return common_flags()->handle_sigill;
1681 case SIGTRAP:
1682 return common_flags()->handle_sigtrap;
1683 case SIGFPE:
1684 return common_flags()->handle_sigfpe;
1685 case SIGSEGV:
1686 return common_flags()->handle_segv;
1687 case SIGBUS:
1688 return common_flags()->handle_sigbus;
1689 }
1690 return kHandleSignalNo;
1691 }
1692
1693 HandleSignalMode GetHandleSignalMode(int signum) {
1694 HandleSignalMode result = GetHandleSignalModeImpl(signum);
1695 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
1696 return kHandleSignalExclusive;
1697 return result;
1698 }
1699
1700 #if !SANITIZER_GO
1701 void *internal_start_thread(void(*func)(void *arg), void *arg) {
1702 // Start the thread with signals blocked, otherwise it can steal user signals.
1703 __sanitizer_sigset_t set, old;
1704 internal_sigfillset(&set);
1705 #if SANITIZER_LINUX && !SANITIZER_ANDROID
1706 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
1707 // on any thread, setuid call hangs (see test/tsan/setuid.c).
1708 internal_sigdelset(&set, 33);
1709 #endif
1710 internal_sigprocmask(SIG_SETMASK, &set, &old);
1711 void *th;
1712 real_pthread_create(&th, nullptr, (void*(*)(void *arg))func, arg);
1713 internal_sigprocmask(SIG_SETMASK, &old, nullptr);
1714 return th;
1715 }
1716
1717 void internal_join_thread(void *th) {
1718 real_pthread_join(th, nullptr);
1719 }
1720 #else
1721 void *internal_start_thread(void (*func)(void *), void *arg) { return 0; }
1722
1723 void internal_join_thread(void *th) {}
1724 #endif
1725
1726 #if defined(__aarch64__)
1727 // Android headers in the older NDK releases miss this definition.
1728 struct __sanitizer_esr_context {
1729 struct _aarch64_ctx head;
1730 uint64_t esr;
1731 };
1732
1733 static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1734 static const u32 kEsrMagic = 0x45535201;
1735 u8 *aux = ucontext->uc_mcontext.__reserved;
1736 while (true) {
1737 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1738 if (ctx->size == 0) break;
1739 if (ctx->magic == kEsrMagic) {
1740 *esr = ((__sanitizer_esr_context *)ctx)->esr;
1741 return true;
1742 }
1743 aux += ctx->size;
1744 }
1745 return false;
1746 }
1747 #endif
1748
1749 #if SANITIZER_OPENBSD
1750 using Context = sigcontext;
1751 #else
1752 using Context = ucontext_t;
1753 #endif
1754
1755 SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
1756 Context *ucontext = (Context *)context;
1757 #if defined(__x86_64__) || defined(__i386__)
1758 static const uptr PF_WRITE = 1U << 1;
1759 #if SANITIZER_FREEBSD
1760 uptr err = ucontext->uc_mcontext.mc_err;
1761 #elif SANITIZER_NETBSD
1762 uptr err = ucontext->uc_mcontext.__gregs[_REG_ERR];
1763 #elif SANITIZER_OPENBSD
1764 uptr err = ucontext->sc_err;
1765 #elif SANITIZER_SOLARIS && defined(__i386__)
1766 const int Err = 13;
1767 uptr err = ucontext->uc_mcontext.gregs[Err];
1768 #else
1769 uptr err = ucontext->uc_mcontext.gregs[REG_ERR];
1770 #endif // SANITIZER_FREEBSD
1771 return err & PF_WRITE ? WRITE : READ;
1772 #elif defined(__mips__)
1773 uint32_t *exception_source;
1774 uint32_t faulty_instruction;
1775 uint32_t op_code;
1776
1777 exception_source = (uint32_t *)ucontext->uc_mcontext.pc;
1778 faulty_instruction = (uint32_t)(*exception_source);
1779
1780 op_code = (faulty_instruction >> 26) & 0x3f;
1781
1782 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions.
1783 switch (op_code) {
1784 case 0x28: // sb
1785 case 0x29: // sh
1786 case 0x2b: // sw
1787 case 0x3f: // sd
1788 #if __mips_isa_rev < 6
1789 case 0x2c: // sdl
1790 case 0x2d: // sdr
1791 case 0x2a: // swl
1792 case 0x2e: // swr
1793 #endif
1794 return SignalContext::WRITE;
1795
1796 case 0x20: // lb
1797 case 0x24: // lbu
1798 case 0x21: // lh
1799 case 0x25: // lhu
1800 case 0x23: // lw
1801 case 0x27: // lwu
1802 case 0x37: // ld
1803 #if __mips_isa_rev < 6
1804 case 0x1a: // ldl
1805 case 0x1b: // ldr
1806 case 0x22: // lwl
1807 case 0x26: // lwr
1808 #endif
1809 return SignalContext::READ;
1810 #if __mips_isa_rev == 6
1811 case 0x3b: // pcrel
1812 op_code = (faulty_instruction >> 19) & 0x3;
1813 switch (op_code) {
1814 case 0x1: // lwpc
1815 case 0x2: // lwupc
1816 return SignalContext::READ;
1817 }
1818 #endif
1819 }
1820 return SignalContext::UNKNOWN;
1821 #elif defined(__arm__)
1822 static const uptr FSR_WRITE = 1U << 11;
1823 uptr fsr = ucontext->uc_mcontext.error_code;
1824 return fsr & FSR_WRITE ? WRITE : READ;
1825 #elif defined(__aarch64__)
1826 static const u64 ESR_ELx_WNR = 1U << 6;
1827 u64 esr;
1828 if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN;
1829 return esr & ESR_ELx_WNR ? WRITE : READ;
1830 #elif defined(__sparc__)
1831 // Decode the instruction to determine the access type.
1832 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype).
1833 #if SANITIZER_SOLARIS
1834 uptr pc = ucontext->uc_mcontext.gregs[REG_PC];
1835 #else
1836 // Historical BSDism here.
1837 struct sigcontext *scontext = (struct sigcontext *)context;
1838 #if defined(__arch64__)
1839 uptr pc = scontext->sigc_regs.tpc;
1840 #else
1841 uptr pc = scontext->si_regs.pc;
1842 #endif
1843 #endif
1844 u32 instr = *(u32 *)pc;
1845 return (instr >> 21) & 1 ? WRITE: READ;
1846 #else
1847 (void)ucontext;
1848 return UNKNOWN; // FIXME: Implement.
1849 #endif
1850 }
1851
1852 void SignalContext::DumpAllRegisters(void *context) {
1853 // FIXME: Implement this.
1854 }
1855
1856 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
1857 #if SANITIZER_NETBSD
1858 // This covers all NetBSD architectures
1859 ucontext_t *ucontext = (ucontext_t *)context;
1860 *pc = _UC_MACHINE_PC(ucontext);
1861 *bp = _UC_MACHINE_FP(ucontext);
1862 *sp = _UC_MACHINE_SP(ucontext);
1863 #elif defined(__arm__)
1864 ucontext_t *ucontext = (ucontext_t*)context;
1865 *pc = ucontext->uc_mcontext.arm_pc;
1866 *bp = ucontext->uc_mcontext.arm_fp;
1867 *sp = ucontext->uc_mcontext.arm_sp;
1868 #elif defined(__aarch64__)
1869 ucontext_t *ucontext = (ucontext_t*)context;
1870 *pc = ucontext->uc_mcontext.pc;
1871 *bp = ucontext->uc_mcontext.regs[29];
1872 *sp = ucontext->uc_mcontext.sp;
1873 #elif defined(__hppa__)
1874 ucontext_t *ucontext = (ucontext_t*)context;
1875 *pc = ucontext->uc_mcontext.sc_iaoq[0];
1876 /* GCC uses %r3 whenever a frame pointer is needed. */
1877 *bp = ucontext->uc_mcontext.sc_gr[3];
1878 *sp = ucontext->uc_mcontext.sc_gr[30];
1879 #elif defined(__x86_64__)
1880 # if SANITIZER_FREEBSD
1881 ucontext_t *ucontext = (ucontext_t*)context;
1882 *pc = ucontext->uc_mcontext.mc_rip;
1883 *bp = ucontext->uc_mcontext.mc_rbp;
1884 *sp = ucontext->uc_mcontext.mc_rsp;
1885 #elif SANITIZER_OPENBSD
1886 sigcontext *ucontext = (sigcontext *)context;
1887 *pc = ucontext->sc_rip;
1888 *bp = ucontext->sc_rbp;
1889 *sp = ucontext->sc_rsp;
1890 # else
1891 ucontext_t *ucontext = (ucontext_t*)context;
1892 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
1893 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
1894 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
1895 # endif
1896 #elif defined(__i386__)
1897 # if SANITIZER_FREEBSD
1898 ucontext_t *ucontext = (ucontext_t*)context;
1899 *pc = ucontext->uc_mcontext.mc_eip;
1900 *bp = ucontext->uc_mcontext.mc_ebp;
1901 *sp = ucontext->uc_mcontext.mc_esp;
1902 #elif SANITIZER_OPENBSD
1903 sigcontext *ucontext = (sigcontext *)context;
1904 *pc = ucontext->sc_eip;
1905 *bp = ucontext->sc_ebp;
1906 *sp = ucontext->sc_esp;
1907 # else
1908 ucontext_t *ucontext = (ucontext_t*)context;
1909 # if SANITIZER_SOLARIS
1910 /* Use the numeric values: the symbolic ones are undefined by llvm
1911 include/llvm/Support/Solaris.h. */
1912 # ifndef REG_EIP
1913 # define REG_EIP 14 // REG_PC
1914 # endif
1915 # ifndef REG_EBP
1916 # define REG_EBP 6 // REG_FP
1917 # endif
1918 # ifndef REG_ESP
1919 # define REG_ESP 17 // REG_SP
1920 # endif
1921 # endif
1922 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
1923 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
1924 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
1925 # endif
1926 #elif defined(__powerpc__) || defined(__powerpc64__)
1927 ucontext_t *ucontext = (ucontext_t*)context;
1928 *pc = ucontext->uc_mcontext.regs->nip;
1929 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
1930 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
1931 // pointer, but GCC always uses r31 when we need a frame pointer.
1932 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
1933 #elif defined(__sparc__)
1934 #if defined(__arch64__) || defined(__sparcv9)
1935 #define STACK_BIAS 2047
1936 #else
1937 #define STACK_BIAS 0
1938 # endif
1939 # if SANITIZER_SOLARIS
1940 ucontext_t *ucontext = (ucontext_t *)context;
1941 *pc = ucontext->uc_mcontext.gregs[REG_PC];
1942 *sp = ucontext->uc_mcontext.gregs[REG_O6] + STACK_BIAS;
1943 #else
1944 // Historical BSDism here.
1945 struct sigcontext *scontext = (struct sigcontext *)context;
1946 #if defined(__arch64__)
1947 *pc = scontext->sigc_regs.tpc;
1948 *sp = scontext->sigc_regs.u_regs[14] + STACK_BIAS;
1949 #else
1950 *pc = scontext->si_regs.pc;
1951 *sp = scontext->si_regs.u_regs[14];
1952 #endif
1953 # endif
1954 *bp = (uptr)((uhwptr *)*sp)[14] + STACK_BIAS;
1955 #elif defined(__mips__)
1956 ucontext_t *ucontext = (ucontext_t*)context;
1957 *pc = ucontext->uc_mcontext.pc;
1958 *bp = ucontext->uc_mcontext.gregs[30];
1959 *sp = ucontext->uc_mcontext.gregs[29];
1960 #elif defined(__s390__)
1961 ucontext_t *ucontext = (ucontext_t*)context;
1962 # if defined(__s390x__)
1963 *pc = ucontext->uc_mcontext.psw.addr;
1964 # else
1965 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
1966 # endif
1967 *bp = ucontext->uc_mcontext.gregs[11];
1968 *sp = ucontext->uc_mcontext.gregs[15];
1969 #else
1970 # error "Unsupported arch"
1971 #endif
1972 }
1973
1974 void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
1975
1976 void InitializePlatformEarly() {
1977 // Do nothing.
1978 }
1979
1980 void MaybeReexec() {
1981 // No need to re-exec on Linux.
1982 }
1983
1984 void CheckASLR() {
1985 #if SANITIZER_NETBSD
1986 int mib[3];
1987 int paxflags;
1988 uptr len = sizeof(paxflags);
1989
1990 mib[0] = CTL_PROC;
1991 mib[1] = internal_getpid();
1992 mib[2] = PROC_PID_PAXFLAGS;
1993
1994 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
1995 Printf("sysctl failed\n");
1996 Die();
1997 }
1998
1999 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) {
2000 Printf("This sanitizer is not compatible with enabled ASLR\n");
2001 Die();
2002 }
2003 #elif SANITIZER_PPC64V2
2004 // Disable ASLR for Linux PPC64LE.
2005 int old_personality = personality(0xffffffff);
2006 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
2007 VReport(1, "WARNING: Program is being run with address space layout "
2008 "randomization (ASLR) enabled which prevents the thread and "
2009 "memory sanitizers from working on powerpc64le.\n"
2010 "ASLR will be disabled and the program re-executed.\n");
2011 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
2012 ReExec();
2013 }
2014 #else
2015 // Do nothing
2016 #endif
2017 }
2018
2019 void CheckMPROTECT() {
2020 #if SANITIZER_NETBSD
2021 int mib[3];
2022 int paxflags;
2023 uptr len = sizeof(paxflags);
2024
2025 mib[0] = CTL_PROC;
2026 mib[1] = internal_getpid();
2027 mib[2] = PROC_PID_PAXFLAGS;
2028
2029 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2030 Printf("sysctl failed\n");
2031 Die();
2032 }
2033
2034 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) {
2035 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2036 Die();
2037 }
2038 #else
2039 // Do nothing
2040 #endif
2041 }
2042
2043 void PrintModuleMap() { }
2044
2045 void CheckNoDeepBind(const char *filename, int flag) {
2046 #ifdef RTLD_DEEPBIND
2047 if (flag & RTLD_DEEPBIND) {
2048 Report(
2049 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
2050 " which is incompatibe with sanitizer runtime "
2051 "(see https://github.com/google/sanitizers/issues/611 for details"
2052 "). If you want to run %s library under sanitizers please remove "
2053 "RTLD_DEEPBIND from dlopen flags.\n",
2054 filename, filename);
2055 Die();
2056 }
2057 #endif
2058 }
2059
2060 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
2061 uptr *largest_gap_found,
2062 uptr *max_occupied_addr) {
2063 UNREACHABLE("FindAvailableMemoryRange is not available");
2064 return 0;
2065 }
2066
2067 bool GetRandom(void *buffer, uptr length, bool blocking) {
2068 if (!buffer || !length || length > 256)
2069 return false;
2070 #if SANITIZER_USE_GETENTROPY
2071 uptr rnd = getentropy(buffer, length);
2072 int rverrno = 0;
2073 if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT)
2074 return false;
2075 else if (rnd == 0)
2076 return true;
2077 #endif // SANITIZER_USE_GETENTROPY
2078
2079 #if SANITIZER_USE_GETRANDOM
2080 static atomic_uint8_t skip_getrandom_syscall;
2081 if (!atomic_load_relaxed(&skip_getrandom_syscall)) {
2082 // Up to 256 bytes, getrandom will not be interrupted.
2083 uptr res = internal_syscall(SYSCALL(getrandom), buffer, length,
2084 blocking ? 0 : GRND_NONBLOCK);
2085 int rverrno = 0;
2086 if (internal_iserror(res, &rverrno) && rverrno == ENOSYS)
2087 atomic_store_relaxed(&skip_getrandom_syscall, 1);
2088 else if (res == length)
2089 return true;
2090 }
2091 #endif // SANITIZER_USE_GETRANDOM
2092 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
2093 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
2094 uptr fd = internal_open("/dev/urandom", O_RDONLY);
2095 if (internal_iserror(fd))
2096 return false;
2097 uptr res = internal_read(fd, buffer, length);
2098 if (internal_iserror(res))
2099 return false;
2100 internal_close(fd);
2101 return true;
2102 }
2103
2104 } // namespace __sanitizer
2105
2106 #endif