PR tree-optimization/84946
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_win_weak_interception.cc
blobdffec0c79438fbbb78e3d5c39392fa9dc7b5f1e0
1 //===-- sanitizer_win_weak_interception.cc --------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 // This module should be included in the sanitizer when it is implemented as a
8 // shared library on Windows (dll), in order to delegate the calls of weak
9 // functions to the implementation in the main executable when a strong
10 // definition is provided.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
14 #if SANITIZER_WINDOWS && SANITIZER_DYNAMIC
15 #include "sanitizer_win_weak_interception.h"
16 #include "sanitizer_allocator_interface.h"
17 #include "sanitizer_interface_internal.h"
18 #include "sanitizer_win_defs.h"
19 #include "interception/interception.h"
21 extern "C" {
22 void *WINAPI GetModuleHandleA(const char *module_name);
23 void abort();
26 namespace __sanitizer {
27 // Try to get a pointer to real_function in the main module and override
28 // dll_function with that pointer. If the function isn't found, nothing changes.
29 int interceptWhenPossible(uptr dll_function, const char *real_function) {
30 uptr real = __interception::InternalGetProcAddress(
31 (void *)GetModuleHandleA(0), real_function);
32 if (real && !__interception::OverrideFunction((uptr)dll_function, real, 0))
33 abort();
34 return 0;
36 } // namespace __sanitizer
38 // Declare weak hooks.
39 extern "C" {
40 void __sanitizer_weak_hook_memcmp(uptr called_pc, const void *s1,
41 const void *s2, uptr n, int result);
42 void __sanitizer_weak_hook_strcmp(uptr called_pc, const char *s1,
43 const char *s2, int result);
44 void __sanitizer_weak_hook_strncmp(uptr called_pc, const char *s1,
45 const char *s2, uptr n, int result);
46 void __sanitizer_weak_hook_strstr(uptr called_pc, const char *s1,
47 const char *s2, char *result);
50 // Include Sanitizer Common interface.
51 #define INTERFACE_FUNCTION(Name)
52 #define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
53 #include "sanitizer_common_interface.inc"
55 #pragma section(".WEAK$A", read) // NOLINT
56 #pragma section(".WEAK$Z", read) // NOLINT
58 typedef void (*InterceptCB)();
59 extern "C" {
60 __declspec(allocate(".WEAK$A")) InterceptCB __start_weak_list;
61 __declspec(allocate(".WEAK$Z")) InterceptCB __stop_weak_list;
64 static int weak_intercept_init() {
65 static bool flag = false;
66 // weak_interception_init is expected to be called by only one thread.
67 if (flag) return 0;
68 flag = true;
70 for (InterceptCB *it = &__start_weak_list; it < &__stop_weak_list; ++it)
71 if (*it)
72 (*it)();
74 // In DLLs, the callbacks are expected to return 0,
75 // otherwise CRT initialization fails.
76 return 0;
79 #pragma section(".CRT$XIB", long, read) // NOLINT
80 __declspec(allocate(".CRT$XIB")) int (*__weak_intercept_preinit)() =
81 weak_intercept_init;
83 static void WINAPI weak_intercept_thread_init(void *mod, unsigned long reason,
84 void *reserved) {
85 if (reason == /*DLL_PROCESS_ATTACH=*/1) weak_intercept_init();
88 #pragma section(".CRT$XLAB", long, read) // NOLINT
89 __declspec(allocate(".CRT$XLAB")) void(WINAPI *__weak_intercept_tls_init)(
90 void *, unsigned long, void *) = weak_intercept_thread_init;
92 #endif // SANITIZER_WINDOWS && SANITIZER_DYNAMIC