Debian Codegraph @

libsanitizer/sanitizer_common/sanitizer_linux.cpp

raw
1 //===-- sanitizer_linux.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements linux-specific functions from
11 // sanitizer_libc.h.
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_platform.h"
15
16 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
17 SANITIZER_SOLARIS
18
19 # include "sanitizer_common.h"
20 # include "sanitizer_flags.h"
21 # include "sanitizer_getauxval.h"
22 # include "sanitizer_internal_defs.h"
23 # include "sanitizer_libc.h"
24 # include "sanitizer_linux.h"
25 # include "sanitizer_mutex.h"
26 # include "sanitizer_placement_new.h"
27 # include "sanitizer_procmaps.h"
28
29 # if SANITIZER_LINUX && !SANITIZER_GO
30 # include <asm/param.h>
31 # endif
32
33 // For mips64, syscall(__NR_stat) fills the buffer in the 'struct kernel_stat'
34 // format. Struct kernel_stat is defined as 'struct stat' in asm/stat.h. To
35 // access stat from asm/stat.h, without conflicting with definition in
36 // sys/stat.h, we use this trick. sparc64 is similar, using
37 // syscall(__NR_stat64) and struct kernel_stat64.
38 # if SANITIZER_LINUX && (SANITIZER_MIPS64 || SANITIZER_SPARC64)
39 # include <asm/unistd.h>
40 # include <sys/types.h>
41 # define stat kernel_stat
42 # if SANITIZER_SPARC64
43 # define stat64 kernel_stat64
44 # endif
45 # if SANITIZER_GO
46 # undef st_atime
47 # undef st_mtime
48 # undef st_ctime
49 # define st_atime st_atim
50 # define st_mtime st_mtim
51 # define st_ctime st_ctim
52 # endif
53 # include <asm/stat.h>
54 # undef stat
55 # undef stat64
56 # endif
57
58 # include <dlfcn.h>
59 # include <errno.h>
60 # include <fcntl.h>
61 # include <link.h>
62 # include <pthread.h>
63 # include <sched.h>
64 # include <signal.h>
65 # include <sys/mman.h>
66 # if !SANITIZER_SOLARIS
67 # include <sys/ptrace.h>
68 # endif
69 # include <sys/resource.h>
70 # include <sys/stat.h>
71 # include <sys/syscall.h>
72 # include <sys/time.h>
73 # include <sys/types.h>
74 # include <ucontext.h>
75 # include <unistd.h>
76
77 # if SANITIZER_LINUX
78 # include <sys/utsname.h>
79 # endif
80
81 # if SANITIZER_LINUX && !SANITIZER_ANDROID
82 # include <sys/personality.h>
83 # endif
84
85 # if SANITIZER_LINUX && defined(__loongarch__)
86 # include <sys/sysmacros.h>
87 # endif
88
89 # if SANITIZER_LINUX && defined(__powerpc64__)
90 # include <asm/ptrace.h>
91 # endif
92
93 # if SANITIZER_FREEBSD
94 # include <machine/atomic.h>
95 # include <sys/exec.h>
96 # include <sys/procctl.h>
97 # include <sys/sysctl.h>
98 extern "C" {
99 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
100 // FreeBSD 9.2 and 10.0.
101 # include <sys/umtx.h>
102 }
103 # include <sys/thr.h>
104 # endif // SANITIZER_FREEBSD
105
106 # if SANITIZER_NETBSD
107 # include <limits.h> // For NAME_MAX
108 # include <sys/exec.h>
109 # include <sys/sysctl.h>
110 extern struct ps_strings *__ps_strings;
111 # endif // SANITIZER_NETBSD
112
113 # if SANITIZER_SOLARIS
114 # include <stddef.h>
115 # include <stdlib.h>
116 # include <sys/frame.h>
117 # include <thread.h>
118 # define environ _environ
119 # endif
120
121 extern char **environ;
122
123 # if SANITIZER_LINUX
124 // <linux/time.h>
125 struct kernel_timeval {
126 long tv_sec;
127 long tv_usec;
128 };
129
130 // <linux/futex.h> is broken on some linux distributions.
131 const int FUTEX_WAIT = 0;
132 const int FUTEX_WAKE = 1;
133 const int FUTEX_PRIVATE_FLAG = 128;
134 const int FUTEX_WAIT_PRIVATE = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
135 const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
136 # endif // SANITIZER_LINUX
137
138 // Are we using 32-bit or 64-bit Linux syscalls?
139 // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
140 // but it still needs to use 64-bit syscalls.
141 # if SANITIZER_LINUX && (defined(__x86_64__) || defined(__powerpc64__) || \
142 SANITIZER_WORDSIZE == 64 || \
143 (defined(__mips__) && _MIPS_SIM == _ABIN32))
144 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
145 # else
146 # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
147 # endif
148
149 // Note : FreeBSD implemented both Linux and OpenBSD apis.
150 # if SANITIZER_LINUX && defined(__NR_getrandom)
151 # if !defined(GRND_NONBLOCK)
152 # define GRND_NONBLOCK 1
153 # endif
154 # define SANITIZER_USE_GETRANDOM 1
155 # else
156 # define SANITIZER_USE_GETRANDOM 0
157 # endif // SANITIZER_LINUX && defined(__NR_getrandom)
158
159 # if SANITIZER_FREEBSD
160 # define SANITIZER_USE_GETENTROPY 1
161 extern "C" void *__sys_mmap(void *addr, size_t len, int prot, int flags, int fd,
162 off_t offset);
163 # endif
164
165 namespace __sanitizer {
166
167 void SetSigProcMask(__sanitizer_sigset_t *set, __sanitizer_sigset_t *oldset) {
168 CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, set, oldset));
169 }
170
171 // Block asynchronous signals
172 void BlockSignals(__sanitizer_sigset_t *oldset) {
173 __sanitizer_sigset_t set;
174 internal_sigfillset(&set);
175 # if SANITIZER_LINUX && !SANITIZER_ANDROID
176 // Glibc uses SIGSETXID signal during setuid call. If this signal is blocked
177 // on any thread, setuid call hangs.
178 // See test/sanitizer_common/TestCases/Linux/setuid.c.
179 internal_sigdelset(&set, 33);
180 # endif
181 # if SANITIZER_LINUX
182 // Seccomp-BPF-sandboxed processes rely on SIGSYS to handle trapped syscalls.
183 // If this signal is blocked, such calls cannot be handled and the process may
184 // hang.
185 internal_sigdelset(&set, 31);
186
187 // Don't block synchronous signals
188 internal_sigdelset(&set, SIGSEGV);
189 internal_sigdelset(&set, SIGBUS);
190 internal_sigdelset(&set, SIGILL);
191 internal_sigdelset(&set, SIGTRAP);
192 internal_sigdelset(&set, SIGABRT);
193 internal_sigdelset(&set, SIGFPE);
194 internal_sigdelset(&set, SIGPIPE);
195 # endif
196
197 SetSigProcMask(&set, oldset);
198 }
199
200 ScopedBlockSignals::ScopedBlockSignals(__sanitizer_sigset_t *copy) {
201 BlockSignals(&saved_);
202 if (copy)
203 internal_memcpy(copy, &saved_, sizeof(saved_));
204 }
205
206 ScopedBlockSignals::~ScopedBlockSignals() { SetSigProcMask(&saved_, nullptr); }
207
208 # if SANITIZER_LINUX && defined(__x86_64__)
209 # include "sanitizer_syscall_linux_x86_64.inc"
210 # elif SANITIZER_LINUX && SANITIZER_RISCV64
211 # include "sanitizer_syscall_linux_riscv64.inc"
212 # elif SANITIZER_LINUX && defined(__aarch64__)
213 # include "sanitizer_syscall_linux_aarch64.inc"
214 # elif SANITIZER_LINUX && defined(__arm__)
215 # include "sanitizer_syscall_linux_arm.inc"
216 # elif SANITIZER_LINUX && defined(__hexagon__)
217 # include "sanitizer_syscall_linux_hexagon.inc"
218 # elif SANITIZER_LINUX && SANITIZER_LOONGARCH64
219 # include "sanitizer_syscall_linux_loongarch64.inc"
220 # else
221 # include "sanitizer_syscall_generic.inc"
222 # endif
223
224 // --------------- sanitizer_libc.h
225 # if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
226 # if !SANITIZER_S390
227 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
228 u64 offset) {
229 # if SANITIZER_FREEBSD
230 return (uptr)__sys_mmap(addr, length, prot, flags, fd, offset);
231 # elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
232 return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
233 offset);
234 # else
235 // mmap2 specifies file offset in 4096-byte units.
236 CHECK(IsAligned(offset, 4096));
237 return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
238 (OFF_T)(offset / 4096));
239 # endif
240 }
241 # endif // !SANITIZER_S390
242
243 uptr internal_munmap(void *addr, uptr length) {
244 return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
245 }
246
247 # if SANITIZER_LINUX
248 uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
249 void *new_address) {
250 return internal_syscall(SYSCALL(mremap), (uptr)old_address, old_size,
251 new_size, flags, (uptr)new_address);
252 }
253 # endif
254
255 int internal_mprotect(void *addr, uptr length, int prot) {
256 return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot);
257 }
258
259 int internal_madvise(uptr addr, uptr length, int advice) {
260 return internal_syscall(SYSCALL(madvise), addr, length, advice);
261 }
262
263 # if SANITIZER_FREEBSD
264 uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags) {
265 return internal_syscall(SYSCALL(close_range), lowfd, highfd, flags);
266 }
267 # endif
268 uptr internal_close(fd_t fd) { return internal_syscall(SYSCALL(close), fd); }
269
270 uptr internal_open(const char *filename, int flags) {
271 # if SANITIZER_LINUX
272 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
273 # else
274 return internal_syscall(SYSCALL(open), (uptr)filename, flags);
275 # endif
276 }
277
278 uptr internal_open(const char *filename, int flags, u32 mode) {
279 # if SANITIZER_LINUX
280 return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
281 mode);
282 # else
283 return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
284 # endif
285 }
286
287 uptr internal_read(fd_t fd, void *buf, uptr count) {
288 sptr res;
289 HANDLE_EINTR(res,
290 (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf, count));
291 return res;
292 }
293
294 uptr internal_write(fd_t fd, const void *buf, uptr count) {
295 sptr res;
296 HANDLE_EINTR(res,
297 (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf, count));
298 return res;
299 }
300
301 uptr internal_ftruncate(fd_t fd, uptr size) {
302 sptr res;
303 HANDLE_EINTR(res,
304 (sptr)internal_syscall(SYSCALL(ftruncate), fd, (OFF_T)size));
305 return res;
306 }
307
308 # if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && SANITIZER_LINUX
309 static void stat64_to_stat(struct stat64 *in, struct stat *out) {
310 internal_memset(out, 0, sizeof(*out));
311 out->st_dev = in->st_dev;
312 out->st_ino = in->st_ino;
313 out->st_mode = in->st_mode;
314 out->st_nlink = in->st_nlink;
315 out->st_uid = in->st_uid;
316 out->st_gid = in->st_gid;
317 out->st_rdev = in->st_rdev;
318 out->st_size = in->st_size;
319 out->st_blksize = in->st_blksize;
320 out->st_blocks = in->st_blocks;
321 out->st_atime = in->st_atime;
322 out->st_mtime = in->st_mtime;
323 out->st_ctime = in->st_ctime;
324 }
325 # endif
326
327 # if SANITIZER_LINUX && defined(__loongarch__)
328 static void statx_to_stat(struct statx *in, struct stat *out) {
329 internal_memset(out, 0, sizeof(*out));
330 out->st_dev = makedev(in->stx_dev_major, in->stx_dev_minor);
331 out->st_ino = in->stx_ino;
332 out->st_mode = in->stx_mode;
333 out->st_nlink = in->stx_nlink;
334 out->st_uid = in->stx_uid;
335 out->st_gid = in->stx_gid;
336 out->st_rdev = makedev(in->stx_rdev_major, in->stx_rdev_minor);
337 out->st_size = in->stx_size;
338 out->st_blksize = in->stx_blksize;
339 out->st_blocks = in->stx_blocks;
340 out->st_atime = in->stx_atime.tv_sec;
341 out->st_atim.tv_nsec = in->stx_atime.tv_nsec;
342 out->st_mtime = in->stx_mtime.tv_sec;
343 out->st_mtim.tv_nsec = in->stx_mtime.tv_nsec;
344 out->st_ctime = in->stx_ctime.tv_sec;
345 out->st_ctim.tv_nsec = in->stx_ctime.tv_nsec;
346 }
347 # endif
348
349 # if SANITIZER_MIPS64 || SANITIZER_SPARC64
350 # if SANITIZER_MIPS64
351 typedef struct kernel_stat kstat_t;
352 # else
353 typedef struct kernel_stat64 kstat_t;
354 # endif
355 // Undefine compatibility macros from <sys/stat.h>
356 // so that they would not clash with the kernel_stat
357 // st_[a|m|c]time fields
358 # if !SANITIZER_GO
359 # undef st_atime
360 # undef st_mtime
361 # undef st_ctime
362 # endif
363 # if defined(SANITIZER_ANDROID)
364 // Bionic sys/stat.h defines additional macros
365 // for compatibility with the old NDKs and
366 // they clash with the kernel_stat structure
367 // st_[a|m|c]time_nsec fields.
368 # undef st_atime_nsec
369 # undef st_mtime_nsec
370 # undef st_ctime_nsec
371 # endif
372 static void kernel_stat_to_stat(kstat_t *in, struct stat *out) {
373 internal_memset(out, 0, sizeof(*out));
374 out->st_dev = in->st_dev;
375 out->st_ino = in->st_ino;
376 out->st_mode = in->st_mode;
377 out->st_nlink = in->st_nlink;
378 out->st_uid = in->st_uid;
379 out->st_gid = in->st_gid;
380 out->st_rdev = in->st_rdev;
381 out->st_size = in->st_size;
382 out->st_blksize = in->st_blksize;
383 out->st_blocks = in->st_blocks;
384 # if defined(__USE_MISC) || defined(__USE_XOPEN2K8) || \
385 defined(SANITIZER_ANDROID)
386 out->st_atim.tv_sec = in->st_atime;
387 out->st_atim.tv_nsec = in->st_atime_nsec;
388 out->st_mtim.tv_sec = in->st_mtime;
389 out->st_mtim.tv_nsec = in->st_mtime_nsec;
390 out->st_ctim.tv_sec = in->st_ctime;
391 out->st_ctim.tv_nsec = in->st_ctime_nsec;
392 # else
393 out->st_atime = in->st_atime;
394 out->st_atimensec = in->st_atime_nsec;
395 out->st_mtime = in->st_mtime;
396 out->st_mtimensec = in->st_mtime_nsec;
397 out->st_ctime = in->st_ctime;
398 out->st_atimensec = in->st_ctime_nsec;
399 # endif
400 }
401 # endif
402
403 uptr internal_stat(const char *path, void *buf) {
404 # if SANITIZER_FREEBSD
405 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf, 0);
406 # elif SANITIZER_LINUX
407 # if defined(__loongarch__)
408 struct statx bufx;
409 int res = internal_syscall(SYSCALL(statx), AT_FDCWD, (uptr)path,
410 AT_NO_AUTOMOUNT, STATX_BASIC_STATS, (uptr)&bufx);
411 statx_to_stat(&bufx, (struct stat *)buf);
412 return res;
413 # elif (SANITIZER_WORDSIZE == 64 || SANITIZER_X32 || \
414 (defined(__mips__) && _MIPS_SIM == _ABIN32)) && \
415 !SANITIZER_SPARC
416 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
417 0);
418 # elif SANITIZER_SPARC64
419 kstat_t buf64;
420 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
421 (uptr)&buf64, 0);
422 kernel_stat_to_stat(&buf64, (struct stat *)buf);
423 return res;
424 # else
425 struct stat64 buf64;
426 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
427 (uptr)&buf64, 0);
428 stat64_to_stat(&buf64, (struct stat *)buf);
429 return res;
430 # endif
431 # else
432 struct stat64 buf64;
433 int res = internal_syscall(SYSCALL(stat64), path, &buf64);
434 stat64_to_stat(&buf64, (struct stat *)buf);
435 return res;
436 # endif
437 }
438
439 uptr internal_lstat(const char *path, void *buf) {
440 # if SANITIZER_FREEBSD
441 return internal_syscall(SYSCALL(fstatat), AT_FDCWD, (uptr)path, (uptr)buf,
442 AT_SYMLINK_NOFOLLOW);
443 # elif SANITIZER_LINUX
444 # if defined(__loongarch__)
445 struct statx bufx;
446 int res = internal_syscall(SYSCALL(statx), AT_FDCWD, (uptr)path,
447 AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT,
448 STATX_BASIC_STATS, (uptr)&bufx);
449 statx_to_stat(&bufx, (struct stat *)buf);
450 return res;
451 # elif (defined(_LP64) || SANITIZER_X32 || \
452 (defined(__mips__) && _MIPS_SIM == _ABIN32)) && \
453 !SANITIZER_SPARC
454 return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path, (uptr)buf,
455 AT_SYMLINK_NOFOLLOW);
456 # elif SANITIZER_SPARC64
457 kstat_t buf64;
458 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
459 (uptr)&buf64, AT_SYMLINK_NOFOLLOW);
460 kernel_stat_to_stat(&buf64, (struct stat *)buf);
461 return res;
462 # else
463 struct stat64 buf64;
464 int res = internal_syscall(SYSCALL(fstatat64), AT_FDCWD, (uptr)path,
465 (uptr)&buf64, AT_SYMLINK_NOFOLLOW);
466 stat64_to_stat(&buf64, (struct stat *)buf);
467 return res;
468 # endif
469 # else
470 struct stat64 buf64;
471 int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
472 stat64_to_stat(&buf64, (struct stat *)buf);
473 return res;
474 # endif
475 }
476
477 uptr internal_fstat(fd_t fd, void *buf) {
478 # if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
479 # if SANITIZER_MIPS64
480 // For mips64, fstat syscall fills buffer in the format of kernel_stat
481 kstat_t kbuf;
482 int res = internal_syscall(SYSCALL(fstat), fd, &kbuf);
483 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
484 return res;
485 # elif SANITIZER_LINUX && SANITIZER_SPARC64
486 // For sparc64, fstat64 syscall fills buffer in the format of kernel_stat64
487 kstat_t kbuf;
488 int res = internal_syscall(SYSCALL(fstat64), fd, &kbuf);
489 kernel_stat_to_stat(&kbuf, (struct stat *)buf);
490 return res;
491 # elif SANITIZER_LINUX && defined(__loongarch__)
492 struct statx bufx;
493 int res = internal_syscall(SYSCALL(statx), fd, "", AT_EMPTY_PATH,
494 STATX_BASIC_STATS, (uptr)&bufx);
495 statx_to_stat(&bufx, (struct stat *)buf);
496 return res;
497 # else
498 return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
499 # endif
500 # else
501 struct stat64 buf64;
502 int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
503 stat64_to_stat(&buf64, (struct stat *)buf);
504 return res;
505 # endif
506 }
507
508 uptr internal_filesize(fd_t fd) {
509 struct stat st;
510 if (internal_fstat(fd, &st))
511 return -1;
512 return (uptr)st.st_size;
513 }
514
515 uptr internal_dup(int oldfd) { return internal_syscall(SYSCALL(dup), oldfd); }
516
517 uptr internal_dup2(int oldfd, int newfd) {
518 # if SANITIZER_LINUX
519 return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
520 # else
521 return internal_syscall(SYSCALL(dup2), oldfd, newfd);
522 # endif
523 }
524
525 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
526 # if SANITIZER_LINUX
527 return internal_syscall(SYSCALL(readlinkat), AT_FDCWD, (uptr)path, (uptr)buf,
528 bufsize);
529 # else
530 return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
531 # endif
532 }
533
534 uptr internal_unlink(const char *path) {
535 # if SANITIZER_LINUX
536 return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
537 # else
538 return internal_syscall(SYSCALL(unlink), (uptr)path);
539 # endif
540 }
541
542 uptr internal_rename(const char *oldpath, const char *newpath) {
543 # if (defined(__riscv) || defined(__loongarch__)) && defined(__linux__)
544 return internal_syscall(SYSCALL(renameat2), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
545 (uptr)newpath, 0);
546 # elif SANITIZER_LINUX
547 return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
548 (uptr)newpath);
549 # else
550 return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
551 # endif
552 }
553
554 uptr internal_sched_yield() { return internal_syscall(SYSCALL(sched_yield)); }
555
556 void internal_usleep(u64 useconds) {
557 struct timespec ts;
558 ts.tv_sec = useconds / 1000000;
559 ts.tv_nsec = (useconds % 1000000) * 1000;
560 internal_syscall(SYSCALL(nanosleep), &ts, &ts);
561 }
562
563 uptr internal_execve(const char *filename, char *const argv[],
564 char *const envp[]) {
565 return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv,
566 (uptr)envp);
567 }
568 # endif // !SANITIZER_SOLARIS && !SANITIZER_NETBSD
569
570 # if !SANITIZER_NETBSD
571 void internal__exit(int exitcode) {
572 # if SANITIZER_FREEBSD || SANITIZER_SOLARIS
573 internal_syscall(SYSCALL(exit), exitcode);
574 # else
575 internal_syscall(SYSCALL(exit_group), exitcode);
576 # endif
577 Die(); // Unreachable.
578 }
579 # endif // !SANITIZER_NETBSD
580
581 // ----------------- sanitizer_common.h
582 bool FileExists(const char *filename) {
583 if (ShouldMockFailureToOpen(filename))
584 return false;
585 struct stat st;
586 if (internal_stat(filename, &st))
587 return false;
588 // Sanity check: filename is a regular file.
589 return S_ISREG(st.st_mode);
590 }
591
592 bool DirExists(const char *path) {
593 struct stat st;
594 if (internal_stat(path, &st))
595 return false;
596 return S_ISDIR(st.st_mode);
597 }
598
599 # if !SANITIZER_NETBSD
600 tid_t GetTid() {
601 # if SANITIZER_FREEBSD
602 long Tid;
603 thr_self(&Tid);
604 return Tid;
605 # elif SANITIZER_SOLARIS
606 return thr_self();
607 # else
608 return internal_syscall(SYSCALL(gettid));
609 # endif
610 }
611
612 int TgKill(pid_t pid, tid_t tid, int sig) {
613 # if SANITIZER_LINUX
614 return internal_syscall(SYSCALL(tgkill), pid, tid, sig);
615 # elif SANITIZER_FREEBSD
616 return internal_syscall(SYSCALL(thr_kill2), pid, tid, sig);
617 # elif SANITIZER_SOLARIS
618 (void)pid;
619 errno = thr_kill(tid, sig);
620 // TgKill is expected to return -1 on error, not an errno.
621 return errno != 0 ? -1 : 0;
622 # endif
623 }
624 # endif
625
626 # if SANITIZER_GLIBC
627 u64 NanoTime() {
628 kernel_timeval tv;
629 internal_memset(&tv, 0, sizeof(tv));
630 internal_syscall(SYSCALL(gettimeofday), &tv, 0);
631 return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
632 }
633 // Used by real_clock_gettime.
634 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
635 return internal_syscall(SYSCALL(clock_gettime), clk_id, tp);
636 }
637 # elif !SANITIZER_SOLARIS && !SANITIZER_NETBSD
638 u64 NanoTime() {
639 struct timespec ts;
640 clock_gettime(CLOCK_REALTIME, &ts);
641 return (u64)ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
642 }
643 # endif
644
645 // Like getenv, but reads env directly from /proc (on Linux) or parses the
646 // 'environ' array (on some others) and does not use libc. This function
647 // should be called first inside __asan_init.
648 const char *GetEnv(const char *name) {
649 # if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_SOLARIS
650 if (::environ != 0) {
651 uptr NameLen = internal_strlen(name);
652 for (char **Env = ::environ; *Env != 0; Env++) {
653 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
654 return (*Env) + NameLen + 1;
655 }
656 }
657 return 0; // Not found.
658 # elif SANITIZER_LINUX
659 static char *environ;
660 static uptr len;
661 static bool inited;
662 if (!inited) {
663 inited = true;
664 uptr environ_size;
665 if (!ReadFileToBuffer("/proc/self/environ", &environ, &environ_size, &len))
666 environ = nullptr;
667 }
668 if (!environ || len == 0)
669 return nullptr;
670 uptr namelen = internal_strlen(name);
671 const char *p = environ;
672 while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
673 // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
674 const char *endp = (char *)internal_memchr(p, '\0', len - (p - environ));
675 if (!endp) // this entry isn't NUL terminated
676 return nullptr;
677 else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
678 return p + namelen + 1; // point after =
679 p = endp + 1;
680 }
681 return nullptr; // Not found.
682 # else
683 # error "Unsupported platform"
684 # endif
685 }
686
687 # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD && !SANITIZER_GO
688 extern "C" {
689 SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
690 }
691 # endif
692
693 # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
694 static void ReadNullSepFileToArray(const char *path, char ***arr,
695 int arr_size) {
696 char *buff;
697 uptr buff_size;
698 uptr buff_len;
699 *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
700 if (!ReadFileToBuffer(path, &buff, &buff_size, &buff_len, 1024 * 1024)) {
701 (*arr)[0] = nullptr;
702 return;
703 }
704 (*arr)[0] = buff;
705 int count, i;
706 for (count = 1, i = 1;; i++) {
707 if (buff[i] == 0) {
708 if (buff[i + 1] == 0)
709 break;
710 (*arr)[count] = &buff[i + 1];
711 CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
712 count++;
713 }
714 }
715 (*arr)[count] = nullptr;
716 }
717 # endif
718
719 static void GetArgsAndEnv(char ***argv, char ***envp) {
720 # if SANITIZER_FREEBSD
721 // On FreeBSD, retrieving the argument and environment arrays is done via the
722 // kern.ps_strings sysctl, which returns a pointer to a structure containing
723 // this information. See also <sys/exec.h>.
724 ps_strings *pss;
725 uptr sz = sizeof(pss);
726 if (internal_sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1) {
727 Printf("sysctl kern.ps_strings failed\n");
728 Die();
729 }
730 *argv = pss->ps_argvstr;
731 *envp = pss->ps_envstr;
732 # elif SANITIZER_NETBSD
733 *argv = __ps_strings->ps_argvstr;
734 *envp = __ps_strings->ps_envstr;
735 # else // SANITIZER_FREEBSD
736 # if !SANITIZER_GO
737 if (&__libc_stack_end) {
738 uptr *stack_end = (uptr *)__libc_stack_end;
739 // Linux/sparc64 needs an adjustment, cf. glibc
740 // sysdeps/sparc/sparc{32,64}/dl-machine.h (DL_STACK_END).
741 # if SANITIZER_LINUX && defined(__sparc__)
742 stack_end = &stack_end[16];
743 # endif
744 // Normally argc can be obtained from *stack_end, however, on ARM glibc's
745 // _start clobbers it:
746 // https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/arm/start.S;hb=refs/heads/release/2.31/master#l75
747 // Do not special-case ARM and infer argc from argv everywhere.
748 int argc = 0;
749 while (stack_end[argc + 1]) argc++;
750 *argv = (char **)(stack_end + 1);
751 *envp = (char **)(stack_end + argc + 2);
752 } else {
753 # endif // !SANITIZER_GO
754 static const int kMaxArgv = 2000, kMaxEnvp = 2000;
755 ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
756 ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
757 # if !SANITIZER_GO
758 }
759 # endif // !SANITIZER_GO
760 # endif // SANITIZER_FREEBSD
761 }
762
763 char **GetArgv() {
764 char **argv, **envp;
765 GetArgsAndEnv(&argv, &envp);
766 return argv;
767 }
768
769 char **GetEnviron() {
770 char **argv, **envp;
771 GetArgsAndEnv(&argv, &envp);
772 return envp;
773 }
774
775 # if !SANITIZER_SOLARIS
776 void FutexWait(atomic_uint32_t *p, u32 cmp) {
777 # if SANITIZER_FREEBSD
778 _umtx_op(p, UMTX_OP_WAIT_UINT, cmp, 0, 0);
779 # elif SANITIZER_NETBSD
780 sched_yield(); /* No userspace futex-like synchronization */
781 # else
782 internal_syscall(SYSCALL(futex), (uptr)p, FUTEX_WAIT_PRIVATE, cmp, 0, 0, 0);
783 # endif
784 }
785
786 void FutexWake(atomic_uint32_t *p, u32 count) {
787 # if SANITIZER_FREEBSD
788 _umtx_op(p, UMTX_OP_WAKE, count, 0, 0);
789 # elif SANITIZER_NETBSD
790 /* No userspace futex-like synchronization */
791 # else
792 internal_syscall(SYSCALL(futex), (uptr)p, FUTEX_WAKE_PRIVATE, count, 0, 0, 0);
793 # endif
794 }
795
796 # endif // !SANITIZER_SOLARIS
797
798 // ----------------- sanitizer_linux.h
799 // The actual size of this structure is specified by d_reclen.
800 // Note that getdents64 uses a different structure format. We only provide the
801 // 32-bit syscall here.
802 # if SANITIZER_NETBSD
803 // Not used
804 # else
805 struct linux_dirent {
806 # if SANITIZER_X32 || SANITIZER_LINUX
807 u64 d_ino;
808 u64 d_off;
809 # else
810 unsigned long d_ino;
811 unsigned long d_off;
812 # endif
813 unsigned short d_reclen;
814 # if SANITIZER_LINUX
815 unsigned char d_type;
816 # endif
817 char d_name[256];
818 };
819 # endif
820
821 # if !SANITIZER_SOLARIS && !SANITIZER_NETBSD
822 // Syscall wrappers.
823 uptr internal_ptrace(int request, int pid, void *addr, void *data) {
824 return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
825 (uptr)data);
826 }
827
828 uptr internal_waitpid(int pid, int *status, int options) {
829 return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
830 0 /* rusage */);
831 }
832
833 uptr internal_getpid() { return internal_syscall(SYSCALL(getpid)); }
834
835 uptr internal_getppid() { return internal_syscall(SYSCALL(getppid)); }
836
837 int internal_dlinfo(void *handle, int request, void *p) {
838 # if SANITIZER_FREEBSD
839 return dlinfo(handle, request, p);
840 # else
841 UNIMPLEMENTED();
842 # endif
843 }
844
845 uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
846 # if SANITIZER_FREEBSD
847 return internal_syscall(SYSCALL(getdirentries), fd, (uptr)dirp, count, NULL);
848 # elif SANITIZER_LINUX
849 return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
850 # else
851 return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
852 # endif
853 }
854
855 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
856 return internal_syscall(SYSCALL(lseek), fd, offset, whence);
857 }
858
859 # if SANITIZER_LINUX
860 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
861 return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
862 }
863 # if defined(__x86_64__)
864 # include <asm/unistd_64.h>
865 // Currently internal_arch_prctl() is only needed on x86_64.
866 uptr internal_arch_prctl(int option, uptr arg2) {
867 return internal_syscall(__NR_arch_prctl, option, arg2);
868 }
869 # endif
870 # endif
871
872 uptr internal_sigaltstack(const void *ss, void *oss) {
873 return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
874 }
875
876 extern "C" pid_t __fork(void);
877
878 int internal_fork() {
879 # if SANITIZER_LINUX
880 # if SANITIZER_S390
881 return internal_syscall(SYSCALL(clone), 0, SIGCHLD);
882 # elif SANITIZER_SPARC
883 // The clone syscall interface on SPARC differs massively from the rest,
884 // so fall back to __fork.
885 return __fork();
886 # else
887 return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
888 # endif
889 # else
890 return internal_syscall(SYSCALL(fork));
891 # endif
892 }
893
894 # if SANITIZER_FREEBSD
895 int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
896 uptr *oldlenp, const void *newp, uptr newlen) {
897 return internal_syscall(SYSCALL(__sysctl), name, namelen, oldp,
898 (size_t *)oldlenp, newp, (size_t)newlen);
899 }
900
901 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
902 const void *newp, uptr newlen) {
903 // Note: this function can be called during startup, so we need to avoid
904 // calling any interceptable functions. On FreeBSD >= 1300045 sysctlbyname()
905 // is a real syscall, but for older versions it calls sysctlnametomib()
906 // followed by sysctl(). To avoid calling the intercepted version and
907 // asserting if this happens during startup, call the real sysctlnametomib()
908 // followed by internal_sysctl() if the syscall is not available.
909 # ifdef SYS___sysctlbyname
910 return internal_syscall(SYSCALL(__sysctlbyname), sname,
911 internal_strlen(sname), oldp, (size_t *)oldlenp, newp,
912 (size_t)newlen);
913 # else
914 static decltype(sysctlnametomib) *real_sysctlnametomib = nullptr;
915 if (!real_sysctlnametomib)
916 real_sysctlnametomib =
917 (decltype(sysctlnametomib) *)dlsym(RTLD_NEXT, "sysctlnametomib");
918 CHECK(real_sysctlnametomib);
919
920 int oid[CTL_MAXNAME];
921 size_t len = CTL_MAXNAME;
922 if (real_sysctlnametomib(sname, oid, &len) == -1)
923 return (-1);
924 return internal_sysctl(oid, len, oldp, oldlenp, newp, newlen);
925 # endif
926 }
927 # endif
928
929 # if SANITIZER_LINUX
930 # define SA_RESTORER 0x04000000
931 // Doesn't set sa_restorer if the caller did not set it, so use with caution
932 //(see below).
933 int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
934 __sanitizer_kernel_sigaction_t k_act, k_oldact;
935 internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
936 internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
937 const __sanitizer_sigaction *u_act = (const __sanitizer_sigaction *)act;
938 __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
939 if (u_act) {
940 k_act.handler = u_act->handler;
941 k_act.sigaction = u_act->sigaction;
942 internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
943 sizeof(__sanitizer_kernel_sigset_t));
944 // Without SA_RESTORER kernel ignores the calls (probably returns EINVAL).
945 k_act.sa_flags = u_act->sa_flags | SA_RESTORER;
946 // FIXME: most often sa_restorer is unset, however the kernel requires it
947 // to point to a valid signal restorer that calls the rt_sigreturn syscall.
948 // If sa_restorer passed to the kernel is NULL, the program may crash upon
949 // signal delivery or fail to unwind the stack in the signal handler.
950 // libc implementation of sigaction() passes its own restorer to
951 // rt_sigaction, so we need to do the same (we'll need to reimplement the
952 // restorers; for x86_64 the restorer address can be obtained from
953 // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
954 # if !SANITIZER_ANDROID || !SANITIZER_MIPS32
955 k_act.sa_restorer = u_act->sa_restorer;
956 # endif
957 }
958
959 uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
960 (uptr)(u_act ? &k_act : nullptr),
961 (uptr)(u_oldact ? &k_oldact : nullptr),
962 (uptr)sizeof(__sanitizer_kernel_sigset_t));
963
964 if ((result == 0) && u_oldact) {
965 u_oldact->handler = k_oldact.handler;
966 u_oldact->sigaction = k_oldact.sigaction;
967 internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
968 sizeof(__sanitizer_kernel_sigset_t));
969 u_oldact->sa_flags = k_oldact.sa_flags;
970 # if !SANITIZER_ANDROID || !SANITIZER_MIPS32
971 u_oldact->sa_restorer = k_oldact.sa_restorer;
972 # endif
973 }
974 return result;
975 }
976 # endif // SANITIZER_LINUX
977
978 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
979 __sanitizer_sigset_t *oldset) {
980 # if SANITIZER_FREEBSD
981 return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
982 # else
983 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
984 __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
985 return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how, (uptr)k_set,
986 (uptr)k_oldset, sizeof(__sanitizer_kernel_sigset_t));
987 # endif
988 }
989
990 void internal_sigfillset(__sanitizer_sigset_t *set) {
991 internal_memset(set, 0xff, sizeof(*set));
992 }
993
994 void internal_sigemptyset(__sanitizer_sigset_t *set) {
995 internal_memset(set, 0, sizeof(*set));
996 }
997
998 # if SANITIZER_LINUX
999 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
1000 signum -= 1;
1001 CHECK_GE(signum, 0);
1002 CHECK_LT(signum, sizeof(*set) * 8);
1003 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
1004 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
1005 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
1006 k_set->sig[idx] &= ~((uptr)1 << bit);
1007 }
1008
1009 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
1010 signum -= 1;
1011 CHECK_GE(signum, 0);
1012 CHECK_LT(signum, sizeof(*set) * 8);
1013 __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
1014 const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
1015 const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
1016 return k_set->sig[idx] & ((uptr)1 << bit);
1017 }
1018 # elif SANITIZER_FREEBSD
1019 uptr internal_procctl(int type, int id, int cmd, void *data) {
1020 return internal_syscall(SYSCALL(procctl), type, id, cmd, data);
1021 }
1022
1023 void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
1024 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
1025 sigdelset(rset, signum);
1026 }
1027
1028 bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
1029 sigset_t *rset = reinterpret_cast<sigset_t *>(set);
1030 return sigismember(rset, signum);
1031 }
1032 # endif
1033 # endif // !SANITIZER_SOLARIS
1034
1035 # if !SANITIZER_NETBSD
1036 // ThreadLister implementation.
1037 ThreadLister::ThreadLister(pid_t pid) : buffer_(4096) {
1038 task_path_.AppendF("/proc/%d/task", pid);
1039 }
1040
1041 ThreadLister::Result ThreadLister::ListThreads(
1042 InternalMmapVector<tid_t> *threads) {
1043 int descriptor = internal_open(task_path_.data(), O_RDONLY | O_DIRECTORY);
1044 if (internal_iserror(descriptor)) {
1045 Report("Can't open %s for reading.\n", task_path_.data());
1046 return Error;
1047 }
1048 auto cleanup = at_scope_exit([&] { internal_close(descriptor); });
1049 threads->clear();
1050
1051 Result result = Ok;
1052 for (bool first_read = true;; first_read = false) {
1053 CHECK_GE(buffer_.size(), 4096);
1054 uptr read = internal_getdents(
1055 descriptor, (struct linux_dirent *)buffer_.data(), buffer_.size());
1056 if (!read)
1057 return result;
1058 if (internal_iserror(read)) {
1059 Report("Can't read directory entries from %s.\n", task_path_.data());
1060 return Error;
1061 }
1062
1063 for (uptr begin = (uptr)buffer_.data(), end = begin + read; begin < end;) {
1064 struct linux_dirent *entry = (struct linux_dirent *)begin;
1065 begin += entry->d_reclen;
1066 if (entry->d_ino == 1) {
1067 // Inode 1 is for bad blocks and also can be a reason for early return.
1068 // Should be emitted if kernel tried to output terminating thread.
1069 // See proc_task_readdir implementation in Linux.
1070 result = Incomplete;
1071 }
1072 if (entry->d_ino && *entry->d_name >= '0' && *entry->d_name <= '9')
1073 threads->push_back(internal_atoll(entry->d_name));
1074 }
1075
1076 // Now we are going to detect short-read or early EOF. In such cases Linux
1077 // can return inconsistent list with missing alive threads.
1078 // Code will just remember that the list can be incomplete but it will
1079 // continue reads to return as much as possible.
1080 if (!first_read) {
1081 // The first one was a short-read by definition.
1082 result = Incomplete;
1083 } else if (read > buffer_.size() - 1024) {
1084 // Read was close to the buffer size. So double the size and assume the
1085 // worst.
1086 buffer_.resize(buffer_.size() * 2);
1087 result = Incomplete;
1088 } else if (!threads->empty() && !IsAlive(threads->back())) {
1089 // Maybe Linux early returned from read on terminated thread (!pid_alive)
1090 // and failed to restore read position.
1091 // See next_tid and proc_task_instantiate in Linux.
1092 result = Incomplete;
1093 }
1094 }
1095 }
1096
1097 const char *ThreadLister::LoadStatus(tid_t tid) {
1098 status_path_.clear();
1099 status_path_.AppendF("%s/%llu/status", task_path_.data(), tid);
1100 auto cleanup = at_scope_exit([&] {
1101 // Resize back to capacity if it is downsized by `ReadFileToVector`.
1102 buffer_.resize(buffer_.capacity());
1103 });
1104 if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
1105 return nullptr;
1106 buffer_.push_back('\0');
1107 return buffer_.data();
1108 }
1109
1110 bool ThreadLister::IsAlive(tid_t tid) {
1111 // /proc/%d/task/%d/status uses same call to detect alive threads as
1112 // proc_task_readdir. See task_state implementation in Linux.
1113 static const char kPrefix[] = "\nPPid:";
1114 const char *status = LoadStatus(tid);
1115 if (!status)
1116 return false;
1117 const char *field = internal_strstr(status, kPrefix);
1118 if (!field)
1119 return false;
1120 field += internal_strlen(kPrefix);
1121 return (int)internal_atoll(field) != 0;
1122 }
1123
1124 # endif
1125
1126 # if SANITIZER_WORDSIZE == 32
1127 // Take care of unusable kernel area in top gigabyte.
1128 static uptr GetKernelAreaSize() {
1129 # if SANITIZER_LINUX && !SANITIZER_X32
1130 const uptr gbyte = 1UL << 30;
1131
1132 // Firstly check if there are writable segments
1133 // mapped to top gigabyte (e.g. stack).
1134 MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
1135 if (proc_maps.Error())
1136 return 0;
1137 MemoryMappedSegment segment;
1138 while (proc_maps.Next(&segment)) {
1139 if ((segment.end >= 3 * gbyte) && segment.IsWritable())
1140 return 0;
1141 }
1142
1143 # if !SANITIZER_ANDROID
1144 // Even if nothing is mapped, top Gb may still be accessible
1145 // if we are running on 64-bit kernel.
1146 // Uname may report misleading results if personality type
1147 // is modified (e.g. under schroot) so check this as well.
1148 struct utsname uname_info;
1149 int pers = personality(0xffffffffUL);
1150 if (!(pers & PER_MASK) && internal_uname(&uname_info) == 0 &&
1151 internal_strstr(uname_info.machine, "64"))
1152 return 0;
1153 # endif // SANITIZER_ANDROID
1154
1155 // Top gigabyte is reserved for kernel.
1156 return gbyte;
1157 # else
1158 return 0;
1159 # endif // SANITIZER_LINUX && !SANITIZER_X32
1160 }
1161 # endif // SANITIZER_WORDSIZE == 32
1162
1163 uptr GetMaxVirtualAddress() {
1164 # if SANITIZER_NETBSD && defined(__x86_64__)
1165 return 0x7f7ffffff000ULL; // (0x00007f8000000000 - PAGE_SIZE)
1166 # elif SANITIZER_WORDSIZE == 64
1167 # if defined(__powerpc64__) || defined(__aarch64__) || \
1168 defined(__loongarch__) || SANITIZER_RISCV64
1169 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
1170 // We somehow need to figure out which one we are using now and choose
1171 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
1172 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
1173 // of the address space, so simply checking the stack address is not enough.
1174 // This should (does) work for both PowerPC64 Endian modes.
1175 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
1176 // loongarch64 also has multiple address space layouts: default is 47-bit.
1177 // RISC-V 64 also has multiple address space layouts: 39, 48 and 57-bit.
1178 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
1179 # elif SANITIZER_MIPS64
1180 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
1181 # elif defined(__s390x__)
1182 return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
1183 # elif defined(__sparc__)
1184 return ~(uptr)0;
1185 # else
1186 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
1187 # endif
1188 # else // SANITIZER_WORDSIZE == 32
1189 # if defined(__s390__)
1190 return (1ULL << 31) - 1; // 0x7fffffff;
1191 # else
1192 return (1ULL << 32) - 1; // 0xffffffff;
1193 # endif
1194 # endif // SANITIZER_WORDSIZE
1195 }
1196
1197 uptr GetMaxUserVirtualAddress() {
1198 uptr addr = GetMaxVirtualAddress();
1199 # if SANITIZER_WORDSIZE == 32 && !defined(__s390__)
1200 if (!common_flags()->full_address_space)
1201 addr -= GetKernelAreaSize();
1202 CHECK_LT(reinterpret_cast<uptr>(&addr), addr);
1203 # endif
1204 return addr;
1205 }
1206
1207 # if !SANITIZER_ANDROID || defined(__aarch64__)
1208 uptr GetPageSize() {
1209 # if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) && \
1210 defined(EXEC_PAGESIZE)
1211 return EXEC_PAGESIZE;
1212 # elif SANITIZER_FREEBSD || SANITIZER_NETBSD
1213 // Use sysctl as sysconf can trigger interceptors internally.
1214 int pz = 0;
1215 uptr pzl = sizeof(pz);
1216 int mib[2] = {CTL_HW, HW_PAGESIZE};
1217 int rv = internal_sysctl(mib, 2, &pz, &pzl, nullptr, 0);
1218 CHECK_EQ(rv, 0);
1219 return (uptr)pz;
1220 # elif SANITIZER_USE_GETAUXVAL
1221 return getauxval(AT_PAGESZ);
1222 # else
1223 return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
1224 # endif
1225 }
1226 # endif
1227
1228 uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
1229 # if SANITIZER_SOLARIS
1230 const char *default_module_name = getexecname();
1231 CHECK_NE(default_module_name, NULL);
1232 return internal_snprintf(buf, buf_len, "%s", default_module_name);
1233 # else
1234 # if SANITIZER_FREEBSD || SANITIZER_NETBSD
1235 # if SANITIZER_FREEBSD
1236 const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
1237 # else
1238 const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
1239 # endif
1240 const char *default_module_name = "kern.proc.pathname";
1241 uptr Size = buf_len;
1242 bool IsErr =
1243 (internal_sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
1244 int readlink_error = IsErr ? errno : 0;
1245 uptr module_name_len = Size;
1246 # else
1247 const char *default_module_name = "/proc/self/exe";
1248 uptr module_name_len = internal_readlink(default_module_name, buf, buf_len);
1249 int readlink_error;
1250 bool IsErr = internal_iserror(module_name_len, &readlink_error);
1251 # endif
1252 if (IsErr) {
1253 // We can't read binary name for some reason, assume it's unknown.
1254 Report(
1255 "WARNING: reading executable name failed with errno %d, "
1256 "some stack frames may not be symbolized\n",
1257 readlink_error);
1258 module_name_len =
1259 internal_snprintf(buf, buf_len, "%s", default_module_name);
1260 CHECK_LT(module_name_len, buf_len);
1261 }
1262 return module_name_len;
1263 # endif
1264 }
1265
1266 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
1267 # if SANITIZER_LINUX
1268 char *tmpbuf;
1269 uptr tmpsize;
1270 uptr tmplen;
1271 if (ReadFileToBuffer("/proc/self/cmdline", &tmpbuf, &tmpsize, &tmplen,
1272 1024 * 1024)) {
1273 internal_strncpy(buf, tmpbuf, buf_len);
1274 UnmapOrDie(tmpbuf, tmpsize);
1275 return internal_strlen(buf);
1276 }
1277 # endif
1278 return ReadBinaryName(buf, buf_len);
1279 }
1280
1281 // Match full names of the form /path/to/base_name{-,.}*
1282 bool LibraryNameIs(const char *full_name, const char *base_name) {
1283 const char *name = full_name;
1284 // Strip path.
1285 while (*name != '\0') name++;
1286 while (name > full_name && *name != '/') name--;
1287 if (*name == '/')
1288 name++;
1289 uptr base_name_length = internal_strlen(base_name);
1290 if (internal_strncmp(name, base_name, base_name_length))
1291 return false;
1292 return (name[base_name_length] == '-' || name[base_name_length] == '.');
1293 }
1294
1295 # if !SANITIZER_ANDROID
1296 // Call cb for each region mapped by map.
1297 void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
1298 CHECK_NE(map, nullptr);
1299 # if !SANITIZER_FREEBSD
1300 typedef ElfW(Phdr) Elf_Phdr;
1301 typedef ElfW(Ehdr) Elf_Ehdr;
1302 # endif // !SANITIZER_FREEBSD
1303 char *base = (char *)map->l_addr;
1304 Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
1305 char *phdrs = base + ehdr->e_phoff;
1306 char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
1307
1308 // Find the segment with the minimum base so we can "relocate" the p_vaddr
1309 // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
1310 // objects have a non-zero base.
1311 uptr preferred_base = (uptr)-1;
1312 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1313 Elf_Phdr *phdr = (Elf_Phdr *)iter;
1314 if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
1315 preferred_base = (uptr)phdr->p_vaddr;
1316 }
1317
1318 // Compute the delta from the real base to get a relocation delta.
1319 sptr delta = (uptr)base - preferred_base;
1320 // Now we can figure out what the loader really mapped.
1321 for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
1322 Elf_Phdr *phdr = (Elf_Phdr *)iter;
1323 if (phdr->p_type == PT_LOAD) {
1324 uptr seg_start = phdr->p_vaddr + delta;
1325 uptr seg_end = seg_start + phdr->p_memsz;
1326 // None of these values are aligned. We consider the ragged edges of the
1327 // load command as defined, since they are mapped from the file.
1328 seg_start = RoundDownTo(seg_start, GetPageSizeCached());
1329 seg_end = RoundUpTo(seg_end, GetPageSizeCached());
1330 cb((void *)seg_start, seg_end - seg_start);
1331 }
1332 }
1333 }
1334 # endif
1335
1336 # if SANITIZER_LINUX
1337 # if defined(__x86_64__)
1338 // We cannot use glibc's clone wrapper, because it messes with the child
1339 // task's TLS. It writes the PID and TID of the child task to its thread
1340 // descriptor, but in our case the child task shares the thread descriptor with
1341 // the parent (because we don't know how to allocate a new thread
1342 // descriptor to keep glibc happy). So the stock version of clone(), when
1343 // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
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;
1353 register void *r8 __asm__("r8") = newtls;
1354 register int *r10 __asm__("r10") = child_tidptr;
1355 __asm__ __volatile__(
1356 /* %rax = syscall(%rax = SYSCALL(clone),
1357 * %rdi = flags,
1358 * %rsi = child_stack,
1359 * %rdx = parent_tidptr,
1360 * %r8 = new_tls,
1361 * %r10 = child_tidptr)
1362 */
1363 "syscall\n"
1364
1365 /* if (%rax != 0)
1366 * return;
1367 */
1368 "testq %%rax,%%rax\n"
1369 "jnz 1f\n"
1370
1371 /* In the child. Terminate unwind chain. */
1372 // XXX: We should also terminate the CFI unwind chain
1373 // here. Unfortunately clang 3.2 doesn't support the
1374 // necessary CFI directives, so we skip that part.
1375 "xorq %%rbp,%%rbp\n"
1376
1377 /* Call "fn(arg)". */
1378 "popq %%rax\n"
1379 "popq %%rdi\n"
1380 "call *%%rax\n"
1381
1382 /* Call _exit(%rax). */
1383 "movq %%rax,%%rdi\n"
1384 "movq %2,%%rax\n"
1385 "syscall\n"
1386
1387 /* Return to parent. */
1388 "1:\n"
1389 : "=a"(res)
1390 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), "S"(child_stack), "D"(flags),
1391 "d"(parent_tidptr), "r"(r8), "r"(r10)
1392 : "memory", "r11", "rcx");
1393 return res;
1394 }
1395 # elif defined(__mips__)
1396 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1397 int *parent_tidptr, void *newtls, int *child_tidptr) {
1398 long long res;
1399 if (!fn || !child_stack)
1400 return -EINVAL;
1401 CHECK_EQ(0, (uptr)child_stack % 16);
1402 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1403 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1404 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1405 register void *a3 __asm__("$7") = newtls;
1406 register int *a4 __asm__("$8") = child_tidptr;
1407 // We don't have proper CFI directives here because it requires alot of code
1408 // for very marginal benefits.
1409 __asm__ __volatile__(
1410 /* $v0 = syscall($v0 = __NR_clone,
1411 * $a0 = flags,
1412 * $a1 = child_stack,
1413 * $a2 = parent_tidptr,
1414 * $a3 = new_tls,
1415 * $a4 = child_tidptr)
1416 */
1417 ".cprestore 16;\n"
1418 "move $4,%1;\n"
1419 "move $5,%2;\n"
1420 "move $6,%3;\n"
1421 "move $7,%4;\n"
1422 /* Store the fifth argument on stack
1423 * if we are using 32-bit abi.
1424 */
1425 # if SANITIZER_WORDSIZE == 32
1426 "lw %5,16($29);\n"
1427 # else
1428 "move $8,%5;\n"
1429 # endif
1430 "li $2,%6;\n"
1431 "syscall;\n"
1432
1433 /* if ($v0 != 0)
1434 * return;
1435 */
1436 "bnez $2,1f;\n"
1437
1438 /* Call "fn(arg)". */
1439 # if SANITIZER_WORDSIZE == 32
1440 # ifdef __BIG_ENDIAN__
1441 "lw $25,4($29);\n"
1442 "lw $4,12($29);\n"
1443 # else
1444 "lw $25,0($29);\n"
1445 "lw $4,8($29);\n"
1446 # endif
1447 # else
1448 "ld $25,0($29);\n"
1449 "ld $4,8($29);\n"
1450 # endif
1451 "jal $25;\n"
1452
1453 /* Call _exit($v0). */
1454 "move $4,$2;\n"
1455 "li $2,%7;\n"
1456 "syscall;\n"
1457
1458 /* Return to parent. */
1459 "1:\n"
1460 : "=r"(res)
1461 : "r"(flags), "r"(child_stack), "r"(parent_tidptr), "r"(a3), "r"(a4),
1462 "i"(__NR_clone), "i"(__NR_exit)
1463 : "memory", "$29");
1464 return res;
1465 }
1466 # elif SANITIZER_RISCV64
1467 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1468 int *parent_tidptr, void *newtls, int *child_tidptr) {
1469 if (!fn || !child_stack)
1470 return -EINVAL;
1471
1472 CHECK_EQ(0, (uptr)child_stack % 16);
1473
1474 register int res __asm__("a0");
1475 register int __flags __asm__("a0") = flags;
1476 register void *__stack __asm__("a1") = child_stack;
1477 register int *__ptid __asm__("a2") = parent_tidptr;
1478 register void *__tls __asm__("a3") = newtls;
1479 register int *__ctid __asm__("a4") = child_tidptr;
1480 register int (*__fn)(void *) __asm__("a5") = fn;
1481 register void *__arg __asm__("a6") = arg;
1482 register int nr_clone __asm__("a7") = __NR_clone;
1483
1484 __asm__ __volatile__(
1485 "ecall\n"
1486
1487 /* if (a0 != 0)
1488 * return a0;
1489 */
1490 "bnez a0, 1f\n"
1491
1492 // In the child, now. Call "fn(arg)".
1493 "mv a0, a6\n"
1494 "jalr a5\n"
1495
1496 // Call _exit(a0).
1497 "addi a7, zero, %9\n"
1498 "ecall\n"
1499 "1:\n"
1500
1501 : "=r"(res)
1502 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__tls), "r"(__ctid),
1503 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit)
1504 : "memory");
1505 return res;
1506 }
1507 # elif defined(__aarch64__)
1508 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1509 int *parent_tidptr, void *newtls, int *child_tidptr) {
1510 register long long res __asm__("x0");
1511 if (!fn || !child_stack)
1512 return -EINVAL;
1513 CHECK_EQ(0, (uptr)child_stack % 16);
1514 child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
1515 ((unsigned long long *)child_stack)[0] = (uptr)fn;
1516 ((unsigned long long *)child_stack)[1] = (uptr)arg;
1517
1518 register int (*__fn)(void *) __asm__("x0") = fn;
1519 register void *__stack __asm__("x1") = child_stack;
1520 register int __flags __asm__("x2") = flags;
1521 register void *__arg __asm__("x3") = arg;
1522 register int *__ptid __asm__("x4") = parent_tidptr;
1523 register void *__tls __asm__("x5") = newtls;
1524 register int *__ctid __asm__("x6") = child_tidptr;
1525
1526 __asm__ __volatile__(
1527 "mov x0,x2\n" /* flags */
1528 "mov x2,x4\n" /* ptid */
1529 "mov x3,x5\n" /* tls */
1530 "mov x4,x6\n" /* ctid */
1531 "mov x8,%9\n" /* clone */
1532
1533 "svc 0x0\n"
1534
1535 /* if (%r0 != 0)
1536 * return %r0;
1537 */
1538 "cmp x0, #0\n"
1539 "bne 1f\n"
1540
1541 /* In the child, now. Call "fn(arg)". */
1542 "ldp x1, x0, [sp], #16\n"
1543 "blr x1\n"
1544
1545 /* Call _exit(%r0). */
1546 "mov x8, %10\n"
1547 "svc 0x0\n"
1548 "1:\n"
1549
1550 : "=r"(res)
1551 : "i"(-EINVAL), "r"(__fn), "r"(__stack), "r"(__flags), "r"(__arg),
1552 "r"(__ptid), "r"(__tls), "r"(__ctid), "i"(__NR_clone), "i"(__NR_exit)
1553 : "x30", "memory");
1554 return res;
1555 }
1556 # elif SANITIZER_LOONGARCH64
1557 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1558 int *parent_tidptr, void *newtls, int *child_tidptr) {
1559 if (!fn || !child_stack)
1560 return -EINVAL;
1561
1562 CHECK_EQ(0, (uptr)child_stack % 16);
1563
1564 register int res __asm__("$a0");
1565 register int __flags __asm__("$a0") = flags;
1566 register void *__stack __asm__("$a1") = child_stack;
1567 register int *__ptid __asm__("$a2") = parent_tidptr;
1568 register int *__ctid __asm__("$a3") = child_tidptr;
1569 register void *__tls __asm__("$a4") = newtls;
1570 register int (*__fn)(void *) __asm__("$a5") = fn;
1571 register void *__arg __asm__("$a6") = arg;
1572 register int nr_clone __asm__("$a7") = __NR_clone;
1573
1574 __asm__ __volatile__(
1575 "syscall 0\n"
1576
1577 // if ($a0 != 0)
1578 // return $a0;
1579 "bnez $a0, 1f\n"
1580
1581 // In the child, now. Call "fn(arg)".
1582 "move $a0, $a6\n"
1583 "jirl $ra, $a5, 0\n"
1584
1585 // Call _exit($a0).
1586 "addi.d $a7, $zero, %9\n"
1587 "syscall 0\n"
1588
1589 "1:\n"
1590
1591 : "=r"(res)
1592 : "0"(__flags), "r"(__stack), "r"(__ptid), "r"(__ctid), "r"(__tls),
1593 "r"(__fn), "r"(__arg), "r"(nr_clone), "i"(__NR_exit)
1594 : "memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7",
1595 "$t8");
1596 return res;
1597 }
1598 # elif defined(__powerpc64__)
1599 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1600 int *parent_tidptr, void *newtls, int *child_tidptr) {
1601 long long res;
1602 // Stack frame structure.
1603 # if SANITIZER_PPC64V1
1604 // Back chain == 0 (SP + 112)
1605 // Frame (112 bytes):
1606 // Parameter save area (SP + 48), 8 doublewords
1607 // TOC save area (SP + 40)
1608 // Link editor doubleword (SP + 32)
1609 // Compiler doubleword (SP + 24)
1610 // LR save area (SP + 16)
1611 // CR save area (SP + 8)
1612 // Back chain (SP + 0)
1613 # define FRAME_SIZE 112
1614 # define FRAME_TOC_SAVE_OFFSET 40
1615 # elif SANITIZER_PPC64V2
1616 // Back chain == 0 (SP + 32)
1617 // Frame (32 bytes):
1618 // TOC save area (SP + 24)
1619 // LR save area (SP + 16)
1620 // CR save area (SP + 8)
1621 // Back chain (SP + 0)
1622 # define FRAME_SIZE 32
1623 # define FRAME_TOC_SAVE_OFFSET 24
1624 # else
1625 # error "Unsupported PPC64 ABI"
1626 # endif
1627 if (!fn || !child_stack)
1628 return -EINVAL;
1629 CHECK_EQ(0, (uptr)child_stack % 16);
1630
1631 register int (*__fn)(void *) __asm__("r3") = fn;
1632 register void *__cstack __asm__("r4") = child_stack;
1633 register int __flags __asm__("r5") = flags;
1634 register void *__arg __asm__("r6") = arg;
1635 register int *__ptidptr __asm__("r7") = parent_tidptr;
1636 register void *__newtls __asm__("r8") = newtls;
1637 register int *__ctidptr __asm__("r9") = child_tidptr;
1638
1639 __asm__ __volatile__(
1640 /* fn and arg are saved across the syscall */
1641 "mr 28, %5\n\t"
1642 "mr 27, %8\n\t"
1643
1644 /* syscall
1645 r0 == __NR_clone
1646 r3 == flags
1647 r4 == child_stack
1648 r5 == parent_tidptr
1649 r6 == newtls
1650 r7 == child_tidptr */
1651 "mr 3, %7\n\t"
1652 "mr 5, %9\n\t"
1653 "mr 6, %10\n\t"
1654 "mr 7, %11\n\t"
1655 "li 0, %3\n\t"
1656 "sc\n\t"
1657
1658 /* Test if syscall was successful */
1659 "cmpdi cr1, 3, 0\n\t"
1660 "crandc cr1*4+eq, cr1*4+eq, cr0*4+so\n\t"
1661 "bne- cr1, 1f\n\t"
1662
1663 /* Set up stack frame */
1664 "li 29, 0\n\t"
1665 "stdu 29, -8(1)\n\t"
1666 "stdu 1, -%12(1)\n\t"
1667 /* Do the function call */
1668 "std 2, %13(1)\n\t"
1669 # if SANITIZER_PPC64V1
1670 "ld 0, 0(28)\n\t"
1671 "ld 2, 8(28)\n\t"
1672 "mtctr 0\n\t"
1673 # elif SANITIZER_PPC64V2
1674 "mr 12, 28\n\t"
1675 "mtctr 12\n\t"
1676 # else
1677 # error "Unsupported PPC64 ABI"
1678 # endif
1679 "mr 3, 27\n\t"
1680 "bctrl\n\t"
1681 "ld 2, %13(1)\n\t"
1682
1683 /* Call _exit(r3) */
1684 "li 0, %4\n\t"
1685 "sc\n\t"
1686
1687 /* Return to parent */
1688 "1:\n\t"
1689 "mr %0, 3\n\t"
1690 : "=r"(res)
1691 : "0"(-1), "i"(EINVAL), "i"(__NR_clone), "i"(__NR_exit), "r"(__fn),
1692 "r"(__cstack), "r"(__flags), "r"(__arg), "r"(__ptidptr), "r"(__newtls),
1693 "r"(__ctidptr), "i"(FRAME_SIZE), "i"(FRAME_TOC_SAVE_OFFSET)
1694 : "cr0", "cr1", "memory", "ctr", "r0", "r27", "r28", "r29");
1695 return res;
1696 }
1697 # elif defined(__i386__)
1698 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1699 int *parent_tidptr, void *newtls, int *child_tidptr) {
1700 int res;
1701 if (!fn || !child_stack)
1702 return -EINVAL;
1703 CHECK_EQ(0, (uptr)child_stack % 16);
1704 child_stack = (char *)child_stack - 7 * sizeof(unsigned int);
1705 ((unsigned int *)child_stack)[0] = (uptr)flags;
1706 ((unsigned int *)child_stack)[1] = (uptr)0;
1707 ((unsigned int *)child_stack)[2] = (uptr)fn;
1708 ((unsigned int *)child_stack)[3] = (uptr)arg;
1709 __asm__ __volatile__(
1710 /* %eax = syscall(%eax = SYSCALL(clone),
1711 * %ebx = flags,
1712 * %ecx = child_stack,
1713 * %edx = parent_tidptr,
1714 * %esi = new_tls,
1715 * %edi = child_tidptr)
1716 */
1717
1718 /* Obtain flags */
1719 "movl (%%ecx), %%ebx\n"
1720 /* Do the system call */
1721 "pushl %%ebx\n"
1722 "pushl %%esi\n"
1723 "pushl %%edi\n"
1724 /* Remember the flag value. */
1725 "movl %%ebx, (%%ecx)\n"
1726 "int $0x80\n"
1727 "popl %%edi\n"
1728 "popl %%esi\n"
1729 "popl %%ebx\n"
1730
1731 /* if (%eax != 0)
1732 * return;
1733 */
1734
1735 "test %%eax,%%eax\n"
1736 "jnz 1f\n"
1737
1738 /* terminate the stack frame */
1739 "xorl %%ebp,%%ebp\n"
1740 /* Call FN. */
1741 "call *%%ebx\n"
1742 # ifdef PIC
1743 "call here\n"
1744 "here:\n"
1745 "popl %%ebx\n"
1746 "addl $_GLOBAL_OFFSET_TABLE_+[.-here], %%ebx\n"
1747 # endif
1748 /* Call exit */
1749 "movl %%eax, %%ebx\n"
1750 "movl %2, %%eax\n"
1751 "int $0x80\n"
1752 "1:\n"
1753 : "=a"(res)
1754 : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)), "c"(child_stack),
1755 "d"(parent_tidptr), "S"(newtls), "D"(child_tidptr)
1756 : "memory");
1757 return res;
1758 }
1759 # elif defined(__arm__)
1760 uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
1761 int *parent_tidptr, void *newtls, int *child_tidptr) {
1762 unsigned int res;
1763 if (!fn || !child_stack)
1764 return -EINVAL;
1765 child_stack = (char *)child_stack - 2 * sizeof(unsigned int);
1766 ((unsigned int *)child_stack)[0] = (uptr)fn;
1767 ((unsigned int *)child_stack)[1] = (uptr)arg;
1768 register int r0 __asm__("r0") = flags;
1769 register void *r1 __asm__("r1") = child_stack;
1770 register int *r2 __asm__("r2") = parent_tidptr;
1771 register void *r3 __asm__("r3") = newtls;
1772 register int *r4 __asm__("r4") = child_tidptr;
1773 register int r7 __asm__("r7") = __NR_clone;
1774
1775 # if __ARM_ARCH > 4 || defined(__ARM_ARCH_4T__)
1776 # define ARCH_HAS_BX
1777 # endif
1778 # if __ARM_ARCH > 4
1779 # define ARCH_HAS_BLX
1780 # endif
1781
1782 # ifdef ARCH_HAS_BX
1783 # ifdef ARCH_HAS_BLX
1784 # define BLX(R) "blx " #R "\n"
1785 # else
1786 # define BLX(R) "mov lr, pc; bx " #R "\n"
1787 # endif
1788 # else
1789 # define BLX(R) "mov lr, pc; mov pc," #R "\n"
1790 # endif
1791
1792 __asm__ __volatile__(
1793 /* %r0 = syscall(%r7 = SYSCALL(clone),
1794 * %r0 = flags,
1795 * %r1 = child_stack,
1796 * %r2 = parent_tidptr,
1797 * %r3 = new_tls,
1798 * %r4 = child_tidptr)
1799 */
1800
1801 /* Do the system call */
1802 "swi 0x0\n"
1803
1804 /* if (%r0 != 0)
1805 * return %r0;
1806 */
1807 "cmp r0, #0\n"
1808 "bne 1f\n"
1809
1810 /* In the child, now. Call "fn(arg)". */
1811 "ldr r0, [sp, #4]\n"
1812 "ldr ip, [sp], #8\n" BLX(ip)
1813 /* Call _exit(%r0). */
1814 "mov r7, %7\n"
1815 "swi 0x0\n"
1816 "1:\n"
1817 "mov %0, r0\n"
1818 : "=r"(res)
1819 : "r"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r7), "i"(__NR_exit)
1820 : "memory");
1821 return res;
1822 }
1823 # endif
1824 # endif // SANITIZER_LINUX
1825
1826 # if SANITIZER_LINUX
1827 int internal_uname(struct utsname *buf) {
1828 return internal_syscall(SYSCALL(uname), buf);
1829 }
1830 # endif
1831
1832 # if SANITIZER_ANDROID
1833 # if __ANDROID_API__ < 21
1834 extern "C" __attribute__((weak)) int dl_iterate_phdr(
1835 int (*)(struct dl_phdr_info *, size_t, void *), void *);
1836 # endif
1837
1838 static int dl_iterate_phdr_test_cb(struct dl_phdr_info *info, size_t size,
1839 void *data) {
1840 // Any name starting with "lib" indicates a bug in L where library base names
1841 // are returned instead of paths.
1842 if (info->dlpi_name && info->dlpi_name[0] == 'l' &&
1843 info->dlpi_name[1] == 'i' && info->dlpi_name[2] == 'b') {
1844 *(bool *)data = true;
1845 return 1;
1846 }
1847 return 0;
1848 }
1849
1850 static atomic_uint32_t android_api_level;
1851
1852 static AndroidApiLevel AndroidDetectApiLevelStatic() {
1853 # if __ANDROID_API__ <= 19
1854 return ANDROID_KITKAT;
1855 # elif __ANDROID_API__ <= 22
1856 return ANDROID_LOLLIPOP_MR1;
1857 # else
1858 return ANDROID_POST_LOLLIPOP;
1859 # endif
1860 }
1861
1862 static AndroidApiLevel AndroidDetectApiLevel() {
1863 if (!&dl_iterate_phdr)
1864 return ANDROID_KITKAT; // K or lower
1865 bool base_name_seen = false;
1866 dl_iterate_phdr(dl_iterate_phdr_test_cb, &base_name_seen);
1867 if (base_name_seen)
1868 return ANDROID_LOLLIPOP_MR1; // L MR1
1869 return ANDROID_POST_LOLLIPOP; // post-L
1870 // Plain L (API level 21) is completely broken wrt ASan and not very
1871 // interesting to detect.
1872 }
1873
1874 extern "C" __attribute__((weak)) void *_DYNAMIC;
1875
1876 AndroidApiLevel AndroidGetApiLevel() {
1877 AndroidApiLevel level =
1878 (AndroidApiLevel)atomic_load(&android_api_level, memory_order_relaxed);
1879 if (level)
1880 return level;
1881 level = &_DYNAMIC == nullptr ? AndroidDetectApiLevelStatic()
1882 : AndroidDetectApiLevel();
1883 atomic_store(&android_api_level, level, memory_order_relaxed);
1884 return level;
1885 }
1886
1887 # endif
1888
1889 static HandleSignalMode GetHandleSignalModeImpl(int signum) {
1890 switch (signum) {
1891 case SIGABRT:
1892 return common_flags()->handle_abort;
1893 case SIGILL:
1894 return common_flags()->handle_sigill;
1895 case SIGTRAP:
1896 return common_flags()->handle_sigtrap;
1897 case SIGFPE:
1898 return common_flags()->handle_sigfpe;
1899 case SIGSEGV:
1900 return common_flags()->handle_segv;
1901 case SIGBUS:
1902 return common_flags()->handle_sigbus;
1903 }
1904 return kHandleSignalNo;
1905 }
1906
1907 HandleSignalMode GetHandleSignalMode(int signum) {
1908 HandleSignalMode result = GetHandleSignalModeImpl(signum);
1909 if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
1910 return kHandleSignalExclusive;
1911 return result;
1912 }
1913
1914 # if !SANITIZER_GO
1915 void *internal_start_thread(void *(*func)(void *arg), void *arg) {
1916 if (&internal_pthread_create == 0)
1917 return nullptr;
1918 // Start the thread with signals blocked, otherwise it can steal user signals.
1919 ScopedBlockSignals block(nullptr);
1920 void *th;
1921 internal_pthread_create(&th, nullptr, func, arg);
1922 return th;
1923 }
1924
1925 void internal_join_thread(void *th) {
1926 if (&internal_pthread_join)
1927 internal_pthread_join(th, nullptr);
1928 }
1929 # else
1930 void *internal_start_thread(void *(*func)(void *), void *arg) { return 0; }
1931
1932 void internal_join_thread(void *th) {}
1933 # endif
1934
1935 # if SANITIZER_LINUX && defined(__aarch64__)
1936 // Android headers in the older NDK releases miss this definition.
1937 struct __sanitizer_esr_context {
1938 struct _aarch64_ctx head;
1939 uint64_t esr;
1940 };
1941
1942 static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) {
1943 static const u32 kEsrMagic = 0x45535201;
1944 u8 *aux = reinterpret_cast<u8 *>(ucontext->uc_mcontext.__reserved);
1945 while (true) {
1946 _aarch64_ctx *ctx = (_aarch64_ctx *)aux;
1947 if (ctx->size == 0)
1948 break;
1949 if (ctx->magic == kEsrMagic) {
1950 *esr = ((__sanitizer_esr_context *)ctx)->esr;
1951 return true;
1952 }
1953 aux += ctx->size;
1954 }
1955 return false;
1956 }
1957 # elif SANITIZER_FREEBSD && defined(__aarch64__)
1958 // FreeBSD doesn't provide ESR in the ucontext.
1959 static bool Aarch64GetESR(ucontext_t *ucontext, u64 *esr) { return false; }
1960 # endif
1961
1962 using Context = ucontext_t;
1963
1964 SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
1965 Context *ucontext = (Context *)context;
1966 # if defined(__x86_64__) || defined(__i386__)
1967 static const uptr PF_WRITE = 1U << 1;
1968 # if SANITIZER_FREEBSD
1969 uptr err = ucontext->uc_mcontext.mc_err;
1970 # elif SANITIZER_NETBSD
1971 uptr err = ucontext->uc_mcontext.__gregs[_REG_ERR];
1972 # elif SANITIZER_SOLARIS && defined(__i386__)
1973 const int Err = 13;
1974 uptr err = ucontext->uc_mcontext.gregs[Err];
1975 # else
1976 uptr err = ucontext->uc_mcontext.gregs[REG_ERR];
1977 # endif // SANITIZER_FREEBSD
1978 return err & PF_WRITE ? Write : Read;
1979 # elif defined(__mips__)
1980 uint32_t *exception_source;
1981 uint32_t faulty_instruction;
1982 uint32_t op_code;
1983
1984 exception_source = (uint32_t *)ucontext->uc_mcontext.pc;
1985 faulty_instruction = (uint32_t)(*exception_source);
1986
1987 op_code = (faulty_instruction >> 26) & 0x3f;
1988
1989 // FIXME: Add support for FPU, microMIPS, DSP, MSA memory instructions.
1990 switch (op_code) {
1991 case 0x28: // sb
1992 case 0x29: // sh
1993 case 0x2b: // sw
1994 case 0x3f: // sd
1995 # if __mips_isa_rev < 6
1996 case 0x2c: // sdl
1997 case 0x2d: // sdr
1998 case 0x2a: // swl
1999 case 0x2e: // swr
2000 # endif
2001 return SignalContext::Write;
2002
2003 case 0x20: // lb
2004 case 0x24: // lbu
2005 case 0x21: // lh
2006 case 0x25: // lhu
2007 case 0x23: // lw
2008 case 0x27: // lwu
2009 case 0x37: // ld
2010 # if __mips_isa_rev < 6
2011 case 0x1a: // ldl
2012 case 0x1b: // ldr
2013 case 0x22: // lwl
2014 case 0x26: // lwr
2015 # endif
2016 return SignalContext::Read;
2017 # if __mips_isa_rev == 6
2018 case 0x3b: // pcrel
2019 op_code = (faulty_instruction >> 19) & 0x3;
2020 switch (op_code) {
2021 case 0x1: // lwpc
2022 case 0x2: // lwupc
2023 return SignalContext::Read;
2024 }
2025 # endif
2026 }
2027 return SignalContext::Unknown;
2028 # elif defined(__arm__)
2029 static const uptr FSR_WRITE = 1U << 11;
2030 uptr fsr = ucontext->uc_mcontext.error_code;
2031 return fsr & FSR_WRITE ? Write : Read;
2032 # elif defined(__aarch64__)
2033 static const u64 ESR_ELx_WNR = 1U << 6;
2034 u64 esr;
2035 if (!Aarch64GetESR(ucontext, &esr))
2036 return Unknown;
2037 return esr & ESR_ELx_WNR ? Write : Read;
2038 # elif defined(__loongarch__)
2039 // In the musl environment, the Linux kernel uapi sigcontext.h is not
2040 // included in signal.h. To avoid missing the SC_ADDRERR_{RD,WR} macros,
2041 // copy them here. The LoongArch Linux kernel uapi is already stable,
2042 // so there's no need to worry about the value changing.
2043 # ifndef SC_ADDRERR_RD
2044 // Address error was due to memory load
2045 # define SC_ADDRERR_RD (1 << 30)
2046 # endif
2047 # ifndef SC_ADDRERR_WR
2048 // Address error was due to memory store
2049 # define SC_ADDRERR_WR (1 << 31)
2050 # endif
2051 u32 flags = ucontext->uc_mcontext.__flags;
2052 if (flags & SC_ADDRERR_RD)
2053 return SignalContext::Read;
2054 if (flags & SC_ADDRERR_WR)
2055 return SignalContext::Write;
2056 return SignalContext::Unknown;
2057 # elif defined(__sparc__)
2058 // Decode the instruction to determine the access type.
2059 // From OpenSolaris $SRC/uts/sun4/os/trap.c (get_accesstype).
2060 # if SANITIZER_SOLARIS
2061 uptr pc = ucontext->uc_mcontext.gregs[REG_PC];
2062 # else
2063 // Historical BSDism here.
2064 struct sigcontext *scontext = (struct sigcontext *)context;
2065 # if defined(__arch64__)
2066 uptr pc = scontext->sigc_regs.tpc;
2067 # else
2068 uptr pc = scontext->si_regs.pc;
2069 # endif
2070 # endif
2071 u32 instr = *(u32 *)pc;
2072 return (instr >> 21) & 1 ? Write : Read;
2073 # elif defined(__riscv)
2074 # if SANITIZER_FREEBSD
2075 unsigned long pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
2076 # else
2077 unsigned long pc = ucontext->uc_mcontext.__gregs[REG_PC];
2078 # endif
2079 unsigned faulty_instruction = *(uint16_t *)pc;
2080
2081 # if defined(__riscv_compressed)
2082 if ((faulty_instruction & 0x3) != 0x3) { // it's a compressed instruction
2083 // set op_bits to the instruction bits [1, 0, 15, 14, 13]
2084 unsigned op_bits =
2085 ((faulty_instruction & 0x3) << 3) | (faulty_instruction >> 13);
2086 unsigned rd = faulty_instruction & 0xF80; // bits 7-11, inclusive
2087 switch (op_bits) {
2088 case 0b10'010: // c.lwsp (rd != x0)
2089 # if __riscv_xlen == 64
2090 case 0b10'011: // c.ldsp (rd != x0)
2091 # endif
2092 return rd ? SignalContext::Read : SignalContext::Unknown;
2093 case 0b00'010: // c.lw
2094 # if __riscv_flen >= 32 && __riscv_xlen == 32
2095 case 0b10'011: // c.flwsp
2096 # endif
2097 # if __riscv_flen >= 32 || __riscv_xlen == 64
2098 case 0b00'011: // c.flw / c.ld
2099 # endif
2100 # if __riscv_flen == 64
2101 case 0b00'001: // c.fld
2102 case 0b10'001: // c.fldsp
2103 # endif
2104 return SignalContext::Read;
2105 case 0b00'110: // c.sw
2106 case 0b10'110: // c.swsp
2107 # if __riscv_flen >= 32 || __riscv_xlen == 64
2108 case 0b00'111: // c.fsw / c.sd
2109 case 0b10'111: // c.fswsp / c.sdsp
2110 # endif
2111 # if __riscv_flen == 64
2112 case 0b00'101: // c.fsd
2113 case 0b10'101: // c.fsdsp
2114 # endif
2115 return SignalContext::Write;
2116 default:
2117 return SignalContext::Unknown;
2118 }
2119 }
2120 # endif
2121
2122 unsigned opcode = faulty_instruction & 0x7f; // lower 7 bits
2123 unsigned funct3 = (faulty_instruction >> 12) & 0x7; // bits 12-14, inclusive
2124 switch (opcode) {
2125 case 0b0000011: // loads
2126 switch (funct3) {
2127 case 0b000: // lb
2128 case 0b001: // lh
2129 case 0b010: // lw
2130 # if __riscv_xlen == 64
2131 case 0b011: // ld
2132 # endif
2133 case 0b100: // lbu
2134 case 0b101: // lhu
2135 return SignalContext::Read;
2136 default:
2137 return SignalContext::Unknown;
2138 }
2139 case 0b0100011: // stores
2140 switch (funct3) {
2141 case 0b000: // sb
2142 case 0b001: // sh
2143 case 0b010: // sw
2144 # if __riscv_xlen == 64
2145 case 0b011: // sd
2146 # endif
2147 return SignalContext::Write;
2148 default:
2149 return SignalContext::Unknown;
2150 }
2151 # if __riscv_flen >= 32
2152 case 0b0000111: // floating-point loads
2153 switch (funct3) {
2154 case 0b010: // flw
2155 # if __riscv_flen == 64
2156 case 0b011: // fld
2157 # endif
2158 return SignalContext::Read;
2159 default:
2160 return SignalContext::Unknown;
2161 }
2162 case 0b0100111: // floating-point stores
2163 switch (funct3) {
2164 case 0b010: // fsw
2165 # if __riscv_flen == 64
2166 case 0b011: // fsd
2167 # endif
2168 return SignalContext::Write;
2169 default:
2170 return SignalContext::Unknown;
2171 }
2172 # endif
2173 default:
2174 return SignalContext::Unknown;
2175 }
2176 # else
2177 (void)ucontext;
2178 return Unknown; // FIXME: Implement.
2179 # endif
2180 }
2181
2182 bool SignalContext::IsTrueFaultingAddress() const {
2183 auto si = static_cast<const siginfo_t *>(siginfo);
2184 // SIGSEGV signals without a true fault address have si_code set to 128.
2185 return si->si_signo == SIGSEGV && si->si_code != 128;
2186 }
2187
2188 UNUSED
2189 static const char *RegNumToRegName(int reg) {
2190 switch (reg) {
2191 # if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
2192 # if defined(__x86_64__)
2193 # if SANITIZER_NETBSD
2194 # define REG_RAX _REG_RAX
2195 # define REG_RBX _REG_RBX
2196 # define REG_RCX _REG_RCX
2197 # define REG_RDX _REG_RDX
2198 # define REG_RDI _REG_RDI
2199 # define REG_RSI _REG_RSI
2200 # define REG_RBP _REG_RBP
2201 # define REG_RSP _REG_RSP
2202 # define REG_R8 _REG_R8
2203 # define REG_R9 _REG_R9
2204 # define REG_R10 _REG_R10
2205 # define REG_R11 _REG_R11
2206 # define REG_R12 _REG_R12
2207 # define REG_R13 _REG_R13
2208 # define REG_R14 _REG_R14
2209 # define REG_R15 _REG_R15
2210 # endif
2211 case REG_RAX:
2212 return "rax";
2213 case REG_RBX:
2214 return "rbx";
2215 case REG_RCX:
2216 return "rcx";
2217 case REG_RDX:
2218 return "rdx";
2219 case REG_RDI:
2220 return "rdi";
2221 case REG_RSI:
2222 return "rsi";
2223 case REG_RBP:
2224 return "rbp";
2225 case REG_RSP:
2226 return "rsp";
2227 case REG_R8:
2228 return "r8";
2229 case REG_R9:
2230 return "r9";
2231 case REG_R10:
2232 return "r10";
2233 case REG_R11:
2234 return "r11";
2235 case REG_R12:
2236 return "r12";
2237 case REG_R13:
2238 return "r13";
2239 case REG_R14:
2240 return "r14";
2241 case REG_R15:
2242 return "r15";
2243 # elif defined(__i386__)
2244 # if SANITIZER_NETBSD
2245 # define REG_EAX _REG_EAX
2246 # define REG_EBX _REG_EBX
2247 # define REG_ECX _REG_ECX
2248 # define REG_EDX _REG_EDX
2249 # define REG_EDI _REG_EDI
2250 # define REG_ESI _REG_ESI
2251 # define REG_EBP _REG_EBP
2252 # define REG_ESP _REG_ESP
2253 # endif
2254 case REG_EAX:
2255 return "eax";
2256 case REG_EBX:
2257 return "ebx";
2258 case REG_ECX:
2259 return "ecx";
2260 case REG_EDX:
2261 return "edx";
2262 case REG_EDI:
2263 return "edi";
2264 case REG_ESI:
2265 return "esi";
2266 case REG_EBP:
2267 return "ebp";
2268 case REG_ESP:
2269 return "esp";
2270 # elif defined(__arm__)
2271 # ifdef MAKE_CASE
2272 # undef MAKE_CASE
2273 # endif
2274 # define REG_STR(reg) #reg
2275 # define MAKE_CASE(N) \
2276 case REG_R##N: \
2277 return REG_STR(r##N)
2278 MAKE_CASE(0);
2279 MAKE_CASE(1);
2280 MAKE_CASE(2);
2281 MAKE_CASE(3);
2282 MAKE_CASE(4);
2283 MAKE_CASE(5);
2284 MAKE_CASE(6);
2285 MAKE_CASE(7);
2286 MAKE_CASE(8);
2287 MAKE_CASE(9);
2288 MAKE_CASE(10);
2289 MAKE_CASE(11);
2290 MAKE_CASE(12);
2291 case REG_R13:
2292 return "sp";
2293 case REG_R14:
2294 return "lr";
2295 case REG_R15:
2296 return "pc";
2297 # elif defined(__aarch64__)
2298 # define REG_STR(reg) #reg
2299 # define MAKE_CASE(N) \
2300 case N: \
2301 return REG_STR(x##N)
2302 MAKE_CASE(0);
2303 MAKE_CASE(1);
2304 MAKE_CASE(2);
2305 MAKE_CASE(3);
2306 MAKE_CASE(4);
2307 MAKE_CASE(5);
2308 MAKE_CASE(6);
2309 MAKE_CASE(7);
2310 MAKE_CASE(8);
2311 MAKE_CASE(9);
2312 MAKE_CASE(10);
2313 MAKE_CASE(11);
2314 MAKE_CASE(12);
2315 MAKE_CASE(13);
2316 MAKE_CASE(14);
2317 MAKE_CASE(15);
2318 MAKE_CASE(16);
2319 MAKE_CASE(17);
2320 MAKE_CASE(18);
2321 MAKE_CASE(19);
2322 MAKE_CASE(20);
2323 MAKE_CASE(21);
2324 MAKE_CASE(22);
2325 MAKE_CASE(23);
2326 MAKE_CASE(24);
2327 MAKE_CASE(25);
2328 MAKE_CASE(26);
2329 MAKE_CASE(27);
2330 MAKE_CASE(28);
2331 case 29:
2332 return "fp";
2333 case 30:
2334 return "lr";
2335 case 31:
2336 return "sp";
2337 # endif
2338 # endif // SANITIZER_LINUX && SANITIZER_GLIBC
2339 default:
2340 return NULL;
2341 }
2342 return NULL;
2343 }
2344
2345 # if ((SANITIZER_LINUX && SANITIZER_GLIBC) || SANITIZER_NETBSD) && \
2346 (defined(__arm__) || defined(__aarch64__))
2347 static uptr GetArmRegister(ucontext_t *ctx, int RegNum) {
2348 switch (RegNum) {
2349 # if defined(__arm__) && !SANITIZER_NETBSD
2350 # ifdef MAKE_CASE
2351 # undef MAKE_CASE
2352 # endif
2353 # define MAKE_CASE(N) \
2354 case REG_R##N: \
2355 return ctx->uc_mcontext.arm_r##N
2356 MAKE_CASE(0);
2357 MAKE_CASE(1);
2358 MAKE_CASE(2);
2359 MAKE_CASE(3);
2360 MAKE_CASE(4);
2361 MAKE_CASE(5);
2362 MAKE_CASE(6);
2363 MAKE_CASE(7);
2364 MAKE_CASE(8);
2365 MAKE_CASE(9);
2366 MAKE_CASE(10);
2367 case REG_R11:
2368 return ctx->uc_mcontext.arm_fp;
2369 case REG_R12:
2370 return ctx->uc_mcontext.arm_ip;
2371 case REG_R13:
2372 return ctx->uc_mcontext.arm_sp;
2373 case REG_R14:
2374 return ctx->uc_mcontext.arm_lr;
2375 case REG_R15:
2376 return ctx->uc_mcontext.arm_pc;
2377 # elif defined(__aarch64__)
2378 # if SANITIZER_LINUX
2379 case 0 ... 30:
2380 return ctx->uc_mcontext.regs[RegNum];
2381 case 31:
2382 return ctx->uc_mcontext.sp;
2383 # elif SANITIZER_NETBSD
2384 case 0 ... 31:
2385 return ctx->uc_mcontext.__gregs[RegNum];
2386 # endif
2387 # endif
2388 default:
2389 return 0;
2390 }
2391 return 0;
2392 }
2393 # endif // SANITIZER_LINUX && SANITIZER_GLIBC && (defined(__arm__) ||
2394 // defined(__aarch64__))
2395
2396 UNUSED
2397 static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
2398 const char *RegName = RegNumToRegName(RegNum);
2399 # if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
2400 # if defined(__x86_64__)
2401 Printf("%s%s = 0x%016llx ", internal_strlen(RegName) == 2 ? " " : "",
2402 RegName,
2403 # if SANITIZER_LINUX
2404 ctx->uc_mcontext.gregs[RegNum]
2405 # elif SANITIZER_NETBSD
2406 ctx->uc_mcontext.__gregs[RegNum]
2407 # endif
2408 );
2409 # elif defined(__i386__)
2410 Printf("%s = 0x%08x ", RegName,
2411 # if SANITIZER_LINUX
2412 ctx->uc_mcontext.gregs[RegNum]
2413 # elif SANITIZER_NETBSD
2414 ctx->uc_mcontext.__gregs[RegNum]
2415 # endif
2416 );
2417 # elif defined(__arm__)
2418 Printf("%s%s = 0x%08zx ", internal_strlen(RegName) == 2 ? " " : "", RegName,
2419 GetArmRegister(ctx, RegNum));
2420 # elif defined(__aarch64__)
2421 Printf("%s%s = 0x%016zx ", internal_strlen(RegName) == 2 ? " " : "", RegName,
2422 GetArmRegister(ctx, RegNum));
2423 # else
2424 (void)RegName;
2425 # endif
2426 # else
2427 (void)RegName;
2428 # endif
2429 }
2430
2431 void SignalContext::DumpAllRegisters(void *context) {
2432 ucontext_t *ucontext = (ucontext_t *)context;
2433 # if SANITIZER_LINUX && SANITIZER_GLIBC || SANITIZER_NETBSD
2434 # if defined(__x86_64__)
2435 Report("Register values:\n");
2436 DumpSingleReg(ucontext, REG_RAX);
2437 DumpSingleReg(ucontext, REG_RBX);
2438 DumpSingleReg(ucontext, REG_RCX);
2439 DumpSingleReg(ucontext, REG_RDX);
2440 Printf("\n");
2441 DumpSingleReg(ucontext, REG_RDI);
2442 DumpSingleReg(ucontext, REG_RSI);
2443 DumpSingleReg(ucontext, REG_RBP);
2444 DumpSingleReg(ucontext, REG_RSP);
2445 Printf("\n");
2446 DumpSingleReg(ucontext, REG_R8);
2447 DumpSingleReg(ucontext, REG_R9);
2448 DumpSingleReg(ucontext, REG_R10);
2449 DumpSingleReg(ucontext, REG_R11);
2450 Printf("\n");
2451 DumpSingleReg(ucontext, REG_R12);
2452 DumpSingleReg(ucontext, REG_R13);
2453 DumpSingleReg(ucontext, REG_R14);
2454 DumpSingleReg(ucontext, REG_R15);
2455 Printf("\n");
2456 # elif defined(__i386__)
2457 // Duplication of this report print is caused by partial support
2458 // of register values dumping. In case of unsupported yet architecture let's
2459 // avoid printing 'Register values:' without actual values in the following
2460 // output.
2461 Report("Register values:\n");
2462 DumpSingleReg(ucontext, REG_EAX);
2463 DumpSingleReg(ucontext, REG_EBX);
2464 DumpSingleReg(ucontext, REG_ECX);
2465 DumpSingleReg(ucontext, REG_EDX);
2466 Printf("\n");
2467 DumpSingleReg(ucontext, REG_EDI);
2468 DumpSingleReg(ucontext, REG_ESI);
2469 DumpSingleReg(ucontext, REG_EBP);
2470 DumpSingleReg(ucontext, REG_ESP);
2471 Printf("\n");
2472 # elif defined(__arm__) && !SANITIZER_NETBSD
2473 Report("Register values:\n");
2474 DumpSingleReg(ucontext, REG_R0);
2475 DumpSingleReg(ucontext, REG_R1);
2476 DumpSingleReg(ucontext, REG_R2);
2477 DumpSingleReg(ucontext, REG_R3);
2478 Printf("\n");
2479 DumpSingleReg(ucontext, REG_R4);
2480 DumpSingleReg(ucontext, REG_R5);
2481 DumpSingleReg(ucontext, REG_R6);
2482 DumpSingleReg(ucontext, REG_R7);
2483 Printf("\n");
2484 DumpSingleReg(ucontext, REG_R8);
2485 DumpSingleReg(ucontext, REG_R9);
2486 DumpSingleReg(ucontext, REG_R10);
2487 DumpSingleReg(ucontext, REG_R11);
2488 Printf("\n");
2489 DumpSingleReg(ucontext, REG_R12);
2490 DumpSingleReg(ucontext, REG_R13);
2491 DumpSingleReg(ucontext, REG_R14);
2492 DumpSingleReg(ucontext, REG_R15);
2493 Printf("\n");
2494 # elif defined(__aarch64__)
2495 Report("Register values:\n");
2496 for (int i = 0; i <= 31; ++i) {
2497 DumpSingleReg(ucontext, i);
2498 if (i % 4 == 3)
2499 Printf("\n");
2500 }
2501 # else
2502 (void)ucontext;
2503 # endif
2504 # elif SANITIZER_FREEBSD
2505 # if defined(__x86_64__)
2506 Report("Register values:\n");
2507 Printf("rax = 0x%016lx ", ucontext->uc_mcontext.mc_rax);
2508 Printf("rbx = 0x%016lx ", ucontext->uc_mcontext.mc_rbx);
2509 Printf("rcx = 0x%016lx ", ucontext->uc_mcontext.mc_rcx);
2510 Printf("rdx = 0x%016lx ", ucontext->uc_mcontext.mc_rdx);
2511 Printf("\n");
2512 Printf("rdi = 0x%016lx ", ucontext->uc_mcontext.mc_rdi);
2513 Printf("rsi = 0x%016lx ", ucontext->uc_mcontext.mc_rsi);
2514 Printf("rbp = 0x%016lx ", ucontext->uc_mcontext.mc_rbp);
2515 Printf("rsp = 0x%016lx ", ucontext->uc_mcontext.mc_rsp);
2516 Printf("\n");
2517 Printf(" r8 = 0x%016lx ", ucontext->uc_mcontext.mc_r8);
2518 Printf(" r9 = 0x%016lx ", ucontext->uc_mcontext.mc_r9);
2519 Printf("r10 = 0x%016lx ", ucontext->uc_mcontext.mc_r10);
2520 Printf("r11 = 0x%016lx ", ucontext->uc_mcontext.mc_r11);
2521 Printf("\n");
2522 Printf("r12 = 0x%016lx ", ucontext->uc_mcontext.mc_r12);
2523 Printf("r13 = 0x%016lx ", ucontext->uc_mcontext.mc_r13);
2524 Printf("r14 = 0x%016lx ", ucontext->uc_mcontext.mc_r14);
2525 Printf("r15 = 0x%016lx ", ucontext->uc_mcontext.mc_r15);
2526 Printf("\n");
2527 # elif defined(__i386__)
2528 Report("Register values:\n");
2529 Printf("eax = 0x%08x ", ucontext->uc_mcontext.mc_eax);
2530 Printf("ebx = 0x%08x ", ucontext->uc_mcontext.mc_ebx);
2531 Printf("ecx = 0x%08x ", ucontext->uc_mcontext.mc_ecx);
2532 Printf("edx = 0x%08x ", ucontext->uc_mcontext.mc_edx);
2533 Printf("\n");
2534 Printf("edi = 0x%08x ", ucontext->uc_mcontext.mc_edi);
2535 Printf("esi = 0x%08x ", ucontext->uc_mcontext.mc_esi);
2536 Printf("ebp = 0x%08x ", ucontext->uc_mcontext.mc_ebp);
2537 Printf("esp = 0x%08x ", ucontext->uc_mcontext.mc_esp);
2538 Printf("\n");
2539 # else
2540 (void)ucontext;
2541 # endif
2542 # else
2543 (void)ucontext;
2544 # endif
2545 // FIXME: Implement this for other OSes and architectures.
2546 }
2547
2548 static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
2549 # if SANITIZER_NETBSD
2550 // This covers all NetBSD architectures
2551 ucontext_t *ucontext = (ucontext_t *)context;
2552 *pc = _UC_MACHINE_PC(ucontext);
2553 *bp = _UC_MACHINE_FP(ucontext);
2554 *sp = _UC_MACHINE_SP(ucontext);
2555 # elif defined(__arm__)
2556 ucontext_t *ucontext = (ucontext_t *)context;
2557 *pc = ucontext->uc_mcontext.arm_pc;
2558 *bp = ucontext->uc_mcontext.arm_fp;
2559 *sp = ucontext->uc_mcontext.arm_sp;
2560 # elif defined(__aarch64__)
2561 # if SANITIZER_FREEBSD
2562 ucontext_t *ucontext = (ucontext_t *)context;
2563 *pc = ucontext->uc_mcontext.mc_gpregs.gp_elr;
2564 *bp = ucontext->uc_mcontext.mc_gpregs.gp_x[29];
2565 *sp = ucontext->uc_mcontext.mc_gpregs.gp_sp;
2566 # else
2567 ucontext_t *ucontext = (ucontext_t *)context;
2568 *pc = ucontext->uc_mcontext.pc;
2569 *bp = ucontext->uc_mcontext.regs[29];
2570 *sp = ucontext->uc_mcontext.sp;
2571 # endif
2572 # elif defined(__hppa__)
2573 ucontext_t *ucontext = (ucontext_t *)context;
2574 *pc = ucontext->uc_mcontext.sc_iaoq[0];
2575 /* GCC uses %r3 whenever a frame pointer is needed. */
2576 *bp = ucontext->uc_mcontext.sc_gr[3];
2577 *sp = ucontext->uc_mcontext.sc_gr[30];
2578 # elif defined(__x86_64__)
2579 # if SANITIZER_FREEBSD
2580 ucontext_t *ucontext = (ucontext_t *)context;
2581 *pc = ucontext->uc_mcontext.mc_rip;
2582 *bp = ucontext->uc_mcontext.mc_rbp;
2583 *sp = ucontext->uc_mcontext.mc_rsp;
2584 # else
2585 ucontext_t *ucontext = (ucontext_t *)context;
2586 *pc = ucontext->uc_mcontext.gregs[REG_RIP];
2587 *bp = ucontext->uc_mcontext.gregs[REG_RBP];
2588 *sp = ucontext->uc_mcontext.gregs[REG_RSP];
2589 # endif
2590 # elif defined(__i386__)
2591 # if SANITIZER_FREEBSD
2592 ucontext_t *ucontext = (ucontext_t *)context;
2593 *pc = ucontext->uc_mcontext.mc_eip;
2594 *bp = ucontext->uc_mcontext.mc_ebp;
2595 *sp = ucontext->uc_mcontext.mc_esp;
2596 # else
2597 ucontext_t *ucontext = (ucontext_t *)context;
2598 # if SANITIZER_SOLARIS
2599 /* Use the numeric values: the symbolic ones are undefined by llvm
2600 include/llvm/Support/Solaris.h. */
2601 # ifndef REG_EIP
2602 # define REG_EIP 14 // REG_PC
2603 # endif
2604 # ifndef REG_EBP
2605 # define REG_EBP 6 // REG_FP
2606 # endif
2607 # ifndef REG_UESP
2608 # define REG_UESP 17 // REG_SP
2609 # endif
2610 # endif
2611 *pc = ucontext->uc_mcontext.gregs[REG_EIP];
2612 *bp = ucontext->uc_mcontext.gregs[REG_EBP];
2613 *sp = ucontext->uc_mcontext.gregs[REG_UESP];
2614 # endif
2615 # elif defined(__powerpc__) || defined(__powerpc64__)
2616 # if SANITIZER_FREEBSD
2617 ucontext_t *ucontext = (ucontext_t *)context;
2618 *pc = ucontext->uc_mcontext.mc_srr0;
2619 *sp = ucontext->uc_mcontext.mc_frame[1];
2620 *bp = ucontext->uc_mcontext.mc_frame[31];
2621 # else
2622 ucontext_t *ucontext = (ucontext_t *)context;
2623 *pc = ucontext->uc_mcontext.regs->nip;
2624 *sp = ucontext->uc_mcontext.regs->gpr[PT_R1];
2625 // The powerpc{,64}-linux ABIs do not specify r31 as the frame
2626 // pointer, but GCC always uses r31 when we need a frame pointer.
2627 *bp = ucontext->uc_mcontext.regs->gpr[PT_R31];
2628 # endif
2629 # elif defined(__sparc__)
2630 # if defined(__arch64__) || defined(__sparcv9)
2631 # define STACK_BIAS 2047
2632 # else
2633 # define STACK_BIAS 0
2634 # endif
2635 # if SANITIZER_SOLARIS
2636 ucontext_t *ucontext = (ucontext_t *)context;
2637 *pc = ucontext->uc_mcontext.gregs[REG_PC];
2638 *sp = ucontext->uc_mcontext.gregs[REG_SP] + STACK_BIAS;
2639 // Avoid SEGV when dereferencing sp on stack overflow with non-faulting load.
2640 // This requires a SPARC V9 CPU. Cannot use #ASI_PNF here: only supported
2641 // since clang-19.
2642 # if defined(__sparcv9)
2643 asm("ldxa [%[fp]] 0x82, %[bp]"
2644 # else
2645 asm("lduwa [%[fp]] 0x82, %[bp]"
2646 # endif
2647 : [bp] "=r"(*bp)
2648 : [fp] "r"(&((struct frame *)*sp)->fr_savfp));
2649 if (*bp)
2650 *bp += STACK_BIAS;
2651 # else
2652 // Historical BSDism here.
2653 struct sigcontext *scontext = (struct sigcontext *)context;
2654 # if defined(__arch64__)
2655 *pc = scontext->sigc_regs.tpc;
2656 *sp = scontext->sigc_regs.u_regs[14] + STACK_BIAS;
2657 # else
2658 *pc = scontext->si_regs.pc;
2659 *sp = scontext->si_regs.u_regs[14];
2660 # endif
2661 *bp = (uptr)((uhwptr *)*sp)[14] + STACK_BIAS;
2662 # endif
2663 # elif defined(__mips__)
2664 ucontext_t *ucontext = (ucontext_t *)context;
2665 *pc = ucontext->uc_mcontext.pc;
2666 *bp = ucontext->uc_mcontext.gregs[30];
2667 *sp = ucontext->uc_mcontext.gregs[29];
2668 # elif defined(__s390__)
2669 ucontext_t *ucontext = (ucontext_t *)context;
2670 # if defined(__s390x__)
2671 *pc = ucontext->uc_mcontext.psw.addr;
2672 # else
2673 *pc = ucontext->uc_mcontext.psw.addr & 0x7fffffff;
2674 # endif
2675 *bp = ucontext->uc_mcontext.gregs[11];
2676 *sp = ucontext->uc_mcontext.gregs[15];
2677 # elif defined(__riscv)
2678 ucontext_t *ucontext = (ucontext_t *)context;
2679 # if SANITIZER_FREEBSD
2680 *pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
2681 *bp = ucontext->uc_mcontext.mc_gpregs.gp_s[0];
2682 *sp = ucontext->uc_mcontext.mc_gpregs.gp_sp;
2683 # else
2684 *pc = ucontext->uc_mcontext.__gregs[REG_PC];
2685 *bp = ucontext->uc_mcontext.__gregs[REG_S0];
2686 *sp = ucontext->uc_mcontext.__gregs[REG_SP];
2687 # endif
2688 # elif defined(__hexagon__)
2689 ucontext_t *ucontext = (ucontext_t *)context;
2690 *pc = ucontext->uc_mcontext.pc;
2691 *bp = ucontext->uc_mcontext.r30;
2692 *sp = ucontext->uc_mcontext.r29;
2693 # elif defined(__loongarch__)
2694 ucontext_t *ucontext = (ucontext_t *)context;
2695 *pc = ucontext->uc_mcontext.__pc;
2696 *bp = ucontext->uc_mcontext.__gregs[22];
2697 *sp = ucontext->uc_mcontext.__gregs[3];
2698 # else
2699 # error "Unsupported arch"
2700 # endif
2701 }
2702
2703 void SignalContext::InitPcSpBp() { GetPcSpBp(context, &pc, &sp, &bp); }
2704
2705 void InitializePlatformEarly() { InitTlsSize(); }
2706
2707 void CheckASLR() {
2708 # if SANITIZER_NETBSD
2709 int mib[3];
2710 int paxflags;
2711 uptr len = sizeof(paxflags);
2712
2713 mib[0] = CTL_PROC;
2714 mib[1] = internal_getpid();
2715 mib[2] = PROC_PID_PAXFLAGS;
2716
2717 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2718 Printf("sysctl failed\n");
2719 Die();
2720 }
2721
2722 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_ASLR)) {
2723 Printf(
2724 "This sanitizer is not compatible with enabled ASLR.\n"
2725 "To disable ASLR, please run \"paxctl +a %s\" and try again.\n",
2726 GetArgv()[0]);
2727 Die();
2728 }
2729 # elif SANITIZER_FREEBSD
2730 int aslr_status;
2731 int r = internal_procctl(P_PID, 0, PROC_ASLR_STATUS, &aslr_status);
2732 if (UNLIKELY(r == -1)) {
2733 // We're making things less 'dramatic' here since
2734 // the cmd is not necessarily guaranteed to be here
2735 // just yet regarding FreeBSD release
2736 return;
2737 }
2738 if ((aslr_status & PROC_ASLR_ACTIVE) != 0) {
2739 VReport(1,
2740 "This sanitizer is not compatible with enabled ASLR "
2741 "and binaries compiled with PIE\n"
2742 "ASLR will be disabled and the program re-executed.\n");
2743 int aslr_ctl = PROC_ASLR_FORCE_DISABLE;
2744 CHECK_NE(internal_procctl(P_PID, 0, PROC_ASLR_CTL, &aslr_ctl), -1);
2745 ReExec();
2746 }
2747 # elif SANITIZER_PPC64V2
2748 // Disable ASLR for Linux PPC64LE.
2749 int old_personality = personality(0xffffffff);
2750 if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) {
2751 VReport(1,
2752 "WARNING: Program is being run with address space layout "
2753 "randomization (ASLR) enabled which prevents the thread and "
2754 "memory sanitizers from working on powerpc64le.\n"
2755 "ASLR will be disabled and the program re-executed.\n");
2756 CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1);
2757 ReExec();
2758 }
2759 # else
2760 // Do nothing
2761 # endif
2762 }
2763
2764 void CheckMPROTECT() {
2765 # if SANITIZER_NETBSD
2766 int mib[3];
2767 int paxflags;
2768 uptr len = sizeof(paxflags);
2769
2770 mib[0] = CTL_PROC;
2771 mib[1] = internal_getpid();
2772 mib[2] = PROC_PID_PAXFLAGS;
2773
2774 if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
2775 Printf("sysctl failed\n");
2776 Die();
2777 }
2778
2779 if (UNLIKELY(paxflags & CTL_PROC_PAXFLAGS_MPROTECT)) {
2780 Printf("This sanitizer is not compatible with enabled MPROTECT\n");
2781 Die();
2782 }
2783 # else
2784 // Do nothing
2785 # endif
2786 }
2787
2788 void CheckNoDeepBind(const char *filename, int flag) {
2789 # ifdef RTLD_DEEPBIND
2790 if (flag & RTLD_DEEPBIND) {
2791 Report(
2792 "You are trying to dlopen a %s shared library with RTLD_DEEPBIND flag"
2793 " which is incompatible with sanitizer runtime "
2794 "(see https://github.com/google/sanitizers/issues/611 for details"
2795 "). If you want to run %s library under sanitizers please remove "
2796 "RTLD_DEEPBIND from dlopen flags.\n",
2797 filename, filename);
2798 Die();
2799 }
2800 # endif
2801 }
2802
2803 uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
2804 uptr *largest_gap_found,
2805 uptr *max_occupied_addr) {
2806 UNREACHABLE("FindAvailableMemoryRange is not available");
2807 return 0;
2808 }
2809
2810 bool GetRandom(void *buffer, uptr length, bool blocking) {
2811 if (!buffer || !length || length > 256)
2812 return false;
2813 # if SANITIZER_USE_GETENTROPY
2814 uptr rnd = getentropy(buffer, length);
2815 int rverrno = 0;
2816 if (internal_iserror(rnd, &rverrno) && rverrno == EFAULT)
2817 return false;
2818 else if (rnd == 0)
2819 return true;
2820 # endif // SANITIZER_USE_GETENTROPY
2821
2822 # if SANITIZER_USE_GETRANDOM
2823 static atomic_uint8_t skip_getrandom_syscall;
2824 if (!atomic_load_relaxed(&skip_getrandom_syscall)) {
2825 // Up to 256 bytes, getrandom will not be interrupted.
2826 uptr res = internal_syscall(SYSCALL(getrandom), buffer, length,
2827 blocking ? 0 : GRND_NONBLOCK);
2828 int rverrno = 0;
2829 if (internal_iserror(res, &rverrno) && rverrno == ENOSYS)
2830 atomic_store_relaxed(&skip_getrandom_syscall, 1);
2831 else if (res == length)
2832 return true;
2833 }
2834 # endif // SANITIZER_USE_GETRANDOM
2835 // Up to 256 bytes, a read off /dev/urandom will not be interrupted.
2836 // blocking is moot here, O_NONBLOCK has no effect when opening /dev/urandom.
2837 uptr fd = internal_open("/dev/urandom", O_RDONLY);
2838 if (internal_iserror(fd))
2839 return false;
2840 uptr res = internal_read(fd, buffer, length);
2841 if (internal_iserror(res))
2842 return false;
2843 internal_close(fd);
2844 return true;
2845 }
2846
2847 } // namespace __sanitizer
2848
2849 #endif