interception_win.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===-- interception_linux.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. // Windows-specific interception methods.
  12. //===----------------------------------------------------------------------===//
  13. #if SANITIZER_WINDOWS
  14. #if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
  15. # error "interception_win.h should be included from interception library only"
  16. #endif
  17. #ifndef INTERCEPTION_WIN_H
  18. #define INTERCEPTION_WIN_H
  19. namespace __interception {
  20. // All the functions in the OverrideFunction() family return true on success,
  21. // false on failure (including "couldn't find the function").
  22. // Overrides a function by its address.
  23. bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func = 0);
  24. // Overrides a function in a system DLL or DLL CRT by its exported name.
  25. bool OverrideFunction(const char *name, uptr new_func, uptr *orig_old_func = 0);
  26. // Windows-only replacement for GetProcAddress. Useful for some sanitizers.
  27. uptr InternalGetProcAddress(void *module, const char *func_name);
  28. // Overrides a function only when it is called from a specific DLL. For example,
  29. // this is used to override calls to HeapAlloc/HeapFree from ucrtbase without
  30. // affecting other third party libraries.
  31. bool OverrideImportedFunction(const char *module_to_patch,
  32. const char *imported_module,
  33. const char *function_name, uptr new_function,
  34. uptr *orig_old_func);
  35. // Sets a callback to be used for reporting errors by interception_win. The
  36. // callback will be called with printf-like arguments. Intended to be used with
  37. // __sanitizer::Report. Pass nullptr to disable error reporting (default).
  38. void SetErrorReportCallback(void (*callback)(const char *format, ...));
  39. #if !SANITIZER_WINDOWS64
  40. // Exposed for unittests
  41. bool OverrideFunctionWithDetour(
  42. uptr old_func, uptr new_func, uptr *orig_old_func);
  43. #endif
  44. // Exposed for unittests
  45. bool OverrideFunctionWithRedirectJump(
  46. uptr old_func, uptr new_func, uptr *orig_old_func);
  47. bool OverrideFunctionWithHotPatch(
  48. uptr old_func, uptr new_func, uptr *orig_old_func);
  49. bool OverrideFunctionWithTrampoline(
  50. uptr old_func, uptr new_func, uptr *orig_old_func);
  51. // Exposed for unittests
  52. void TestOnlyReleaseTrampolineRegions();
  53. } // namespace __interception
  54. #if defined(INTERCEPTION_DYNAMIC_CRT)
  55. #define INTERCEPT_FUNCTION_WIN(func) \
  56. ::__interception::OverrideFunction(#func, \
  57. (::__interception::uptr)WRAP(func), \
  58. (::__interception::uptr *)&REAL(func))
  59. #else
  60. #define INTERCEPT_FUNCTION_WIN(func) \
  61. ::__interception::OverrideFunction((::__interception::uptr)func, \
  62. (::__interception::uptr)WRAP(func), \
  63. (::__interception::uptr *)&REAL(func))
  64. #endif
  65. #define INTERCEPT_FUNCTION_VER_WIN(func, symver) INTERCEPT_FUNCTION_WIN(func)
  66. #define INTERCEPT_FUNCTION_DLLIMPORT(user_dll, provider_dll, func) \
  67. ::__interception::OverrideImportedFunction( \
  68. user_dll, provider_dll, #func, (::__interception::uptr)WRAP(func), \
  69. (::__interception::uptr *)&REAL(func))
  70. #endif // INTERCEPTION_WIN_H
  71. #endif // SANITIZER_WINDOWS