2013-02-04 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / asan / asan_intercepted_functions.h
blob2d678ab70005f202396d6cc6a1f05c4c1ba5026d
1 //===-- asan_intercepted_functions.h ----------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan-private header containing prototypes for wrapper functions and wrappers
11 //===----------------------------------------------------------------------===//
12 #ifndef ASAN_INTERCEPTED_FUNCTIONS_H
13 #define ASAN_INTERCEPTED_FUNCTIONS_H
15 #include "asan_internal.h"
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_platform_interceptors.h"
19 #include <stdarg.h>
21 using __sanitizer::uptr;
23 // Use macro to describe if specific function should be
24 // intercepted on a given platform.
25 #if !defined(_WIN32)
26 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
27 # define ASAN_INTERCEPT__LONGJMP 1
28 # define ASAN_INTERCEPT_STRDUP 1
29 # define ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 1
30 # define ASAN_INTERCEPT_INDEX 1
31 # define ASAN_INTERCEPT_PTHREAD_CREATE 1
32 # define ASAN_INTERCEPT_MLOCKX 1
33 #else
34 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
35 # define ASAN_INTERCEPT__LONGJMP 0
36 # define ASAN_INTERCEPT_STRDUP 0
37 # define ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 0
38 # define ASAN_INTERCEPT_INDEX 0
39 # define ASAN_INTERCEPT_PTHREAD_CREATE 0
40 # define ASAN_INTERCEPT_MLOCKX 0
41 #endif
43 #if defined(__linux__)
44 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
45 #else
46 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
47 #endif
49 #if !defined(__APPLE__)
50 # define ASAN_INTERCEPT_STRNLEN 1
51 #else
52 # define ASAN_INTERCEPT_STRNLEN 0
53 #endif
55 #if defined(__linux__) && !defined(ANDROID)
56 # define ASAN_INTERCEPT_SWAPCONTEXT 1
57 #else
58 # define ASAN_INTERCEPT_SWAPCONTEXT 0
59 #endif
61 #if !defined(ANDROID) && !defined(_WIN32)
62 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 1
63 #else
64 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 0
65 #endif
67 // On Darwin siglongjmp tailcalls longjmp, so we don't want to intercept it
68 // there.
69 #if !defined(_WIN32) && (!defined(__APPLE__) || MAC_INTERPOSE_FUNCTIONS)
70 # define ASAN_INTERCEPT_SIGLONGJMP 1
71 #else
72 # define ASAN_INTERCEPT_SIGLONGJMP 0
73 #endif
75 #if ASAN_HAS_EXCEPTIONS && !defined(_WIN32)
76 # define ASAN_INTERCEPT___CXA_THROW 1
77 #else
78 # define ASAN_INTERCEPT___CXA_THROW 0
79 #endif
81 #define DECLARE_FUNCTION_AND_WRAPPER(ret_type, func, ...) \
82 ret_type func(__VA_ARGS__); \
83 ret_type WRAP(func)(__VA_ARGS__)
85 // Use extern declarations of intercepted functions on Mac and Windows
86 // to avoid including system headers.
87 #if defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
88 extern "C" {
89 // signal.h
90 # if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
91 struct sigaction;
92 DECLARE_FUNCTION_AND_WRAPPER(int, sigaction, int sig,
93 const struct sigaction *act,
94 struct sigaction *oldact);
95 DECLARE_FUNCTION_AND_WRAPPER(void*, signal, int signum, void *handler);
96 # endif
98 // setjmp.h
99 DECLARE_FUNCTION_AND_WRAPPER(void, longjmp, void *env, int value);
100 # if ASAN_INTERCEPT__LONGJMP
101 DECLARE_FUNCTION_AND_WRAPPER(void, _longjmp, void *env, int value);
102 # endif
103 # if ASAN_INTERCEPT_SIGLONGJMP
104 DECLARE_FUNCTION_AND_WRAPPER(void, siglongjmp, void *env, int value);
105 # endif
106 # if ASAN_INTERCEPT___CXA_THROW
107 DECLARE_FUNCTION_AND_WRAPPER(void, __cxa_throw, void *a, void *b, void *c);
108 # endif
110 // string.h / strings.h
111 DECLARE_FUNCTION_AND_WRAPPER(int, memcmp,
112 const void *a1, const void *a2, uptr size);
113 DECLARE_FUNCTION_AND_WRAPPER(void*, memmove,
114 void *to, const void *from, uptr size);
115 DECLARE_FUNCTION_AND_WRAPPER(void*, memcpy,
116 void *to, const void *from, uptr size);
117 DECLARE_FUNCTION_AND_WRAPPER(void*, memset, void *block, int c, uptr size);
118 DECLARE_FUNCTION_AND_WRAPPER(char*, strchr, const char *str, int c);
119 DECLARE_FUNCTION_AND_WRAPPER(char*, strcat, /* NOLINT */
120 char *to, const char* from);
121 DECLARE_FUNCTION_AND_WRAPPER(char*, strncat,
122 char *to, const char* from, uptr size);
123 DECLARE_FUNCTION_AND_WRAPPER(char*, strcpy, /* NOLINT */
124 char *to, const char* from);
125 DECLARE_FUNCTION_AND_WRAPPER(char*, strncpy,
126 char *to, const char* from, uptr size);
127 DECLARE_FUNCTION_AND_WRAPPER(int, strcmp, const char *s1, const char* s2);
128 DECLARE_FUNCTION_AND_WRAPPER(int, strncmp,
129 const char *s1, const char* s2, uptr size);
130 DECLARE_FUNCTION_AND_WRAPPER(uptr, strlen, const char *s);
131 # if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
132 DECLARE_FUNCTION_AND_WRAPPER(int, strcasecmp, const char *s1, const char *s2);
133 DECLARE_FUNCTION_AND_WRAPPER(int, strncasecmp,
134 const char *s1, const char *s2, uptr n);
135 # endif
136 # if ASAN_INTERCEPT_STRDUP
137 DECLARE_FUNCTION_AND_WRAPPER(char*, strdup, const char *s);
138 # endif
139 # if ASAN_INTERCEPT_STRNLEN
140 DECLARE_FUNCTION_AND_WRAPPER(uptr, strnlen, const char *s, uptr maxlen);
141 # endif
142 # if ASAN_INTERCEPT_INDEX
143 DECLARE_FUNCTION_AND_WRAPPER(char*, index, const char *string, int c);
144 # endif
146 // stdlib.h
147 DECLARE_FUNCTION_AND_WRAPPER(int, atoi, const char *nptr);
148 DECLARE_FUNCTION_AND_WRAPPER(long, atol, const char *nptr); // NOLINT
149 DECLARE_FUNCTION_AND_WRAPPER(long, strtol, const char *nptr, char **endptr, int base); // NOLINT
150 # if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
151 DECLARE_FUNCTION_AND_WRAPPER(long long, atoll, const char *nptr); // NOLINT
152 DECLARE_FUNCTION_AND_WRAPPER(long long, strtoll, const char *nptr, char **endptr, int base); // NOLINT
153 # endif
155 // unistd.h
156 # if SANITIZER_INTERCEPT_READ
157 DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, read, int fd, void *buf, SIZE_T count);
158 # endif
159 # if SANITIZER_INTERCEPT_PREAD
160 DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, pread, int fd, void *buf,
161 SIZE_T count, OFF_T offset);
162 # endif
163 # if SANITIZER_INTERCEPT_PREAD64
164 DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, pread64, int fd, void *buf,
165 SIZE_T count, OFF64_T offset);
166 # endif
168 # if SANITIZER_INTERCEPT_WRITE
169 DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, write, int fd, void *ptr, SIZE_T count);
170 # endif
171 # if SANITIZER_INTERCEPT_PWRITE
172 DECLARE_FUNCTION_AND_WRAPPER(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count);
173 # endif
175 # if ASAN_INTERCEPT_MLOCKX
176 // mlock/munlock
177 DECLARE_FUNCTION_AND_WRAPPER(int, mlock, const void *addr, SIZE_T len);
178 DECLARE_FUNCTION_AND_WRAPPER(int, munlock, const void *addr, SIZE_T len);
179 DECLARE_FUNCTION_AND_WRAPPER(int, mlockall, int flags);
180 DECLARE_FUNCTION_AND_WRAPPER(int, munlockall, void);
181 # endif
183 // Windows threads.
184 # if defined(_WIN32)
185 __declspec(dllimport)
186 void* __stdcall CreateThread(void *sec, uptr st, void* start,
187 void *arg, DWORD fl, DWORD *id);
188 # endif
189 // Posix threads.
190 # if ASAN_INTERCEPT_PTHREAD_CREATE
191 DECLARE_FUNCTION_AND_WRAPPER(int, pthread_create,
192 void *thread, void *attr,
193 void *(*start_routine)(void*), void *arg);
194 # endif
196 DECLARE_FUNCTION_AND_WRAPPER(int, vscanf, const char *format, va_list ap);
197 DECLARE_FUNCTION_AND_WRAPPER(int, vsscanf, const char *str, const char *format,
198 va_list ap);
199 DECLARE_FUNCTION_AND_WRAPPER(int, vfscanf, void *stream, const char *format,
200 va_list ap);
201 DECLARE_FUNCTION_AND_WRAPPER(int, scanf, const char *format, ...);
202 DECLARE_FUNCTION_AND_WRAPPER(int, fscanf,
203 void* stream, const char *format, ...);
204 DECLARE_FUNCTION_AND_WRAPPER(int, sscanf, // NOLINT
205 const char *str, const char *format, ...);
207 # if defined(__APPLE__)
208 typedef void* pthread_workqueue_t;
209 typedef void* pthread_workitem_handle_t;
211 typedef void* dispatch_group_t;
212 typedef void* dispatch_queue_t;
213 typedef void* dispatch_source_t;
214 typedef u64 dispatch_time_t;
215 typedef void (*dispatch_function_t)(void *block);
216 typedef void* (*worker_t)(void *block);
218 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async_f,
219 dispatch_queue_t dq,
220 void *ctxt, dispatch_function_t func);
221 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_sync_f,
222 dispatch_queue_t dq,
223 void *ctxt, dispatch_function_t func);
224 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_after_f,
225 dispatch_time_t when, dispatch_queue_t dq,
226 void *ctxt, dispatch_function_t func);
227 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_barrier_async_f,
228 dispatch_queue_t dq,
229 void *ctxt, dispatch_function_t func);
230 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_group_async_f,
231 dispatch_group_t group, dispatch_queue_t dq,
232 void *ctxt, dispatch_function_t func);
234 # if MAC_INTERPOSE_FUNCTIONS && !defined(MISSING_BLOCKS_SUPPORT)
235 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_group_async,
236 dispatch_group_t dg,
237 dispatch_queue_t dq, void (^work)(void));
238 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async,
239 dispatch_queue_t dq, void (^work)(void));
240 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_after,
241 dispatch_queue_t dq, void (^work)(void));
242 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_source_set_event_handler,
243 dispatch_source_t ds, void (^work)(void));
244 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_source_set_cancel_handler,
245 dispatch_source_t ds, void (^work)(void));
246 # endif // MAC_INTERPOSE_FUNCTIONS
248 typedef void malloc_zone_t;
249 typedef size_t vm_size_t;
250 DECLARE_FUNCTION_AND_WRAPPER(malloc_zone_t *, malloc_create_zone,
251 vm_size_t start_size, unsigned flags);
252 DECLARE_FUNCTION_AND_WRAPPER(malloc_zone_t *, malloc_default_zone, void);
253 DECLARE_FUNCTION_AND_WRAPPER(
254 malloc_zone_t *, malloc_default_purgeable_zone, void);
255 DECLARE_FUNCTION_AND_WRAPPER(void, malloc_make_purgeable, void *ptr);
256 DECLARE_FUNCTION_AND_WRAPPER(int, malloc_make_nonpurgeable, void *ptr);
257 DECLARE_FUNCTION_AND_WRAPPER(void, malloc_set_zone_name,
258 malloc_zone_t *zone, const char *name);
259 DECLARE_FUNCTION_AND_WRAPPER(void *, malloc, size_t size);
260 DECLARE_FUNCTION_AND_WRAPPER(void, free, void *ptr);
261 DECLARE_FUNCTION_AND_WRAPPER(void *, realloc, void *ptr, size_t size);
262 DECLARE_FUNCTION_AND_WRAPPER(void *, calloc, size_t nmemb, size_t size);
263 DECLARE_FUNCTION_AND_WRAPPER(void *, valloc, size_t size);
264 DECLARE_FUNCTION_AND_WRAPPER(size_t, malloc_good_size, size_t size);
265 DECLARE_FUNCTION_AND_WRAPPER(int, posix_memalign,
266 void **memptr, size_t alignment, size_t size);
267 DECLARE_FUNCTION_AND_WRAPPER(void, _malloc_fork_prepare, void);
268 DECLARE_FUNCTION_AND_WRAPPER(void, _malloc_fork_parent, void);
269 DECLARE_FUNCTION_AND_WRAPPER(void, _malloc_fork_child, void);
273 # endif // __APPLE__
274 } // extern "C"
275 #endif // defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
277 #endif // ASAN_INTERCEPTED_FUNCTIONS_H