blob: e5f8916c1861c8979b6d4903a206647e671c63d6 [file] [log] [blame]
Alexey Samsonov8489f2a2012-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
18#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
19# error "Interception doesn't work on this operating system."
20#endif
21
Kostya Serebryany07bb3922012-12-13 06:31:40 +000022#include "sanitizer/common_interface_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::u64 OFF_T;
29typedef __sanitizer::u64 OFF64_T;
30
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000031// How to use this library:
32// 1) Include this header to define your own interceptors
33// (see details below).
34// 2) Build all *.cc files and link against them.
35// On Mac you will also need to:
36// 3) Provide your own implementation for the following functions:
37// mach_error_t __interception::allocate_island(void **ptr,
38// size_t size,
39// void *hint);
40// mach_error_t __interception::deallocate_island(void *ptr);
41// See "interception_mac.h" for more details.
42
43// How to add an interceptor:
44// Suppose you need to wrap/replace system function (generally, from libc):
45// int foo(const char *bar, double baz);
46// You'll need to:
47// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
48// your source file.
49// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
50// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
51// intercepted successfully.
52// You can access original function by calling REAL(foo)(bar, baz).
53// By default, REAL(foo) will be visible only inside your interceptor, and if
54// you want to use it in other parts of RTL, you'll need to:
Alexey Samsonov70386aa2012-06-28 08:27:24 +000055// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000056// header file.
57// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
58// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
Alexey Samsonov70386aa2012-06-28 08:27:24 +000059// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000060// to a header file.
61
62// Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} or
Alexey Samsonov70386aa2012-06-28 08:27:24 +000063// DECLARE_REAL(...) are located inside namespaces.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000064// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo);" to
65// effectively redirect calls from "foo" to "zoo". In this case
66// you aren't required to implement
Alexey Samsonov70386aa2012-06-28 08:27:24 +000067// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000068// but instead you'll have to add
Alexey Samsonov70386aa2012-06-28 08:27:24 +000069// DEFINE_REAL(int, foo, const char *bar, double baz) in your
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000070// source file (to define a pointer to overriden function).
71
72// How it works:
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000073// To replace system functions on Linux we just need to declare functions
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000074// with same names in our library and then obtain the real function pointers
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000075// using dlsym().
76// There is one complication. A user may also intercept some of the functions
Dmitry Vyukovbd310f02012-05-28 07:47:35 +000077// we intercept. To resolve this we declare our interceptors with __interceptor_
78// prefix, and then make actual interceptors weak aliases to __interceptor_
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000079// functions.
80// This is not so on Mac OS, where the two-level namespace makes
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000081// our replacement functions invisible to other libraries. This may be overcomed
82// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000083// libraries in Chromium were noticed when doing so. Instead we use
84// mach_override, a handy framework for patching functions at runtime.
85// To avoid possible name clashes, our replacement functions have
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000086// the "wrap_" prefix on Mac.
Alexander Potapenkoc62210e2012-08-17 09:00:08 +000087// An alternative to function patching is to create a dylib containing a
88// __DATA,__interpose section that associates library functions with their
89// wrappers. When this dylib is preloaded before an executable using
90// DYLD_INSERT_LIBRARIES, it routes all the calls to interposed functions done
91// through stubs to the wrapper functions. Such a library is built with
92// -DMAC_INTERPOSE_FUNCTIONS=1.
93
94#if !defined(MAC_INTERPOSE_FUNCTIONS) || !defined(__APPLE__)
95# define MAC_INTERPOSE_FUNCTIONS 0
96#endif
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000097
98#if defined(__APPLE__)
99# define WRAP(x) wrap_##x
100# define WRAPPER_NAME(x) "wrap_"#x
101# define INTERCEPTOR_ATTRIBUTE
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000102# define DECLARE_WRAPPER(ret_type, func, ...)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000103#elif defined(_WIN32)
Timur Iskhodzhanov2f48b872012-03-12 11:45:09 +0000104# if defined(_DLL) // DLL CRT
105# define WRAP(x) x
106# define WRAPPER_NAME(x) #x
107# define INTERCEPTOR_ATTRIBUTE
108# else // Static CRT
109# define WRAP(x) wrap_##x
110# define WRAPPER_NAME(x) "wrap_"#x
111# define INTERCEPTOR_ATTRIBUTE
112# endif
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000113# define DECLARE_WRAPPER(ret_type, func, ...)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000114#else
Dmitry Vyukovbd310f02012-05-28 07:47:35 +0000115# define WRAP(x) __interceptor_ ## x
116# define WRAPPER_NAME(x) "__interceptor_" #x
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000117# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000118# define DECLARE_WRAPPER(ret_type, func, ...) \
119 extern "C" ret_type func(__VA_ARGS__) \
Alexey Samsonovfa42dd72012-06-28 08:07:19 +0000120 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000121#endif
122
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000123#if !MAC_INTERPOSE_FUNCTIONS
124# define PTR_TO_REAL(x) real_##x
125# define REAL(x) __interception::PTR_TO_REAL(x)
126# define FUNC_TYPE(x) x##_f
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000127
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000128# define DECLARE_REAL(ret_type, func, ...) \
129 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
130 namespace __interception { \
131 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
132 }
133#else // MAC_INTERPOSE_FUNCTIONS
134# define REAL(x) x
135# define DECLARE_REAL(ret_type, func, ...) \
136 extern "C" ret_type func(__VA_ARGS__);
137#endif // MAC_INTERPOSE_FUNCTIONS
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000138
Chandler Carruthbbff2782012-06-25 06:53:10 +0000139#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonova81d2682012-09-12 09:42:23 +0000140 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000141 extern "C" ret_type WRAP(func)(__VA_ARGS__);
142
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000143// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
144// macros does its job. In exceptional cases you may need to call REAL(foo)
145// without defining INTERCEPTOR(..., foo, ...). For example, if you override
146// foo with an interceptor for other function.
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000147#if !MAC_INTERPOSE_FUNCTIONS
148# define DEFINE_REAL(ret_type, func, ...) \
149 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
150 namespace __interception { \
151 FUNC_TYPE(func) PTR_TO_REAL(func); \
152 }
153#else
154# define DEFINE_REAL(ret_type, func, ...)
155#endif
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000156
157#define INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000158 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
159 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
160 extern "C" \
161 INTERCEPTOR_ATTRIBUTE \
162 ret_type WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000163
164#if defined(_WIN32)
165# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000166 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
167 namespace __interception { \
168 FUNC_TYPE(func) PTR_TO_REAL(func); \
169 } \
170 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
171 extern "C" \
172 INTERCEPTOR_ATTRIBUTE \
173 ret_type __stdcall WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000174#endif
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000175
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000176// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
177// so we use casting via an integral type __interception::uptr,
178// assuming that system is POSIX-compliant. Using other hacks seem
179// challenging, as we don't even pass function type to
180// INTERCEPT_FUNCTION macro, only its name.
181namespace __interception {
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000182#if defined(_WIN64)
183typedef unsigned long long uptr; // NOLINT
184#else
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000185typedef unsigned long uptr; // NOLINT
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000186#endif // _WIN64
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000187} // namespace __interception
188
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000189#define INCLUDED_FROM_INTERCEPTION_LIB
190
191#if defined(__linux__)
192# include "interception_linux.h"
193# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
194#elif defined(__APPLE__)
195# include "interception_mac.h"
196# define OVERRIDE_FUNCTION(old_func, new_func) \
197 OVERRIDE_FUNCTION_MAC(old_func, new_func)
198# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
199#else // defined(_WIN32)
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +0000200# include "interception_win.h"
201# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000202#endif
203
204#undef INCLUDED_FROM_INTERCEPTION_LIB
205
206#endif // INTERCEPTION_H