blob: 87b2365fd7671bc17b9f18fbedeb176b7e2579e9 [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
Kamil Rytarowski90b46352017-12-06 17:02:00 +000018#include "sanitizer_common/sanitizer_internal_defs.h"
19
20#if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_MAC && \
Kamil Rytarowski7e552452018-03-02 07:32:30 +000021 !SANITIZER_NETBSD && !SANITIZER_OPENBSD && !SANITIZER_WINDOWS && \
Walter Leeb134dbb2018-05-18 00:43:54 +000022 !SANITIZER_FUCHSIA && !SANITIZER_RTEMS && !SANITIZER_SOLARIS
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000023# error "Interception doesn't work on this operating system."
24#endif
25
Kostya Serebryany07bb3922012-12-13 06:31:40 +000026// These typedefs should be used only in the interceptor definitions to replace
27// the standard system types (e.g. SSIZE_T instead of ssize_t)
Kostya Serebryanyf0b8f982013-02-27 11:22:40 +000028typedef __sanitizer::uptr SIZE_T;
29typedef __sanitizer::sptr SSIZE_T;
30typedef __sanitizer::sptr PTRDIFF_T;
31typedef __sanitizer::s64 INTMAX_T;
Kamil Rytarowskid1c1e032018-08-29 09:11:17 +000032typedef __sanitizer::u64 UINTMAX_T;
Kostya Serebryanyf0b8f982013-02-27 11:22:40 +000033typedef __sanitizer::OFF_T OFF_T;
34typedef __sanitizer::OFF64_T OFF64_T;
Kostya Serebryany07bb3922012-12-13 06:31:40 +000035
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000036// How to add an interceptor:
37// Suppose you need to wrap/replace system function (generally, from libc):
38// int foo(const char *bar, double baz);
39// You'll need to:
40// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
Alexander Potapenkoa15d49c2013-05-20 13:32:35 +000041// your source file. See the notes below for cases when
42// INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000043// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
44// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
45// intercepted successfully.
46// You can access original function by calling REAL(foo)(bar, baz).
47// By default, REAL(foo) will be visible only inside your interceptor, and if
48// you want to use it in other parts of RTL, you'll need to:
Alexey Samsonov70386aa2012-06-28 08:27:24 +000049// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000050// header file.
51// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
52// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
Alexey Samsonov70386aa2012-06-28 08:27:24 +000053// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000054// to a header file.
55
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000056// Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
Alexey Samsonov70386aa2012-06-28 08:27:24 +000057// DECLARE_REAL(...) are located inside namespaces.
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000058// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000059// effectively redirect calls from "foo" to "zoo". In this case
60// you aren't required to implement
Alexey Samsonov70386aa2012-06-28 08:27:24 +000061// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000062// but instead you'll have to add
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000063// DECLARE_REAL(int, foo, const char *bar, double baz) in your
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000064// source file (to define a pointer to overriden function).
Alexander Potapenkoa15d49c2013-05-20 13:32:35 +000065// 3. Some Mac functions have symbol variants discriminated by
66// additional suffixes, e.g. _$UNIX2003 (see
67// https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
68// for more details). To intercept such functions you need to use the
69// INTERCEPTOR_WITH_SUFFIX(...) macro.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000070
71// How it works:
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000072// To replace system functions on Linux we just need to declare functions
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000073// with same names in our library and then obtain the real function pointers
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000074// using dlsym().
75// There is one complication. A user may also intercept some of the functions
Dmitry Vyukovbd310f02012-05-28 07:47:35 +000076// we intercept. To resolve this we declare our interceptors with __interceptor_
77// prefix, and then make actual interceptors weak aliases to __interceptor_
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000078// functions.
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000079//
Dmitry Vyukov7fb73302012-05-24 13:54:31 +000080// 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
Alexander Potapenko34157fc2013-02-05 15:57:12 +000083// libraries in Chromium were noticed when doing so.
84// Instead we create a dylib containing a __DATA,__interpose section that
85// associates library functions with their wrappers. When this dylib is
86// preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
87// the calls to interposed functions done through stubs to the wrapper
88// functions.
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000089// As it's decided at compile time which functions are to be intercepted on Mac,
90// INTERCEPT_FUNCTION() is effectively a no-op on this system.
Alexey Samsonov8489f2a2012-02-08 19:52:01 +000091
Kamil Rytarowski90b46352017-12-06 17:02:00 +000092#if SANITIZER_MAC
Alexander Potapenkoa15d49c2013-05-20 13:32:35 +000093#include <sys/cdefs.h> // For __DARWIN_ALIAS_C().
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000094
95// Just a pair of pointers.
96struct interpose_substitution {
Anna Zaks691644f2016-09-15 21:02:18 +000097 const __sanitizer::uptr replacement;
98 const __sanitizer::uptr original;
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +000099};
100
101// For a function foo() create a global pair of pointers { wrap_foo, foo } in
102// the __DATA,__interpose section.
103// As a result all the calls to foo() will be routed to wrap_foo() at runtime.
104#define INTERPOSER(func_name) __attribute__((used)) \
105const interpose_substitution substitution_##func_name[] \
106 __attribute__((section("__DATA, __interpose"))) = { \
107 { reinterpret_cast<const uptr>(WRAP(func_name)), \
108 reinterpret_cast<const uptr>(func_name) } \
109}
110
111// For a function foo() and a wrapper function bar() create a global pair
112// of pointers { bar, foo } in the __DATA,__interpose section.
113// As a result all the calls to foo() will be routed to bar() at runtime.
114#define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
115const interpose_substitution substitution_##func_name[] \
116 __attribute__((section("__DATA, __interpose"))) = { \
117 { reinterpret_cast<const uptr>(wrapper_name), \
118 reinterpret_cast<const uptr>(func_name) } \
119}
120
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000121# define WRAP(x) wrap_##x
122# define WRAPPER_NAME(x) "wrap_"#x
123# define INTERCEPTOR_ATTRIBUTE
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000124# define DECLARE_WRAPPER(ret_type, func, ...)
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +0000125
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000126#elif SANITIZER_WINDOWS
Timur Iskhodzhanov220ddac2014-08-22 12:38:07 +0000127# define WRAP(x) __asan_wrap_##x
128# define WRAPPER_NAME(x) "__asan_wrap_"#x
129# define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
Timur Iskhodzhanov92702d82013-09-10 14:42:15 +0000130# define DECLARE_WRAPPER(ret_type, func, ...) \
131 extern "C" ret_type func(__VA_ARGS__);
132# define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
133 extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
Walter Leeb134dbb2018-05-18 00:43:54 +0000134#elif SANITIZER_RTEMS
135# define WRAP(x) x
136# define WRAPPER_NAME(x) #x
137# define INTERCEPTOR_ATTRIBUTE
138# define DECLARE_WRAPPER(ret_type, func, ...)
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000139#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
Viktor Kutuzovf294f252014-07-10 09:16:58 +0000140# define WRAP(x) __interceptor_ ## x
141# define WRAPPER_NAME(x) "__interceptor_" #x
142# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
143// FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher
144// priority than weak ones so weak aliases won't work for indirect calls
145// in position-independent (-fPIC / -fPIE) mode.
146# define DECLARE_WRAPPER(ret_type, func, ...) \
147 extern "C" ret_type func(__VA_ARGS__) \
148 __attribute__((alias("__interceptor_" #func), visibility("default")));
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000149#elif !SANITIZER_FUCHSIA
Dmitry Vyukovbd310f02012-05-28 07:47:35 +0000150# define WRAP(x) __interceptor_ ## x
151# define WRAPPER_NAME(x) "__interceptor_" #x
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000152# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000153# define DECLARE_WRAPPER(ret_type, func, ...) \
154 extern "C" ret_type func(__VA_ARGS__) \
Alexey Samsonovfa42dd72012-06-28 08:07:19 +0000155 __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000156#endif
157
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000158#if SANITIZER_FUCHSIA
Vitaly Bukad4e03d52017-08-01 21:15:19 +0000159// There is no general interception at all on Fuchsia.
160// Sanitizer runtimes just define functions directly to preempt them,
161// and have bespoke ways to access the underlying libc functions.
Petr Hosekd8328f12017-09-13 01:18:15 +0000162# include <zircon/sanitizer.h>
Vitaly Bukad4e03d52017-08-01 21:15:19 +0000163# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
164# define REAL(x) __unsanitized_##x
165# define DECLARE_REAL(ret_type, func, ...)
Walter Leeb134dbb2018-05-18 00:43:54 +0000166#elif SANITIZER_RTEMS
167# define REAL(x) __real_ ## x
168# define DECLARE_REAL(ret_type, func, ...) \
169 extern "C" ret_type REAL(func)(__VA_ARGS__);
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000170#elif !SANITIZER_MAC
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000171# define PTR_TO_REAL(x) real_##x
172# define REAL(x) __interception::PTR_TO_REAL(x)
Kuba Mracek975352c2018-10-22 20:54:48 +0000173# define FUNC_TYPE(x) x##_type
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000174
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000175# define DECLARE_REAL(ret_type, func, ...) \
176 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
177 namespace __interception { \
178 extern FUNC_TYPE(func) PTR_TO_REAL(func); \
179 }
Etienne Bergeron8c6eb152016-07-27 16:16:54 +0000180# define ASSIGN_REAL(dst, src) REAL(dst) = REAL(src)
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000181#else // SANITIZER_MAC
Alexander Potapenkoc62210e2012-08-17 09:00:08 +0000182# define REAL(x) x
183# define DECLARE_REAL(ret_type, func, ...) \
184 extern "C" ret_type func(__VA_ARGS__);
Etienne Bergeron8c6eb152016-07-27 16:16:54 +0000185# define ASSIGN_REAL(x, y)
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000186#endif // SANITIZER_MAC
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000187
Walter Leeb134dbb2018-05-18 00:43:54 +0000188#if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
Chandler Carruthbbff2782012-06-25 06:53:10 +0000189#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonova81d2682012-09-12 09:42:23 +0000190 DECLARE_REAL(ret_type, func, __VA_ARGS__) \
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000191 extern "C" ret_type WRAP(func)(__VA_ARGS__);
Vitaly Bukad4e03d52017-08-01 21:15:19 +0000192#else
193#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)
194#endif
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000195
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000196// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
197// macros does its job. In exceptional cases you may need to call REAL(foo)
198// without defining INTERCEPTOR(..., foo, ...). For example, if you override
199// foo with an interceptor for other function.
Walter Leeb134dbb2018-05-18 00:43:54 +0000200#if !SANITIZER_MAC && !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000201# define DEFINE_REAL(ret_type, func, ...) \
202 typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
203 namespace __interception { \
204 FUNC_TYPE(func) PTR_TO_REAL(func); \
205 }
206#else
207# define DEFINE_REAL(ret_type, func, ...)
208#endif
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000209
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000210#if SANITIZER_FUCHSIA
Vitaly Bukad4e03d52017-08-01 21:15:19 +0000211
212// We need to define the __interceptor_func name just to get
213// sanitizer_common/scripts/gen_dynamic_list.py to export func.
214// But we don't need to export __interceptor_func to get that.
215#define INTERCEPTOR(ret_type, func, ...) \
216 extern "C"[[ gnu::alias(#func), gnu::visibility("hidden") ]] ret_type \
217 __interceptor_##func(__VA_ARGS__); \
218 extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__)
219
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000220#elif !SANITIZER_MAC
Vitaly Bukad4e03d52017-08-01 21:15:19 +0000221
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000222#define INTERCEPTOR(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000223 DEFINE_REAL(ret_type, func, __VA_ARGS__) \
224 DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
225 extern "C" \
226 INTERCEPTOR_ATTRIBUTE \
227 ret_type WRAP(func)(__VA_ARGS__)
Alexander Potapenkoa15d49c2013-05-20 13:32:35 +0000228
229// We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
230#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
231 INTERCEPTOR(ret_type, func, __VA_ARGS__)
232
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000233#else // SANITIZER_MAC
Alexander Potapenkoa15d49c2013-05-20 13:32:35 +0000234
235#define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
236 extern "C" ret_type func(__VA_ARGS__) suffix; \
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +0000237 extern "C" ret_type WRAP(func)(__VA_ARGS__); \
238 INTERPOSER(func); \
239 extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
240
Alexander Potapenkoa15d49c2013-05-20 13:32:35 +0000241#define INTERCEPTOR(ret_type, func, ...) \
242 INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
243
244#define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
245 INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
246
Alexander Potapenkoe8ba1c82013-02-21 14:41:16 +0000247// Override |overridee| with |overrider|.
248#define OVERRIDE_FUNCTION(overridee, overrider) \
249 INTERPOSER_2(overridee, WRAP(overrider))
250#endif
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000251
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000252#if SANITIZER_WINDOWS
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000253# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000254 typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
255 namespace __interception { \
256 FUNC_TYPE(func) PTR_TO_REAL(func); \
257 } \
Alexey Samsonov788eaeb2012-09-11 15:02:20 +0000258 extern "C" \
259 INTERCEPTOR_ATTRIBUTE \
260 ret_type __stdcall WRAP(func)(__VA_ARGS__)
Timur Iskhodzhanov0f9c9a52012-02-24 15:28:43 +0000261#endif
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000262
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000263// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
264// so we use casting via an integral type __interception::uptr,
265// assuming that system is POSIX-compliant. Using other hacks seem
266// challenging, as we don't even pass function type to
267// INTERCEPT_FUNCTION macro, only its name.
268namespace __interception {
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000269#if defined(_WIN64)
270typedef unsigned long long uptr; // NOLINT
271#else
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000272typedef unsigned long uptr; // NOLINT
Alexey Samsonova555b3f2012-09-24 11:43:40 +0000273#endif // _WIN64
Alexey Samsonov0f840bd2012-08-02 11:19:13 +0000274} // namespace __interception
275
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000276#define INCLUDED_FROM_INTERCEPTION_LIB
277
Kamil Rytarowski271018d2017-12-14 20:14:29 +0000278#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
Kamil Rytarowski7e552452018-03-02 07:32:30 +0000279 SANITIZER_OPENBSD || SANITIZER_SOLARIS
Kamil Rytarowski271018d2017-12-14 20:14:29 +0000280
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000281# include "interception_linux.h"
Kostya Serebryany04f2bf02014-02-24 08:37:41 +0000282# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
Alexey Samsonovedecc382013-10-16 08:20:31 +0000283# define INTERCEPT_FUNCTION_VER(func, symver) \
Kostya Serebryany04f2bf02014-02-24 08:37:41 +0000284 INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000285#elif SANITIZER_MAC
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000286# include "interception_mac.h"
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000287# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
Alexey Samsonovedecc382013-10-16 08:20:31 +0000288# define INTERCEPT_FUNCTION_VER(func, symver) \
289 INTERCEPT_FUNCTION_VER_MAC(func, symver)
Kamil Rytarowski90b46352017-12-06 17:02:00 +0000290#elif SANITIZER_WINDOWS
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +0000291# include "interception_win.h"
292# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonovedecc382013-10-16 08:20:31 +0000293# define INTERCEPT_FUNCTION_VER(func, symver) \
294 INTERCEPT_FUNCTION_VER_WIN(func, symver)
Alexey Samsonov8489f2a2012-02-08 19:52:01 +0000295#endif
296
297#undef INCLUDED_FROM_INTERCEPTION_LIB
298
299#endif // INTERCEPTION_H