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