llvm/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

//===-- sanitizer_common_interceptors.inc -----------------------*- 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
//
//===----------------------------------------------------------------------===//
//
// Common function interceptors for tools like AddressSanitizer,
// ThreadSanitizer, MemorySanitizer, etc.
//
// This file should be included into the tool's interceptor file,
// which has to define its own macros:
//   COMMON_INTERCEPTOR_ENTER
//   COMMON_INTERCEPTOR_ENTER_NOIGNORE
//   COMMON_INTERCEPTOR_READ_RANGE
//   COMMON_INTERCEPTOR_WRITE_RANGE
//   COMMON_INTERCEPTOR_INITIALIZE_RANGE
//   COMMON_INTERCEPTOR_DIR_ACQUIRE
//   COMMON_INTERCEPTOR_FD_ACQUIRE
//   COMMON_INTERCEPTOR_FD_RELEASE
//   COMMON_INTERCEPTOR_FD_ACCESS
//   COMMON_INTERCEPTOR_SET_THREAD_NAME
//   COMMON_INTERCEPTOR_DLOPEN
//   COMMON_INTERCEPTOR_ON_EXIT
//   COMMON_INTERCEPTOR_SET_PTHREAD_NAME
//   COMMON_INTERCEPTOR_HANDLE_RECVMSG
//   COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
//   COMMON_INTERCEPTOR_MMAP_IMPL
//   COMMON_INTERCEPTOR_MUNMAP_IMPL
//   COMMON_INTERCEPTOR_COPY_STRING
//   COMMON_INTERCEPTOR_STRNDUP_IMPL
//   COMMON_INTERCEPTOR_STRERROR
//===----------------------------------------------------------------------===//

#include <stdarg.h>

#include "interception/interception.h"
#include "sanitizer_addrhashmap.h"
#include "sanitizer_dl.h"
#include "sanitizer_errno.h"
#include "sanitizer_placement_new.h"
#include "sanitizer_platform_interceptors.h"
#include "sanitizer_platform_limits_posix.h"
#include "sanitizer_symbolizer.h"
#include "sanitizer_tls_get_addr.h"

#if SANITIZER_INTERCEPTOR_HOOKS
#define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
#define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
#else
#define DECLARE_WEAK_INTERCEPTOR_HOOK
#define CALL_WEAK_INTERCEPTOR_HOOK

#endif  // SANITIZER_INTERCEPTOR_HOOKS

#if SANITIZER_WINDOWS && !defined(va_copy)
#define va_copy
#endif // _WIN32

#if SANITIZER_FREEBSD
#define pthread_setname_np
#define inet_aton
#define inet_pton
#define iconv
#endif

#if SANITIZER_NETBSD
#define clock_getres
#define clock_gettime
#define clock_settime
#define ctime
#define ctime_r
#define devname
#define fgetpos
#define fsetpos
#define fstatvfs
#define fstatvfs1
#define fts_children
#define fts_close
#define fts_open
#define fts_read
#define fts_set
#define getitimer
#define getmntinfo
#define getpwent
#define getpwnam
#define getpwnam_r
#define getpwuid
#define getpwuid_r
#define getutent
#define getutxent
#define getutxid
#define getutxline
#define getvfsstat
#define pututxline
#define glob
#define gmtime
#define gmtime_r
#define localtime
#define localtime_r
#define mktime
#define lstat
#define opendir
#define readdir
#define readdir_r
#define scandir
#define setitimer
#define setlocale
#define shmctl
#define sigaltstack
#define sigemptyset
#define sigfillset
#define sigpending
#define sigprocmask
#define sigtimedwait
#define stat
#define statvfs
#define statvfs1
#define time
#define times
#define unvis
#define wait3
#define wait4
extern const unsigned short *_ctype_tab_;
extern const short *_toupper_tab_;
extern const short *_tolower_tab_;
#endif

#if SANITIZER_LINUX && SANITIZER_SPARC32
// On 32-bit Linux/sparc64, double and long double are identical and glibc
// uses a __nldbl_ (no long double) prefix for various stdio functions.
#define __isoc23_fscanf
#define __isoc23_scanf
#define __isoc23_sscanf
#define __isoc23_vfscanf
#define __isoc23_vscanf
#define __isoc23_vsscanf
#define __isoc99_fscanf
#define __isoc99_scanf
#define __isoc99_sscanf
#define __isoc99_vfscanf
#define __isoc99_vscanf
#define __isoc99_vsscanf
#define asprintf
#define fprintf
#define fscanf
#define printf
#define scanf
#define snprintf
#define sprintf
#define sscanf
#define vasprintf
#define vfprintf
#define vfscanf
#define vprintf
#define vscanf
#define vsnprintf
#define vsprintf
#define vsscanf
#endif

#if SANITIZER_MUSL && \
  (defined(__i386__) || defined(__arm__) || SANITIZER_MIPS32 || SANITIZER_PPC32)
// musl 1.2.0 on existing 32-bit architectures uses new symbol names for the
// time-related functions that take 64-bit time_t values.  See
// https://musl.libc.org/time64.html
#define adjtime
#define adjtimex
#define aio_suspend
#define clock_adjtime
#define clock_getres
#define clock_gettime
#define clock_nanosleep
#define clock_settime
#define cnd_timedwait
#define ctime
#define ctime_r
#define difftime
#define dlsym
#define fstatat
#define fstat
#define ftime
#define futimens
#define futimesat
#define futimes
#define getitimer
#define getrusage
#define gettimeofday
#define gmtime
#define gmtime_r
#define localtime
#define localtime_r
#define lstat
#define lutimes
#define mktime
#define mq_timedreceive
#define mq_timedsend
#define mtx_timedlock
#define nanosleep
#define ppoll
#define pselect
#define pthread_cond_timedwait
#define pthread_mutex_timedlock
#define pthread_rwlock_timedrdlock
#define pthread_rwlock_timedwrlock
#define pthread_timedjoin_np
#define recvmmsg
#define sched_rr_get_interval
#define select
#define semtimedop
#define sem_timedwait
#define setitimer
#define settimeofday
#define sigtimedwait
#define stat
#define stime
#define thrd_sleep
#define timegm
#define timerfd_gettime
#define timerfd_settime
#define timer_gettime
#define timer_settime
#define timespec_get
#define time
#define utimensat
#define utimes
#define utime
#define wait3
#define wait4
#endif

#ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
#define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size)
#endif

#ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
#define COMMON_INTERCEPTOR_UNPOISON_PARAM(count)
#endif

#ifndef COMMON_INTERCEPTOR_FD_ACCESS
#define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd)
#endif

#ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
#define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg)
#endif

#ifndef COMMON_INTERCEPTOR_FILE_OPEN
#define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path)
#endif

#ifndef COMMON_INTERCEPTOR_FILE_CLOSE
#define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file)
#endif

#ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
#define COMMON_INTERCEPTOR_LIBRARY_LOADED
#endif

#ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
#define COMMON_INTERCEPTOR_LIBRARY_UNLOADED
#endif

#ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
#define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...)
#endif

#ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
#endif

#define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n)

#ifndef COMMON_INTERCEPTOR_DLOPEN
#define COMMON_INTERCEPTOR_DLOPEN
#endif

#ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
#define COMMON_INTERCEPTOR_GET_TLS_RANGE
#endif

#ifndef COMMON_INTERCEPTOR_ACQUIRE
#define COMMON_INTERCEPTOR_ACQUIRE(ctx, u)
#endif

#ifndef COMMON_INTERCEPTOR_RELEASE
#define COMMON_INTERCEPTOR_RELEASE(ctx, u)
#endif

#ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
#define COMMON_INTERCEPTOR_USER_CALLBACK_START()
#endif

#ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
#define COMMON_INTERCEPTOR_USER_CALLBACK_END()
#endif

#ifdef SANITIZER_NLDBL_VERSION
#define COMMON_INTERCEPT_FUNCTION_LDBL
#else
#define COMMON_INTERCEPT_FUNCTION_LDBL(fn)
#endif

#if SANITIZER_GLIBC
// If we could not find the versioned symbol, fall back to an unversioned
// lookup. This is needed to work around a GLibc bug that causes dlsym
// with RTLD_NEXT to return the oldest versioned symbol.
// See https://sourceware.org/bugzilla/show_bug.cgi?id=14932.
// For certain symbols (e.g. regexec) we have to perform a versioned lookup,
// but that versioned symbol will only exist for architectures where the
// oldest Glibc version pre-dates support for that architecture.
// For example, regexec@GLIBC_2.3.4 exists on x86_64, but not RISC-V.
// See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98920.
#define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver)
#else
#define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN
#endif

#ifndef COMMON_INTERCEPTOR_MMAP_IMPL
#define COMMON_INTERCEPTOR_MMAP_IMPL
#endif

#ifndef COMMON_INTERCEPTOR_MUNMAP_IMPL
#define COMMON_INTERCEPTOR_MUNMAP_IMPL
#endif

#ifndef COMMON_INTERCEPTOR_COPY_STRING
#define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size)
#endif

#ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
#define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size)
#endif

#ifndef COMMON_INTERCEPTOR_STRERROR
#define COMMON_INTERCEPTOR_STRERROR
#endif

struct FileMetadata {};

struct CommonInterceptorMetadata {};

#if SI_POSIX
MetadataHashMap;

static MetadataHashMap *interceptor_metadata_map;

UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
                                          const FileMetadata &file) {}

UNUSED static const FileMetadata *GetInterceptorMetadata(
    __sanitizer_FILE *addr) {}

UNUSED static void DeleteInterceptorMetadata(void *addr) {}
#endif  // SI_POSIX

#if SANITIZER_INTERCEPT_STRLEN
INTERCEPTOR(SIZE_T, strlen, const char *s) {}
#define INIT_STRLEN
#else
#define INIT_STRLEN
#endif

#if SANITIZER_INTERCEPT_STRNLEN
INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {}
#define INIT_STRNLEN
#else
#define INIT_STRNLEN
#endif

#if SANITIZER_INTERCEPT_STRNDUP
INTERCEPTOR(char*, strndup, const char *s, uptr size) {}
#define INIT_STRNDUP
#else
#define INIT_STRNDUP
#endif // SANITIZER_INTERCEPT_STRNDUP

#if SANITIZER_INTERCEPT___STRNDUP
INTERCEPTOR(char*, __strndup, const char *s, uptr size) {}
#define INIT___STRNDUP
#else
#define INIT___STRNDUP
#endif // SANITIZER_INTERCEPT___STRNDUP

#if SANITIZER_INTERCEPT_TEXTDOMAIN
INTERCEPTOR(char*, textdomain, const char *domainname) {}
#define INIT_TEXTDOMAIN
#else
#define INIT_TEXTDOMAIN
#endif

#if SANITIZER_INTERCEPT_STRCMP || SANITIZER_INTERCEPT_MEMCMP
static inline int CharCmpX(unsigned char c1, unsigned char c2) {}
#endif

#if SANITIZER_INTERCEPT_STRCMP
DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
                              const char *s1, const char *s2, int result)

INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {}

DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
                              const char *s1, const char *s2, uptr n,
                              int result)

INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {}

#define INIT_STRCMP
#define INIT_STRNCMP
#else
#define INIT_STRCMP
#define INIT_STRNCMP
#endif

#if SANITIZER_INTERCEPT_STRCASECMP
static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {}

DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
                              const char *s1, const char *s2, int result)

INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {}

DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
                              const char *s1, const char *s2, uptr size,
                              int result)

INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {}

#define INIT_STRCASECMP
#define INIT_STRNCASECMP
#else
#define INIT_STRCASECMP
#define INIT_STRNCASECMP
#endif

#if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
static inline void StrstrCheck(void *ctx, char *r, const char *s1,
                               const char *s2) {}
#endif

#if SANITIZER_INTERCEPT_STRSTR

DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
                              const char *s1, const char *s2, char *result)

INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {}

#define INIT_STRSTR
#else
#define INIT_STRSTR
#endif

#if SANITIZER_INTERCEPT_STRCASESTR

DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
                              const char *s1, const char *s2, char *result)

INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {}

#define INIT_STRCASESTR
#else
#define INIT_STRCASESTR
#endif

#if SANITIZER_INTERCEPT_STRTOK

INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {}

#define INIT_STRTOK
#else
#define INIT_STRTOK
#endif

#if SANITIZER_INTERCEPT_MEMMEM
DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
                              const void *s1, SIZE_T len1, const void *s2,
                              SIZE_T len2, void *result)

INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
            SIZE_T len2) {}

#define INIT_MEMMEM
#else
#define INIT_MEMMEM
#endif  // SANITIZER_INTERCEPT_MEMMEM

#if SANITIZER_INTERCEPT_STRCHR
INTERCEPTOR(char*, strchr, const char *s, int c) {}
#define INIT_STRCHR
#else
#define INIT_STRCHR
#endif

#if SANITIZER_INTERCEPT_STRCHRNUL
INTERCEPTOR(char*, strchrnul, const char *s, int c) {}
#define INIT_STRCHRNUL
#else
#define INIT_STRCHRNUL
#endif

#if SANITIZER_INTERCEPT_STRRCHR
INTERCEPTOR(char*, strrchr, const char *s, int c) {}
#define INIT_STRRCHR
#else
#define INIT_STRRCHR
#endif

#if SANITIZER_INTERCEPT_STRSPN
INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {}

INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {}

#define INIT_STRSPN
#else
#define INIT_STRSPN
#endif

#if SANITIZER_INTERCEPT_STRPBRK
INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {}

#define INIT_STRPBRK
#else
#define INIT_STRPBRK
#endif

#if SANITIZER_INTERCEPT_MEMCMP
DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
                              const void *s1, const void *s2, uptr n,
                              int result)

// Common code for `memcmp` and `bcmp`.
int MemcmpInterceptorCommon(void *ctx,
                            int (*real_fn)(const void *, const void *, uptr),
                            const void *a1, const void *a2, uptr size) {}

INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {}

#define INIT_MEMCMP
#else
#define INIT_MEMCMP
#endif

#if SANITIZER_INTERCEPT_BCMP
INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {}

#define INIT_BCMP
#else
#define INIT_BCMP
#endif

#if SANITIZER_INTERCEPT_MEMCHR
INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {}

#define INIT_MEMCHR
#else
#define INIT_MEMCHR
#endif

#if SANITIZER_INTERCEPT_MEMRCHR
INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {}

#define INIT_MEMRCHR
#else
#define INIT_MEMRCHR
#endif

#if SANITIZER_INTERCEPT_FREXP
INTERCEPTOR(double, frexp, double x, int *exp) {}

#define INIT_FREXP
#else
#define INIT_FREXP
#endif  // SANITIZER_INTERCEPT_FREXP

