1 //===-- sanitizer_win_dll_thunk.h -----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This header provide helper macros to delegate calls to the shared runtime
10 // that lives in the main executable. It should be included to dll_thunks that
11 // will be linked to the dlls, when the sanitizer is a static library included
12 // in the main executable.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_WIN_DLL_THUNK_H
15 #define SANITIZER_WIN_DLL_THUNK_H
16 #include "sanitizer_internal_defs.h"
18 namespace __sanitizer {
19 uptr dllThunkGetRealAddrOrDie(const char *name);
21 int dllThunkIntercept(const char* main_function, uptr dll_function);
23 int dllThunkInterceptWhenPossible(const char* main_function,
24 const char* default_function, uptr dll_function);
27 extern "C" int __dll_thunk_init();
29 // ----------------- Function interception helper macros -------------------- //
30 // Override dll_function with main_function from main executable.
31 #define INTERCEPT_OR_DIE(main_function, dll_function) \
32 static int intercept_##dll_function() { \
33 return __sanitizer::dllThunkIntercept(main_function, (__sanitizer::uptr) \
36 __pragma(section(".DLLTH$M", long, read)) \
37 __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \
38 intercept_##dll_function;
40 // Try to override dll_function with main_function from main executable.
41 // If main_function is not present, override dll_function with default_function.
42 #define INTERCEPT_WHEN_POSSIBLE(main_function, default_function, dll_function) \
43 static int intercept_##dll_function() { \
44 return __sanitizer::dllThunkInterceptWhenPossible(main_function, \
45 default_function, (__sanitizer::uptr)dll_function); \
47 __pragma(section(".DLLTH$M", long, read)) \
48 __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \
49 intercept_##dll_function;
51 // -------------------- Function interception macros ------------------------ //
52 // Special case of hooks -- ASan own interface functions. Those are only called
53 // after __asan_init, thus an empty implementation is sufficient.
54 #define INTERCEPT_SANITIZER_FUNCTION(name) \
55 extern "C" __declspec(noinline) void name() { \
56 volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__; \
57 static const char function_name[] = #name; \
58 for (const char* ptr = &function_name[0]; *ptr; ++ptr) \
59 prevent_icf ^= *ptr; \
63 INTERCEPT_OR_DIE(#name, name)
65 // Special case of hooks -- Weak functions, could be redefined in the main
66 // executable, but that is not necessary, so we shouldn't die if we can not find
67 // a reference. Instead, when the function is not present in the main executable
68 // we consider the default impl provided by asan library.
69 #define INTERCEPT_SANITIZER_WEAK_FUNCTION(name) \
70 extern "C" __declspec(noinline) void name() { \
71 volatile int prevent_icf = (__LINE__ << 8) ^ __COUNTER__; \
72 static const char function_name[] = #name; \
73 for (const char* ptr = &function_name[0]; *ptr; ++ptr) \
74 prevent_icf ^= *ptr; \
78 INTERCEPT_WHEN_POSSIBLE(#name, STRINGIFY(WEAK_EXPORT_NAME(name)), name)
80 // We can't define our own version of strlen etc. because that would lead to
81 // link-time or even type mismatch errors. Instead, we can declare a function
82 // just to be able to get its address. Me may miss the first few calls to the
83 // functions since it can be called before __dll_thunk_init, but that would lead
84 // to false negatives in the startup code before user's global initializers,
85 // which isn't a big deal.
86 #define INTERCEPT_LIBRARY_FUNCTION(name) \
87 extern "C" void name(); \
88 INTERCEPT_OR_DIE(WRAPPER_NAME(name), name)
90 // Use these macros for functions that could be called before __dll_thunk_init()
91 // is executed and don't lead to errors if defined (free, malloc, etc).
92 #define INTERCEPT_WRAP_V_V(name) \
93 extern "C" void name() { \
94 typedef decltype(name) *fntype; \
95 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
98 INTERCEPT_OR_DIE(#name, name);
100 #define INTERCEPT_WRAP_V_W(name) \
101 extern "C" void name(void *arg) { \
102 typedef decltype(name) *fntype; \
103 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
106 INTERCEPT_OR_DIE(#name, name);
108 #define INTERCEPT_WRAP_V_WW(name) \
109 extern "C" void name(void *arg1, void *arg2) { \
110 typedef decltype(name) *fntype; \
111 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
114 INTERCEPT_OR_DIE(#name, name);
116 #define INTERCEPT_WRAP_V_WWW(name) \
117 extern "C" void name(void *arg1, void *arg2, void *arg3) { \
118 typedef decltype(name) *fntype; \
119 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
120 fn(arg1, arg2, arg3); \
122 INTERCEPT_OR_DIE(#name, name);
124 #define INTERCEPT_WRAP_W_V(name) \
125 extern "C" void *name() { \
126 typedef decltype(name) *fntype; \
127 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
130 INTERCEPT_OR_DIE(#name, name);
132 #define INTERCEPT_WRAP_W_W(name) \
133 extern "C" void *name(void *arg) { \
134 typedef decltype(name) *fntype; \
135 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
138 INTERCEPT_OR_DIE(#name, name);
140 #define INTERCEPT_WRAP_W_WW(name) \
141 extern "C" void *name(void *arg1, void *arg2) { \
142 typedef decltype(name) *fntype; \
143 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
144 return fn(arg1, arg2); \
146 INTERCEPT_OR_DIE(#name, name);
148 #define INTERCEPT_WRAP_W_WWW(name) \
149 extern "C" void *name(void *arg1, void *arg2, void *arg3) { \
150 typedef decltype(name) *fntype; \
151 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
152 return fn(arg1, arg2, arg3); \
154 INTERCEPT_OR_DIE(#name, name);
156 #define INTERCEPT_WRAP_W_WWWW(name) \
157 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) { \
158 typedef decltype(name) *fntype; \
159 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
160 return fn(arg1, arg2, arg3, arg4); \
162 INTERCEPT_OR_DIE(#name, name);
164 #define INTERCEPT_WRAP_W_WWWWW(name) \
165 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
167 typedef decltype(name) *fntype; \
168 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
169 return fn(arg1, arg2, arg3, arg4, arg5); \
171 INTERCEPT_OR_DIE(#name, name);
173 #define INTERCEPT_WRAP_W_WWWWWW(name) \
174 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
175 void *arg5, void *arg6) { \
176 typedef decltype(name) *fntype; \
177 static fntype fn = (fntype)__sanitizer::dllThunkGetRealAddrOrDie(#name); \
178 return fn(arg1, arg2, arg3, arg4, arg5, arg6); \
180 INTERCEPT_OR_DIE(#name, name);
182 #endif // SANITIZER_WIN_DLL_THUNK_H