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