]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_platform.h
MFV r337818:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_platform.h
1 //===-- sanitizer_platform.h ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Common platform macros.
11 //===----------------------------------------------------------------------===//
12
13 #ifndef SANITIZER_PLATFORM_H
14 #define SANITIZER_PLATFORM_H
15
16 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
17   !defined(__APPLE__) && !defined(_WIN32) && !defined(__Fuchsia__) && \
18   !(defined(__sun__) && defined(__svr4__))
19 # error "This operating system is not supported"
20 #endif
21
22 #if defined(__linux__)
23 # define SANITIZER_LINUX   1
24 #else
25 # define SANITIZER_LINUX   0
26 #endif
27
28 #if defined(__FreeBSD__)
29 # define SANITIZER_FREEBSD 1
30 #else
31 # define SANITIZER_FREEBSD 0
32 #endif
33
34 #if defined(__NetBSD__)
35 # define SANITIZER_NETBSD 1
36 #else
37 # define SANITIZER_NETBSD 0
38 #endif
39
40 #if defined(__sun__) && defined(__svr4__)
41 # define SANITIZER_SOLARIS 1
42 #else
43 # define SANITIZER_SOLARIS 0
44 #endif
45
46 #if defined(__APPLE__)
47 # define SANITIZER_MAC     1
48 # include <TargetConditionals.h>
49 # if TARGET_OS_IPHONE
50 #  define SANITIZER_IOS    1
51 # else
52 #  define SANITIZER_IOS    0
53 # endif
54 # if TARGET_OS_SIMULATOR
55 #  define SANITIZER_IOSSIM 1
56 # else
57 #  define SANITIZER_IOSSIM 0
58 # endif
59 #else
60 # define SANITIZER_MAC     0
61 # define SANITIZER_IOS     0
62 # define SANITIZER_IOSSIM  0
63 #endif
64
65 #if defined(__APPLE__) && TARGET_OS_IPHONE && TARGET_OS_WATCH
66 # define SANITIZER_WATCHOS 1
67 #else
68 # define SANITIZER_WATCHOS 0
69 #endif
70
71 #if defined(__APPLE__) && TARGET_OS_IPHONE && TARGET_OS_TV
72 # define SANITIZER_TVOS 1
73 #else
74 # define SANITIZER_TVOS 0
75 #endif
76
77 #if defined(_WIN32)
78 # define SANITIZER_WINDOWS 1
79 #else
80 # define SANITIZER_WINDOWS 0
81 #endif
82
83 #if defined(_WIN64)
84 # define SANITIZER_WINDOWS64 1
85 #else
86 # define SANITIZER_WINDOWS64 0
87 #endif
88
89 #if defined(__ANDROID__)
90 # define SANITIZER_ANDROID 1
91 #else
92 # define SANITIZER_ANDROID 0
93 #endif
94
95 #if defined(__Fuchsia__)
96 # define SANITIZER_FUCHSIA 1
97 #else
98 # define SANITIZER_FUCHSIA 0
99 #endif
100
101 #define SANITIZER_POSIX \
102   (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC || \
103     SANITIZER_NETBSD || SANITIZER_SOLARIS)
104
105 #if __LP64__ || defined(_WIN64)
106 #  define SANITIZER_WORDSIZE 64
107 #else
108 #  define SANITIZER_WORDSIZE 32
109 #endif
110
111 #if SANITIZER_WORDSIZE == 64
112 # define FIRST_32_SECOND_64(a, b) (b)
113 #else
114 # define FIRST_32_SECOND_64(a, b) (a)
115 #endif
116
117 #if defined(__x86_64__) && !defined(_LP64)
118 # define SANITIZER_X32 1
119 #else
120 # define SANITIZER_X32 0
121 #endif
122
123 #if defined(__mips__)
124 # define SANITIZER_MIPS 1
125 # if defined(__mips64)
126 #  define SANITIZER_MIPS32 0
127 #  define SANITIZER_MIPS64 1
128 # else
129 #  define SANITIZER_MIPS32 1
130 #  define SANITIZER_MIPS64 0
131 # endif
132 #else
133 # define SANITIZER_MIPS 0
134 # define SANITIZER_MIPS32 0
135 # define SANITIZER_MIPS64 0
136 #endif
137
138 #if defined(__s390__)
139 # define SANITIZER_S390 1
140 # if defined(__s390x__)
141 #  define SANITIZER_S390_31 0
142 #  define SANITIZER_S390_64 1
143 # else
144 #  define SANITIZER_S390_31 1
145 #  define SANITIZER_S390_64 0
146 # endif
147 #else
148 # define SANITIZER_S390 0
149 # define SANITIZER_S390_31 0
150 # define SANITIZER_S390_64 0
151 #endif
152
153 #if defined(__powerpc__)
154 # define SANITIZER_PPC 1
155 # if defined(__powerpc64__)
156 #  define SANITIZER_PPC32 0
157 #  define SANITIZER_PPC64 1
158 // 64-bit PPC has two ABIs (v1 and v2).  The old powerpc64 target is
159 // big-endian, and uses v1 ABI (known for its function descriptors),
160 // while the new powerpc64le target is little-endian and uses v2.
161 // In theory, you could convince gcc to compile for their evil twins
162 // (eg. big-endian v2), but you won't find such combinations in the wild
163 // (it'd require bootstrapping a whole system, which would be quite painful
164 // - there's no target triple for that).  LLVM doesn't support them either.
165 #  if _CALL_ELF == 2
166 #   define SANITIZER_PPC64V1 0
167 #   define SANITIZER_PPC64V2 1
168 #  else
169 #   define SANITIZER_PPC64V1 1
170 #   define SANITIZER_PPC64V2 0
171 #  endif
172 # else
173 #  define SANITIZER_PPC32 1
174 #  define SANITIZER_PPC64 0
175 #  define SANITIZER_PPC64V1 0
176 #  define SANITIZER_PPC64V2 0
177 # endif
178 #else
179 # define SANITIZER_PPC 0
180 # define SANITIZER_PPC32 0
181 # define SANITIZER_PPC64 0
182 # define SANITIZER_PPC64V1 0
183 # define SANITIZER_PPC64V2 0
184 #endif
185
186 #if defined(__arm__)
187 # define SANITIZER_ARM 1
188 #else
189 # define SANITIZER_ARM 0
190 #endif
191
192 #if SANITIZER_SOLARIS && SANITIZER_WORDSIZE == 32
193 # define SANITIZER_SOLARIS32 1
194 #else
195 # define SANITIZER_SOLARIS32 0
196 #endif
197
198 // By default we allow to use SizeClassAllocator64 on 64-bit platform.
199 // But in some cases (e.g. AArch64's 39-bit address space) SizeClassAllocator64
200 // does not work well and we need to fallback to SizeClassAllocator32.
201 // For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
202 // change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
203 #ifndef SANITIZER_CAN_USE_ALLOCATOR64
204 # if (SANITIZER_ANDROID && defined(__aarch64__)) || SANITIZER_FUCHSIA
205 #  define SANITIZER_CAN_USE_ALLOCATOR64 1
206 # elif defined(__mips64) || defined(__aarch64__)
207 #  define SANITIZER_CAN_USE_ALLOCATOR64 0
208 # else
209 #  define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64)
210 # endif
211 #endif
212
213 // The range of addresses which can be returned my mmap.
214 // FIXME: this value should be different on different platforms.  Larger values
215 // will still work but will consume more memory for TwoLevelByteMap.
216 #if defined(__mips__)
217 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 40)
218 #elif defined(__aarch64__)
219 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48)
220 #else
221 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
222 #endif
223
224 // The AArch64 linux port uses the canonical syscall set as mandated by
225 // the upstream linux community for all new ports. Other ports may still
226 // use legacy syscalls.
227 #ifndef SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
228 # if defined(__aarch64__) && SANITIZER_LINUX
229 # define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 1
230 # else
231 # define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 0
232 # endif
233 #endif
234
235 // udi16 syscalls can only be used when the following conditions are
236 // met:
237 // * target is one of arm32, x86-32, sparc32, sh or m68k
238 // * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15
239 //   built against > linux-2.2 kernel headers
240 // Since we don't want to include libc headers here, we check the
241 // target only.
242 #if defined(__arm__) || SANITIZER_X32 || defined(__sparc__)
243 #define SANITIZER_USES_UID16_SYSCALLS 1
244 #else
245 #define SANITIZER_USES_UID16_SYSCALLS 0
246 #endif
247
248 #if defined(__mips__)
249 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10)
250 #else
251 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
252 #endif
253
254 // Assume obsolete RPC headers are available by default
255 #if !defined(HAVE_RPC_XDR_H) && !defined(HAVE_TIRPC_RPC_XDR_H)
256 # define HAVE_RPC_XDR_H (SANITIZER_LINUX && !SANITIZER_ANDROID)
257 # define HAVE_TIRPC_RPC_XDR_H 0
258 #endif
259
260 /// \macro MSC_PREREQ
261 /// \brief Is the compiler MSVC of at least the specified version?
262 /// The common \param version values to check for are:
263 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
264 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
265 #ifdef _MSC_VER
266 # define MSC_PREREQ(version) (_MSC_VER >= (version))
267 #else
268 # define MSC_PREREQ(version) 0
269 #endif
270
271 #if defined(__arm64__) && SANITIZER_IOS
272 # define SANITIZER_NON_UNIQUE_TYPEINFO 1
273 #else
274 # define SANITIZER_NON_UNIQUE_TYPEINFO 0
275 #endif
276
277 // On linux, some architectures had an ABI transition from 64-bit long double
278 // (ie. same as double) to 128-bit long double.  On those, glibc symbols
279 // involving long doubles come in two versions, and we need to pass the
280 // correct one to dlvsym when intercepting them.
281 #if SANITIZER_LINUX && (SANITIZER_S390 || SANITIZER_PPC32 || SANITIZER_PPC64V1)
282 #define SANITIZER_NLDBL_VERSION "GLIBC_2.4"
283 #endif
284
285 #if SANITIZER_GO == 0
286 # define SANITIZER_GO 0
287 #endif
288
289 // On PowerPC and ARM Thumb, calling pthread_exit() causes LSan to detect leaks.
290 // pthread_exit() performs unwinding that leads to dlopen'ing libgcc_s.so.
291 // dlopen mallocs "libgcc_s.so" string which confuses LSan, it fails to realize
292 // that this allocation happens in dynamic linker and should be ignored.
293 #if SANITIZER_PPC || defined(__thumb__)
294 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 1
295 #else
296 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 0
297 #endif
298
299 #if SANITIZER_FREEBSD || SANITIZER_MAC || SANITIZER_NETBSD || SANITIZER_SOLARIS
300 # define SANITIZER_MADVISE_DONTNEED MADV_FREE
301 #else
302 # define SANITIZER_MADVISE_DONTNEED MADV_DONTNEED
303 #endif
304
305 #endif // SANITIZER_PLATFORM_H