1 //===-- asan_win_static_runtime_thunk.cpp ---------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // This file defines a family of thunks that should be statically linked into
12 // modules that are statically linked with the C Runtime in order to delegate
13 // the calls to the ASAN runtime DLL.
14 // See https://github.com/google/sanitizers/issues/209 for the details.
15 //===----------------------------------------------------------------------===//
17 #ifdef SANITIZER_STATIC_RUNTIME_THUNK
18 # include "asan_init_version.h"
19 # include "asan_interface_internal.h"
20 # include "asan_win_common_runtime_thunk.h"
21 # include "sanitizer_common/sanitizer_platform_interceptors.h"
22 # include "sanitizer_common/sanitizer_win_defs.h"
23 # include "sanitizer_common/sanitizer_win_thunk_interception.h"
25 # if defined(_MSC_VER) && !defined(__clang__)
26 // Disable warnings such as: 'void memchr(void)': incorrect number of arguments
27 // for intrinsic function, expected '3' arguments.
28 # pragma warning(push)
29 # pragma warning(disable : 4392)
32 # define INTERCEPT_LIBRARY_FUNCTION_ASAN(X) \
33 INTERCEPT_LIBRARY_FUNCTION(X, "__asan_wrap_" #X)
35 INTERCEPT_LIBRARY_FUNCTION_ASAN(atoi
);
36 INTERCEPT_LIBRARY_FUNCTION_ASAN(atol
);
37 INTERCEPT_LIBRARY_FUNCTION_ASAN(atoll
);
38 INTERCEPT_LIBRARY_FUNCTION_ASAN(frexp
);
39 INTERCEPT_LIBRARY_FUNCTION_ASAN(longjmp
);
40 # if SANITIZER_INTERCEPT_MEMCHR
41 INTERCEPT_LIBRARY_FUNCTION_ASAN(memchr
);
43 INTERCEPT_LIBRARY_FUNCTION_ASAN(memcmp
);
44 INTERCEPT_LIBRARY_FUNCTION_ASAN(memcpy
);
46 // memmove and memcpy share an implementation on amd64
47 INTERCEPT_LIBRARY_FUNCTION_ASAN(memmove
);
49 INTERCEPT_LIBRARY_FUNCTION_ASAN(memset
);
50 INTERCEPT_LIBRARY_FUNCTION_ASAN(strcat
);
51 INTERCEPT_LIBRARY_FUNCTION_ASAN(strchr
);
52 INTERCEPT_LIBRARY_FUNCTION_ASAN(strcmp
);
53 INTERCEPT_LIBRARY_FUNCTION_ASAN(strcpy
);
54 INTERCEPT_LIBRARY_FUNCTION_ASAN(strcspn
);
55 INTERCEPT_LIBRARY_FUNCTION_ASAN(_strdup
);
56 INTERCEPT_LIBRARY_FUNCTION_ASAN(strlen
);
57 INTERCEPT_LIBRARY_FUNCTION_ASAN(strncat
);
58 INTERCEPT_LIBRARY_FUNCTION_ASAN(strncmp
);
59 INTERCEPT_LIBRARY_FUNCTION_ASAN(strncpy
);
60 INTERCEPT_LIBRARY_FUNCTION_ASAN(strnlen
);
61 INTERCEPT_LIBRARY_FUNCTION_ASAN(strpbrk
);
62 // INTERCEPT_LIBRARY_FUNCTION_ASAN(strrchr);
63 INTERCEPT_LIBRARY_FUNCTION_ASAN(strspn
);
64 INTERCEPT_LIBRARY_FUNCTION_ASAN(strstr
);
65 INTERCEPT_LIBRARY_FUNCTION_ASAN(strtok
);
66 INTERCEPT_LIBRARY_FUNCTION_ASAN(wcslen
);
67 INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsnlen
);
69 // Note: Don't intercept strtol(l). They are supposed to set errno for out-of-
70 // range values, but since the ASan runtime is linked against the dynamic CRT,
71 // its errno is different from the one in the current module.
73 # if defined(_MSC_VER) && !defined(__clang__)
78 INTERCEPT_LIBRARY_FUNCTION_ASAN(__C_specific_handler
);
80 extern "C" void abort();
81 INTERCEPT_LIBRARY_FUNCTION_ASAN(_except_handler3
);
82 // _except_handler4 checks -GS cookie which is different for each module, so we
83 // can't use INTERCEPT_LIBRARY_FUNCTION_ASAN(_except_handler4), need to apply
85 extern "C" int _except_handler4(void *, void *, void *, void *);
86 static int (*real_except_handler4
)(void *, void *, void *,
87 void *) = &_except_handler4
;
88 static int intercept_except_handler4(void *a
, void *b
, void *c
, void *d
) {
89 __asan_handle_no_return();
90 return real_except_handler4(a
, b
, c
, d
);
94 // Windows specific functions not included in asan_interface.inc.
95 // INTERCEPT_WRAP_W_V(__asan_should_detect_stack_use_after_return)
96 // INTERCEPT_WRAP_W_V(__asan_get_shadow_memory_dynamic_address)
97 // INTERCEPT_WRAP_W_W(__asan_unhandled_exception_filter)
99 extern "C" void __asan_initialize_static_thunk() {
101 if (real_except_handler4
== &_except_handler4
) {
102 // Single threaded, no need for synchronization.
103 if (!__sanitizer_override_function_by_addr(
104 reinterpret_cast<__sanitizer::uptr
>(&intercept_except_handler4
),
105 reinterpret_cast<__sanitizer::uptr
>(&_except_handler4
),
106 reinterpret_cast<__sanitizer::uptr
*>(&real_except_handler4
))) {
113 #endif // SANITIZER_DLL_THUNK