#if SANITIZER_INTERCEPT_FREXPF_FREXPL
INTERCEPTOR(float, frexpf, float x, int *exp) {}

INTERCEPTOR(long double, frexpl, long double x, int *exp) {}

#define INIT_FREXPF_FREXPL
#else
#define INIT_FREXPF_FREXPL
#endif  // SANITIZER_INTERCEPT_FREXPF_FREXPL

#if SI_POSIX
static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
                        SIZE_T iovlen, SIZE_T maxlen) {}

static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
                       SIZE_T iovlen, SIZE_T maxlen) {}
#endif

#if SANITIZER_INTERCEPT_READ
INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {}
#define INIT_READ
#else
#define INIT_READ
#endif

#if SANITIZER_INTERCEPT_FREAD
INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {}
#define INIT_FREAD
#else
#define INIT_FREAD
#endif

#if SANITIZER_INTERCEPT_PREAD
INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {}
#define INIT_PREAD
#else
#define INIT_PREAD
#endif

#if SANITIZER_INTERCEPT_PREAD64
INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {}
#define INIT_PREAD64
#else
#define INIT_PREAD64
#endif

#if SANITIZER_INTERCEPT_READV
INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
                        int iovcnt) {}
#define INIT_READV
#else
#define INIT_READV
#endif

#if SANITIZER_INTERCEPT_PREADV
INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
            OFF_T offset) {}
#define INIT_PREADV
#else
#define INIT_PREADV
#endif

#if SANITIZER_INTERCEPT_PREADV64
INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
            OFF64_T offset) {}
#define INIT_PREADV64
#else
#define INIT_PREADV64
#endif

#if SANITIZER_INTERCEPT_WRITE
INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {}
#define INIT_WRITE
#else
#define INIT_WRITE
#endif

#if SANITIZER_INTERCEPT_FWRITE
INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {}
#define INIT_FWRITE
#else
#define INIT_FWRITE
#endif

#if SANITIZER_INTERCEPT_PWRITE
INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {}
#define INIT_PWRITE
#else
#define INIT_PWRITE
#endif

#if SANITIZER_INTERCEPT_PWRITE64
INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
            OFF64_T offset) {}
#define INIT_PWRITE64
#else
#define INIT_PWRITE64
#endif

#if SANITIZER_INTERCEPT_WRITEV
INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
                        int iovcnt) {}
#define INIT_WRITEV
#else
#define INIT_WRITEV
#endif

#if SANITIZER_INTERCEPT_PWRITEV
INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
            OFF_T offset) {}
#define INIT_PWRITEV
#else
#define INIT_PWRITEV
#endif

#if SANITIZER_INTERCEPT_PWRITEV64
INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
            OFF64_T offset) {}
#define INIT_PWRITEV64
#else
#define INIT_PWRITEV64
#endif

#if SANITIZER_INTERCEPT_FGETS
INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {}
#define INIT_FGETS
#else
#define INIT_FGETS
#endif

#if SANITIZER_INTERCEPT_FPUTS
INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {}
#define INIT_FPUTS
#else
#define INIT_FPUTS
#endif

#if SANITIZER_INTERCEPT_PUTS
INTERCEPTOR(int, puts, char *s) {}
#define INIT_PUTS
#else
#define INIT_PUTS
#endif

#if SANITIZER_INTERCEPT_PRCTL
INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
            unsigned long arg4, unsigned long arg5) {}
#define INIT_PRCTL
#else
#define INIT_PRCTL
#endif  // SANITIZER_INTERCEPT_PRCTL

#if SANITIZER_INTERCEPT_TIME
INTERCEPTOR(unsigned long, time, unsigned long *t) {}
#define INIT_TIME
#else
#define INIT_TIME
#endif  // SANITIZER_INTERCEPT_TIME

#if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {}
INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {}
INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {}
INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {}
INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {}
INTERCEPTOR(char *, ctime, unsigned long *timep) {}
INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {}
INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {}
INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {}
INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {}
#define INIT_LOCALTIME_AND_FRIENDS
#else
#define INIT_LOCALTIME_AND_FRIENDS
#endif  // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS

#if SANITIZER_INTERCEPT_STRPTIME
INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {}
#define INIT_STRPTIME
#else
#define INIT_STRPTIME
#endif

#if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
#include "sanitizer_common_interceptors_format.inc"

#define FORMAT_INTERCEPTOR_IMPL(name, vname, ...)

#endif

#if SANITIZER_INTERCEPT_SCANF

#define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...)

INTERCEPTOR(int, vscanf, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)

INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)

INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)

#if SANITIZER_INTERCEPT_ISOC99_SCANF
INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)

INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
            va_list ap)
VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)

INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)

INTERCEPTOR(int, __isoc23_vscanf, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(__isoc23_vscanf, false, format, ap)

INTERCEPTOR(int, __isoc23_vsscanf, const char *str, const char *format,
            va_list ap)
VSCANF_INTERCEPTOR_IMPL(__isoc23_vsscanf, false, str, format, ap)

INTERCEPTOR(int, __isoc23_vfscanf, void *stream, const char *format, va_list ap)
VSCANF_INTERCEPTOR_IMPL(__isoc23_vfscanf, false, stream, format, ap)
#endif  // SANITIZER_INTERCEPT_ISOC99_SCANF

INTERCEPTOR(int, scanf, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)

INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)

INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)

#if SANITIZER_INTERCEPT_ISOC99_SCANF
INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)

INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)

INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)

INTERCEPTOR(int, __isoc23_scanf, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc23_scanf, __isoc23_vscanf, format)

INTERCEPTOR(int, __isoc23_fscanf, void *stream, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc23_fscanf, __isoc23_vfscanf, stream, format)

INTERCEPTOR(int, __isoc23_sscanf, const char *str, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc23_sscanf, __isoc23_vsscanf, str, format)
#endif

#endif

#if SANITIZER_INTERCEPT_SCANF
#define INIT_SCANF
#else
#define INIT_SCANF
#endif

#if SANITIZER_INTERCEPT_ISOC99_SCANF
#define INIT_ISOC99_SCANF
#else
#define INIT_ISOC99_SCANF
#endif

#if SANITIZER_INTERCEPT_PRINTF

#define VPRINTF_INTERCEPTOR_ENTER(vname, ...)

#define VPRINTF_INTERCEPTOR_RETURN()

#define VPRINTF_INTERCEPTOR_IMPL(vname, ...)

// FIXME: under ASan the REAL() call below may write to freed memory and
// corrupt its metadata. See
// https://github.com/google/sanitizers/issues/321.
#define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...)

// FIXME: under ASan the REAL() call below may write to freed memory and
// corrupt its metadata. See
// https://github.com/google/sanitizers/issues/321.
#define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...)

// FIXME: under ASan the REAL() call below may write to freed memory and
// corrupt its metadata. See
// https://github.com/google/sanitizers/issues/321.
#define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...)

INTERCEPTOR(int, vprintf, const char *format, va_list ap)
VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)

INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
            va_list ap)
VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)

INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
            va_list ap)
VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)

#if SANITIZER_INTERCEPT___PRINTF_CHK
INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
            SIZE_T size_to, const char *format, va_list ap)
VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
#endif

#if SANITIZER_INTERCEPT_PRINTF_L
INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
            const char *format, va_list ap)
VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)

INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
            const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
#endif  // SANITIZER_INTERCEPT_PRINTF_L

INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)

#if SANITIZER_INTERCEPT___PRINTF_CHK
INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
            const char *format, va_list ap)
VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
#endif

INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)

#if SANITIZER_INTERCEPT_ISOC99_PRINTF
INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)

INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
            const char *format, va_list ap)
VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)

INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
            va_list ap)
VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)

INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
            va_list ap)
VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
                          ap)

#endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF

INTERCEPTOR(int, printf, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)

INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)

#if SANITIZER_INTERCEPT___PRINTF_CHK
INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
            const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
#endif

INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)

#if SANITIZER_INTERCEPT___PRINTF_CHK
INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
            const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
#endif

INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)

#if SANITIZER_INTERCEPT___PRINTF_CHK
INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
            SIZE_T size_to, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
#endif

INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)

#if SANITIZER_INTERCEPT_ISOC99_PRINTF
INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)

INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
            ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)

INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)

INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
            const char *format, ...)
FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
                        format)

#endif  // SANITIZER_INTERCEPT_ISOC99_PRINTF

#endif  // SANITIZER_INTERCEPT_PRINTF

#if SANITIZER_INTERCEPT_PRINTF
#define INIT_PRINTF
#else
#define INIT_PRINTF
#endif

#if SANITIZER_INTERCEPT___PRINTF_CHK
#define INIT___PRINTF_CHK
#else
#define INIT___PRINTF_CHK
#endif

#if SANITIZER_INTERCEPT_PRINTF_L
#define INIT_PRINTF_L
#else
#define INIT_PRINTF_L
#endif

#if SANITIZER_INTERCEPT_ISOC99_PRINTF
#define INIT_ISOC99_PRINTF
#else
#define INIT_ISOC99_PRINTF
#endif

#if SANITIZER_INTERCEPT_IOCTL
#include "sanitizer_common_interceptors_ioctl.inc"
#include "sanitizer_interceptors_ioctl_netbsd.inc"
INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {}
#define INIT_IOCTL
#else
#define INIT_IOCTL
#endif

#if SANITIZER_POSIX
UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {}

UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {}
#endif  // SANITIZER_POSIX

#if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {}
INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {}
INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {}
INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {}
#define INIT_GETPWNAM_AND_FRIENDS
#else
#define INIT_GETPWNAM_AND_FRIENDS
#endif

#if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
            char *buf, SIZE_T buflen, __sanitizer_passwd **result) {}
INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
            SIZE_T buflen, __sanitizer_passwd **result) {}
INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
            char *buf, SIZE_T buflen, __sanitizer_group **result) {}
INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
            SIZE_T buflen, __sanitizer_group **result) {}
#define INIT_GETPWNAM_R_AND_FRIENDS
#else
#define INIT_GETPWNAM_R_AND_FRIENDS
#endif

#if SANITIZER_INTERCEPT_GETPWENT
INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {}
INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {}
#define INIT_GETPWENT
#else
#define INIT_GETPWENT
#endif

#if SANITIZER_INTERCEPT_FGETPWENT
INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {}
INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {}
#define INIT_FGETPWENT
#else
#define INIT_FGETPWENT
#endif

#if SANITIZER_INTERCEPT_GETPWENT_R
INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
            SIZE_T buflen, __sanitizer_passwd **pwbufp) {}
INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
            __sanitizer_group **pwbufp) {}
#define INIT_GETPWENT_R
#else
#define INIT_GETPWENT_R
#endif

#if SANITIZER_INTERCEPT_FGETPWENT_R
INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
            SIZE_T buflen, __sanitizer_passwd **pwbufp) {}
#define INIT_FGETPWENT_R
#else
#define INIT_FGETPWENT_R
#endif

#if SANITIZER_INTERCEPT_FGETGRENT_R
INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
            SIZE_T buflen, __sanitizer_group **pwbufp) {}
#define INIT_FGETGRENT_R
#else
#define INIT_FGETGRENT_R
#endif

#if SANITIZER_INTERCEPT_SETPWENT
// The only thing these interceptors do is disable any nested interceptors.
// These functions may open nss modules and call uninstrumented functions from
// them, and we don't want things like strlen() to trigger.
INTERCEPTOR(void, setpwent, int dummy) {}
INTERCEPTOR(void, endpwent, int dummy) {}
INTERCEPTOR(void, setgrent, int dummy) {}
INTERCEPTOR(void, endgrent, int dummy) {}
#define INIT_SETPWENT
#else
#define INIT_SETPWENT
#endif

#if SANITIZER_INTERCEPT_CLOCK_GETTIME
INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {}
INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {}
#if SANITIZER_GLIBC
namespace __sanitizer {
extern "C" {
int real_clock_gettime(u32 clk_id, void *tp) {}
}  // extern "C"
}  // namespace __sanitizer
#endif
INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {}
#define INIT_CLOCK_GETTIME
#else
#define INIT_CLOCK_GETTIME
#endif

#if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
            __sanitizer_clockid_t *clockid) {}

INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
            __sanitizer_clockid_t *clockid) {}

#define INIT_CLOCK_GETCPUCLOCKID
#else
#define INIT_CLOCK_GETCPUCLOCKID
#endif

#if SANITIZER_INTERCEPT_GETITIMER
INTERCEPTOR(int, getitimer, int which, void *curr_value) {}
INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {}
#define INIT_GETITIMER
#else
#define INIT_GETITIMER
#endif

#if SANITIZER_INTERCEPT_GLOB
static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {}

#if SANITIZER_SOLARIS
INTERCEPTOR(int, glob, const char *pattern, int flags,
            int (*errfunc)(const char *epath, int eerrno),
            __sanitizer_glob_t *pglob) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
  COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
  int res = REAL(glob)(pattern, flags, errfunc, pglob);
  if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
  return res;
}
#else
static THREADLOCAL __sanitizer_glob_t *pglob_copy;

static void wrapped_gl_closedir(void *dir) {}

static void *wrapped_gl_readdir(void *dir) {}

static void *wrapped_gl_opendir(const char *s) {}

static int wrapped_gl_lstat(const char *s, void *st) {}

static int wrapped_gl_stat(const char *s, void *st) {}

static const __sanitizer_glob_t kGlobCopy =;

INTERCEPTOR(int, glob, const char *pattern, int flags,
            int (*errfunc)(const char *epath, int eerrno),
            __sanitizer_glob_t *pglob) {}
#endif  // SANITIZER_SOLARIS
#define INIT_GLOB
#else  // SANITIZER_INTERCEPT_GLOB
#define INIT_GLOB
#endif  // SANITIZER_INTERCEPT_GLOB

#if SANITIZER_INTERCEPT_GLOB64
INTERCEPTOR(int, glob64, const char *pattern, int flags,
            int (*errfunc)(const char *epath, int eerrno),
            __sanitizer_glob_t *pglob) {}
#define INIT_GLOB64
#else  // SANITIZER_INTERCEPT_GLOB64
#define INIT_GLOB64
#endif  // SANITIZER_INTERCEPT_GLOB64

#if SANITIZER_INTERCEPT___B64_TO
INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
            char *target, SIZE_T targsize) {}
INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {}
#define INIT___B64_TO
#else  // SANITIZER_INTERCEPT___B64_TO
#define INIT___B64_TO
#endif  // SANITIZER_INTERCEPT___B64_TO

