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