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