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