Bug Summary

File:compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Warning:line 385, column 3
Undefined or garbage value returned to caller

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name sanitizer_linux.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/build-llvm/projects/compiler-rt/lib/sanitizer_common -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D HAVE_RPC_XDR_H=1 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/build-llvm/projects/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/compiler-rt/lib/sanitizer_common -I /build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/llvm/include -I /build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/compiler-rt/lib/sanitizer_common/.. -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -Wno-unused-parameter -Wno-variadic-macros -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/build-llvm/projects/compiler-rt/lib/sanitizer_common -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f=. -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -fno-builtin -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-04-14-063029-18377-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

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

/build/llvm-toolchain-snapshot-13~++20210413100635+64c24f493e5f/compiler-rt/lib/sanitizer_common/sanitizer_syscall_linux_x86_64.inc

1//===-- sanitizer_syscall_linux_x86_64.inc ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Implementations of internal_syscall and internal_iserror for Linux/x86_64.
10//
11//===----------------------------------------------------------------------===//
12
13#define SYSCALL(name)__NR_name __NR_ ## name
14
15static uptr internal_syscall(u64 nr) {
16 u64 retval;
17 asm volatile("syscall" : "=a"(retval) : "a"(nr) : "rcx", "r11",
18 "memory", "cc");
19 return retval;
20}
21
22template <typename T1>
23static uptr internal_syscall(u64 nr, T1 arg1) {
24 u64 retval;
25 asm volatile("syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1) :
26 "rcx", "r11", "memory", "cc");
27 return retval;
28}
29
30template <typename T1, typename T2>
31static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2) {
32 u64 retval;
33 asm volatile("syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
34 "S"((u64)arg2) : "rcx", "r11", "memory", "cc");
35 return retval;
3
Returning without writing to 'arg2->st_size'
36}
37
38template <typename T1, typename T2, typename T3>
39static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3) {
40 u64 retval;
41 asm volatile("syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
42 "S"((u64)arg2), "d"((u64)arg3) : "rcx", "r11", "memory", "cc");
43 return retval;
44}
45
46template <typename T1, typename T2, typename T3, typename T4>
47static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
48 u64 retval;
49 asm volatile("mov %5, %%r10;"
50 "syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
51 "S"((u64)arg2), "d"((u64)arg3), "r"((u64)arg4) :
52 "rcx", "r11", "r10", "memory", "cc");
53 return retval;
54}
55
56template <typename T1, typename T2, typename T3, typename T4, typename T5>
57static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
58 T5 arg5) {
59 u64 retval;
60 asm volatile("mov %5, %%r10;"
61 "mov %6, %%r8;"
62 "syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
63 "S"((u64)arg2), "d"((u64)arg3), "r"((u64)arg4), "r"((u64)arg5) :
64 "rcx", "r11", "r10", "r8", "memory", "cc");
65 return retval;
66}
67
68template <typename T1, typename T2, typename T3, typename T4, typename T5,
69 typename T6>
70static uptr internal_syscall(u64 nr, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
71 T5 arg5, T6 arg6) {
72 u64 retval;
73 asm volatile("mov %5, %%r10;"
74 "mov %6, %%r8;"
75 "mov %7, %%r9;"
76 "syscall" : "=a"(retval) : "a"(nr), "D"((u64)arg1),
77 "S"((u64)arg2), "d"((u64)arg3), "r"((u64)arg4), "r"((u64)arg5),
78 "r"((u64)arg6) : "rcx", "r11", "r10", "r8", "r9",
79 "memory", "cc");
80 return retval;
81}
82
83bool internal_iserror(uptr retval, int *rverrno) {
84 if (retval >= (uptr)-4095) {
85 if (rverrno)
86 *rverrno = -retval;
87 return true;
88 }
89 return false;
90}