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
20using __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))
81extern "C" {
82// signal.h
83# if ASAN_INTERCEPT_SIGNAL_AND_SIGACTION
84struct sigaction;
85DECLARE_FUNCTION_AND_WRAPPER(int, sigaction, int sig,
86              const struct sigaction *act,
87              struct sigaction *oldact);
88DECLARE_FUNCTION_AND_WRAPPER(void*, signal, int signum, void *handler);
89# endif
90
91// setjmp.h
92DECLARE_FUNCTION_AND_WRAPPER(void, longjmp, void *env, int value);
93# if ASAN_INTERCEPT__LONGJMP
94DECLARE_FUNCTION_AND_WRAPPER(void, _longjmp, void *env, int value);
95# endif
96# if ASAN_INTERCEPT_SIGLONGJMP
97DECLARE_FUNCTION_AND_WRAPPER(void, siglongjmp, void *env, int value);
98# endif
99# if ASAN_INTERCEPT___CXA_THROW
100DECLARE_FUNCTION_AND_WRAPPER(void, __cxa_throw, void *a, void *b, void *c);
101#endif
102
103// string.h / strings.h
104DECLARE_FUNCTION_AND_WRAPPER(int, memcmp,
105                             const void *a1, const void *a2, uptr size);
106DECLARE_FUNCTION_AND_WRAPPER(void*, memmove,
107                             void *to, const void *from, uptr size);
108DECLARE_FUNCTION_AND_WRAPPER(void*, memcpy,
109                             void *to, const void *from, uptr size);
110DECLARE_FUNCTION_AND_WRAPPER(void*, memset, void *block, int c, uptr size);
111DECLARE_FUNCTION_AND_WRAPPER(char*, strchr, const char *str, int c);
112DECLARE_FUNCTION_AND_WRAPPER(char*, strcat,  /* NOLINT */
113                             char *to, const char* from);
114DECLARE_FUNCTION_AND_WRAPPER(char*, strncat,
115                             char *to, const char* from, uptr size);
116DECLARE_FUNCTION_AND_WRAPPER(char*, strcpy,  /* NOLINT */
117                             char *to, const char* from);
118DECLARE_FUNCTION_AND_WRAPPER(char*, strncpy,
119                             char *to, const char* from, uptr size);
120DECLARE_FUNCTION_AND_WRAPPER(int, strcmp, const char *s1, const char* s2);
121DECLARE_FUNCTION_AND_WRAPPER(int, strncmp,
122                             const char *s1, const char* s2, uptr size);
123DECLARE_FUNCTION_AND_WRAPPER(uptr, strlen, const char *s);
124# if ASAN_INTERCEPT_STRCASECMP_AND_STRNCASECMP
125DECLARE_FUNCTION_AND_WRAPPER(int, strcasecmp, const char *s1, const char *s2);
126DECLARE_FUNCTION_AND_WRAPPER(int, strncasecmp,
127                             const char *s1, const char *s2, uptr n);
128# endif
129# if ASAN_INTERCEPT_STRDUP
130DECLARE_FUNCTION_AND_WRAPPER(char*, strdup, const char *s);
131# endif
132# if ASAN_INTERCEPT_STRNLEN
133DECLARE_FUNCTION_AND_WRAPPER(uptr, strnlen, const char *s, uptr maxlen);
134# endif
135#if ASAN_INTERCEPT_INDEX
136DECLARE_FUNCTION_AND_WRAPPER(char*, index, const char *string, int c);
137#endif
138
139// stdlib.h
140DECLARE_FUNCTION_AND_WRAPPER(int, atoi, const char *nptr);
141DECLARE_FUNCTION_AND_WRAPPER(long, atol, const char *nptr);  // NOLINT
142DECLARE_FUNCTION_AND_WRAPPER(long, strtol, const char *nptr, char **endptr, int base);  // NOLINT
143# if ASAN_INTERCEPT_ATOLL_AND_STRTOLL
144DECLARE_FUNCTION_AND_WRAPPER(long long, atoll, const char *nptr);  // NOLINT
145DECLARE_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
150DECLARE_FUNCTION_AND_WRAPPER(int, mlock, const void *addr, size_t len);
151DECLARE_FUNCTION_AND_WRAPPER(int, munlock, const void *addr, size_t len);
152DECLARE_FUNCTION_AND_WRAPPER(int, mlockall, int flags);
153DECLARE_FUNCTION_AND_WRAPPER(int, munlockall, void);
154# endif
155
156// Windows threads.
157# if defined(_WIN32)
158__declspec(dllimport)
159void* __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
164DECLARE_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__)
170typedef void* pthread_workqueue_t;
171typedef void* pthread_workitem_handle_t;
172
173typedef void* dispatch_group_t;
174typedef void* dispatch_queue_t;
175typedef void* dispatch_source_t;
176typedef u64 dispatch_time_t;
177typedef void (*dispatch_function_t)(void *block);
178typedef void* (*worker_t)(void *block);
179typedef void* CFStringRef;
180typedef void* CFAllocatorRef;
181
182DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async_f,
183                             dispatch_queue_t dq,
184                             void *ctxt, dispatch_function_t func);
185DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_sync_f,
186                             dispatch_queue_t dq,
187                             void *ctxt, dispatch_function_t func);
188DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_after_f,
189                             dispatch_time_t when, dispatch_queue_t dq,
190                             void *ctxt, dispatch_function_t func);
191DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_barrier_async_f,
192                             dispatch_queue_t dq,
193                             void *ctxt, dispatch_function_t func);
194DECLARE_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
198DECLARE_FUNCTION_AND_WRAPPER(void, __CFInitialize, void);
199DECLARE_FUNCTION_AND_WRAPPER(CFStringRef, CFStringCreateCopy,
200                             CFAllocatorRef alloc, CFStringRef str);
201DECLARE_FUNCTION_AND_WRAPPER(void, free, void* ptr);
202#if MAC_INTERPOSE_FUNCTIONS
203DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_group_async,
204                             dispatch_group_t dg,
205                             dispatch_queue_t dq, void (^work)(void));
206DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_async,
207                             dispatch_queue_t dq, void (^work)(void));
208DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_after,
209                             dispatch_queue_t dq, void (^work)(void));
210DECLARE_FUNCTION_AND_WRAPPER(void, dispatch_source_set_event_handler,
211                             dispatch_source_t ds, void (^work)(void));
212DECLARE_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