Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name sanitizer_linux.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D HAVE_RPC_XDR_H=0 -D HAVE_TIRPC_RPC_XDR_H=0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/projects/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-7~svn329677/projects/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -I /build/llvm-toolchain-snapshot-7~svn329677/projects/compiler-rt/lib/sanitizer_common/.. -U NDEBUG -D SANITIZER_SUPPORTS_WEAK_HOOKS=0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-unused-parameter -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-unused-parameter -Wno-variadic-macros -Wno-non-virtual-dtor -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/projects/compiler-rt/lib/sanitizer_common -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -fvisibility-inlines-hidden -fno-builtin -fno-rtti -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc

/build/llvm-toolchain-snapshot-7~svn329677/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc

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

/build/llvm-toolchain-snapshot-7~svn329677/projects/compiler-rt/lib/sanitizer_common/sanitizer_syscall_linux_x86_64.inc

1//===-- sanitizer_syscall_linux_x86_64.inc ----------------------*- C++ -*-===//
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// Implementations of internal_syscall and internal_iserror for Linux/x86_64.
11//
12//===----------------------------------------------------------------------===//
13
14#define SYSCALL(name)__NR_name __NR_ ## name
15
16static uptr internal_syscall(u64 nr) {
17 u64 retval;
18 asm volatile("syscall" : "=a"(retval) : "a"(nr) : "rcx", "r11",
19 "memory", "cc");
20 return retval;
21}
22
23#define internal_syscall_ptrinternal_syscall internal_syscall
24#define internal_syscall64internal_syscall internal_syscall
25
26template <typename T1>
27static uptr internal_syscall(u64 nr, T1 arg1) {
28 u64 retval;
29 asm volatile("syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1) :
30 "rcx", "r11", "memory", "cc");
31 return retval;
32}
33
34template <typename T1, typename T2>
35static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2) {
36 u64 retval;
37 asm volatile("syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
38 "S"((u64)arg2) : "rcx", "r11", "memory", "cc");
39 return retval;
3
Returning without writing to 'arg2->st_size'
40}
41
42template <typename T1, typename T2, typename T3>
43static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3) {
44 u64 retval;
45 asm volatile("syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
46 "S"((u64)arg2), "d"((u64)arg3) : "rcx", "r11", "memory", "cc");
47 return retval;
48}
49
50template <typename T1, typename T2, typename T3, typename T4>
51static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
52 u64 retval;
53 asm volatile("mov %5, %%r10;"
54 "syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
55 "S"((u64)arg2), "d"((u64)arg3), "r"((u64)arg4) :
56 "rcx", "r11", "r10", "memory", "cc");
57 return retval;
58}
59
60template <typename T1, typename T2, typename T3, typename T4, typename T5>
61static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
62 T5 arg5) {
63 u64 retval;
64 asm volatile("mov %5, %%r10;"
65 "mov %6, %%r8;"
66 "syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
67 "S"((u64)arg2), "d"((u64)arg3), "r"((u64)arg4), "r"((u64)arg5) :
68 "rcx", "r11", "r10", "r8", "memory", "cc");
69 return retval;
70}
71
72template <typename T1, typename T2, typename T3, typename T4, typename T5,
73 typename T6>
74static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
75 T5 arg5, T6 arg6) {
76 u64 retval;
77 asm volatile("mov %5, %%r10;"
78 "mov %6, %%r8;"
79 "mov %7, %%r9;"
80 "syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
81 "S"((u64)arg2), "d"((u64)arg3), "r"((u64)arg4), "r"((u64)arg5),
82 "r"((u64)arg6) : "rcx", "r11", "r10", "r8", "r9",
83 "memory", "cc");
84 return retval;
85}
86
87bool internal_iserror(uptr retval, int *rverrno) {
88 if (retval >= (uptr)-4095) {
89 if (rverrno)
90 *rverrno = -retval;
91 return true;
92 }
93 return false;
94}