d: Merge upstream dmd, druntime c8ae4adb2e, phobos 792c8b7c1.
[official-gcc.git] / libsanitizer / asan / asan_interceptors.h
blob9a6c22c764a9cc4af8ca1aa89f3dedb4c9d2ef92
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.
11 // ASan-private header for asan_interceptors.cpp
12 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_INTERCEPTORS_H
14 #define ASAN_INTERCEPTORS_H
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"
22 namespace __asan {
24 void InitializeAsanInterceptors();
25 void InitializePlatformInterceptors();
27 #define ENSURE_ASAN_INITED() \
28 do { \
29 CHECK(!asan_init_is_running); \
30 if (UNLIKELY(!asan_inited)) { \
31 AsanInitFromRtl(); \
32 } \
33 } while (0)
35 } // namespace __asan
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
42 // Use macro to describe if specific function should be
43 // intercepted on a given platform.
44 #if !SANITIZER_WINDOWS
45 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 1
46 # define ASAN_INTERCEPT__LONGJMP 1
47 # define ASAN_INTERCEPT_INDEX 1
48 # define ASAN_INTERCEPT_PTHREAD_CREATE 1
49 #else
50 # define ASAN_INTERCEPT_ATOLL_AND_STRTOLL 0
51 # define ASAN_INTERCEPT__LONGJMP 0
52 # define ASAN_INTERCEPT_INDEX 0
53 # define ASAN_INTERCEPT_PTHREAD_CREATE 0
54 #endif
56 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
57 SANITIZER_SOLARIS
58 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 1
59 #else
60 # define ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX 0
61 #endif
63 #if SANITIZER_GLIBC || SANITIZER_SOLARIS
64 # define ASAN_INTERCEPT_SWAPCONTEXT 1
65 #else
66 # define ASAN_INTERCEPT_SWAPCONTEXT 0
67 #endif
69 #if !SANITIZER_WINDOWS
70 # define ASAN_INTERCEPT_SIGLONGJMP 1
71 #else
72 # define ASAN_INTERCEPT_SIGLONGJMP 0
73 #endif
75 #if SANITIZER_GLIBC
76 # define ASAN_INTERCEPT___LONGJMP_CHK 1
77 #else
78 # define ASAN_INTERCEPT___LONGJMP_CHK 0
79 #endif
81 #if ASAN_HAS_EXCEPTIONS && !SANITIZER_WINDOWS && !SANITIZER_SOLARIS && \
82 !SANITIZER_NETBSD
83 # define ASAN_INTERCEPT___CXA_THROW 1
84 # if ! defined(ASAN_HAS_CXA_RETHROW_PRIMARY_EXCEPTION) \
85 || ASAN_HAS_CXA_RETHROW_PRIMARY_EXCEPTION
86 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 1
87 # else
88 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 0
89 # endif
90 # if defined(_GLIBCXX_SJLJ_EXCEPTIONS) || (SANITIZER_IOS && defined(__arm__))
91 # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 1
92 # else
93 # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 1
94 # endif
95 #else
96 # define ASAN_INTERCEPT___CXA_THROW 0
97 # define ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION 0
98 # define ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION 0
99 # define ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION 0
100 #endif
102 #if !SANITIZER_WINDOWS
103 # define ASAN_INTERCEPT___CXA_ATEXIT 1
104 #else
105 # define ASAN_INTERCEPT___CXA_ATEXIT 0
106 #endif
108 #if SANITIZER_NETBSD
109 # define ASAN_INTERCEPT_ATEXIT 1
110 #else
111 # define ASAN_INTERCEPT_ATEXIT 0
112 #endif
114 #if SANITIZER_GLIBC
115 # define ASAN_INTERCEPT___STRDUP 1
116 #else
117 # define ASAN_INTERCEPT___STRDUP 0
118 #endif
120 #if SANITIZER_LINUX && \
121 (defined(__arm__) || defined(__aarch64__) || defined(__i386__) || \
122 defined(__x86_64__) || SANITIZER_RISCV64 || SANITIZER_LOONGARCH64)
123 # define ASAN_INTERCEPT_VFORK 1
124 #else
125 # define ASAN_INTERCEPT_VFORK 0
126 #endif
128 #if SANITIZER_NETBSD
129 # define ASAN_INTERCEPT_PTHREAD_ATFORK 1
130 #else
131 # define ASAN_INTERCEPT_PTHREAD_ATFORK 0
132 #endif
134 DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size)
135 DECLARE_REAL(char*, strchr, const char *str, int c)
136 DECLARE_REAL(SIZE_T, strlen, const char *s)
137 DECLARE_REAL(char*, strncpy, char *to, const char *from, uptr size)
138 DECLARE_REAL(uptr, strnlen, const char *s, uptr maxlen)
139 DECLARE_REAL(char*, strstr, const char *s1, const char *s2)
141 # if !SANITIZER_APPLE
142 # define ASAN_INTERCEPT_FUNC(name) \
143 do { \
144 if (!INTERCEPT_FUNCTION(name)) \
145 VReport(1, "AddressSanitizer: failed to intercept '%s'\n", #name); \
146 } while (0)
147 # define ASAN_INTERCEPT_FUNC_VER(name, ver) \
148 do { \
149 if (!INTERCEPT_FUNCTION_VER(name, ver)) \
150 VReport(1, "AddressSanitizer: failed to intercept '%s@@%s'\n", \
151 #name, ver); \
152 } while (0)
153 # define ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) \
154 do { \
155 if (!INTERCEPT_FUNCTION_VER(name, ver) && !INTERCEPT_FUNCTION(name)) \
156 VReport(1, \
157 "AddressSanitizer: failed to intercept '%s@@%s' or '%s'\n", \
158 #name, ver, #name); \
159 } while (0)
161 # else
162 // OS X interceptors don't need to be initialized with INTERCEPT_FUNCTION.
163 # define ASAN_INTERCEPT_FUNC(name)
164 # endif // SANITIZER_APPLE
166 #endif // !SANITIZER_FUCHSIA
168 #endif // ASAN_INTERCEPTORS_H