xref: /llvm-project/compiler-rt/lib/interception/interception.h (revision ce44640fe29550461120d22b0358e6cac4aed822)
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 // On Windows the system headers (basetsd.h) provide a conflicting definition
29 // of SIZE_T/SSIZE_T that do not match the real size_t/ssize_t for 32-bit
30 // systems (using long instead of the expected int). Work around the typedef
31 // redefinition by #defining SIZE_T instead of using a typedef.
32 // TODO: We should be using __sanitizer::usize (and a new ssize) instead of
33 // these new macros as long as we ensure they match the real system definitions.
34 #if SANITIZER_WINDOWS
35 // Ensure that (S)SIZE_T were already defined as we are about to override them.
36 #  include <basetsd.h>
37 #endif
38 
39 #define SIZE_T __sanitizer::usize
40 #define SSIZE_T __sanitizer::ssize
41 typedef __sanitizer::sptr    PTRDIFF_T;
42 typedef __sanitizer::s64     INTMAX_T;
43 typedef __sanitizer::u64     UINTMAX_T;
44 typedef __sanitizer::OFF_T   OFF_T;
45 typedef __sanitizer::OFF64_T OFF64_T;
46 
47 // How to add an interceptor:
48 // Suppose you need to wrap/replace system function (generally, from libc):
49 //      int foo(const char *bar, double baz);
50 // You'll need to:
51 //      1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
52 //         your source file. See the notes below for cases when
53 //         INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
54 //      2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
55 //         INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
56 //         intercepted successfully.
57 // You can access original function by calling REAL(foo)(bar, baz).
58 // By default, REAL(foo) will be visible only inside your interceptor, and if
59 // you want to use it in other parts of RTL, you'll need to:
60 //      3a) add DECLARE_REAL(int, foo, const char*, double) to a
61 //          header file.
62 // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
63 // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
64 //      3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
65 //          to a header file.
66 
67 // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
68 //           DECLARE_REAL(...) are located inside namespaces.
69 //        2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
70 //           effectively redirect calls from "foo" to "zoo". In this case
71 //           you aren't required to implement
72 //           INTERCEPTOR(int, foo, const char *bar, double baz) {...}
73 //           but instead you'll have to add
74 //           DECLARE_REAL(int, foo, const char *bar, double baz) in your
75 //           source file (to define a pointer to overriden function).
76 //        3. Some Mac functions have symbol variants discriminated by
77 //           additional suffixes, e.g. _$UNIX2003 (see
78 //           https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
79 //           for more details). To intercept such functions you need to use the
80 //           INTERCEPTOR_WITH_SUFFIX(...) macro.
81 
82 // How it works on Linux
83 // ---------------------
84 //
85 // To replace system functions on Linux we just need to declare functions with
86 // the same names in our library and then obtain the real function pointers
87 // using dlsym().
88 //
89 // There is one complication: a user may also intercept some of the functions we
90 // intercept. To allow for up to 3 interceptors (including ours) of a given
91 // function "func", the interceptor implementation is in ___interceptor_func,
92 // which is aliased by a weak function __interceptor_func, which in turn is
93 // aliased (via a trampoline) by weak wrapper function "func".
94 //
95 // Most user interceptors should define a foreign interceptor as follows:
96 //
97 //  - provide a non-weak function "func" that performs interception;
98 //  - if __interceptor_func exists, call it to perform the real functionality;
99 //  - if it does not exist, figure out the real function and call it instead.
100 //
101 // In rare cases, a foreign interceptor (of another dynamic analysis runtime)
102 // may be defined as follows (on supported architectures):
103 //
104 //  - provide a non-weak function __interceptor_func that performs interception;
105 //  - if ___interceptor_func exists, call it to perform the real functionality;
106 //  - if it does not exist, figure out the real function and call it instead;
107 //  - provide a weak function "func" that is an alias to __interceptor_func.
108 //
109 // With this protocol, sanitizer interceptors, foreign user interceptors, and
110 // foreign interceptors of other dynamic analysis runtimes, or any combination
111 // thereof, may co-exist simultaneously.
112 //
113 // How it works on Mac OS
114 // ----------------------
115 //
116 // This is not so on Mac OS, where the two-level namespace makes our replacement
117 // functions invisible to other libraries. This may be overcomed using the
118 // DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared libraries in
119 // Chromium were noticed when doing so.
120 //
121 // Instead we create a dylib containing a __DATA,__interpose section that
122 // associates library functions with their wrappers. When this dylib is
123 // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all the
124 // calls to interposed functions done through stubs to the wrapper functions.
125 //
126 // As it's decided at compile time which functions are to be intercepted on Mac,
127 // INTERCEPT_FUNCTION() is effectively a no-op on this system.
128 
129 #if SANITIZER_APPLE
130 #include <sys/cdefs.h>  // For __DARWIN_ALIAS_C().
131 
132 // Just a pair of pointers.
133 struct interpose_substitution {
134   const __sanitizer::uptr replacement;
135   const __sanitizer::uptr original;
136 };
137 
138 // For a function foo() create a global pair of pointers { wrap_foo, foo } in
139 // the __DATA,__interpose section.
140 // As a result all the calls to foo() will be routed to wrap_foo() at runtime.
141 #define INTERPOSER(func_name) __attribute__((used))     \
142 const interpose_substitution substitution_##func_name[] \
143     __attribute__((section("__DATA, __interpose"))) = { \
144     { reinterpret_cast<const uptr>(WRAP(func_name)),    \
145       reinterpret_cast<const uptr>(func_name) }         \
146 }
147 
148 // For a function foo() and a wrapper function bar() create a global pair
149 // of pointers { bar, foo } in the __DATA,__interpose section.
150 // As a result all the calls to foo() will be routed to bar() at runtime.
151 #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
152 const interpose_substitution substitution_##func_name[]             \
153     __attribute__((section("__DATA, __interpose"))) = {             \
154     { reinterpret_cast<const uptr>(wrapper_name),                   \
155       reinterpret_cast<const uptr>(func_name) }                     \
156 }
157 
158 # define WRAP(x) wrap_##x
159 # define TRAMPOLINE(x) WRAP(x)
160 # define INTERCEPTOR_ATTRIBUTE
161 # define DECLARE_WRAPPER(ret_type, func, ...)
162 
163 #elif SANITIZER_WINDOWS
164 # define WRAP(x) __asan_wrap_##x
165 # define TRAMPOLINE(x) WRAP(x)
166 # define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
167 # define DECLARE_WRAPPER(ret_type, func, ...)         \
168     extern "C" ret_type func(__VA_ARGS__);
169 # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...)  \
170     extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
171 #elif !SANITIZER_FUCHSIA  // LINUX, FREEBSD, NETBSD, SOLARIS
172 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
173 # if ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
174 // Weak aliases of weak aliases do not work, therefore we need to set up a
175 // trampoline function. The function "func" is a weak alias to the trampoline
176 // (so that we may check if "func" was overridden), which calls the weak
177 // function __interceptor_func, which in turn aliases the actual interceptor
178 // implementation ___interceptor_func:
179 //
180 //    [wrapper "func": weak] --(alias)--> [TRAMPOLINE(func)]
181 //                                                |
182 //                     +--------(tail call)-------+
183 //                     |
184 //                     v
185 //      [__interceptor_func: weak] --(alias)--> [WRAP(func)]
186 //
187 // We use inline assembly to define most of this, because not all compilers
188 // support functions with the "naked" attribute with every architecture.
189 #  define WRAP(x) ___interceptor_ ## x
190 #  define TRAMPOLINE(x) __interceptor_trampoline_ ## x
191 #  if SANITIZER_FREEBSD || SANITIZER_NETBSD
192 // FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher
193 // priority than weak ones so weak aliases won't work for indirect calls
194 // in position-independent (-fPIC / -fPIE) mode.
195 #   define __ASM_WEAK_WRAPPER(func) ".globl " #func "\n"
196 #  else
197 #   define __ASM_WEAK_WRAPPER(func) ".weak " #func "\n"
198 #  endif  // SANITIZER_FREEBSD || SANITIZER_NETBSD
199 #  if defined(__arm__) || defined(__aarch64__)
200 #   define ASM_TYPE_FUNCTION_STR "%function"
201 #  else
202 #   define ASM_TYPE_FUNCTION_STR "@function"
203 #  endif
204 // Keep trampoline implementation in sync with sanitizer_common/sanitizer_asm.h
205 #  define DECLARE_WRAPPER(ret_type, func, ...)                                 \
206      extern "C" ret_type func(__VA_ARGS__);                                    \
207      extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__);                        \
208      extern "C" ret_type __interceptor_##func(__VA_ARGS__)                     \
209        INTERCEPTOR_ATTRIBUTE __attribute__((weak)) ALIAS(WRAP(func));          \
210      asm(                                                                      \
211        ".text\n"                                                               \
212        __ASM_WEAK_WRAPPER(func)                                                \
213        ".set " #func ", " SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n"           \
214        ".globl " SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n"                    \
215        ".type  " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", "                    \
216          ASM_TYPE_FUNCTION_STR "\n"                                            \
217        SANITIZER_STRINGIFY(TRAMPOLINE(func)) ":\n"                             \
218        C_ASM_STARTPROC "\n"                                                    \
219        C_ASM_TAIL_CALL(SANITIZER_STRINGIFY(TRAMPOLINE(func)),                  \
220                        "__interceptor_"                                        \
221                          SANITIZER_STRINGIFY(ASM_PREEMPTIBLE_SYM(func))) "\n"  \
222        C_ASM_ENDPROC "\n"                                                      \
223        ".size  " SANITIZER_STRINGIFY(TRAMPOLINE(func)) ", "                    \
224             ".-" SANITIZER_STRINGIFY(TRAMPOLINE(func)) "\n"                    \
225      );
226 # else  // ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
227 // Some architectures cannot implement efficient interceptor trampolines with
228 // just a plain jump due to complexities of resolving a preemptible symbol. In
229 // those cases, revert to just this scheme:
230 //
231 //    [wrapper "func": weak] --(alias)--> [WRAP(func)]
232 //
233 #  define WRAP(x) __interceptor_ ## x
234 #  define TRAMPOLINE(x) WRAP(x)
235 #  if SANITIZER_FREEBSD || SANITIZER_NETBSD
236 #   define __ATTRIBUTE_WEAK_WRAPPER
237 #  else
238 #   define __ATTRIBUTE_WEAK_WRAPPER __attribute__((weak))
239 #  endif  // SANITIZER_FREEBSD || SANITIZER_NETBSD
240 #  define DECLARE_WRAPPER(ret_type, func, ...)                                 \
241      extern "C" ret_type func(__VA_ARGS__)                                     \
242        INTERCEPTOR_ATTRIBUTE __ATTRIBUTE_WEAK_WRAPPER ALIAS(WRAP(func));
243 # endif  // ASM_INTERCEPTOR_TRAMPOLINE_SUPPORT
244 #endif
245 
246 #if SANITIZER_FUCHSIA
247 // There is no general interception at all on Fuchsia.
248 // Sanitizer runtimes just define functions directly to preempt them,
249 // and have bespoke ways to access the underlying libc functions.
250 # include <zircon/sanitizer.h>
251 # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
252 # define REAL(x) __unsanitized_##x
253 # define DECLARE_REAL(ret_type, func, ...)
254 #elif !SANITIZER_APPLE
255 # define PTR_TO_REAL(x) real_##x
256 # define REAL(x) __interception::PTR_TO_REAL(x)
257 # define FUNC_TYPE(x) x##_type
258 
259 # define DECLARE_REAL(ret_type, func, ...)            \
260     typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
261     namespace __interception {                        \
262     extern FUNC_TYPE(func) PTR_TO_REAL(func);         \
263     }
264 # define ASSIGN_REAL(dst, src) REAL(dst) = REAL(src)
265 #else  // SANITIZER_APPLE
266 # define REAL(x) x
267 # define DECLARE_REAL(ret_type, func, ...) \
268     extern "C" ret_type func(__VA_ARGS__);
269 # define ASSIGN_REAL(x, y)
270 #endif  // SANITIZER_APPLE
271 
272 #if !SANITIZER_FUCHSIA
273 # define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)  \
274     DECLARE_REAL(ret_type, func, __VA_ARGS__)               \
275     extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__);      \
276     extern "C" ret_type WRAP(func)(__VA_ARGS__);
277 // Declare an interceptor and its wrapper defined in a different translation
278 // unit (ex. asm).
279 # define DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(ret_type, func, ...)  \
280     extern "C" ret_type TRAMPOLINE(func)(__VA_ARGS__);                \
281     extern "C" ret_type WRAP(func)(__VA_ARGS__);                      \
282     extern "C" ret_type func(__VA_ARGS__);
283 #else
284 # define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...)
285 # define DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(ret_type, func, ...)
286 #endif
287 
288 // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
289 // macros does its job. In exceptional cases you may need to call REAL(foo)
290 // without defining INTERCEPTOR(..., foo, ...). For example, if you override
291 // foo with an interceptor for other function.
292 #if !SANITIZER_APPLE && !SANITIZER_FUCHSIA
293 #  define DEFINE_REAL(ret_type, func, ...)            \
294     typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
295     namespace __interception {                        \
296     FUNC_TYPE(func) PTR_TO_REAL(func);                \
297     }
298 #else
299 # define DEFINE_REAL(ret_type, func, ...)
300 #endif
301 
302 #if SANITIZER_FUCHSIA
303 
304 // We need to define the __interceptor_func name just to get
305 // sanitizer_common/scripts/gen_dynamic_list.py to export func.
306 // But we don't need to export __interceptor_func to get that.
307 #define INTERCEPTOR(ret_type, func, ...)                                \
308   extern "C"[[ gnu::alias(#func), gnu::visibility("hidden") ]] ret_type \
309       __interceptor_##func(__VA_ARGS__);                                \
310   extern "C" INTERCEPTOR_ATTRIBUTE ret_type func(__VA_ARGS__)
311 
312 #elif !SANITIZER_APPLE
313 
314 #define INTERCEPTOR(ret_type, func, ...)        \
315   DEFINE_REAL(ret_type, func, __VA_ARGS__)      \
316   DECLARE_WRAPPER(ret_type, func, __VA_ARGS__)  \
317   extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
318 
319 // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
320 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
321   INTERCEPTOR(ret_type, func, __VA_ARGS__)
322 
323 #else  // SANITIZER_APPLE
324 
325 #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...)  \
326   extern "C" ret_type func(__VA_ARGS__) suffix;       \
327   extern "C" ret_type WRAP(func)(__VA_ARGS__);        \
328   INTERPOSER(func);                                   \
329   extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
330 
331 #define INTERCEPTOR(ret_type, func, ...) \
332   INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
333 
334 #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
335   INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
336 
337 // Override |overridee| with |overrider|.
338 #define OVERRIDE_FUNCTION(overridee, overrider) \
339   INTERPOSER_2(overridee, WRAP(overrider))
340 #endif
341 
342 #if SANITIZER_WINDOWS
343 # define INTERCEPTOR_WINAPI(ret_type, func, ...)                \
344     typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
345     namespace __interception {                                  \
346       FUNC_TYPE(func) PTR_TO_REAL(func);                        \
347     }                                                           \
348     extern "C" INTERCEPTOR_ATTRIBUTE ret_type __stdcall WRAP(func)(__VA_ARGS__)
349 #endif
350 
351 // ISO C++ forbids casting between pointer-to-function and pointer-to-object,
352 // so we use casts via uintptr_t (the local __sanitizer::uptr equivalent).
353 namespace __interception {
354 
355 #if defined(__ELF__) && !SANITIZER_FUCHSIA
356 // The use of interceptors makes many sanitizers unusable for static linking.
357 // Define a function, if called, will cause a linker error (undefined _DYNAMIC).
358 // However, -static-pie (which is not common) cannot be detected at link time.
359 extern uptr kDynamic[] asm("_DYNAMIC");
360 inline void DoesNotSupportStaticLinking() {
361   [[maybe_unused]] volatile auto x = &kDynamic;
362 }
363 #else
364 inline void DoesNotSupportStaticLinking() {}
365 #endif
366 }  // namespace __interception
367 
368 #define INCLUDED_FROM_INTERCEPTION_LIB
369 
370 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
371     SANITIZER_SOLARIS
372 
373 # include "interception_linux.h"
374 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
375 # define INTERCEPT_FUNCTION_VER(func, symver) \
376     INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
377 #elif SANITIZER_APPLE
378 # include "interception_mac.h"
379 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
380 # define INTERCEPT_FUNCTION_VER(func, symver) \
381     INTERCEPT_FUNCTION_VER_MAC(func, symver)
382 #elif SANITIZER_WINDOWS
383 # include "interception_win.h"
384 # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
385 # define INTERCEPT_FUNCTION_VER(func, symver) \
386     INTERCEPT_FUNCTION_VER_WIN(func, symver)
387 #endif
388 
389 #undef INCLUDED_FROM_INTERCEPTION_LIB
390 
391 #endif  // INTERCEPTION_H
392