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