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