[compiler-rt][rtsan] fopencookie support. (#120864)
[llvm-project.git] / compiler-rt / lib / rtsan / rtsan_interceptors_posix.cpp
blob072923ab35ae0d6c4394277d35f5507dd7023b55
1 //===--- rtsan_interceptors.cpp - Realtime Sanitizer ------------*- 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 //===----------------------------------------------------------------------===//
11 #include "sanitizer_common/sanitizer_platform.h"
12 #if SANITIZER_POSIX
14 #include "rtsan/rtsan_interceptors.h"
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_allocator_dlsym.h"
18 #include "sanitizer_common/sanitizer_platform_interceptors.h"
20 #include "interception/interception.h"
21 #include "rtsan/rtsan.h"
23 #if SANITIZER_APPLE
25 #if TARGET_OS_MAC
26 // On MacOS OSSpinLockLock is deprecated and no longer present in the headers,
27 // but the symbol still exists on the system. Forward declare here so we
28 // don't get compilation errors.
29 #include <stdint.h>
30 extern "C" {
31 typedef int32_t OSSpinLock;
32 void OSSpinLockLock(volatile OSSpinLock *__lock);
34 #endif // TARGET_OS_MAC
36 #include <libkern/OSAtomic.h>
37 #include <os/lock.h>
38 #endif // SANITIZER_APPLE
40 #if SANITIZER_INTERCEPT_MEMALIGN || SANITIZER_INTERCEPT_PVALLOC
41 #include <malloc.h>
42 #endif
44 #include <fcntl.h>
45 #include <poll.h>
46 #include <pthread.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <sys/select.h>
50 #include <sys/socket.h>
51 #include <sys/stat.h>
52 #include <time.h>
53 #include <unistd.h>
55 using namespace __sanitizer;
57 namespace {
58 struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
59 static bool UseImpl() { return !__rtsan_is_initialized(); }
61 } // namespace
63 // Filesystem
65 INTERCEPTOR(int, open, const char *path, int oflag, ...) {
66 // We do not early exit if O_NONBLOCK is set.
67 // O_NONBLOCK **does not prevent the syscall** it simply sets the FD to be in
68 // nonblocking mode, which is a different concept than our
69 // [[clang::nonblocking]], and is not rt-safe. This behavior was confirmed
70 // using Instruments on Darwin with a simple test program
71 __rtsan_notify_intercepted_call("open");
73 if (OpenReadsVaArgs(oflag)) {
74 va_list args;
75 va_start(args, oflag);
76 const mode_t mode = va_arg(args, int);
77 va_end(args);
78 return REAL(open)(path, oflag, mode);
81 return REAL(open)(path, oflag);
84 #if SANITIZER_INTERCEPT_OPEN64
85 INTERCEPTOR(int, open64, const char *path, int oflag, ...) {
86 // See comment above about O_NONBLOCK
87 __rtsan_notify_intercepted_call("open64");
89 if (OpenReadsVaArgs(oflag)) {
90 va_list args;
91 va_start(args, oflag);
92 const mode_t mode = va_arg(args, int);
93 va_end(args);
94 return REAL(open64)(path, oflag, mode);
97 return REAL(open64)(path, oflag);
99 #define RTSAN_MAYBE_INTERCEPT_OPEN64 INTERCEPT_FUNCTION(open64)
100 #else
101 #define RTSAN_MAYBE_INTERCEPT_OPEN64
102 #endif // SANITIZER_INTERCEPT_OPEN64
104 INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
105 // See comment above about O_NONBLOCK
106 __rtsan_notify_intercepted_call("openat");
108 if (OpenReadsVaArgs(oflag)) {
109 va_list args;
110 va_start(args, oflag);
111 const mode_t mode = va_arg(args, int);
112 va_end(args);
113 return REAL(openat)(fd, path, oflag, mode);
116 return REAL(openat)(fd, path, oflag);
119 #if SANITIZER_INTERCEPT_OPENAT64
120 INTERCEPTOR(int, openat64, int fd, const char *path, int oflag, ...) {
121 // See comment above about O_NONBLOCK
122 __rtsan_notify_intercepted_call("openat64");
124 if (OpenReadsVaArgs(oflag)) {
125 va_list args;
126 va_start(args, oflag);
127 const mode_t mode = va_arg(args, int);
128 va_end(args);
129 return REAL(openat64)(fd, path, oflag, mode);
132 return REAL(openat64)(fd, path, oflag);
134 #define RTSAN_MAYBE_INTERCEPT_OPENAT64 INTERCEPT_FUNCTION(openat64)
135 #else
136 #define RTSAN_MAYBE_INTERCEPT_OPENAT64
137 #endif // SANITIZER_INTERCEPT_OPENAT64
139 INTERCEPTOR(int, creat, const char *path, mode_t mode) {
140 // See comment above about O_NONBLOCK
141 __rtsan_notify_intercepted_call("creat");
142 const int result = REAL(creat)(path, mode);
143 return result;
146 #if SANITIZER_INTERCEPT_CREAT64
147 INTERCEPTOR(int, creat64, const char *path, mode_t mode) {
148 // See comment above about O_NONBLOCK
149 __rtsan_notify_intercepted_call("creat64");
150 const int result = REAL(creat64)(path, mode);
151 return result;
153 #define RTSAN_MAYBE_INTERCEPT_CREAT64 INTERCEPT_FUNCTION(creat64)
154 #else
155 #define RTSAN_MAYBE_INTERCEPT_CREAT64
156 #endif // SANITIZER_INTERCEPT_CREAT64
158 INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
159 __rtsan_notify_intercepted_call("fcntl");
161 // Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the
162 // final argument in a variable that will hold the largest of the possible
163 // argument types. It is then assumed that the implementation of fcntl will
164 // cast it properly depending on cmd.
166 // The two types we expect for possible args are `struct flock*` and `int`
167 // we will cast to `intptr_t` which should hold both comfortably.
168 // Why `intptr_t`? It should fit both types, and it follows the freeBSD
169 // approach linked below.
170 using arg_type = intptr_t;
171 static_assert(sizeof(arg_type) >= sizeof(struct flock *));
172 static_assert(sizeof(arg_type) >= sizeof(int));
174 // Some cmds will not actually have an argument passed in this va_list.
175 // Calling va_arg when no arg exists is UB, however all currently
176 // supported architectures will give us a result in all three cases
177 // (no arg/int arg/struct flock* arg)
178 // va_arg() will generally read the next argument register or the
179 // stack. If we ever support an arch like CHERI with bounds checking, we
180 // may have to re-evaluate this approach.
182 // More discussion, and other examples following this approach
183 // https://discourse.llvm.org/t/how-to-write-an-interceptor-for-fcntl/81203
184 // https://reviews.freebsd.org/D46403
185 // https://github.com/bminor/glibc/blob/c444cc1d8335243c5c4e636d6a26c472df85522c/sysdeps/unix/sysv/linux/fcntl64.c#L37-L46
187 va_list args;
188 va_start(args, cmd);
189 const arg_type arg = va_arg(args, arg_type);
190 va_end(args);
192 return REAL(fcntl)(filedes, cmd, arg);
195 INTERCEPTOR(int, ioctl, int filedes, unsigned long request, ...) {
196 __rtsan_notify_intercepted_call("ioctl");
198 // See fcntl for discussion on why we use intptr_t
199 // And why we read from va_args on all request types
200 using arg_type = intptr_t;
201 static_assert(sizeof(arg_type) >= sizeof(struct ifreq *));
202 static_assert(sizeof(arg_type) >= sizeof(int));
204 va_list args;
205 va_start(args, request);
206 arg_type arg = va_arg(args, arg_type);
207 va_end(args);
209 return REAL(ioctl)(filedes, request, arg);
212 #if SANITIZER_INTERCEPT_FCNTL64
213 INTERCEPTOR(int, fcntl64, int filedes, int cmd, ...) {
214 __rtsan_notify_intercepted_call("fcntl64");
216 va_list args;
217 va_start(args, cmd);
219 // Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the
220 // final argument in a variable that will hold the largest of the possible
221 // argument types (pointers and ints are typical in fcntl) It is then assumed
222 // that the implementation of fcntl will cast it properly depending on cmd.
224 // This is also similar to what is done in
225 // sanitizer_common/sanitizer_common_syscalls.inc
226 const unsigned long arg = va_arg(args, unsigned long);
227 int result = REAL(fcntl64)(filedes, cmd, arg);
229 va_end(args);
231 return result;
233 #define RTSAN_MAYBE_INTERCEPT_FCNTL64 INTERCEPT_FUNCTION(fcntl64)
234 #else
235 #define RTSAN_MAYBE_INTERCEPT_FCNTL64
236 #endif // SANITIZER_INTERCEPT_FCNTL64
238 INTERCEPTOR(int, close, int filedes) {
239 __rtsan_notify_intercepted_call("close");
240 return REAL(close)(filedes);
243 INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
244 __rtsan_notify_intercepted_call("fopen");
245 return REAL(fopen)(path, mode);
248 INTERCEPTOR(FILE *, freopen, const char *path, const char *mode, FILE *stream) {
249 __rtsan_notify_intercepted_call("freopen");
250 return REAL(freopen)(path, mode, stream);
253 // Streams
255 #if SANITIZER_INTERCEPT_FOPEN64
256 INTERCEPTOR(FILE *, fopen64, const char *path, const char *mode) {
257 __rtsan_notify_intercepted_call("fopen64");
258 return REAL(fopen64)(path, mode);
261 INTERCEPTOR(FILE *, freopen64, const char *path, const char *mode,
262 FILE *stream) {
263 __rtsan_notify_intercepted_call("freopen64");
264 return REAL(freopen64)(path, mode, stream);
266 #define RTSAN_MAYBE_INTERCEPT_FOPEN64 INTERCEPT_FUNCTION(fopen64);
267 #define RTSAN_MAYBE_INTERCEPT_FREOPEN64 INTERCEPT_FUNCTION(freopen64);
268 #else
269 #define RTSAN_MAYBE_INTERCEPT_FOPEN64
270 #define RTSAN_MAYBE_INTERCEPT_FREOPEN64
271 #endif // SANITIZER_INTERCEPT_FOPEN64
273 INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems,
274 FILE *stream) {
275 __rtsan_notify_intercepted_call("fread");
276 return REAL(fread)(ptr, size, nitems, stream);
279 INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems,
280 FILE *stream) {
281 __rtsan_notify_intercepted_call("fwrite");
282 return REAL(fwrite)(ptr, size, nitems, stream);
285 INTERCEPTOR(int, fclose, FILE *stream) {
286 __rtsan_notify_intercepted_call("fclose");
287 return REAL(fclose)(stream);
290 INTERCEPTOR(int, fputs, const char *s, FILE *stream) {
291 __rtsan_notify_intercepted_call("fputs");
292 return REAL(fputs)(s, stream);
295 INTERCEPTOR(FILE *, fdopen, int fd, const char *mode) {
296 __rtsan_notify_intercepted_call("fdopen");
297 return REAL(fdopen)(fd, mode);
300 INTERCEPTOR(FILE *, fopencookie, void *cookie, const char *mode,
301 cookie_io_functions_t funcs) {
302 __rtsan_notify_intercepted_call("fopencookie");
303 return REAL(fopencookie)(cookie, mode, funcs);
306 #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
307 INTERCEPTOR(FILE *, open_memstream, char **buf, size_t *size) {
308 __rtsan_notify_intercepted_call("open_memstream");
309 return REAL(open_memstream)(buf, size);
312 INTERCEPTOR(FILE *, fmemopen, void *buf, size_t size, const char *mode) {
313 __rtsan_notify_intercepted_call("fmemopen");
314 return REAL(fmemopen)(buf, size, mode);
316 #define RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM INTERCEPT_FUNCTION(open_memstream)
317 #define RTSAN_MAYBE_INTERCEPT_FMEMOPEN INTERCEPT_FUNCTION(fmemopen)
318 #else
319 #define RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM
320 #define RTSAN_MAYBE_INTERCEPT_FMEMOPEN
321 #endif
323 INTERCEPTOR(int, puts, const char *s) {
324 __rtsan_notify_intercepted_call("puts");
325 return REAL(puts)(s);
328 INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) {
329 __rtsan_notify_intercepted_call("read");
330 return REAL(read)(fd, buf, count);
333 INTERCEPTOR(ssize_t, write, int fd, const void *buf, size_t count) {
334 __rtsan_notify_intercepted_call("write");
335 return REAL(write)(fd, buf, count);
338 INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) {
339 __rtsan_notify_intercepted_call("pread");
340 return REAL(pread)(fd, buf, count, offset);
343 #if SANITIZER_INTERCEPT_PREAD64
344 INTERCEPTOR(ssize_t, pread64, int fd, void *buf, size_t count, off_t offset) {
345 __rtsan_notify_intercepted_call("pread64");
346 return REAL(pread64)(fd, buf, count, offset);
348 #define RTSAN_MAYBE_INTERCEPT_PREAD64 INTERCEPT_FUNCTION(pread64)
349 #else
350 #define RTSAN_MAYBE_INTERCEPT_PREAD64
351 #endif // SANITIZER_INTERCEPT_PREAD64
353 INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) {
354 __rtsan_notify_intercepted_call("readv");
355 return REAL(readv)(fd, iov, iovcnt);
358 INTERCEPTOR(ssize_t, pwrite, int fd, const void *buf, size_t count,
359 off_t offset) {
360 __rtsan_notify_intercepted_call("pwrite");
361 return REAL(pwrite)(fd, buf, count, offset);
364 #if SANITIZER_INTERCEPT_PWRITE64
365 INTERCEPTOR(ssize_t, pwrite64, int fd, const void *buf, size_t count,
366 off_t offset) {
367 __rtsan_notify_intercepted_call("pwrite64");
368 return REAL(pwrite64)(fd, buf, count, offset);
370 #define RTSAN_MAYBE_INTERCEPT_PWRITE64 INTERCEPT_FUNCTION(pwrite64)
371 #else
372 #define RTSAN_MAYBE_INTERCEPT_PWRITE64
373 #endif // SANITIZER_INTERCEPT_PWRITE64
375 INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {
376 __rtsan_notify_intercepted_call("writev");
377 return REAL(writev)(fd, iov, iovcnt);
380 INTERCEPTOR(off_t, lseek, int fd, off_t offset, int whence) {
381 __rtsan_notify_intercepted_call("lseek");
382 return REAL(lseek)(fd, offset, whence);
385 #if SANITIZER_INTERCEPT_LSEEK64
386 INTERCEPTOR(off64_t, lseek64, int fd, off64_t offset, int whence) {
387 __rtsan_notify_intercepted_call("lseek64");
388 return REAL(lseek64)(fd, offset, whence);
390 #define RTSAN_MAYBE_INTERCEPT_LSEEK64 INTERCEPT_FUNCTION(lseek64)
391 #else
392 #define RTSAN_MAYBE_INTERCEPT_LSEEK64
393 #endif // SANITIZER_INTERCEPT_LSEEK64
395 INTERCEPTOR(int, dup, int oldfd) {
396 __rtsan_notify_intercepted_call("dup");
397 return REAL(dup)(oldfd);
400 INTERCEPTOR(int, dup2, int oldfd, int newfd) {
401 __rtsan_notify_intercepted_call("dup2");
402 return REAL(dup2)(oldfd, newfd);
405 INTERCEPTOR(int, chmod, const char *path, mode_t mode) {
406 __rtsan_notify_intercepted_call("chmod");
407 return REAL(chmod)(path, mode);
410 INTERCEPTOR(int, fchmod, int fd, mode_t mode) {
411 __rtsan_notify_intercepted_call("fchmod");
412 return REAL(fchmod)(fd, mode);
415 INTERCEPTOR(int, mkdir, const char *path, mode_t mode) {
416 __rtsan_notify_intercepted_call("mkdir");
417 return REAL(mkdir)(path, mode);
420 INTERCEPTOR(int, rmdir, const char *path) {
421 __rtsan_notify_intercepted_call("rmdir");
422 return REAL(rmdir)(path);
425 INTERCEPTOR(mode_t, umask, mode_t cmask) {
426 __rtsan_notify_intercepted_call("umask");
427 return REAL(umask)(cmask);
430 // Concurrency
431 #if SANITIZER_APPLE
432 #pragma clang diagnostic push
433 // OSSpinLockLock is deprecated, but still in use in libc++
434 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
435 INTERCEPTOR(void, OSSpinLockLock, volatile OSSpinLock *lock) {
436 __rtsan_notify_intercepted_call("OSSpinLockLock");
437 return REAL(OSSpinLockLock)(lock);
439 #pragma clang diagnostic pop
440 #define RTSAN_MAYBE_INTERCEPT_OSSPINLOCKLOCK INTERCEPT_FUNCTION(OSSpinLockLock)
441 #else
442 #define RTSAN_MAYBE_INTERCEPT_OSSPINLOCKLOCK
443 #endif // SANITIZER_APPLE
445 #if SANITIZER_APPLE
446 INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) {
447 __rtsan_notify_intercepted_call("os_unfair_lock_lock");
448 return REAL(os_unfair_lock_lock)(lock);
450 #define RTSAN_MAYBE_INTERCEPT_OS_UNFAIR_LOCK_LOCK \
451 INTERCEPT_FUNCTION(os_unfair_lock_lock)
452 #else
453 #define RTSAN_MAYBE_INTERCEPT_OS_UNFAIR_LOCK_LOCK
454 #endif // SANITIZER_APPLE
456 #if SANITIZER_LINUX
457 INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *spinlock) {
458 __rtsan_notify_intercepted_call("pthread_spin_lock");
459 return REAL(pthread_spin_lock)(spinlock);
461 #define RTSAN_MAYBE_INTERCEPT_PTHREAD_SPIN_LOCK \
462 INTERCEPT_FUNCTION(pthread_spin_lock)
463 #else
464 #define RTSAN_MAYBE_INTERCEPT_PTHREAD_SPIN_LOCK
465 #endif // SANITIZER_LINUX
467 INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr,
468 void *(*start_routine)(void *), void *arg) {
469 __rtsan_notify_intercepted_call("pthread_create");
470 return REAL(pthread_create)(thread, attr, start_routine, arg);
473 INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *mutex) {
474 __rtsan_notify_intercepted_call("pthread_mutex_lock");
475 return REAL(pthread_mutex_lock)(mutex);
478 INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *mutex) {
479 __rtsan_notify_intercepted_call("pthread_mutex_unlock");
480 return REAL(pthread_mutex_unlock)(mutex);
483 INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) {
484 __rtsan_notify_intercepted_call("pthread_join");
485 return REAL(pthread_join)(thread, value_ptr);
488 INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
489 __rtsan_notify_intercepted_call("pthread_cond_signal");
490 return REAL(pthread_cond_signal)(cond);
493 INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *cond) {
494 __rtsan_notify_intercepted_call("pthread_cond_broadcast");
495 return REAL(pthread_cond_broadcast)(cond);
498 INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *cond,
499 pthread_mutex_t *mutex) {
500 __rtsan_notify_intercepted_call("pthread_cond_wait");
501 return REAL(pthread_cond_wait)(cond, mutex);
504 INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond,
505 pthread_mutex_t *mutex, const timespec *ts) {
506 __rtsan_notify_intercepted_call("pthread_cond_timedwait");
507 return REAL(pthread_cond_timedwait)(cond, mutex, ts);
510 INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
511 __rtsan_notify_intercepted_call("pthread_rwlock_rdlock");
512 return REAL(pthread_rwlock_rdlock)(lock);
515 INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *lock) {
516 __rtsan_notify_intercepted_call("pthread_rwlock_unlock");
517 return REAL(pthread_rwlock_unlock)(lock);
520 INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) {
521 __rtsan_notify_intercepted_call("pthread_rwlock_wrlock");
522 return REAL(pthread_rwlock_wrlock)(lock);
525 // Sleeping
527 INTERCEPTOR(unsigned int, sleep, unsigned int s) {
528 __rtsan_notify_intercepted_call("sleep");
529 return REAL(sleep)(s);
532 INTERCEPTOR(int, usleep, useconds_t u) {
533 __rtsan_notify_intercepted_call("usleep");
534 return REAL(usleep)(u);
537 INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
538 struct timespec *rmtp) {
539 __rtsan_notify_intercepted_call("nanosleep");
540 return REAL(nanosleep)(rqtp, rmtp);
543 INTERCEPTOR(int, sched_yield, void) {
544 __rtsan_notify_intercepted_call("sched_yield");
545 return REAL(sched_yield)();
548 // Memory
550 INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {
551 if (DlsymAlloc::Use())
552 return DlsymAlloc::Callocate(num, size);
554 __rtsan_notify_intercepted_call("calloc");
555 return REAL(calloc)(num, size);
558 INTERCEPTOR(void, free, void *ptr) {
559 if (DlsymAlloc::PointerIsMine(ptr))
560 return DlsymAlloc::Free(ptr);
562 // According to the C and C++ standard, freeing a nullptr is guaranteed to be
563 // a no-op (and thus real-time safe). This can be confirmed for looking at
564 // __libc_free in the glibc source.
565 if (ptr != nullptr)
566 __rtsan_notify_intercepted_call("free");
568 return REAL(free)(ptr);
571 INTERCEPTOR(void *, malloc, SIZE_T size) {
572 if (DlsymAlloc::Use())
573 return DlsymAlloc::Allocate(size);
575 __rtsan_notify_intercepted_call("malloc");
576 return REAL(malloc)(size);
579 INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
580 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
581 return DlsymAlloc::Realloc(ptr, size);
583 __rtsan_notify_intercepted_call("realloc");
584 return REAL(realloc)(ptr, size);
587 INTERCEPTOR(void *, reallocf, void *ptr, SIZE_T size) {
588 __rtsan_notify_intercepted_call("reallocf");
589 return REAL(reallocf)(ptr, size);
592 INTERCEPTOR(void *, valloc, SIZE_T size) {
593 __rtsan_notify_intercepted_call("valloc");
594 return REAL(valloc)(size);
597 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
599 // In some cases, when targeting older Darwin versions, this warning may pop up.
600 // Because we are providing a wrapper, the client is responsible to check
601 // whether aligned_alloc is available, not us. We still succeed linking on an
602 // old OS, because we are using a weak symbol (see aligned_alloc in
603 // sanitizer_platform_interceptors.h)
604 #pragma clang diagnostic push
605 #pragma clang diagnostic ignored "-Wunguarded-availability-new"
606 INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
607 __rtsan_notify_intercepted_call("aligned_alloc");
608 return REAL(aligned_alloc)(alignment, size);
610 #pragma clang diagnostic pop
611 #define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
612 #else
613 #define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
614 #endif
616 INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
617 __rtsan_notify_intercepted_call("posix_memalign");
618 return REAL(posix_memalign)(memptr, alignment, size);
621 #if SANITIZER_INTERCEPT_MEMALIGN
622 INTERCEPTOR(void *, memalign, size_t alignment, size_t size) {
623 __rtsan_notify_intercepted_call("memalign");
624 return REAL(memalign)(alignment, size);
626 #define RTSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
627 #else
628 #define RTSAN_MAYBE_INTERCEPT_MEMALIGN
629 #endif
631 #if SANITIZER_INTERCEPT_PVALLOC
632 INTERCEPTOR(void *, pvalloc, size_t size) {
633 __rtsan_notify_intercepted_call("pvalloc");
634 return REAL(pvalloc)(size);
636 #define RTSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
637 #else
638 #define RTSAN_MAYBE_INTERCEPT_PVALLOC
639 #endif
641 INTERCEPTOR(void *, mmap, void *addr, size_t length, int prot, int flags,
642 int fd, off_t offset) {
643 __rtsan_notify_intercepted_call("mmap");
644 return REAL(mmap)(addr, length, prot, flags, fd, offset);
647 #if SANITIZER_INTERCEPT_MMAP64
648 INTERCEPTOR(void *, mmap64, void *addr, size_t length, int prot, int flags,
649 int fd, off64_t offset) {
650 __rtsan_notify_intercepted_call("mmap64");
651 return REAL(mmap64)(addr, length, prot, flags, fd, offset);
653 #define RTSAN_MAYBE_INTERCEPT_MMAP64 INTERCEPT_FUNCTION(mmap64)
654 #else
655 #define RTSAN_MAYBE_INTERCEPT_MMAP64
656 #endif // SANITIZER_INTERCEPT_MMAP64
658 INTERCEPTOR(int, munmap, void *addr, size_t length) {
659 __rtsan_notify_intercepted_call("munmap");
660 return REAL(munmap)(addr, length);
663 INTERCEPTOR(int, shm_open, const char *name, int oflag, mode_t mode) {
664 __rtsan_notify_intercepted_call("shm_open");
665 return REAL(shm_open)(name, oflag, mode);
668 INTERCEPTOR(int, shm_unlink, const char *name) {
669 __rtsan_notify_intercepted_call("shm_unlink");
670 return REAL(shm_unlink)(name);
673 // Sockets
674 INTERCEPTOR(int, getaddrinfo, const char *node, const char *service,
675 const struct addrinfo *hints, struct addrinfo **res) {
676 __rtsan_notify_intercepted_call("getaddrinfo");
677 return REAL(getaddrinfo)(node, service, hints, res);
680 INTERCEPTOR(int, getnameinfo, const struct sockaddr *sa, socklen_t salen,
681 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
682 int flags) {
683 __rtsan_notify_intercepted_call("getnameinfo");
684 return REAL(getnameinfo)(sa, salen, host, hostlen, serv, servlen, flags);
687 INTERCEPTOR(int, bind, int socket, const struct sockaddr *address,
688 socklen_t address_len) {
689 __rtsan_notify_intercepted_call("bind");
690 return REAL(bind)(socket, address, address_len);
693 INTERCEPTOR(int, listen, int socket, int backlog) {
694 __rtsan_notify_intercepted_call("listen");
695 return REAL(listen)(socket, backlog);
698 INTERCEPTOR(int, accept, int socket, struct sockaddr *address,
699 socklen_t *address_len) {
700 __rtsan_notify_intercepted_call("accept");
701 return REAL(accept)(socket, address, address_len);
704 INTERCEPTOR(int, connect, int socket, const struct sockaddr *address,
705 socklen_t address_len) {
706 __rtsan_notify_intercepted_call("connect");
707 return REAL(connect)(socket, address, address_len);
710 INTERCEPTOR(int, socket, int domain, int type, int protocol) {
711 __rtsan_notify_intercepted_call("socket");
712 return REAL(socket)(domain, type, protocol);
715 INTERCEPTOR(ssize_t, send, int sockfd, const void *buf, size_t len, int flags) {
716 __rtsan_notify_intercepted_call("send");
717 return REAL(send)(sockfd, buf, len, flags);
720 INTERCEPTOR(ssize_t, sendmsg, int socket, const struct msghdr *message,
721 int flags) {
722 __rtsan_notify_intercepted_call("sendmsg");
723 return REAL(sendmsg)(socket, message, flags);
726 INTERCEPTOR(ssize_t, sendto, int socket, const void *buffer, size_t length,
727 int flags, const struct sockaddr *dest_addr, socklen_t dest_len) {
728 __rtsan_notify_intercepted_call("sendto");
729 return REAL(sendto)(socket, buffer, length, flags, dest_addr, dest_len);
732 INTERCEPTOR(ssize_t, recv, int socket, void *buffer, size_t length, int flags) {
733 __rtsan_notify_intercepted_call("recv");
734 return REAL(recv)(socket, buffer, length, flags);
737 INTERCEPTOR(ssize_t, recvfrom, int socket, void *buffer, size_t length,
738 int flags, struct sockaddr *address, socklen_t *address_len) {
739 __rtsan_notify_intercepted_call("recvfrom");
740 return REAL(recvfrom)(socket, buffer, length, flags, address, address_len);
743 INTERCEPTOR(ssize_t, recvmsg, int socket, struct msghdr *message, int flags) {
744 __rtsan_notify_intercepted_call("recvmsg");
745 return REAL(recvmsg)(socket, message, flags);
748 INTERCEPTOR(int, shutdown, int socket, int how) {
749 __rtsan_notify_intercepted_call("shutdown");
750 return REAL(shutdown)(socket, how);
753 #if SANITIZER_INTERCEPT_ACCEPT4
754 INTERCEPTOR(int, accept4, int socket, struct sockaddr *address,
755 socklen_t *address_len, int flags) {
756 __rtsan_notify_intercepted_call("accept4");
757 return REAL(accept4)(socket, address, address_len, flags);
759 #define RTSAN_MAYBE_INTERCEPT_ACCEPT4 INTERCEPT_FUNCTION(accept4)
760 #else
761 #define RTSAN_MAYBE_INTERCEPT_ACCEPT4
762 #endif
764 // I/O Multiplexing
766 INTERCEPTOR(int, poll, struct pollfd *fds, nfds_t nfds, int timeout) {
767 __rtsan_notify_intercepted_call("poll");
768 return REAL(poll)(fds, nfds, timeout);
771 #if !SANITIZER_APPLE
772 // FIXME: This should work on all unix systems, even Mac, but currently
773 // it is showing some weird error while linking
774 // error: declaration of 'select' has a different language linkage
775 INTERCEPTOR(int, select, int nfds, fd_set *readfds, fd_set *writefds,
776 fd_set *exceptfds, struct timeval *timeout) {
777 __rtsan_notify_intercepted_call("select");
778 return REAL(select)(nfds, readfds, writefds, exceptfds, timeout);
780 #define RTSAN_MAYBE_INTERCEPT_SELECT INTERCEPT_FUNCTION(select)
781 #else
782 #define RTSAN_MAYBE_INTERCEPT_SELECT
783 #endif // !SANITIZER_APPLE
785 INTERCEPTOR(int, pselect, int nfds, fd_set *readfds, fd_set *writefds,
786 fd_set *exceptfds, const struct timespec *timeout,
787 const sigset_t *sigmask) {
788 __rtsan_notify_intercepted_call("pselect");
789 return REAL(pselect)(nfds, readfds, writefds, exceptfds, timeout, sigmask);
792 #if SANITIZER_INTERCEPT_EPOLL
793 INTERCEPTOR(int, epoll_create, int size) {
794 __rtsan_notify_intercepted_call("epoll_create");
795 return REAL(epoll_create)(size);
798 INTERCEPTOR(int, epoll_create1, int flags) {
799 __rtsan_notify_intercepted_call("epoll_create1");
800 return REAL(epoll_create1)(flags);
803 INTERCEPTOR(int, epoll_ctl, int epfd, int op, int fd,
804 struct epoll_event *event) {
805 __rtsan_notify_intercepted_call("epoll_ctl");
806 return REAL(epoll_ctl)(epfd, op, fd, event);
809 INTERCEPTOR(int, epoll_wait, int epfd, struct epoll_event *events,
810 int maxevents, int timeout) {
811 __rtsan_notify_intercepted_call("epoll_wait");
812 return REAL(epoll_wait)(epfd, events, maxevents, timeout);
815 INTERCEPTOR(int, epoll_pwait, int epfd, struct epoll_event *events,
816 int maxevents, int timeout, const sigset_t *sigmask) {
817 __rtsan_notify_intercepted_call("epoll_pwait");
818 return REAL(epoll_pwait)(epfd, events, maxevents, timeout, sigmask);
820 #define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE INTERCEPT_FUNCTION(epoll_create)
821 #define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE1 INTERCEPT_FUNCTION(epoll_create1)
822 #define RTSAN_MAYBE_INTERCEPT_EPOLL_CTL INTERCEPT_FUNCTION(epoll_ctl)
823 #define RTSAN_MAYBE_INTERCEPT_EPOLL_WAIT INTERCEPT_FUNCTION(epoll_wait)
824 #define RTSAN_MAYBE_INTERCEPT_EPOLL_PWAIT INTERCEPT_FUNCTION(epoll_pwait)
825 #else
826 #define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE
827 #define RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE1
828 #define RTSAN_MAYBE_INTERCEPT_EPOLL_CTL
829 #define RTSAN_MAYBE_INTERCEPT_EPOLL_WAIT
830 #define RTSAN_MAYBE_INTERCEPT_EPOLL_PWAIT
831 #endif // SANITIZER_INTERCEPT_EPOLL
833 #if SANITIZER_INTERCEPT_PPOLL
834 INTERCEPTOR(int, ppoll, struct pollfd *fds, nfds_t n, const struct timespec *ts,
835 const sigset_t *set) {
836 __rtsan_notify_intercepted_call("ppoll");
837 return REAL(ppoll)(fds, n, ts, set);
839 #define RTSAN_MAYBE_INTERCEPT_PPOLL INTERCEPT_FUNCTION(ppoll)
840 #else
841 #define RTSAN_MAYBE_INTERCEPT_PPOLL
842 #endif
844 #if SANITIZER_INTERCEPT_KQUEUE
845 INTERCEPTOR(int, kqueue, void) {
846 __rtsan_notify_intercepted_call("kqueue");
847 return REAL(kqueue)();
850 INTERCEPTOR(int, kevent, int kq, const struct kevent *changelist, int nchanges,
851 struct kevent *eventlist, int nevents,
852 const struct timespec *timeout) {
853 __rtsan_notify_intercepted_call("kevent");
854 return REAL(kevent)(kq, changelist, nchanges, eventlist, nevents, timeout);
857 INTERCEPTOR(int, kevent64, int kq, const struct kevent64_s *changelist,
858 int nchanges, struct kevent64_s *eventlist, int nevents,
859 unsigned int flags, const struct timespec *timeout) {
860 __rtsan_notify_intercepted_call("kevent64");
861 return REAL(kevent64)(kq, changelist, nchanges, eventlist, nevents, flags,
862 timeout);
864 #define RTSAN_MAYBE_INTERCEPT_KQUEUE INTERCEPT_FUNCTION(kqueue)
865 #define RTSAN_MAYBE_INTERCEPT_KEVENT INTERCEPT_FUNCTION(kevent)
866 #define RTSAN_MAYBE_INTERCEPT_KEVENT64 INTERCEPT_FUNCTION(kevent64)
867 #else
868 #define RTSAN_MAYBE_INTERCEPT_KQUEUE
869 #define RTSAN_MAYBE_INTERCEPT_KEVENT
870 #define RTSAN_MAYBE_INTERCEPT_KEVENT64
871 #endif // SANITIZER_INTERCEPT_KQUEUE
873 INTERCEPTOR(int, pipe, int pipefd[2]) {
874 __rtsan_notify_intercepted_call("pipe");
875 return REAL(pipe)(pipefd);
878 INTERCEPTOR(int, mkfifo, const char *pathname, mode_t mode) {
879 __rtsan_notify_intercepted_call("mkfifo");
880 return REAL(mkfifo)(pathname, mode);
883 INTERCEPTOR(pid_t, fork, void) {
884 __rtsan_notify_intercepted_call("fork");
885 return REAL(fork)();
888 INTERCEPTOR(int, execve, const char *filename, char *const argv[],
889 char *const envp[]) {
890 __rtsan_notify_intercepted_call("execve");
891 return REAL(execve)(filename, argv, envp);
894 // TODO: the `wait` family of functions is an oddity. In testing, if you
895 // intercept them, Darwin seemingly ignores them, and linux never returns from
896 // the test. Revisit this in the future, but hopefully intercepting fork/exec is
897 // enough to dissuade usage of wait by proxy.
899 #if SANITIZER_APPLE
900 #define INT_TYPE_SYSCALL int
901 #else
902 #define INT_TYPE_SYSCALL long
903 #endif
905 #pragma clang diagnostic push
906 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
907 INTERCEPTOR(INT_TYPE_SYSCALL, syscall, INT_TYPE_SYSCALL number, ...) {
908 __rtsan_notify_intercepted_call("syscall");
910 va_list args;
911 va_start(args, number);
913 // the goal is to pick something large enough to hold all syscall args
914 // see fcntl for more discussion and why we always pull all 6 args
915 using arg_type = unsigned long;
916 arg_type arg1 = va_arg(args, arg_type);
917 arg_type arg2 = va_arg(args, arg_type);
918 arg_type arg3 = va_arg(args, arg_type);
919 arg_type arg4 = va_arg(args, arg_type);
920 arg_type arg5 = va_arg(args, arg_type);
921 arg_type arg6 = va_arg(args, arg_type);
923 // these are various examples of things that COULD be passed
924 static_assert(sizeof(arg_type) >= sizeof(off_t));
925 static_assert(sizeof(arg_type) >= sizeof(struct flock *));
926 static_assert(sizeof(arg_type) >= sizeof(const char *));
927 static_assert(sizeof(arg_type) >= sizeof(int));
928 static_assert(sizeof(arg_type) >= sizeof(unsigned long));
930 va_end(args);
932 return REAL(syscall)(number, arg1, arg2, arg3, arg4, arg5, arg6);
934 #pragma clang diagnostic pop
936 // Preinit
937 void __rtsan::InitializeInterceptors() {
938 INTERCEPT_FUNCTION(calloc);
939 INTERCEPT_FUNCTION(free);
940 INTERCEPT_FUNCTION(malloc);
941 INTERCEPT_FUNCTION(realloc);
942 INTERCEPT_FUNCTION(reallocf);
943 INTERCEPT_FUNCTION(valloc);
944 RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
945 INTERCEPT_FUNCTION(posix_memalign);
946 INTERCEPT_FUNCTION(mmap);
947 RTSAN_MAYBE_INTERCEPT_MMAP64;
948 INTERCEPT_FUNCTION(munmap);
949 INTERCEPT_FUNCTION(shm_open);
950 INTERCEPT_FUNCTION(shm_unlink);
951 RTSAN_MAYBE_INTERCEPT_MEMALIGN;
952 RTSAN_MAYBE_INTERCEPT_PVALLOC;
954 INTERCEPT_FUNCTION(open);
955 RTSAN_MAYBE_INTERCEPT_OPEN64;
956 INTERCEPT_FUNCTION(openat);
957 RTSAN_MAYBE_INTERCEPT_OPENAT64;
958 INTERCEPT_FUNCTION(close);
959 INTERCEPT_FUNCTION(fopen);
960 RTSAN_MAYBE_INTERCEPT_FOPEN64;
961 RTSAN_MAYBE_INTERCEPT_FREOPEN64;
962 INTERCEPT_FUNCTION(fread);
963 INTERCEPT_FUNCTION(read);
964 INTERCEPT_FUNCTION(write);
965 INTERCEPT_FUNCTION(pread);
966 RTSAN_MAYBE_INTERCEPT_PREAD64;
967 INTERCEPT_FUNCTION(readv);
968 INTERCEPT_FUNCTION(pwrite);
969 RTSAN_MAYBE_INTERCEPT_PWRITE64;
970 INTERCEPT_FUNCTION(writev);
971 INTERCEPT_FUNCTION(fwrite);
972 INTERCEPT_FUNCTION(fclose);
973 INTERCEPT_FUNCTION(fcntl);
974 RTSAN_MAYBE_INTERCEPT_FCNTL64;
975 INTERCEPT_FUNCTION(creat);
976 RTSAN_MAYBE_INTERCEPT_CREAT64;
977 INTERCEPT_FUNCTION(puts);
978 INTERCEPT_FUNCTION(fputs);
979 INTERCEPT_FUNCTION(fdopen);
980 INTERCEPT_FUNCTION(freopen);
981 INTERCEPT_FUNCTION(fopencookie);
982 RTSAN_MAYBE_INTERCEPT_OPEN_MEMSTREAM;
983 RTSAN_MAYBE_INTERCEPT_FMEMOPEN;
984 INTERCEPT_FUNCTION(lseek);
985 RTSAN_MAYBE_INTERCEPT_LSEEK64;
986 INTERCEPT_FUNCTION(dup);
987 INTERCEPT_FUNCTION(dup2);
988 INTERCEPT_FUNCTION(chmod);
989 INTERCEPT_FUNCTION(fchmod);
990 INTERCEPT_FUNCTION(mkdir);
991 INTERCEPT_FUNCTION(rmdir);
992 INTERCEPT_FUNCTION(umask);
993 INTERCEPT_FUNCTION(ioctl);
995 RTSAN_MAYBE_INTERCEPT_OSSPINLOCKLOCK;
996 RTSAN_MAYBE_INTERCEPT_OS_UNFAIR_LOCK_LOCK;
997 RTSAN_MAYBE_INTERCEPT_PTHREAD_SPIN_LOCK;
999 INTERCEPT_FUNCTION(pthread_create);
1000 INTERCEPT_FUNCTION(pthread_mutex_lock);
1001 INTERCEPT_FUNCTION(pthread_mutex_unlock);
1002 INTERCEPT_FUNCTION(pthread_join);
1003 INTERCEPT_FUNCTION(pthread_cond_signal);
1004 INTERCEPT_FUNCTION(pthread_cond_broadcast);
1005 INTERCEPT_FUNCTION(pthread_cond_wait);
1006 INTERCEPT_FUNCTION(pthread_cond_timedwait);
1007 INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
1008 INTERCEPT_FUNCTION(pthread_rwlock_unlock);
1009 INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
1011 INTERCEPT_FUNCTION(sleep);
1012 INTERCEPT_FUNCTION(usleep);
1013 INTERCEPT_FUNCTION(nanosleep);
1014 INTERCEPT_FUNCTION(sched_yield);
1016 INTERCEPT_FUNCTION(accept);
1017 INTERCEPT_FUNCTION(bind);
1018 INTERCEPT_FUNCTION(connect);
1019 INTERCEPT_FUNCTION(getaddrinfo);
1020 INTERCEPT_FUNCTION(getnameinfo);
1021 INTERCEPT_FUNCTION(listen);
1022 INTERCEPT_FUNCTION(recv);
1023 INTERCEPT_FUNCTION(recvfrom);
1024 INTERCEPT_FUNCTION(recvmsg);
1025 INTERCEPT_FUNCTION(send);
1026 INTERCEPT_FUNCTION(sendmsg);
1027 INTERCEPT_FUNCTION(sendto);
1028 INTERCEPT_FUNCTION(shutdown);
1029 INTERCEPT_FUNCTION(socket);
1030 RTSAN_MAYBE_INTERCEPT_ACCEPT4;
1032 RTSAN_MAYBE_INTERCEPT_SELECT;
1033 INTERCEPT_FUNCTION(pselect);
1034 INTERCEPT_FUNCTION(poll);
1035 RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE;
1036 RTSAN_MAYBE_INTERCEPT_EPOLL_CREATE1;
1037 RTSAN_MAYBE_INTERCEPT_EPOLL_CTL;
1038 RTSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
1039 RTSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
1040 RTSAN_MAYBE_INTERCEPT_PPOLL;
1041 RTSAN_MAYBE_INTERCEPT_KQUEUE;
1042 RTSAN_MAYBE_INTERCEPT_KEVENT;
1043 RTSAN_MAYBE_INTERCEPT_KEVENT64;
1045 INTERCEPT_FUNCTION(pipe);
1046 INTERCEPT_FUNCTION(mkfifo);
1048 INTERCEPT_FUNCTION(fork);
1049 INTERCEPT_FUNCTION(execve);
1051 INTERCEPT_FUNCTION(syscall);
1054 #endif // SANITIZER_POSIX