interception.h revision 9213e07a7f95b3e634b97a7bba70849badcf2c8a
1//===-- 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
18#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
19# error "Interception doesn't work on this operating system."
20#endif
21
22#include "sanitizer_common/sanitizer_internal_defs.h"
23
24// These typedefs should be used only in the interceptor definitions to replace
25// the standard system types (e.g. SSIZE_T instead of ssize_t)
26typedef __sanitizer::uptr    SIZE_T;
27typedef __sanitizer::sptr    SSIZE_T;
28typedef __sanitizer::sptr    PTRDIFF_T;
29typedef __sanitizer::s64     INTMAX_T;
30typedef __sanitizer::OFF_T   OFF_T;
31typedef __sanitizer::OFF64_T OFF64_T;
32
33// How to add an interceptor:
34// Suppose you need to wrap/replace system function (generally, from libc):
35//      int foo(const char *bar, double baz);
36// You'll need to:
37//      1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
38//         your source file. See the notes below for cases when
39//         INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
40//      2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
41//         INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
42//         intercepted successfully.
43// You can access original function by calling REAL(foo)(bar, baz).
44// By default, REAL(foo) will be visible only inside your interceptor, and if
45// you want to use it in other parts of RTL, you'll need to:
46//      3a) add DECLARE_REAL(int, foo, const char*, double) to a
47//          header file.
48// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
49// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
50//      3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
51//          to a header file.
52
53// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
54//           DECLARE_REAL(...) are located inside namespaces.
55//        2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
56//           effectively redirect calls from "foo" to "zoo". In this case
57//           you aren't required to implement
58//           INTERCEPTOR(int, foo, const char *bar, double baz) {...}
59//           but instead you'll have to add
60//           DECLARE_REAL(int, foo, const char *bar, double baz) in your
61//           source file (to define a pointer to overriden function).
62//        3. Some Mac functions have symbol variants discriminated by
63//           additional suffixes, e.g. _$UNIX2003 (see
64//           https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
65//           for more details). To intercept such functions you need to use the
66//           INTERCEPTOR_WITH_SUFFIX(...) macro.
67
68// How it works:
69// To replace system functions on Linux we just need to declare functions
70// with same names in our library and then obtain the real function pointers
71// using dlsym().
72// There is one complication. A user may also intercept some of the functions
73// we intercept. To resolve this we declare our interceptors with __interceptor_
74// prefix, and then make actual interceptors weak aliases to __interceptor_
75// functions.
76//
77// This is not so on Mac OS, where the two-level namespace makes
78// our replacement functions invisible to other libraries. This may be overcomed
79// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
80// libraries in Chromium were noticed when doing so.
81// Instead we create a dylib containing a __DATA,__interpose section that
82// associates library functions with their wrappers. When this dylib is
83// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
84// the calls to interposed functions done through stubs to the wrapper
85// functions.
86// As it's decided at compile time which functions are to be intercepted on Mac,
87// INTERCEPT_FUNCTION() is effectively a no-op on this system.
88
89#if defined(__APPLE__)
90#include <sys/cdefs.h>  // For __DARWIN_ALIAS_C().
91
92// Just a pair of pointers.
93struct interpose_substitution {
94  const uptr replacement;
95  const uptr original;
96};
97
98// For a function foo() create a global pair of pointers { wrap_foo, foo } in
99// the __DATA,__interpose section.
100// As a result all the calls to foo() will be routed to wrap_foo() at runtime.
101#define INTERPOSER(func_name) __attribute__((used)) \
102const interpose_substitution substitution_##func_name[] \
103    __attribute__((section("__DATA, __interpose"))) = { \
104    { reinterpret_cast<const uptr>(WRAP(func_name)), \
105      reinterpret_cast<const uptr>(func_name) } \
106}
107
108// For a function foo() and a wrapper function bar() create a global pair
109// of pointers { bar, foo } in the __DATA,__interpose section.
110// As a result all the calls to foo() will be routed to bar() at runtime.
111#define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
112const interpose_substitution substitution_##func_name[] \
113    __attribute__((section("__DATA, __interpose"))) = { \
114    { reinterpret_cast<const uptr>(wrapper_name), \
115      reinterpret_cast<const uptr>(func_name) } \
116}
117
118# define WRAP(x) wrap_##x
119# define WRAPPER_NAME(x) "wrap_"#x
120# define INTERCEPTOR_ATTRIBUTE
121# define DECLARE_WRAPPER(ret_type, func, ...)
122
123#elif defined(_WIN32)
124# if defined(_DLL)  // DLL CRT
125#  define WRAP(x) x
126#  define WRAPPER_NAME(x) #x
127#  define INTERCEPTOR_ATTRIBUTE
128# else  // Static CRT
129#  define WRAP(x) wrap_##x
130#  define WRAPPER_NAME(x) "wrap_"#x
131#  define INTERCEPTOR_ATTRIBUTE
132# endif
133# define DECLARE_WRAPPER(ret_type, func, ...) \
134    extern "C" ret_type func(__VA_ARGS__);
135# define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
136    extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
137#else
138# define WRAP(x) __interceptor_ ## x
139# define WRAPPER_NAME(x) "__interceptor_" #x
140# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
141# define DECLARE_WRAPPER(ret_type, func, ...) \
142    extern "C" ret_type func(__VA_ARGS__) \
143    __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
144#endif
145
146#if !defined(__APPLE__)
147# define PTR_TO_REAL(x) real_##x
148# define REAL(x) __interception::PTR_TO_REAL(x)
149# define FUNC_TYPE(x) x##_f
150
151# define DECLARE_REAL(ret_type, func, ...) \
152    typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
153    namespace __interception { \
154      extern FUNC_TYPE(func) PTR_TO_REAL(func); \
155    }
156#else  // __APPLE__
157# define REAL(x) x
158# define DECLARE_REAL(ret_type, func, ...) \
159    extern "C" ret_type func(__VA_ARGS__);
160#endif  // __APPLE__
161
162#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
163  DECLARE_REAL(ret_type, func, __VA_ARGS__) \
164  extern "C" ret_type WRAP(func)(__VA_ARGS__);
165
166// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
167// macros does its job. In exceptional cases you may need to call REAL(foo)
168// without defining INTERCEPTOR(..., foo, ...). For example, if you override
169// foo with an interceptor for other function.
170#if !defined(__APPLE__)
171# define DEFINE_REAL(ret_type, func, ...) \
172    typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
173    namespace __interception { \
174      FUNC_TYPE(func) PTR_TO_REAL(func); \
175    }
176#else
177# define DEFINE_REAL(ret_type, func, ...)
178#endif
179
180#if !defined(__APPLE__)
181#define INTERCEPTOR(ret_type, func, ...) \
182  DEFINE_REAL(ret_type, func, __VA_ARGS__) \
183  DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
184  extern "C" \
185  INTERCEPTOR_ATTRIBUTE \
186  ret_type WRAP(func)(__VA_ARGS__)
187
188// We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
189#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
190  INTERCEPTOR(ret_type, func, __VA_ARGS__)
191
192#else  // __APPLE__
193
194#define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
195  extern "C" ret_type func(__VA_ARGS__) suffix; \
196  extern "C" ret_type WRAP(func)(__VA_ARGS__); \
197  INTERPOSER(func); \
198  extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
199
200#define INTERCEPTOR(ret_type, func, ...) \
201  INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
202
203#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
204  INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
205
206// Override |overridee| with |overrider|.
207#define OVERRIDE_FUNCTION(overridee, overrider) \
208  INTERPOSER_2(overridee, WRAP(overrider))
209#endif
210
211#if defined(_WIN32)
212# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
213    typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
214    namespace __interception { \
215      FUNC_TYPE(func) PTR_TO_REAL(func); \
216    } \
217    DECLARE_WRAPPER_WINAPI(ret_type, func, __VA_ARGS__) \
218    extern "C" \
219    INTERCEPTOR_ATTRIBUTE \
220    ret_type __stdcall WRAP(func)(__VA_ARGS__)
221#endif
222
223// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
224// so we use casting via an integral type __interception::uptr,
225// assuming that system is POSIX-compliant. Using other hacks seem
226// challenging, as we don't even pass function type to
227// INTERCEPT_FUNCTION macro, only its name.
228namespace __interception {
229#if defined(_WIN64)
230typedef unsigned long long uptr;  // NOLINT
231#else
232typedef unsigned long uptr;  // NOLINT
233#endif  // _WIN64
234}  // namespace __interception
235
236#define INCLUDED_FROM_INTERCEPTION_LIB
237
238#if defined(__linux__)
239# include "interception_linux.h"
240# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
241#elif defined(__APPLE__)
242# include "interception_mac.h"
243# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
244#else  // defined(_WIN32)
245# include "interception_win.h"
246# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
247#endif
248
249#undef INCLUDED_FROM_INTERCEPTION_LIB
250
251#endif  // INTERCEPTION_H
252