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