1 //===-- asan_intercepted_functions.h ----------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
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.
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
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
40 #if defined(__linux__)
41 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
43 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
46 #if !defined(__APPLE__)
47 # define ASAN_INTERCEPT_STRNLEN 1
49 # define ASAN_INTERCEPT_STRNLEN 0
52 #if defined(__linux__) && !defined(ANDROID)
53 # define ASAN_INTERCEPT_SWAPCONTEXT 1
55 # define ASAN_INTERCEPT_SWAPCONTEXT 0
58 #if !defined(ANDROID) && !defined(_WIN32)
59 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 1
61 # define ASAN_INTERCEPT_SIGNAL_AND_SIGACTION 0
64 // On Darwin siglongjmp tailcalls longjmp, so we don't want to intercept it
66 #if !defined(_WIN32) && (!defined(__APPLE__) || MAC_INTERPOSE_FUNCTIONS)
67 # define ASAN_INTERCEPT_SIGLONGJMP 1
69 # define ASAN_INTERCEPT_SIGLONGJMP 0
72 #if ASAN_HAS_EXCEPTIONS && !defined(_WIN32)
73 # define ASAN_INTERCEPT___CXA_THROW 1
75 # define ASAN_INTERCEPT___CXA_THROW 0
78 #define DECLARE_FUNCTION_AND_WRAPPER(ret_type, func, ...) \
79 ret_type func(__VA_ARGS__); \
80 ret_type WRAP(func)(__VA_ARGS__)
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))
87 # if ASAN_INTERCEPT_SIGNAL_AND_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
);
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
);
100 # if ASAN_INTERCEPT_SIGLONGJMP
101 DECLARE_FUNCTION_AND_WRAPPER(void, siglongjmp
, void *env
, int value
);
103 # if ASAN_INTERCEPT___CXA_THROW
104 DECLARE_FUNCTION_AND_WRAPPER(void, __cxa_throw
, void *a
, void *b
, void *c
);
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
);
133 # if ASAN_INTERCEPT_STRDUP
134 DECLARE_FUNCTION_AND_WRAPPER(char*, strdup
, const char *s
);
136 # if ASAN_INTERCEPT_STRNLEN
137 DECLARE_FUNCTION_AND_WRAPPER(uptr
, strnlen
, const char *s
, uptr maxlen
);
139 #if ASAN_INTERCEPT_INDEX
140 DECLARE_FUNCTION_AND_WRAPPER(char*, index
, const char *string
, int c
);
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
152 # if ASAN_INTERCEPT_MLOCKX
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);
162 __declspec(dllimport
)
163 void* __stdcall
CreateThread(void *sec
, uptr st
, void* start
,
164 void *arg
, DWORD fl
, DWORD
*id
);
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
);
173 #if defined(__APPLE__)
174 typedef void* pthread_workqueue_t
;
175 typedef void* pthread_workitem_handle_t
;
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
;
186 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async_f
,
188 void *ctxt
, dispatch_function_t func
);
189 DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_sync_f
,
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
,
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
);
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
,
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
223 #endif // ASAN_INTERCEPTED_FUNCTIONS_H