]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_wrappers.cc
Update ACPICA to 20181003.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / symbolizer / sanitizer_wrappers.cc
1 //===-- sanitizer_wrappers.cc -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Redirect some functions to sanitizer interceptors.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include <dlfcn.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <unistd.h>
20
21 #include <tuple>
22
23 // Need to match ../sanitizer_common/sanitizer_internal_defs.h
24 #if defined(ARCH_PPC)
25 #define OFF_T unsigned long
26 #else
27 #define OFF_T unsigned long long
28 #endif
29
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
44
45 namespace {
46
47 template <typename T>
48 struct GetTypes;
49
50 template <typename R, typename... Args>
51 struct GetTypes<R(Args...)> {
52   using Result = R;
53   template <size_t i>
54   struct Arg {
55     using Type = typename std::tuple_element<i, std::tuple<Args...>>::type;
56   };
57 };
58
59 #define LLVM_SYMBOLIZER_GET_FUNC(Function) \
60   ((__interceptor_##Function)              \
61        ? (__interceptor_##Function)        \
62        : reinterpret_cast<decltype(&Function)>(dlsym(RTLD_NEXT, #Function)))
63
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);              \
70   }
71
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);        \
80   }
81
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);  \
92   }
93
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); \
106   }
107
108 }  // namespace
109
110 // C-style interface around internal sanitizer libc functions.
111 extern "C" {
112
113 #define RETURN_OR_SET_ERRNO(T, res)                   \
114   int rverrno;                                        \
115   if (__sanitizer::internal_iserror(res, &rverrno)) { \
116     errno = rverrno;                                  \
117     return (T)-1;                                     \
118   }                                                   \
119   return (T)res;
120
121 int open(const char *filename, int flags, ...) {
122   unsigned long res;
123   if (flags | O_CREAT) {
124     va_list va;
125     va_start(va, flags);
126     unsigned mode = va_arg(va, unsigned);
127     va_end(va);
128     res = __sanitizer::internal_open(filename, flags, mode);
129   } else {
130     res = __sanitizer::internal_open(filename, flags);
131   }
132   RETURN_OR_SET_ERRNO(int, res);
133 }
134
135 int close(int fd) {
136   unsigned long res = __sanitizer::internal_close(fd);
137   RETURN_OR_SET_ERRNO(int, res);
138 }
139
140 #define STAT(func, arg, buf)                                  \
141   unsigned long res = __sanitizer::internal_##func(arg, buf); \
142   RETURN_OR_SET_ERRNO(int, res);
143
144 int stat(const char *path, struct stat *buf) { STAT(stat, path, buf); }
145
146 int lstat(const char *path, struct stat *buf) { STAT(lstat, path, buf); }
147
148 int fstat(int fd, struct stat *buf) { STAT(fstat, fd, buf); }
149
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);
153 }
154
155 int __lxstat(int version, const char *path, struct stat *buf) {
156   STAT(lstat, path, buf);
157 }
158
159 int __fxstat(int version, int fd, struct stat *buf) { STAT(fstat, fd, buf); }
160
161 size_t strlen(const char *s) { return __sanitizer::internal_strlen(s); }
162
163 void *mmap(void *addr, size_t length, int prot, int flags, int fd,
164            off_t offset) {
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);
168 }
169
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 *))
174
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 *))
198
199 }  // extern "C"