llvm/compiler-rt/lib/rtsan/rtsan_interceptors.cpp

//===--- rtsan_interceptors.cpp - Realtime Sanitizer ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//

#include "rtsan/rtsan_interceptors.h"

#include "interception/interception.h"
#include "sanitizer_common/sanitizer_allocator_dlsym.h"
#include "sanitizer_common/sanitizer_allocator_internal.h"
#include "sanitizer_common/sanitizer_platform.h"
#include "sanitizer_common/sanitizer_platform_interceptors.h"

#include "interception/interception.h"
#include "rtsan/rtsan.h"
#include "rtsan/rtsan_context.h"

#if SANITIZER_APPLE

#if TARGET_OS_MAC
// On MacOS OSSpinLockLock is deprecated and no longer present in the headers,
// but the symbol still exists on the system. Forward declare here so we
// don't get compilation errors.
#include <stdint.h>
extern "C" {
typedef int32_t OSSpinLock;
void OSSpinLockLock(volatile OSSpinLock *__lock);
}
#endif

#include <libkern/OSAtomic.h>
#include <os/lock.h>
#endif

#if SANITIZER_INTERCEPT_MEMALIGN || SANITIZER_INTERCEPT_PVALLOC
#include <malloc.h>
#endif

#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>

usingnamespace__sanitizer;

namespace {
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {};
} // namespace

// Filesystem

INTERCEPTOR(int, open, const char *path, int oflag, ...) {}

#if SANITIZER_INTERCEPT_OPEN64
INTERCEPTOR(int, open64, const char *path, int oflag, ...) {}
#define RTSAN_MAYBE_INTERCEPT_OPEN64
#else
#define RTSAN_MAYBE_INTERCEPT_OPEN64
#endif // SANITIZER_INTERCEPT_OPEN64

INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {}

#if SANITIZER_INTERCEPT_OPENAT64
INTERCEPTOR(int, openat64, int fd, const char *path, int oflag, ...) {}
#define RTSAN_MAYBE_INTERCEPT_OPENAT64
#else
#define RTSAN_MAYBE_INTERCEPT_OPENAT64
#endif // SANITIZER_INTERCEPT_OPENAT64

INTERCEPTOR(int, creat, const char *path, mode_t mode) {}

#if SANITIZER_INTERCEPT_CREAT64
INTERCEPTOR(int, creat64, const char *path, mode_t mode) {}
#define RTSAN_MAYBE_INTERCEPT_CREAT64
#else
#define RTSAN_MAYBE_INTERCEPT_CREAT64
#endif // SANITIZER_INTERCEPT_CREAT64

INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {}

#if SANITIZER_INTERCEPT_FCNTL64
INTERCEPTOR(int, fcntl64, int filedes, int cmd, ...) {}
#define RTSAN_MAYBE_INTERCEPT_FCNTL64
#else
#define RTSAN_MAYBE_INTERCEPT_FCNTL64
#endif // SANITIZER_INTERCEPT_FCNTL64

INTERCEPTOR(int, close, int filedes) {}

INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {}

#if SANITIZER_INTERCEPT_FOPEN64
INTERCEPTOR(FILE *, fopen64, const char *path, const char *mode) {}
#define RTSAN_MAYBE_INTERCEPT_FOPEN64
#else
#define RTSAN_MAYBE_INTERCEPT_FOPEN64
#endif // SANITIZER_INTERCEPT_FOPEN64

INTERCEPTOR(size_t, fread, void *ptr, size_t size, size_t nitems,
            FILE *stream) {}

INTERCEPTOR(size_t, fwrite, const void *ptr, size_t size, size_t nitems,
            FILE *stream) {}

INTERCEPTOR(int, fclose, FILE *stream) {}

INTERCEPTOR(int, fputs, const char *s, FILE *stream) {}

// Streams
INTERCEPTOR(int, puts, const char *s) {}

INTERCEPTOR(ssize_t, read, int fd, void *buf, size_t count) {}

INTERCEPTOR(ssize_t, write, int fd, const void *buf, size_t count) {}

INTERCEPTOR(ssize_t, pread, int fd, void *buf, size_t count, off_t offset) {}

#if SANITIZER_INTERCEPT_PREAD64
INTERCEPTOR(ssize_t, pread64, int fd, void *buf, size_t count, off_t offset) {}
#define RTSAN_MAYBE_INTERCEPT_PREAD64
#else
#define RTSAN_MAYBE_INTERCEPT_PREAD64
#endif // SANITIZER_INTERCEPT_PREAD64

