118 lines · cpp
1//===-- asan_win_static_runtime_thunk.cpp ---------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of AddressSanitizer, an address sanity checker.10//11// This file defines a family of thunks that should be statically linked into12// modules that are statically linked with the C Runtime in order to delegate13// the calls to the ASAN runtime DLL.14// See https://github.com/google/sanitizers/issues/209 for the details.15//===----------------------------------------------------------------------===//16 17#ifdef SANITIZER_STATIC_RUNTIME_THUNK18# include "asan_init_version.h"19# include "asan_interface_internal.h"20# include "asan_win_common_runtime_thunk.h"21# include "sanitizer_common/sanitizer_platform_interceptors.h"22# include "sanitizer_common/sanitizer_win_defs.h"23# include "sanitizer_common/sanitizer_win_thunk_interception.h"24 25# if defined(_MSC_VER) && !defined(__clang__)26// Disable warnings such as: 'void memchr(void)': incorrect number of arguments27// for intrinsic function, expected '3' arguments.28# pragma warning(push)29# pragma warning(disable : 4392)30# endif31 32# define INTERCEPT_LIBRARY_FUNCTION_ASAN(X) \33 INTERCEPT_LIBRARY_FUNCTION(X, "__asan_wrap_" #X)34 35INTERCEPT_LIBRARY_FUNCTION_ASAN(atoi);36INTERCEPT_LIBRARY_FUNCTION_ASAN(atol);37INTERCEPT_LIBRARY_FUNCTION_ASAN(atoll);38INTERCEPT_LIBRARY_FUNCTION_ASAN(frexp);39INTERCEPT_LIBRARY_FUNCTION_ASAN(longjmp);40# if SANITIZER_INTERCEPT_MEMCHR41INTERCEPT_LIBRARY_FUNCTION_ASAN(memchr);42# endif43INTERCEPT_LIBRARY_FUNCTION_ASAN(memcmp);44INTERCEPT_LIBRARY_FUNCTION_ASAN(memcpy);45# ifndef _WIN6446// memmove and memcpy share an implementation on amd6447INTERCEPT_LIBRARY_FUNCTION_ASAN(memmove);48# endif49INTERCEPT_LIBRARY_FUNCTION_ASAN(memset);50INTERCEPT_LIBRARY_FUNCTION_ASAN(strcat);51INTERCEPT_LIBRARY_FUNCTION_ASAN(strchr);52INTERCEPT_LIBRARY_FUNCTION_ASAN(strcmp);53INTERCEPT_LIBRARY_FUNCTION_ASAN(strcpy);54INTERCEPT_LIBRARY_FUNCTION_ASAN(strcspn);55INTERCEPT_LIBRARY_FUNCTION_ASAN(_strdup);56INTERCEPT_LIBRARY_FUNCTION_ASAN(strlen);57INTERCEPT_LIBRARY_FUNCTION_ASAN(strncat);58INTERCEPT_LIBRARY_FUNCTION_ASAN(strncmp);59INTERCEPT_LIBRARY_FUNCTION_ASAN(strncpy);60INTERCEPT_LIBRARY_FUNCTION_ASAN(strnlen);61INTERCEPT_LIBRARY_FUNCTION_ASAN(strpbrk);62// INTERCEPT_LIBRARY_FUNCTION_ASAN(strrchr);63INTERCEPT_LIBRARY_FUNCTION_ASAN(strspn);64INTERCEPT_LIBRARY_FUNCTION_ASAN(strstr);65INTERCEPT_LIBRARY_FUNCTION_ASAN(strtok);66INTERCEPT_LIBRARY_FUNCTION_ASAN(wcscat);67INTERCEPT_LIBRARY_FUNCTION_ASAN(wcscpy);68INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsncat);69INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsncpy);70INTERCEPT_LIBRARY_FUNCTION_ASAN(wcslen);71INTERCEPT_LIBRARY_FUNCTION_ASAN(wcsnlen);72 73// Note: Don't intercept strtol(l). They are supposed to set errno for out-of-74// range values, but since the ASan runtime is linked against the dynamic CRT,75// its errno is different from the one in the current module.76 77# if defined(_MSC_VER) && !defined(__clang__)78# pragma warning(pop)79# endif80 81# ifdef _WIN6482INTERCEPT_LIBRARY_FUNCTION_ASAN(__C_specific_handler);83# else84extern "C" void abort();85INTERCEPT_LIBRARY_FUNCTION_ASAN(_except_handler3);86// _except_handler4 checks -GS cookie which is different for each module, so we87// can't use INTERCEPT_LIBRARY_FUNCTION_ASAN(_except_handler4), need to apply88// manually89extern "C" int _except_handler4(void *, void *, void *, void *);90static int (*real_except_handler4)(void *, void *, void *,91 void *) = &_except_handler4;92static int intercept_except_handler4(void *a, void *b, void *c, void *d) {93 __asan_handle_no_return();94 return real_except_handler4(a, b, c, d);95}96# endif97 98// Windows specific functions not included in asan_interface.inc.99// INTERCEPT_WRAP_W_V(__asan_should_detect_stack_use_after_return)100// INTERCEPT_WRAP_W_V(__asan_get_shadow_memory_dynamic_address)101// INTERCEPT_WRAP_W_W(__asan_unhandled_exception_filter)102 103extern "C" void __asan_initialize_static_thunk() {104# ifndef _WIN64105 if (real_except_handler4 == &_except_handler4) {106 // Single threaded, no need for synchronization.107 if (!__sanitizer_override_function_by_addr(108 reinterpret_cast<__sanitizer::uptr>(&intercept_except_handler4),109 reinterpret_cast<__sanitizer::uptr>(&_except_handler4),110 reinterpret_cast<__sanitizer::uptr*>(&real_except_handler4))) {111 abort();112 }113 }114# endif115}116 117#endif // SANITIZER_DLL_THUNK118