1 //===-- asan_dll_thunk.cc -------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // This file defines a family of thunks that should be statically linked into
13 // the DLLs that have ASan instrumentation in order to delegate the calls to the
14 // shared runtime that lives in the main binary.
15 // See https://code.google.com/p/address-sanitizer/issues/detail?id=209 for the
17 //===----------------------------------------------------------------------===//
19 // Only compile this code when buidling asan_dll_thunk.lib
20 // Using #ifdef rather than relying on Makefiles etc.
21 // simplifies the build procedure.
24 // ----------------- Helper functions and macros --------------------- {{{1
26 void *__stdcall
GetModuleHandleA(const char *module_name
);
27 void *__stdcall
GetProcAddress(void *module
, const char *proc_name
);
31 static void *getRealProcAddressOrDie(const char *name
) {
32 void *ret
= GetProcAddress(GetModuleHandleA(0), name
);
38 #define WRAP_V_V(name) \
39 extern "C" void name() { \
40 typedef void (*fntype)(); \
41 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
45 #define WRAP_V_W(name) \
46 extern "C" void name(void *arg) { \
47 typedef void (*fntype)(void *arg); \
48 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
52 #define WRAP_V_WW(name) \
53 extern "C" void name(void *arg1, void *arg2) { \
54 typedef void (*fntype)(void *, void *); \
55 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
59 #define WRAP_V_WWW(name) \
60 extern "C" void name(void *arg1, void *arg2, void *arg3) { \
61 typedef void *(*fntype)(void *, void *, void *); \
62 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
63 fn(arg1, arg2, arg3); \
66 #define WRAP_W_V(name) \
67 extern "C" void *name() { \
68 typedef void *(*fntype)(); \
69 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
73 #define WRAP_W_W(name) \
74 extern "C" void *name(void *arg) { \
75 typedef void *(*fntype)(void *arg); \
76 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
80 #define WRAP_W_WW(name) \
81 extern "C" void *name(void *arg1, void *arg2) { \
82 typedef void *(*fntype)(void *, void *); \
83 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
84 return fn(arg1, arg2); \
87 #define WRAP_W_WWW(name) \
88 extern "C" void *name(void *arg1, void *arg2, void *arg3) { \
89 typedef void *(*fntype)(void *, void *, void *); \
90 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
91 return fn(arg1, arg2, arg3); \
94 #define WRAP_W_WWWW(name) \
95 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) { \
96 typedef void *(*fntype)(void *, void *, void *, void *); \
97 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
98 return fn(arg1, arg2, arg3, arg4); \
101 #define WRAP_W_WWWWW(name) \
102 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
104 typedef void *(*fntype)(void *, void *, void *, void *, void *); \
105 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
106 return fn(arg1, arg2, arg3, arg4, arg5); \
109 #define WRAP_W_WWWWWW(name) \
110 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
111 void *arg5, void *arg6) { \
112 typedef void *(*fntype)(void *, void *, void *, void *, void *, void *); \
113 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
114 return fn(arg1, arg2, arg3, arg4, arg5, arg6); \
118 // ----------------- ASan own interface functions --------------------
119 WRAP_W_V(__asan_should_detect_stack_use_after_return
)
122 int __asan_option_detect_stack_use_after_return
;
124 // Manually wrap __asan_init as we need to initialize
125 // __asan_option_detect_stack_use_after_return afterwards.
126 void __asan_init_v3() {
127 typedef void (*fntype
)();
128 static fntype fn
= (fntype
)getRealProcAddressOrDie("__asan_init_v3");
130 __asan_option_detect_stack_use_after_return
=
131 (__asan_should_detect_stack_use_after_return() != 0);
135 WRAP_V_V(__asan_handle_no_return
)
137 WRAP_V_W(__asan_report_store1
)
138 WRAP_V_W(__asan_report_store2
)
139 WRAP_V_W(__asan_report_store4
)
140 WRAP_V_W(__asan_report_store8
)
141 WRAP_V_W(__asan_report_store16
)
142 WRAP_V_WW(__asan_report_store_n
)
144 WRAP_V_W(__asan_report_load1
)
145 WRAP_V_W(__asan_report_load2
)
146 WRAP_V_W(__asan_report_load4
)
147 WRAP_V_W(__asan_report_load8
)
148 WRAP_V_W(__asan_report_load16
)
149 WRAP_V_WW(__asan_report_load_n
)
151 WRAP_V_WW(__asan_register_globals
)
152 WRAP_V_WW(__asan_unregister_globals
)
154 WRAP_V_W(__asan_before_dynamic_init
)
155 WRAP_V_V(__asan_after_dynamic_init
)
157 WRAP_V_WW(__asan_poison_stack_memory
)
158 WRAP_V_WW(__asan_unpoison_stack_memory
)
160 WRAP_V_WW(__asan_poison_memory_region
)
161 WRAP_V_WW(__asan_unpoison_memory_region
)
163 WRAP_W_WW(__asan_stack_malloc_0
)
164 WRAP_W_WW(__asan_stack_malloc_1
)
165 WRAP_W_WW(__asan_stack_malloc_2
)
166 WRAP_W_WW(__asan_stack_malloc_3
)
167 WRAP_W_WW(__asan_stack_malloc_4
)
168 WRAP_W_WW(__asan_stack_malloc_5
)
169 WRAP_W_WW(__asan_stack_malloc_6
)
170 WRAP_W_WW(__asan_stack_malloc_7
)
171 WRAP_W_WW(__asan_stack_malloc_8
)
172 WRAP_W_WW(__asan_stack_malloc_9
)
173 WRAP_W_WW(__asan_stack_malloc_10
)
175 WRAP_V_WWW(__asan_stack_free_0
)
176 WRAP_V_WWW(__asan_stack_free_1
)
177 WRAP_V_WWW(__asan_stack_free_2
)
178 WRAP_V_WWW(__asan_stack_free_4
)
179 WRAP_V_WWW(__asan_stack_free_5
)
180 WRAP_V_WWW(__asan_stack_free_6
)
181 WRAP_V_WWW(__asan_stack_free_7
)
182 WRAP_V_WWW(__asan_stack_free_8
)
183 WRAP_V_WWW(__asan_stack_free_9
)
184 WRAP_V_WWW(__asan_stack_free_10
)
186 // TODO(timurrrr): Add more interface functions on the as-needed basis.
188 // ----------------- Memory allocation functions ---------------------
193 WRAP_W_WWWW(_malloc_dbg
)
196 WRAP_W_WWWWW(_calloc_dbg
)
197 WRAP_W_WWW(_calloc_impl
)
200 WRAP_W_WWW(_realloc_dbg
)
201 WRAP_W_WWW(_recalloc
)
205 // TODO(timurrrr): Do we need to add _Crt* stuff here? (see asan_malloc_win.cc).
207 #endif // ASAN_DLL_THUNK