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 <machine/atomic.h>
66extern "C" {
67// <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
68// FreeBSD 9.2 and 10.0.
69#include <sys/umtx.h>
70}
71extern char **environ; // provided by crt1
72#endif // SANITIZER_FREEBSD
73
74#if !SANITIZER_ANDROID0
75#include <sys/signal.h>
76#endif
77
78#ifndef __GLIBC_PREREQ
79#define __GLIBC_PREREQ(x, y)((2 << 16) + 24 >= ((x) << 16) + (y)) 0
80#endif
81
82#if SANITIZER_LINUX1 && __GLIBC_PREREQ(2, 16)((2 << 16) + 24 >= ((2) << 16) + (16))
83# define SANITIZER_USE_GETAUXVAL1 1
84#else
85# define SANITIZER_USE_GETAUXVAL1 0
86#endif
87
88#if SANITIZER_USE_GETAUXVAL1
89#include <sys/auxv.h>
90#endif
91
92#if SANITIZER_LINUX1
93// <linux/time.h>
94struct kernel_timeval {
95 long tv_sec;
96 long tv_usec;
97};
98
99// <linux/futex.h> is broken on some linux distributions.
100const int FUTEX_WAIT = 0;
101const int FUTEX_WAKE = 1;
102#endif // SANITIZER_LINUX
103
104// Are we using 32-bit or 64-bit Linux syscalls?
105// x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
106// but it still needs to use 64-bit syscalls.
107#if SANITIZER_LINUX1 && (defined(__x86_64__1) || defined(__powerpc64__) || \
108 SANITIZER_WORDSIZE64 == 64)
109# define SANITIZER_LINUX_USES_64BIT_SYSCALLS1 1
110#else
111# define SANITIZER_LINUX_USES_64BIT_SYSCALLS1 0
112#endif
113
114#if defined(__x86_64__1) || SANITIZER_MIPS640
115extern "C" {
116extern void internal_sigreturn();
117}
118#endif
119
120namespace __sanitizer {
121
122#if SANITIZER_LINUX1 && defined(__x86_64__1)
123#include "sanitizer_syscall_linux_x86_64.inc"
124#elif SANITIZER_LINUX1 && defined(__aarch64__)
125#include "sanitizer_syscall_linux_aarch64.inc"
126#else
127#include "sanitizer_syscall_generic.inc"
128#endif
129
130// --------------- sanitizer_libc.h
131#if !SANITIZER_S3900
132uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
133 OFF_T offset) {
134#if SANITIZER_FREEBSD0 || SANITIZER_LINUX_USES_64BIT_SYSCALLS1
135 return internal_syscall(SYSCALL(mmap)9, (uptr)addr, length, prot, flags, fd,
136 offset);
137#else
138 // mmap2 specifies file offset in 4096-byte units.
139 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 139, "(" "(IsAligned(offset, 4096))" ") " "!=" " (" "0" ")"
, v1, v2); } while (false)
;
140 return internal_syscall(SYSCALL(mmap2)__NR_mmap2, addr, length, prot, flags, fd,
141 offset / 4096);
142#endif
143}
144#endif // !SANITIZER_S390
145
146uptr internal_munmap(void *addr, uptr length) {
147 return internal_syscall(SYSCALL(munmap)11, (uptr)addr, length);
148}
149
150int internal_mprotect(void *addr, uptr length, int prot) {
151 return internal_syscall(SYSCALL(mprotect)10, (uptr)addr, length, prot);
152}
153
154uptr internal_close(fd_t fd) {
155 return internal_syscall(SYSCALL(close)3, fd);
156}
157
158uptr internal_open(const char *filename, int flags) {
159#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
160 return internal_syscall(SYSCALL(openat)257, AT_FDCWD-100, (uptr)filename, flags);
161#else
162 return internal_syscall(SYSCALL(open)2, (uptr)filename, flags);
163#endif
164}
165
166uptr internal_open(const char *filename, int flags, u32 mode) {
167#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
168 return internal_syscall(SYSCALL(openat)257, AT_FDCWD-100, (uptr)filename, flags,
169 mode);
170#else
171 return internal_syscall(SYSCALL(open)2, (uptr)filename, flags, mode);
172#endif
173}
174
175uptr internal_read(fd_t fd, void *buf, uptr count) {
176 sptr res;
177 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); }
178 count)){ int rverrno; do { res = ((sptr)internal_syscall(0, fd, (uptr
)buf, count)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
;
179 return res;
180}
181
182uptr internal_write(fd_t fd, const void *buf, uptr count) {
183 sptr res;
184 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); }
185 count)){ int rverrno; do { res = ((sptr)internal_syscall(1, fd, (uptr
)buf, count)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
;
186 return res;
187}
188
189uptr internal_ftruncate(fd_t fd, uptr size) {
190 sptr res;
191 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); }
192 (OFF_T)size)){ int rverrno; do { res = ((sptr)internal_syscall(77, fd, (OFF_T
)size)); } while (internal_iserror(res, &rverrno) &&
rverrno == 4); }
;
193 return res;
194}
195
196#if !SANITIZER_LINUX_USES_64BIT_SYSCALLS1 && !SANITIZER_FREEBSD0
197static void stat64_to_stat(struct stat64 *in, struct stat *out) {
198 internal_memset(out, 0, sizeof(*out));
199 out->st_dev = in->st_dev;
200 out->st_ino = in->st_ino;
201 out->st_mode = in->st_mode;
202 out->st_nlink = in->st_nlink;
203 out->st_uid = in->st_uid;
204 out->st_gid = in->st_gid;
205 out->st_rdev = in->st_rdev;
206 out->st_size = in->st_size;
207 out->st_blksize = in->st_blksize;
208 out->st_blocks = in->st_blocks;
209 out->st_atimest_atim.tv_sec = in->st_atimest_atim.tv_sec;
210 out->st_mtimest_mtim.tv_sec = in->st_mtimest_mtim.tv_sec;
211 out->st_ctimest_ctim.tv_sec = in->st_ctimest_ctim.tv_sec;
212 out->st_ino = in->st_ino;
213}
214#endif
215
216#if defined(__mips64)
217static void kernel_stat_to_stat(struct kernel_stat *in, struct stat *out) {
218 internal_memset(out, 0, sizeof(*out));
219 out->st_dev = in->st_dev;
220 out->st_ino = in->st_ino;
221 out->st_mode = in->st_mode;
222 out->st_nlink = in->st_nlink;
223 out->st_uid = in->st_uid;
224 out->st_gid = in->st_gid;
225 out->st_rdev = in->st_rdev;
226 out->st_size = in->st_size;
227 out->st_blksize = in->st_blksize;
228 out->st_blocks = in->st_blocks;
229 out->st_atimest_atim.tv_sec = in->st_atime_nsec;
230 out->st_mtimest_mtim.tv_sec = in->st_mtime_nsec;
231 out->st_ctimest_ctim.tv_sec = in->st_ctime_nsec;
232 out->st_ino = in->st_ino;
233}
234#endif
235
236uptr internal_stat(const char *path, void *buf) {
237#if SANITIZER_FREEBSD0
238 return internal_syscall(SYSCALL(fstatat)__NR_fstatat, AT_FDCWD-100, (uptr)path,
239 (uptr)buf, 0);
240#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
241 return internal_syscall(SYSCALL(newfstatat)262, AT_FDCWD-100, (uptr)path,
242 (uptr)buf, 0);
243#elif SANITIZER_LINUX_USES_64BIT_SYSCALLS1
244# if defined(__mips64)
245 // For mips64, stat syscall fills buffer in the format of kernel_stat
246 struct kernel_stat kbuf;
247 int res = internal_syscall(SYSCALL(stat)4, path, &kbuf);
248 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
249 return res;
250# else
251 return internal_syscall(SYSCALL(stat)4, (uptr)path, (uptr)buf);
252# endif
253#else
254 struct stat64 buf64;
255 int res = internal_syscall(SYSCALL(stat64)__NR_stat64, path, &buf64);
256 stat64_to_stat(&buf64, (struct stat *)buf);
257 return res;
258#endif
259}
260
261uptr internal_lstat(const char *path, void *buf) {
262#if SANITIZER_FREEBSD0
263 return internal_syscall(SYSCALL(fstatat)__NR_fstatat, AT_FDCWD-100, (uptr)path,
264 (uptr)buf, AT_SYMLINK_NOFOLLOW0x100);
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~svn306458/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~svn306458/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~svn306458/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_release);
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~svn306458/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~svn306458/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_FREEBSD0
608 return internal_syscall(SYSCALL(getdirentries)__NR_getdirentries, fd, (uptr)dirp, count, NULL__null);
609#elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
610 return internal_syscall(SYSCALL(getdents64)217, fd, (uptr)dirp, count);
611#else
612 return internal_syscall(SYSCALL(getdents)78, fd, (uptr)dirp, count);
613#endif
614}
615
616uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
617 return internal_syscall(SYSCALL(lseek)8, fd, offset, whence);
618}
619
620#if SANITIZER_LINUX1
621uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
622 return internal_syscall(SYSCALL(prctl)157, option, arg2, arg3, arg4, arg5);
623}
624#endif
625
626uptr internal_sigaltstack(const struct sigaltstack *ss,
627 struct sigaltstack *oss) {
628 return internal_syscall(SYSCALL(sigaltstack)131, (uptr)ss, (uptr)oss);
629}
630
631int internal_fork() {
632#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS0
633 return internal_syscall(SYSCALL(clone)56, SIGCHLD17, 0);
634#else
635 return internal_syscall(SYSCALL(fork)57);
636#endif
637}
638
639#if SANITIZER_LINUX1
640#define SA_RESTORER0x04000000 0x04000000
641// Doesn't set sa_restorer if the caller did not set it, so use with caution
642//(see below).
643int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
644 __sanitizer_kernel_sigaction_t k_act, k_oldact;
645 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
646 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
647 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
648 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
649 if (u_act) {
650 k_act.handler = u_act->handler;
651 k_act.sigaction = u_act->sigaction;
652 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
653 sizeof(__sanitizer_kernel_sigset_t));
654 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
655 k_act.sa_flags = u_act->sa_flags | SA_RESTORER0x04000000;
656 // FIXME: most often sa_restorer is unset, however the kernel requires it
657 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
658 // If sa_restorer passed to the kernel is NULL, the program may crash upon
659 // signal delivery or fail to unwind the stack in the signal handler.
660 // libc implementation of sigaction() passes its own restorer to
661 // rt_sigaction, so we need to do the same (we'll need to reimplement the
662 // restorers; for x86_64 the restorer address can be obtained from
663 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
664#if !SANITIZER_ANDROID0 || !SANITIZER_MIPS320
665 k_act.sa_restorer = u_act->sa_restorer;
666#endif
667 }
668
669 uptr result = internal_syscall(SYSCALL(rt_sigaction)13, (uptr)signum,
670 (uptr)(u_act ? &k_act : nullptr),
671 (uptr)(u_oldact ? &k_oldact : nullptr),
672 (uptr)sizeof(__sanitizer_kernel_sigset_t));
673
674 if ((result == 0) && u_oldact) {
675 u_oldact->handler = k_oldact.handler;
676 u_oldact->sigaction = k_oldact.sigaction;
677 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
678 sizeof(__sanitizer_kernel_sigset_t));
679 u_oldact->sa_flags = k_oldact.sa_flags;
680#if !SANITIZER_ANDROID0 || !SANITIZER_MIPS320
681 u_oldact->sa_restorer = k_oldact.sa_restorer;
682#endif
683 }
684 return result;
685}
686
687// Invokes sigaction via a raw syscall with a restorer, but does not support
688// all platforms yet.
689// We disable for Go simply because we have not yet added to buildgo.sh.
690#if (defined(__x86_64__1) || SANITIZER_MIPS640) && !SANITIZER_GO0
691int internal_sigaction_syscall(int signum, const void *act, void *oldact) {
692 if (act == nullptr)
693 return internal_sigaction_norestorer(signum, act, oldact);
694 __sanitizer_sigaction u_adjust;
695 internal_memcpy(&u_adjust, act, sizeof(u_adjust));
696#if !SANITIZER_ANDROID0 || !SANITIZER_MIPS320
697 if (u_adjust.sa_restorer == nullptr) {
698 u_adjust.sa_restorer = internal_sigreturn;
699 }
700#endif
701 return internal_sigaction_norestorer(signum, (const void *)&u_adjust,
702 oldact);
703}
704#endif // defined(__x86_64__) && !SANITIZER_GO
705#endif // SANITIZER_LINUX
706
707uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
708 __sanitizer_sigset_t *oldset) {
709#if SANITIZER_FREEBSD0
710 return internal_syscall(SYSCALL(sigprocmask)__NR_sigprocmask, how, set, oldset);
711#else
712 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
713 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
714 return internal_syscall(SYSCALL(rt_sigprocmask)14, (uptr)how,
715 (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
716 sizeof(__sanitizer_kernel_sigset_t));
717#endif
718}
719
720void internal_sigfillset(__sanitizer_sigset_t *set) {
721 internal_memset(set, 0xff, sizeof(*set));
722}
723
724void internal_sigemptyset(__sanitizer_sigset_t *set) {
725 internal_memset(set, 0, sizeof(*set));
726}
727
728#if SANITIZER_LINUX1
729void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
730 signum -= 1;
731 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 731, "(" "(signum)" ") " ">=" " (" "(0)" ")", v1, v2); }
while (false)
;
732 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 732, "(" "(signum)" ") " "<" " (" "(sizeof(*set) * 8)" ")"
, v1, v2); } while (false)
;
733 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
734 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
735 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
736 k_set->sig[idx] &= ~(1 << bit);
737}
738
739bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
740 signum -= 1;
741 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 741, "(" "(signum)" ") " ">=" " (" "(0)" ")", v1, v2); }
while (false)
;
742 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 742, "(" "(signum)" ") " "<" " (" "(sizeof(*set) * 8)" ")"
, v1, v2); } while (false)
;
743 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
744 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
745 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
746 return k_set->sig[idx] & (1 << bit);
747}
748#endif // SANITIZER_LINUX
749
750// ThreadLister implementation.
751ThreadLister::ThreadLister(int pid)
752 : pid_(pid),
753 descriptor_(-1),
754 buffer_(4096),
755 error_(true),
756 entry_((struct linux_dirent *)buffer_.data()),
757 bytes_read_(0) {
758 char task_directory_path[80];
759 internal_snprintf(task_directory_path, sizeof(task_directory_path),
760 "/proc/%d/task/", pid);
761 uptr openrv = internal_open(task_directory_path, O_RDONLY00 | O_DIRECTORY0200000);
762 if (internal_iserror(openrv)) {
763 error_ = true;
764 Report("Can't open /proc/%d/task for reading.\n", pid);
765 } else {
766 error_ = false;
767 descriptor_ = openrv;
768 }
769}
770
771int ThreadLister::GetNextTID() {
772 int tid = -1;
773 do {
774 if (error_)
775 return -1;
776 if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
777 return -1;
778 if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
779 entry_->d_name[0] <= '9') {
780 // Found a valid tid.
781 tid = (int)internal_atoll(entry_->d_name);
782 }
783 entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
784 } while (tid < 0);
785 return tid;
786}
787
788void ThreadLister::Reset() {
789 if (error_ || descriptor_ < 0)
790 return;
791 internal_lseek(descriptor_, 0, SEEK_SET0);
792}
793
794ThreadLister::~ThreadLister() {
795 if (descriptor_ >= 0)
796 internal_close(descriptor_);
797}
798
799bool ThreadLister::error() { return error_; }
800
801bool ThreadLister::GetDirectoryEntries() {
802 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 802, "(" "(descriptor_)" ") " ">=" " (" "(0)" ")", v1, v2
); } while (false)
;
803 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 803, "(" "(error_)" ") " "!=" " (" "(true)" ")", v1, v2); }
while (false)
;
804 bytes_read_ = internal_getdents(descriptor_,
805 (struct linux_dirent *)buffer_.data(),
806 buffer_.size());
807 if (internal_iserror(bytes_read_)) {
808 Report("Can't read directory entries from /proc/%d/task.\n", pid_);
809 error_ = true;
810 return false;
811 } else if (bytes_read_ == 0) {
812 return false;
813 }
814 entry_ = (struct linux_dirent *)buffer_.data();
815 return true;
816}
817
818uptr GetPageSize() {
819// Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
820#if SANITIZER_ANDROID0
821 return 4096;
822#elif SANITIZER_LINUX1 && (defined(__x86_64__1) || defined(__i386__))
823 return EXEC_PAGESIZE4096;
824#elif SANITIZER_USE_GETAUXVAL1
825 return getauxval(AT_PAGESZ6);
826#else
827 return sysconf(_SC_PAGESIZE_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
828#endif
829}
830
831uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
832#if SANITIZER_FREEBSD0
833 const int Mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
834 const char *default_module_name = "kern.proc.pathname";
835 size_t Size = buf_len;
836 bool IsErr = (sysctl(Mib, ARRAY_SIZE(Mib)(sizeof(Mib)/sizeof((Mib)[0])), buf, &Size, NULL__null, 0) != 0);
837 int readlink_error = IsErr ? errno(*__errno_location ()) : 0;
838 uptr module_name_len = Size;
839#else
840 const char *default_module_name = "/proc/self/exe";
841 uptr module_name_len = internal_readlink(
842 default_module_name, buf, buf_len);
843 int readlink_error;
844 bool IsErr = internal_iserror(module_name_len, &readlink_error);
845#endif
846 if (IsErr) {
847 // We can't read binary name for some reason, assume it's unknown.
848 Report("WARNING: reading executable name failed with errno %d, "
849 "some stack frames may not be symbolized\n", readlink_error);
850 module_name_len = internal_snprintf(buf, buf_len, "%s",
851 default_module_name);
852 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 852, "(" "(module_name_len)" ") " "<" " (" "(buf_len)" ")"
, v1, v2); } while (false)
;
853 }
854 return module_name_len;
855}
856
857uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
858#if SANITIZER_LINUX1
859 char *tmpbuf;
860 uptr tmpsize;
861 uptr tmplen;
862 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
863 1024 * 1024)) {
864 internal_strncpy(buf, tmpbuf, buf_len);
865 UnmapOrDie(tmpbuf, tmpsize);
866 return internal_strlen(buf);
867 }
868#endif
869 return ReadBinaryName(buf, buf_len);
870}
871
872// Match full names of the form /path/to/base_name{-,.}*
873bool LibraryNameIs(const char *full_name, const char *base_name) {
874 const char *name = full_name;
875 // Strip path.
876 while (*name != '\0') name++;
877 while (name > full_name && *name != '/') name--;
878 if (*name == '/') name++;
879 uptr base_name_length = internal_strlen(base_name);
880 if (internal_strncmp(name, base_name, base_name_length)) return false;
881 return (name[base_name_length] == '-' || name[base_name_length] == '.');
882}
883
884#if !SANITIZER_ANDROID0
885// Call cb for each region mapped by map.
886void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
887 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 887, "(" "(map)" ") " "!=" " (" "(nullptr)" ")", v1, v2); }
while (false)
;
888#if !SANITIZER_FREEBSD0
889 typedef ElfW(Phdr)Elf64_Phdr Elf_Phdr;
890 typedef ElfW(Ehdr)Elf64_Ehdr Elf_Ehdr;
891#endif // !SANITIZER_FREEBSD
892 char *base = (char *)map->l_addr;
893 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
894 char *phdrs = base + ehdr->e_phoff;
895 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
896
897 // Find the segment with the minimum base so we can "relocate" the p_vaddr
898 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
899 // objects have a non-zero base.
900 uptr preferred_base = (uptr)-1;
901 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
902 Elf_Phdr *phdr = (Elf_Phdr *)iter;
903 if (phdr->p_type == PT_LOAD1 && preferred_base > (uptr)phdr->p_vaddr)
904 preferred_base = (uptr)phdr->p_vaddr;
905 }
906
907 // Compute the delta from the real base to get a relocation delta.
908 sptr delta = (uptr)base - preferred_base;
909 // Now we can figure out what the loader really mapped.
910 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
911 Elf_Phdr *phdr = (Elf_Phdr *)iter;
912 if (phdr->p_type == PT_LOAD1) {
913 uptr seg_start = phdr->p_vaddr + delta;
914 uptr seg_end = seg_start + phdr->p_memsz;
915 // None of these values are aligned. We consider the ragged edges of the
916 // load command as defined, since they are mapped from the file.
917 seg_start = RoundDownTo(seg_start, GetPageSizeCached());
918 seg_end = RoundUpTo(seg_end, GetPageSizeCached());
919 cb((void *)seg_start, seg_end - seg_start);
920 }
921 }
922}
923#endif
924
925#if defined(__x86_64__1) && SANITIZER_LINUX1
926// We cannot use glibc's clone wrapper, because it messes with the child
927// task's TLS. It writes the PID and TID of the child task to its thread
928// descriptor, but in our case the child task shares the thread descriptor with
929// the parent (because we don't know how to allocate a new thread
930// descriptor to keep glibc happy). So the stock version of clone(), when
931// used with CLONE_VM, would end up corrupting the parent's thread descriptor.
932uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
933 int *parent_tidptr, void *newtls, int *child_tidptr) {
934 long long res;
935 if (!fn || !child_stack)
936 return -EINVAL22;
937 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 937, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
938 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
939 ((unsigned long long *)child_stack)[0] = (uptr)fn;
940 ((unsigned long long *)child_stack)[1] = (uptr)arg;
941 register void *r8 __asm__("r8") = newtls;
942 register int *r10 __asm__("r10") = child_tidptr;
943 __asm__ __volatile__(
944 /* %rax = syscall(%rax = SYSCALL(clone),
945 * %rdi = flags,
946 * %rsi = child_stack,
947 * %rdx = parent_tidptr,
948 * %r8 = new_tls,
949 * %r10 = child_tidptr)
950 */
951 "syscall\n"
952
953 /* if (%rax != 0)
954 * return;
955 */
956 "testq %%rax,%%rax\n"
957 "jnz 1f\n"
958
959 /* In the child. Terminate unwind chain. */
960 // XXX: We should also terminate the CFI unwind chain
961 // here. Unfortunately clang 3.2 doesn't support the
962 // necessary CFI directives, so we skip that part.
963 "xorq %%rbp,%%rbp\n"
964
965 /* Call "fn(arg)". */
966 "popq %%rax\n"
967 "popq %%rdi\n"
968 "call *%%rax\n"
969
970 /* Call _exit(%rax). */
971 "movq %%rax,%%rdi\n"
972 "movq %2,%%rax\n"
973 "syscall\n"
974
975 /* Return to parent. */
976 "1:\n"
977 : "=a" (res)
978 : "a"(SYSCALL(clone)56), "i"(SYSCALL(exit)60),
979 "S"(child_stack),
980 "D"(flags),
981 "d"(parent_tidptr),
982 "r"(r8),
983 "r"(r10)
984 : "rsp", "memory", "r11", "rcx");
985 return res;
986}
987#elif defined(__mips__)
988uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
989 int *parent_tidptr, void *newtls, int *child_tidptr) {
990 long long res;
991 if (!fn || !child_stack)
992 return -EINVAL22;
993 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 993, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
994 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
995 ((unsigned long long *)child_stack)[0] = (uptr)fn;
996 ((unsigned long long *)child_stack)[1] = (uptr)arg;
997 register void *a3 __asm__("$7") = newtls;
998 register int *a4 __asm__("$8") = child_tidptr;
999 // We don't have proper CFI directives here because it requires alot of code
1000 // for very marginal benefits.
1001 __asm__ __volatile__(
1002 /* $v0 = syscall($v0 = __NR_clone,
1003 * $a0 = flags,
1004 * $a1 = child_stack,
1005 * $a2 = parent_tidptr,
1006 * $a3 = new_tls,
1007 * $a4 = child_tidptr)
1008 */
1009 ".cprestore 16;\n"
1010 "move $4,%1;\n"
1011 "move $5,%2;\n"
1012 "move $6,%3;\n"
1013 "move $7,%4;\n"
1014 /* Store the fifth argument on stack
1015 * if we are using 32-bit abi.
1016 */
1017#if SANITIZER_WORDSIZE64 == 32
1018 "lw %5,16($29);\n"
1019#else
1020 "move $8,%5;\n"
1021#endif
1022 "li $2,%6;\n"
1023 "syscall;\n"
1024
1025 /* if ($v0 != 0)
1026 * return;
1027 */
1028 "bnez $2,1f;\n"
1029
1030 /* Call "fn(arg)". */
1031#if SANITIZER_WORDSIZE64 == 32
1032#ifdef __BIG_ENDIAN__
1033 "lw $25,4($29);\n"
1034 "lw $4,12($29);\n"
1035#else
1036 "lw $25,0($29);\n"
1037 "lw $4,8($29);\n"
1038#endif
1039#else
1040 "ld $25,0($29);\n"
1041 "ld $4,8($29);\n"
1042#endif
1043 "jal $25;\n"
1044
1045 /* Call _exit($v0). */
1046 "move $4,$2;\n"
1047 "li $2,%7;\n"
1048 "syscall;\n"
1049
1050 /* Return to parent. */
1051 "1:\n"
1052 : "=r" (res)
1053 : "r"(flags),
1054 "r"(child_stack),
1055 "r"(parent_tidptr),
1056 "r"(a3),
1057 "r"(a4),
1058 "i"(__NR_clone56),
1059 "i"(__NR_exit60)
1060 : "memory", "$29" );
1061 return res;
1062}
1063#elif defined(__aarch64__)
1064uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1065 int *parent_tidptr, void *newtls, int *child_tidptr) {
1066 long long res;
1067 if (!fn || !child_stack)
1068 return -EINVAL22;
1069 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1069, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
1070 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1071 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1072 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1073
1074 register int (*__fn)(void *) __asm__("x0") = fn;
1075 register void *__stack __asm__("x1") = child_stack;
1076 register int __flags __asm__("x2") = flags;
1077 register void *__arg __asm__("x3") = arg;
1078 register int *__ptid __asm__("x4") = parent_tidptr;
1079 register void *__tls __asm__("x5") = newtls;
1080 register int *__ctid __asm__("x6") = child_tidptr;
1081
1082 __asm__ __volatile__(
1083 "mov x0,x2\n" /* flags */
1084 "mov x2,x4\n" /* ptid */
1085 "mov x3,x5\n" /* tls */
1086 "mov x4,x6\n" /* ctid */
1087 "mov x8,%9\n" /* clone */
1088
1089 "svc 0x0\n"
1090
1091 /* if (%r0 != 0)
1092 * return %r0;
1093 */
1094 "cmp x0, #0\n"
1095 "bne 1f\n"
1096
1097 /* In the child, now. Call "fn(arg)". */
1098 "ldp x1, x0, [sp], #16\n"
1099 "blr x1\n"
1100
1101 /* Call _exit(%r0). */
1102 "mov x8, %10\n"
1103 "svc 0x0\n"
1104 "1:\n"
1105
1106 : "=r" (res)
1107 : "i"(-EINVAL22),
1108 "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1109 "r"(__ptid), "r"(__tls), "r"(__ctid),
1110 "i"(__NR_clone56), "i"(__NR_exit60)
1111 : "x30", "memory");
1112 return res;
1113}
1114#elif defined(__powerpc64__)
1115uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1116 int *parent_tidptr, void *newtls, int *child_tidptr) {
1117 long long res;
1118// Stack frame structure.
1119#if SANITIZER_PPC64V10
1120// Back chain == 0 (SP + 112)
1121// Frame (112 bytes):
1122// Parameter save area (SP + 48), 8 doublewords
1123// TOC save area (SP + 40)
1124// Link editor doubleword (SP + 32)
1125// Compiler doubleword (SP + 24)
1126// LR save area (SP + 16)
1127// CR save area (SP + 8)
1128// Back chain (SP + 0)
1129# define FRAME_SIZE 112
1130# define FRAME_TOC_SAVE_OFFSET 40
1131#elif SANITIZER_PPC64V20
1132// Back chain == 0 (SP + 32)
1133// Frame (32 bytes):
1134// TOC save area (SP + 24)
1135// LR save area (SP + 16)
1136// CR save area (SP + 8)
1137// Back chain (SP + 0)
1138# define FRAME_SIZE 32
1139# define FRAME_TOC_SAVE_OFFSET 24
1140#else
1141# error "Unsupported PPC64 ABI"
1142#endif
1143 if (!fn || !child_stack)
1144 return -EINVAL22;
1145 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1145, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
1146
1147 register int (*__fn)(void *) __asm__("r3") = fn;
1148 register void *__cstack __asm__("r4") = child_stack;
1149 register int __flags __asm__("r5") = flags;
1150 register void *__arg __asm__("r6") = arg;
1151 register int *__ptidptr __asm__("r7") = parent_tidptr;
1152 register void *__newtls __asm__("r8") = newtls;
1153 register int *__ctidptr __asm__("r9") = child_tidptr;
1154
1155 __asm__ __volatile__(
1156 /* fn and arg are saved across the syscall */
1157 "mr 28, %5\n\t"
1158 "mr 27, %8\n\t"
1159
1160 /* syscall
1161 r0 == __NR_clone
1162 r3 == flags
1163 r4 == child_stack
1164 r5 == parent_tidptr
1165 r6 == newtls
1166 r7 == child_tidptr */
1167 "mr 3, %7\n\t"
1168 "mr 5, %9\n\t"
1169 "mr 6, %10\n\t"
1170 "mr 7, %11\n\t"
1171 "li 0, %3\n\t"
1172 "sc\n\t"
1173
1174 /* Test if syscall was successful */
1175 "cmpdi cr1, 3, 0\n\t"
1176 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1177 "bne- cr1, 1f\n\t"
1178
1179 /* Set up stack frame */
1180 "li 29, 0\n\t"
1181 "stdu 29, -8(1)\n\t"
1182 "stdu 1, -%12(1)\n\t"
1183 /* Do the function call */
1184 "std 2, %13(1)\n\t"
1185#if SANITIZER_PPC64V10
1186 "ld 0, 0(28)\n\t"
1187 "ld 2, 8(28)\n\t"
1188 "mtctr 0\n\t"
1189#elif SANITIZER_PPC64V20
1190 "mr 12, 28\n\t"
1191 "mtctr 12\n\t"
1192#else
1193# error "Unsupported PPC64 ABI"
1194#endif
1195 "mr 3, 27\n\t"
1196 "bctrl\n\t"
1197 "ld 2, %13(1)\n\t"
1198
1199 /* Call _exit(r3) */
1200 "li 0, %4\n\t"
1201 "sc\n\t"
1202
1203 /* Return to parent */
1204 "1:\n\t"
1205 "mr %0, 3\n\t"
1206 : "=r" (res)
1207 : "0" (-1),
1208 "i" (EINVAL22),
1209 "i" (__NR_clone56),
1210 "i" (__NR_exit60),
1211 "r" (__fn),
1212 "r" (__cstack),
1213 "r" (__flags),
1214 "r" (__arg),
1215 "r" (__ptidptr),
1216 "r" (__newtls),
1217 "r" (__ctidptr),
1218 "i" (FRAME_SIZE),
1219 "i" (FRAME_TOC_SAVE_OFFSET)
1220 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1221 return res;
1222}
1223#elif defined(__i386__) && SANITIZER_LINUX1
1224uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1225 int *parent_tidptr, void *newtls, int *child_tidptr) {
1226 int res;
1227 if (!fn || !child_stack)
1228 return -EINVAL22;
1229 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1229, "(" "(0)" ") " "==" " (" "((uptr)child_stack % 16)" ")"
, v1, v2); } while (false)
;
1230 child_stack = (char *)child_stack - 7 * sizeof(unsigned int);
1231 ((unsigned int *)child_stack)[0] = (uptr)flags;
1232 ((unsigned int *)child_stack)[1] = (uptr)0;
1233 ((unsigned int *)child_stack)[2] = (uptr)fn;
1234 ((unsigned int *)child_stack)[3] = (uptr)arg;
1235 __asm__ __volatile__(
1236 /* %eax = syscall(%eax = SYSCALL(clone),
1237 * %ebx = flags,
1238 * %ecx = child_stack,
1239 * %edx = parent_tidptr,
1240 * %esi = new_tls,
1241 * %edi = child_tidptr)
1242 */
1243
1244 /* Obtain flags */
1245 "movl (%%ecx), %%ebx\n"
1246 /* Do the system call */
1247 "pushl %%ebx\n"
1248 "pushl %%esi\n"
1249 "pushl %%edi\n"
1250 /* Remember the flag value. */
1251 "movl %%ebx, (%%ecx)\n"
1252 "int $0x80\n"
1253 "popl %%edi\n"
1254 "popl %%esi\n"
1255 "popl %%ebx\n"
1256
1257 /* if (%eax != 0)
1258 * return;
1259 */
1260
1261 "test %%eax,%%eax\n"
1262 "jnz 1f\n"
1263
1264 /* terminate the stack frame */
1265 "xorl %%ebp,%%ebp\n"
1266 /* Call FN. */
1267 "call *%%ebx\n"
1268#ifdef PIC
1269 "call here\n"
1270 "here:\n"
1271 "popl %%ebx\n"
1272 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1273#endif
1274 /* Call exit */
1275 "movl %%eax, %%ebx\n"
1276 "movl %2, %%eax\n"
1277 "int $0x80\n"
1278 "1:\n"
1279 : "=a" (res)
1280 : "a"(SYSCALL(clone)56), "i"(SYSCALL(exit)60),
1281 "c"(child_stack),
1282 "d"(parent_tidptr),
1283 "S"(newtls),
1284 "D"(child_tidptr)
1285 : "memory");
1286 return res;
1287}
1288#elif defined(__arm__) && SANITIZER_LINUX1
1289uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1290 int *parent_tidptr, void *newtls, int *child_tidptr) {
1291 unsigned int res;
1292 if (!fn || !child_stack)
1293 return -EINVAL22;
1294 child_stack = (char *)child_stack - 2 * sizeof(unsigned int);
1295 ((unsigned int *)child_stack)[0] = (uptr)fn;
1296 ((unsigned int *)child_stack)[1] = (uptr)arg;
1297 register int r0 __asm__("r0") = flags;
1298 register void *r1 __asm__("r1") = child_stack;
1299 register int *r2 __asm__("r2") = parent_tidptr;
1300 register void *r3 __asm__("r3") = newtls;
1301 register int *r4 __asm__("r4") = child_tidptr;
1302 register int r7 __asm__("r7") = __NR_clone56;
1303
1304#if __ARM_ARCH > 4 || defined (__ARM_ARCH_4T__)
1305# define ARCH_HAS_BX
1306#endif
1307#if __ARM_ARCH > 4
1308# define ARCH_HAS_BLX
1309#endif
1310
1311#ifdef ARCH_HAS_BX
1312# ifdef ARCH_HAS_BLX
1313# define BLX(R) "blx " #R "\n"
1314# else
1315# define BLX(R) "mov lr, pc; bx " #R "\n"
1316# endif
1317#else
1318# define BLX(R) "mov lr, pc; mov pc," #R "\n"
1319#endif
1320
1321 __asm__ __volatile__(
1322 /* %r0 = syscall(%r7 = SYSCALL(clone),
1323 * %r0 = flags,
1324 * %r1 = child_stack,
1325 * %r2 = parent_tidptr,
1326 * %r3 = new_tls,
1327 * %r4 = child_tidptr)
1328 */
1329
1330 /* Do the system call */
1331 "swi 0x0\n"
1332
1333 /* if (%r0 != 0)
1334 * return %r0;
1335 */
1336 "cmp r0, #0\n"
1337 "bne 1f\n"
1338
1339 /* In the child, now. Call "fn(arg)". */
1340 "ldr r0, [sp, #4]\n"
1341 "ldr ip, [sp], #8\n"
1342 BLX(ip)
1343 /* Call _exit(%r0). */
1344 "mov r7, %7\n"
1345 "swi 0x0\n"
1346 "1:\n"
1347 "mov %0, r0\n"
1348 : "=r"(res)
1349 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7),
1350 "i"(__NR_exit60)
1351 : "memory");
1352 return res;
1353}
1354#endif // defined(__x86_64__) && SANITIZER_LINUX
1355
1356#if SANITIZER_ANDROID0
1357#if __ANDROID_API__ < 21
1358extern "C" __attribute__((weak)) int dl_iterate_phdr(
1359 int (*)(struct dl_phdr_info *, size_t, void *), void *);
1360#endif
1361
1362static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1363 void *data) {
1364 // Any name starting with "lib" indicates a bug in L where library base names
1365 // are returned instead of paths.
1366 if (info->dlpi_name && info->dlpi_name[0] == 'l' &&
1367 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') {
1368 *(bool *)data = true;
1369 return 1;
1370 }
1371 return 0;
1372}
1373
1374static atomic_uint32_t android_api_level;
1375
1376static AndroidApiLevel AndroidDetectApiLevel() {
1377 if (!&dl_iterate_phdr)
1378 return ANDROID_KITKAT; // K or lower
1379 bool base_name_seen = false;
1380 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen);
1381 if (base_name_seen)
1382 return ANDROID_LOLLIPOP_MR1; // L MR1
1383 return ANDROID_POST_LOLLIPOP; // post-L
1384 // Plain L (API level 21) is completely broken wrt ASan and not very
1385 // interesting to detect.
1386}
1387
1388AndroidApiLevel AndroidGetApiLevel() {
1389 AndroidApiLevel level =
1390 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed);
1391 if (level) return level;
1392 level = AndroidDetectApiLevel();
1393 atomic_store(&android_api_level, level, memory_order_relaxed);
1394 return level;
1395}
1396
1397#endif
1398
1399static HandleSignalMode GetHandleSignalModeImpl(int signum) {
1400 switch (signum) {
1401 case SIGABRT6:
1402 return common_flags()->handle_abort;
1403 case SIGILL4:
1404 return common_flags()->handle_sigill;
1405 case SIGFPE8:
1406 return common_flags()->handle_sigfpe;
1407 case SIGSEGV11:
1408 return common_flags()->handle_segv;
1409 case SIGBUS7:
1410 return common_flags()->handle_sigbus;
1411 }
1412 return kHandleSignalNo;
1413}
1414
1415HandleSignalMode GetHandleSignalMode(int signum) {
1416 HandleSignalMode result = GetHandleSignalModeImpl(signum);
1417 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
1418 return kHandleSignalExclusive;
1419 return result;
1420}
1421
1422#if !SANITIZER_GO0
1423void *internal_start_thread(void(*func)(void *arg), void *arg) {
1424 // Start the thread with signals blocked, otherwise it can steal user signals.
1425 __sanitizer_sigset_t set, old;
1426 internal_sigfillset(&set);
1427#if SANITIZER_LINUX1 && !SANITIZER_ANDROID0
1428 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
1429 // on any thread, setuid call hangs (see test/tsan/setuid.c).
1430 internal_sigdelset(&set, 33);
1431#endif
1432 internal_sigprocmask(SIG_SETMASK2, &set, &old);
1433 void *th;
1434 real_pthread_create(&th, nullptr, (void*(*)(void *arg))func, arg);
1435 internal_sigprocmask(SIG_SETMASK2, &old, nullptr);
1436 return th;
1437}
1438
1439void internal_join_thread(void *th) {
1440 real_pthread_join(th, nullptr);
1441}
1442#else
1443void *internal_start_thread(void (*func)(void *), void *arg) { return 0; }
1444
1445void internal_join_thread(void *th) {}
1446#endif
1447
1448#if defined(__aarch64__)
1449// Android headers in the older NDK releases miss this definition.
1450struct __sanitizer_esr_context {
1451 struct _aarch64_ctx head;
1452 uint64_t esr;
1453};
1454
1455static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1456 static const u32 kEsrMagic = 0x45535201;
1457 u8 *aux = ucontext->uc_mcontext.__reserved;
1458 while (true) {
1459 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1460 if (ctx->size == 0) break;
1461 if (ctx->magic == kEsrMagic) {
1462 *esr = ((__sanitizer_esr_context *)ctx)->esr;
1463 return true;
1464 }
1465 aux += ctx->size;
1466 }
1467 return false;
1468}
1469#endif
1470
1471SignalContext::WriteFlag SignalContext::GetWriteFlag(void *context) {
1472 ucontext_t *ucontext = (ucontext_t *)context;
1473#if defined(__x86_64__1) || defined(__i386__)
1474 static const uptr PF_WRITE = 1U << 1;
1475#if SANITIZER_FREEBSD0
1476 uptr err = ucontext->uc_mcontext.mc_err;
1477#else
1478 uptr err = ucontext->uc_mcontext.gregs[REG_ERRREG_ERR];
1479#endif
1480 return err & PF_WRITE ? WRITE : READ;
1481#elif defined(__arm__)
1482 static const uptr FSR_WRITE = 1U << 11;
1483 uptr fsr = ucontext->uc_mcontext.error_code;
1484 return fsr & FSR_WRITE ? WRITE : READ;
1485#elif defined(__aarch64__)
1486 static const u64 ESR_ELx_WNR = 1U << 6;
1487 u64 esr;
1488 if (!Aarch64GetESR(ucontext, &esr)) return UNKNOWN;
1489 return esr & ESR_ELx_WNR ? WRITE : READ;
1490#else
1491 (void)ucontext;
1492 return UNKNOWN; // FIXME: Implement.
1493#endif
1494}
1495
1496void SignalContext::DumpAllRegisters(void *context) {
1497 // FIXME: Implement this.
1498}
1499
1500void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
1501#if defined(__arm__)
1502 ucontext_t *ucontext = (ucontext_t*)context;
1503 *pc = ucontext->uc_mcontext.arm_pc;
1504 *bp = ucontext->uc_mcontext.arm_fp;
1505 *sp = ucontext->uc_mcontext.arm_sp;
1506#elif defined(__aarch64__)
1507 ucontext_t *ucontext = (ucontext_t*)context;
1508 *pc = ucontext->uc_mcontext.pc;
1509 *bp = ucontext->uc_mcontext.regs[29];
1510 *sp = ucontext->uc_mcontext.sp;
1511#elif defined(__hppa__)
1512 ucontext_t *ucontext = (ucontext_t*)context;
1513 *pc = ucontext->uc_mcontext.sc_iaoq[0];
1514 /* GCC uses %r3 whenever a frame pointer is needed. */
1515 *bp = ucontext->uc_mcontext.sc_gr[3];
1516 *sp = ucontext->uc_mcontext.sc_gr[30];
1517#elif defined(__x86_64__1)
1518# if SANITIZER_FREEBSD0
1519 ucontext_t *ucontext = (ucontext_t*)context;
1520 *pc = ucontext->uc_mcontext.mc_rip;
1521 *bp = ucontext->uc_mcontext.mc_rbp;
1522 *sp = ucontext->uc_mcontext.mc_rsp;
1523# else
1524 ucontext_t *ucontext = (ucontext_t*)context;
1525 *pc = ucontext->uc_mcontext.gregs[REG_RIPREG_RIP];
1526 *bp = ucontext->uc_mcontext.gregs[REG_RBPREG_RBP];
1527 *sp = ucontext->uc_mcontext.gregs[REG_RSPREG_RSP];
1528# endif
1529#elif defined(__i386__)
1530# if SANITIZER_FREEBSD0
1531 ucontext_t *ucontext = (ucontext_t*)context;
1532 *pc = ucontext->uc_mcontext.mc_eip;
1533 *bp = ucontext->uc_mcontext.mc_ebp;
1534 *sp = ucontext->uc_mcontext.mc_esp;
1535# else
1536 ucontext_t *ucontext = (ucontext_t*)context;
1537 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
1538 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
1539 *sp = ucontext->uc_mcontext.gregs[REG_ESP];
1540# endif
1541#elif defined(__powerpc__) || defined(__powerpc64__)
1542 ucontext_t *ucontext = (ucontext_t*)context;
1543 *pc = ucontext->uc_mcontext.regs->nip;
1544 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
1545 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
1546 // pointer, but GCC always uses r31 when we need a frame pointer.
1547 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
1548#elif defined(__sparc__)
1549 ucontext_t *ucontext = (ucontext_t*)context;
1550 uptr *stk_ptr;
1551# if defined (__arch64__)
1552 *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
1553 *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
1554 stk_ptr = (uptr *) (*sp + 2047);
1555 *bp = stk_ptr[15];
1556# else
1557 *pc = ucontext->uc_mcontext.gregs[REG_PC];
1558 *sp = ucontext->uc_mcontext.gregs[REG_O6];
1559 stk_ptr = (uptr *) *sp;
1560 *bp = stk_ptr[15];
1561# endif
1562#elif defined(__mips__)
1563 ucontext_t *ucontext = (ucontext_t*)context;
1564 *pc = ucontext->uc_mcontext.pc;
1565 *bp = ucontext->uc_mcontext.gregs[30];
1566 *sp = ucontext->uc_mcontext.gregs[29];
1567#elif defined(__s390__)
1568 ucontext_t *ucontext = (ucontext_t*)context;
1569# if defined(__s390x__)
1570 *pc = ucontext->uc_mcontext.psw.addr;
1571# else
1572 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
1573# endif
1574 *bp = ucontext->uc_mcontext.gregs[11];
1575 *sp = ucontext->uc_mcontext.gregs[15];
1576#else
1577# error "Unsupported arch"
1578#endif
1579}
1580
1581void MaybeReexec() {
1582 // No need to re-exec on Linux.
1583}
1584
1585void PrintModuleMap() { }
1586
1587void CheckNoDeepBind(const char *filename, int flag) {
1588#ifdef RTLD_DEEPBIND0x00008
1589 if (flag & RTLD_DEEPBIND0x00008) {
1590 Report(
1591 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
1592 " which is incompatibe with sanitizer runtime "
1593 "(see https://github.com/google/sanitizers/issues/611 for details"
1594 "). If you want to run %s library under sanitizers please remove "
1595 "RTLD_DEEPBIND from dlopen flags.\n",
1596 filename, filename);
1597 Die();
1598 }
1599#endif
1600}
1601
1602uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding) {
1603 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~svn306458/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc"
, 1603, "(" "(0 && \"FindAvailableMemoryRange is not available\")"
") " "!=" " (" "0" ")", v1, v2); } while (false); Die(); } while
(0)
;
1604 return 0;
1605}
1606
1607bool GetRandom(void *buffer, uptr length) {
1608 if (!buffer || !length || length > 256)
1609 return false;
1610#if defined(__NR_getrandom318)
1611 static atomic_uint8_t skip_getrandom_syscall;
1612 if (!atomic_load_relaxed(&skip_getrandom_syscall)) {
1613 // Up to 256 bytes, getrandom will not be interrupted.
1614 uptr res = internal_syscall(SYSCALL(getrandom)318, buffer, length, 0);
1615 int rverrno = 0;
1616 if (internal_iserror(res, &rverrno) && rverrno == ENOSYS38)
1617 atomic_store_relaxed(&skip_getrandom_syscall, 1);
1618 else if (res == length)
1619 return true;
1620 }
1621#endif
1622 uptr fd = internal_open("/dev/urandom", O_RDONLY00);
1623 if (internal_iserror(fd))
1624 return false;
1625 // internal_read deals with EINTR.
1626 uptr res = internal_read(fd, buffer, length);
1627 if (internal_iserror(res))
1628 return false;
1629 internal_close(fd);
1630 return true;
1631}
1632
1633} // namespace __sanitizer
1634
1635#endif // SANITIZER_FREEBSD || SANITIZER_LINUX