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