blob: 2ccc90387b22a1a8a674a8a1648fc4b05cfaa6ab [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
18#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
19# error "Interception doesn't work on this operating system."
20#endif
21
Alexey Samsonov216719b2013-01-30 14:27:41 +000022#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000023
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)
Kostya Serebryany6fb47af2013-02-27 11:22:40 +000026typedef __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;
Kostya Serebryany6afa1b02012-12-13 06:31:40 +000032
Alexey Samsonov5b290182012-02-08 19:52:01 +000033// 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.
39// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
40// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
41// intercepted successfully.
42// You can access original function by calling REAL(foo)(bar, baz).
43// By default, REAL(foo) will be visible only inside your interceptor, and if
44// you want to use it in other parts of RTL, you'll need to:
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000045// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
Alexey Samsonov5b290182012-02-08 19:52:01 +000046// header file.
47// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
48// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000049// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
Alexey Samsonov5b290182012-02-08 19:52:01 +000050// to a header file.
51
Alexander Potapenko50a002e2013-02-21 14:41:16 +000052// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000053// DECLARE_REAL(...) are located inside namespaces.
Alexander Potapenko50a002e2013-02-21 14:41:16 +000054// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
Alexey Samsonov5b290182012-02-08 19:52:01 +000055// effectively redirect calls from "foo" to "zoo". In this case
56// you aren't required to implement
Alexey Samsonovb4fefa72012-06-28 08:27:24 +000057// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
Alexey Samsonov5b290182012-02-08 19:52:01 +000058// but instead you'll have to add
Alexander Potapenko50a002e2013-02-21 14:41:16 +000059// DECLARE_REAL(int, foo, const char *bar, double baz) in your
Alexey Samsonov5b290182012-02-08 19:52:01 +000060// source file (to define a pointer to overriden function).
61
62// How it works:
Dmitry Vyukov580469d2012-05-24 13:54:31 +000063// To replace system functions on Linux we just need to declare functions
Alexey Samsonov5b290182012-02-08 19:52:01 +000064// with same names in our library and then obtain the real function pointers
Dmitry Vyukov580469d2012-05-24 13:54:31 +000065// using dlsym().
66// There is one complication. A user may also intercept some of the functions
Dmitry Vyukovf54c0e32012-05-28 07:47:35 +000067// we intercept. To resolve this we declare our interceptors with __interceptor_
68// prefix, and then make actual interceptors weak aliases to __interceptor_
Dmitry Vyukov580469d2012-05-24 13:54:31 +000069// functions.
Alexander Potapenko50a002e2013-02-21 14:41:16 +000070//
Dmitry Vyukov580469d2012-05-24 13:54:31 +000071// This is not so on Mac OS, where the two-level namespace makes
Alexey Samsonov5b290182012-02-08 19:52:01 +000072// our replacement functions invisible to other libraries. This may be overcomed
73// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
Alexander Potapenko69563982013-02-05 15:57:12 +000074// libraries in Chromium were noticed when doing so.
75// Instead we create a dylib containing a __DATA,__interpose section that
76// associates library functions with their wrappers. When this dylib is
77// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
78// the calls to interposed functions done through stubs to the wrapper
79// functions.
Alexander Potapenko50a002e2013-02-21 14:41:16 +000080// As it's decided at compile time which functions are to be intercepted on Mac,
81// INTERCEPT_FUNCTION() is effectively a no-op on this system.
Alexey Samsonov5b290182012-02-08 19:52:01 +000082
83#if defined(__APPLE__)
Alexander Potapenko50a002e2013-02-21 14:41:16 +000084
85// Just a pair of pointers.
86struct interpose_substitution {
87 const uptr replacement;
88 const uptr original;
89};
90
91// For a function foo() create a global pair of pointers { wrap_foo, foo } in
92// the __DATA,__interpose section.
93// As a result all the calls to foo() will be routed to wrap_foo() at runtime.
94#define INTERPOSER(func_name) __attribute__((used)) \
95const interpose_substitution substitution_##func_name[] \
96 __attribute__((section("__DATA, __interpose"))) = { \
97 { reinterpret_cast<const uptr>(WRAP(func_name)), \
98 reinterpret_cast<const uptr>(func_name) } \
99}
100
101// For a function foo() and a wrapper function bar() create a global pair
102// of pointers { bar, foo } in the __DATA,__interpose section.
103// As a result all the calls to foo() will be routed to bar() at runtime.
104#define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
105const interpose_substitution substitution_##func_name[] \
106 __attribute__((section("__DATA, __interpose"))) = { \
107 { reinterpret_cast<const uptr>(wrapper_name), \
108 reinterpret_cast<const uptr>(func_name) } \
109}
110
Alexey Samsonov5b290182012-02-08 19:52:01 +0000111# define WRAP(x) wrap_##x
112# define WRAPPER_NAME(x) "wrap_"#x
113# define INTERCEPTOR_ATTRIBUTE
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000114# define DECLARE_WRAPPER(ret_type, func, ...)
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000115
Alexey Samsonov5b290182012-02-08 19:52:01 +0000116#elif defined(_WIN32)
Timur Iskhodzhanov2716a612012-03-12 11:45:09 +0000117# if defined(_DLL) // DLL CRT
118# define WRAP(x) x
119# define WRAPPER_NAME(x) #x
120# define INTERCEPTOR_ATTRIBUTE
121# else // Static CRT
122# define WRAP(x) wrap_##x
123# define WRAPPER_NAME(x) "wrap_"#x
124# define INTERCEPTOR_ATTRIBUTE
125# endif
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000126# define DECLARE_WRAPPER(ret_type, func, ...)
Alexey Samsonov5b290182012-02-08 19:52:01 +0000127#else
Dmitry Vyukovf54c0e32012-05-28 07:47:35 +0000128# define WRAP(x) __interceptor_ ## x
129# define WRAPPER_NAME(x) "__interceptor_" #x
Alexey Samsonov5b290182012-02-08 19:52:01 +0000130# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000131# define DECLARE_WRAPPER(ret_type, func, ...) \
132 extern "C" ret_type func(__VA_ARGS__) \
Alexey Samsonovba362a72012-06-28 08:07:19 +0000133 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov5b290182012-02-08 19:52:01 +0000134#endif
135
Alexander Potapenko69563982013-02-05 15:57:12 +0000136#if !defined(__APPLE__)
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000137# define PTR_TO_REAL(x) real_##x
138# define REAL(x) __interception::PTR_TO_REAL(x)
139# define FUNC_TYPE(x) x##_f
Alexey Samsonov5b290182012-02-08 19:52:01 +0000140
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000141# define DECLARE_REAL(ret_type, func, ...) \
142 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
143 namespace __interception { \
144 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
145 }
Alexander Potapenko69563982013-02-05 15:57:12 +0000146#else // __APPLE__
Alexander Potapenko0ef53102012-08-17 09:00:08 +0000147# define REAL(x) x
148# define DECLARE_REAL(ret_type, func, ...) \
149 extern "C" ret_type func(__VA_ARGS__);
Alexander Potapenko69563982013-02-05 15:57:12 +0000150#endif // __APPLE__
Alexey Samsonov5b290182012-02-08 19:52:01 +0000151
Chandler Carruth6bae39d2012-06-25 06:53:10 +0000152#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonovc27279a2012-09-12 09:42:23 +0000153 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
Alexey Samsonov5b290182012-02-08 19:52:01 +0000154 extern "C" ret_type WRAP(func)(__VA_ARGS__);
155
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000156// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
157// macros does its job. In exceptional cases you may need to call REAL(foo)
158// without defining INTERCEPTOR(..., foo, ...). For example, if you override
159// foo with an interceptor for other function.
Alexander Potapenko69563982013-02-05 15:57:12 +0000160#if !defined(__APPLE__)
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000161# define DEFINE_REAL(ret_type, func, ...) \
162 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
163 namespace __interception { \
164 FUNC_TYPE(func) PTR_TO_REAL(func); \
165 }
166#else
167# define DEFINE_REAL(ret_type, func, ...)
168#endif
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000169
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000170#if !defined(__APPLE__)
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000171#define INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000172 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
173 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
174 extern "C" \
175 INTERCEPTOR_ATTRIBUTE \
176 ret_type WRAP(func)(__VA_ARGS__)
Alexander Potapenko50a002e2013-02-21 14:41:16 +0000177#else // __APPLE__
178#define INTERCEPTOR(ret_type, func, ...) \
179 extern "C" ret_type func(__VA_ARGS__); \
180 extern "C" ret_type WRAP(func)(__VA_ARGS__); \
181 INTERPOSER(func); \
182 extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
183
184// Override |overridee| with |overrider|.
185#define OVERRIDE_FUNCTION(overridee, overrider) \
186 INTERPOSER_2(overridee, WRAP(overrider))
187#endif
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000188
189#if defined(_WIN32)
190# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
Alexey Samsonovb66bfd12012-09-11 15:02:20 +0000191 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
192 namespace __interception { \
193 FUNC_TYPE(func) PTR_TO_REAL(func); \
194 } \
195 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
196 extern "C" \
197 INTERCEPTOR_ATTRIBUTE \
198 ret_type __stdcall WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov600972e2012-02-24 15:28:43 +0000199#endif
Alexey Samsonov5b290182012-02-08 19:52:01 +0000200
Alexey Samsonov592d3f72012-08-02 11:19:13 +0000201// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
202// so we use casting via an integral type __interception::uptr,
203// assuming that system is POSIX-compliant. Using other hacks seem
204// challenging, as we don't even pass function type to
205// INTERCEPT_FUNCTION macro, only its name.
206namespace __interception {
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000207#if defined(_WIN64)
208typedef unsigned long long uptr; // NOLINT
209#else
Alexey Samsonov592d3f72012-08-02 11:19:13 +0000210typedef unsigned long uptr; // NOLINT
Alexey Samsonovb46941a2012-09-24 11:43:40 +0000211#endif // _WIN64
Alexey Samsonov592d3f72012-08-02 11:19:13 +0000212} // namespace __interception
213
Alexey Samsonov5b290182012-02-08 19:52:01 +0000214#define INCLUDED_FROM_INTERCEPTION_LIB
215
216#if defined(__linux__)
217# include "interception_linux.h"
218# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
219#elif defined(__APPLE__)
220# include "interception_mac.h"
Alexey Samsonov5b290182012-02-08 19:52:01 +0000221# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
222#else // defined(_WIN32)
Timur Iskhodzhanov07bb9f12012-02-22 13:59:49 +0000223# include "interception_win.h"
224# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonov5b290182012-02-08 19:52:01 +0000225#endif
226
227#undef INCLUDED_FROM_INTERCEPTION_LIB
228
229#endif // INTERCEPTION_H