1*68d75effSDimitry Andric //===-- sanitizer_win_weak_interception.cpp -------------------------------===//
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*68d75effSDimitry Andric //
7*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
8*68d75effSDimitry Andric // This module should be included in the sanitizer when it is implemented as a
9*68d75effSDimitry Andric // shared library on Windows (dll), in order to delegate the calls of weak
10*68d75effSDimitry Andric // functions to the implementation in the main executable when a strong
11*68d75effSDimitry Andric // definition is provided.
12*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
13*68d75effSDimitry Andric 
14*68d75effSDimitry Andric #include "sanitizer_platform.h"
15*68d75effSDimitry Andric #if SANITIZER_WINDOWS && SANITIZER_DYNAMIC
16*68d75effSDimitry Andric #include "sanitizer_win_weak_interception.h"
17*68d75effSDimitry Andric #include "sanitizer_allocator_interface.h"
18*68d75effSDimitry Andric #include "sanitizer_interface_internal.h"
19*68d75effSDimitry Andric #include "sanitizer_win_defs.h"
20*68d75effSDimitry Andric #include "interception/interception.h"
21*68d75effSDimitry Andric 
22*68d75effSDimitry Andric extern "C" {
23*68d75effSDimitry Andric void *WINAPI GetModuleHandleA(const char *module_name);
24*68d75effSDimitry Andric void abort();
25*68d75effSDimitry Andric }
26*68d75effSDimitry Andric 
27*68d75effSDimitry Andric namespace __sanitizer {
28*68d75effSDimitry Andric // Try to get a pointer to real_function in the main module and override
29*68d75effSDimitry Andric // dll_function with that pointer. If the function isn't found, nothing changes.
interceptWhenPossible(uptr dll_function,const char * real_function)30*68d75effSDimitry Andric int interceptWhenPossible(uptr dll_function, const char *real_function) {
31*68d75effSDimitry Andric   uptr real = __interception::InternalGetProcAddress(
32*68d75effSDimitry Andric       (void *)GetModuleHandleA(0), real_function);
33*68d75effSDimitry Andric   if (real && !__interception::OverrideFunction((uptr)dll_function, real, 0))
34*68d75effSDimitry Andric     abort();
35*68d75effSDimitry Andric   return 0;
36*68d75effSDimitry Andric }
37*68d75effSDimitry Andric } // namespace __sanitizer
38*68d75effSDimitry Andric 
39*68d75effSDimitry Andric // Declare weak hooks.
40*68d75effSDimitry Andric extern "C" {
41*68d75effSDimitry Andric void __sanitizer_on_print(const char *str);
42*68d75effSDimitry Andric void __sanitizer_weak_hook_memcmp(uptr called_pc, const void *s1,
43*68d75effSDimitry Andric                                   const void *s2, uptr n, int result);
44*68d75effSDimitry Andric void __sanitizer_weak_hook_strcmp(uptr called_pc, const char *s1,
45*68d75effSDimitry Andric                                   const char *s2, int result);
46*68d75effSDimitry Andric void __sanitizer_weak_hook_strncmp(uptr called_pc, const char *s1,
47*68d75effSDimitry Andric                                    const char *s2, uptr n, int result);
48*68d75effSDimitry Andric void __sanitizer_weak_hook_strstr(uptr called_pc, const char *s1,
49*68d75effSDimitry Andric                                   const char *s2, char *result);
50*68d75effSDimitry Andric }
51*68d75effSDimitry Andric 
52*68d75effSDimitry Andric // Include Sanitizer Common interface.
53*68d75effSDimitry Andric #define INTERFACE_FUNCTION(Name)
54*68d75effSDimitry Andric #define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
55*68d75effSDimitry Andric #include "sanitizer_common_interface.inc"
56*68d75effSDimitry Andric 
57*68d75effSDimitry Andric #pragma section(".WEAK$A", read)
58*68d75effSDimitry Andric #pragma section(".WEAK$Z", read)
59*68d75effSDimitry Andric 
60*68d75effSDimitry Andric typedef void (*InterceptCB)();
61*68d75effSDimitry Andric extern "C" {
62*68d75effSDimitry Andric __declspec(allocate(".WEAK$A")) InterceptCB __start_weak_list;
63*68d75effSDimitry Andric __declspec(allocate(".WEAK$Z")) InterceptCB __stop_weak_list;
64*68d75effSDimitry Andric }
65*68d75effSDimitry Andric 
weak_intercept_init()66*68d75effSDimitry Andric static int weak_intercept_init() {
67*68d75effSDimitry Andric   static bool flag = false;
68*68d75effSDimitry Andric   // weak_interception_init is expected to be called by only one thread.
69*68d75effSDimitry Andric   if (flag) return 0;
70*68d75effSDimitry Andric   flag = true;
71*68d75effSDimitry Andric 
72*68d75effSDimitry Andric   for (InterceptCB *it = &__start_weak_list; it < &__stop_weak_list; ++it)
73*68d75effSDimitry Andric     if (*it)
74*68d75effSDimitry Andric       (*it)();
75*68d75effSDimitry Andric 
76*68d75effSDimitry Andric   // In DLLs, the callbacks are expected to return 0,
77*68d75effSDimitry Andric   // otherwise CRT initialization fails.
78*68d75effSDimitry Andric   return 0;
79*68d75effSDimitry Andric }
80*68d75effSDimitry Andric 
81*68d75effSDimitry Andric #pragma section(".CRT$XIB", long, read)
82*68d75effSDimitry Andric __declspec(allocate(".CRT$XIB")) int (*__weak_intercept_preinit)() =
83*68d75effSDimitry Andric     weak_intercept_init;
84*68d75effSDimitry Andric 
weak_intercept_thread_init(void * mod,unsigned long reason,void * reserved)85*68d75effSDimitry Andric static void WINAPI weak_intercept_thread_init(void *mod, unsigned long reason,
86*68d75effSDimitry Andric                                               void *reserved) {
87*68d75effSDimitry Andric   if (reason == /*DLL_PROCESS_ATTACH=*/1) weak_intercept_init();
88*68d75effSDimitry Andric }
89*68d75effSDimitry Andric 
90*68d75effSDimitry Andric #pragma section(".CRT$XLAB", long, read)
91*68d75effSDimitry Andric __declspec(allocate(".CRT$XLAB")) void(WINAPI *__weak_intercept_tls_init)(
92*68d75effSDimitry Andric     void *, unsigned long, void *) = weak_intercept_thread_init;
93*68d75effSDimitry Andric 
94*68d75effSDimitry Andric #endif // SANITIZER_WINDOWS && SANITIZER_DYNAMIC
95