interception_win.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #if !SANITIZER_WINDOWS64
  36. // Exposed for unittests
  37. bool OverrideFunctionWithDetour(
  38. uptr old_func, uptr new_func, uptr *orig_old_func);
  39. #endif
  40. // Exposed for unittests
  41. bool OverrideFunctionWithRedirectJump(
  42. uptr old_func, uptr new_func, uptr *orig_old_func);
  43. bool OverrideFunctionWithHotPatch(
  44. uptr old_func, uptr new_func, uptr *orig_old_func);
  45. bool OverrideFunctionWithTrampoline(
  46. uptr old_func, uptr new_func, uptr *orig_old_func);
  47. // Exposed for unittests
  48. void TestOnlyReleaseTrampolineRegions();
  49. } // namespace __interception
  50. #if defined(INTERCEPTION_DYNAMIC_CRT)
  51. #define INTERCEPT_FUNCTION_WIN(func) \
  52. ::__interception::OverrideFunction(#func, \
  53. (::__interception::uptr)WRAP(func), \
  54. (::__interception::uptr *)&REAL(func))
  55. #else
  56. #define INTERCEPT_FUNCTION_WIN(func) \
  57. ::__interception::OverrideFunction((::__interception::uptr)func, \
  58. (::__interception::uptr)WRAP(func), \
  59. (::__interception::uptr *)&REAL(func))
  60. #endif
  61. #define INTERCEPT_FUNCTION_VER_WIN(func, symver) INTERCEPT_FUNCTION_WIN(func)
  62. #define INTERCEPT_FUNCTION_DLLIMPORT(user_dll, provider_dll, func) \
  63. ::__interception::OverrideImportedFunction( \
  64. user_dll, provider_dll, #func, (::__interception::uptr)WRAP(func), \
  65. (::__interception::uptr *)&REAL(func))
  66. #endif // INTERCEPTION_WIN_H
  67. #endif // SANITIZER_WINDOWS