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