#if SANITIZER_INTERCEPT_DN_COMP_EXPAND
#  if __GLIBC_PREREQ(2, 34)
// Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
#define DN_COMP_INTERCEPTOR_NAME
#define DN_EXPAND_INTERCEPTOR_NAME
#  else
#define DN_COMP_INTERCEPTOR_NAME
#define DN_EXPAND_INTERCEPTOR_NAME
#  endif
INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
            unsigned char *comp_dn, int length, unsigned char **dnptrs,
            unsigned char **lastdnptr) {}
INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
            unsigned char const *end, unsigned char const *src, char *dest,
            int space) {}
#define INIT_DN_COMP_EXPAND
#else  // SANITIZER_INTERCEPT_DN_COMP_EXPAND
#define INIT_DN_COMP_EXPAND
#endif  // SANITIZER_INTERCEPT_DN_COMP_EXPAND

#if SANITIZER_INTERCEPT_POSIX_SPAWN

template <class RealSpawnPtr>
static int PosixSpawnImpl(void *ctx, RealSpawnPtr *real_posix_spawn, pid_t *pid,
                          const char *file_or_path, const void *file_actions,
                          const void *attrp, char *const argv[],
                          char *const envp[]) {}
INTERCEPTOR(int, posix_spawn, pid_t *pid, const char *path,
            const void *file_actions, const void *attrp, char *const argv[],
            char *const envp[]) {}
INTERCEPTOR(int, posix_spawnp, pid_t *pid, const char *file,
            const void *file_actions, const void *attrp, char *const argv[],
            char *const envp[]) {}
#define INIT_POSIX_SPAWN
#else  // SANITIZER_INTERCEPT_POSIX_SPAWN
#define INIT_POSIX_SPAWN
#endif  // SANITIZER_INTERCEPT_POSIX_SPAWN

#if SANITIZER_INTERCEPT_WAIT
// According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
// suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
// details.
INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {}
// On FreeBSD id_t is always 64-bit wide.
#if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
                        int options) {
#else
INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
                        int options) {}
INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {}
INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {}
#if SANITIZER_ANDROID
INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
  // FIXME: under ASan the call below may write to freed memory and corrupt
  // its metadata. See
  // https://github.com/google/sanitizers/issues/321.
  int res =
      COMMON_INTERCEPTOR_BLOCK_REAL(__wait4)(pid, status, options, rusage);
  if (res != -1) {
    if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
    if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  }
  return res;
}
#define INIT_WAIT4
#else
INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {}
#define INIT_WAIT4
#endif  // SANITIZER_ANDROID
#define INIT_WAIT
#else
#define INIT_WAIT
#define INIT_WAIT4
#endif

#if SANITIZER_INTERCEPT_INET
INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {}
INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {}
#define INIT_INET
#else
#define INIT_INET
#endif

#if SANITIZER_INTERCEPT_INET
INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {}
#define INIT_INET_ATON
#else
#define INIT_INET_ATON
#endif

#if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {}
#define INIT_PTHREAD_GETSCHEDPARAM
#else
#define INIT_PTHREAD_GETSCHEDPARAM
#endif

#if SANITIZER_INTERCEPT_GETADDRINFO
INTERCEPTOR(int, getaddrinfo, char *node, char *service,
            struct __sanitizer_addrinfo *hints,
            struct __sanitizer_addrinfo **out) {}
#define INIT_GETADDRINFO
#else
#define INIT_GETADDRINFO
#endif

#if SANITIZER_INTERCEPT_GETNAMEINFO
INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
            unsigned hostlen, char *serv, unsigned servlen, int flags) {}
#define INIT_GETNAMEINFO
#else
#define INIT_GETNAMEINFO
#endif

#if SANITIZER_INTERCEPT_GETSOCKNAME
INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {}
#define INIT_GETSOCKNAME
#else
#define INIT_GETSOCKNAME
#endif

#if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {}
#endif

#if SANITIZER_INTERCEPT_GETHOSTBYNAME
INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {}

INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
            int type) {}

INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {}
#define INIT_GETHOSTBYNAME
#else
#define INIT_GETHOSTBYNAME
#endif  // SANITIZER_INTERCEPT_GETHOSTBYNAME

#if SANITIZER_INTERCEPT_GETHOSTBYNAME2
INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {}
#define INIT_GETHOSTBYNAME2
#else
#define INIT_GETHOSTBYNAME2
#endif  // SANITIZER_INTERCEPT_GETHOSTBYNAME2

#if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
            char *buf, SIZE_T buflen, __sanitizer_hostent **result,
            int *h_errnop) {}
#define INIT_GETHOSTBYNAME_R
#else
#define INIT_GETHOSTBYNAME_R
#endif

#if SANITIZER_INTERCEPT_GETHOSTENT_R
INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
            SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {}
#define INIT_GETHOSTENT_R
#else
#define INIT_GETHOSTENT_R
#endif

#if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
            struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
            __sanitizer_hostent **result, int *h_errnop) {}
#define INIT_GETHOSTBYADDR_R
#else
#define INIT_GETHOSTBYADDR_R
#endif

#if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
            struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
            __sanitizer_hostent **result, int *h_errnop) {}
#define INIT_GETHOSTBYNAME2_R
#else
#define INIT_GETHOSTBYNAME2_R
#endif

#if SANITIZER_INTERCEPT_GETSOCKOPT
INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
            int *optlen) {}
#define INIT_GETSOCKOPT
#else
#define INIT_GETSOCKOPT
#endif

#if SANITIZER_INTERCEPT_ACCEPT
INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {}
#define INIT_ACCEPT
#else
#define INIT_ACCEPT
#endif

#if SANITIZER_INTERCEPT_ACCEPT4
INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {}
#define INIT_ACCEPT4
#else
#define INIT_ACCEPT4
#endif

#if SANITIZER_INTERCEPT_PACCEPT
INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
            __sanitizer_sigset_t *set, int f) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
  unsigned addrlen0 = 0;
  if (addrlen) {
    COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
    addrlen0 = *addrlen;
  }
  if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  int fd2 = COMMON_INTERCEPTOR_BLOCK_REAL(paccept)(fd, addr, addrlen, set, f);
  if (fd2 >= 0) {
    if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
    if (addr && addrlen)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
  }
  return fd2;
}
#define INIT_PACCEPT
#else
#define INIT_PACCEPT
#endif

#if SANITIZER_INTERCEPT_MODF
INTERCEPTOR(double, modf, double x, double *iptr) {}
INTERCEPTOR(float, modff, float x, float *iptr) {}
INTERCEPTOR(long double, modfl, long double x, long double *iptr) {}
#define INIT_MODF
#else
#define INIT_MODF
#endif

#if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
                         SSIZE_T maxlen) {}
#endif

#if SANITIZER_INTERCEPT_RECVMSG
INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
            int flags) {}
#define INIT_RECVMSG
#else
#define INIT_RECVMSG
#endif

#if SANITIZER_INTERCEPT_RECVMMSG
INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
            unsigned int vlen, int flags, void *timeout) {}
#define INIT_RECVMMSG
#else
#define INIT_RECVMMSG
#endif

#if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
static void read_msghdr_control(void *ctx, void *control, uptr controllen) {}

static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
                        SSIZE_T maxlen) {}
#endif

#if SANITIZER_INTERCEPT_SENDMSG
INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
            int flags) {}
#define INIT_SENDMSG
#else
#define INIT_SENDMSG
#endif

#if SANITIZER_INTERCEPT_SENDMMSG
INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
            unsigned vlen, int flags) {}
#define INIT_SENDMMSG
#else
#define INIT_SENDMMSG
#endif

#if SANITIZER_INTERCEPT_SYSMSG
INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
            int msgflg) {}

INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
            long msgtyp, int msgflg) {}

#define INIT_SYSMSG
#else
#define INIT_SYSMSG
#endif

#if SANITIZER_INTERCEPT_GETPEERNAME
INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {}
#define INIT_GETPEERNAME
#else
#define INIT_GETPEERNAME
#endif

#if SANITIZER_INTERCEPT_SYSINFO
INTERCEPTOR(int, sysinfo, void *info) {}
#define INIT_SYSINFO
#else
#define INIT_SYSINFO
#endif

#if SANITIZER_INTERCEPT_READDIR
INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {}

INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {}

INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
            __sanitizer_dirent **result) {}

#define INIT_READDIR
#else
#define INIT_READDIR
#endif

#if SANITIZER_INTERCEPT_READDIR64
INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {}

INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
            __sanitizer_dirent64 **result) {}
#define INIT_READDIR64
#else
#define INIT_READDIR64
#endif

#if SANITIZER_INTERCEPT_PTRACE
INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {}

#define INIT_PTRACE
#else
#define INIT_PTRACE
#endif

#if SANITIZER_INTERCEPT_SETLOCALE
static void unpoison_ctype_arrays(void *ctx) {}

INTERCEPTOR(char *, setlocale, int category, char *locale) {}

#define INIT_SETLOCALE
#else
#define INIT_SETLOCALE
#endif

#if SANITIZER_INTERCEPT_GETCWD
INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {}
#define INIT_GETCWD
#else
#define INIT_GETCWD
#endif

#if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
INTERCEPTOR(char *, get_current_dir_name, int fake) {}

#define INIT_GET_CURRENT_DIR_NAME
#else
#define INIT_GET_CURRENT_DIR_NAME
#endif

UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {}

UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
                             char **endptr, char *real_endptr, int base) {}

#if SANITIZER_INTERCEPT_STRTOIMAX
template <typename Fn>
static ALWAYS_INLINE auto StrtoimaxImpl(void *ctx, Fn real, const char *nptr,
                                        char **endptr, int base)
    -> decltype(real(nullptr, nullptr, 0)) {}

INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {}
INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {}

#define INIT_STRTOIMAX
#else
#define INIT_STRTOIMAX
#endif

#if SANITIZER_INTERCEPT_STRTOIMAX && SANITIZER_GLIBC
INTERCEPTOR(INTMAX_T, __isoc23_strtoimax, const char *nptr, char **endptr, int base) {}
INTERCEPTOR(UINTMAX_T, __isoc23_strtoumax, const char *nptr, char **endptr, int base) {}

#define INIT_STRTOIMAX_C23
#else
#define INIT_STRTOIMAX_C23
#endif

#if SANITIZER_INTERCEPT_MBSTOWCS
INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {}

INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
            void *ps) {}

#define INIT_MBSTOWCS
#else
#define INIT_MBSTOWCS
#endif

#if SANITIZER_INTERCEPT_MBSNRTOWCS
INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
            SIZE_T len, void *ps) {}

#define INIT_MBSNRTOWCS
#else
#define INIT_MBSNRTOWCS
#endif

#if SANITIZER_INTERCEPT_WCSTOMBS
INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {}

INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
            void *ps) {}

#define INIT_WCSTOMBS
#else
#define INIT_WCSTOMBS
#endif

#if SANITIZER_INTERCEPT_WCSNRTOMBS
INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
            SIZE_T len, void *ps) {}

#define INIT_WCSNRTOMBS
#else
#define INIT_WCSNRTOMBS
#endif


#if SANITIZER_INTERCEPT_WCRTOMB
INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {}

#define INIT_WCRTOMB
#else
#define INIT_WCRTOMB
#endif

#if SANITIZER_INTERCEPT_WCTOMB
INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {}

#define INIT_WCTOMB
#else
#define INIT_WCTOMB
#endif

#if SANITIZER_INTERCEPT_TCGETATTR
INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {}

#define INIT_TCGETATTR
#else
#define INIT_TCGETATTR
#endif

#if SANITIZER_INTERCEPT_REALPATH
INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {}
#define INIT_REALPATH
#else
#define INIT_REALPATH
#endif

#if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
INTERCEPTOR(char *, canonicalize_file_name, const char *path) {}
#define INIT_CANONICALIZE_FILE_NAME
#else
#define INIT_CANONICALIZE_FILE_NAME
#endif

#if SANITIZER_INTERCEPT_CONFSTR
INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {}
#define INIT_CONFSTR
#else
#define INIT_CONFSTR
#endif

#if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {}
#define INIT_SCHED_GETAFFINITY
#else
#define INIT_SCHED_GETAFFINITY
#endif

#if SANITIZER_INTERCEPT_SCHED_GETPARAM
INTERCEPTOR(int, sched_getparam, int pid, void *param) {}
#define INIT_SCHED_GETPARAM
#else
#define INIT_SCHED_GETPARAM
#endif

#if SANITIZER_INTERCEPT_STRERROR
INTERCEPTOR(char *, strerror, int errnum) {}
#define INIT_STRERROR
#else
#define INIT_STRERROR
#endif

#if SANITIZER_INTERCEPT_STRERROR_R
// There are 2 versions of strerror_r:
//  * POSIX version returns 0 on success, negative error code on failure,
//    writes message to buf.
//  * GNU version returns message pointer, which points to either buf or some
//    static storage.
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
    SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD ||                 \
    SANITIZER_FREEBSD
// POSIX version. Spec is not clear on whether buf is NULL-terminated.
// At least on OSX, buf contents are valid even when the call fails.
INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
  // FIXME: under ASan the call below may write to freed memory and corrupt
  // its metadata. See
  // https://github.com/google/sanitizers/issues/321.
  int res = REAL(strerror_r)(errnum, buf, buflen);

  SIZE_T sz = internal_strnlen(buf, buflen);
  if (sz < buflen) ++sz;
  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
  return res;
}
#else
// GNU version.
INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {}
#endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
       //SANITIZER_APPLE
#define INIT_STRERROR_R
#else
#define INIT_STRERROR_R
#endif

#if SANITIZER_INTERCEPT_XPG_STRERROR_R
INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {}
#define INIT_XPG_STRERROR_R
#else
#define INIT_XPG_STRERROR_R
#endif

#if SANITIZER_INTERCEPT_SCANDIR
scandir_filter_f;
scandir_compar_f;

static THREADLOCAL scandir_filter_f scandir_filter;
static THREADLOCAL scandir_compar_f scandir_compar;

static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {}

static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
                                  const struct __sanitizer_dirent **b) {}

INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
            scandir_filter_f filter, scandir_compar_f compar) {}
#define INIT_SCANDIR
#else
#define INIT_SCANDIR
#endif

#if SANITIZER_INTERCEPT_SCANDIR64
scandir64_filter_f;
scandir64_compar_f;

static THREADLOCAL scandir64_filter_f scandir64_filter;
static THREADLOCAL scandir64_compar_f scandir64_compar;

static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {}

static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
                                    const struct __sanitizer_dirent64 **b) {}

INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
            scandir64_filter_f filter, scandir64_compar_f compar) {}
#define INIT_SCANDIR64
#else
#define INIT_SCANDIR64
#endif

