PR c/68320
[official-gcc.git] / libsanitizer / asan / asan_win_dll_thunk.cc
blob19d73f9f89e3ae9d696d66a1afb1e5fa8a6b4635
1 //===-- asan_win_dll_thunk.cc ---------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // This file defines a family of thunks that should be statically linked into
11 // the DLLs that have ASan instrumentation in order to delegate the calls to the
12 // shared runtime that lives in the main binary.
13 // See https://code.google.com/p/address-sanitizer/issues/detail?id=209 for the
14 // details.
15 //===----------------------------------------------------------------------===//
17 // Only compile this code when buidling asan_dll_thunk.lib
18 // Using #ifdef rather than relying on Makefiles etc.
19 // simplifies the build procedure.
20 #ifdef ASAN_DLL_THUNK
21 #include "asan_init_version.h"
22 #include "interception/interception.h"
24 // ---------- Function interception helper functions and macros ----------- {{{1
25 extern "C" {
26 void *__stdcall GetModuleHandleA(const char *module_name);
27 void *__stdcall GetProcAddress(void *module, const char *proc_name);
28 void abort();
31 static uptr getRealProcAddressOrDie(const char *name) {
32 uptr ret =
33 __interception::InternalGetProcAddress((void *)GetModuleHandleA(0), name);
34 if (!ret)
35 abort();
36 return ret;
39 // We need to intercept some functions (e.g. ASan interface, memory allocator --
40 // let's call them "hooks") exported by the DLL thunk and forward the hooks to
41 // the runtime in the main module.
42 // However, we don't want to keep two lists of these hooks.
43 // To avoid that, the list of hooks should be defined using the
44 // INTERCEPT_WHEN_POSSIBLE macro. Then, all these hooks can be intercepted
45 // at once by calling INTERCEPT_HOOKS().
47 // Use macro+template magic to automatically generate the list of hooks.
48 // Each hook at line LINE defines a template class with a static
49 // FunctionInterceptor<LINE>::Execute() method intercepting the hook.
50 // The default implementation of FunctionInterceptor<LINE> is to call
51 // the Execute() method corresponding to the previous line.
52 template<int LINE>
53 struct FunctionInterceptor {
54 static void Execute() { FunctionInterceptor<LINE-1>::Execute(); }
57 // There shouldn't be any hooks with negative definition line number.
58 template<>
59 struct FunctionInterceptor<0> {
60 static void Execute() {}
63 #define INTERCEPT_WHEN_POSSIBLE(main_function, dll_function) \
64 template <> struct FunctionInterceptor<__LINE__> { \
65 static void Execute() { \
66 uptr wrapper = getRealProcAddressOrDie(main_function); \
67 if (!__interception::OverrideFunction((uptr)dll_function, wrapper, 0)) \
68 abort(); \
69 FunctionInterceptor<__LINE__ - 1>::Execute(); \
70 } \
73 // Special case of hooks -- ASan own interface functions. Those are only called
74 // after __asan_init, thus an empty implementation is sufficient.
75 #define INTERFACE_FUNCTION(name) \
76 extern "C" __declspec(noinline) void name() { \
77 volatile int prevent_icf = (__LINE__ << 8); (void)prevent_icf; \
78 __debugbreak(); \
79 } \
80 INTERCEPT_WHEN_POSSIBLE(#name, name)
82 // INTERCEPT_HOOKS must be used after the last INTERCEPT_WHEN_POSSIBLE.
83 #define INTERCEPT_HOOKS FunctionInterceptor<__LINE__>::Execute
85 // We can't define our own version of strlen etc. because that would lead to
86 // link-time or even type mismatch errors. Instead, we can declare a function
87 // just to be able to get its address. Me may miss the first few calls to the
88 // functions since it can be called before __asan_init, but that would lead to
89 // false negatives in the startup code before user's global initializers, which
90 // isn't a big deal.
91 #define INTERCEPT_LIBRARY_FUNCTION(name) \
92 extern "C" void name(); \
93 INTERCEPT_WHEN_POSSIBLE(WRAPPER_NAME(name), name)
95 // Disable compiler warnings that show up if we declare our own version
96 // of a compiler intrinsic (e.g. strlen).
97 #pragma warning(disable: 4391)
98 #pragma warning(disable: 4392)
100 static void InterceptHooks();
101 // }}}
103 // ---------- Function wrapping helpers ----------------------------------- {{{1
104 #define WRAP_V_V(name) \
105 extern "C" void name() { \
106 typedef void (*fntype)(); \
107 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
108 fn(); \
110 INTERCEPT_WHEN_POSSIBLE(#name, name);
112 #define WRAP_V_W(name) \
113 extern "C" void name(void *arg) { \
114 typedef void (*fntype)(void *arg); \
115 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
116 fn(arg); \
118 INTERCEPT_WHEN_POSSIBLE(#name, name);
120 #define WRAP_V_WW(name) \
121 extern "C" void name(void *arg1, void *arg2) { \
122 typedef void (*fntype)(void *, void *); \
123 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
124 fn(arg1, arg2); \
126 INTERCEPT_WHEN_POSSIBLE(#name, name);
128 #define WRAP_V_WWW(name) \
129 extern "C" void name(void *arg1, void *arg2, void *arg3) { \
130 typedef void *(*fntype)(void *, void *, void *); \
131 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
132 fn(arg1, arg2, arg3); \
134 INTERCEPT_WHEN_POSSIBLE(#name, name);
136 #define WRAP_W_V(name) \
137 extern "C" void *name() { \
138 typedef void *(*fntype)(); \
139 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
140 return fn(); \
142 INTERCEPT_WHEN_POSSIBLE(#name, name);
144 #define WRAP_W_W(name) \
145 extern "C" void *name(void *arg) { \
146 typedef void *(*fntype)(void *arg); \
147 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
148 return fn(arg); \
150 INTERCEPT_WHEN_POSSIBLE(#name, name);
152 #define WRAP_W_WW(name) \
153 extern "C" void *name(void *arg1, void *arg2) { \
154 typedef void *(*fntype)(void *, void *); \
155 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
156 return fn(arg1, arg2); \
158 INTERCEPT_WHEN_POSSIBLE(#name, name);
160 #define WRAP_W_WWW(name) \
161 extern "C" void *name(void *arg1, void *arg2, void *arg3) { \
162 typedef void *(*fntype)(void *, void *, void *); \
163 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
164 return fn(arg1, arg2, arg3); \
166 INTERCEPT_WHEN_POSSIBLE(#name, name);
168 #define WRAP_W_WWWW(name) \
169 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4) { \
170 typedef void *(*fntype)(void *, void *, void *, void *); \
171 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
172 return fn(arg1, arg2, arg3, arg4); \
174 INTERCEPT_WHEN_POSSIBLE(#name, name);
176 #define WRAP_W_WWWWW(name) \
177 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
178 void *arg5) { \
179 typedef void *(*fntype)(void *, void *, void *, void *, void *); \
180 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
181 return fn(arg1, arg2, arg3, arg4, arg5); \
183 INTERCEPT_WHEN_POSSIBLE(#name, name);
185 #define WRAP_W_WWWWWW(name) \
186 extern "C" void *name(void *arg1, void *arg2, void *arg3, void *arg4, \
187 void *arg5, void *arg6) { \
188 typedef void *(*fntype)(void *, void *, void *, void *, void *, void *); \
189 static fntype fn = (fntype)getRealProcAddressOrDie(#name); \
190 return fn(arg1, arg2, arg3, arg4, arg5, arg6); \
192 INTERCEPT_WHEN_POSSIBLE(#name, name);
193 // }}}
195 // ----------------- ASan own interface functions --------------------
196 // Don't use the INTERFACE_FUNCTION machinery for this function as we actually
197 // want to call it in the __asan_init interceptor.
198 WRAP_W_V(__asan_should_detect_stack_use_after_return)
200 extern "C" {
201 int __asan_option_detect_stack_use_after_return;
203 // Manually wrap __asan_init as we need to initialize
204 // __asan_option_detect_stack_use_after_return afterwards.
205 void __asan_init() {
206 typedef void (*fntype)();
207 static fntype fn = 0;
208 // __asan_init is expected to be called by only one thread.
209 if (fn) return;
211 fn = (fntype)getRealProcAddressOrDie("__asan_init");
212 fn();
213 __asan_option_detect_stack_use_after_return =
214 (__asan_should_detect_stack_use_after_return() != 0);
216 InterceptHooks();
220 extern "C" void __asan_version_mismatch_check() {
221 // Do nothing.
224 INTERFACE_FUNCTION(__asan_handle_no_return)
226 INTERFACE_FUNCTION(__asan_report_store1)
227 INTERFACE_FUNCTION(__asan_report_store2)
228 INTERFACE_FUNCTION(__asan_report_store4)
229 INTERFACE_FUNCTION(__asan_report_store8)
230 INTERFACE_FUNCTION(__asan_report_store16)
231 INTERFACE_FUNCTION(__asan_report_store_n)
233 INTERFACE_FUNCTION(__asan_report_load1)
234 INTERFACE_FUNCTION(__asan_report_load2)
235 INTERFACE_FUNCTION(__asan_report_load4)
236 INTERFACE_FUNCTION(__asan_report_load8)
237 INTERFACE_FUNCTION(__asan_report_load16)
238 INTERFACE_FUNCTION(__asan_report_load_n)
240 INTERFACE_FUNCTION(__asan_store1)
241 INTERFACE_FUNCTION(__asan_store2)
242 INTERFACE_FUNCTION(__asan_store4)
243 INTERFACE_FUNCTION(__asan_store8)
244 INTERFACE_FUNCTION(__asan_store16)
245 INTERFACE_FUNCTION(__asan_storeN)
247 INTERFACE_FUNCTION(__asan_load1)
248 INTERFACE_FUNCTION(__asan_load2)
249 INTERFACE_FUNCTION(__asan_load4)
250 INTERFACE_FUNCTION(__asan_load8)
251 INTERFACE_FUNCTION(__asan_load16)
252 INTERFACE_FUNCTION(__asan_loadN)
254 INTERFACE_FUNCTION(__asan_memcpy);
255 INTERFACE_FUNCTION(__asan_memset);
256 INTERFACE_FUNCTION(__asan_memmove);
258 INTERFACE_FUNCTION(__asan_register_globals)
259 INTERFACE_FUNCTION(__asan_unregister_globals)
261 INTERFACE_FUNCTION(__asan_before_dynamic_init)
262 INTERFACE_FUNCTION(__asan_after_dynamic_init)
264 INTERFACE_FUNCTION(__asan_poison_stack_memory)
265 INTERFACE_FUNCTION(__asan_unpoison_stack_memory)
267 INTERFACE_FUNCTION(__asan_poison_memory_region)
268 INTERFACE_FUNCTION(__asan_unpoison_memory_region)
270 INTERFACE_FUNCTION(__asan_address_is_poisoned)
271 INTERFACE_FUNCTION(__asan_region_is_poisoned)
273 INTERFACE_FUNCTION(__asan_get_current_fake_stack)
274 INTERFACE_FUNCTION(__asan_addr_is_in_fake_stack)
276 INTERFACE_FUNCTION(__asan_stack_malloc_0)
277 INTERFACE_FUNCTION(__asan_stack_malloc_1)
278 INTERFACE_FUNCTION(__asan_stack_malloc_2)
279 INTERFACE_FUNCTION(__asan_stack_malloc_3)
280 INTERFACE_FUNCTION(__asan_stack_malloc_4)
281 INTERFACE_FUNCTION(__asan_stack_malloc_5)
282 INTERFACE_FUNCTION(__asan_stack_malloc_6)
283 INTERFACE_FUNCTION(__asan_stack_malloc_7)
284 INTERFACE_FUNCTION(__asan_stack_malloc_8)
285 INTERFACE_FUNCTION(__asan_stack_malloc_9)
286 INTERFACE_FUNCTION(__asan_stack_malloc_10)
288 INTERFACE_FUNCTION(__asan_stack_free_0)
289 INTERFACE_FUNCTION(__asan_stack_free_1)
290 INTERFACE_FUNCTION(__asan_stack_free_2)
291 INTERFACE_FUNCTION(__asan_stack_free_4)
292 INTERFACE_FUNCTION(__asan_stack_free_5)
293 INTERFACE_FUNCTION(__asan_stack_free_6)
294 INTERFACE_FUNCTION(__asan_stack_free_7)
295 INTERFACE_FUNCTION(__asan_stack_free_8)
296 INTERFACE_FUNCTION(__asan_stack_free_9)
297 INTERFACE_FUNCTION(__asan_stack_free_10)
299 // FIXME: we might want to have a sanitizer_win_dll_thunk?
300 INTERFACE_FUNCTION(__sanitizer_annotate_contiguous_container)
301 INTERFACE_FUNCTION(__sanitizer_cov)
302 INTERFACE_FUNCTION(__sanitizer_cov_dump)
303 INTERFACE_FUNCTION(__sanitizer_cov_indir_call16)
304 INTERFACE_FUNCTION(__sanitizer_cov_init)
305 INTERFACE_FUNCTION(__sanitizer_cov_module_init)
306 INTERFACE_FUNCTION(__sanitizer_cov_trace_basic_block)
307 INTERFACE_FUNCTION(__sanitizer_cov_trace_func_enter)
308 INTERFACE_FUNCTION(__sanitizer_cov_trace_cmp)
309 INTERFACE_FUNCTION(__sanitizer_cov_trace_switch)
310 INTERFACE_FUNCTION(__sanitizer_cov_with_check)
311 INTERFACE_FUNCTION(__sanitizer_get_allocated_size)
312 INTERFACE_FUNCTION(__sanitizer_get_coverage_guards)
313 INTERFACE_FUNCTION(__sanitizer_get_current_allocated_bytes)
314 INTERFACE_FUNCTION(__sanitizer_get_estimated_allocated_size)
315 INTERFACE_FUNCTION(__sanitizer_get_free_bytes)
316 INTERFACE_FUNCTION(__sanitizer_get_heap_size)
317 INTERFACE_FUNCTION(__sanitizer_get_ownership)
318 INTERFACE_FUNCTION(__sanitizer_get_total_unique_coverage)
319 INTERFACE_FUNCTION(__sanitizer_get_unmapped_bytes)
320 INTERFACE_FUNCTION(__sanitizer_maybe_open_cov_file)
321 INTERFACE_FUNCTION(__sanitizer_print_stack_trace)
322 INTERFACE_FUNCTION(__sanitizer_ptr_cmp)
323 INTERFACE_FUNCTION(__sanitizer_ptr_sub)
324 INTERFACE_FUNCTION(__sanitizer_report_error_summary)
325 INTERFACE_FUNCTION(__sanitizer_reset_coverage)
326 INTERFACE_FUNCTION(__sanitizer_get_number_of_counters)
327 INTERFACE_FUNCTION(__sanitizer_update_counter_bitset_and_clear_counters)
328 INTERFACE_FUNCTION(__sanitizer_sandbox_on_notify)
329 INTERFACE_FUNCTION(__sanitizer_set_death_callback)
330 INTERFACE_FUNCTION(__sanitizer_set_report_path)
331 INTERFACE_FUNCTION(__sanitizer_unaligned_load16)
332 INTERFACE_FUNCTION(__sanitizer_unaligned_load32)
333 INTERFACE_FUNCTION(__sanitizer_unaligned_load64)
334 INTERFACE_FUNCTION(__sanitizer_unaligned_store16)
335 INTERFACE_FUNCTION(__sanitizer_unaligned_store32)
336 INTERFACE_FUNCTION(__sanitizer_unaligned_store64)
337 INTERFACE_FUNCTION(__sanitizer_verify_contiguous_container)
339 // TODO(timurrrr): Add more interface functions on the as-needed basis.
341 // ----------------- Memory allocation functions ---------------------
342 WRAP_V_W(free)
343 WRAP_V_WW(_free_dbg)
345 WRAP_W_W(malloc)
346 WRAP_W_WWWW(_malloc_dbg)
348 WRAP_W_WW(calloc)
349 WRAP_W_WWWWW(_calloc_dbg)
350 WRAP_W_WWW(_calloc_impl)
352 WRAP_W_WW(realloc)
353 WRAP_W_WWW(_realloc_dbg)
354 WRAP_W_WWW(_recalloc)
356 WRAP_W_W(_msize)
357 WRAP_W_W(_expand)
358 WRAP_W_W(_expand_dbg)
360 // TODO(timurrrr): Might want to add support for _aligned_* allocation
361 // functions to detect a bit more bugs. Those functions seem to wrap malloc().
363 // TODO(timurrrr): Do we need to add _Crt* stuff here? (see asan_malloc_win.cc).
365 INTERCEPT_LIBRARY_FUNCTION(atoi);
366 INTERCEPT_LIBRARY_FUNCTION(atol);
367 INTERCEPT_LIBRARY_FUNCTION(_except_handler3);
369 // _except_handler4 checks -GS cookie which is different for each module, so we
370 // can't use INTERCEPT_LIBRARY_FUNCTION(_except_handler4).
371 INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
372 __asan_handle_no_return();
373 return REAL(_except_handler4)(a, b, c, d);
376 INTERCEPT_LIBRARY_FUNCTION(frexp);
377 INTERCEPT_LIBRARY_FUNCTION(longjmp);
378 INTERCEPT_LIBRARY_FUNCTION(memchr);
379 INTERCEPT_LIBRARY_FUNCTION(memcmp);
380 INTERCEPT_LIBRARY_FUNCTION(memcpy);
381 INTERCEPT_LIBRARY_FUNCTION(memmove);
382 INTERCEPT_LIBRARY_FUNCTION(memset);
383 INTERCEPT_LIBRARY_FUNCTION(strcat); // NOLINT
384 INTERCEPT_LIBRARY_FUNCTION(strchr);
385 INTERCEPT_LIBRARY_FUNCTION(strcmp);
386 INTERCEPT_LIBRARY_FUNCTION(strcpy); // NOLINT
387 INTERCEPT_LIBRARY_FUNCTION(strcspn);
388 INTERCEPT_LIBRARY_FUNCTION(strlen);
389 INTERCEPT_LIBRARY_FUNCTION(strncat);
390 INTERCEPT_LIBRARY_FUNCTION(strncmp);
391 INTERCEPT_LIBRARY_FUNCTION(strncpy);
392 INTERCEPT_LIBRARY_FUNCTION(strnlen);
393 INTERCEPT_LIBRARY_FUNCTION(strpbrk);
394 INTERCEPT_LIBRARY_FUNCTION(strspn);
395 INTERCEPT_LIBRARY_FUNCTION(strstr);
396 INTERCEPT_LIBRARY_FUNCTION(strtol);
397 INTERCEPT_LIBRARY_FUNCTION(wcslen);
399 // Must be after all the interceptor declarations due to the way INTERCEPT_HOOKS
400 // is defined.
401 void InterceptHooks() {
402 INTERCEPT_HOOKS();
403 INTERCEPT_FUNCTION(_except_handler4);
406 // We want to call __asan_init before C/C++ initializers/constructors are
407 // executed, otherwise functions like memset might be invoked.
408 // For some strange reason, merely linking in asan_preinit.cc doesn't work
409 // as the callback is never called... Is link.exe doing something too smart?
411 // In DLLs, the callbacks are expected to return 0,
412 // otherwise CRT initialization fails.
413 static int call_asan_init() {
414 __asan_init();
415 return 0;
417 #pragma section(".CRT$XIB", long, read) // NOLINT
418 __declspec(allocate(".CRT$XIB")) int (*__asan_preinit)() = call_asan_init;
420 #endif // ASAN_DLL_THUNK