xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_wrappers.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
168d75effSDimitry Andric //===-- sanitizer_wrappers.cpp ----------------------------------*- C++ -*-===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // Redirect some functions to sanitizer interceptors.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric //===----------------------------------------------------------------------===//
1268d75effSDimitry Andric 
1368d75effSDimitry Andric #include <dlfcn.h>
1468d75effSDimitry Andric #include <errno.h>
1568d75effSDimitry Andric #include <fcntl.h>
16*81ad6265SDimitry Andric #include <inttypes.h>
1768d75effSDimitry Andric #include <stdarg.h>
1868d75effSDimitry Andric #include <stdio.h>
1968d75effSDimitry Andric #include <unistd.h>
2068d75effSDimitry Andric 
2168d75effSDimitry Andric #include <tuple>
2268d75effSDimitry Andric 
2368d75effSDimitry Andric namespace __sanitizer {
2468d75effSDimitry Andric unsigned long internal_open(const char *filename, int flags);
2568d75effSDimitry Andric unsigned long internal_open(const char *filename, int flags, unsigned mode);
2668d75effSDimitry Andric unsigned long internal_close(int fd);
2768d75effSDimitry Andric unsigned long internal_stat(const char *path, void *buf);
2868d75effSDimitry Andric unsigned long internal_lstat(const char *path, void *buf);
2968d75effSDimitry Andric unsigned long internal_fstat(int fd, void *buf);
3068d75effSDimitry Andric size_t internal_strlen(const char *s);
31*81ad6265SDimitry Andric unsigned long internal_mmap(void *addr, uintptr_t length, int prot, int flags,
32*81ad6265SDimitry Andric                             int fd, unsigned long long offset);
3368d75effSDimitry Andric void *internal_memcpy(void *dest, const void *src, unsigned long n);
3468d75effSDimitry Andric // Used to propagate errno.
35*81ad6265SDimitry Andric bool internal_iserror(uintptr_t retval, int *rverrno = 0);
3668d75effSDimitry Andric }  // namespace __sanitizer
3768d75effSDimitry Andric 
3868d75effSDimitry Andric namespace {
3968d75effSDimitry Andric 
4068d75effSDimitry Andric template <typename T>
4168d75effSDimitry Andric struct GetTypes;
4268d75effSDimitry Andric 
4368d75effSDimitry Andric template <typename R, typename... Args>
4468d75effSDimitry Andric struct GetTypes<R(Args...)> {
4568d75effSDimitry Andric   using Result = R;
4668d75effSDimitry Andric   template <size_t i>
4768d75effSDimitry Andric   struct Arg {
4868d75effSDimitry Andric     using Type = typename std::tuple_element<i, std::tuple<Args...>>::type;
4968d75effSDimitry Andric   };
5068d75effSDimitry Andric };
5168d75effSDimitry Andric 
5268d75effSDimitry Andric #define LLVM_SYMBOLIZER_GET_FUNC(Function) \
5368d75effSDimitry Andric   ((__interceptor_##Function)              \
5468d75effSDimitry Andric        ? (__interceptor_##Function)        \
5568d75effSDimitry Andric        : reinterpret_cast<decltype(&Function)>(dlsym(RTLD_NEXT, #Function)))
5668d75effSDimitry Andric 
5768d75effSDimitry Andric #define LLVM_SYMBOLIZER_INTERCEPTOR1(Function, ...)               \
5868d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result __interceptor_##Function(         \
5968d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type) __attribute__((weak)); \
6068d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result Function(                         \
6168d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type arg0) {                 \
6268d75effSDimitry Andric     return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0);              \
6368d75effSDimitry Andric   }
6468d75effSDimitry Andric 
6568d75effSDimitry Andric #define LLVM_SYMBOLIZER_INTERCEPTOR2(Function, ...)               \
6668d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result __interceptor_##Function(         \
6768d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type,                        \
6868d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<1>::Type) __attribute__((weak)); \
6968d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result Function(                         \
7068d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type arg0,                   \
7168d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<1>::Type arg1) {                 \
7268d75effSDimitry Andric     return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1);        \
7368d75effSDimitry Andric   }
7468d75effSDimitry Andric 
7568d75effSDimitry Andric #define LLVM_SYMBOLIZER_INTERCEPTOR3(Function, ...)               \
7668d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result __interceptor_##Function(         \
7768d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type,                        \
7868d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<1>::Type,                        \
7968d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<2>::Type) __attribute__((weak)); \
8068d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result Function(                         \
8168d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type arg0,                   \
8268d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<1>::Type arg1,                   \
8368d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<2>::Type arg2) {                 \
8468d75effSDimitry Andric     return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2);  \
8568d75effSDimitry Andric   }
8668d75effSDimitry Andric 
8768d75effSDimitry Andric #define LLVM_SYMBOLIZER_INTERCEPTOR4(Function, ...)                    \
8868d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result __interceptor_##Function(              \
8968d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type,                             \
9068d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<1>::Type,                             \
9168d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<2>::Type,                             \
9268d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<3>::Type) __attribute__((weak));      \
9368d75effSDimitry Andric   GetTypes<__VA_ARGS__>::Result Function(                              \
9468d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<0>::Type arg0,                        \
9568d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<1>::Type arg1,                        \
9668d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<2>::Type arg2,                        \
9768d75effSDimitry Andric       GetTypes<__VA_ARGS__>::Arg<3>::Type arg3) {                      \
9868d75effSDimitry Andric     return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2, arg3); \
9968d75effSDimitry Andric   }
10068d75effSDimitry Andric 
10168d75effSDimitry Andric }  // namespace
10268d75effSDimitry Andric 
10368d75effSDimitry Andric // C-style interface around internal sanitizer libc functions.
10468d75effSDimitry Andric extern "C" {
10568d75effSDimitry Andric 
10668d75effSDimitry Andric #define RETURN_OR_SET_ERRNO(T, res)                   \
10768d75effSDimitry Andric   int rverrno;                                        \
10868d75effSDimitry Andric   if (__sanitizer::internal_iserror(res, &rverrno)) { \
10968d75effSDimitry Andric     errno = rverrno;                                  \
11068d75effSDimitry Andric     return (T)-1;                                     \
11168d75effSDimitry Andric   }                                                   \
11268d75effSDimitry Andric   return (T)res;
11368d75effSDimitry Andric 
open(const char * filename,int flags,...)11468d75effSDimitry Andric int open(const char *filename, int flags, ...) {
11568d75effSDimitry Andric   unsigned long res;
11668d75effSDimitry Andric   if (flags | O_CREAT) {
11768d75effSDimitry Andric     va_list va;
11868d75effSDimitry Andric     va_start(va, flags);
11968d75effSDimitry Andric     unsigned mode = va_arg(va, unsigned);
12068d75effSDimitry Andric     va_end(va);
12168d75effSDimitry Andric     res = __sanitizer::internal_open(filename, flags, mode);
12268d75effSDimitry Andric   } else {
12368d75effSDimitry Andric     res = __sanitizer::internal_open(filename, flags);
12468d75effSDimitry Andric   }
12568d75effSDimitry Andric   RETURN_OR_SET_ERRNO(int, res);
12668d75effSDimitry Andric }
12768d75effSDimitry Andric 
close(int fd)12868d75effSDimitry Andric int close(int fd) {
12968d75effSDimitry Andric   unsigned long res = __sanitizer::internal_close(fd);
13068d75effSDimitry Andric   RETURN_OR_SET_ERRNO(int, res);
13168d75effSDimitry Andric }
13268d75effSDimitry Andric 
13368d75effSDimitry Andric #define STAT(func, arg, buf)                                  \
13468d75effSDimitry Andric   unsigned long res = __sanitizer::internal_##func(arg, buf); \
13568d75effSDimitry Andric   RETURN_OR_SET_ERRNO(int, res);
13668d75effSDimitry Andric 
stat(const char * path,struct stat * buf)13768d75effSDimitry Andric int stat(const char *path, struct stat *buf) { STAT(stat, path, buf); }
13868d75effSDimitry Andric 
lstat(const char * path,struct stat * buf)13968d75effSDimitry Andric int lstat(const char *path, struct stat *buf) { STAT(lstat, path, buf); }
14068d75effSDimitry Andric 
fstat(int fd,struct stat * buf)14168d75effSDimitry Andric int fstat(int fd, struct stat *buf) { STAT(fstat, fd, buf); }
14268d75effSDimitry Andric 
14368d75effSDimitry Andric // Redirect versioned stat functions to the __sanitizer::internal() as well.
__xstat(int version,const char * path,struct stat * buf)14468d75effSDimitry Andric int __xstat(int version, const char *path, struct stat *buf) {
14568d75effSDimitry Andric   STAT(stat, path, buf);
14668d75effSDimitry Andric }
14768d75effSDimitry Andric 
__lxstat(int version,const char * path,struct stat * buf)14868d75effSDimitry Andric int __lxstat(int version, const char *path, struct stat *buf) {
14968d75effSDimitry Andric   STAT(lstat, path, buf);
15068d75effSDimitry Andric }
15168d75effSDimitry Andric 
__fxstat(int version,int fd,struct stat * buf)15268d75effSDimitry Andric int __fxstat(int version, int fd, struct stat *buf) { STAT(fstat, fd, buf); }
15368d75effSDimitry Andric 
strlen(const char * s)15468d75effSDimitry Andric size_t strlen(const char *s) { return __sanitizer::internal_strlen(s); }
15568d75effSDimitry Andric 
mmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)15668d75effSDimitry Andric void *mmap(void *addr, size_t length, int prot, int flags, int fd,
15768d75effSDimitry Andric            off_t offset) {
158*81ad6265SDimitry Andric   unsigned long res =
159*81ad6265SDimitry Andric       __sanitizer::internal_mmap(addr, length, prot, flags, fd, offset);
16068d75effSDimitry Andric   RETURN_OR_SET_ERRNO(void *, res);
16168d75effSDimitry Andric }
16268d75effSDimitry Andric 
16368d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR3(read, ssize_t(int, void *, size_t))
16468d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR4(pread, ssize_t(int, void *, size_t, off_t))
16568d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR4(pread64, ssize_t(int, void *, size_t, off64_t))
16668d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(realpath, char *(const char *, char *))
16768d75effSDimitry Andric 
16868d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_cond_broadcast, int(pthread_cond_t *))
16968d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_cond_wait,
17068d75effSDimitry Andric                              int(pthread_cond_t *, pthread_mutex_t *))
17168d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_lock, int(pthread_mutex_t *))
17268d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_unlock, int(pthread_mutex_t *))
17368d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_destroy, int(pthread_mutex_t *))
17468d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutex_init,
17568d75effSDimitry Andric                              int(pthread_mutex_t *,
17668d75effSDimitry Andric                                  const pthread_mutexattr_t *))
17768d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_destroy,
17868d75effSDimitry Andric                              int(pthread_mutexattr_t *))
17968d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_init, int(pthread_mutexattr_t *))
18068d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutexattr_settype,
18168d75effSDimitry Andric                              int(pthread_mutexattr_t *, int))
18268d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_getspecific, void *(pthread_key_t))
18368d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_key_create,
18468d75effSDimitry Andric                              int(pthread_key_t *, void (*)(void *)))
18568d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_once,
18668d75effSDimitry Andric                              int(pthread_once_t *, void (*)(void)))
18768d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_setspecific,
18868d75effSDimitry Andric                              int(pthread_key_t, const void *))
18968d75effSDimitry Andric LLVM_SYMBOLIZER_INTERCEPTOR3(pthread_sigmask,
19068d75effSDimitry Andric                              int(int, const sigset_t *, sigset_t *))
19168d75effSDimitry Andric 
19268d75effSDimitry Andric }  // extern "C"
193