169 lines · c
1//===-- asan_interceptors.h -------------------------------------*- C++ -*-===//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// ASan-private header for asan_interceptors.cpp12//===----------------------------------------------------------------------===//13#ifndef ASAN_INTERCEPTORS_H14#define ASAN_INTERCEPTORS_H15 16#include "asan_interceptors_memintrinsics.h"17#include "asan_internal.h"18#include "interception/interception.h"19#include "sanitizer_common/sanitizer_platform.h"20#include "sanitizer_common/sanitizer_platform_interceptors.h"21 22namespace __asan {23 24void InitializeAsanInterceptors();25void InitializePlatformInterceptors();26 27} // namespace __asan28 29// There is no general interception at all on Fuchsia.30// Only the functions in asan_interceptors_memintrinsics.h are31// really defined to replace libc functions.32#if !SANITIZER_FUCHSIA33 34// Use macro to describe if specific function should be35// intercepted on a given platform.36#if !SANITIZER_WINDOWS37# define ASAN_INTERCEPT__LONGJMP 138# define ASAN_INTERCEPT_INDEX 139# define ASAN_INTERCEPT_PTHREAD_CREATE 140#else41# define ASAN_INTERCEPT__LONGJMP 042# define ASAN_INTERCEPT_INDEX 043# define ASAN_INTERCEPT_PTHREAD_CREATE 044#endif45 46#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \47 SANITIZER_SOLARIS48# define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 149#else50# define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 051#endif52 53#if SANITIZER_GLIBC || SANITIZER_SOLARIS54# define ASAN_INTERCEPT_SWAPCONTEXT 155#else56# define ASAN_INTERCEPT_SWAPCONTEXT 057#endif58 59#if !SANITIZER_WINDOWS60# define ASAN_INTERCEPT_SIGLONGJMP 161#else62# define ASAN_INTERCEPT_SIGLONGJMP 063#endif64 65#if SANITIZER_GLIBC66# define ASAN_INTERCEPT___LONGJMP_CHK 167#else68# define ASAN_INTERCEPT___LONGJMP_CHK 069#endif70 71#if ASAN_HAS_EXCEPTIONS && !SANITIZER_SOLARIS && !SANITIZER_NETBSD && \72 (!SANITIZER_WINDOWS || (defined(__MINGW32__) && defined(__i386__)))73# define ASAN_INTERCEPT___CXA_THROW 174# define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 175# if defined(_GLIBCXX_SJLJ_EXCEPTIONS) || (SANITIZER_IOS && defined(__arm__))76# define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 177# else78# define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 179# endif80#else81# define ASAN_INTERCEPT___CXA_THROW 082# define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 083# define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 084# define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 085#endif86 87#if !SANITIZER_WINDOWS88# define ASAN_INTERCEPT___CXA_ATEXIT 189#else90# define ASAN_INTERCEPT___CXA_ATEXIT 091#endif92 93#if SANITIZER_NETBSD94# define ASAN_INTERCEPT_ATEXIT 195#else96# define ASAN_INTERCEPT_ATEXIT 097#endif98 99#if SANITIZER_GLIBC100# define ASAN_INTERCEPT___STRDUP 1101#else102# define ASAN_INTERCEPT___STRDUP 0103#endif104 105#if SANITIZER_GLIBC && ASAN_INTERCEPT_PTHREAD_CREATE106# define ASAN_INTERCEPT_TIMEDJOIN 1107# define ASAN_INTERCEPT_TRYJOIN 1108#else109# define ASAN_INTERCEPT_TIMEDJOIN 0110# define ASAN_INTERCEPT_TRYJOIN 0111#endif112 113#if SANITIZER_LINUX && \114 (defined(__arm__) || defined(__aarch64__) || defined(__i386__) || \115 defined(__x86_64__) || SANITIZER_RISCV64 || SANITIZER_LOONGARCH64)116# define ASAN_INTERCEPT_VFORK 1117#else118# define ASAN_INTERCEPT_VFORK 0119#endif120 121#if SANITIZER_NETBSD122# define ASAN_INTERCEPT_PTHREAD_ATFORK 1123#else124# define ASAN_INTERCEPT_PTHREAD_ATFORK 0125#endif126 127DECLARE_REAL(int, memcmp, const void *a1, const void *a2, SIZE_T size)128DECLARE_REAL(char*, strchr, const char *str, int c)129DECLARE_REAL(SIZE_T, strlen, const char *s)130DECLARE_REAL(char*, strncpy, char *to, const char *from, SIZE_T size)131DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen)132DECLARE_REAL(SIZE_T, wcsnlen, const wchar_t* s, SIZE_T maxlen)133DECLARE_REAL(char*, strstr, const char *s1, const char *s2)134 135# if !SANITIZER_APPLE136# define ASAN_INTERCEPT_FUNC(name) \137 do { \138 if (!INTERCEPT_FUNCTION(name)) \139 VReport(1, "AddressSanitizer: failed to intercept '%s'\n", #name); \140 } while (0)141# define ASAN_INTERCEPT_FUNC_VER(name, ver) \142 do { \143 if (!INTERCEPT_FUNCTION_VER(name, ver)) \144 VReport(1, "AddressSanitizer: failed to intercept '%s@@%s'\n", \145 #name, ver); \146 } while (0)147# define ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \148 do { \149 if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \150 VReport(1, \151 "AddressSanitizer: failed to intercept '%s@@%s' or '%s'\n", \152 #name, ver, #name); \153 } while (0)154 155# else156// OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.157# define ASAN_INTERCEPT_FUNC(name)158# endif // SANITIZER_APPLE159 160#define ASAN_INTERCEPTOR_ENTER(ctx, func) \161 AsanInterceptorContext _ctx = {#func}; \162 ctx = (void *)&_ctx; \163 (void) ctx;164#define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name)165 166#endif // !SANITIZER_FUCHSIA167 168#endif // ASAN_INTERCEPTORS_H169