11553Srgrimes//===-- sanitizer_win_weak_interception.cpp -------------------------------===//
21553Srgrimes//
31553Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41553Srgrimes// See https://llvm.org/LICENSE.txt for license information.
51553Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61553Srgrimes//
71553Srgrimes//===----------------------------------------------------------------------===//
81553Srgrimes// This module should be included in the sanitizer when it is implemented as a
91553Srgrimes// shared library on Windows (dll), in order to delegate the calls of weak
101553Srgrimes// functions to the implementation in the main executable when a strong
111553Srgrimes// definition is provided.
121553Srgrimes//===----------------------------------------------------------------------===//
13121300Sphk
141553Srgrimes#include "sanitizer_platform.h"
151553Srgrimes#if SANITIZER_WINDOWS && SANITIZER_DYNAMIC
161553Srgrimes#include "sanitizer_win_weak_interception.h"
171553Srgrimes#include "sanitizer_allocator_interface.h"
181553Srgrimes#include "sanitizer_interface_internal.h"
191553Srgrimes#include "sanitizer_win_defs.h"
201553Srgrimes#include "interception/interception.h"
211553Srgrimes
221553Srgrimesextern "C" {
231553Srgrimesvoid *WINAPI GetModuleHandleA(const char *module_name);
241553Srgrimesvoid abort();
251553Srgrimes}
261553Srgrimes
271553Srgrimesnamespace __sanitizer {
281553Srgrimes// Try to get a pointer to real_function in the main module and override
291553Srgrimes// dll_function with that pointer. If the function isn't found, nothing changes.
30114601Sobrienint interceptWhenPossible(uptr dll_function, const char *real_function) {
311553Srgrimes  uptr real = __interception::InternalGetProcAddress(
321553Srgrimes      (void *)GetModuleHandleA(0), real_function);
33114601Sobrien  if (real && !__interception::OverrideFunction((uptr)dll_function, real, 0))
3430027Scharnier    abort();
35114601Sobrien  return 0;
36114601Sobrien}
371553Srgrimes} // namespace __sanitizer
381553Srgrimes
391553Srgrimes// Declare weak hooks.
4030027Scharnierextern "C" {
4130027Scharniervoid __sanitizer_on_print(const char *str);
4230027Scharniervoid __sanitizer_weak_hook_memcmp(uptr called_pc, const void *s1,
431553Srgrimes                                  const void *s2, uptr n, int result);
4430027Scharniervoid __sanitizer_weak_hook_strcmp(uptr called_pc, const char *s1,
451553Srgrimes                                  const char *s2, int result);
4630027Scharniervoid __sanitizer_weak_hook_strncmp(uptr called_pc, const char *s1,
471553Srgrimes                                   const char *s2, uptr n, int result);
4842561Sjkoshyvoid __sanitizer_weak_hook_strstr(uptr called_pc, const char *s1,
491553Srgrimes                                  const char *s2, char *result);
501553Srgrimes}
511553Srgrimes
521553Srgrimes// Include Sanitizer Common interface.
531553Srgrimes#define INTERFACE_FUNCTION(Name)
5499800Salfred#define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name)
5599800Salfred#include "sanitizer_common_interface.inc"
561553Srgrimes
571553Srgrimes#pragma section(".WEAK$A", read)
58121299Sphk#pragma section(".WEAK$Z", read)
591553Srgrimes
60121299Sphktypedef void (*InterceptCB)();
61121299Sphkextern "C" {
621553Srgrimes__declspec(allocate(".WEAK$A")) InterceptCB __start_weak_list;
631553Srgrimes__declspec(allocate(".WEAK$Z")) InterceptCB __stop_weak_list;
641553Srgrimes}
651553Srgrimes
662860Srgrimesstatic int weak_intercept_init() {
671553Srgrimes  static bool flag = false;
681553Srgrimes  // weak_interception_init is expected to be called by only one thread.
691553Srgrimes  if (flag) return 0;
701553Srgrimes  flag = true;
711553Srgrimes
721553Srgrimes  for (InterceptCB *it = &__start_weak_list; it < &__stop_weak_list; ++it)
731553Srgrimes    if (*it)
741553Srgrimes      (*it)();
751553Srgrimes
761553Srgrimes  // In DLLs, the callbacks are expected to return 0,
7730027Scharnier  // otherwise CRT initialization fails.
781553Srgrimes  return 0;
791553Srgrimes}
801553Srgrimes
811553Srgrimes#pragma section(".CRT$XIB", long, read)
821553Srgrimes__declspec(allocate(".CRT$XIB")) int (*__weak_intercept_preinit)() =
831553Srgrimes    weak_intercept_init;
841553Srgrimes
851553Srgrimesstatic void WINAPI weak_intercept_thread_init(void *mod, unsigned long reason,
861553Srgrimes                                              void *reserved) {
871553Srgrimes  if (reason == /*DLL_PROCESS_ATTACH=*/1) weak_intercept_init();
881553Srgrimes}
891553Srgrimes
901553Srgrimes#pragma section(".CRT$XLAB", long, read)
911553Srgrimes__declspec(allocate(".CRT$XLAB")) void(WINAPI *__weak_intercept_tls_init)(
921553Srgrimes    void *, unsigned long, void *) = weak_intercept_thread_init;
931553Srgrimes
941553Srgrimes#endif // SANITIZER_WINDOWS && SANITIZER_DYNAMIC
951553Srgrimes