Bug Summary

File:projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Warning:line 464, column 10
The left operand of '&' is a garbage value

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.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D HAVE_RPC_XDR_H=1 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/projects/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-10~svn374877/projects/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/include -I /build/llvm-toolchain-snapshot-10~svn374877/include -I /build/llvm-toolchain-snapshot-10~svn374877/projects/compiler-rt/lib/sanitizer_common/.. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.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-variadic-macros -Wno-non-virtual-dtor -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/projects/compiler-rt/lib/sanitizer_common -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~svn374877=. -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -fvisibility-inlines-hidden -fno-builtin -fno-rtti -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-10-15-233810-7101-1 -x c++ /build/llvm-toolchain-snapshot-10~svn374877/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

/build/llvm-toolchain-snapshot-10~svn374877/projects/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

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

/build/llvm-toolchain-snapshot-10~svn374877/projects/compiler-rt/lib/sanitizer_common/sanitizer_syscall_linux_x86_64.inc

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