xref: /freebsd/contrib/llvm-project/compiler-rt/lib/interception/interception.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 //===-- interception.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // Machinery for providing replacements/wrappers for system functions.
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef INTERCEPTION_H
15 #define INTERCEPTION_H
16 
17 #include "sanitizer_common/sanitizer_asm.h"
18 #include "sanitizer_common/sanitizer_internal_defs.h"
19 
20 #if !SANITIZER_LINUX && !SANITIZER_FREEBSD && !SANITIZER_APPLE &&    \
21     !SANITIZER_NETBSD && !SANITIZER_WINDOWS && !SANITIZER_FUCHSIA && \
22     !SANITIZER_SOLARIS
23 #  error "Interception doesn't work on this operating system."
24 #endif
25 
26 // 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)
28 typedef __sanitizer::uptr    SIZE_T;
29 typedef __sanitizer::sptr    SSIZE_T;
30 typedef __sanitizer::sptr    PTRDIFF_T;
31 typedef __sanitizer::s64     INTMAX_T;
32 typedef __sanitizer::u64     UINTMAX_T;
33 typedef __sanitizer::OFF_T   OFF_T;
34 typedef __sanitizer::OFF64_T OFF64_T;
35 
36 // 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
41 //         your source file. See the notes below for cases when
42 //         INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
43 //      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:
49 //      3a) add DECLARE_REAL(int, foo, const char*, double) to a
50 //          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:
53 //      3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
54 //          to a header file.
55 
56 // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
57 //           DECLARE_REAL(...) are located inside namespaces.
58 //        2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
59 //           effectively redirect calls from "foo" to "zoo". In this case
60 //           you aren't required to implement
61 //           INTERCEPTOR(int, foo, const char *bar, double baz) {...}
62 //           but instead you'll have to add
63 //           DECLARE_REAL(int, foo, const char *bar, double baz) in your
64 //           source file (to define a pointer to overriden function).
65 //        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.
70 
71 // How it works on Linux
72 // ---------------------
73 //
74 // To replace system functions on Linux we just need to declare functions with
75 // the same names in our library and then obtain the real function pointers
76 // using dlsym().
77 //
78 // There is one complication: a user may also intercept some of the functions we
79 // intercept. To allow for up to 3 interceptors (including ours) of a given
80 // function "func", the interceptor implementation is in ___interceptor_func,
81 // which is aliased by a weak function __interceptor_func, which in turn is
82 // aliased (via a trampoline) by weak wrapper function "func".
83 //
84 // Most user interceptors should define a foreign interceptor as follows:
85 //
86 //  - provide a non-weak function "func" that performs interception;
87 //  - if __interceptor_func exists, call it to perform the real functionality;
88 //  - if it does not exist, figure out the real function and call it instead.
89 //
90 // In rare cases, a foreign interceptor (of another dynamic analysis runtime)
91 // may be defined as follows (on supported architectures):
92 //
93 //  - provide a non-weak function __interceptor_func that performs interception;
94 //  - if ___interceptor_func exists, call it to perform the real functionality;
95 //  - if it does not exist, figure out the real function and call it instead;
96 //  - provide a weak function "func" that is an alias to __interceptor_func.
97 //
98 // With this protocol, sanitizer interceptors, foreign user interceptors, and
99 // foreign interceptors of other dynamic analysis runtimes, or any combination
100 // thereof, may co-exist simultaneously.
101 //
102 // How it works on Mac OS
103 // ----------------------
104 //
105 // This is not so on Mac OS, where the two-level namespace makes our replacement
106 // functions invisible to other libraries. This may be overcomed using the
107 // DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared libraries in
108 // Chromium were noticed when doing so.
109 //
110 // Instead we create a dylib containing a __DATA,__interpose section that
111 // associates library functions with their wrappers. When this dylib is
112 // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all the
113 // calls to interposed functions done through stubs to the wrapper functions.
114 //
115 // As it's decided at compile time which functions are to be intercepted on Mac,
116 // INTERCEPT_FUNCTION() is effectively a no-op on this system.
117 
118 #if SANITIZER_APPLE
119 #include <sys/cdefs.h>  // For __DARWIN_ALIAS_C().
120 
121 // Just a pair of pointers.
122 struct interpose_substitution {
123   const __sanitizer::uptr replacement;
124   const __sanitizer::uptr original;
125 };
126 
127 // For a function foo() create a global pair of pointers { wrap_foo, foo } in
128 // the __DATA,__interpose section.
129 // As a result all the calls to foo() will be routed to wrap_foo() at runtime.
130 #define INTERPOSER(func_name) __attribute__((used))     \
131 const interpose_substitution substitution_##func_name[] \
132     __attribute__((section("__DATA, __interpose"))) = { \
133     { reinterpret_cast<const uptr>(WRAP(func_name)),    \
134       reinterpret_cast<const uptr>(func_name) }         \
135 }
136 
137 // For a function foo() and a wrapper function bar() create a global pair
138 // of pointers { bar, foo } in the __DATA,__interpose section.
139 // As a result all the calls to foo() will be routed to bar() at runtime.
140 #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
141 const interpose_substitution substitution_##func_name[]             \
142     __attribute__((section("__DATA, __interpose"))) = {             \
143     { reinterpret_cast<const uptr>(wrapper_name),                   \
144       reinterpret_cast<const uptr>(func_name) }                     \
145 }
146 
147 # define WRAP(x) wrap_##x
148 # define TRAMPOLINE(x) WRAP(x)
149 # define INTERCEPTOR_ATTRIBUTE
150 # define DECLARE_WRAPPER(ret_type, func, ...)
151 
152 #elif SANITIZER_WINDOWS
153 # define WRAP(x) __asan_wrap_##x
154 # define TRAMPOLINE(x) WRAP(x)
155 # define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
156 # define DECLARE_WRAPPER(ret_type, func, ...)         \
157     extern "C" ret_type func(__VA_ARGS__);
158 # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...)  \
159     extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
160 #elif !SANITIZER_FUCHSIA  // LINUX, FREEBSD, NETBSD, SOLARIS
161 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
162 # if ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
163 // Weak aliases of weak aliases do not work, therefore we need to set up a
164 // trampoline function. The function "func" is a weak alias to the trampoline
165 // (so that we may check if "func" was overridden), which calls the weak
166 // function __interceptor_func, which in turn aliases the actual interceptor
167 // implementation ___interceptor_func:
168 //
169 //    [wrapper "func": weak] --(alias)--> [TRAMPOLINE(func)]
170 //                                                |
171 //                     +--------(tail call)-------+
172 //                     |
173 //                     v
174 //      [__interceptor_func: weak] --(alias)--> [WRAP(func)]
175 //
176 // We use inline assembly to define most of this, because not all compilers
177 // support functions with the "naked" attribute with every architecture.
178 #  define WRAP(x) ___interceptor_ ## x
179 #  define TRAMPOLINE(x) __interceptor_trampoline_ ## x
180 #  if SANITIZER_FREEBSD || SANITIZER_NETBSD
181 // FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher
182 // priority than weak ones so weak aliases won't work for indirect calls
183 // in position-independent (-fPIC / -fPIE) mode.
184 #   define __ASM_WEAK_WRAPPER(func) ".globl " #func "\n"
185 #  else
186 #   define __ASM_WEAK_WRAPPER(func) ".weak " #func "\n"
187 #  endif  // SANITIZER_FREEBSD || SANITIZER_NETBSD
188 // Keep trampoline implementation in sync with sanitizer_common/sanitizer_asm.h
189 #  define DECLARE_WRAPPER(ret_type, func, ...)                                 \
190      extern "C" ret_type func(__VA_ARGS__);                                    \
191      extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__);                        \
192      extern "C" ret_type __interceptor_##func(__VA_ARGS__)                     \
193        INTERCEPTOR_ATTRIBUTE __attribute__((weak)) ALIAS(WRAP(func));          \
194      asm(                                                                      \
195        ".text\n"                                                               \
196        __ASM_WEAK_WRAPPER(func)                                                \
197        ".set " #func ", " SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n"           \
198        ".globl " SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n"                    \
199        ".type  " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", %function\n"         \
200        SANITIZER_STRINGIFY(TRAMPOLINE(func)) ":\n"                             \
201        SANITIZER_STRINGIFY(CFI_STARTPROC) "\n"                                 \
202        SANITIZER_STRINGIFY(ASM_TAIL_CALL) " __interceptor_"                    \
203          SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func)) "\n"                   \
204        SANITIZER_STRINGIFY(CFI_ENDPROC) "\n"                                   \
205        ".size  " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", "                    \
206             ".-" SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n"                    \
207      );
208 # else  // ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
209 // Some architectures cannot implement efficient interceptor trampolines with
210 // just a plain jump due to complexities of resolving a preemptible symbol. In
211 // those cases, revert to just this scheme:
212 //
213 //    [wrapper "func": weak] --(alias)--> [WRAP(func)]
214 //
215 #  define WRAP(x) __interceptor_ ## x
216 #  define TRAMPOLINE(x) WRAP(x)
217 #  if SANITIZER_FREEBSD || SANITIZER_NETBSD
218 #   define __ATTRIBUTE_WEAK_WRAPPER
219 #  else
220 #   define __ATTRIBUTE_WEAK_WRAPPER __attribute__((weak))
221 #  endif  // SANITIZER_FREEBSD || SANITIZER_NETBSD
222 #  define DECLARE_WRAPPER(ret_type, func, ...)                                 \
223      extern "C" ret_type func(__VA_ARGS__)                                     \
224        INTERCEPTOR_ATTRIBUTE __ATTRIBUTE_WEAK_WRAPPER ALIAS(WRAP(func));
225 # endif  // ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
226 #endif
227 
228 #if SANITIZER_FUCHSIA
229 // There is no general interception at all on Fuchsia.
230 // Sanitizer runtimes just define functions directly to preempt them,
231 // and have bespoke ways to access the underlying libc functions.
232 # include <zircon/sanitizer.h>
233 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
234 # define REAL(x) __unsanitized_##x
235 # define DECLARE_REAL(ret_type, func, ...)
236 #elif !SANITIZER_APPLE
237 # define PTR_TO_REAL(x) real_##x
238 # define REAL(x) __interception::PTR_TO_REAL(x)
239 # define FUNC_TYPE(x) x##_type
240 
241 # define DECLARE_REAL(ret_type, func, ...)            \
242     typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
243     namespace __interception {                        \
244     extern FUNC_TYPE(func) PTR_TO_REAL(func);         \
245     }
246 # define ASSIGN_REAL(dst, src) REAL(dst) = REAL(src)
247 #else  // SANITIZER_APPLE
248 # define REAL(x) x
249 # define DECLARE_REAL(ret_type, func, ...) \
250     extern "C" ret_type func(__VA_ARGS__);
251 # define ASSIGN_REAL(x, y)
252 #endif  // SANITIZER_APPLE
253 
254 #if !SANITIZER_FUCHSIA
255 # define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)  \
256     DECLARE_REAL(ret_type, func, __VA_ARGS__)               \
257     extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__);      \
258     extern "C" ret_type WRAP(func)(__VA_ARGS__);
259 // Declare an interceptor and its wrapper defined in a different translation
260 // unit (ex. asm).
261 # define DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(ret_type, func, ...)  \
262     extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__);                \
263     extern "C" ret_type WRAP(func)(__VA_ARGS__);                      \
264     extern "C" ret_type func(__VA_ARGS__);
265 #else
266 # define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)
267 # define DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(ret_type, func, ...)
268 #endif
269 
270 // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
271 // macros does its job. In exceptional cases you may need to call REAL(foo)
272 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
273 // foo with an interceptor for other function.
274 #if !SANITIZER_APPLE && !SANITIZER_FUCHSIA
275 #  define DEFINE_REAL(ret_type, func, ...)            \
276     typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
277     namespace __interception {                        \
278     FUNC_TYPE(func) PTR_TO_REAL(func);                \
279     }
280 #else
281 # define DEFINE_REAL(ret_type, func, ...)
282 #endif
283 
284 #if SANITIZER_FUCHSIA
285 
286 // We need to define the __interceptor_func name just to get
287 // sanitizer_common/scripts/gen_dynamic_list.py to export func.
288 // But we don't need to export __interceptor_func to get that.
289 #define INTERCEPTOR(ret_type, func, ...)                                \
290   extern "C"[[ gnu::alias(#func), gnu::visibility("hidden") ]] ret_type \
291       __interceptor_##func(__VA_ARGS__);                                \
292   extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__)
293 
294 #elif !SANITIZER_APPLE
295 
296 #define INTERCEPTOR(ret_type, func, ...)        \
297   DEFINE_REAL(ret_type, func, __VA_ARGS__)      \
298   DECLARE_WRAPPER(ret_type, func, __VA_ARGS__)  \
299   extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
300 
301 // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
302 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
303   INTERCEPTOR(ret_type, func, __VA_ARGS__)
304 
305 #else  // SANITIZER_APPLE
306 
307 #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...)  \
308   extern "C" ret_type func(__VA_ARGS__) suffix;       \
309   extern "C" ret_type WRAP(func)(__VA_ARGS__);        \
310   INTERPOSER(func);                                   \
311   extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
312 
313 #define INTERCEPTOR(ret_type, func, ...) \
314   INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
315 
316 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
317   INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
318 
319 // Override |overridee| with |overrider|.
320 #define OVERRIDE_FUNCTION(overridee, overrider) \
321   INTERPOSER_2(overridee, WRAP(overrider))
322 #endif
323 
324 #if SANITIZER_WINDOWS
325 # define INTERCEPTOR_WINAPI(ret_type, func, ...)                \
326     typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
327     namespace __interception {                                  \
328       FUNC_TYPE(func) PTR_TO_REAL(func);                        \
329     }                                                           \
330     extern "C" INTERCEPTOR_ATTRIBUTE ret_type __stdcall WRAP(func)(__VA_ARGS__)
331 #endif
332 
333 // ISO C++ forbids casting between pointer-to-function and pointer-to-object,
334 // so we use casting via an integral type __interception::uptr,
335 // assuming that system is POSIX-compliant. Using other hacks seem
336 // challenging, as we don't even pass function type to
337 // INTERCEPT_FUNCTION macro, only its name.
338 namespace __interception {
339 #if defined(_WIN64)
340 typedef unsigned long long uptr;
341 #else
342 typedef unsigned long uptr;
343 #endif  // _WIN64
344 }  // namespace __interception
345 
346 #define INCLUDED_FROM_INTERCEPTION_LIB
347 
348 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
349     SANITIZER_SOLARIS
350 
351 # include "interception_linux.h"
352 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
353 # define INTERCEPT_FUNCTION_VER(func, symver) \
354     INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
355 #elif SANITIZER_APPLE
356 # include "interception_mac.h"
357 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
358 # define INTERCEPT_FUNCTION_VER(func, symver) \
359     INTERCEPT_FUNCTION_VER_MAC(func, symver)
360 #elif SANITIZER_WINDOWS
361 # include "interception_win.h"
362 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
363 # define INTERCEPT_FUNCTION_VER(func, symver) \
364     INTERCEPT_FUNCTION_VER_WIN(func, symver)
365 #endif
366 
367 #undef INCLUDED_FROM_INTERCEPTION_LIB
368 
369 #endif  // INTERCEPTION_H
370