Bug Summary

File:projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc
Warning:line 309, column 3
Undefined or garbage value returned to caller

Annotated Source Code

1//===-- sanitizer_linux.cc ------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer
11// run-time libraries and implements linux-specific functions from
12// sanitizer_libc.h.
13//===----------------------------------------------------------------------===//
14
15#include "sanitizer_platform.h"
16
17#if SANITIZER_FREEBSD0 || SANITIZER_LINUX1
18
19#include "sanitizer_common.h"
20#include "sanitizer_flags.h"
21#include "sanitizer_internal_defs.h"
22#include "sanitizer_libc.h"
23#include "sanitizer_linux.h"
24#include "sanitizer_mutex.h"
25#include "sanitizer_placement_new.h"
26#include "sanitizer_procmaps.h"
27#include "sanitizer_stacktrace.h"
28#include "sanitizer_symbolizer.h"
29
30#if !SANITIZER_FREEBSD0
31#include <asm/param.h>
32#endif
33
34// For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
35// format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
36// access stat from asm/stat.h, without conflicting with definition in
37// sys/stat.h, we use this trick.
38#if defined(__mips64)
39#include <asm/unistd.h>
40#include <sys/types.h>
41#define stat kernel_stat
42#include <asm/stat.h>
43#undef stat
44#endif
45
46#include <dlfcn.h>
47#include <errno(*__errno_location ()).h>
48#include <fcntl.h>
49#include <link.h>
50#include <pthread.h>
51#include <sched.h>
52#include <sys/mman.h>
53#include <sys/ptrace.h>
54#include <sys/resource.h>
55#include <sys/stat.h>
56#include <sys/syscall.h>
57#include <sys/time.h>
58#include <sys/types.h>
59#include <ucontext.h>
60#include <unistd.h>
61
62#if SANITIZER_FREEBSD0
63#include <sys/exec.h>
64#include <sys/sysctl.h>
65#include <vm/vm_param.h>
66#include <vm/pmap.h>
67#include <machine/atomic.h>
68extern "C" {
69// <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
70// FreeBSD 9.2 and 10.0.
71#include <sys/umtx.h>
72}
73extern char **environ; // provided by crt1
74#endif // SANITIZER_FREEBSD
75
76#if !SANITIZER_ANDROID0
77#include <sys/signal.h>
78#endif
79
80#ifndef __GLIBC_PREREQ
81#define __GLIBC_PREREQ(x, y)((2 << 16) + 24 >= ((x) << 16) + (y)) 0
82#endif
83
84#if SANITIZER_LINUX1 && __GLIBC_PREREQ(2, 16)((2 << 16) + 24 >= ((2) << 16) + (16))
85# define SANITIZER_USE_GETAUXVAL1 1
86#else
87# define SANITIZER_USE_GETAUXVAL1 0
88#endif
89
90#if SANITIZER_USE_GETAUXVAL1
91#include <sys/auxv.h>
92#endif
93
94#if SANITIZER_LINUX1
95// <linux/time.h>
96struct kernel_timeval {
97 long tv_sec;
98 long tv_usec;
99};
100
101// <linux/futex.h> is broken on some linux distributions.
102const int FUTEX_WAIT = 0;
103const int FUTEX_WAKE = 1;
104#endif // SANITIZER_LINUX
105
106// Are we using 32-bit or 64-bit Linux syscalls?
107// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
108// but it still needs to use 64-bit syscalls.
109#if SANITIZER_LINUX1 && (defined(__x86_64__1) || defined(__powerpc64__) || \
110 SANITIZER_WORDSIZE64 == 64)
111# define SANITIZER_LINUX_USES_64BIT_SYSCALLS1 1
112#else
113# define SANITIZER_LINUX_USES_64BIT_SYSCALLS1 0
114#endif
115
116#if defined(__x86_64__1) || SANITIZER_MIPS640
117extern "C" {
118extern void internal_sigreturn();
119}
120#endif
121
122namespace __sanitizer {
123
124#if SANITIZER_LINUX1 && defined(__x86_64__1)
125#include "sanitizer_syscall_linux_x86_64.inc"
126#elif SANITIZER_LINUX1 && defined(__aarch64__)
127#include "sanitizer_syscall_linux_aarch64.inc"
128#else
129#include "sanitizer_syscall_generic.inc"
130#endif
131
132// --------------- sanitizer_libc.h
133#if !SANITIZER_S3900
134uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
135 OFF_T offset) {
136#if SANITIZER_FREEBSD0 || SANITIZER_LINUX_USES_64BIT_SYSCALLS1
137 return internal_syscall(SYSCALL(mmap)9, (uptr)addr, length, prot, flags, fd,
138 offset);
139#else
140 // mmap2 specifies file offset in 4096-byte units.
141 CHECK(IsAligned(offset, 4096))do { __sanitizer::u64 v1 = (u64)((IsAligned(offset, 4096))); __sanitizer
::u64 v2 = (u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)
) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 141, "(" "(IsAligned(offset, 4096))" ") " "!=" " (" "0" ")"
, v1, v2); } while (false)
;
142 return internal_syscall(SYSCALL(mmap2)__NR_mmap2, addr, length, prot, flags, fd,
143 offset / 4096);
144#endif
145}
146#endif // !SANITIZER_S390
147
148uptr internal_munmap(void *addr, uptr length) {
149 return internal_syscall(SYSCALL(munmap)11, (uptr)addr, length);
150}
151
152int internal_mprotect(void *addr, uptr length, int prot) {
153 return internal_syscall(SYSCALL(mprotect)10, (uptr)addr, length, prot);
154}
155
156uptr internal_close(fd_t fd) {
157 return internal_syscall(SYSCALL(close)3, fd);
158}
159
160uptr internal_open(const char *filename, int flags) {
161#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
162 return internal_syscall(SYSCALL(openat)257, AT_FDCWD-100, (uptr)filename, flags);
163#else
164 return internal_syscall(SYSCALL(open)2, (uptr)filename, flags);
165#endif
166}
167
168uptr internal_open(const char *filename, int flags, u32 mode) {
169#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
170 return internal_syscall(SYSCALL(openat)257, AT_FDCWD-100, (uptr)filename, flags,
171 mode);
172#else
173 return internal_syscall(SYSCALL(open)2, (uptr)filename, flags, mode);
174#endif
175}
176
177uptr internal_read(fd_t fd, void *buf, uptr count) {
178 sptr res;
179 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf,{ int rverrno; do { res = ((sptr)internal_syscall(0, fd, (uptr
)buf, count)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
180 count)){ int rverrno; do { res = ((sptr)internal_syscall(0, fd, (uptr
)buf, count)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
;
181 return res;
182}
183
184uptr internal_write(fd_t fd, const void *buf, uptr count) {
185 sptr res;
186 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf,{ int rverrno; do { res = ((sptr)internal_syscall(1, fd, (uptr
)buf, count)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
187 count)){ int rverrno; do { res = ((sptr)internal_syscall(1, fd, (uptr
)buf, count)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
;
188 return res;
189}
190
191uptr internal_ftruncate(fd_t fd, uptr size) {
192 sptr res;
193 HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd,{ int rverrno; do { res = ((sptr)internal_syscall(77, fd, (OFF_T
)size)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
194 (OFF_T)size)){ int rverrno; do { res = ((sptr)internal_syscall(77, fd, (OFF_T
)size)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
;
195 return res;
196}
197
198#if !SANITIZER_LINUX_USES_64BIT_SYSCALLS1 && !SANITIZER_FREEBSD0
199static void stat64_to_stat(struct stat64 *in, struct stat *out) {
200 internal_memset(out, 0, sizeof(*out));
201 out->st_dev = in->st_dev;
202 out->st_ino = in->st_ino;
203 out->st_mode = in->st_mode;
204 out->st_nlink = in->st_nlink;
205 out->st_uid = in->st_uid;
206 out->st_gid = in->st_gid;
207 out->st_rdev = in->st_rdev;
208 out->st_size = in->st_size;
209 out->st_blksize = in->st_blksize;
210 out->st_blocks = in->st_blocks;
211 out->st_atimest_atim.tv_sec = in->st_atimest_atim.tv_sec;
212 out->st_mtimest_mtim.tv_sec = in->st_mtimest_mtim.tv_sec;
213 out->st_ctimest_ctim.tv_sec = in->st_ctimest_ctim.tv_sec;
214 out->st_ino = in->st_ino;
215}
216#endif
217
218#if defined(__mips64)
219static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) {
220 internal_memset(out, 0, sizeof(*out));
221 out->st_dev = in->st_dev;
222 out->st_ino = in->st_ino;
223 out->st_mode = in->st_mode;
224 out->st_nlink = in->st_nlink;
225 out->st_uid = in->st_uid;
226 out->st_gid = in->st_gid;
227 out->st_rdev = in->st_rdev;
228 out->st_size = in->st_size;
229 out->st_blksize = in->st_blksize;
230 out->st_blocks = in->st_blocks;
231 out->st_atimest_atim.tv_sec = in->st_atime_nsec;
232 out->st_mtimest_mtim.tv_sec = in->st_mtime_nsec;
233 out->st_ctimest_ctim.tv_sec = in->st_ctime_nsec;
234 out->st_ino = in->st_ino;
235}
236#endif
237
238uptr internal_stat(const char *path, void *buf) {
239#if SANITIZER_FREEBSD0
240 return internal_syscall(SYSCALL(stat)4, path, buf);
241#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
242 return internal_syscall(SYSCALL(newfstatat)262, AT_FDCWD-100, (uptr)path,
243 (uptr)buf, 0);
244#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS1
245# if defined(__mips64)
246 // For mips64, stat syscall fills buffer in the format of kernel_stat
247 struct kernel_stat kbuf;
248 int res = internal_syscall(SYSCALL(stat)4, path, &kbuf);
249 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
250 return res;
251# else
252 return internal_syscall(SYSCALL(stat)4, (uptr)path, (uptr)buf);
253# endif
254#else
255 struct stat64 buf64;
256 int res = internal_syscall(SYSCALL(stat64)__NR_stat64, path, &buf64);
257 stat64_to_stat(&buf64, (struct stat *)buf);
258 return res;
259#endif
260}
261
262uptr internal_lstat(const char *path, void *buf) {
263#if SANITIZER_FREEBSD0
264 return internal_syscall(SYSCALL(lstat)6, path, buf);
265#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
266 return internal_syscall(SYSCALL(newfstatat)262, AT_FDCWD-100, (uptr)path,
267 (uptr)buf, AT_SYMLINK_NOFOLLOW0x100);
268#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS1
269# if SANITIZER_MIPS640
270 // For mips64, lstat syscall fills buffer in the format of kernel_stat
271 struct kernel_stat kbuf;
272 int res = internal_syscall(SYSCALL(lstat)6, path, &kbuf);
273 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
274 return res;
275# else
276 return internal_syscall(SYSCALL(lstat)6, (uptr)path, (uptr)buf);
277# endif
278#else
279 struct stat64 buf64;
280 int res = internal_syscall(SYSCALL(lstat64)__NR_lstat64, path, &buf64);
281 stat64_to_stat(&buf64, (struct stat *)buf);
282 return res;
283#endif
284}
285
286uptr internal_fstat(fd_t fd, void *buf) {
287#if SANITIZER_FREEBSD0 || SANITIZER_LINUX_USES_64BIT_SYSCALLS1
288# if SANITIZER_MIPS640
289 // For mips64, fstat syscall fills buffer in the format of kernel_stat
290 struct kernel_stat kbuf;
291 int res = internal_syscall(SYSCALL(fstat)5, fd, &kbuf);
292 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
293 return res;
294# else
295 return internal_syscall(SYSCALL(fstat)5, fd, (uptr)buf);
2
Calling 'internal_syscall'
3
Returning from 'internal_syscall'
296# endif
297#else
298 struct stat64 buf64;
299 int res = internal_syscall(SYSCALL(fstat64)__NR_fstat64, fd, &buf64);
300 stat64_to_stat(&buf64, (struct stat *)buf);
301 return res;
302#endif
303}
304
305uptr internal_filesize(fd_t fd) {
306 struct stat st;
307 if (internal_fstat(fd, &st))
1
Calling 'internal_fstat'
4
Returning from 'internal_fstat'
5
Taking false branch
308 return -1;
309 return (uptr)st.st_size;
6
Undefined or garbage value returned to caller
310}
311
312uptr internal_dup2(int oldfd, int newfd) {
313#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
314 return internal_syscall(SYSCALL(dup3)292, oldfd, newfd, 0);
315#else
316 return internal_syscall(SYSCALL(dup2)33, oldfd, newfd);
317#endif
318}
319
320uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
321#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
322 return internal_syscall(SYSCALL(readlinkat)267, AT_FDCWD-100,
323 (uptr)path, (uptr)buf, bufsize);
324#else
325 return internal_syscall(SYSCALL(readlink)89, (uptr)path, (uptr)buf, bufsize);
326#endif
327}
328
329uptr internal_unlink(const char *path) {
330#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
331 return internal_syscall(SYSCALL(unlinkat)263, AT_FDCWD-100, (uptr)path, 0);
332#else
333 return internal_syscall(SYSCALL(unlink)87, (uptr)path);
334#endif
335}
336
337uptr internal_rename(const char *oldpath, const char *newpath) {
338#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
339 return internal_syscall(SYSCALL(renameat)264, AT_FDCWD-100, (uptr)oldpath, AT_FDCWD-100,
340 (uptr)newpath);
341#else
342 return internal_syscall(SYSCALL(rename)82, (uptr)oldpath, (uptr)newpath);
343#endif
344}
345
346uptr internal_sched_yield() {
347 return internal_syscall(SYSCALL(sched_yield)24);
348}
349
350void internal__exit(int exitcode) {
351#if SANITIZER_FREEBSD0
352 internal_syscall(SYSCALL(exit)60, exitcode);
353#else
354 internal_syscall(SYSCALL(exit_group)231, exitcode);
355#endif
356 Die(); // Unreachable.
357}
358
359unsigned int internal_sleep(unsigned int seconds) {
360 struct timespec ts;
361 ts.tv_sec = 1;
362 ts.tv_nsec = 0;
363 int res = internal_syscall(SYSCALL(nanosleep)35, &ts, &ts);
364 if (res) return ts.tv_sec;
365 return 0;
366}
367
368uptr internal_execve(const char *filename, char *const argv[],
369 char *const envp[]) {
370 return internal_syscall(SYSCALL(execve)59, (uptr)filename, (uptr)argv,
371 (uptr)envp);
372}
373
374// ----------------- sanitizer_common.h
375bool FileExists(const char *filename) {
376 struct stat st;
377#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
378 if (internal_syscall(SYSCALL(newfstatat)262, AT_FDCWD-100, filename, &st, 0))
379#else
380 if (internal_stat(filename, &st))
381#endif
382 return false;
383 // Sanity check: filename is a regular file.
384 return S_ISREG(st.st_mode)((((st.st_mode)) & 0170000) == (0100000));
385}
386
387tid_t GetTid() {
388#if SANITIZER_FREEBSD0
389 return (uptr)pthread_self();
390#else
391 return internal_syscall(SYSCALL(gettid)186);
392#endif
393}
394
395u64 NanoTime() {
396#if SANITIZER_FREEBSD0
397 timeval tv;
398#else
399 kernel_timeval tv;
400#endif
401 internal_memset(&tv, 0, sizeof(tv));
402 internal_syscall(SYSCALL(gettimeofday)96, (uptr)&tv, 0);
403 return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
404}
405
406// Like getenv, but reads env directly from /proc (on Linux) or parses the
407// 'environ' array (on FreeBSD) and does not use libc. This function should be
408// called first inside __asan_init.
409const char *GetEnv(const char *name) {
410#if SANITIZER_FREEBSD0
411 if (::environ != 0) {
412 uptr NameLen = internal_strlen(name);
413 for (char **Env = ::environ; *Env != 0; Env++) {
414 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
415 return (*Env) + NameLen + 1;
416 }
417 }
418 return 0; // Not found.
419#elif SANITIZER_LINUX1
420 static char *environ;
421 static uptr len;
422 static bool inited;
423 if (!inited) {
424 inited = true;
425 uptr environ_size;
426 if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len))
427 environ = nullptr;
428 }
429 if (!environ || len == 0) return nullptr;
430 uptr namelen = internal_strlen(name);
431 const char *p = environ;
432 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
433 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
434 const char* endp =
435 (char*)internal_memchr(p, '\0', len - (p - environ));
436 if (!endp) // this entry isn't NUL terminated
437 return nullptr;
438 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
439 return p + namelen + 1; // point after =
440 p = endp + 1;
441 }
442 return nullptr; // Not found.
443#else
444#error "Unsupported platform"
445#endif
446}
447
448#if !SANITIZER_FREEBSD0
449extern "C" {
450 SANITIZER_WEAK_ATTRIBUTE__attribute__((weak)) extern void *__libc_stack_end;
451}
452#endif
453
454#if !SANITIZER_GO0 && !SANITIZER_FREEBSD0
455static void ReadNullSepFileToArray(const char *path, char ***arr,
456 int arr_size) {
457 char *buff;
458 uptr buff_size;
459 uptr buff_len;
460 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
461 if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) {
462 (*arr)[0] = nullptr;
463 return;
464 }
465 (*arr)[0] = buff;
466 int count, i;
467 for (count = 1, i = 1; ; i++) {
468 if (buff[i] == 0) {
469 if (buff[i+1] == 0) break;
470 (*arr)[count] = &buff[i+1];
471 CHECK_LE(count, arr_size - 1)do { __sanitizer::u64 v1 = (u64)((count)); __sanitizer::u64 v2
= (u64)((arr_size - 1)); if (__builtin_expect(!!(!(v1 <= v2
)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 471, "(" "(count)" ") " "<=" " (" "(arr_size - 1)" ")", v1
, v2); } while (false)
; // FIXME: make this more flexible.
472 count++;
473 }
474 }
475 (*arr)[count] = nullptr;
476}
477#endif
478
479static void GetArgsAndEnv(char ***argv, char ***envp) {
480#if !SANITIZER_FREEBSD0
481#if !SANITIZER_GO0
482 if (&__libc_stack_end) {
483#endif
484 uptr* stack_end = (uptr*)__libc_stack_end;
485 int argc = *stack_end;
486 *argv = (char**)(stack_end + 1);
487 *envp = (char**)(stack_end + argc + 2);
488#if !SANITIZER_GO0
489 } else {
490 static const int kMaxArgv = 2000, kMaxEnvp = 2000;
491 ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
492 ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
493 }
494#endif
495#else
496 // On FreeBSD, retrieving the argument and environment arrays is done via the
497 // kern.ps_strings sysctl, which returns a pointer to a structure containing
498 // this information. See also <sys/exec.h>.
499 ps_strings *pss;
500 size_t sz = sizeof(pss);
501 if (sysctlbyname("kern.ps_strings", &pss, &sz, NULL__null, 0) == -1) {
502 Printf("sysctl kern.ps_strings failed\n");
503 Die();
504 }
505 *argv = pss->ps_argvstr;
506 *envp = pss->ps_envstr;
507#endif
508}
509
510char **GetArgv() {
511 char **argv, **envp;
512 GetArgsAndEnv(&argv, &envp);
513 return argv;
514}
515
516void ReExec() {
517 char **argv, **envp;
518 GetArgsAndEnv(&argv, &envp);
519 uptr rv = internal_execve("/proc/self/exe", argv, envp);
520 int rverrno;
521 CHECK_EQ(internal_iserror(rv, &rverrno), true)do { __sanitizer::u64 v1 = (u64)((internal_iserror(rv, &rverrno
))); __sanitizer::u64 v2 = (u64)((true)); if (__builtin_expect
(!!(!(v1 == v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 521, "(" "(internal_iserror(rv, &rverrno))" ") " "==" " ("
"(true)" ")", v1, v2); } while (false)
;
522 Printf("execve failed, errno %d\n", rverrno);
523 Die();
524}
525
526enum MutexState {
527 MtxUnlocked = 0,
528 MtxLocked = 1,
529 MtxSleeping = 2
530};
531
532BlockingMutex::BlockingMutex() {
533 internal_memset(this, 0, sizeof(*this));
534}
535
536void BlockingMutex::Lock() {
537 CHECK_EQ(owner_, 0)do { __sanitizer::u64 v1 = (u64)((owner_)); __sanitizer::u64 v2
= (u64)((0)); if (__builtin_expect(!!(!(v1 == v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 537, "(" "(owner_)" ") " "==" " (" "(0)" ")", v1, v2); } while
(false)
;
538 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
539 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
540 return;
541 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
542#if SANITIZER_FREEBSD0
543 _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
544#else
545 internal_syscall(SYSCALL(futex)202, (uptr)m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
546#endif
547 }
548}
549
550void BlockingMutex::Unlock() {
551 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
552 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
553 CHECK_NE(v, MtxUnlocked)do { __sanitizer::u64 v1 = (u64)((v)); __sanitizer::u64 v2 = (
u64)((MtxUnlocked)); if (__builtin_expect(!!(!(v1 != v2)), 0)
) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 553, "(" "(v)" ") " "!=" " (" "(MtxUnlocked)" ")", v1, v2);
} while (false)
;
554 if (v == MtxSleeping) {
555#if SANITIZER_FREEBSD0
556 _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
557#else
558 internal_syscall(SYSCALL(futex)202, (uptr)m, FUTEX_WAKE, 1, 0, 0, 0);
559#endif
560 }
561}
562
563void BlockingMutex::CheckLocked() {
564 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
565 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed))do { __sanitizer::u64 v1 = (u64)((MtxUnlocked)); __sanitizer::
u64 v2 = (u64)((atomic_load(m, memory_order_relaxed))); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 565, "(" "(MtxUnlocked)" ") " "!=" " (" "(atomic_load(m, memory_order_relaxed))"
")", v1, v2); } while (false)
;
566}
567
568// ----------------- sanitizer_linux.h
569// The actual size of this structure is specified by d_reclen.
570// Note that getdents64 uses a different structure format. We only provide the
571// 32-bit syscall here.
572struct linux_dirent {
573#if SANITIZER_X320 || defined(__aarch64__)
574 u64 d_ino;
575 u64 d_off;
576#else
577 unsigned long d_ino;
578 unsigned long d_off;
579#endif
580 unsigned short d_reclen;
581#ifdef __aarch64__
582 unsigned char d_type;
583#endif
584 char d_name[256];
585};
586
587// Syscall wrappers.
588uptr internal_ptrace(int request, int pid, void *addr, void *data) {
589 return internal_syscall(SYSCALL(ptrace)101, request, pid, (uptr)addr,
590 (uptr)data);
591}
592
593uptr internal_waitpid(int pid, int *status, int options) {
594 return internal_syscall(SYSCALL(wait4)61, pid, (uptr)status, options,
595 0 /* rusage */);
596}
597
598uptr internal_getpid() {
599 return internal_syscall(SYSCALL(getpid)39);
600}
601
602uptr internal_getppid() {
603 return internal_syscall(SYSCALL(getppid)110);
604}
605
606uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
607#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
608 return internal_syscall(SYSCALL(getdents64)217, fd, (uptr)dirp, count);
609#else
610 return internal_syscall(SYSCALL(getdents)78, fd, (uptr)dirp, count);
611#endif
612}
613
614uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
615 return internal_syscall(SYSCALL(lseek)8, fd, offset, whence);
616}
617
618#if SANITIZER_LINUX1
619uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
620 return internal_syscall(SYSCALL(prctl)157, option, arg2, arg3, arg4, arg5);
621}
622#endif
623
624uptr internal_sigaltstack(const struct sigaltstack *ss,
625 struct sigaltstack *oss) {
626 return internal_syscall(SYSCALL(sigaltstack)131, (uptr)ss, (uptr)oss);
627}
628
629int internal_fork() {
630#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
631 return internal_syscall(SYSCALL(clone)56, SIGCHLD17, 0);
632#else
633 return internal_syscall(SYSCALL(fork)57);
634#endif
635}
636
637#if SANITIZER_LINUX1
638#define SA_RESTORER0x04000000 0x04000000
639// Doesn't set sa_restorer if the caller did not set it, so use with caution
640//(see below).
641int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
642 __sanitizer_kernel_sigaction_t k_act, k_oldact;
643 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
644 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
645 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
646 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
647 if (u_act) {
648 k_act.handler = u_act->handler;
649 k_act.sigaction = u_act->sigaction;
650 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
651 sizeof(__sanitizer_kernel_sigset_t));
652 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
653 k_act.sa_flags = u_act->sa_flags | SA_RESTORER0x04000000;
654 // FIXME: most often sa_restorer is unset, however the kernel requires it
655 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
656 // If sa_restorer passed to the kernel is NULL, the program may crash upon
657 // signal delivery or fail to unwind the stack in the signal handler.
658 // libc implementation of sigaction() passes its own restorer to
659 // rt_sigaction, so we need to do the same (we'll need to reimplement the
660 // restorers; for x86_64 the restorer address can be obtained from
661 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
662#if !SANITIZER_ANDROID0 || !SANITIZER_MIPS320
663 k_act.sa_restorer = u_act->sa_restorer;
664#endif
665 }
666
667 uptr result = internal_syscall(SYSCALL(rt_sigaction)13, (uptr)signum,
668 (uptr)(u_act ? &k_act : nullptr),
669 (uptr)(u_oldact ? &k_oldact : nullptr),
670 (uptr)sizeof(__sanitizer_kernel_sigset_t));
671
672 if ((result == 0) && u_oldact) {
673 u_oldact->handler = k_oldact.handler;
674 u_oldact->sigaction = k_oldact.sigaction;
675 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
676 sizeof(__sanitizer_kernel_sigset_t));
677 u_oldact->sa_flags = k_oldact.sa_flags;
678#if !SANITIZER_ANDROID0 || !SANITIZER_MIPS320
679 u_oldact->sa_restorer = k_oldact.sa_restorer;
680#endif
681 }
682 return result;
683}
684
685// Invokes sigaction via a raw syscall with a restorer, but does not support
686// all platforms yet.
687// We disable for Go simply because we have not yet added to buildgo.sh.
688#if (defined(__x86_64__1) || SANITIZER_MIPS640) && !SANITIZER_GO0
689int internal_sigaction_syscall(int signum, const void *act, void *oldact) {
690 if (act == nullptr)
691 return internal_sigaction_norestorer(signum, act, oldact);
692 __sanitizer_sigaction u_adjust;
693 internal_memcpy(&u_adjust, act, sizeof(u_adjust));
694#if !SANITIZER_ANDROID0 || !SANITIZER_MIPS320
695 if (u_adjust.sa_restorer == nullptr) {
696 u_adjust.sa_restorer = internal_sigreturn;
697 }
698#endif
699 return internal_sigaction_norestorer(signum, (const void *)&u_adjust,
700 oldact);
701}
702#endif // defined(__x86_64__) && !SANITIZER_GO
703#endif // SANITIZER_LINUX
704
705uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
706 __sanitizer_sigset_t *oldset) {
707#if SANITIZER_FREEBSD0
708 return internal_syscall(SYSCALL(sigprocmask)__NR_sigprocmask, how, set, oldset);
709#else
710 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
711 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
712 return internal_syscall(SYSCALL(rt_sigprocmask)14, (uptr)how,
713 (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
714 sizeof(__sanitizer_kernel_sigset_t));
715#endif
716}
717
718void internal_sigfillset(__sanitizer_sigset_t *set) {
719 internal_memset(set, 0xff, sizeof(*set));
720}
721
722void internal_sigemptyset(__sanitizer_sigset_t *set) {
723 internal_memset(set, 0, sizeof(*set));
724}
725
726#if SANITIZER_LINUX1
727void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
728 signum -= 1;
729 CHECK_GE(signum, 0)do { __sanitizer::u64 v1 = (u64)((signum)); __sanitizer::u64 v2
= (u64)((0)); if (__builtin_expect(!!(!(v1 >= v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 729, "(" "(signum)" ") " ">=" " (" "(0)" ")", v1, v2); }
while (false)
;
730 CHECK_LT(signum, sizeof(*set) * 8)do { __sanitizer::u64 v1 = (u64)((signum)); __sanitizer::u64 v2
= (u64)((sizeof(*set) * 8)); if (__builtin_expect(!!(!(v1 <
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 730, "(" "(signum)" ") " "<" " (" "(sizeof(*set) * 8)" ")"
, v1, v2); } while (false)
;
731 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
732 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
733 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
734 k_set->sig[idx] &= ~(1 << bit);
735}
736
737bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
738 signum -= 1;
739 CHECK_GE(signum, 0)do { __sanitizer::u64 v1 = (u64)((signum)); __sanitizer::u64 v2
= (u64)((0)); if (__builtin_expect(!!(!(v1 >= v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 739, "(" "(signum)" ") " ">=" " (" "(0)" ")", v1, v2); }
while (false)
;
740 CHECK_LT(signum, sizeof(*set) * 8)do { __sanitizer::u64 v1 = (u64)((signum)); __sanitizer::u64 v2
= (u64)((sizeof(*set) * 8)); if (__builtin_expect(!!(!(v1 <
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 740, "(" "(signum)" ") " "<" " (" "(sizeof(*set) * 8)" ")"
, v1, v2); } while (false)
;
741 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
742 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
743 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
744 return k_set->sig[idx] & (1 << bit);
745}
746#endif // SANITIZER_LINUX
747
748// ThreadLister implementation.
749ThreadLister::ThreadLister(int pid)
750 : pid_(pid),
751 descriptor_(-1),
752 buffer_(4096),
753 error_(true),
754 entry_((struct linux_dirent *)buffer_.data()),
755 bytes_read_(0) {
756 char task_directory_path[80];
757 internal_snprintf(task_directory_path, sizeof(task_directory_path),
758 "/proc/%d/task/", pid);
759 uptr openrv = internal_open(task_directory_path, O_RDONLY00 | O_DIRECTORY0200000);
760 if (internal_iserror(openrv)) {
761 error_ = true;
762 Report("Can't open /proc/%d/task for reading.\n", pid);
763 } else {
764 error_ = false;
765 descriptor_ = openrv;
766 }
767}
768
769int ThreadLister::GetNextTID() {
770 int tid = -1;
771 do {
772 if (error_)
773 return -1;
774 if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
775 return -1;
776 if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
777 entry_->d_name[0] <= '9') {
778 // Found a valid tid.
779 tid = (int)internal_atoll(entry_->d_name);
780 }
781 entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
782 } while (tid < 0);
783 return tid;
784}
785
786void ThreadLister::Reset() {
787 if (error_ || descriptor_ < 0)
788 return;
789 internal_lseek(descriptor_, 0, SEEK_SET0);
790}
791
792ThreadLister::~ThreadLister() {
793 if (descriptor_ >= 0)
794 internal_close(descriptor_);
795}
796
797bool ThreadLister::error() { return error_; }
798
799bool ThreadLister::GetDirectoryEntries() {
800 CHECK_GE(descriptor_, 0)do { __sanitizer::u64 v1 = (u64)((descriptor_)); __sanitizer::
u64 v2 = (u64)((0)); if (__builtin_expect(!!(!(v1 >= v2)),
0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 800, "(" "(descriptor_)" ") " ">=" " (" "(0)" ")", v1, v2
); } while (false)
;
801 CHECK_NE(error_, true)do { __sanitizer::u64 v1 = (u64)((error_)); __sanitizer::u64 v2
= (u64)((true)); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 801, "(" "(error_)" ") " "!=" " (" "(true)" ")", v1, v2); }
while (false)
;
802 bytes_read_ = internal_getdents(descriptor_,
803 (struct linux_dirent *)buffer_.data(),
804 buffer_.size());
805 if (internal_iserror(bytes_read_)) {
806 Report("Can't read directory entries from /proc/%d/task.\n", pid_);
807 error_ = true;
808 return false;
809 } else if (bytes_read_ == 0) {
810 return false;
811 }
812 entry_ = (struct linux_dirent *)buffer_.data();
813 return true;
814}
815
816uptr GetPageSize() {
817// Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
818#if SANITIZER_ANDROID0
819 return 4096;
820#elif SANITIZER_LINUX1 && (defined(__x86_64__1) || defined(__i386__))
821 return EXEC_PAGESIZE4096;
822#elif SANITIZER_USE_GETAUXVAL1
823 return getauxval(AT_PAGESZ6);
824#else
825 return sysconf(_SC_PAGESIZE_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
826#endif
827}
828
829uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
830#if SANITIZER_FREEBSD0
831 const int Mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
832 const char *default_module_name = "kern.proc.pathname";
833 size_t Size = buf_len;
834 bool IsErr = (sysctl(Mib, ARRAY_SIZE(Mib)(sizeof(Mib)/sizeof((Mib)[0])), buf, &Size, NULL__null, 0) != 0);
835 int readlink_error = IsErr ? errno(*__errno_location ()) : 0;
836 uptr module_name_len = Size;
837#else
838 const char *default_module_name = "/proc/self/exe";
839 uptr module_name_len = internal_readlink(
840 default_module_name, buf, buf_len);
841 int readlink_error;
842 bool IsErr = internal_iserror(module_name_len, &readlink_error);
843#endif
844 if (IsErr) {
845 // We can't read binary name for some reason, assume it's unknown.
846 Report("WARNING: reading executable name failed with errno %d, "
847 "some stack frames may not be symbolized\n", readlink_error);
848 module_name_len = internal_snprintf(buf, buf_len, "%s",
849 default_module_name);
850 CHECK_LT(module_name_len, buf_len)do { __sanitizer::u64 v1 = (u64)((module_name_len)); __sanitizer
::u64 v2 = (u64)((buf_len)); if (__builtin_expect(!!(!(v1 <
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 850, "(" "(module_name_len)" ") " "<" " (" "(buf_len)" ")"
, v1, v2); } while (false)
;
851 }
852 return module_name_len;
853}
854
855uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
856#if SANITIZER_LINUX1
857 char *tmpbuf;
858 uptr tmpsize;
859 uptr tmplen;
860 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
861 1024 * 1024)) {
862 internal_strncpy(buf, tmpbuf, buf_len);
863 UnmapOrDie(tmpbuf, tmpsize);
864 return internal_strlen(buf);
865 }
866#endif
867 return ReadBinaryName(buf, buf_len);
868}
869
870// Match full names of the form /path/to/base_name{-,.}*
871bool LibraryNameIs(const char *full_name, const char *base_name) {
872 const char *name = full_name;
873 // Strip path.
874 while (*name != '\0') name++;
875 while (name > full_name && *name != '/') name--;
876 if (*name == '/') name++;
877 uptr base_name_length = internal_strlen(base_name);
878 if (internal_strncmp(name, base_name, base_name_length)) return false;
879 return (name[base_name_length] == '-' || name[base_name_length] == '.');
880}
881
882#if !SANITIZER_ANDROID0
883// Call cb for each region mapped by map.
884void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
885 CHECK_NE(map, nullptr)do { __sanitizer::u64 v1 = (u64)((map)); __sanitizer::u64 v2 =
(u64)((nullptr)); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 885, "(" "(map)" ") " "!=" " (" "(nullptr)" ")", v1, v2); }
while (false)
;
886#if !SANITIZER_FREEBSD0
887 typedef ElfW(Phdr)Elf64_Phdr Elf_Phdr;
888 typedef ElfW(Ehdr)Elf64_Ehdr Elf_Ehdr;
889#endif // !SANITIZER_FREEBSD
890 char *base = (char *)map->l_addr;
891 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
892 char *phdrs = base + ehdr->e_phoff;
893 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
894
895 // Find the segment with the minimum base so we can "relocate" the p_vaddr
896 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
897 // objects have a non-zero base.
898 uptr preferred_base = (uptr)-1;
899 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
900 Elf_Phdr *phdr = (Elf_Phdr *)iter;
901 if (phdr->p_type == PT_LOAD1 && preferred_base > (uptr)phdr->p_vaddr)
902 preferred_base = (uptr)phdr->p_vaddr;
903 }
904
905 // Compute the delta from the real base to get a relocation delta.
906 sptr delta = (uptr)base - preferred_base;
907 // Now we can figure out what the loader really mapped.
908 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
909 Elf_Phdr *phdr = (Elf_Phdr *)iter;
910 if (phdr->p_type == PT_LOAD1) {
911 uptr seg_start = phdr->p_vaddr + delta;
912 uptr seg_end = seg_start + phdr->p_memsz;
913 // None of these values are aligned. We consider the ragged edges of the
914 // load command as defined, since they are mapped from the file.
915 seg_start = RoundDownTo(seg_start, GetPageSizeCached());
916 seg_end = RoundUpTo(seg_end, GetPageSizeCached());
917 cb((void *)seg_start, seg_end - seg_start);
918 }
919 }
920}
921#endif
922
923#if defined(__x86_64__1) && SANITIZER_LINUX1
924// We cannot use glibc's clone wrapper, because it messes with the child
925// task's TLS. It writes the PID and TID of the child task to its thread
926// descriptor, but in our case the child task shares the thread descriptor with
927// the parent (because we don't know how to allocate a new thread
928// descriptor to keep glibc happy). So the stock version of clone(), when
929// used with CLONE_VM, would end up corrupting the parent's thread descriptor.
930uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
931 int *parent_tidptr, void *newtls, int *child_tidptr) {
932 long long res;
933 if (!fn || !child_stack)
934 return -EINVAL22;
935 CHECK_EQ(0, (uptr)child_stack % 16)do { __sanitizer::u64 v1 = (u64)((0)); __sanitizer::u64 v2 = (
u64)(((uptr)child_stack % 16)); if (__builtin_expect(!!(!(v1 ==
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 935, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
936 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
937 ((unsigned long long *)child_stack)[0] = (uptr)fn;
938 ((unsigned long long *)child_stack)[1] = (uptr)arg;
939 register void *r8 __asm__("r8") = newtls;
940 register int *r10 __asm__("r10") = child_tidptr;
941 __asm__ __volatile__(
942 /* %rax = syscall(%rax = SYSCALL(clone),
943 * %rdi = flags,
944 * %rsi = child_stack,
945 * %rdx = parent_tidptr,
946 * %r8 = new_tls,
947 * %r10 = child_tidptr)
948 */
949 "syscall\n"
950
951 /* if (%rax != 0)
952 * return;
953 */
954 "testq %%rax,%%rax\n"
955 "jnz 1f\n"
956
957 /* In the child. Terminate unwind chain. */
958 // XXX: We should also terminate the CFI unwind chain
959 // here. Unfortunately clang 3.2 doesn't support the
960 // necessary CFI directives, so we skip that part.
961 "xorq %%rbp,%%rbp\n"
962
963 /* Call "fn(arg)". */
964 "popq %%rax\n"
965 "popq %%rdi\n"
966 "call *%%rax\n"
967
968 /* Call _exit(%rax). */
969 "movq %%rax,%%rdi\n"
970 "movq %2,%%rax\n"
971 "syscall\n"
972
973 /* Return to parent. */
974 "1:\n"
975 : "=a" (res)
976 : "a"(SYSCALL(clone)56), "i"(SYSCALL(exit)60),
977 "S"(child_stack),
978 "D"(flags),
979 "d"(parent_tidptr),
980 "r"(r8),
981 "r"(r10)
982 : "rsp", "memory", "r11", "rcx");
983 return res;
984}
985#elif defined(__mips__)
986uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
987 int *parent_tidptr, void *newtls, int *child_tidptr) {
988 long long res;
989 if (!fn || !child_stack)
990 return -EINVAL22;
991 CHECK_EQ(0, (uptr)child_stack % 16)do { __sanitizer::u64 v1 = (u64)((0)); __sanitizer::u64 v2 = (
u64)(((uptr)child_stack % 16)); if (__builtin_expect(!!(!(v1 ==
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 991, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
992 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
993 ((unsigned long long *)child_stack)[0] = (uptr)fn;
994 ((unsigned long long *)child_stack)[1] = (uptr)arg;
995 register void *a3 __asm__("$7") = newtls;
996 register int *a4 __asm__("$8") = child_tidptr;
997 // We don't have proper CFI directives here because it requires alot of code
998 // for very marginal benefits.
999 __asm__ __volatile__(
1000 /* $v0 = syscall($v0 = __NR_clone,
1001 * $a0 = flags,
1002 * $a1 = child_stack,
1003 * $a2 = parent_tidptr,
1004 * $a3 = new_tls,
1005 * $a4 = child_tidptr)
1006 */
1007 ".cprestore 16;\n"
1008 "move $4,%1;\n"
1009 "move $5,%2;\n"
1010 "move $6,%3;\n"
1011 "move $7,%4;\n"
1012 /* Store the fifth argument on stack
1013 * if we are using 32-bit abi.
1014 */
1015#if SANITIZER_WORDSIZE64 == 32
1016 "lw %5,16($29);\n"
1017#else
1018 "move $8,%5;\n"
1019#endif
1020 "li $2,%6;\n"
1021 "syscall;\n"
1022
1023 /* if ($v0 != 0)
1024 * return;
1025 */
1026 "bnez $2,1f;\n"
1027
1028 /* Call "fn(arg)". */
1029#if SANITIZER_WORDSIZE64 == 32
1030#ifdef __BIG_ENDIAN__
1031 "lw $25,4($29);\n"
1032 "lw $4,12($29);\n"
1033#else
1034 "lw $25,0($29);\n"
1035 "lw $4,8($29);\n"
1036#endif
1037#else
1038 "ld $25,0($29);\n"
1039 "ld $4,8($29);\n"
1040#endif
1041 "jal $25;\n"
1042
1043 /* Call _exit($v0). */
1044 "move $4,$2;\n"
1045 "li $2,%7;\n"
1046 "syscall;\n"
1047
1048 /* Return to parent. */
1049 "1:\n"
1050 : "=r" (res)
1051 : "r"(flags),
1052 "r"(child_stack),
1053 "r"(parent_tidptr),
1054 "r"(a3),
1055 "r"(a4),
1056 "i"(__NR_clone56),
1057 "i"(__NR_exit60)
1058 : "memory", "$29" );
1059 return res;
1060}
1061#elif defined(__aarch64__)
1062uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1063 int *parent_tidptr, void *newtls, int *child_tidptr) {
1064 long long res;
1065 if (!fn || !child_stack)
1066 return -EINVAL22;
1067 CHECK_EQ(0, (uptr)child_stack % 16)do { __sanitizer::u64 v1 = (u64)((0)); __sanitizer::u64 v2 = (
u64)(((uptr)child_stack % 16)); if (__builtin_expect(!!(!(v1 ==
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1067, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
1068 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1069 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1070 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1071
1072 register int (*__fn)(void *) __asm__("x0") = fn;
1073 register void *__stack __asm__("x1") = child_stack;
1074 register int __flags __asm__("x2") = flags;
1075 register void *__arg __asm__("x3") = arg;
1076 register int *__ptid __asm__("x4") = parent_tidptr;
1077 register void *__tls __asm__("x5") = newtls;
1078 register int *__ctid __asm__("x6") = child_tidptr;
1079
1080 __asm__ __volatile__(
1081 "mov x0,x2\n" /* flags */
1082 "mov x2,x4\n" /* ptid */
1083 "mov x3,x5\n" /* tls */
1084 "mov x4,x6\n" /* ctid */
1085 "mov x8,%9\n" /* clone */
1086
1087 "svc 0x0\n"
1088
1089 /* if (%r0 != 0)
1090 * return %r0;
1091 */
1092 "cmp x0, #0\n"
1093 "bne 1f\n"
1094
1095 /* In the child, now. Call "fn(arg)". */
1096 "ldp x1, x0, [sp], #16\n"
1097 "blr x1\n"
1098
1099 /* Call _exit(%r0). */
1100 "mov x8, %10\n"
1101 "svc 0x0\n"
1102 "1:\n"
1103
1104 : "=r" (res)
1105 : "i"(-EINVAL22),
1106 "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1107 "r"(__ptid), "r"(__tls), "r"(__ctid),
1108 "i"(__NR_clone56), "i"(__NR_exit60)
1109 : "x30", "memory");
1110 return res;
1111}
1112#elif defined(__powerpc64__)
1113uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1114 int *parent_tidptr, void *newtls, int *child_tidptr) {
1115 long long res;
1116// Stack frame structure.
1117#if SANITIZER_PPC64V10
1118// Back chain == 0 (SP + 112)
1119// Frame (112 bytes):
1120// Parameter save area (SP + 48), 8 doublewords
1121// TOC save area (SP + 40)
1122// Link editor doubleword (SP + 32)
1123// Compiler doubleword (SP + 24)
1124// LR save area (SP + 16)
1125// CR save area (SP + 8)
1126// Back chain (SP + 0)
1127# define FRAME_SIZE 112
1128# define FRAME_TOC_SAVE_OFFSET 40
1129#elif SANITIZER_PPC64V20
1130// Back chain == 0 (SP + 32)
1131// Frame (32 bytes):
1132// TOC save area (SP + 24)
1133// LR save area (SP + 16)
1134// CR save area (SP + 8)
1135// Back chain (SP + 0)
1136# define FRAME_SIZE 32
1137# define FRAME_TOC_SAVE_OFFSET 24
1138#else
1139# error "Unsupported PPC64 ABI"
1140#endif
1141 if (!fn || !child_stack)
1142 return -EINVAL22;
1143 CHECK_EQ(0, (uptr)child_stack % 16)do { __sanitizer::u64 v1 = (u64)((0)); __sanitizer::u64 v2 = (
u64)(((uptr)child_stack % 16)); if (__builtin_expect(!!(!(v1 ==
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1143, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
1144
1145 register int (*__fn)(void *) __asm__("r3") = fn;
1146 register void *__cstack __asm__("r4") = child_stack;
1147 register int __flags __asm__("r5") = flags;
1148 register void *__arg __asm__("r6") = arg;
1149 register int *__ptidptr __asm__("r7") = parent_tidptr;
1150 register void *__newtls __asm__("r8") = newtls;
1151 register int *__ctidptr __asm__("r9") = child_tidptr;
1152
1153 __asm__ __volatile__(
1154 /* fn and arg are saved across the syscall */
1155 "mr 28, %5\n\t"
1156 "mr 27, %8\n\t"
1157
1158 /* syscall
1159 r0 == __NR_clone
1160 r3 == flags
1161 r4 == child_stack
1162 r5 == parent_tidptr
1163 r6 == newtls
1164 r7 == child_tidptr */
1165 "mr 3, %7\n\t"
1166 "mr 5, %9\n\t"
1167 "mr 6, %10\n\t"
1168 "mr 7, %11\n\t"
1169 "li 0, %3\n\t"
1170 "sc\n\t"
1171
1172 /* Test if syscall was successful */
1173 "cmpdi cr1, 3, 0\n\t"
1174 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1175 "bne- cr1, 1f\n\t"
1176
1177 /* Set up stack frame */
1178 "li 29, 0\n\t"
1179 "stdu 29, -8(1)\n\t"
1180 "stdu 1, -%12(1)\n\t"
1181 /* Do the function call */
1182 "std 2, %13(1)\n\t"
1183#if SANITIZER_PPC64V10
1184 "ld 0, 0(28)\n\t"
1185 "ld 2, 8(28)\n\t"
1186 "mtctr 0\n\t"
1187#elif SANITIZER_PPC64V20
1188 "mr 12, 28\n\t"
1189 "mtctr 12\n\t"
1190#else
1191# error "Unsupported PPC64 ABI"
1192#endif
1193 "mr 3, 27\n\t"
1194 "bctrl\n\t"
1195 "ld 2, %13(1)\n\t"
1196
1197 /* Call _exit(r3) */
1198 "li 0, %4\n\t"
1199 "sc\n\t"
1200
1201 /* Return to parent */
1202 "1:\n\t"
1203 "mr %0, 3\n\t"
1204 : "=r" (res)
1205 : "0" (-1),
1206 "i" (EINVAL22),
1207 "i" (__NR_clone56),
1208 "i" (__NR_exit60),
1209 "r" (__fn),
1210 "r" (__cstack),
1211 "r" (__flags),
1212 "r" (__arg),
1213 "r" (__ptidptr),
1214 "r" (__newtls),
1215 "r" (__ctidptr),
1216 "i" (FRAME_SIZE),
1217 "i" (FRAME_TOC_SAVE_OFFSET)
1218 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1219 return res;
1220}
1221#elif defined(__i386__) && SANITIZER_LINUX1
1222uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1223 int *parent_tidptr, void *newtls, int *child_tidptr) {
1224 int res;
1225 if (!fn || !child_stack)
1226 return -EINVAL22;
1227 CHECK_EQ(0, (uptr)child_stack % 16)do { __sanitizer::u64 v1 = (u64)((0)); __sanitizer::u64 v2 = (
u64)(((uptr)child_stack % 16)); if (__builtin_expect(!!(!(v1 ==
v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1227, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
1228 child_stack = (char *)child_stack - 7 * sizeof(unsigned int);
1229 ((unsigned int *)child_stack)[0] = (uptr)flags;
1230 ((unsigned int *)child_stack)[1] = (uptr)0;
1231 ((unsigned int *)child_stack)[2] = (uptr)fn;
1232 ((unsigned int *)child_stack)[3] = (uptr)arg;
1233 __asm__ __volatile__(
1234 /* %eax = syscall(%eax = SYSCALL(clone),
1235 * %ebx = flags,
1236 * %ecx = child_stack,
1237 * %edx = parent_tidptr,
1238 * %esi = new_tls,
1239 * %edi = child_tidptr)
1240 */
1241
1242 /* Obtain flags */
1243 "movl (%%ecx), %%ebx\n"
1244 /* Do the system call */
1245 "pushl %%ebx\n"
1246 "pushl %%esi\n"
1247 "pushl %%edi\n"
1248 /* Remember the flag value. */
1249 "movl %%ebx, (%%ecx)\n"
1250 "int $0x80\n"
1251 "popl %%edi\n"
1252 "popl %%esi\n"
1253 "popl %%ebx\n"
1254
1255 /* if (%eax != 0)
1256 * return;
1257 */
1258
1259 "test %%eax,%%eax\n"
1260 "jnz 1f\n"
1261
1262 /* terminate the stack frame */
1263 "xorl %%ebp,%%ebp\n"
1264 /* Call FN. */
1265 "call *%%ebx\n"
1266#ifdef PIC
1267 "call here\n"
1268 "here:\n"
1269 "popl %%ebx\n"
1270 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1271#endif
1272 /* Call exit */
1273 "movl %%eax, %%ebx\n"
1274 "movl %2, %%eax\n"
1275 "int $0x80\n"
1276 "1:\n"
1277 : "=a" (res)
1278 : "a"(SYSCALL(clone)56), "i"(SYSCALL(exit)60),
1279 "c"(child_stack),
1280 "d"(parent_tidptr),
1281 "S"(newtls),
1282 "D"(child_tidptr)
1283 : "memory");
1284 return res;
1285}
1286#elif defined(__arm__) && SANITIZER_LINUX1
1287uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1288 int *parent_tidptr, void *newtls, int *child_tidptr) {
1289 unsigned int res;
1290 if (!fn || !child_stack)
1291 return -EINVAL22;
1292 child_stack = (char *)child_stack - 2 * sizeof(unsigned int);
1293 ((unsigned int *)child_stack)[0] = (uptr)fn;
1294 ((unsigned int *)child_stack)[1] = (uptr)arg;
1295 register int r0 __asm__("r0") = flags;
1296 register void *r1 __asm__("r1") = child_stack;
1297 register int *r2 __asm__("r2") = parent_tidptr;
1298 register void *r3 __asm__("r3") = newtls;
1299 register int *r4 __asm__("r4") = child_tidptr;
1300 register int r7 __asm__("r7") = __NR_clone56;
1301
1302#if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__)
1303# define ARCH_HAS_BX
1304#endif
1305#if __ARM_ARCH > 4
1306# define ARCH_HAS_BLX
1307#endif
1308
1309#ifdef ARCH_HAS_BX
1310# ifdef ARCH_HAS_BLX
1311# define BLX(R) "blx " #R "\n"
1312# else
1313# define BLX(R) "mov lr, pc; bx " #R "\n"
1314# endif
1315#else
1316# define BLX(R) "mov lr, pc; mov pc," #R "\n"
1317#endif
1318
1319 __asm__ __volatile__(
1320 /* %r0 = syscall(%r7 = SYSCALL(clone),
1321 * %r0 = flags,
1322 * %r1 = child_stack,
1323 * %r2 = parent_tidptr,
1324 * %r3 = new_tls,
1325 * %r4 = child_tidptr)
1326 */
1327
1328 /* Do the system call */
1329 "swi 0x0\n"
1330
1331 /* if (%r0 != 0)
1332 * return %r0;
1333 */
1334 "cmp r0, #0\n"
1335 "bne 1f\n"
1336
1337 /* In the child, now. Call "fn(arg)". */
1338 "ldr r0, [sp, #4]\n"
1339 "ldr ip, [sp], #8\n"
1340 BLX(ip)
1341 /* Call _exit(%r0). */
1342 "mov r7, %7\n"
1343 "swi 0x0\n"
1344 "1:\n"
1345 "mov %0, r0\n"
1346 : "=r"(res)
1347 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7),
1348 "i"(__NR_exit60)
1349 : "memory");
1350 return res;
1351}
1352#endif // defined(__x86_64__) && SANITIZER_LINUX
1353
1354#if SANITIZER_ANDROID0
1355#if __ANDROID_API__ < 21
1356extern "C" __attribute__((weak)) int dl_iterate_phdr(
1357 int (*)(struct dl_phdr_info *, size_t, void *), void *);
1358#endif
1359
1360static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1361 void *data) {
1362 // Any name starting with "lib" indicates a bug in L where library base names
1363 // are returned instead of paths.
1364 if (info->dlpi_name && info->dlpi_name[0] == 'l' &&
1365 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') {
1366 *(bool *)data = true;
1367 return 1;
1368 }
1369 return 0;
1370}
1371
1372static atomic_uint32_t android_api_level;
1373
1374static AndroidApiLevel AndroidDetectApiLevel() {
1375 if (!&dl_iterate_phdr)
1376 return ANDROID_KITKAT; // K or lower
1377 bool base_name_seen = false;
1378 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen);
1379 if (base_name_seen)
1380 return ANDROID_LOLLIPOP_MR1; // L MR1
1381 return ANDROID_POST_LOLLIPOP; // post-L
1382 // Plain L (API level 21) is completely broken wrt ASan and not very
1383 // interesting to detect.
1384}
1385
1386AndroidApiLevel AndroidGetApiLevel() {
1387 AndroidApiLevel level =
1388 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed);
1389 if (level) return level;
1390 level = AndroidDetectApiLevel();
1391 atomic_store(&android_api_level, level, memory_order_relaxed);
1392 return level;
1393}
1394
1395#endif
1396
1397bool IsHandledDeadlySignal(int signum) {
1398 if (common_flags()->handle_abort && signum == SIGABRT6)
1399 return true;
1400 if (common_flags()->handle_sigill && signum == SIGILL4)
1401 return true;
1402 if (common_flags()->handle_sigfpe && signum == SIGFPE8)
1403 return true;
1404 if (common_flags()->handle_segv && signum == SIGSEGV11)
1405 return true;
1406 return common_flags()->handle_sigbus && signum == SIGBUS7;
1407}
1408
1409#if !SANITIZER_GO0
1410void *internal_start_thread(void(*func)(void *arg), void *arg) {
1411 // Start the thread with signals blocked, otherwise it can steal user signals.
1412 __sanitizer_sigset_t set, old;
1413 internal_sigfillset(&set);
1414#if SANITIZER_LINUX1 && !SANITIZER_ANDROID0
1415 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
1416 // on any thread, setuid call hangs (see test/tsan/setuid.c).
1417 internal_sigdelset(&set, 33);
1418#endif
1419 internal_sigprocmask(SIG_SETMASK2, &set, &old);
1420 void *th;
1421 real_pthread_create(&th, nullptr, (void*(*)(void *arg))func, arg);
1422 internal_sigprocmask(SIG_SETMASK2, &old, nullptr);
1423 return th;
1424}
1425
1426void internal_join_thread(void *th) {
1427 real_pthread_join(th, nullptr);
1428}
1429#else
1430void *internal_start_thread(void (*func)(void *), void *arg) { return 0; }
1431
1432void internal_join_thread(void *th) {}
1433#endif
1434
1435#if defined(__aarch64__)
1436// Android headers in the older NDK releases miss this definition.
1437struct __sanitizer_esr_context {
1438 struct _aarch64_ctx head;
1439 uint64_t esr;
1440};
1441
1442static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1443 static const u32 kEsrMagic = 0x45535201;
1444 u8 *aux = ucontext->uc_mcontext.__reserved;
1445 while (true) {
1446 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1447 if (ctx->size == 0) break;
1448 if (ctx->magic == kEsrMagic) {
1449 *esr = ((__sanitizer_esr_context *)ctx)->esr;
1450 return true;
1451 }
1452 aux += ctx->size;
1453 }
1454 return false;
1455}
1456#endif
1457
1458SignalContext::WriteFlag SignalContext::GetWriteFlag(void *context) {
1459 ucontext_t *ucontext = (ucontext_t *)context;
1460#if defined(__x86_64__1) || defined(__i386__)
1461 static const uptr PF_WRITE = 1U << 1;
1462#if SANITIZER_FREEBSD0
1463 uptr err = ucontext->uc_mcontext.mc_err;
1464#else
1465 uptr err = ucontext->uc_mcontext.gregs[REG_ERRREG_ERR];
1466#endif
1467 return err & PF_WRITE ? WRITE : READ;
1468#elif defined(__arm__)
1469 static const uptr FSR_WRITE = 1U << 11;
1470 uptr fsr = ucontext->uc_mcontext.error_code;
1471 return fsr & FSR_WRITE ? WRITE : READ;
1472#elif defined(__aarch64__)
1473 static const u64 ESR_ELx_WNR = 1U << 6;
1474 u64 esr;
1475 if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN;
1476 return esr & ESR_ELx_WNR ? WRITE : READ;
1477#else
1478 (void)ucontext;
1479 return UNKNOWN; // FIXME: Implement.
1480#endif
1481}
1482
1483void SignalContext::DumpAllRegisters(void *context) {
1484 // FIXME: Implement this.
1485}
1486
1487void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
1488#if defined(__arm__)
1489 ucontext_t *ucontext = (ucontext_t*)context;
1490 *pc = ucontext->uc_mcontext.arm_pc;
1491 *bp = ucontext->uc_mcontext.arm_fp;
1492 *sp = ucontext->uc_mcontext.arm_sp;
1493#elif defined(__aarch64__)
1494 ucontext_t *ucontext = (ucontext_t*)context;
1495 *pc = ucontext->uc_mcontext.pc;
1496 *bp = ucontext->uc_mcontext.regs[29];
1497 *sp = ucontext->uc_mcontext.sp;
1498#elif defined(__hppa__)
1499 ucontext_t *ucontext = (ucontext_t*)context;
1500 *pc = ucontext->uc_mcontext.sc_iaoq[0];
1501 /* GCC uses %r3 whenever a frame pointer is needed. */
1502 *bp = ucontext->uc_mcontext.sc_gr[3];
1503 *sp = ucontext->uc_mcontext.sc_gr[30];
1504#elif defined(__x86_64__1)
1505# if SANITIZER_FREEBSD0
1506 ucontext_t *ucontext = (ucontext_t*)context;
1507 *pc = ucontext->uc_mcontext.mc_rip;
1508 *bp = ucontext->uc_mcontext.mc_rbp;
1509 *sp = ucontext->uc_mcontext.mc_rsp;
1510# else
1511 ucontext_t *ucontext = (ucontext_t*)context;
1512 *pc = ucontext->uc_mcontext.gregs[REG_RIPREG_RIP];
1513 *bp = ucontext->uc_mcontext.gregs[REG_RBPREG_RBP];
1514 *sp = ucontext->uc_mcontext.gregs[REG_RSPREG_RSP];
1515# endif
1516#elif defined(__i386__)
1517# if SANITIZER_FREEBSD0
1518 ucontext_t *ucontext = (ucontext_t*)context;
1519 *pc = ucontext->uc_mcontext.mc_eip;
1520 *bp = ucontext->uc_mcontext.mc_ebp;
1521 *sp = ucontext->uc_mcontext.mc_esp;
1522# else
1523 ucontext_t *ucontext = (ucontext_t*)context;
1524 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
1525 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
1526 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
1527# endif
1528#elif defined(__powerpc__) || defined(__powerpc64__)
1529 ucontext_t *ucontext = (ucontext_t*)context;
1530 *pc = ucontext->uc_mcontext.regs->nip;
1531 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
1532 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
1533 // pointer, but GCC always uses r31 when we need a frame pointer.
1534 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
1535#elif defined(__sparc__)
1536 ucontext_t *ucontext = (ucontext_t*)context;
1537 uptr *stk_ptr;
1538# if defined (__arch64__)
1539 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
1540 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
1541 stk_ptr = (uptr *) (*sp + 2047);
1542 *bp = stk_ptr[15];
1543# else
1544 *pc = ucontext->uc_mcontext.gregs[REG_PC];
1545 *sp = ucontext->uc_mcontext.gregs[REG_O6];
1546 stk_ptr = (uptr *) *sp;
1547 *bp = stk_ptr[15];
1548# endif
1549#elif defined(__mips__)
1550 ucontext_t *ucontext = (ucontext_t*)context;
1551 *pc = ucontext->uc_mcontext.pc;
1552 *bp = ucontext->uc_mcontext.gregs[30];
1553 *sp = ucontext->uc_mcontext.gregs[29];
1554#elif defined(__s390__)
1555 ucontext_t *ucontext = (ucontext_t*)context;
1556# if defined(__s390x__)
1557 *pc = ucontext->uc_mcontext.psw.addr;
1558# else
1559 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
1560# endif
1561 *bp = ucontext->uc_mcontext.gregs[11];
1562 *sp = ucontext->uc_mcontext.gregs[15];
1563#else
1564# error "Unsupported arch"
1565#endif
1566}
1567
1568void MaybeReexec() {
1569 // No need to re-exec on Linux.
1570}
1571
1572void PrintModuleMap() { }
1573
1574void CheckNoDeepBind(const char *filename, int flag) {
1575#ifdef RTLD_DEEPBIND0x00008
1576 if (flag & RTLD_DEEPBIND0x00008) {
1577 Report(
1578 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
1579 " which is incompatibe with sanitizer runtime "
1580 "(see https://github.com/google/sanitizers/issues/611 for details"
1581 "). If you want to run %s library under sanitizers please remove "
1582 "RTLD_DEEPBIND from dlopen flags.\n",
1583 filename, filename);
1584 Die();
1585 }
1586#endif
1587}
1588
1589uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding) {
1590 UNREACHABLE("FindAvailableMemoryRange is not available")do { do { __sanitizer::u64 v1 = (u64)((0 && "FindAvailableMemoryRange is not available"
)); __sanitizer::u64 v2 = (u64)(0); if (__builtin_expect(!!(!
(v1 != v2)), 0)) __sanitizer::CheckFailed("/tmp/buildd/llvm-toolchain-snapshot-5.0~svn303373/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1590, "(" "(0 && \"FindAvailableMemoryRange is not available\")"
") " "!=" " (" "0" ")", v1, v2); } while (false); Die(); } while
(0)
;
1591 return 0;
1592}
1593
1594} // namespace __sanitizer
1595
1596#endif // SANITIZER_FREEBSD || SANITIZER_LINUX