#if SANITIZER_INTERCEPT_GETGROUPS
INTERCEPTOR(int, getgroups, int size, u32 *lst) {}
#define INIT_GETGROUPS
#else
#define INIT_GETGROUPS
#endif

#if SANITIZER_INTERCEPT_POLL
static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
                        __sanitizer_nfds_t nfds) {}

static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
                         __sanitizer_nfds_t nfds) {}

INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
            int timeout) {}
#define INIT_POLL
#else
#define INIT_POLL
#endif

#if SANITIZER_INTERCEPT_PPOLL
INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
            void *timeout_ts, __sanitizer_sigset_t *sigmask) {}
#define INIT_PPOLL
#else
#define INIT_PPOLL
#endif

#if SANITIZER_INTERCEPT_WORDEXP
INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {}
#define INIT_WORDEXP
#else
#define INIT_WORDEXP
#endif

#if SANITIZER_INTERCEPT_SIGWAIT
INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {}
#define INIT_SIGWAIT
#else
#define INIT_SIGWAIT
#endif

#if SANITIZER_INTERCEPT_SIGWAITINFO
INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {}
#define INIT_SIGWAITINFO
#else
#define INIT_SIGWAITINFO
#endif

#if SANITIZER_INTERCEPT_SIGTIMEDWAIT
INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
            void *timeout) {}
#define INIT_SIGTIMEDWAIT
#else
#define INIT_SIGTIMEDWAIT
#endif

#if SANITIZER_INTERCEPT_SIGSETOPS
INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {}

INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {}
#define INIT_SIGSETOPS
#else
#define INIT_SIGSETOPS
#endif

#if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
            __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {}

INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
            __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {}
#define INIT_SIGSET_LOGICOPS
#else
#define INIT_SIGSET_LOGICOPS
#endif

#if SANITIZER_INTERCEPT_SIGPENDING
INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {}
#define INIT_SIGPENDING
#else
#define INIT_SIGPENDING
#endif

#if SANITIZER_INTERCEPT_SIGPROCMASK
INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
            __sanitizer_sigset_t *oldset) {}
#define INIT_SIGPROCMASK
#else
#define INIT_SIGPROCMASK
#endif

#if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
            __sanitizer_sigset_t *oldset) {}
#define INIT_PTHREAD_SIGMASK
#else
#define INIT_PTHREAD_SIGMASK
#endif

#if SANITIZER_INTERCEPT_BACKTRACE
INTERCEPTOR(int, backtrace, void **buffer, int size) {}

INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {}
#define INIT_BACKTRACE
#else
#define INIT_BACKTRACE
#endif

#if SANITIZER_INTERCEPT__EXIT
INTERCEPTOR(void, _exit, int status) {}
#define INIT__EXIT
#else
#define INIT__EXIT
#endif

#if SANITIZER_INTERCEPT___LIBC_MUTEX
INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
ALIAS(WRAP(pthread_setcancelstate));

#define INIT___LIBC_THR_SETCANCELSTATE
#else
#define INIT___LIBC_THR_SETCANCELSTATE
#endif

#if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {}
#endif

#if SANITIZER_INTERCEPT_GETMNTENT
INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {}
#define INIT_GETMNTENT
#else
#define INIT_GETMNTENT
#endif

#if SANITIZER_INTERCEPT_GETMNTENT_R
INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
            __sanitizer_mntent *mntbuf, char *buf, int buflen) {}
#define INIT_GETMNTENT_R
#else
#define INIT_GETMNTENT_R
#endif

#if SANITIZER_INTERCEPT_STATFS
INTERCEPTOR(int, statfs, char *path, void *buf) {}
INTERCEPTOR(int, fstatfs, int fd, void *buf) {}
#define INIT_STATFS
#else
#define INIT_STATFS
#endif

#if SANITIZER_INTERCEPT_STATFS64
INTERCEPTOR(int, statfs64, char *path, void *buf) {}
INTERCEPTOR(int, fstatfs64, int fd, void *buf) {}
#define INIT_STATFS64
#else
#define INIT_STATFS64
#endif

#if SANITIZER_INTERCEPT_STATVFS
INTERCEPTOR(int, statvfs, char *path, void *buf) {}
INTERCEPTOR(int, fstatvfs, int fd, void *buf) {}
#define INIT_STATVFS
#else
#define INIT_STATVFS
#endif

#if SANITIZER_INTERCEPT_STATVFS64
INTERCEPTOR(int, statvfs64, char *path, void *buf) {}
INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {}
#define INIT_STATVFS64
#else
#define INIT_STATVFS64
#endif

#if SANITIZER_INTERCEPT_INITGROUPS
INTERCEPTOR(int, initgroups, char *user, u32 group) {}
#define INIT_INITGROUPS
#else
#define INIT_INITGROUPS
#endif

#if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {}
INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {}
#define INIT_ETHER_NTOA_ATON
#else
#define INIT_ETHER_NTOA_ATON
#endif

#if SANITIZER_INTERCEPT_ETHER_HOST
INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {}
INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {}
INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
            char *hostname) {}
#define INIT_ETHER_HOST
#else
#define INIT_ETHER_HOST
#endif

#if SANITIZER_INTERCEPT_ETHER_R
INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {}
INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
            __sanitizer_ether_addr *addr) {}
#define INIT_ETHER_R
#else
#define INIT_ETHER_R
#endif

#if SANITIZER_INTERCEPT_SHMCTL
INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {}
#define INIT_SHMCTL
#else
#define INIT_SHMCTL
#endif

#if SANITIZER_INTERCEPT_RANDOM_R
INTERCEPTOR(int, random_r, void *buf, u32 *result) {}
#define INIT_RANDOM_R
#else
#define INIT_RANDOM_R
#endif

// FIXME: under ASan the REAL() call below may write to freed memory and corrupt
// its metadata. See
// https://github.com/google/sanitizers/issues/321.
#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET ||              \
    SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED ||        \
    SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
    SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET ||         \
    SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET ||        \
    SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET ||          \
    SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
#define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz)
#define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz)
#define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz)
#define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz)
#define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz)
#define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz)
#endif

#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
INTERCEPTOR_PTHREAD_ATTR_GET()
INTERCEPTOR_PTHREAD_ATTR_GET()
INTERCEPTOR_PTHREAD_ATTR_GET()
INTERCEPTOR_PTHREAD_ATTR_GET()
INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {}

// We may need to call the real pthread_attr_getstack from the run-time
// in sanitizer_common, but we don't want to include the interception headers
// there. So, just define this function here.
namespace __sanitizer {
extern "C" {
int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {}
}  // extern "C"
}  // namespace __sanitizer

#define INIT_PTHREAD_ATTR_GET
#else
#define INIT_PTHREAD_ATTR_GET
#endif

#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
INTERCEPTOR_PTHREAD_ATTR_GET()
INTERCEPTOR_PTHREAD_ATTR_GET()

#define INIT_PTHREAD_ATTR_GET_SCHED
#else
#define INIT_PTHREAD_ATTR_GET_SCHED
#endif

#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
INTERCEPTOR_PTHREAD_ATTR_GET()

#define INIT_PTHREAD_ATTR_GETINHERITSCHED
#else
#define INIT_PTHREAD_ATTR_GETINHERITSCHED
#endif

#if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
            void *cpuset) {}

#define INIT_PTHREAD_ATTR_GETAFFINITY_NP
#else
#define INIT_PTHREAD_ATTR_GETAFFINITY_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
            void *cpuset) {}

#define INIT_PTHREAD_GETAFFINITY_NP
#else
#define INIT_PTHREAD_GETAFFINITY_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
INTERCEPTOR_PTHREAD_MUTEXATTR_GET()
#define INIT_PTHREAD_MUTEXATTR_GETPSHARED
#else
#define INIT_PTHREAD_MUTEXATTR_GETPSHARED
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
INTERCEPTOR_PTHREAD_MUTEXATTR_GET()
#define INIT_PTHREAD_MUTEXATTR_GETTYPE
#else
#define INIT_PTHREAD_MUTEXATTR_GETTYPE
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
INTERCEPTOR_PTHREAD_MUTEXATTR_GET()
#define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
#else
#define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
INTERCEPTOR_PTHREAD_MUTEXATTR_GET()
#define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
#else
#define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
INTERCEPTOR_PTHREAD_MUTEXATTR_GET()
#define INIT_PTHREAD_MUTEXATTR_GETROBUST
#else
#define INIT_PTHREAD_MUTEXATTR_GETROBUST
#endif

#if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
INTERCEPTOR_PTHREAD_MUTEXATTR_GET()
#define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
#else
#define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
INTERCEPTOR_PTHREAD_RWLOCKATTR_GET()
#define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
#else
#define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
#endif

#if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
INTERCEPTOR_PTHREAD_RWLOCKATTR_GET()
#define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
#else
#define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
INTERCEPTOR_PTHREAD_CONDATTR_GET()
#define INIT_PTHREAD_CONDATTR_GETPSHARED
#else
#define INIT_PTHREAD_CONDATTR_GETPSHARED
#endif

#if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
INTERCEPTOR_PTHREAD_CONDATTR_GET()
#define INIT_PTHREAD_CONDATTR_GETCLOCK
#else
#define INIT_PTHREAD_CONDATTR_GETCLOCK
#endif

#if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
INTERCEPTOR_PTHREAD_BARRIERATTR_GET() // !mac !android
#define INIT_PTHREAD_BARRIERATTR_GETPSHARED
#else
#define INIT_PTHREAD_BARRIERATTR_GETPSHARED
#endif

#if SANITIZER_INTERCEPT_TMPNAM
INTERCEPTOR(char *, tmpnam, char *s) {}
#define INIT_TMPNAM
#else
#define INIT_TMPNAM
#endif

#if SANITIZER_INTERCEPT_TMPNAM_R
INTERCEPTOR(char *, tmpnam_r, char *s) {}
#define INIT_TMPNAM_R
#else
#define INIT_TMPNAM_R
#endif

#if SANITIZER_INTERCEPT_PTSNAME
INTERCEPTOR(char *, ptsname, int fd) {}
#define INIT_PTSNAME
#else
#define INIT_PTSNAME
#endif

#if SANITIZER_INTERCEPT_PTSNAME_R
INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {}
#define INIT_PTSNAME_R
#else
#define INIT_PTSNAME_R
#endif

#if SANITIZER_INTERCEPT_TTYNAME
INTERCEPTOR(char *, ttyname, int fd) {}
#define INIT_TTYNAME
#else
#define INIT_TTYNAME
#endif

#if SANITIZER_INTERCEPT_TTYNAME_R
INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {}
#define INIT_TTYNAME_R
#else
#define INIT_TTYNAME_R
#endif

#if SANITIZER_INTERCEPT_TEMPNAM
INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {}
#define INIT_TEMPNAM
#else
#define INIT_TEMPNAM
#endif

#if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {}
#define INIT_PTHREAD_SETNAME_NP
#elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
  void *ctx;
  char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
  COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
  COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
  internal_snprintf(newname, sizeof(newname), name, arg);
  COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
  return REAL(pthread_setname_np)(thread, name, arg);
}
#define INIT_PTHREAD_SETNAME_NP
#else
#define INIT_PTHREAD_SETNAME_NP
#endif

#if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {}
#define INIT_PTHREAD_GETNAME_NP
#else
#define INIT_PTHREAD_GETNAME_NP
#endif

#if SANITIZER_INTERCEPT_SINCOS
INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {}
INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {}
INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {}
#define INIT_SINCOS
#else
#define INIT_SINCOS
#endif

#if SANITIZER_INTERCEPT_REMQUO
INTERCEPTOR(double, remquo, double x, double y, int *quo) {}
INTERCEPTOR(float, remquof, float x, float y, int *quo) {}
#define INIT_REMQUO
#else
#define INIT_REMQUO
#endif

#if SANITIZER_INTERCEPT_REMQUOL
INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {}
#define INIT_REMQUOL
#else
#define INIT_REMQUOL
#endif

#if SANITIZER_INTERCEPT_LGAMMA
extern int signgam;
INTERCEPTOR(double, lgamma, double x) {}
INTERCEPTOR(float, lgammaf, float x) {}
#define INIT_LGAMMA
#else
#define INIT_LGAMMA
#endif

#if SANITIZER_INTERCEPT_LGAMMAL
INTERCEPTOR(long double, lgammal, long double x) {}
#define INIT_LGAMMAL
#else
#define INIT_LGAMMAL
#endif

#if SANITIZER_INTERCEPT_LGAMMA_R
INTERCEPTOR(double, lgamma_r, double x, int *signp) {}
INTERCEPTOR(float, lgammaf_r, float x, int *signp) {}
#define INIT_LGAMMA_R
#else
#define INIT_LGAMMA_R
#endif

#if SANITIZER_INTERCEPT_LGAMMAL_R
INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {}
#define INIT_LGAMMAL_R
#else
#define INIT_LGAMMAL_R
#endif

#if SANITIZER_INTERCEPT_DRAND48_R
INTERCEPTOR(int, drand48_r, void *buffer, double *result) {}
INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {}
#define INIT_DRAND48_R
#else
#define INIT_DRAND48_R
#endif

#if SANITIZER_INTERCEPT_RAND_R
INTERCEPTOR(int, rand_r, unsigned *seedp) {}
#define INIT_RAND_R
#else
#define INIT_RAND_R
#endif

#if SANITIZER_INTERCEPT_GETLINE
INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {}

// FIXME: under ASan the call below may write to freed memory and corrupt its
// metadata. See
// https://github.com/google/sanitizers/issues/321.
#define GETDELIM_INTERCEPTOR_IMPL(vname)

INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
            void *stream)
GETDELIM_INTERCEPTOR_IMPL(__getdelim)

// There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
// with its own body.
INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
            void *stream)
GETDELIM_INTERCEPTOR_IMPL(getdelim)

#define INIT_GETLINE
#else
#define INIT_GETLINE
#endif

#if SANITIZER_INTERCEPT_ICONV
INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
            char **outbuf, SIZE_T *outbytesleft) {}
#define INIT_ICONV
#else
#define INIT_ICONV
#endif

#if SANITIZER_INTERCEPT_TIMES
INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {}
#define INIT_TIMES
#else
#define INIT_TIMES
#endif

