1 //===-- sanitizer_wrappers.cc -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Redirect some functions to sanitizer interceptors.
12 //===----------------------------------------------------------------------===//
23 // Need to match ../sanitizer_common/sanitizer_internal_defs.h
25 #define OFF_T unsigned long
27 #define OFF_T unsigned long long
30 namespace __sanitizer {
31 unsigned long internal_open(const char *filename, int flags);
32 unsigned long internal_open(const char *filename, int flags, unsigned mode);
33 unsigned long internal_close(int fd);
34 unsigned long internal_stat(const char *path, void *buf);
35 unsigned long internal_lstat(const char *path, void *buf);
36 unsigned long internal_fstat(int fd, void *buf);
37 size_t internal_strlen(const char *s);
38 unsigned long internal_mmap(void *addr, unsigned long length, int prot,
39 int flags, int fd, OFF_T offset);
40 void *internal_memcpy(void *dest, const void *src, unsigned long n);
41 // Used to propagate errno.
42 bool internal_iserror(unsigned long retval, int *rverrno = 0);
43 } // namespace __sanitizer
50 template <typename R, typename... Args>
51 struct GetTypes<R(Args...)> {
55 using Type = typename std::tuple_element<i, std::tuple<Args...>>::type;
59 #define LLVM_SYMBOLIZER_GET_FUNC(Function) \
60 ((__interceptor_##Function) \
61 ? (__interceptor_##Function) \
62 : reinterpret_cast<decltype(&Function)>(dlsym(RTLD_NEXT, #Function)))
64 #define LLVM_SYMBOLIZER_INTERCEPTOR1(Function, ...) \
65 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \
66 GetTypes<__VA_ARGS__>::Arg<0>::Type) __attribute__((weak)); \
67 GetTypes<__VA_ARGS__>::Result Function( \
68 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0) { \
69 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0); \
72 #define LLVM_SYMBOLIZER_INTERCEPTOR2(Function, ...) \
73 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \
74 GetTypes<__VA_ARGS__>::Arg<0>::Type, \
75 GetTypes<__VA_ARGS__>::Arg<1>::Type) __attribute__((weak)); \
76 GetTypes<__VA_ARGS__>::Result Function( \
77 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \
78 GetTypes<__VA_ARGS__>::Arg<1>::Type arg1) { \
79 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1); \
82 #define LLVM_SYMBOLIZER_INTERCEPTOR3(Function, ...) \
83 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \
84 GetTypes<__VA_ARGS__>::Arg<0>::Type, \
85 GetTypes<__VA_ARGS__>::Arg<1>::Type, \
86 GetTypes<__VA_ARGS__>::Arg<2>::Type) __attribute__((weak)); \
87 GetTypes<__VA_ARGS__>::Result Function( \
88 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \
89 GetTypes<__VA_ARGS__>::Arg<1>::Type arg1, \
90 GetTypes<__VA_ARGS__>::Arg<2>::Type arg2) { \
91 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2); \
94 #define LLVM_SYMBOLIZER_INTERCEPTOR4(Function, ...) \
95 GetTypes<__VA_ARGS__>::Result __interceptor_##Function( \
96 GetTypes<__VA_ARGS__>::Arg<0>::Type, \
97 GetTypes<__VA_ARGS__>::Arg<1>::Type, \
98 GetTypes<__VA_ARGS__>::Arg<2>::Type, \
99 GetTypes<__VA_ARGS__>::Arg<3>::Type) __attribute__((weak)); \
100 GetTypes<__VA_ARGS__>::Result Function( \
101 GetTypes<__VA_ARGS__>::Arg<0>::Type arg0, \
102 GetTypes<__VA_ARGS__>::Arg<1>::Type arg1, \
103 GetTypes<__VA_ARGS__>::Arg<2>::Type arg2, \
104 GetTypes<__VA_ARGS__>::Arg<3>::Type arg3) { \
105 return LLVM_SYMBOLIZER_GET_FUNC(Function)(arg0, arg1, arg2, arg3); \
110 // C-style interface around internal sanitizer libc functions.
113 #define RETURN_OR_SET_ERRNO(T, res) \
115 if (__sanitizer::internal_iserror(res, &rverrno)) { \
121 int open(const char *filename, int flags, ...) {
123 if (flags | O_CREAT) {
126 unsigned mode = va_arg(va, unsigned);
128 res = __sanitizer::internal_open(filename, flags, mode);
130 res = __sanitizer::internal_open(filename, flags);
132 RETURN_OR_SET_ERRNO(int, res);
136 unsigned long res = __sanitizer::internal_close(fd);
137 RETURN_OR_SET_ERRNO(int, res);
140 #define STAT(func, arg, buf) \
141 unsigned long res = __sanitizer::internal_##func(arg, buf); \
142 RETURN_OR_SET_ERRNO(int, res);
144 int stat(const char *path, struct stat *buf) { STAT(stat, path, buf); }
146 int lstat(const char *path, struct stat *buf) { STAT(lstat, path, buf); }
148 int fstat(int fd, struct stat *buf) { STAT(fstat, fd, buf); }
150 // Redirect versioned stat functions to the __sanitizer::internal() as well.
151 int __xstat(int version, const char *path, struct stat *buf) {
152 STAT(stat, path, buf);
155 int __lxstat(int version, const char *path, struct stat *buf) {
156 STAT(lstat, path, buf);
159 int __fxstat(int version, int fd, struct stat *buf) { STAT(fstat, fd, buf); }
161 size_t strlen(const char *s) { return __sanitizer::internal_strlen(s); }
163 void *mmap(void *addr, size_t length, int prot, int flags, int fd,
165 unsigned long res = __sanitizer::internal_mmap(
166 addr, (unsigned long)length, prot, flags, fd, (unsigned long long)offset);
167 RETURN_OR_SET_ERRNO(void *, res);
170 LLVM_SYMBOLIZER_INTERCEPTOR3(read, ssize_t(int, void *, size_t))
171 LLVM_SYMBOLIZER_INTERCEPTOR4(pread, ssize_t(int, void *, size_t, off_t))
172 LLVM_SYMBOLIZER_INTERCEPTOR4(pread64, ssize_t(int, void *, size_t, off64_t))
173 LLVM_SYMBOLIZER_INTERCEPTOR2(realpath, char *(const char *, char *))
175 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_cond_broadcast, int(pthread_cond_t *))
176 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_cond_wait,
177 int(pthread_cond_t *, pthread_mutex_t *))
178 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_lock, int(pthread_mutex_t *))
179 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_unlock, int(pthread_mutex_t *))
180 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutex_destroy, int(pthread_mutex_t *))
181 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutex_init,
182 int(pthread_mutex_t *,
183 const pthread_mutexattr_t *))
184 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_destroy,
185 int(pthread_mutexattr_t *))
186 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_mutexattr_init, int(pthread_mutexattr_t *))
187 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_mutexattr_settype,
188 int(pthread_mutexattr_t *, int))
189 LLVM_SYMBOLIZER_INTERCEPTOR1(pthread_getspecific, void *(pthread_key_t))
190 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_key_create,
191 int(pthread_key_t *, void (*)(void *)))
192 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_once,
193 int(pthread_once_t *, void (*)(void)))
194 LLVM_SYMBOLIZER_INTERCEPTOR2(pthread_setspecific,
195 int(pthread_key_t, const void *))
196 LLVM_SYMBOLIZER_INTERCEPTOR3(pthread_sigmask,
197 int(int, const sigset_t *, sigset_t *))