]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_platform.h
Merge compiler-rt trunk r366426, resolve conflicts, and add
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_platform.h
1 //===-- sanitizer_platform.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 // Common platform macros.
10 //===----------------------------------------------------------------------===//
11
12 #ifndef SANITIZER_PLATFORM_H
13 #define SANITIZER_PLATFORM_H
14
15 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
16   !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(_WIN32) && \
17   !defined(__Fuchsia__) && !defined(__rtems__) && \
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(__OpenBSD__)
41 # define SANITIZER_OPENBSD 1
42 #else
43 # define SANITIZER_OPENBSD 0
44 #endif
45
46 #if defined(__sun__) && defined(__svr4__)
47 # define SANITIZER_SOLARIS 1
48 #else
49 # define SANITIZER_SOLARIS 0
50 #endif
51
52 #if defined(__APPLE__)
53 # define SANITIZER_MAC     1
54 # include <TargetConditionals.h>
55 # if TARGET_OS_IPHONE
56 #  define SANITIZER_IOS    1
57 # else
58 #  define SANITIZER_IOS    0
59 # endif
60 # if TARGET_OS_SIMULATOR
61 #  define SANITIZER_IOSSIM 1
62 # else
63 #  define SANITIZER_IOSSIM 0
64 # endif
65 #else
66 # define SANITIZER_MAC     0
67 # define SANITIZER_IOS     0
68 # define SANITIZER_IOSSIM  0
69 #endif
70
71 #if defined(__APPLE__) && TARGET_OS_IPHONE && TARGET_OS_WATCH
72 # define SANITIZER_WATCHOS 1
73 #else
74 # define SANITIZER_WATCHOS 0
75 #endif
76
77 #if defined(__APPLE__) && TARGET_OS_IPHONE && TARGET_OS_TV
78 # define SANITIZER_TVOS 1
79 #else
80 # define SANITIZER_TVOS 0
81 #endif
82
83 #if defined(_WIN32)
84 # define SANITIZER_WINDOWS 1
85 #else
86 # define SANITIZER_WINDOWS 0
87 #endif
88
89 #if defined(_WIN64)
90 # define SANITIZER_WINDOWS64 1
91 #else
92 # define SANITIZER_WINDOWS64 0
93 #endif
94
95 #if defined(__ANDROID__)
96 # define SANITIZER_ANDROID 1
97 #else
98 # define SANITIZER_ANDROID 0
99 #endif
100
101 #if defined(__Fuchsia__)
102 # define SANITIZER_FUCHSIA 1
103 #else
104 # define SANITIZER_FUCHSIA 0
105 #endif
106
107 #if defined(__rtems__)
108 # define SANITIZER_RTEMS 1
109 #else
110 # define SANITIZER_RTEMS 0
111 #endif
112
113 #define SANITIZER_POSIX \
114   (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_MAC || \
115     SANITIZER_NETBSD || SANITIZER_OPENBSD || SANITIZER_SOLARIS)
116
117 #if __LP64__ || defined(_WIN64)
118 #  define SANITIZER_WORDSIZE 64
119 #else
120 #  define SANITIZER_WORDSIZE 32
121 #endif
122
123 #if SANITIZER_WORDSIZE == 64
124 # define FIRST_32_SECOND_64(a, b) (b)
125 #else
126 # define FIRST_32_SECOND_64(a, b) (a)
127 #endif
128
129 #if defined(__x86_64__) && !defined(_LP64)
130 # define SANITIZER_X32 1
131 #else
132 # define SANITIZER_X32 0
133 #endif
134
135 #if defined(__mips__)
136 # define SANITIZER_MIPS 1
137 # if defined(__mips64)
138 #  define SANITIZER_MIPS32 0
139 #  define SANITIZER_MIPS64 1
140 # else
141 #  define SANITIZER_MIPS32 1
142 #  define SANITIZER_MIPS64 0
143 # endif
144 #else
145 # define SANITIZER_MIPS 0
146 # define SANITIZER_MIPS32 0
147 # define SANITIZER_MIPS64 0
148 #endif
149
150 #if defined(__s390__)
151 # define SANITIZER_S390 1
152 # if defined(__s390x__)
153 #  define SANITIZER_S390_31 0
154 #  define SANITIZER_S390_64 1
155 # else
156 #  define SANITIZER_S390_31 1
157 #  define SANITIZER_S390_64 0
158 # endif
159 #else
160 # define SANITIZER_S390 0
161 # define SANITIZER_S390_31 0
162 # define SANITIZER_S390_64 0
163 #endif
164
165 #if defined(__powerpc__)
166 # define SANITIZER_PPC 1
167 # if defined(__powerpc64__)
168 #  define SANITIZER_PPC32 0
169 #  define SANITIZER_PPC64 1
170 // 64-bit PPC has two ABIs (v1 and v2).  The old powerpc64 target is
171 // big-endian, and uses v1 ABI (known for its function descriptors),
172 // while the new powerpc64le target is little-endian and uses v2.
173 // In theory, you could convince gcc to compile for their evil twins
174 // (eg. big-endian v2), but you won't find such combinations in the wild
175 // (it'd require bootstrapping a whole system, which would be quite painful
176 // - there's no target triple for that).  LLVM doesn't support them either.
177 #  if _CALL_ELF == 2
178 #   define SANITIZER_PPC64V1 0
179 #   define SANITIZER_PPC64V2 1
180 #  else
181 #   define SANITIZER_PPC64V1 1
182 #   define SANITIZER_PPC64V2 0
183 #  endif
184 # else
185 #  define SANITIZER_PPC32 1
186 #  define SANITIZER_PPC64 0
187 #  define SANITIZER_PPC64V1 0
188 #  define SANITIZER_PPC64V2 0
189 # endif
190 #else
191 # define SANITIZER_PPC 0
192 # define SANITIZER_PPC32 0
193 # define SANITIZER_PPC64 0
194 # define SANITIZER_PPC64V1 0
195 # define SANITIZER_PPC64V2 0
196 #endif
197
198 #if defined(__arm__)
199 # define SANITIZER_ARM 1
200 #else
201 # define SANITIZER_ARM 0
202 #endif
203
204 #if SANITIZER_SOLARIS && SANITIZER_WORDSIZE == 32
205 # define SANITIZER_SOLARIS32 1
206 #else
207 # define SANITIZER_SOLARIS32 0
208 #endif
209
210 #if defined(__myriad2__)
211 # define SANITIZER_MYRIAD2 1
212 #else
213 # define SANITIZER_MYRIAD2 0
214 #endif
215
216 // By default we allow to use SizeClassAllocator64 on 64-bit platform.
217 // But in some cases (e.g. AArch64's 39-bit address space) SizeClassAllocator64
218 // does not work well and we need to fallback to SizeClassAllocator32.
219 // For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or
220 // change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here.
221 #ifndef SANITIZER_CAN_USE_ALLOCATOR64
222 # if (SANITIZER_ANDROID && defined(__aarch64__)) || SANITIZER_FUCHSIA
223 #  define SANITIZER_CAN_USE_ALLOCATOR64 1
224 # elif defined(__mips64) || defined(__aarch64__)
225 #  define SANITIZER_CAN_USE_ALLOCATOR64 0
226 # else
227 #  define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64)
228 # endif
229 #endif
230
231 // The range of addresses which can be returned my mmap.
232 // FIXME: this value should be different on different platforms.  Larger values
233 // will still work but will consume more memory for TwoLevelByteMap.
234 #if defined(__mips__)
235 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 40)
236 #elif defined(__aarch64__)
237 # if SANITIZER_MAC
238 // Darwin iOS/ARM64 has a 36-bit VMA, 64GiB VM
239 #  define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 36)
240 # else
241 #  define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48)
242 # endif
243 #elif defined(__sparc__)
244 #define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 52)
245 #else
246 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47)
247 #endif
248
249 // Whether the addresses are sign-extended from the VMA range to the word.
250 // The SPARC64 Linux port implements this to split the VMA space into two
251 // non-contiguous halves with a huge hole in the middle.
252 #if defined(__sparc__) && SANITIZER_WORDSIZE == 64
253 #define SANITIZER_SIGN_EXTENDED_ADDRESSES 1
254 #else
255 #define SANITIZER_SIGN_EXTENDED_ADDRESSES 0
256 #endif
257
258 // The AArch64 linux port uses the canonical syscall set as mandated by
259 // the upstream linux community for all new ports. Other ports may still
260 // use legacy syscalls.
261 #ifndef SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
262 # if defined(__aarch64__) && SANITIZER_LINUX
263 # define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 1
264 # else
265 # define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 0
266 # endif
267 #endif
268
269 // udi16 syscalls can only be used when the following conditions are
270 // met:
271 // * target is one of arm32, x86-32, sparc32, sh or m68k
272 // * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15
273 //   built against > linux-2.2 kernel headers
274 // Since we don't want to include libc headers here, we check the
275 // target only.
276 #if defined(__arm__) || SANITIZER_X32 || defined(__sparc__)
277 #define SANITIZER_USES_UID16_SYSCALLS 1
278 #else
279 #define SANITIZER_USES_UID16_SYSCALLS 0
280 #endif
281
282 #if defined(__mips__)
283 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10)
284 #else
285 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12)
286 #endif
287
288 /// \macro MSC_PREREQ
289 /// \brief Is the compiler MSVC of at least the specified version?
290 /// The common \param version values to check for are:
291 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
292 ///  * 1900: Microsoft Visual Studio 2015 / 14.0
293 #ifdef _MSC_VER
294 # define MSC_PREREQ(version) (_MSC_VER >= (version))
295 #else
296 # define MSC_PREREQ(version) 0
297 #endif
298
299 #if SANITIZER_MAC && !(defined(__arm64__) && SANITIZER_IOS)
300 # define SANITIZER_NON_UNIQUE_TYPEINFO 0
301 #else
302 # define SANITIZER_NON_UNIQUE_TYPEINFO 1
303 #endif
304
305 // On linux, some architectures had an ABI transition from 64-bit long double
306 // (ie. same as double) to 128-bit long double.  On those, glibc symbols
307 // involving long doubles come in two versions, and we need to pass the
308 // correct one to dlvsym when intercepting them.
309 #if SANITIZER_LINUX && (SANITIZER_S390 || SANITIZER_PPC32 || SANITIZER_PPC64V1)
310 #define SANITIZER_NLDBL_VERSION "GLIBC_2.4"
311 #endif
312
313 #if SANITIZER_GO == 0
314 # define SANITIZER_GO 0
315 #endif
316
317 // On PowerPC and ARM Thumb, calling pthread_exit() causes LSan to detect leaks.
318 // pthread_exit() performs unwinding that leads to dlopen'ing libgcc_s.so.
319 // dlopen mallocs "libgcc_s.so" string which confuses LSan, it fails to realize
320 // that this allocation happens in dynamic linker and should be ignored.
321 #if SANITIZER_PPC || defined(__thumb__)
322 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 1
323 #else
324 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 0
325 #endif
326
327 #if SANITIZER_FREEBSD || SANITIZER_MAC || SANITIZER_NETBSD || \
328   SANITIZER_OPENBSD || SANITIZER_SOLARIS
329 # define SANITIZER_MADVISE_DONTNEED MADV_FREE
330 #else
331 # define SANITIZER_MADVISE_DONTNEED MADV_DONTNEED
332 #endif
333
334 // Older gcc have issues aligning to a constexpr, and require an integer.
335 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859 among others.
336 #if defined(__powerpc__) || defined(__powerpc64__)
337 # define SANITIZER_CACHE_LINE_SIZE 128
338 #else
339 # define SANITIZER_CACHE_LINE_SIZE 64
340 #endif
341
342 // Enable offline markup symbolizer for Fuchsia and RTEMS.
343 #if SANITIZER_FUCHSIA || SANITIZER_RTEMS
344 #define SANITIZER_SYMBOLIZER_MARKUP 1
345 #else
346 #define SANITIZER_SYMBOLIZER_MARKUP 0
347 #endif
348
349 // Enable ability to support sanitizer initialization that is
350 // compatible with the sanitizer library being loaded via
351 // `dlopen()`.
352 #if SANITIZER_MAC
353 #define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 1
354 #else
355 #define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 0
356 #endif
357
358 #endif // SANITIZER_PLATFORM_H