#if SANITIZER_S390 && \
    (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
DEFINE_REAL(uptr, __tls_get_offset, void *arg)
#endif

#if SANITIZER_INTERCEPT_TLS_GET_ADDR
#if !SANITIZER_S390
#define INIT_TLS_GET_ADDR
// If you see any crashes around this functions, there are 2 known issues with
// it: 1. __tls_get_addr can be called with mis-aligned stack due to:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
// 2. It can be called recursively if sanitizer code uses __tls_get_addr
// to access thread local variables (it should not happen normally,
// because sanitizers use initial-exec tls model).
INTERCEPTOR(void *, __tls_get_addr, void *arg) {}
#if SANITIZER_PPC
// On PowerPC, we also need to intercept __tls_get_addr_opt, which has
// mostly the same semantics as __tls_get_addr, but its presence enables
// some optimizations in linker (which are safe to ignore here).
INTERCEPTOR(void *, __tls_get_addr_opt, void *arg) ALIAS(WRAP(__tls_get_addr));
#endif
#else // SANITIZER_S390
// On s390, we have to intercept two functions here:
// - __tls_get_addr_internal, which is a glibc-internal function that is like
//   the usual __tls_get_addr, but returns a TP-relative offset instead of
//   a proper pointer.  It is used by dlsym for TLS symbols.
// - __tls_get_offset, which is like the above, but also takes a GOT-relative
//   descriptor offset as an argument instead of a pointer.  GOT address
//   is passed in r12, so it's necessary to write it in assembly.  This is
//   the function used by the compiler.
#define INIT_TLS_GET_ADDR
INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
  uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
  uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
  void *ptr = reinterpret_cast<void *>(res + tp);
  uptr tls_begin, tls_end;
  COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
  DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
  if (dtv) {
    // New DTLS block has been allocated.
    COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
  }
  return res;
}
#endif // SANITIZER_S390
#else
#define INIT_TLS_GET_ADDR
#endif

#if SANITIZER_S390 && \
    (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
// We need a hidden symbol aliasing the above, so that we can jump
// directly to it from the assembly below.
extern "C" __attribute__((visibility("hidden"))) uptr __tls_get_addr_hidden(
    void *arg) ALIAS(WRAP(__tls_get_addr_internal));
extern "C" uptr __tls_get_offset(void *arg);
extern "C" uptr TRAMPOLINE(__tls_get_offset)(void *arg);
extern "C" uptr WRAP(__tls_get_offset)(void *arg);
// Now carefully intercept __tls_get_offset.
asm(
  ".text\n"
// The __intercept_ version has to exist, so that gen_dynamic_list.py
// exports our symbol.
  ".weak __tls_get_offset\n"
  ".set __tls_get_offset, __interceptor___tls_get_offset\n"
  ".global __interceptor___tls_get_offset\n"
  ".type __interceptor___tls_get_offset, @function\n"
  "__interceptor___tls_get_offset:\n"
#ifdef __s390x__
  "la %r2, 0(%r2,%r12)\n"
  "jg __tls_get_addr_hidden\n"
#else
  "basr %r3,0\n"
  "0: la %r2,0(%r2,%r12)\n"
  "l %r4,1f-0b(%r3)\n"
  "b 0(%r4,%r3)\n"
  "1: .long __tls_get_addr_hidden - 0b\n"
#endif
  ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
// Assembly wrapper to call REAL(__tls_get_offset)(arg)
  ".type __tls_get_offset_wrapper, @function\n"
  "__tls_get_offset_wrapper:\n"
#ifdef __s390x__
  "sgr %r2,%r12\n"
#else
  "sr %r2,%r12\n"
#endif
  "br %r3\n"
  ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
);
#endif

#if SANITIZER_INTERCEPT_LISTXATTR
INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {}
INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {}
INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {}
#define INIT_LISTXATTR
#else
#define INIT_LISTXATTR
#endif

#if SANITIZER_INTERCEPT_GETXATTR
INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
            SIZE_T size) {}
INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
            SIZE_T size) {}
INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
            SIZE_T size) {}
#define INIT_GETXATTR
#else
#define INIT_GETXATTR
#endif

#if SANITIZER_INTERCEPT_GETRESID
INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {}
INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {}
#define INIT_GETRESID
#else
#define INIT_GETRESID
#endif

#if SANITIZER_INTERCEPT_GETIFADDRS
// As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
// intercept freeifaddrs(). If that ceases to be the case, we might need to
// intercept it to poison the memory again.
INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {}
#define INIT_GETIFADDRS
#else
#define INIT_GETIFADDRS
#endif

#if SANITIZER_INTERCEPT_IF_INDEXTONAME
INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {}
INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {}
#define INIT_IF_INDEXTONAME
#else
#define INIT_IF_INDEXTONAME
#endif

#if SANITIZER_INTERCEPT_CAPGET
INTERCEPTOR(int, capget, void *hdrp, void *datap) {}
INTERCEPTOR(int, capset, void *hdrp, const void *datap) {}
#define INIT_CAPGET
#else
#define INIT_CAPGET
#endif

#if SANITIZER_INTERCEPT_FTIME
INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {}
#define INIT_FTIME
#else
#define INIT_FTIME
#endif  // SANITIZER_INTERCEPT_FTIME

#if SANITIZER_INTERCEPT_XDR
INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
            unsigned size, int op) {}

INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {}

// FIXME: under ASan the call below may write to freed memory and corrupt
// its metadata. See
// https://github.com/google/sanitizers/issues/321.
#define XDR_INTERCEPTOR(F, T)

XDR_INTERCEPTOR(xdr_short, short)
XDR_INTERCEPTOR(xdr_u_short, unsigned short)
XDR_INTERCEPTOR(xdr_int, int)
XDR_INTERCEPTOR(xdr_u_int, unsigned)
XDR_INTERCEPTOR(xdr_long, long)
XDR_INTERCEPTOR(xdr_u_long, unsigned long)
XDR_INTERCEPTOR(xdr_hyper, long long)
XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
XDR_INTERCEPTOR(xdr_longlong_t, long long)
XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
XDR_INTERCEPTOR()
XDR_INTERCEPTOR()
XDR_INTERCEPTOR()
XDR_INTERCEPTOR()
XDR_INTERCEPTOR(xdr_int32_t, u32)
XDR_INTERCEPTOR(xdr_uint32_t, u32)
XDR_INTERCEPTOR(xdr_int64_t, u64)
XDR_INTERCEPTOR(xdr_uint64_t, u64)
XDR_INTERCEPTOR(xdr_quad_t, long long)
XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
XDR_INTERCEPTOR(xdr_bool, bool)
XDR_INTERCEPTOR(xdr_enum, int)
XDR_INTERCEPTOR(xdr_char, char)
XDR_INTERCEPTOR(xdr_u_char, unsigned char)
XDR_INTERCEPTOR(xdr_float, float)
XDR_INTERCEPTOR(xdr_double, double)

// FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
// wrapstring, sizeof

INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
            unsigned maxsize) {}

INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
            unsigned maxsize) {}

#define INIT_XDR
#else
#define INIT_XDR
#endif  // SANITIZER_INTERCEPT_XDR

#if SANITIZER_INTERCEPT_XDRREC
xdrrec_cb;
struct XdrRecWrapper {};
XdrRecWrapMap;
static XdrRecWrapMap *xdrrec_wrap_map;

static int xdrrec_wr_wrap(char *handle, char *buf, int count) {}

static int xdrrec_rd_wrap(char *handle, char *buf, int count) {}

// This doesn't apply to the solaris version as it has a different function
// signature.
INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
            unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
            int (*wr)(char*, char*, int)) {}

// We have to intercept this to be able to free wrapper memory;
// otherwise it's not necessary.
INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {}
#define INIT_XDRREC_LINUX
#else
#define INIT_XDRREC_LINUX
#endif

#if SANITIZER_INTERCEPT_TSEARCH
INTERCEPTOR(void *, tsearch, void *key, void **rootp,
            int (*compar)(const void *, const void *)) {}
#define INIT_TSEARCH
#else
#define INIT_TSEARCH
#endif

#if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
    SANITIZER_INTERCEPT_OPEN_MEMSTREAM
void unpoison_file(__sanitizer_FILE *fp) {}
#endif

#if SANITIZER_INTERCEPT_LIBIO_INTERNALS
// These guys are called when a .c source is built with -O2.
INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {}
INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {}
INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {}
INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {}
INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {}
INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {}
#define INIT_LIBIO_INTERNALS
#else
#define INIT_LIBIO_INTERNALS
#endif

#if SANITIZER_INTERCEPT_FOPEN
INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {}
INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {}
INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
            __sanitizer_FILE *fp) {}
#define INIT_FOPEN
#else
#define INIT_FOPEN
#endif

#if SANITIZER_INTERCEPT_FLOPEN
INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
  void *ctx;
  va_list ap;
  va_start(ap, flags);
  u16 mode = static_cast<u16>(va_arg(ap, u32));
  va_end(ap);
  COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
  if (path) {
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  }
  return COMMON_INTERCEPTOR_BLOCK_REAL(flopen)(path, flags, mode);
}

INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
  void *ctx;
  va_list ap;
  va_start(ap, flags);
  u16 mode = static_cast<u16>(va_arg(ap, u32));
  va_end(ap);
  COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
  if (path) {
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  }
  return COMMON_INTERCEPTOR_BLOCK_REAL(flopenat)(dirfd, path, flags, mode);
}

#define INIT_FLOPEN
#else
#define INIT_FLOPEN
#endif

#if SANITIZER_INTERCEPT_FOPEN64
INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {}
INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
            __sanitizer_FILE *fp) {}
#define INIT_FOPEN64
#else
#define INIT_FOPEN64
#endif

#if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {}
INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
            SIZE_T *sizeloc) {}
INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
            const char *mode) {}
#define INIT_OPEN_MEMSTREAM
#else
#define INIT_OPEN_MEMSTREAM
#endif

#if SANITIZER_INTERCEPT_OBSTACK
static void initialize_obstack(__sanitizer_obstack *obstack) {}

INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
            int align, void *(*alloc_fn)(uptr arg, uptr sz),
            void (*free_fn)(uptr arg, void *p)) {}
INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
            int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {}
INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {}
#define INIT_OBSTACK
#else
#define INIT_OBSTACK
#endif

#if SANITIZER_INTERCEPT_FFLUSH
INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {}
#define INIT_FFLUSH
#else
#define INIT_FFLUSH
#endif

#if SANITIZER_INTERCEPT_FCLOSE
INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {}
#define INIT_FCLOSE
#else
#define INIT_FCLOSE
#endif

#if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
INTERCEPTOR(void*, dlopen, const char *filename, int flag) {}

INTERCEPTOR(int, dlclose, void *handle) {}
#define INIT_DLOPEN_DLCLOSE
#else
#define INIT_DLOPEN_DLCLOSE
#endif

#if SANITIZER_INTERCEPT_GETPASS
INTERCEPTOR(char *, getpass, const char *prompt) {}

#define INIT_GETPASS
#else
#define INIT_GETPASS
#endif

#if SANITIZER_INTERCEPT_TIMERFD
INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
            void *old_value) {}

INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {}
#define INIT_TIMERFD
#else
#define INIT_TIMERFD
#endif

#if SANITIZER_INTERCEPT_MLOCKX
// Linux kernel has a bug that leads to kernel deadlock if a process
// maps TBs of memory and then calls mlock().
static void MlockIsUnsupported() {}

INTERCEPTOR(int, mlock, const void *addr, uptr len) {}

INTERCEPTOR(int, munlock, const void *addr, uptr len) {}

INTERCEPTOR(int, mlockall, int flags) {}

INTERCEPTOR(int, munlockall, void) {}

#define INIT_MLOCKX

#else
#define INIT_MLOCKX
#endif  // SANITIZER_INTERCEPT_MLOCKX

#if SANITIZER_INTERCEPT_FOPENCOOKIE
struct WrappedCookie {};

static uptr wrapped_read(void *cookie, char *buf, uptr size) {}

static uptr wrapped_write(void *cookie, const char *buf, uptr size) {}

static int wrapped_seek(void *cookie, u64 *offset, int whence) {}

static int wrapped_close(void *cookie) {}

INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
            __sanitizer_cookie_io_functions_t io_funcs) {}

#define INIT_FOPENCOOKIE
#else
#define INIT_FOPENCOOKIE
#endif  // SANITIZER_INTERCEPT_FOPENCOOKIE

#if SANITIZER_INTERCEPT_SEM
INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {}

INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {}

INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {}

INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {}

INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {}

INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {}

INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {}

INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {}

INTERCEPTOR(int, sem_unlink, const char *name) {}

#define INIT_SEM
#else
#define INIT_SEM
#endif  // SANITIZER_INTERCEPT_SEM

#if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {}

INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {}
#define INIT_PTHREAD_SETCANCEL
#else
#define INIT_PTHREAD_SETCANCEL
#endif

#if SANITIZER_INTERCEPT_MINCORE
INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {}
#define INIT_MINCORE
#else
#define INIT_MINCORE
#endif

#if SANITIZER_INTERCEPT_PROCESS_VM_READV
INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
            uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
            uptr flags) {}

INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
            uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
            uptr flags) {}
#define INIT_PROCESS_VM_READV
#else
#define INIT_PROCESS_VM_READV
#endif

#if SANITIZER_INTERCEPT_CTERMID
INTERCEPTOR(char *, ctermid, char *s) {}
#define INIT_CTERMID
#else
#define INIT_CTERMID
#endif

#if SANITIZER_INTERCEPT_CTERMID_R
INTERCEPTOR(char *, ctermid_r, char *s) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
  char *res = REAL(ctermid_r)(s);
  if (res) {
    COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  }
  return res;
}
#define INIT_CTERMID_R
#else
#define INIT_CTERMID_R
#endif

#if SANITIZER_INTERCEPT_RECV_RECVFROM
INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {}

INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
            void *srcaddr, int *addrlen) {}
#define INIT_RECV_RECVFROM
#else
#define INIT_RECV_RECVFROM
#endif

#if SANITIZER_INTERCEPT_SEND_SENDTO
INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {}

INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
            void *dstaddr, int addrlen) {}
#define INIT_SEND_SENDTO
#else
#define INIT_SEND_SENDTO
#endif

#if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
INTERCEPTOR(int, eventfd_read, int fd, __sanitizer_eventfd_t *value) {}
INTERCEPTOR(int, eventfd_write, int fd, __sanitizer_eventfd_t value) {}
#define INIT_EVENTFD_READ_WRITE
#else
#define INIT_EVENTFD_READ_WRITE
#endif

#if SANITIZER_INTERCEPT_STAT
INTERCEPTOR(int, stat, const char *path, void *buf) {}
#define INIT_STAT
#else
#define INIT_STAT
#endif

#if SANITIZER_INTERCEPT_STAT64
INTERCEPTOR(int, stat64, const char *path, void *buf) {}
#define INIT_STAT64
#else
#define INIT_STAT64
#endif


