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