interception.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //===-- interception.h ------------------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is a part of AddressSanitizer, an address sanity checker.
  9. //
  10. // Machinery for providing replacements/wrappers for system functions.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef INTERCEPTION_H
  13. #define INTERCEPTION_H
  14. #if !defined(__linux__) && !defined(__FreeBSD__) && \
  15. !defined(__APPLE__) && !defined(_WIN32)
  16. # error "Interception doesn't work on this operating system."
  17. #endif
  18. #include "sanitizer_common/sanitizer_internal_defs.h"
  19. // These typedefs should be used only in the interceptor definitions to replace
  20. // the standard system types (e.g. SSIZE_T instead of ssize_t)
  21. typedef __sanitizer::uptr SIZE_T;
  22. typedef __sanitizer::sptr SSIZE_T;
  23. typedef __sanitizer::sptr PTRDIFF_T;
  24. typedef __sanitizer::s64 INTMAX_T;
  25. typedef __sanitizer::OFF_T OFF_T;
  26. typedef __sanitizer::OFF64_T OFF64_T;
  27. // How to add an interceptor:
  28. // Suppose you need to wrap/replace system function (generally, from libc):
  29. // int foo(const char *bar, double baz);
  30. // You'll need to:
  31. // 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
  32. // your source file. See the notes below for cases when
  33. // INTERCEPTOR_WITH_SUFFIX(...) should be used instead.
  34. // 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
  35. // INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
  36. // intercepted successfully.
  37. // You can access original function by calling REAL(foo)(bar, baz).
  38. // By default, REAL(foo) will be visible only inside your interceptor, and if
  39. // you want to use it in other parts of RTL, you'll need to:
  40. // 3a) add DECLARE_REAL(int, foo, const char*, double) to a
  41. // header file.
  42. // However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
  43. // INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
  44. // 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
  45. // to a header file.
  46. // Notes: 1. Things may not work properly if macro INTERCEPTOR(...) {...} or
  47. // DECLARE_REAL(...) are located inside namespaces.
  48. // 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo)" to
  49. // effectively redirect calls from "foo" to "zoo". In this case
  50. // you aren't required to implement
  51. // INTERCEPTOR(int, foo, const char *bar, double baz) {...}
  52. // but instead you'll have to add
  53. // DECLARE_REAL(int, foo, const char *bar, double baz) in your
  54. // source file (to define a pointer to overriden function).
  55. // 3. Some Mac functions have symbol variants discriminated by
  56. // additional suffixes, e.g. _$UNIX2003 (see
  57. // https://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/index.html
  58. // for more details). To intercept such functions you need to use the
  59. // INTERCEPTOR_WITH_SUFFIX(...) macro.
  60. // How it works:
  61. // To replace system functions on Linux we just need to declare functions
  62. // with same names in our library and then obtain the real function pointers
  63. // using dlsym().
  64. // There is one complication. A user may also intercept some of the functions
  65. // we intercept. To resolve this we declare our interceptors with __interceptor_
  66. // prefix, and then make actual interceptors weak aliases to __interceptor_
  67. // functions.
  68. //
  69. // This is not so on Mac OS, where the two-level namespace makes
  70. // our replacement functions invisible to other libraries. This may be overcomed
  71. // using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
  72. // libraries in Chromium were noticed when doing so.
  73. // Instead we create a dylib containing a __DATA,__interpose section that
  74. // associates library functions with their wrappers. When this dylib is
  75. // preloaded before an executable using DYLD_INSERT_LIBRARIES, it routes all
  76. // the calls to interposed functions done through stubs to the wrapper
  77. // functions.
  78. // As it's decided at compile time which functions are to be intercepted on Mac,
  79. // INTERCEPT_FUNCTION() is effectively a no-op on this system.
  80. #if defined(__APPLE__)
  81. #include <sys/cdefs.h> // For __DARWIN_ALIAS_C().
  82. // Just a pair of pointers.
  83. struct interpose_substitution {
  84. const uptr replacement;
  85. const uptr original;
  86. };
  87. // For a function foo() create a global pair of pointers { wrap_foo, foo } in
  88. // the __DATA,__interpose section.
  89. // As a result all the calls to foo() will be routed to wrap_foo() at runtime.
  90. #define INTERPOSER(func_name) __attribute__((used)) \
  91. const interpose_substitution substitution_##func_name[] \
  92. __attribute__((section("__DATA, __interpose"))) = { \
  93. { reinterpret_cast<const uptr>(WRAP(func_name)), \
  94. reinterpret_cast<const uptr>(func_name) } \
  95. }
  96. // For a function foo() and a wrapper function bar() create a global pair
  97. // of pointers { bar, foo } in the __DATA,__interpose section.
  98. // As a result all the calls to foo() will be routed to bar() at runtime.
  99. #define INTERPOSER_2(func_name, wrapper_name) __attribute__((used)) \
  100. const interpose_substitution substitution_##func_name[] \
  101. __attribute__((section("__DATA, __interpose"))) = { \
  102. { reinterpret_cast<const uptr>(wrapper_name), \
  103. reinterpret_cast<const uptr>(func_name) } \
  104. }
  105. # define WRAP(x) wrap_##x
  106. # define WRAPPER_NAME(x) "wrap_"#x
  107. # define INTERCEPTOR_ATTRIBUTE
  108. # define DECLARE_WRAPPER(ret_type, func, ...)
  109. #elif defined(_WIN32)
  110. # define WRAP(x) __asan_wrap_##x
  111. # define WRAPPER_NAME(x) "__asan_wrap_"#x
  112. # define INTERCEPTOR_ATTRIBUTE __declspec(dllexport)
  113. # define DECLARE_WRAPPER(ret_type, func, ...) \
  114. extern "C" ret_type func(__VA_ARGS__);
  115. # define DECLARE_WRAPPER_WINAPI(ret_type, func, ...) \
  116. extern "C" __declspec(dllimport) ret_type __stdcall func(__VA_ARGS__);
  117. #elif defined(__FreeBSD__)
  118. # define WRAP(x) __interceptor_ ## x
  119. # define WRAPPER_NAME(x) "__interceptor_" #x
  120. # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
  121. // FreeBSD's dynamic linker (incompliantly) gives non-weak symbols higher
  122. // priority than weak ones so weak aliases won't work for indirect calls
  123. // in position-independent (-fPIC / -fPIE) mode.
  124. # define DECLARE_WRAPPER(ret_type, func, ...) \
  125. extern "C" ret_type func(__VA_ARGS__) \
  126. __attribute__((alias("__interceptor_" #func), visibility("default")));
  127. #else
  128. # define WRAP(x) __interceptor_ ## x
  129. # define WRAPPER_NAME(x) "__interceptor_" #x
  130. # define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
  131. # define DECLARE_WRAPPER(ret_type, func, ...) \
  132. extern "C" ret_type func(__VA_ARGS__) \
  133. __attribute__((weak, alias("__interceptor_" #func), visibility("default")));
  134. #endif
  135. #if !defined(__APPLE__)
  136. # define PTR_TO_REAL(x) real_##x
  137. # define REAL(x) __interception::PTR_TO_REAL(x)
  138. # define FUNC_TYPE(x) x##_f
  139. # define DECLARE_REAL(ret_type, func, ...) \
  140. typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
  141. namespace __interception { \
  142. extern FUNC_TYPE(func) PTR_TO_REAL(func); \
  143. }
  144. #else // __APPLE__
  145. # define REAL(x) x
  146. # define DECLARE_REAL(ret_type, func, ...) \
  147. extern "C" ret_type func(__VA_ARGS__);
  148. #endif // __APPLE__
  149. #define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
  150. DECLARE_REAL(ret_type, func, __VA_ARGS__) \
  151. extern "C" ret_type WRAP(func)(__VA_ARGS__);
  152. // Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
  153. // macros does its job. In exceptional cases you may need to call REAL(foo)
  154. // without defining INTERCEPTOR(..., foo, ...). For example, if you override
  155. // foo with an interceptor for other function.
  156. #if !defined(__APPLE__)
  157. # define DEFINE_REAL(ret_type, func, ...) \
  158. typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
  159. namespace __interception { \
  160. FUNC_TYPE(func) PTR_TO_REAL(func); \
  161. }
  162. #else
  163. # define DEFINE_REAL(ret_type, func, ...)
  164. #endif
  165. #if !defined(__APPLE__)
  166. #define INTERCEPTOR(ret_type, func, ...) \
  167. DEFINE_REAL(ret_type, func, __VA_ARGS__) \
  168. DECLARE_WRAPPER(ret_type, func, __VA_ARGS__) \
  169. extern "C" \
  170. INTERCEPTOR_ATTRIBUTE \
  171. ret_type WRAP(func)(__VA_ARGS__)
  172. // We don't need INTERCEPTOR_WITH_SUFFIX on non-Darwin for now.
  173. #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
  174. INTERCEPTOR(ret_type, func, __VA_ARGS__)
  175. #else // __APPLE__
  176. #define INTERCEPTOR_ZZZ(suffix, ret_type, func, ...) \
  177. extern "C" ret_type func(__VA_ARGS__) suffix; \
  178. extern "C" ret_type WRAP(func)(__VA_ARGS__); \
  179. INTERPOSER(func); \
  180. extern "C" INTERCEPTOR_ATTRIBUTE ret_type WRAP(func)(__VA_ARGS__)
  181. #define INTERCEPTOR(ret_type, func, ...) \
  182. INTERCEPTOR_ZZZ(/*no symbol variants*/, ret_type, func, __VA_ARGS__)
  183. #define INTERCEPTOR_WITH_SUFFIX(ret_type, func, ...) \
  184. INTERCEPTOR_ZZZ(__DARWIN_ALIAS_C(func), ret_type, func, __VA_ARGS__)
  185. // Override |overridee| with |overrider|.
  186. #define OVERRIDE_FUNCTION(overridee, overrider) \
  187. INTERPOSER_2(overridee, WRAP(overrider))
  188. #endif
  189. #if defined(_WIN32)
  190. # define INTERCEPTOR_WINAPI(ret_type, func, ...) \
  191. typedef ret_type (__stdcall *FUNC_TYPE(func))(__VA_ARGS__); \
  192. namespace __interception { \
  193. FUNC_TYPE(func) PTR_TO_REAL(func); \
  194. } \
  195. DECLARE_WRAPPER_WINAPI(ret_type, func, __VA_ARGS__) \
  196. extern "C" \
  197. INTERCEPTOR_ATTRIBUTE \
  198. ret_type __stdcall WRAP(func)(__VA_ARGS__)
  199. #endif
  200. // ISO C++ forbids casting between pointer-to-function and pointer-to-object,
  201. // so we use casting via an integral type __interception::uptr,
  202. // assuming that system is POSIX-compliant. Using other hacks seem
  203. // challenging, as we don't even pass function type to
  204. // INTERCEPT_FUNCTION macro, only its name.
  205. namespace __interception {
  206. #if defined(_WIN64)
  207. typedef unsigned long long uptr; // NOLINT
  208. #else
  209. typedef unsigned long uptr; // NOLINT
  210. #endif // _WIN64
  211. } // namespace __interception
  212. #define INCLUDED_FROM_INTERCEPTION_LIB
  213. #if defined(__linux__) || defined(__FreeBSD__)
  214. # include "interception_linux.h"
  215. # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func)
  216. # define INTERCEPT_FUNCTION_VER(func, symver) \
  217. INTERCEPT_FUNCTION_VER_LINUX_OR_FREEBSD(func, symver)
  218. #elif defined(__APPLE__)
  219. # include "interception_mac.h"
  220. # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
  221. # define INTERCEPT_FUNCTION_VER(func, symver) \
  222. INTERCEPT_FUNCTION_VER_MAC(func, symver)
  223. #else // defined(_WIN32)
  224. # include "interception_win.h"
  225. # define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
  226. # define INTERCEPT_FUNCTION_VER(func, symver) \
  227. INTERCEPT_FUNCTION_VER_WIN(func, symver)
  228. #endif
  229. #undef INCLUDED_FROM_INTERCEPTION_LIB
  230. #endif // INTERCEPTION_H