1 //===-- sanitizer_win_weak_interception.cc --------------------------------===//
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 //===----------------------------------------------------------------------===//
8 // This module should be included in the sanitizer when it is implemented as a
9 // shared library on Windows (dll), in order to delegate the calls of weak
10 // functions to the implementation in the main executable when a strong
11 // definition is provided.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
15 #if SANITIZER_WINDOWS && SANITIZER_DYNAMIC
16 #include "sanitizer_win_weak_interception.h"
17 #include "sanitizer_allocator_interface.h"
18 #include "sanitizer_interface_internal.h"
19 #include "sanitizer_win_defs.h"
20 #include "interception/interception.h"
23 void *WINAPI GetModuleHandleA(const char *module_name);
27 namespace __sanitizer {
28 // Try to get a pointer to real_function in the main module and override
29 // dll_function with that pointer. If the function isn't found, nothing changes.
30 int interceptWhenPossible(uptr dll_function, const char *real_function) {
31 uptr real = __interception::InternalGetProcAddress(
32 (void *)GetModuleHandleA(0), real_function);
33 if (real && !__interception::OverrideFunction((uptr)dll_function, real, 0))
37 } // namespace __sanitizer
39 // Declare weak hooks.
41 void __sanitizer_weak_hook_memcmp(uptr called_pc, const void *s1,
42 const void *s2, uptr n, int result);
43 void __sanitizer_weak_hook_strcmp(uptr called_pc, const char *s1,
44 const char *s2, int result);
45 void __sanitizer_weak_hook_strncmp(uptr called_pc, const char *s1,
46 const char *s2, uptr n, int result);
47 void __sanitizer_weak_hook_strstr(uptr called_pc, const char *s1,
48 const char *s2, char *result);
51 // Include Sanitizer Common interface.
52 #define INTERFACE_FUNCTION(Name)
53 #define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
54 #include "sanitizer_common_interface.inc"
56 #pragma section(".WEAK$A", read) // NOLINT
57 #pragma section(".WEAK$Z", read) // NOLINT
59 typedef void (*InterceptCB)();
61 __declspec(allocate(".WEAK$A")) InterceptCB __start_weak_list;
62 __declspec(allocate(".WEAK$Z")) InterceptCB __stop_weak_list;
65 static int weak_intercept_init() {
66 static bool flag = false;
67 // weak_interception_init is expected to be called by only one thread.
71 for (InterceptCB *it = &__start_weak_list; it < &__stop_weak_list; ++it)
75 // In DLLs, the callbacks are expected to return 0,
76 // otherwise CRT initialization fails.
80 #pragma section(".CRT$XIB", long, read) // NOLINT
81 __declspec(allocate(".CRT$XIB")) int (*__weak_intercept_preinit)() =
84 static void WINAPI weak_intercept_thread_init(void *mod, unsigned long reason,
86 if (reason == /*DLL_PROCESS_ATTACH=*/1) weak_intercept_init();
89 #pragma section(".CRT$XLAB", long, read) // NOLINT
90 __declspec(allocate(".CRT$XLAB")) void(WINAPI *__weak_intercept_tls_init)(
91 void *, unsigned long, void *) = weak_intercept_thread_init;
93 #endif // SANITIZER_WINDOWS && SANITIZER_DYNAMIC