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 
14 #if SANITIZER_WINDOWS
15 
16 #if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
17 # error "interception_win.h should be included from interception library only"
18 #endif
19 
20 #ifndef INTERCEPTION_WIN_H
21 #define INTERCEPTION_WIN_H
22 
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").
26 
27 // Overrides a function by its address.
28 bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func = 0);
29 
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);
32 
33 // Windows-only replacement for GetProcAddress. Useful for some sanitizers.
34 uptr InternalGetProcAddress(void *module, const char *func_name);
35 
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,
42                               uptr *orig_old_func);
43 
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, ...));
48 
49 #if !SANITIZER_WINDOWS64
50 // Exposed for unittests
51 bool OverrideFunctionWithDetour(
52     uptr old_func, uptr new_func, uptr *orig_old_func);
53 #endif
54 
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);
62 
63 // Exposed for unittests
64 void TestOnlyReleaseTrampolineRegions();
65 
66 }  // namespace __interception
67 
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))
73 #else
74 #define INTERCEPT_FUNCTION_WIN(func)                                           \
75   ::__interception::OverrideFunction((::__interception::uptr)func,             \
76                                      (::__interception::uptr)WRAP(func),       \
77                                      (::__interception::uptr *)&REAL(func))
78 #endif
79 
80 #define INTERCEPT_FUNCTION_VER_WIN(func, symver) INTERCEPT_FUNCTION_WIN(func)
81 
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))
86 
87 #endif  // INTERCEPTION_WIN_H
88 #endif  // SANITIZER_WINDOWS
89