blob: 9061f9ed4c2152c447970abf60624a15552797fb [file] [log] [blame]
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +00001//===-- interception_linux.h ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Windows-specific interception methods.
13//===----------------------------------------------------------------------===//
14
15#ifdef _WIN32
16
17#if !defined(INCLUDED_FROM_INTERCEPTION_LIB)
18# error "interception_win.h should be included from interception library only"
19#endif
20
21#ifndef INTERCEPTION_WIN_H
22#define INTERCEPTION_WIN_H
23
24namespace __interception {
Timur Iskhodzhanov0a88b252014-08-25 13:19:05 +000025// All the functions in the OverrideFunction() family return true on success,
26// false on failure (including "couldn't find the function").
Timur Iskhodzhanov2f48b872012-03-12 11:45:09 +000027
Timur Iskhodzhanov0a88b252014-08-25 13:19:05 +000028// Overrides a function by its address.
29bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func = 0);
30
31// Overrides a function in a system DLL or DLL CRT by its exported name.
32bool OverrideFunction(const char *name, uptr new_func, uptr *orig_old_func = 0);
Reid Klecknerd85f7012015-08-18 22:38:27 +000033
34// Windows-only replacement for GetProcAddress. Useful for some sanitizers.
35uptr InternalGetProcAddress(void *module, const char *func_name);
36
Reid Kleckner3b029052016-03-24 20:19:48 +000037// Overrides a function only when it is called from a specific DLL. For example,
38// this is used to override calls to HeapAlloc/HeapFree from ucrtbase without
39// affecting other third party libraries.
40bool OverrideImportedFunction(const char *module_to_patch,
41 const char *imported_module,
42 const char *function_name, uptr new_function,
43 uptr *orig_old_func);
44
Etienne Bergeronf5525672016-07-11 23:02:18 +000045#if !SANITIZER_WINDOWS64
46// Exposed for unittests
47bool OverrideFunctionWithDetour(
48 uptr old_func, uptr new_func, uptr *orig_old_func);
49#endif
50
51// Exposed for unittests
52bool OverrideFunctionWithRedirectJump(
53 uptr old_func, uptr new_func, uptr *orig_old_func);
54bool OverrideFunctionWithHotPatch(
55 uptr old_func, uptr new_func, uptr *orig_old_func);
56bool OverrideFunctionWithTrampoline(
57 uptr old_func, uptr new_func, uptr *orig_old_func);
58
59// Exposed for unittests
60void TestOnlyReleaseTrampolineRegions();
61
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +000062} // namespace __interception
63
Timur Iskhodzhanov0a88b252014-08-25 13:19:05 +000064#if defined(INTERCEPTION_DYNAMIC_CRT)
65#define INTERCEPT_FUNCTION_WIN(func) \
66 ::__interception::OverrideFunction(#func, \
67 (::__interception::uptr)WRAP(func), \
68 (::__interception::uptr *)&REAL(func))
Timur Iskhodzhanov2f48b872012-03-12 11:45:09 +000069#else
Timur Iskhodzhanov0a88b252014-08-25 13:19:05 +000070#define INTERCEPT_FUNCTION_WIN(func) \
71 ::__interception::OverrideFunction((::__interception::uptr)func, \
72 (::__interception::uptr)WRAP(func), \
73 (::__interception::uptr *)&REAL(func))
Timur Iskhodzhanov2f48b872012-03-12 11:45:09 +000074#endif
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +000075
Timur Iskhodzhanov0a88b252014-08-25 13:19:05 +000076#define INTERCEPT_FUNCTION_VER_WIN(func, symver) INTERCEPT_FUNCTION_WIN(func)
Alexey Samsonovedecc382013-10-16 08:20:31 +000077
Reid Kleckner3b029052016-03-24 20:19:48 +000078#define INTERCEPT_FUNCTION_DLLIMPORT(user_dll, provider_dll, func) \
79 ::__interception::OverrideImportedFunction( \
80 user_dll, provider_dll, #func, (::__interception::uptr)WRAP(func), \
81 (::__interception::uptr *)&REAL(func))
82
Timur Iskhodzhanov36d297d2012-02-22 13:59:49 +000083#endif // INTERCEPTION_WIN_H
84#endif // _WIN32