Bug Summary

File:compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Warning:line 462, 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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.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-11~++20200309111110+2c36c23f347/build-llvm/projects/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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-11/lib/clang/11.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-11~++20200309111110+2c36c23f347/build-llvm/projects/compiler-rt/lib/sanitizer_common -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -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-2020-03-09-184146-41876-1 -x c++ /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

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

/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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}