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