INTERCEPTOR(ssize_t, readv, int fd, const struct iovec *iov, int iovcnt) {}

INTERCEPTOR(ssize_t, pwrite, int fd, const void *buf, size_t count,
            off_t offset) {}

#if SANITIZER_INTERCEPT_PWRITE64
INTERCEPTOR(ssize_t, pwrite64, int fd, const void *buf, size_t count,
            off_t offset) {}
#define RTSAN_MAYBE_INTERCEPT_PWRITE64
#else
#define RTSAN_MAYBE_INTERCEPT_PWRITE64
#endif // SANITIZER_INTERCEPT_PWRITE64

INTERCEPTOR(ssize_t, writev, int fd, const struct iovec *iov, int iovcnt) {}

// Concurrency
#if SANITIZER_APPLE
#pragma clang diagnostic push
// OSSpinLockLock is deprecated, but still in use in libc++
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
INTERCEPTOR(void, OSSpinLockLock, volatile OSSpinLock *lock) {
  __rtsan_notify_intercepted_call("OSSpinLockLock");
  return REAL(OSSpinLockLock)(lock);
}
#pragma clang diagnostic pop

INTERCEPTOR(void, os_unfair_lock_lock, os_unfair_lock_t lock) {
  __rtsan_notify_intercepted_call("os_unfair_lock_lock");
  return REAL(os_unfair_lock_lock)(lock);
}
#elif SANITIZER_LINUX
INTERCEPTOR(int, pthread_spin_lock, pthread_spinlock_t *spinlock) {}
#endif

INTERCEPTOR(int, pthread_create, pthread_t *thread, const pthread_attr_t *attr,
            void *(*start_routine)(void *), void *arg) {}

INTERCEPTOR(int, pthread_mutex_lock, pthread_mutex_t *mutex) {}

INTERCEPTOR(int, pthread_mutex_unlock, pthread_mutex_t *mutex) {}

INTERCEPTOR(int, pthread_join, pthread_t thread, void **value_ptr) {}

INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {}

INTERCEPTOR(int, pthread_cond_broadcast, pthread_cond_t *cond) {}

INTERCEPTOR(int, pthread_cond_wait, pthread_cond_t *cond,
            pthread_mutex_t *mutex) {}

INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t *cond,
            pthread_mutex_t *mutex, const timespec *ts) {}

INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {}

INTERCEPTOR(int, pthread_rwlock_unlock, pthread_rwlock_t *lock) {}

INTERCEPTOR(int, pthread_rwlock_wrlock, pthread_rwlock_t *lock) {}

// Sleeping

INTERCEPTOR(unsigned int, sleep, unsigned int s) {}

INTERCEPTOR(int, usleep, useconds_t u) {}

INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
            struct timespec *rmtp) {}

// Memory

INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {}

INTERCEPTOR(void, free, void *ptr) {}

INTERCEPTOR(void *, malloc, SIZE_T size) {}

INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {}

INTERCEPTOR(void *, reallocf, void *ptr, SIZE_T size) {}

INTERCEPTOR(void *, valloc, SIZE_T size) {}

#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {}
#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
#else
#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
#endif

INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {}

#if SANITIZER_INTERCEPT_MEMALIGN
INTERCEPTOR(void *, memalign, size_t alignment, size_t size) {}
#endif

#if SANITIZER_INTERCEPT_PVALLOC
INTERCEPTOR(void *, pvalloc, size_t size) {}
#endif

// Sockets
INTERCEPTOR(int, socket, int domain, int type, int protocol) {}

INTERCEPTOR(ssize_t, send, int sockfd, const void *buf, size_t len, int flags) {}

INTERCEPTOR(ssize_t, sendmsg, int socket, const struct msghdr *message,
            int flags) {}

INTERCEPTOR(ssize_t, sendto, int socket, const void *buffer, size_t length,
            int flags, const struct sockaddr *dest_addr, socklen_t dest_len) {}

INTERCEPTOR(ssize_t, recv, int socket, void *buffer, size_t length, int flags) {}

INTERCEPTOR(ssize_t, recvfrom, int socket, void *buffer, size_t length,
            int flags, struct sockaddr *address, socklen_t *address_len) {}

INTERCEPTOR(ssize_t, recvmsg, int socket, struct msghdr *message, int flags) {}

INTERCEPTOR(int, shutdown, int socket, int how) {}

// Preinit
void __rtsan::InitializeInterceptors() {}