blob: 9e9aca215c4d053a76ed97fd0f20467959bbe506 [file] [log] [blame]
Alexey Samsonov5b290182012-02-08 19:52:01 +00001//===-- interception.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// Machinery for providing replacements/wrappers for system functions.
13//===----------------------------------------------------------------------===//
14
15#ifndef INTERCEPTION_H
16#define INTERCEPTION_H
17
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018#if !defined(__linux__) && !defined(__FreeBSD__) && \
19 !defined(__APPLE__) && !defined(_WIN32)
Alexey Samsonov5b290182012-02-08 19:52:01 +000020# error "Interception doesn't work on this operating system."
21#endif
22
Alexey Samsonov216719b2013-01-30 14:27:41 +000023#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000024
25// These typedefs should be used only in the interceptor definitions to replace
26// the standard system types (e.g. SSIZE_T instead of ssize_t)
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000027typedef __sanitizer::uptr SIZE_T;
28typedef __sanitizer::sptr SSIZE_T;
29typedef __sanitizer::sptr PTRDIFF_T;
30typedef __sanitizer::s64 INTMAX_T;
31typedef __sanitizer::OFF_T OFF_T;
32typedef __sanitizer::OFF64_T OFF64_T;
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000033
Alexey Samsonov5b290182012-02-08 19:52:01 +000034// How to add an interceptor:
35// Suppose you need to wrap/replace system function (generally, from libc):
36// int foo(const char *bar, double baz);
37// You'll need to:
38// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
Alexander Potapenko6a659df2013-05-20 13:32:35 +000039// your source file. See the notes below for cases when
40// INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
Alexey Samsonov5b290182012-02-08 19:52:01 +000041// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
42// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
43// intercepted successfully.
44// You can access original function by calling REAL(foo)(bar, baz).
45// By default, REAL(foo) will be visible only inside your interceptor, and if
46// you want to use it in other parts of RTL, you'll need to:
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000047// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
Alexey Samsonov5b290182012-02-08 19:52:01 +000048// header file.
49// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
50// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000051// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
Alexey Samsonov5b290182012-02-08 19:52:01 +000052// to a header file.
53
Alexander Potapenko50a002e2013-02-21 14:41:16 +000054// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000055// DECLARE_REAL(...) are located inside namespaces.
Alexander Potapenko50a002e2013-02-21 14:41:16 +000056// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
Alexey Samsonov5b290182012-02-08 19:52:01 +000057// effectively redirect calls from "foo" to "zoo". In this case
58// you aren't required to implement
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000059// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
Alexey Samsonov5b290182012-02-08 19:52:01 +000060// but instead you'll have to add
Alexander Potapenko50a002e2013-02-21 14:41:16 +000061// DECLARE_REAL(int, foo, const char *bar, double baz) in your
Alexey Samsonov5b290182012-02-08 19:52:01 +000062// source file (to define a pointer to overriden function).
Alexander Potapenko6a659df2013-05-20 13:32:35 +000063// 3. Some Mac functions have symbol variants discriminated by
64// additional suffixes, e.g. _$UNIX2003 (see
65// https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
66// for more details). To intercept such functions you need to use the
67// INTERCEPTOR_WITH_SUFFIX(...) macro.
Alexey Samsonov5b290182012-02-08 19:52:01 +000068
69// How it works:
Dmitry Vyukov580469d2012-05-24 13:54:31 +000070// To replace system functions on Linux we just need to declare functions
Alexey Samsonov5b290182012-02-08 19:52:01 +000071// with same names in our library and then obtain the real function pointers
Dmitry Vyukov580469d2012-05-24 13:54:31 +000072// using dlsym().
73// There is one complication. A user may also intercept some of the functions
Dmitry Vyukovf54c0e32012-05-28 07:47:35 +000074// we intercept. To resolve this we declare our interceptors with __interceptor_
75// prefix, and then make actual interceptors weak aliases to __interceptor_
Dmitry Vyukov580469d2012-05-24 13:54:31 +000076// functions.
Alexander Potapenko50a002e2013-02-21 14:41:16 +000077//
Dmitry Vyukov580469d2012-05-24 13:54:31 +000078// This is not so on Mac OS, where the two-level namespace makes
Alexey Samsonov5b290182012-02-08 19:52:01 +000079// our replacement functions invisible to other libraries. This may be overcomed
80// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
Alexander Potapenko69563982013-02-05 15:57:12 +000081// libraries in Chromium were noticed when doing so.
82// Instead we create a dylib containing a __DATA,__interpose section that
83// associates library functions with their wrappers. When this dylib is
84// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
85// the calls to interposed functions done through stubs to the wrapper
86// functions.
Alexander Potapenko50a002e2013-02-21 14:41:16 +000087// As it's decided at compile time which functions are to be intercepted on Mac,
88// INTERCEPT_FUNCTION() is effectively a no-op on this system.
Alexey Samsonov5b290182012-02-08 19:52:01 +000089
90#if defined(__APPLE__)
Alexander Potapenko6a659df2013-05-20 13:32:35 +000091#include <sys/cdefs.h> // For __DARWIN_ALIAS_C().
Alexander Potapenko50a002e2013-02-21 14:41:16 +000092
93// Just a pair of pointers.
94struct interpose_substitution {
95 const uptr replacement;
96 const uptr original;
97};
98
99// For a function foo() create a global pair of pointers { wrap_foo, foo } in
100// the __DATA,__interpose section.
101// As a result all the calls to foo() will be routed to wrap_foo() at runtime.
102#define INTERPOSER(func_name) __attribute__((used)) \
103const interpose_substitution substitution_##func_name[] \
104 __attribute__((section("__DATA, __interpose"))) = { \
105 { reinterpret_cast<const uptr>(WRAP(func_name)), \
106 reinterpret_cast<const uptr>(func_name) } \
107}
108
109// For a function foo() and a wrapper function bar() create a global pair
110// of pointers { bar, foo } in the __DATA,__interpose section.
111// As a result all the calls to foo() will be routed to bar() at runtime.
112#define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
113const interpose_substitution substitution_##func_name[] \
114 __attribute__((section("__DATA, __interpose"))) = { \
115 { reinterpret_cast<const uptr>(wrapper_name), \
116 reinterpret_cast<const uptr>(func_name) } \
117}
118
Alexey Samsonov5b290182012-02-08 19:52:01 +0000119# define WRAP(x) wrap_##x
120# define WRAPPER_NAME(x) "wrap_"#x
121# define INTERCEPTOR_ATTRIBUTE
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000122# define DECLARE_WRAPPER(ret_type, func, ...)
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000123
Alexey Samsonov5b290182012-02-08 19:52:01 +0000124#elif defined(_WIN32)
Stephen Hines6d186232014-11-26 17:56:19 -0800125# define WRAP(x) __asan_wrap_##x
126# define WRAPPER_NAME(x) "__asan_wrap_"#x
127# define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
Timur Iskhodzhanov9213e072013-09-10 14:42:15 +0000128# define DECLARE_WRAPPER(ret_type, func, ...) \
129 extern "C" ret_type func(__VA_ARGS__);
130# define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
131 extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
Stephen Hines6a211c52014-07-21 00:49:56 -0700132#elif defined(__FreeBSD__)
133# define WRAP(x) __interceptor_ ## x
134# define WRAPPER_NAME(x) "__interceptor_" #x
135# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
136// FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher
137// priority than weak ones so weak aliases won't work for indirect calls
138// in position-independent (-fPIC / -fPIE) mode.
139# define DECLARE_WRAPPER(ret_type, func, ...) \
140 extern "C" ret_type func(__VA_ARGS__) \
141 __attribute__((alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov5b290182012-02-08 19:52:01 +0000142#else
Dmitry Vyukovf54c0e32012-05-28 07:47:35 +0000143# define WRAP(x) __interceptor_ ## x
144# define WRAPPER_NAME(x) "__interceptor_" #x
Alexey Samsonov5b290182012-02-08 19:52:01 +0000145# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000146# define DECLARE_WRAPPER(ret_type, func, ...) \
147 extern "C" ret_type func(__VA_ARGS__) \
Alexey Samsonovba362a72012-06-28 08:07:19 +0000148 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov5b290182012-02-08 19:52:01 +0000149#endif
150
Alexander Potapenko69563982013-02-05 15:57:12 +0000151#if !defined(__APPLE__)
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000152# define PTR_TO_REAL(x) real_##x
153# define REAL(x) __interception::PTR_TO_REAL(x)
154# define FUNC_TYPE(x) x##_f
Alexey Samsonov5b290182012-02-08 19:52:01 +0000155
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000156# define DECLARE_REAL(ret_type, func, ...) \
157 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
158 namespace __interception { \
159 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
160 }
Alexander Potapenko69563982013-02-05 15:57:12 +0000161#else // __APPLE__
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000162# define REAL(x) x
163# define DECLARE_REAL(ret_type, func, ...) \
164 extern "C" ret_type func(__VA_ARGS__);
Alexander Potapenko69563982013-02-05 15:57:12 +0000165#endif // __APPLE__
Alexey Samsonov5b290182012-02-08 19:52:01 +0000166
Chandler Carruth6bae39d2012-06-25 06:53:10 +0000167#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonovc27279a2012-09-12 09:42:23 +0000168 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
Alexey Samsonov5b290182012-02-08 19:52:01 +0000169 extern "C" ret_type WRAP(func)(__VA_ARGS__);
170
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000171// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
172// macros does its job. In exceptional cases you may need to call REAL(foo)
173// without defining INTERCEPTOR(..., foo, ...). For example, if you override
174// foo with an interceptor for other function.
Alexander Potapenko69563982013-02-05 15:57:12 +0000175#if !defined(__APPLE__)
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000176# define DEFINE_REAL(ret_type, func, ...) \
177 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
178 namespace __interception { \
179 FUNC_TYPE(func) PTR_TO_REAL(func); \
180 }
181#else
182# define DEFINE_REAL(ret_type, func, ...)
183#endif
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000184
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000185#if !defined(__APPLE__)
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000186#define INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000187 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
188 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
189 extern "C" \
190 INTERCEPTOR_ATTRIBUTE \
191 ret_type WRAP(func)(__VA_ARGS__)
Alexander Potapenko6a659df2013-05-20 13:32:35 +0000192
193// We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
194#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
195 INTERCEPTOR(ret_type, func, __VA_ARGS__)
196
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000197#else // __APPLE__
Alexander Potapenko6a659df2013-05-20 13:32:35 +0000198
199#define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
200 extern "C" ret_type func(__VA_ARGS__) suffix; \
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000201 extern "C" ret_type WRAP(func)(__VA_ARGS__); \
202 INTERPOSER(func); \
203 extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
204
Alexander Potapenko6a659df2013-05-20 13:32:35 +0000205#define INTERCEPTOR(ret_type, func, ...) \
206 INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
207
208#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
209 INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
210
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000211// Override |overridee| with |overrider|.
212#define OVERRIDE_FUNCTION(overridee, overrider) \
213 INTERPOSER_2(overridee, WRAP(overrider))
214#endif
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000215
216#if defined(_WIN32)
217# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000218 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
219 namespace __interception { \
220 FUNC_TYPE(func) PTR_TO_REAL(func); \
221 } \
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000222 extern "C" \
223 INTERCEPTOR_ATTRIBUTE \
224 ret_type __stdcall WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000225#endif
Alexey Samsonov5b290182012-02-08 19:52:01 +0000226
Alexey Samsonov592d3f72012-08-02 11:19:13 +0000227// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
228// so we use casting via an integral type __interception::uptr,
229// assuming that system is POSIX-compliant. Using other hacks seem
230// challenging, as we don't even pass function type to
231// INTERCEPT_FUNCTION macro, only its name.
232namespace __interception {
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000233#if defined(_WIN64)
234typedef unsigned long long uptr; // NOLINT
235#else
Alexey Samsonov592d3f72012-08-02 11:19:13 +0000236typedef unsigned long uptr; // NOLINT
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000237#endif // _WIN64
Alexey Samsonov592d3f72012-08-02 11:19:13 +0000238} // namespace __interception
239
Alexey Samsonov5b290182012-02-08 19:52:01 +0000240#define INCLUDED_FROM_INTERCEPTION_LIB
241
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700242#if defined(__linux__) || defined(__FreeBSD__)
Alexey Samsonov5b290182012-02-08 19:52:01 +0000243# include "interception_linux.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700244# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
Alexey Samsonov5e2d3772013-10-16 08:20:31 +0000245# define INTERCEPT_FUNCTION_VER(func, symver) \
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700246 INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
Alexey Samsonov5b290182012-02-08 19:52:01 +0000247#elif defined(__APPLE__)
248# include "interception_mac.h"
Alexey Samsonov5b290182012-02-08 19:52:01 +0000249# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
Alexey Samsonov5e2d3772013-10-16 08:20:31 +0000250# define INTERCEPT_FUNCTION_VER(func, symver) \
251 INTERCEPT_FUNCTION_VER_MAC(func, symver)
Alexey Samsonov5b290182012-02-08 19:52:01 +0000252#else // defined(_WIN32)
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000253# include "interception_win.h"
254# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonov5e2d3772013-10-16 08:20:31 +0000255# define INTERCEPT_FUNCTION_VER(func, symver) \
256 INTERCEPT_FUNCTION_VER_WIN(func, symver)
Alexey Samsonov5b290182012-02-08 19:52:01 +0000257#endif
258
259#undef INCLUDED_FROM_INTERCEPTION_LIB
260
261#endif // INTERCEPTION_H