blob: b6be72c69027a6428069ee13c9f6dc5517240cf2 [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
Alexey Samsonov32832e62013-01-30 14:27:41 +000022#include "sanitizer_common/sanitizer_internal_defs.h"
Kostya Serebryany07bb3922012-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)
26typedef __sanitizer::uptr SIZE_T;
27typedef __sanitizer::sptr SSIZE_T;
Evgeniy Stepanov222076e2013-01-18 11:17:23 +000028typedef __sanitizer::sptr PTRDIFF_T;
29typedef __sanitizer::s64 INTMAX_T;
Evgeniy Stepanov82687852013-02-07 07:37:12 +000030// WARNING: OFF_T may be different from OS type off_t, depending on the value of
31// _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
32// like pread and mmap, as opposed to pread64 and mmap64.
Kostya Serebryany039fa752013-02-14 08:05:55 +000033// Mac and Linux/x86-64 are special.
34#if defined(__APPLE__) || (defined(__linux__) && defined(__x86_64__))
Evgeniy Stepanov4ea68162013-02-07 09:52:43 +000035typedef __sanitizer::u64 OFF_T;
36#else
Evgeniy Stepanov82687852013-02-07 07:37:12 +000037typedef __sanitizer::uptr OFF_T;
Evgeniy Stepanov4ea68162013-02-07 09:52:43 +000038#endif
Kostya Serebryany07bb3922012-12-13 06:31:40 +000039typedef __sanitizer::u64 OFF64_T;
40
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000041// How to add an interceptor:
42// Suppose you need to wrap/replace system function (generally, from libc):
43// int foo(const char *bar, double baz);
44// You'll need to:
45// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
46// your source file.
47// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
48// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
49// intercepted successfully.
50// You can access original function by calling REAL(foo)(bar, baz).
51// By default, REAL(foo) will be visible only inside your interceptor, and if
52// you want to use it in other parts of RTL, you'll need to:
Alexey Samsonov70386aa2012-06-28 08:27:24 +000053// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000054// header file.
55// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
56// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
Alexey Samsonov70386aa2012-06-28 08:27:24 +000057// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000058// to a header file.
59
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000060// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
Alexey Samsonov70386aa2012-06-28 08:27:24 +000061// DECLARE_REAL(...) are located inside namespaces.
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000062// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000063// effectively redirect calls from "foo" to "zoo". In this case
64// you aren't required to implement
Alexey Samsonov70386aa2012-06-28 08:27:24 +000065// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000066// but instead you'll have to add
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000067// DECLARE_REAL(int, foo, const char *bar, double baz) in your
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000068// source file (to define a pointer to overriden function).
69
70// How it works:
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000071// To replace system functions on Linux we just need to declare functions
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000072// with same names in our library and then obtain the real function pointers
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000073// using dlsym().
74// There is one complication. A user may also intercept some of the functions
Dmitry Vyukovbd310f02012-05-28 07:47:35 +000075// we intercept. To resolve this we declare our interceptors with __interceptor_
76// prefix, and then make actual interceptors weak aliases to __interceptor_
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000077// functions.
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000078//
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000079// This is not so on Mac OS, where the two-level namespace makes
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000080// our replacement functions invisible to other libraries. This may be overcomed
81// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
Alexander Potapenko34157fc2013-02-05 15:57:12 +000082// libraries in Chromium were noticed when doing so.
83// Instead we create a dylib containing a __DATA,__interpose section that
84// associates library functions with their wrappers. When this dylib is
85// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
86// the calls to interposed functions done through stubs to the wrapper
87// functions.
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000088// As it's decided at compile time which functions are to be intercepted on Mac,
89// INTERCEPT_FUNCTION() is effectively a no-op on this system.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000090
91#if defined(__APPLE__)
Alexander Potapenkoe8ba1c82013-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 Samsonov8489f2a2012-02-08 19:52:01 +0000119# define WRAP(x) wrap_##x
120# define WRAPPER_NAME(x) "wrap_"#x
121# define INTERCEPTOR_ATTRIBUTE
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000122# define DECLARE_WRAPPER(ret_type, func, ...)
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +0000123
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000124#elif defined(_WIN32)
Timur Iskhodzhanov2f48b872012-03-12 11:45:09 +0000125# if defined(_DLL) // DLL CRT
126# define WRAP(x) x
127# define WRAPPER_NAME(x) #x
128# define INTERCEPTOR_ATTRIBUTE
129# else // Static CRT
130# define WRAP(x) wrap_##x
131# define WRAPPER_NAME(x) "wrap_"#x
132# define INTERCEPTOR_ATTRIBUTE
133# endif
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000134# define DECLARE_WRAPPER(ret_type, func, ...)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000135#else
Dmitry Vyukovbd310f02012-05-28 07:47:35 +0000136# define WRAP(x) __interceptor_ ## x
137# define WRAPPER_NAME(x) "__interceptor_" #x
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000138# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000139# define DECLARE_WRAPPER(ret_type, func, ...) \
140 extern "C" ret_type func(__VA_ARGS__) \
Alexey Samsonovfa42dd72012-06-28 08:07:19 +0000141 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000142#endif
143
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000144#if !defined(__APPLE__)
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000145# define PTR_TO_REAL(x) real_##x
146# define REAL(x) __interception::PTR_TO_REAL(x)
147# define FUNC_TYPE(x) x##_f
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000148
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000149# define DECLARE_REAL(ret_type, func, ...) \
150 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
151 namespace __interception { \
152 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
153 }
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000154#else // __APPLE__
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000155# define REAL(x) x
156# define DECLARE_REAL(ret_type, func, ...) \
157 extern "C" ret_type func(__VA_ARGS__);
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000158#endif // __APPLE__
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000159
Chandler Carruthbbff2782012-06-25 06:53:10 +0000160#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonova81d2682012-09-12 09:42:23 +0000161 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000162 extern "C" ret_type WRAP(func)(__VA_ARGS__);
163
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000164// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
165// macros does its job. In exceptional cases you may need to call REAL(foo)
166// without defining INTERCEPTOR(..., foo, ...). For example, if you override
167// foo with an interceptor for other function.
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000168#if !defined(__APPLE__)
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000169# define DEFINE_REAL(ret_type, func, ...) \
170 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
171 namespace __interception { \
172 FUNC_TYPE(func) PTR_TO_REAL(func); \
173 }
174#else
175# define DEFINE_REAL(ret_type, func, ...)
176#endif
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000177
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +0000178#if !defined(__APPLE__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000179#define INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000180 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
181 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
182 extern "C" \
183 INTERCEPTOR_ATTRIBUTE \
184 ret_type WRAP(func)(__VA_ARGS__)
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +0000185#else // __APPLE__
186#define INTERCEPTOR(ret_type, func, ...) \
187 extern "C" ret_type func(__VA_ARGS__); \
188 extern "C" ret_type WRAP(func)(__VA_ARGS__); \
189 INTERPOSER(func); \
190 extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
191
192// Override |overridee| with |overrider|.
193#define OVERRIDE_FUNCTION(overridee, overrider) \
194 INTERPOSER_2(overridee, WRAP(overrider))
195#endif
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000196
197#if defined(_WIN32)
198# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000199 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
200 namespace __interception { \
201 FUNC_TYPE(func) PTR_TO_REAL(func); \
202 } \
203 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
204 extern "C" \
205 INTERCEPTOR_ATTRIBUTE \
206 ret_type __stdcall WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000207#endif
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000208
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000209// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
210// so we use casting via an integral type __interception::uptr,
211// assuming that system is POSIX-compliant. Using other hacks seem
212// challenging, as we don't even pass function type to
213// INTERCEPT_FUNCTION macro, only its name.
214namespace __interception {
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000215#if defined(_WIN64)
216typedef unsigned long long uptr; // NOLINT
217#else
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000218typedef unsigned long uptr; // NOLINT
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000219#endif // _WIN64
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000220} // namespace __interception
221
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000222#define INCLUDED_FROM_INTERCEPTION_LIB
223
224#if defined(__linux__)
225# include "interception_linux.h"
226# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
227#elif defined(__APPLE__)
228# include "interception_mac.h"
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000229# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
230#else // defined(_WIN32)
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +0000231# include "interception_win.h"
232# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000233#endif
234
235#undef INCLUDED_FROM_INTERCEPTION_LIB
236
237#endif // INTERCEPTION_H