#if SANITIZER_INTERCEPT_LSTAT
INTERCEPTOR(int, lstat, const char *path, void *buf) {}
#define INIT_LSTAT
#else
#define INIT_LSTAT
#endif

#if SANITIZER_INTERCEPT_STAT64
INTERCEPTOR(int, lstat64, const char *path, void *buf) {}
#define INIT_LSTAT64
#else
#define INIT_LSTAT64
#endif

#if SANITIZER_INTERCEPT___XSTAT
INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {}
#define INIT___XSTAT
#else
#define INIT___XSTAT
#endif

#if SANITIZER_INTERCEPT___XSTAT64
INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {}
#define INIT___XSTAT64
#else
#define INIT___XSTAT64
#endif

#if SANITIZER_INTERCEPT___LXSTAT
INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {}
#define INIT___LXSTAT
#else
#define INIT___LXSTAT
#endif

#if SANITIZER_INTERCEPT___LXSTAT64
INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {}
#define INIT___LXSTAT64
#else
#define INIT___LXSTAT64
#endif

// FIXME: add other *stat interceptor

#if SANITIZER_INTERCEPT_UTMP
INTERCEPTOR(void *, getutent, int dummy) {}
INTERCEPTOR(void *, getutid, void *ut) {}
INTERCEPTOR(void *, getutline, void *ut) {}
#define INIT_UTMP
#else
#define INIT_UTMP
#endif

#if SANITIZER_INTERCEPT_UTMPX
INTERCEPTOR(void *, getutxent, int dummy) {}
INTERCEPTOR(void *, getutxid, void *ut) {}
INTERCEPTOR(void *, getutxline, void *ut) {}
INTERCEPTOR(void *, pututxline, const void *ut) {}
#define INIT_UTMPX
#else
#define INIT_UTMPX
#endif

#if SANITIZER_INTERCEPT_GETLOADAVG
INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {}
#define INIT_GETLOADAVG
#else
#define INIT_GETLOADAVG
#endif

#if SANITIZER_INTERCEPT_MCHECK_MPROBE
INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {}

INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {}

INTERCEPTOR(int, mprobe, void *ptr) {}
#endif

#if SANITIZER_INTERCEPT_WCSLEN
INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {}

INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {}
#define INIT_WCSLEN
#else
#define INIT_WCSLEN
#endif

#if SANITIZER_INTERCEPT_WCSCAT
INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {}

INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {}
#define INIT_WCSCAT
#else
#define INIT_WCSCAT
#endif

#if SANITIZER_INTERCEPT_WCSDUP
INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {}

#define INIT_WCSDUP
#else
#define INIT_WCSDUP
#endif

#if SANITIZER_INTERCEPT_STRXFRM
static SIZE_T RealStrLen(const char *str) {}

static SIZE_T RealStrLen(const wchar_t *str) {}

#define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...)

INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {}

INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
            void *locale) {}

#define INIT_STRXFRM
#else
#define INIT_STRXFRM
#endif

#if SANITIZER_INTERCEPT___STRXFRM_L
INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
            void *locale) {}

#define INIT___STRXFRM_L
#else
#define INIT___STRXFRM_L
#endif

#if SANITIZER_INTERCEPT_WCSXFRM
INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {}

INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
            void *locale) {}

#define INIT_WCSXFRM
#else
#define INIT_WCSXFRM
#endif

#if SANITIZER_INTERCEPT___WCSXFRM_L
INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
            void *locale) {}

#define INIT___WCSXFRM_L
#else
#define INIT___WCSXFRM_L
#endif

#if SANITIZER_INTERCEPT_ACCT
INTERCEPTOR(int, acct, const char *file) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
  if (file)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
  return REAL(acct)(file);
}
#define INIT_ACCT
#else
#define INIT_ACCT
#endif

#if SANITIZER_INTERCEPT_USER_FROM_UID
INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
  void *ctx;
  const char *user;
  COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
  user = REAL(user_from_uid)(uid, nouser);
  if (user)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
  return user;
}
#define INIT_USER_FROM_UID
#else
#define INIT_USER_FROM_UID
#endif

#if SANITIZER_INTERCEPT_UID_FROM_USER
INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
  void *ctx;
  int res;
  COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
  if (name)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  res = REAL(uid_from_user)(name, uid);
  if (uid)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
  return res;
}
#define INIT_UID_FROM_USER
#else
#define INIT_UID_FROM_USER
#endif

#if SANITIZER_INTERCEPT_GROUP_FROM_GID
INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
  void *ctx;
  const char *group;
  COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
  group = REAL(group_from_gid)(gid, nogroup);
  if (group)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
  return group;
}
#define INIT_GROUP_FROM_GID
#else
#define INIT_GROUP_FROM_GID
#endif

#if SANITIZER_INTERCEPT_GID_FROM_GROUP
INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
  void *ctx;
  int res;
  COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
  if (group)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
  res = REAL(gid_from_group)(group, gid);
  if (gid)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
  return res;
}
#define INIT_GID_FROM_GROUP
#else
#define INIT_GID_FROM_GROUP
#endif

#if SANITIZER_INTERCEPT_ACCESS
INTERCEPTOR(int, access, const char *path, int mode) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
  if (path)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  return REAL(access)(path, mode);
}
#define INIT_ACCESS
#else
#define INIT_ACCESS
#endif

#if SANITIZER_INTERCEPT_FACCESSAT
INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
  if (path)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  return REAL(faccessat)(fd, path, mode, flags);
}
#define INIT_FACCESSAT
#else
#define INIT_FACCESSAT
#endif

#if SANITIZER_INTERCEPT_GETGROUPLIST
INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
            int *ngroups) {}

#define INIT_GETGROUPLIST
#else
#define INIT_GETGROUPLIST
#endif

#if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
            int maxgrp, int *ngroups) {
  void *ctx;
  int res;
  COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
                           maxgrp, ngroups);
  if (name)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
  if (!res && groups && ngroups) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
  }
  return res;
}

#define INIT_GETGROUPMEMBERSHIP
#else
#define INIT_GETGROUPMEMBERSHIP
#endif

#if SANITIZER_INTERCEPT_READLINK
INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {}

#define INIT_READLINK
#else
#define INIT_READLINK
#endif

#if SANITIZER_INTERCEPT_READLINKAT
INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
            SIZE_T bufsiz) {}

#define INIT_READLINKAT
#else
#define INIT_READLINKAT
#endif

#if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
            struct file_handle *handle, int *mount_id, int flags) {}

#define INIT_NAME_TO_HANDLE_AT
#else
#define INIT_NAME_TO_HANDLE_AT
#endif

#if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
            int flags) {}

#define INIT_OPEN_BY_HANDLE_AT
#else
#define INIT_OPEN_BY_HANDLE_AT
#endif

#if SANITIZER_INTERCEPT_STRLCPY
INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
  void *ctx;
  SIZE_T res;
  COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
  if (src) {
    // Keep strnlen as macro argument, as macro may ignore it.
    COMMON_INTERCEPTOR_READ_STRING(
        ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
  }
  res = REAL(strlcpy)(dst, src, size);
  COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
  return res;
}

INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
  void *ctx;
  SIZE_T len = 0;
  COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
  // src is checked in the strlcpy() interceptor
  if (dst) {
    len = internal_strnlen(dst, size);
    COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
  }
  // Reuse the rest of the code in the strlcpy() interceptor
  return WRAP(strlcpy)(dst + len, src, size - len) + len;
}
#define INIT_STRLCPY
#else
#define INIT_STRLCPY
#endif

#if SANITIZER_INTERCEPT_MMAP
INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
            OFF_T off) {}

INTERCEPTOR(int, munmap, void *addr, SIZE_T sz) {}

INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {}
#define INIT_MMAP
#else
#define INIT_MMAP
#endif

#if SANITIZER_INTERCEPT_MMAP64
INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
            OFF64_T off) {}
#define INIT_MMAP64
#else
#define INIT_MMAP64
#endif

#if SANITIZER_INTERCEPT_DEVNAME
INTERCEPTOR(char *, devname, u64 dev, u32 type) {
  void *ctx;
  char *name;
  COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
  name = REAL(devname)(dev, type);
  if (name)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  return name;
}
#define INIT_DEVNAME
#else
#define INIT_DEVNAME
#endif

#if SANITIZER_INTERCEPT_DEVNAME_R
#if SANITIZER_NETBSD
#define DEVNAME_R_RETTYPE
#define DEVNAME_R_SUCCESS
#else
#define DEVNAME_R_RETTYPE
#define DEVNAME_R_SUCCESS
#endif
INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
            uptr len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
  DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
  if (DEVNAME_R_SUCCESS(res))
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
  return res;
}
#define INIT_DEVNAME_R
#else
#define INIT_DEVNAME_R
#endif

#if SANITIZER_INTERCEPT_FGETLN
INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
  char *str = REAL(fgetln)(stream, len);
  if (str && len) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
  }
  return str;
}
#define INIT_FGETLN
#else
#define INIT_FGETLN
#endif

#if SANITIZER_INTERCEPT_STRMODE
INTERCEPTOR(void, strmode, u32 mode, char *bp) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
  REAL(strmode)(mode, bp);
  if (bp)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
}
#define INIT_STRMODE
#else
#define INIT_STRMODE
#endif

#if SANITIZER_INTERCEPT_TTYENT
INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
  struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
  if (ttyent)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
  return ttyent;
}
INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
  if (name)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
  if (ttyent)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
  return ttyent;
}
#define INIT_TTYENT
#else
#define INIT_TTYENT
#endif

#if SANITIZER_INTERCEPT_TTYENTPATH
INTERCEPTOR(int, setttyentpath, char *path) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
  if (path)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  return REAL(setttyentpath)(path);
}
#define INIT_TTYENTPATH
#else
#define INIT_TTYENTPATH
#endif

#if SANITIZER_INTERCEPT_PROTOENT
static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {}

INTERCEPTOR(struct __sanitizer_protoent *, getprotoent,) {}

INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {}

INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {}
#define INIT_PROTOENT
#else
#define INIT_PROTOENT
#endif

#if SANITIZER_INTERCEPT_PROTOENT_R
INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
            char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {}

INTERCEPTOR(int, getprotobyname_r, const char *name,
            struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
            struct __sanitizer_protoent **result) {}

INTERCEPTOR(int, getprotobynumber_r, int num,
            struct __sanitizer_protoent *result_buf, char *buf,
            SIZE_T buflen, struct __sanitizer_protoent **result) {}

#define INIT_PROTOENT_R
#else
#define INIT_PROTOENT_R
#endif

#if SANITIZER_INTERCEPT_NETENT
INTERCEPTOR(struct __sanitizer_netent *, getnetent,) {}

INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {}

INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {}
#define INIT_NETENT
#else
#define INIT_NETENT
#endif

#if SANITIZER_INTERCEPT_GETMNTINFO
INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
  int cnt = REAL(getmntinfo)(mntbufp, flags);
  if (cnt > 0 && mntbufp) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
    if (*mntbufp)
#if SANITIZER_NETBSD
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
#else
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
#endif
  }
  return cnt;
}
#define INIT_GETMNTINFO
#else
#define INIT_GETMNTINFO
#endif

#if SANITIZER_INTERCEPT_MI_VECTOR_HASH
INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
            u32 hashes[3]) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
  if (key)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
  REAL(mi_vector_hash)(key, len, seed, hashes);
  if (hashes)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
}
#define INIT_MI_VECTOR_HASH
#else
#define INIT_MI_VECTOR_HASH
#endif

#if SANITIZER_INTERCEPT_SETVBUF
INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
  SIZE_T size) {}

INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {}

INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {}

INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {}
#define INIT_SETVBUF
#else
#define INIT_SETVBUF
#endif

#if SANITIZER_INTERCEPT_GETVFSSTAT
INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
  int ret = REAL(getvfsstat)(buf, bufsize, flags);
  if (buf && ret > 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
  return ret;
}
#define INIT_GETVFSSTAT
#else
#define INIT_GETVFSSTAT
#endif

#if SANITIZER_INTERCEPT_REGEX
INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {}
INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
            struct __sanitizer_regmatch *pmatch[], int eflags) {}
INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
            SIZE_T errbuf_size) {}
INTERCEPTOR(void, regfree, const void *preg) {}
#define INIT_REGEX
#else
#define INIT_REGEX
#endif

#if SANITIZER_INTERCEPT_REGEXSUB
INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
            const struct __sanitizer_regmatch *rm, const char *str) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
  if (sub)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
  // The implementation demands and hardcodes 10 elements
  if (rm)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
  if (str)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
  SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
  if (res > 0 && buf)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
  return res;
}
INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
            const struct __sanitizer_regmatch *rm, const char *sstr) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
  if (sub)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
  // Hardcode 10 elements as this is hardcoded size
  if (rm)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
  if (sstr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
  SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
  if (res > 0 && buf) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
  }
  return res;
}

#define INIT_REGEXSUB
#else
#define INIT_REGEXSUB
#endif

#if SANITIZER_INTERCEPT_FTS
INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
            int (*compar)(void **, void **)) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
  if (path_argv) {
    for (char *const *pa = path_argv; ; ++pa) {
      COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
      if (!*pa)
        break;
      COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
    }
  }
  // TODO(kamil): handle compar callback
  void *fts = REAL(fts_open)(path_argv, options, compar);
  if (fts)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
  return fts;
}

INTERCEPTOR(void *, fts_read, void *ftsp) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
  if (ftsp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  void *ftsent = REAL(fts_read)(ftsp);
  if (ftsent)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
  return ftsent;
}

INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
  if (ftsp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  void *ftsent = REAL(fts_children)(ftsp, options);
  if (ftsent)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
  return ftsent;
}

INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
  if (ftsp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  if (f)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
  return REAL(fts_set)(ftsp, f, options);
}

INTERCEPTOR(int, fts_close, void *ftsp) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
  if (ftsp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  return REAL(fts_close)(ftsp);
}
#define INIT_FTS
#else
#define INIT_FTS
#endif

#if SANITIZER_INTERCEPT_SYSCTL
INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
            SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
  void *ctx;
  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
    return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
  COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
                           newlen);
  if (name)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
  if (oldlenp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  if (newp && newlen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
  int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
  if (!res) {
    if (oldlenp) {
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
      if (oldp)
        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
    }
  }
  return res;
}

INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
            void *newp, SIZE_T newlen) {
  void *ctx;
  if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
    return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
  COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
                           newlen);
  if (sname)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  if (oldlenp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  if (newp && newlen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
  int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
  if (!res) {
    if (oldlenp) {
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
      if (oldp)
        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
    }
  }
  return res;
}

INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
            SIZE_T *namelenp) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
  if (sname)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  if (namelenp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
  int res = REAL(sysctlnametomib)(sname, name, namelenp);
  if (!res) {
    if (namelenp) {
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
      if (name)
        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
    }
  }
  return res;
}

