blob: ccebf14c167222accae9598f6f96934e3aee468b [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;
Kostya Serebryany07bb3922012-12-13 06:31:40 +000030typedef __sanitizer::u64 OFF_T;
31typedef __sanitizer::u64 OFF64_T;
32
Alexey Samsonov8489f2a2012-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 Samsonov70386aa2012-06-28 08:27:24 +000045// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
Alexey Samsonov8489f2a2012-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 Samsonov70386aa2012-06-28 08:27:24 +000049// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000050// to a header file.
51
52// Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} or
Alexey Samsonov70386aa2012-06-28 08:27:24 +000053// DECLARE_REAL(...) are located inside namespaces.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000054// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo);" to
55// effectively redirect calls from "foo" to "zoo". In this case
56// you aren't required to implement
Alexey Samsonov70386aa2012-06-28 08:27:24 +000057// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000058// but instead you'll have to add
Alexey Samsonov70386aa2012-06-28 08:27:24 +000059// DEFINE_REAL(int, foo, const char *bar, double baz) in your
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000060// source file (to define a pointer to overriden function).
61
62// How it works:
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000063// To replace system functions on Linux we just need to declare functions
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000064// with same names in our library and then obtain the real function pointers
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000065// using dlsym().
66// There is one complication. A user may also intercept some of the functions
Dmitry Vyukovbd310f02012-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 Vyukov7fb73302012-05-24 13:54:31 +000069// functions.
70// This is not so on Mac OS, where the two-level namespace makes
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000071// our replacement functions invisible to other libraries. This may be overcomed
72// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
Alexander Potapenko34157fc2013-02-05 15:57:12 +000073// libraries in Chromium were noticed when doing so.
74// Instead we create a dylib containing a __DATA,__interpose section that
75// associates library functions with their wrappers. When this dylib is
76// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
77// the calls to interposed functions done through stubs to the wrapper
78// functions.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000079
80#if defined(__APPLE__)
81# define WRAP(x) wrap_##x
82# define WRAPPER_NAME(x) "wrap_"#x
83# define INTERCEPTOR_ATTRIBUTE
Alexey Samsonov788eaeb2012-09-11 15:02:20 +000084# define DECLARE_WRAPPER(ret_type, func, ...)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000085#elif defined(_WIN32)
Timur Iskhodzhanov2f48b872012-03-12 11:45:09 +000086# if defined(_DLL) // DLL CRT
87# define WRAP(x) x
88# define WRAPPER_NAME(x) #x
89# define INTERCEPTOR_ATTRIBUTE
90# else // Static CRT
91# define WRAP(x) wrap_##x
92# define WRAPPER_NAME(x) "wrap_"#x
93# define INTERCEPTOR_ATTRIBUTE
94# endif
Alexey Samsonov788eaeb2012-09-11 15:02:20 +000095# define DECLARE_WRAPPER(ret_type, func, ...)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000096#else
Dmitry Vyukovbd310f02012-05-28 07:47:35 +000097# define WRAP(x) __interceptor_ ## x
98# define WRAPPER_NAME(x) "__interceptor_" #x
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000099# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000100# define DECLARE_WRAPPER(ret_type, func, ...) \
101 extern "C" ret_type func(__VA_ARGS__) \
Alexey Samsonovfa42dd72012-06-28 08:07:19 +0000102 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000103#endif
104
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000105#if !defined(__APPLE__)
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000106# define PTR_TO_REAL(x) real_##x
107# define REAL(x) __interception::PTR_TO_REAL(x)
108# define FUNC_TYPE(x) x##_f
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000109
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000110# define DECLARE_REAL(ret_type, func, ...) \
111 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
112 namespace __interception { \
113 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
114 }
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000115#else // __APPLE__
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000116# define REAL(x) x
117# define DECLARE_REAL(ret_type, func, ...) \
118 extern "C" ret_type func(__VA_ARGS__);
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000119#endif // __APPLE__
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000120
Chandler Carruthbbff2782012-06-25 06:53:10 +0000121#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonova81d2682012-09-12 09:42:23 +0000122 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000123 extern "C" ret_type WRAP(func)(__VA_ARGS__);
124
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000125// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
126// macros does its job. In exceptional cases you may need to call REAL(foo)
127// without defining INTERCEPTOR(..., foo, ...). For example, if you override
128// foo with an interceptor for other function.
Alexander Potapenko34157fc2013-02-05 15:57:12 +0000129#if !defined(__APPLE__)
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000130# define DEFINE_REAL(ret_type, func, ...) \
131 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
132 namespace __interception { \
133 FUNC_TYPE(func) PTR_TO_REAL(func); \
134 }
135#else
136# define DEFINE_REAL(ret_type, func, ...)
137#endif
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000138
139#define INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000140 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
141 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
142 extern "C" \
143 INTERCEPTOR_ATTRIBUTE \
144 ret_type WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000145
146#if defined(_WIN32)
147# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000148 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
149 namespace __interception { \
150 FUNC_TYPE(func) PTR_TO_REAL(func); \
151 } \
152 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
153 extern "C" \
154 INTERCEPTOR_ATTRIBUTE \
155 ret_type __stdcall WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000156#endif
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000157
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000158// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
159// so we use casting via an integral type __interception::uptr,
160// assuming that system is POSIX-compliant. Using other hacks seem
161// challenging, as we don't even pass function type to
162// INTERCEPT_FUNCTION macro, only its name.
163namespace __interception {
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000164#if defined(_WIN64)
165typedef unsigned long long uptr; // NOLINT
166#else
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000167typedef unsigned long uptr; // NOLINT
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000168#endif // _WIN64
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000169} // namespace __interception
170
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000171#define INCLUDED_FROM_INTERCEPTION_LIB
172
173#if defined(__linux__)
174# include "interception_linux.h"
175# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
176#elif defined(__APPLE__)
177# include "interception_mac.h"
178# define OVERRIDE_FUNCTION(old_func, new_func) \
179 OVERRIDE_FUNCTION_MAC(old_func, new_func)
180# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
181#else // defined(_WIN32)
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +0000182# include "interception_win.h"
183# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000184#endif
185
186#undef INCLUDED_FROM_INTERCEPTION_LIB
187
188#endif // INTERCEPTION_H