1 //===-- sanitizer_wrappers.cc -----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Redirect some functions to sanitizer interceptors.
11 //===----------------------------------------------------------------------===//
22 // Need to match ../sanitizer_common/sanitizer_internal_defs.h
24 #define OFF_T unsigned long
26 #define OFF_T unsigned long long
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
49 template <typename R, typename... Args>
50 struct GetTypes<R(Args...)> {
54 using Type = typename std::tuple_element<i, std::tuple<Args...>>::type;
58 #define LLVM_SYMBOLIZER_GET_FUNC(Function) \
59 ((__interceptor_##Function) \
60 ? (__interceptor_##Function) \
61 : reinterpret_cast<decltype(&Function)>(dlsym(RTLD_NEXT, #Function)))
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); \
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); \
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); \
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); \
109 // C-style interface around internal sanitizer libc functions.
112 #define RETURN_OR_SET_ERRNO(T, res) \
114 if (__sanitizer::internal_iserror(res, &rverrno)) { \
120 int open(const char *filename, int flags, ...) {
122 if (flags | O_CREAT) {
125 unsigned mode = va_arg(va, unsigned);
127 res = __sanitizer::internal_open(filename, flags, mode);
129 res = __sanitizer::internal_open(filename, flags);
131 RETURN_OR_SET_ERRNO(int, res);
135 unsigned long res = __sanitizer::internal_close(fd);
136 RETURN_OR_SET_ERRNO(int, res);
139 #define STAT(func, arg, buf) \
140 unsigned long res = __sanitizer::internal_##func(arg, buf); \
141 RETURN_OR_SET_ERRNO(int, res);
143 int stat(const char *path, struct stat *buf) { STAT(stat, path, buf); }
145 int lstat(const char *path, struct stat *buf) { STAT(lstat, path, buf); }
147 int fstat(int fd, struct stat *buf) { STAT(fstat, fd, buf); }
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);
154 int __lxstat(int version, const char *path, struct stat *buf) {
155 STAT(lstat, path, buf);
158 int __fxstat(int version, int fd, struct stat *buf) { STAT(fstat, fd, buf); }
160 size_t strlen(const char *s) { return __sanitizer::internal_strlen(s); }
162 void *mmap(void *addr, size_t length, int prot, int flags, int fd,
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);
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 *))
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 *))