1 //===-- sanitizer_wrappers.cpp ----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Redirect some functions to sanitizer interceptors. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include <dlfcn.h> 14 #include <errno.h> 15 #include <fcntl.h> 16 #include <stdarg.h> 17 #include <stdio.h> 18 #include <unistd.h> 19 20 #include <tuple> 21 22 // Need to match ../sanitizer_common/sanitizer_internal_defs.h 23 #if defined(ARCH_PPC) 24 #define OFF_T unsigned long 25 #else 26 #define OFF_T unsigned long long 27 #endif 28 29 namespace __sanitizer { 30 unsigned long internal_open(const char *filename, int flags); 31 unsigned long internal_open(const char *filename, int flags, unsigned mode); 32 unsigned long internal_close(int fd); 33 unsigned long internal_stat(const char *path, void *buf); 34 unsigned long internal_lstat(const char *path, void *buf); 35 unsigned long internal_fstat(int fd, void *buf); 36 size_t internal_strlen(const char *s); 37 unsigned long internal_mmap(void *addr, unsigned long length, int prot, 38 int flags, int fd, OFF_T offset); 39 void *internal_memcpy(void *dest, const void *src, unsigned long n); 40 // Used to propagate errno. 41 bool internal_iserror(unsigned long retval, int *rverrno = 0); 42 } // namespace __sanitizer 43 44 namespace { 45 46 template <typename T> 47 struct GetTypes; 48 49 template <typename R, typename... Args> 50 struct GetTypes<R(Args...)> { 51 using Result = R; 52 template <size_t i> 53 struct Arg { 54 using Type = typename std::tuple_element<i, std::tuple<Args...>>::type; 55 }; 56 }; 57 58 #define LLVM_SYMBOLIZER_GET_FUNC(Function) \ 59 ((__interceptor_##Function) \ 60 ? (__interceptor_##Function) \ 61 : reinterpret_cast<decltype(&Function)>(dlsym(RTLD_NEXT, #Function))) 62 63 #define LLVM_SYMBOLIZER_INTERCEPTOR1(Function, ...) \ 64 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ 65 GetTypes<__VA_ARGS__>::Arg<0>::Type) __attribute__((weak)); \ 66 GetTypes<__VA_ARGS__>::Result Function( \ 67 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0) { \ 68 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0); \ 69 } 70 71 #define LLVM_SYMBOLIZER_INTERCEPTOR2(Function, ...) \ 72 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ 73 GetTypes<__VA_ARGS__>::Arg<0>::Type, \ 74 GetTypes<__VA_ARGS__>::Arg<1>::Type) __attribute__((weak)); \ 75 GetTypes<__VA_ARGS__>::Result Function( \ 76 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \ 77 GetTypes<__VA_ARGS__>::Arg<1>::Type arg1) { \ 78 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1); \ 79 } 80 81 #define LLVM_SYMBOLIZER_INTERCEPTOR3(Function, ...) \ 82 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ 83 GetTypes<__VA_ARGS__>::Arg<0>::Type, \ 84 GetTypes<__VA_ARGS__>::Arg<1>::Type, \ 85 GetTypes<__VA_ARGS__>::Arg<2>::Type) __attribute__((weak)); \ 86 GetTypes<__VA_ARGS__>::Result Function( \ 87 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \ 88 GetTypes<__VA_ARGS__>::Arg<1>::Type arg1, \ 89 GetTypes<__VA_ARGS__>::Arg<2>::Type arg2) { \ 90 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2); \ 91 } 92 93 #define LLVM_SYMBOLIZER_INTERCEPTOR4(Function, ...) \ 94 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \ 95 GetTypes<__VA_ARGS__>::Arg<0>::Type, \ 96 GetTypes<__VA_ARGS__>::Arg<1>::Type, \ 97 GetTypes<__VA_ARGS__>::Arg<2>::Type, \ 98 GetTypes<__VA_ARGS__>::Arg<3>::Type) __attribute__((weak)); \ 99 GetTypes<__VA_ARGS__>::Result Function( \ 100 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \ 101 GetTypes<__VA_ARGS__>::Arg<1>::Type arg1, \ 102 GetTypes<__VA_ARGS__>::Arg<2>::Type arg2, \ 103 GetTypes<__VA_ARGS__>::Arg<3>::Type arg3) { \ 104 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2, arg3); \ 105 } 106 107 } // namespace 108 109 // C-style interface around internal sanitizer libc functions. 110 extern "C" { 111 112 #define RETURN_OR_SET_ERRNO(T, res) \ 113 int rverrno; \ 114 if (__sanitizer::internal_iserror(res, &rverrno)) { \ 115 errno = rverrno; \ 116 return (T)-1; \ 117 } \ 118 return (T)res; 119 120 int open(const char *filename, int flags, ...) { 121 unsigned long res; 122 if (flags | O_CREAT) { 123 va_list va; 124 va_start(va, flags); 125 unsigned mode = va_arg(va, unsigned); 126 va_end(va); 127 res = __sanitizer::internal_open(filename, flags, mode); 128 } else { 129 res = __sanitizer::internal_open(filename, flags); 130 } 131 RETURN_OR_SET_ERRNO(int, res); 132 } 133 134 int close(int fd) { 135 unsigned long res = __sanitizer::internal_close(fd); 136 RETURN_OR_SET_ERRNO(int, res); 137 } 138 139 #define STAT(func, arg, buf) \ 140 unsigned long res = __sanitizer::internal_##func(arg, buf); \ 141 RETURN_OR_SET_ERRNO(int, res); 142 143 int stat(const char *path, struct stat *buf) { STAT(stat, path, buf); } 144 145 int lstat(const char *path, struct stat *buf) { STAT(lstat, path, buf); } 146 147 int fstat(int fd, struct stat *buf) { STAT(fstat, fd, buf); } 148 149 // Redirect versioned stat functions to the __sanitizer::internal() as well. 150 int __xstat(int version, const char *path, struct stat *buf) { 151 STAT(stat, path, buf); 152 } 153 154 int __lxstat(int version, const char *path, struct stat *buf) { 155 STAT(lstat, path, buf); 156 } 157 158 int __fxstat(int version, int fd, struct stat *buf) { STAT(fstat, fd, buf); } 159 160 size_t strlen(const char *s) { return __sanitizer::internal_strlen(s); } 161 162 void *mmap(void *addr, size_t length, int prot, int flags, int fd, 163 off_t offset) { 164 unsigned long res = __sanitizer::internal_mmap( 165 addr, (unsigned long)length, prot, flags, fd, (unsigned long long)offset); 166 RETURN_OR_SET_ERRNO(void *, res); 167 } 168 169 LLVM_SYMBOLIZER_INTERCEPTOR3(read, ssize_t(int, void *, size_t)) 170 LLVM_SYMBOLIZER_INTERCEPTOR4(pread, ssize_t(int, void *, size_t, off_t)) 171 LLVM_SYMBOLIZER_INTERCEPTOR4(pread64, ssize_t(int, void *, size_t, off64_t)) 172 LLVM_SYMBOLIZER_INTERCEPTOR2(realpath, char *(const char *, char *)) 173 174 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_cond_broadcast, int(pthread_cond_t *)) 175 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_cond_wait, 176 int(pthread_cond_t *, pthread_mutex_t *)) 177 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_lock, int(pthread_mutex_t *)) 178 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_unlock, int(pthread_mutex_t *)) 179 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_destroy, int(pthread_mutex_t *)) 180 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutex_init, 181 int(pthread_mutex_t *, 182 const pthread_mutexattr_t *)) 183 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_destroy, 184 int(pthread_mutexattr_t *)) 185 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_init, int(pthread_mutexattr_t *)) 186 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutexattr_settype, 187 int(pthread_mutexattr_t *, int)) 188 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_getspecific, void *(pthread_key_t)) 189 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_key_create, 190 int(pthread_key_t *, void (*)(void *))) 191 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_once, 192 int(pthread_once_t *, void (*)(void))) 193 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_setspecific, 194 int(pthread_key_t, const void *)) 195 LLVM_SYMBOLIZER_INTERCEPTOR3(pthread_sigmask, 196 int(int, const sigset_t *, sigset_t *)) 197 198 } // extern "C" 199