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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of AddressSanitizer, an address sanity checker. 10 // 11 // ASan-private header for asan_interceptors.cpp 12 //===----------------------------------------------------------------------===// 13 #ifndef ASAN_INTERCEPTORS_H 14 #define ASAN_INTERCEPTORS_H 15 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 22 namespace __asan { 23 24 void InitializeAsanInterceptors(); 25 void InitializePlatformInterceptors(); 26 27 #define ENSURE_ASAN_INITED() \ 28 do { \ 29 CHECK(!asan_init_is_running); \ 30 if (UNLIKELY(!asan_inited)) { \ 31 AsanInitFromRtl(); \ 32 } \ 33 } while (0) 34 35 } // namespace __asan 36 37 // There is no general interception at all on Fuchsia. 38 // Only the functions in asan_interceptors_memintrinsics.h are 39 // really defined to replace libc functions. 40 #if !SANITIZER_FUCHSIA 41 42 // Use macro to describe if specific function should be 43 // intercepted on a given platform. 44 #if !SANITIZER_WINDOWS 45 # define ASAN_INTERCEPT__LONGJMP 1 46 # define ASAN_INTERCEPT_INDEX 1 47 # define ASAN_INTERCEPT_PTHREAD_CREATE 1 48 #else 49 # define ASAN_INTERCEPT__LONGJMP 0 50 # define ASAN_INTERCEPT_INDEX 0 51 # define ASAN_INTERCEPT_PTHREAD_CREATE 0 52 #endif 53 54 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ 55 SANITIZER_SOLARIS 56 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1 57 #else 58 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0 59 #endif 60 61 #if SANITIZER_GLIBC || SANITIZER_SOLARIS 62 # define ASAN_INTERCEPT_SWAPCONTEXT 1 63 #else 64 # define ASAN_INTERCEPT_SWAPCONTEXT 0 65 #endif 66 67 #if !SANITIZER_WINDOWS 68 # define ASAN_INTERCEPT_SIGLONGJMP 1 69 #else 70 # define ASAN_INTERCEPT_SIGLONGJMP 0 71 #endif 72 73 #if SANITIZER_GLIBC 74 # define ASAN_INTERCEPT___LONGJMP_CHK 1 75 #else 76 # define ASAN_INTERCEPT___LONGJMP_CHK 0 77 #endif 78 79 #if ASAN_HAS_EXCEPTIONS && !SANITIZER_SOLARIS && !SANITIZER_NETBSD && \ 80 (!SANITIZER_WINDOWS || (defined(__MINGW32__) && defined(__i386__))) 81 # define ASAN_INTERCEPT___CXA_THROW 1 82 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 1 83 # if defined(_GLIBCXX_SJLJ_EXCEPTIONS) || (SANITIZER_IOS && defined(__arm__)) 84 # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 1 85 # else 86 # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 1 87 # endif 88 #else 89 # define ASAN_INTERCEPT___CXA_THROW 0 90 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 0 91 # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 0 92 # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 0 93 #endif 94 95 #if !SANITIZER_WINDOWS 96 # define ASAN_INTERCEPT___CXA_ATEXIT 1 97 #else 98 # define ASAN_INTERCEPT___CXA_ATEXIT 0 99 #endif 100 101 #if SANITIZER_NETBSD 102 # define ASAN_INTERCEPT_ATEXIT 1 103 #else 104 # define ASAN_INTERCEPT_ATEXIT 0 105 #endif 106 107 #if SANITIZER_GLIBC 108 # define ASAN_INTERCEPT___STRDUP 1 109 #else 110 # define ASAN_INTERCEPT___STRDUP 0 111 #endif 112 113 #if SANITIZER_GLIBC && ASAN_INTERCEPT_PTHREAD_CREATE 114 # define ASAN_INTERCEPT_TIMEDJOIN 1 115 # define ASAN_INTERCEPT_TRYJOIN 1 116 #else 117 # define ASAN_INTERCEPT_TIMEDJOIN 0 118 # define ASAN_INTERCEPT_TRYJOIN 0 119 #endif 120 121 #if SANITIZER_LINUX && \ 122 (defined(__arm__) || defined(__aarch64__) || defined(__i386__) || \ 123 defined(__x86_64__) || SANITIZER_RISCV64 || SANITIZER_LOONGARCH64) 124 # define ASAN_INTERCEPT_VFORK 1 125 #else 126 # define ASAN_INTERCEPT_VFORK 0 127 #endif 128 129 #if SANITIZER_NETBSD 130 # define ASAN_INTERCEPT_PTHREAD_ATFORK 1 131 #else 132 # define ASAN_INTERCEPT_PTHREAD_ATFORK 0 133 #endif 134 135 DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size) 136 DECLARE_REAL(char*, strchr, const char *str, int c) 137 DECLARE_REAL(SIZE_T, strlen, const char *s) 138 DECLARE_REAL(char*, strncpy, char *to, const char *from, uptr size) 139 DECLARE_REAL(uptr, strnlen, const char *s, uptr maxlen) 140 DECLARE_REAL(char*, strstr, const char *s1, const char *s2) 141 142 # if !SANITIZER_APPLE 143 # define ASAN_INTERCEPT_FUNC(name) \ 144 do { \ 145 if (!INTERCEPT_FUNCTION(name)) \ 146 VReport(1, "AddressSanitizer: failed to intercept '%s'\n", #name); \ 147 } while (0) 148 # define ASAN_INTERCEPT_FUNC_VER(name, ver) \ 149 do { \ 150 if (!INTERCEPT_FUNCTION_VER(name, ver)) \ 151 VReport(1, "AddressSanitizer: failed to intercept '%s@@%s'\n", \ 152 #name, ver); \ 153 } while (0) 154 # define ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \ 155 do { \ 156 if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \ 157 VReport(1, \ 158 "AddressSanitizer: failed to intercept '%s@@%s' or '%s'\n", \ 159 #name, ver, #name); \ 160 } while (0) 161 162 # else 163 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION. 164 # define ASAN_INTERCEPT_FUNC(name) 165 # endif // SANITIZER_APPLE 166 167 #define ASAN_INTERCEPTOR_ENTER(ctx, func) \ 168 AsanInterceptorContext _ctx = {#func}; \ 169 ctx = (void *)&_ctx; \ 170 (void) ctx; 171 #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name) 172 173 #endif // !SANITIZER_FUCHSIA 174 175 #endif // ASAN_INTERCEPTORS_H 176