#define INIT_SYSCTL
#else
#define INIT_SYSCTL
#endif

#if SANITIZER_INTERCEPT_ASYSCTL
INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
  if (name)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
  void *res = REAL(asysctl)(name, namelen, len);
  if (res && len) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
  }
  return res;
}

INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
  if (sname)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  void *res = REAL(asysctlbyname)(sname, len);
  if (res && len) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
  }
  return res;
}
#define INIT_ASYSCTL
#else
#define INIT_ASYSCTL
#endif

#if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
            unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
            int v) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
                           csz, rnode, v);
  if (sname)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  if (namelenp)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
  if (csz)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
  // Skip rnode, it's rarely used and not trivial to sanitize
  // It's also used mostly internally
  int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
  if (!res) {
    if (namelenp) {
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
      if (name)
        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
    }
    if (csz) {
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
      if (cname)
        COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
    }
  }
  return res;
}
#define INIT_SYSCTLGETMIBINFO
#else
#define INIT_SYSCTLGETMIBINFO
#endif

#if SANITIZER_INTERCEPT_NL_LANGINFO
INTERCEPTOR(char *, nl_langinfo, long item) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
  char *ret = REAL(nl_langinfo)(item);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
  return ret;
}
#define INIT_NL_LANGINFO
#else
#define INIT_NL_LANGINFO
#endif

#if SANITIZER_INTERCEPT_MODCTL
INTERCEPTOR(int, modctl, int operation, void *argp) {
  void *ctx;
  int ret;
  COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);

  if (operation == modctl_load) {
    if (argp) {
      __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
      COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
      if (ml->ml_filename)
        COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
                                      internal_strlen(ml->ml_filename) + 1);
      if (ml->ml_props)
        COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
    }
    ret = REAL(modctl)(operation, argp);
  } else if (operation == modctl_unload) {
    if (argp) {
      const char *name = (const char *)argp;
      COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
    }
    ret = REAL(modctl)(operation, argp);
  } else if (operation == modctl_stat) {
    uptr iov_len;
    struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
    if (iov) {
      COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
      iov_len = iov->iov_len;
    }
    ret = REAL(modctl)(operation, argp);
    if (iov)
      COMMON_INTERCEPTOR_WRITE_RANGE(
          ctx, iov->iov_base, Min(iov_len,  iov->iov_len));
  } else if (operation == modctl_exists) {
    ret = REAL(modctl)(operation, argp);
  } else {
    ret = REAL(modctl)(operation, argp);
  }

  return ret;
}
#define INIT_MODCTL
#else
#define INIT_MODCTL
#endif

#if SANITIZER_INTERCEPT_STRTONUM
INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
            long long maxval, const char **errstr) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);

  // TODO(kamil): Implement strtoll as a common inteceptor
  char *real_endptr;
  long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
  StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);

  ret = REAL(strtonum)(nptr, minval, maxval, errstr);
  if (errstr) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
     if (*errstr)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
  }
  return ret;
}
#define INIT_STRTONUM
#else
#define INIT_STRTONUM
#endif

#if SANITIZER_INTERCEPT_FPARSELN
INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
            SIZE_T *lineno, const char delim[3], int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
  if (lineno)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
  if (delim)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
  char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
  if (ret) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
    if (len)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
    if (lineno)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
  }
  return ret;
}
#define INIT_FPARSELN
#else
#define INIT_FPARSELN
#endif

#if SANITIZER_INTERCEPT_STATVFS1
INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
  if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  int res = REAL(statvfs1)(path, buf, flags);
  if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  return res;
}
INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  int res = REAL(fstatvfs1)(fd, buf, flags);
  if (!res) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
    if (fd >= 0)
      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  }
  return res;
}
#define INIT_STATVFS1
#else
#define INIT_STATVFS1
#endif

#if SANITIZER_INTERCEPT_STRTOI
INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
            INTMAX_T low, INTMAX_T high, int *rstatus) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
  char *real_endptr;
  INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  if (rstatus)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
  return ret;
}

INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
            UINTMAX_T low, UINTMAX_T high, int *rstatus) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
  char *real_endptr;
  UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
  StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  if (rstatus)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
  return ret;
}
#define INIT_STRTOI
#else
#define INIT_STRTOI
#endif

#if SANITIZER_INTERCEPT_CAPSICUM
#define CAP_RIGHTS_INIT_INTERCEPTOR

#define CAP_RIGHTS_SET_INTERCEPTOR

#define CAP_RIGHTS_CLEAR_INTERCEPTOR

#define CAP_RIGHTS_IS_SET_INTERCEPTOR

INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
            __sanitizer_cap_rights_t *rights) {
  CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
}

INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
            __sanitizer_cap_rights_t *rights) {
  CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
}

INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
            __sanitizer_cap_rights_t *rights) {
  CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
}

INTERCEPTOR(bool, cap_rights_is_set,
            __sanitizer_cap_rights_t *rights) {
  CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
}

INTERCEPTOR(int, cap_rights_limit, int fd,
            const __sanitizer_cap_rights_t *rights) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
  if (rights)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));

  return REAL(cap_rights_limit)(fd, rights);
}

INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
  int ret = REAL(cap_rights_get)(fd, rights);
  if (!ret && rights)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));

  return ret;
}

INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
  if (rights)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));

  return REAL(cap_rights_is_valid(rights));
}

INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
  __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));

  __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
  if (dst)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));

  return ret;
}

INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
  __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));

  __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
  if (dst)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));

  return ret;
}

INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
  const __sanitizer_cap_rights *little) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
  if (little)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
  if (big)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));

  return REAL(cap_rights_contains)(big, little);
}

INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
  if (cmds)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);

  return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
}

INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
  int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
  if (!ret && cmds)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);

  return ret;
}
#define INIT_CAPSICUM
#else
#define INIT_CAPSICUM
#endif

#if SANITIZER_INTERCEPT_SHA1
INTERCEPTOR(void, SHA1Init, void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
  REAL(SHA1Init)(context);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
}
INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  REAL(SHA1Update)(context, data, len);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
}
INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  REAL(SHA1Final)(digest, context);
  if (digest)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
}
INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
  if (state)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
  if (buffer)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
  REAL(SHA1Transform)(state, buffer);
  if (state)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
}
INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  char *ret = REAL(SHA1End)(context, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  return ret;
}
INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(SHA1File)(filename, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  return ret;
}
INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
  OFF_T length) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  return ret;
}
INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
  if (data)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  char *ret = REAL(SHA1Data)(data, len, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  return ret;
}
#define INIT_SHA1
#else
#define INIT_SHA1
#endif

#if SANITIZER_INTERCEPT_MD4
INTERCEPTOR(void, MD4Init, void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
  REAL(MD4Init)(context);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
}

INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
            unsigned int len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  REAL(MD4Update)(context, data, len);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
}

INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  REAL(MD4Final)(digest, context);
  if (digest)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
}

INTERCEPTOR(char *, MD4End, void *context, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  char *ret = REAL(MD4End)(context, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  return ret;
}

INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(MD4File)(filename, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  return ret;
}

INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
            char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  char *ret = REAL(MD4Data)(data, len, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  return ret;
}

#define INIT_MD4
#else
#define INIT_MD4
#endif

#if SANITIZER_INTERCEPT_RMD160
INTERCEPTOR(void, RMD160Init, void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
  REAL(RMD160Init)(context);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
}
INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  REAL(RMD160Update)(context, data, len);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
}
INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  REAL(RMD160Final)(digest, context);
  if (digest)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
}
INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
  if (state)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
  if (buffer)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
  REAL(RMD160Transform)(state, buffer);
  if (state)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
}
INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  char *ret = REAL(RMD160End)(context, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  return ret;
}
INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(RMD160File)(filename, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  return ret;
}
INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
  OFF_T length) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  return ret;
}
INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  char *ret = REAL(RMD160Data)(data, len, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  return ret;
}
#define INIT_RMD160
#else
#define INIT_RMD160
#endif

#if SANITIZER_INTERCEPT_MD5
INTERCEPTOR(void, MD5Init, void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
  REAL(MD5Init)(context);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
}

INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
            unsigned int len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  REAL(MD5Update)(context, data, len);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
}

INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  REAL(MD5Final)(digest, context);
  if (digest)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
}

INTERCEPTOR(char *, MD5End, void *context, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  char *ret = REAL(MD5End)(context, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  return ret;
}

INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(MD5File)(filename, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  return ret;
}

INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
            char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  char *ret = REAL(MD5Data)(data, len, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  return ret;
}

#define INIT_MD5
#else
#define INIT_MD5
#endif

#if SANITIZER_INTERCEPT_FSEEK
INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
  return REAL(fseek)(stream, offset, whence);
}
INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
  return REAL(fseeko)(stream, offset, whence);
}
INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
  return REAL(ftell)(stream);
}
INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
  return REAL(ftello)(stream);
}
INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
  return REAL(rewind)(stream);
}
INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
  int ret = REAL(fgetpos)(stream, pos);
  if (pos && !ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
  return ret;
}
INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
  if (pos)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
  return REAL(fsetpos)(stream, pos);
}
#define INIT_FSEEK
#else
#define INIT_FSEEK
#endif

#if SANITIZER_INTERCEPT_MD2
INTERCEPTOR(void, MD2Init, void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
  REAL(MD2Init)(context);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
}

INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
            unsigned int len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  REAL(MD2Update)(context, data, len);
  if (context)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
}

INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  REAL(MD2Final)(digest, context);
  if (digest)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
}

INTERCEPTOR(char *, MD2End, void *context, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
  if (context)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  char *ret = REAL(MD2End)(context, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  return ret;
}

INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
  if (filename)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  char *ret = REAL(MD2File)(filename, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  return ret;
}

INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
            char *buf) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
  if (data && len > 0)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  char *ret = REAL(MD2Data)(data, len, buf);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  return ret;
}

#define INIT_MD2
#else
#define INIT_MD2
#endif

#if SANITIZER_INTERCEPT_SHA2
#define SHA2_INTERCEPTORS

SHA2_INTERCEPTORS(224, u32)
SHA2_INTERCEPTORS(256, u32)
SHA2_INTERCEPTORS(384, u64)
SHA2_INTERCEPTORS(512, u64)

#define INIT_SHA2_INTECEPTORS

#define INIT_SHA2
#undef SHA2_INTERCEPTORS
#else
#define INIT_SHA2
#endif

#if SANITIZER_INTERCEPT_VIS
INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
  char *end = REAL(vis)(dst, c, flag, nextc);
  // dst is NULL terminated and end points to the NULL char
  if (dst && end)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  return end;
}
INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
  char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
  // nvis cannot make sure the dst is NULL terminated
  if (dst && end)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  return end;
}
INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int len = REAL(strvis)(dst, src, flag);
  if (dst)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  return len;
}
INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int len = REAL(stravis)(dst, src, flag);
  if (dst) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
    if (*dst)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
  }
  return len;
}
INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int len = REAL(strnvis)(dst, dlen, src, flag);
  // The interface will be valid even if there is no space for NULL char
  if (dst && len > 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  return len;
}
INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  int ret = REAL(strvisx)(dst, src, len, flag);
  if (dst)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
            int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
  if (dst && ret >= 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
            int flag, int *cerr_ptr) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
  // according to the implementation
  if (cerr_ptr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
  int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
  if (dst && ret >= 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  if (cerr_ptr)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
  return ret;
}
INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
            const char *extra) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  char *end = REAL(svis)(dst, c, flag, nextc, extra);
  if (dst && end)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  return end;
}
INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
            const char *extra) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
  if (dst && end)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
                                   Min((SIZE_T)(end - dst + 1), dlen));
  return end;
}
INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
            const char *extra) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  int len = REAL(strsvis)(dst, src, flag, extra);
  if (dst)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  return len;
}
INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
            const char *extra) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
  // The interface will be valid even if there is no space for NULL char
  if (dst && len >= 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  return len;
}
INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
            const char *extra) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  int ret = REAL(strsvisx)(dst, src, len, flag, extra);
  if (dst)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
            int flag, const char *extra) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
  if (dst && ret >= 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
            SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
                           cerr_ptr);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  if (extra)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
  // according to the implementation
  if (cerr_ptr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
  int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
  if (dst && ret >= 0)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  if (cerr_ptr)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
  return ret;
}
INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
  if (astate)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
  int ret = REAL(unvis)(cp, c, astate, flag);
  if (ret == unvis_valid || ret == unvis_validpush) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
  }
  return ret;
}
INTERCEPTOR(int, strunvis, char *dst, const char *src) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int ret = REAL(strunvis)(dst, src);
  if (ret != -1)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int ret = REAL(strnunvis)(dst, dlen, src);
  if (ret != -1)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int ret = REAL(strunvisx)(dst, src, flag);
  if (ret != -1)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
            int flag) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
  if (src)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  int ret = REAL(strnunvisx)(dst, dlen, src, flag);
  if (ret != -1)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  return ret;
}
#define INIT_VIS
#else
#define INIT_VIS
#endif

#if SANITIZER_INTERCEPT_CDB
INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
  if (path)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
  if (cdbr)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
  return cdbr;
}

INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
  int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
    cookie);
  if (base && size)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
  struct __sanitizer_cdbr *cdbr =
    REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
  if (cdbr)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
  return cdbr;
}

INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
  if (cdbr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  return REAL(cdbr_entries)(cdbr);
}

INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
            const void **data, SIZE_T *datalen) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
  if (cdbr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
  if (!ret) {
    if (data)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
    if (datalen)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
    if (data && datalen)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
  }
  return ret;
}

INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
            SIZE_T keylen, const void **data, SIZE_T *datalen) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
  if (cdbr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  if (key)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
  if (!ret) {
    if (data)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
    if (datalen)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
    if (data && datalen)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
  }
  return ret;
}

INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
  if (cdbr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  REAL(cdbr_close)(cdbr);
}

INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
  struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
  return ret;
}

INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
  SIZE_T keylen, const void *data, SIZE_T datalen) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
  if (cdbw)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  if (data && datalen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
  if (key && keylen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
  if (!ret && cdbw)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  return ret;
}

INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
  SIZE_T datalen, u32 *index) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
  if (cdbw)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  if (data && datalen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
  int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
  if (!ret) {
    if (index)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
    if (cdbw)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  }
  return ret;
}

INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
  SIZE_T keylen, u32 index) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
  if (cdbw)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  if (key && keylen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
  if (!ret && cdbw)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  return ret;
}

INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
  const char descr[16], u32 (*seedgen)(void)) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
  COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
  if (cdbw)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  if (descr)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
  if (seedgen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
  int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
  if (!ret) {
    if (cdbw)
      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
    if (output >= 0)
      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
  }
  return ret;
}

INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
  if (cdbw)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  REAL(cdbw_close)(cdbw);
}

#define INIT_CDB
#else
#define INIT_CDB
#endif

#if SANITIZER_INTERCEPT_GETFSENT
INTERCEPTOR(void *, getfsent) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
  void *ret = REAL(getfsent)();
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  return ret;
}

INTERCEPTOR(void *, getfsspec, const char *spec) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
  if (spec)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
  void *ret = REAL(getfsspec)(spec);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  return ret;
}

INTERCEPTOR(void *, getfsfile, const char *file) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
  if (file)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
  void *ret = REAL(getfsfile)(file);
  if (ret)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  return ret;
}

#define INIT_GETFSENT
#else
#define INIT_GETFSENT
#endif

#if SANITIZER_INTERCEPT_ARC4RANDOM
INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
  REAL(arc4random_buf)(buf, len);
  if (buf && len)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
}

INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
  if (dat && datlen)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
  REAL(arc4random_addrandom)(dat, datlen);
}

#define INIT_ARC4RANDOM
#else
#define INIT_ARC4RANDOM
#endif

#if SANITIZER_INTERCEPT_POPEN
INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {}
#define INIT_POPEN
#else
#define INIT_POPEN
#endif

#if SANITIZER_INTERCEPT_POPENVE
INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
            char *const *argv, char *const *envp, const char *type) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
  if (path)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  if (argv) {
    for (char *const *pa = argv; ; ++pa) {
      COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
      if (!*pa)
        break;
      COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
    }
  }
  if (envp) {
    for (char *const *pa = envp; ; ++pa) {
      COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
      if (!*pa)
        break;
      COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
    }
  }
  if (type)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
  __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
  COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
  if (res) unpoison_file(res);
  return res;
}
#define INIT_POPENVE
#else
#define INIT_POPENVE
#endif

#if SANITIZER_INTERCEPT_PCLOSE
INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {}
#define INIT_PCLOSE
#else
#define INIT_PCLOSE
#endif

#if SANITIZER_INTERCEPT_FUNOPEN
typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
typedef int (*funopen_closefn)(void *cookie);

struct WrappedFunopenCookie {
  void *real_cookie;
  funopen_readfn real_read;
  funopen_writefn real_write;
  funopen_seekfn real_seek;
  funopen_closefn real_close;
};

static int wrapped_funopen_read(void *cookie, char *buf, int len) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  funopen_readfn real_read = wrapped_cookie->real_read;
  return real_read(wrapped_cookie->real_cookie, buf, len);
}

static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  funopen_writefn real_write = wrapped_cookie->real_write;
  return real_write(wrapped_cookie->real_cookie, buf, len);
}

static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  funopen_seekfn real_seek = wrapped_cookie->real_seek;
  return real_seek(wrapped_cookie->real_cookie, offset, whence);
}

static int wrapped_funopen_close(void *cookie) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  funopen_closefn real_close = wrapped_cookie->real_close;
  int res = real_close(wrapped_cookie->real_cookie);
  InternalFree(wrapped_cookie);
  return res;
}

INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
            funopen_writefn writefn, funopen_seekfn seekfn,
            funopen_closefn closefn) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
                           closefn);

  WrappedFunopenCookie *wrapped_cookie =
      (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
  wrapped_cookie->real_cookie = cookie;
  wrapped_cookie->real_read = readfn;
  wrapped_cookie->real_write = writefn;
  wrapped_cookie->real_seek = seekfn;
  wrapped_cookie->real_close = closefn;

  __sanitizer_FILE *res =
      REAL(funopen)(wrapped_cookie,
                    readfn  ? wrapped_funopen_read  : nullptr,
                    writefn ? wrapped_funopen_write : nullptr,
                    seekfn  ? wrapped_funopen_seek  : nullptr,
                    closefn ? wrapped_funopen_close : nullptr);
  if (res)
    unpoison_file(res);
  return res;
}
#define INIT_FUNOPEN
#else
#define INIT_FUNOPEN
#endif

#if SANITIZER_INTERCEPT_FUNOPEN2
typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
typedef int (*funopen2_flushfn)(void *cookie);
typedef int (*funopen2_closefn)(void *cookie);

struct WrappedFunopen2Cookie {
  void *real_cookie;
  funopen2_readfn real_read;
  funopen2_writefn real_write;
  funopen2_seekfn real_seek;
  funopen2_flushfn real_flush;
  funopen2_closefn real_close;
};

static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  funopen2_readfn real_read = wrapped_cookie->real_read;
  return real_read(wrapped_cookie->real_cookie, buf, len);
}

static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
                                      SIZE_T len) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  funopen2_writefn real_write = wrapped_cookie->real_write;
  return real_write(wrapped_cookie->real_cookie, buf, len);
}

static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  funopen2_seekfn real_seek = wrapped_cookie->real_seek;
  return real_seek(wrapped_cookie->real_cookie, offset, whence);
}

static int wrapped_funopen2_flush(void *cookie) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  funopen2_flushfn real_flush = wrapped_cookie->real_flush;
  return real_flush(wrapped_cookie->real_cookie);
}

static int wrapped_funopen2_close(void *cookie) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  funopen2_closefn real_close = wrapped_cookie->real_close;
  int res = real_close(wrapped_cookie->real_cookie);
  InternalFree(wrapped_cookie);
  return res;
}

INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
            funopen2_writefn writefn, funopen2_seekfn seekfn,
            funopen2_flushfn flushfn, funopen2_closefn closefn) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
                           flushfn, closefn);

  WrappedFunopen2Cookie *wrapped_cookie =
      (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
  wrapped_cookie->real_cookie = cookie;
  wrapped_cookie->real_read = readfn;
  wrapped_cookie->real_write = writefn;
  wrapped_cookie->real_seek = seekfn;
  wrapped_cookie->real_flush = flushfn;
  wrapped_cookie->real_close = closefn;

  __sanitizer_FILE *res =
      REAL(funopen2)(wrapped_cookie,
                     readfn  ? wrapped_funopen2_read  : nullptr,
                     writefn ? wrapped_funopen2_write : nullptr,
                     seekfn  ? wrapped_funopen2_seek  : nullptr,
                     flushfn ? wrapped_funopen2_flush : nullptr,
                     closefn ? wrapped_funopen2_close : nullptr);
  if (res)
    unpoison_file(res);
  return res;
}
#define INIT_FUNOPEN2
#else
#define INIT_FUNOPEN2
#endif

#if SANITIZER_INTERCEPT_FDEVNAME
INTERCEPTOR(char *, fdevname,  int fd) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  char *name = REAL(fdevname)(fd);
  if (name) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
    if (fd > 0)
      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  }
  return name;
}

INTERCEPTOR(char *, fdevname_r,  int fd, char *buf, SIZE_T len) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
  COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  char *name = REAL(fdevname_r)(fd, buf, len);
  if (name && buf && len > 0) {
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
    if (fd > 0)
      COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  }
  return name;
}

#define INIT_FDEVNAME
#else
#define INIT_FDEVNAME
#endif

#if SANITIZER_INTERCEPT_GETUSERSHELL
INTERCEPTOR(char *, getusershell,) {}

#define INIT_GETUSERSHELL
#else
#define INIT_GETUSERSHELL
#endif

#if SANITIZER_INTERCEPT_SL_INIT
INTERCEPTOR(void *, sl_init) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
  void *res = REAL(sl_init)();
  if (res)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
  return res;
}

INTERCEPTOR(int, sl_add, void *sl, char *item) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
  if (sl)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  if (item)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
  int res = REAL(sl_add)(sl, item);
  if (!res)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  return res;
}

INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
  if (sl)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  if (item)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
  char *res = REAL(sl_find)(sl, item);
  if (res)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  return res;
}

INTERCEPTOR(void, sl_free, void *sl, int freeall) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
  if (sl)
    COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  REAL(sl_free)(sl, freeall);
}

#define INIT_SL_INIT
#else
#define INIT_SL_INIT
#endif

#if SANITIZER_INTERCEPT_GETRANDOM
INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {}
#define INIT_GETRANDOM
#else
#define INIT_GETRANDOM
#endif

#if SANITIZER_INTERCEPT_GETENTROPY
INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {}
#define INIT_GETENTROPY
#else
#define INIT_GETENTROPY
#endif

#if SANITIZER_INTERCEPT_QSORT_R
qsort_r_compar_f;
struct qsort_r_compar_params {};
static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {}

INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
            qsort_r_compar_f compar, void *arg) {}
#define INIT_QSORT_R
#else
#define INIT_QSORT_R
#endif

#if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
            qsort_r_compar_f compar) {}
#define INIT_QSORT
#elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
// Glibc qsort uses a temporary buffer allocated either on stack or on heap.
// Poisoned memory from there may get copied into the comparator arguments,
// where it needs to be dealt with. But even that is not enough - the results of
// the sort may be copied into the input/output array based on the results of
// the comparator calls, but directly from the temp memory, bypassing the
// unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
// unpoisoning the entire array after the sort is done.
//
// We can not check that the entire array is initialized at the beginning. IMHO,
// it's fine for parts of the sorted objects to contain uninitialized memory,
// ex. as padding in structs.
typedef int (*qsort_compar_f)(const void *, const void *);
static THREADLOCAL qsort_compar_f qsort_compar;
static THREADLOCAL SIZE_T qsort_size;
static int wrapped_qsort_compar(const void *a, const void *b) {
  COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
  COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
  return qsort_compar(a, b);
}

INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
            qsort_compar_f compar) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
  // Run the comparator over all array elements to detect any memory issues.
  if (nmemb > 1) {
    for (SIZE_T i = 0; i < nmemb - 1; ++i) {
      void *p = (void *)((char *)base + i * size);
      void *q = (void *)((char *)base + (i + 1) * size);
      COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
      compar(p, q);
    }
  }
  qsort_compar_f old_compar = qsort_compar;
  SIZE_T old_size = qsort_size;
  // Handle qsort() implementations that recurse using an
  // interposable function call:
  bool already_wrapped = compar == wrapped_qsort_compar;
  if (already_wrapped) {
    // This case should only happen if the qsort() implementation calls itself
    // using a preemptible function call (e.g. the FreeBSD libc version).
    // Check that the size and comparator arguments are as expected.
    CHECK_NE(compar, qsort_compar);
    CHECK_EQ(qsort_size, size);
  } else {
    qsort_compar = compar;
    qsort_size = size;
  }
  REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
  if (!already_wrapped) {
    qsort_compar = old_compar;
    qsort_size = old_size;
  }
  COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
}
#define INIT_QSORT
#else
#define INIT_QSORT
#endif

#if SANITIZER_INTERCEPT_BSEARCH
bsearch_compar_f;
struct bsearch_compar_params {};

static int wrapped_bsearch_compar(const void *key, const void *b) {}

INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
            SIZE_T size, bsearch_compar_f compar) {}
#define INIT_BSEARCH
#else
#define INIT_BSEARCH
#endif

#if SANITIZER_INTERCEPT_SIGALTSTACK
INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {}
#define INIT_SIGALTSTACK
#else
#define INIT_SIGALTSTACK
#endif

#if SANITIZER_INTERCEPT_PROCCTL
INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
   static const int PROC_REAP_ACQUIRE = 2;
   static const int PROC_REAP_RELEASE = 3;
   static const int PROC_REAP_STATUS = 4;
   static const int PROC_REAP_GETPIDS = 5;
   static const int PROC_REAP_KILL = 6;
   if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
     COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
   } else {
     // reap_acquire/reap_release bears no arguments.
     if (cmd > PROC_REAP_RELEASE) {
       unsigned int reapsz;
       switch (cmd) {
       case PROC_REAP_STATUS:
         reapsz = struct_procctl_reaper_status_sz;
         break;
       case PROC_REAP_GETPIDS:
         reapsz = struct_procctl_reaper_pids_sz;
         break;
       case PROC_REAP_KILL:
         reapsz = struct_procctl_reaper_kill_sz;
         break;
       }
       COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
     }
   }
   return REAL(procctl)(idtype, id, cmd, data);
}
#define INIT_PROCCTL
#else
#define INIT_PROCCTL
#endif

#if SANITIZER_INTERCEPT_UNAME
INTERCEPTOR(int, uname, struct utsname *utsname) {}
#define INIT_UNAME
#else
#define INIT_UNAME
#endif

#if SANITIZER_INTERCEPT___XUNAME
// FreeBSD's <sys/utsname.h> define uname() as
// static __inline int uname(struct utsname *name) {
//   return __xuname(SYS_NMLN, (void*)name);
// }
INTERCEPTOR(int, __xuname, int size, void *utsname) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
  int res = REAL(__xuname)(size, utsname);
  if (!res)
    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
                                   __sanitizer::struct_utsname_sz);
  return res;
}
#define INIT___XUNAME
#else
#define INIT___XUNAME
#endif

#if SANITIZER_INTERCEPT_ARGP_PARSE
INTERCEPTOR(int, argp_parse, const struct argp *argp, int argc, char **argv,
            unsigned flags, int *arg_index, void *input) {}

#define INIT_ARGP_PARSE
#else
#define INIT_ARGP_PARSE
#endif

#if SANITIZER_INTERCEPT_CPUSET_GETAFFINITY
INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T cpusetsize, __sanitizer_cpuset_t *mask) {
  void *ctx;
  COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getaffinity, level, which, id, cpusetsize, mask);
  int res = REAL(cpuset_getaffinity)(level, which, id, cpusetsize, mask);
  if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
  return res;
}
#define INIT_CPUSET_GETAFFINITY
#else
#define INIT_CPUSET_GETAFFINITY
#endif

#if SANITIZER_INTERCEPT_PREADV2
INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
            OFF_T offset, int flags) {}
#define INIT_PREADV2
#else
#define INIT_PREADV2
#endif

#if SANITIZER_INTERCEPT_PWRITEV2
INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
            OFF_T offset, int flags) {}
#define INIT_PWRITEV2
#else
#define INIT_PWRITEV2
#endif

#include "sanitizer_common_interceptors_netbsd_compat.inc"

namespace __sanitizer {
void InitializeMemintrinsicInterceptors();
}  // namespace __sanitizer

static void InitializeCommonInterceptors() {}