interception.h 17 KB

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