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