* configure.ac: Change target-libasan to target-libsanitizer.
[official-gcc.git] / libsanitizer / asan / asan_intercepted_functions.h
blobceb596cd48ecea2e309e7cddc5c85391b4703790
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"
18 using __sanitizer::uptr;
20 // Use macro to describe if specific function should be
21 // intercepted on a given platform.
22 #if !defined(_WIN32)
23 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
24 # define ASAN_INTERCEPT__LONGJMP 1
25 # define ASAN_INTERCEPT_STRDUP 1
26 # define ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 1
27 # define ASAN_INTERCEPT_INDEX 1
28 # define ASAN_INTERCEPT_PTHREAD_CREATE 1
29 # define ASAN_INTERCEPT_MLOCKX 1
30 #else
31 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
32 # define ASAN_INTERCEPT__LONGJMP 0
33 # define ASAN_INTERCEPT_STRDUP 0
34 # define ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP 0
35 # define ASAN_INTERCEPT_INDEX 0
36 # define ASAN_INTERCEPT_PTHREAD_CREATE 0
37 # define ASAN_INTERCEPT_MLOCKX 0
38 #endif
40 #if defined(__linux__)
41 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
42 #else
43 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
44 #endif
46 #if !defined(__APPLE__)
47 # define ASAN_INTERCEPT_STRNLEN 1
48 #else
49 # define ASAN_INTERCEPT_STRNLEN 0
50 #endif
52 #if !defined(ANDROID) && !defined(_WIN32)
53 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 1
54 #else
55 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 0
56 #endif
58 // On Darwin siglongjmp tailcalls longjmp, so we don't want to intercept it
59 // there.
60 #if !defined(_WIN32) && (!defined(__APPLE__) || MAC_INTERPOSE_FUNCTIONS)
61 # define ASAN_INTERCEPT_SIGLONGJMP 1
62 #else
63 # define ASAN_INTERCEPT_SIGLONGJMP 0
64 #endif
66 #if ASAN_HAS_EXCEPTIONS && !defined(_WIN32)
67 # define ASAN_INTERCEPT___CXA_THROW 1
68 #else
69 # define ASAN_INTERCEPT___CXA_THROW 0
70 #endif
72 #define DECLARE_FUNCTION_AND_WRAPPER(ret_type, func, ...) \
73 ret_type func(__VA_ARGS__); \
74 ret_type WRAP(func)(__VA_ARGS__)
76 // Use extern declarations of intercepted functions on Mac and Windows
77 // to avoid including system headers.
78 #if defined(__APPLE__) || (defined(_WIN32) && !defined(_DLL))
79 extern "C" {
80 // signal.h
81 # if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
82 struct sigaction;
83 DECLARE_FUNCTION_AND_WRAPPER(int, sigaction, int sig,
84 const struct sigaction *act,
85 struct sigaction *oldact);
86 DECLARE_FUNCTION_AND_WRAPPER(void*, signal, int signum, void *handler);
87 # endif
89 // setjmp.h
90 DECLARE_FUNCTION_AND_WRAPPER(void, longjmp, void *env, int value);
91 # if ASAN_INTERCEPT__LONGJMP
92 DECLARE_FUNCTION_AND_WRAPPER(void, _longjmp, void *env, int value);
93 # endif
94 # if ASAN_INTERCEPT_SIGLONGJMP
95 DECLARE_FUNCTION_AND_WRAPPER(void, siglongjmp, void *env, int value);
96 # endif
97 # if ASAN_INTERCEPT___CXA_THROW
98 DECLARE_FUNCTION_AND_WRAPPER(void, __cxa_throw, void *a, void *b, void *c);
99 #endif
101 // string.h / strings.h
102 DECLARE_FUNCTION_AND_WRAPPER(int, memcmp,
103 const void *a1, const void *a2, uptr size);
104 DECLARE_FUNCTION_AND_WRAPPER(void*, memmove,
105 void *to, const void *from, uptr size);
106 DECLARE_FUNCTION_AND_WRAPPER(void*, memcpy,
107 void *to, const void *from, uptr size);
108 DECLARE_FUNCTION_AND_WRAPPER(void*, memset, void *block, int c, uptr size);
109 DECLARE_FUNCTION_AND_WRAPPER(char*, strchr, const char *str, int c);
110 DECLARE_FUNCTION_AND_WRAPPER(char*, strcat, /* NOLINT */
111 char *to, const char* from);
112 DECLARE_FUNCTION_AND_WRAPPER(char*, strncat,
113 char *to, const char* from, uptr size);
114 DECLARE_FUNCTION_AND_WRAPPER(char*, strcpy, /* NOLINT */
115 char *to, const char* from);
116 DECLARE_FUNCTION_AND_WRAPPER(char*, strncpy,
117 char *to, const char* from, uptr size);
118 DECLARE_FUNCTION_AND_WRAPPER(int, strcmp, const char *s1, const char* s2);
119 DECLARE_FUNCTION_AND_WRAPPER(int, strncmp,
120 const char *s1, const char* s2, uptr size);
121 DECLARE_FUNCTION_AND_WRAPPER(uptr, strlen, const char *s);
122 # if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
123 DECLARE_FUNCTION_AND_WRAPPER(int, strcasecmp, const char *s1, const char *s2);
124 DECLARE_FUNCTION_AND_WRAPPER(int, strncasecmp,
125 const char *s1, const char *s2, uptr n);
126 # endif
127 # if ASAN_INTERCEPT_STRDUP
128 DECLARE_FUNCTION_AND_WRAPPER(char*, strdup, const char *s);
129 # endif
130 # if ASAN_INTERCEPT_STRNLEN
131 DECLARE_FUNCTION_AND_WRAPPER(uptr, strnlen, const char *s, uptr maxlen);
132 # endif
133 #if ASAN_INTERCEPT_INDEX
134 DECLARE_FUNCTION_AND_WRAPPER(char*, index, const char *string, int c);
135 #endif
137 // stdlib.h
138 DECLARE_FUNCTION_AND_WRAPPER(int, atoi, const char *nptr);
139 DECLARE_FUNCTION_AND_WRAPPER(long, atol, const char *nptr); // NOLINT
140 DECLARE_FUNCTION_AND_WRAPPER(long, strtol, const char *nptr, char **endptr, int base); // NOLINT
141 # if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
142 DECLARE_FUNCTION_AND_WRAPPER(long long, atoll, const char *nptr); // NOLINT
143 DECLARE_FUNCTION_AND_WRAPPER(long long, strtoll, const char *nptr, char **endptr, int base); // NOLINT
144 # endif
146 # if ASAN_INTERCEPT_MLOCKX
147 // mlock/munlock
148 DECLARE_FUNCTION_AND_WRAPPER(int, mlock, const void *addr, size_t len);
149 DECLARE_FUNCTION_AND_WRAPPER(int, munlock, const void *addr, size_t len);
150 DECLARE_FUNCTION_AND_WRAPPER(int, mlockall, int flags);
151 DECLARE_FUNCTION_AND_WRAPPER(int, munlockall, void);
152 # endif
154 // Windows threads.
155 # if defined(_WIN32)
156 __declspec(dllimport)
157 void* __stdcall CreateThread(void *sec, uptr st, void* start,
158 void *arg, DWORD fl, DWORD *id);
159 # endif
160 // Posix threads.
161 # if ASAN_INTERCEPT_PTHREAD_CREATE
162 DECLARE_FUNCTION_AND_WRAPPER(int, pthread_create,
163 void *thread, void *attr,
164 void *(*start_routine)(void*), void *arg);
165 # endif
167 #if defined(__APPLE__)
168 typedef void* pthread_workqueue_t;
169 typedef void* pthread_workitem_handle_t;
171 typedef void* dispatch_group_t;
172 typedef void* dispatch_queue_t;
173 typedef void* dispatch_source_t;
174 typedef u64 dispatch_time_t;
175 typedef void (*dispatch_function_t)(void *block);
176 typedef void* (*worker_t)(void *block);
177 typedef void* CFStringRef;
178 typedef void* CFAllocatorRef;
180 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async_f,
181 dispatch_queue_t dq,
182 void *ctxt, dispatch_function_t func);
183 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_sync_f,
184 dispatch_queue_t dq,
185 void *ctxt, dispatch_function_t func);
186 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_after_f,
187 dispatch_time_t when, dispatch_queue_t dq,
188 void *ctxt, dispatch_function_t func);
189 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_barrier_async_f,
190 dispatch_queue_t dq,
191 void *ctxt, dispatch_function_t func);
192 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_group_async_f,
193 dispatch_group_t group, dispatch_queue_t dq,
194 void *ctxt, dispatch_function_t func);
196 DECLARE_FUNCTION_AND_WRAPPER(void, __CFInitialize, void);
197 DECLARE_FUNCTION_AND_WRAPPER(CFStringRef, CFStringCreateCopy,
198 CFAllocatorRef alloc, CFStringRef str);
199 DECLARE_FUNCTION_AND_WRAPPER(void, free, void* ptr);
200 #if MAC_INTERPOSE_FUNCTIONS
201 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_group_async,
202 dispatch_group_t dg,
203 dispatch_queue_t dq, void (^work)(void));
204 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async,
205 dispatch_queue_t dq, void (^work)(void));
206 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_after,
207 dispatch_queue_t dq, void (^work)(void));
208 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_source_set_event_handler,
209 dispatch_source_t ds, void (^work)(void));
210 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_source_set_cancel_handler,
211 dispatch_source_t ds, void (^work)(void));
212 #endif // MAC_INTERPOSE_FUNCTIONS
213 #endif // __APPLE__
214 } // extern "C"
215 #endif
217 #endif // ASAN_INTERCEPTED_FUNCTIONS_H