xref: /freebsd/contrib/llvm-project/compiler-rt/lib/interception/interception_win.h (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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 #if !SANITIZER_WINDOWS64
45 // Exposed for unittests
46 bool OverrideFunctionWithDetour(
47     uptr old_func, uptr new_func, uptr *orig_old_func);
48 #endif
49 
50 // Exposed for unittests
51 bool OverrideFunctionWithRedirectJump(
52     uptr old_func, uptr new_func, uptr *orig_old_func);
53 bool OverrideFunctionWithHotPatch(
54     uptr old_func, uptr new_func, uptr *orig_old_func);
55 bool OverrideFunctionWithTrampoline(
56     uptr old_func, uptr new_func, uptr *orig_old_func);
57 
58 // Exposed for unittests
59 void TestOnlyReleaseTrampolineRegions();
60 
61 }  // namespace __interception
62 
63 #if defined(INTERCEPTION_DYNAMIC_CRT)
64 #define INTERCEPT_FUNCTION_WIN(func)                                           \
65   ::__interception::OverrideFunction(#func,                                    \
66                                      (::__interception::uptr)WRAP(func),       \
67                                      (::__interception::uptr *)&REAL(func))
68 #else
69 #define INTERCEPT_FUNCTION_WIN(func)                                           \
70   ::__interception::OverrideFunction((::__interception::uptr)func,             \
71                                      (::__interception::uptr)WRAP(func),       \
72                                      (::__interception::uptr *)&REAL(func))
73 #endif
74 
75 #define INTERCEPT_FUNCTION_VER_WIN(func, symver) INTERCEPT_FUNCTION_WIN(func)
76 
77 #define INTERCEPT_FUNCTION_DLLIMPORT(user_dll, provider_dll, func)       \
78   ::__interception::OverrideImportedFunction(                            \
79       user_dll, provider_dll, #func, (::__interception::uptr)WRAP(func), \
80       (::__interception::uptr *)&REAL(func))
81 
82 #endif  // INTERCEPTION_WIN_H
83 #endif  // SANITIZER_WINDOWS
84